Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1799,15 +1799,20 @@ target_compile_features(startup_storage_root_check PRIVATE cxx_std_17)
add_test(NAME startup_storage_root COMMAND startup_storage_root_check)
add_test(NAME startup_storage_root_separator_control COMMAND startup_storage_root_check --mutate separator)
set_tests_properties(startup_storage_root_separator_control PROPERTIES WILL_FAIL TRUE)
# Stand-in format for 16-bit packed targets the adapter cannot use (src/startup_stubs/packed_target_format.h).
# Stand-in formats for packed targets the adapter cannot use (src/startup_stubs/packed_target_format.h).
add_executable(startup_packed_target_check tests/startup_packed_target_check.cpp)
target_include_directories(startup_packed_target_check PRIVATE src)
target_compile_features(startup_packed_target_check PRIVATE cxx_std_17)
add_test(NAME startup_packed_target COMMAND startup_packed_target_check)
foreach(mutation IN ITEMS stand-in array b565 view tag)
foreach(mutation IN ITEMS stand-in array b565 sharedexp sharedexp-uav view tag)
add_test(NAME startup_packed_target_${mutation}_control COMMAND startup_packed_target_check --mutate ${mutation})
set_tests_properties(startup_packed_target_${mutation}_control PROPERTIES WILL_FAIL TRUE)
endforeach()
target_sources(offscreen_check PRIVATE tests/startup_sharedexp_target.cpp)
foreach(adapter IN ITEMS warp hardware)
add_test(NAME startup_sharedexp_target_${adapter} COMMAND offscreen_check ${adapter} --startup-sharedexp-target)
set_tests_properties(startup_sharedexp_target_${adapter} PROPERTIES TIMEOUT 45 SKIP_RETURN_CODE 77)
endforeach()
# Static samplers built from smp and smpc manifest lines (src/startup_stubs/translated_sampler_bindings.h).
add_executable(startup_sampler_comparison_check tests/startup_sampler_comparison_check.cpp)
target_include_directories(startup_sampler_comparison_check PRIVATE src)
Expand Down
14 changes: 14 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ to check RGBA8 texel and alpha values, rebinding, and a mismatched 2D view on ha
ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_buffer_store$' --output-on-failure
```

## Shared-exponent target stand-in

`startup_packed_target` checks the selection rule with synthetic format support
and verifies view compatibility and tags. `startup_sharedexp_target_*` creates
RTV/UAV textures through the startup device as one 2D texture and one 2D array.
Both name format 67 in their RTV, UAV and SRV. The fixture checks format 26 and
the format-67 tag, then compares raw texels from an RTV clear and a partial UAV
clear. The UAV colour's blue component of `996.0` stores as `992.0` in format 26
and is exactly representable in format 67.

```powershell
ctest --preset dev --no-tests=error -R '^(startup_(packed_target|sharedexp_target).*)$' -V
```

## Offline store context forwarding

The fake-user runtime creates an offline store context token. Unimplemented methods receiving
Expand Down
22 changes: 22 additions & 0 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,28 @@ Checks:
and never substituted; a packed view of a plain R8G8B8A8_UNORM texture and a
B5G5R5A1 view of a B4G4R4A4 target must be refused.

## Shared-exponent render and unordered-access targets

The packed-target mapping also covers single-sample 2D textures and arrays in
`R9G9B9E5_SHAREDEXP` (67) that request a render target, a typed UAV, or both.
When the adapter lacks one of the requested uses and supports all of them for
`R11G11B10_FLOAT` (26), the stand-in creates format 26. Sampled-only resources,
depth-stencil, multisampled textures and other dimensions keep format 67.
The format query also preserves native format 67 where it supports the requested
uses. [Microsoft Direct3D format specification](https://microsoft.github.io/DirectX-Specs/d3d/D3D12R9G9B9E5Format.html)

The stand-in carries a format-67 tag. RTVs, UAVs and SRVs naming 67 on that
resource use format 26, including SRVs reconstructed by the translated draw.
An optimized clear value naming 67 follows the replacement format. A format-67
view on a native format-67 texture is passed through. A format-67 view on an
incompatible texture is refused.

Both formats use 32 bits per texel and clamp negative values to zero.
Format 26 has 6/6/5 mantissa bits with a separate exponent per channel.
Format 67 has three 9-bit mantissas with a shared exponent. Rendered and
UAV-written values can round differently. Uploads and copies in the format-67
bit layout are not converted.

## Descriptor heap starts that are not addresses

A console executable treats the CPU start of a descriptor heap as an address. A desktop handle is
Expand Down
7 changes: 4 additions & 3 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2010,13 +2010,14 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI
if (texture && x2w_startup::packed_target_format(desc.Format)) {
// The view recorded from the title names a packed format: follow a stand-in texture,
// and bind nothing for a texture the view does not fit (packed_target_format.h).
const auto verdict = x2w_startup::packed_view(desc.Format, texture->GetDesc().Format, x2w_startup::packed_stand_in_of(texture));
if (verdict == x2w_startup::PackedView::stand_in) desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
const auto actual = texture->GetDesc().Format;
const auto verdict = x2w_startup::packed_view(desc.Format, actual, x2w_startup::packed_stand_in_of(texture));
if (verdict == x2w_startup::PackedView::stand_in) desc.Format = actual;
if (verdict == x2w_startup::PackedView::refused) {
static LONG refused;
if (first_calls(refused, 6))
log("x2w-draw %016llx srv %u: packed format %d on a texture of format %d that does not stand in for it; binding null",
tr->key, i, desc.Format, texture->GetDesc().Format);
tr->key, i, desc.Format, actual);
texture = nullptr;
desc = {};
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
Expand Down
43 changes: 34 additions & 9 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ D3D12_RESOURCE_DESC sanitized(const D3D12_RESOURCE_DESC& desc)
}
if (desc.Alignment != 0 && desc.Alignment != 0x10000 && desc.Alignment != 0x400000) copy.Alignment = 0;
copy.Format = desktop_format(desc.Format);
// A 16-bit packed target the adapter cannot render to or write as a UAV: packed_target_format.h.
// A packed target the adapter cannot render to or write as a UAV: packed_target_format.h.
if (the_device && x2w_startup::packed_target_format(copy.Format)) {
ID3D12Device* const device = static_cast<ID3D12Device*>(the_device->own().real);
const DXGI_FORMAT stand_in = x2w_startup::packed_target_stand_in(copy, [device](DXGI_FORMAT format) -> UINT {
Expand All @@ -767,6 +767,19 @@ D3D12_RESOURCE_DESC sanitized(const D3D12_RESOURCE_DESC& desc)
return copy;
}

// An optimized clear belongs to the actual texture format, including when the
// wrapper substitutes it. Preserve the caller's clear colour.
const D3D12_CLEAR_VALUE* packed_clear_value(const D3D12_CLEAR_VALUE* clear, DXGI_FORMAT requested,
DXGI_FORMAT created, D3D12_CLEAR_VALUE& copy)
{
const auto* mapping = x2w_startup::packed_target_mapping(requested);
if (!clear || !mapping || created != mapping->replacement || desktop_format(clear->Format) != requested)
return clear;
copy = *clear;
copy.Format = created;
return &copy;
}

bool release_retired_enabled()
{
static const bool on = x2w_local::env_on("X2W_D3D12_RELEASE_RETIRED");
Expand All @@ -793,11 +806,15 @@ __declspec(noinline) u64 create_slot(Wrapper* self, u64 a, u64 b, u64 c, u64 d,
u64 args[8] = {a, b, c, d, e, f, g, h};
for (int i = 0; i < R; ++i) args[i] = unwrap(args[i]);
D3D12_RESOURCE_DESC desc_copy{};
D3D12_CLEAR_VALUE clear_copy{};
DXGI_FORMAT requested_format = DXGI_FORMAT_UNKNOWN;
if (Desc >= 0 && args[Desc]) {
requested_format = desktop_format(reinterpret_cast<const D3D12_RESOURCE_DESC*>(args[Desc])->Format);
desc_copy = sanitized(*reinterpret_cast<const D3D12_RESOURCE_DESC*>(args[Desc]));
args[Desc] = reinterpret_cast<u64>(&desc_copy);
if constexpr (K == device_kind && (N == 27 || N == 29))
args[4] = reinterpret_cast<u64>(packed_clear_value(reinterpret_cast<const D3D12_CLEAR_VALUE*>(args[4]),
requested_format, desc_copy.Format, clear_copy));
}
const u64 result = method_of(self->own().real, N)(self->own().real, args[0], args[1], args[2], args[3], args[4], args[5],
args[6], args[7]);
Expand Down Expand Up @@ -3882,26 +3899,30 @@ __declspec(noinline) u64 view_slot(Wrapper* self, u64 a, u64 b, u64 c, u64 d)
return 0;
}
// The view descriptions of slots 18-21 start with a DXGI_FORMAT.
unsigned char desc_copy[64];
alignas(D3D12_SHADER_RESOURCE_VIEW_DESC) unsigned char desc_copy[64];
const int desc_index = N == 19 ? 2 : 1;
if (N >= 18 && N <= 21 && args[desc_index] && x2w_local::readable(reinterpret_cast<const void*>(args[desc_index]), sizeof(desc_copy))) {
std::memcpy(desc_copy, reinterpret_cast<const void*>(args[desc_index]), sizeof(desc_copy));
constexpr std::size_t view_bytes = N == 18 ? sizeof(D3D12_SHADER_RESOURCE_VIEW_DESC) :
N == 19 ? sizeof(D3D12_UNORDERED_ACCESS_VIEW_DESC) :
N == 20 ? sizeof(D3D12_RENDER_TARGET_VIEW_DESC) : sizeof(D3D12_DEPTH_STENCIL_VIEW_DESC);
if (N >= 18 && N <= 21 && args[desc_index] && x2w_local::readable(reinterpret_cast<const void*>(args[desc_index]), view_bytes)) {
std::memcpy(desc_copy, reinterpret_cast<const void*>(args[desc_index]), view_bytes);
DXGI_FORMAT format;
std::memcpy(&format, desc_copy, sizeof(format));
DXGI_FORMAT mapped = desktop_format(format);
if (args[0] && x2w_startup::packed_target_format(mapped)) {
// A packed view follows a stand-in texture and is refused on any other texture of another format.
auto* const resource = reinterpret_cast<ID3D12Resource*>(args[0]);
const auto verdict = x2w_startup::packed_view(mapped, resource->GetDesc().Format, x2w_startup::packed_stand_in_of(resource));
const auto actual = resource->GetDesc().Format;
const auto verdict = x2w_startup::packed_view(mapped, actual, x2w_startup::packed_stand_in_of(resource));
if (verdict == x2w_startup::PackedView::refused) {
record_view(false);
static LONG refused;
if (first_calls(refused, 6))
log("x2w-d3d12 Device::slot%d view skipped: packed format %d on a texture of format %d that does not stand in for it",
N, mapped, resource->GetDesc().Format);
N, mapped, actual);
return 0;
}
if (verdict == x2w_startup::PackedView::stand_in) mapped = DXGI_FORMAT_R8G8B8A8_UNORM;
if (verdict == x2w_startup::PackedView::stand_in) mapped = actual;
}
if (mapped != format) {
std::memcpy(desc_copy, &mapped, sizeof(mapped));
Expand Down Expand Up @@ -4153,7 +4174,9 @@ HRESULT __stdcall device_place_resource_x(Wrapper* self, void* address, const D3
texture.Height = surface.rows * x2w_startup::Bc16Surface::block_texels;
}
const D3D12_HEAP_PROPERTIES props{D3D12_HEAP_TYPE_DEFAULT};
const auto* clear_value = static_cast<const D3D12_CLEAR_VALUE*>(clear);
D3D12_CLEAR_VALUE clear_copy{};
const auto* clear_value = packed_clear_value(static_cast<const D3D12_CLEAR_VALUE*>(clear),
desktop_format(desc->Format), texture.Format, clear_copy);
ID3D12Resource* created = nullptr;
HRESULT texture_hr = device_of(self)->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &texture,
D3D12_RESOURCE_STATE_COMMON, clear_value,
Expand Down Expand Up @@ -4862,8 +4885,10 @@ HRESULT __stdcall device_create_committed_x(Wrapper* self, const D3D12_HEAP_PROP
const D3D12_RESOURCE_STATES initial = props->Type == D3D12_HEAP_TYPE_UPLOAD ? D3D12_RESOURCE_STATE_GENERIC_READ
: props->Type == D3D12_HEAP_TYPE_READBACK ? D3D12_RESOURCE_STATE_COPY_DEST
: D3D12_RESOURCE_STATE_COMMON;
D3D12_CLEAR_VALUE clear_copy{};
const auto* clear_value = packed_clear_value(clear, desktop_format(desc->Format), copy.Format, clear_copy);
HRESULT hr = kind_for(*iid) == resource_kind
? device_of(self)->CreateCommittedResource(props, flags, &copy, initial, clear, IID_PPV_ARGS(&resource))
? device_of(self)->CreateCommittedResource(props, flags, &copy, initial, clear_value, IID_PPV_ARGS(&resource))
: E_NOINTERFACE;
bool cleared = clear != nullptr;
if (FAILED(hr) && clear && kind_for(*iid) == resource_kind) {
Expand Down
Loading
Loading