From 5d8c1d6869a00af75b5e10a777c9a5eb8279d8d2 Mon Sep 17 00:00:00 2001 From: Jess-Doit Date: Tue, 24 Nov 2026 14:07:25 -0800 Subject: [PATCH] runtime: stand in for shared-exponent render and UAV textures Desktop D3D12 refuses R9G9B9E5_SHAREDEXP (67) as a render target or typed UAV. A single-sample 2D texture or array in that format that asks for either use is now created as R11G11B10_FLOAT (26) when the adapter lacks the use for 67 and has it for 26, through the same table-driven mapping as the 16-bit packed stand-ins. The resource is tagged with 67; RTV, UAV and SRV descriptions naming 67 on it, including SRVs rebuilt for translated draws, use 26, and an optimized clear value follows the created format. Tests: selection rows and two registered controls (format 67 left alone, a UAV-only target left alone) in startup_packed_target_check, and a WARP and hardware readback of RTV and partial UAV clears through the startup device against independently encoded R11G11B10 words. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 9 +- docs/experiments.md | 14 ++ docs/startup-runtime.md | 22 +++ src/startup_stubs/d3d12_draw.inl | 7 +- src/startup_stubs/d3d12_xs_device.cpp | 43 ++++-- src/startup_stubs/packed_target_format.h | 63 +++++--- tests/offscreen_check.cpp | 10 ++ tests/startup_packed_target_check.cpp | 57 ++++++- tests/startup_sharedexp_target.cpp | 186 +++++++++++++++++++++++ 9 files changed, 372 insertions(+), 39 deletions(-) create mode 100644 tests/startup_sharedexp_target.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 417d15551..cbaf5f01e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1766,15 +1766,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) diff --git a/docs/experiments.md b/docs/experiments.md index efca64a67..9305d0f74 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -199,6 +199,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 diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 043429b30..d018545ac 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -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 diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 335498a90..70e612c1c 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -1989,13 +1989,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; diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 00f1b36fc..1a7c62e90 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -745,7 +745,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(the_device->own().real); const DXGI_FORMAT stand_in = x2w_startup::packed_target_stand_in(copy, [device](DXGI_FORMAT format) -> UINT { @@ -764,6 +764,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 © +} + template __declspec(noinline) u64 create_slot(Wrapper* self, u64 a, u64 b, u64 c, u64 d, u64 e, u64 f, u64 g, u64 h) { @@ -771,11 +784,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(args[Desc])->Format); desc_copy = sanitized(*reinterpret_cast(args[Desc])); args[Desc] = reinterpret_cast(&desc_copy); + if constexpr (K == device_kind && (N == 27 || N == 29)) + args[4] = reinterpret_cast(packed_clear_value(reinterpret_cast(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]); @@ -3736,26 +3753,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(args[desc_index]), sizeof(desc_copy))) { - std::memcpy(desc_copy, reinterpret_cast(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(args[desc_index]), view_bytes)) { + std::memcpy(desc_copy, reinterpret_cast(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(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)); @@ -4007,7 +4028,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(clear); + D3D12_CLEAR_VALUE clear_copy{}; + const auto* clear_value = packed_clear_value(static_cast(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, @@ -4448,8 +4471,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, ©, initial, clear, IID_PPV_ARGS(&resource)) + ? device_of(self)->CreateCommittedResource(props, flags, ©, initial, clear_value, IID_PPV_ARGS(&resource)) : E_NOINTERFACE; bool cleared = clear != nullptr; if (FAILED(hr) && clear && kind_for(*iid) == resource_kind) { diff --git a/src/startup_stubs/packed_target_format.h b/src/startup_stubs/packed_target_format.h index ff52d8b4f..4dd8fb0d0 100644 --- a/src/startup_stubs/packed_target_format.h +++ b/src/startup_stubs/packed_target_format.h @@ -1,4 +1,4 @@ -// Experimental (docs/startup-runtime.md): a stand-in format for 16-bit packed render targets that +// Experimental (docs/startup-runtime.md): stand-in formats for packed render and unordered-access targets that // the adapter cannot use the way the description asks. // // A console executable creates B5G5R5A1_UNORM and B4G4R4A4_UNORM 2D textures as render targets, @@ -9,7 +9,12 @@ // texture is created as R8G8B8A8_UNORM and tagged with the format it stands in for; a view that // names that format on that texture is created as R8G8B8A8_UNORM. // -// Limits. The stand-in keeps 8 bits a channel where the packed format keeps 5, 6, 4 or 1; a +// R9G9B9E5_SHAREDEXP can use R11G11B10_FLOAT for a render target, a typed UAV, or both. Both +// use 32 bits per texel and clamp negative values to zero. R11G11B10_FLOAT has 6/6/5 mantissa +// bits and per-channel exponents; format 67 has three 9-bit mantissas with one shared exponent. +// Uploads and copies in the original shared-exponent bit layout are not converted. +// +// For 16-bit formats the stand-in keeps 8 bits a channel where the packed format keeps 5, 6, 4 or 1; a // B5G6R5_UNORM stand-in also has an alpha channel the packed format lacks (it samples as 1): values the // executable renders are not quantized, so a read of the target can differ from the console's in // the low bits (0.45 reads 115 here, 119 from B4G4R4A4). Texel data the executable writes or reads @@ -20,16 +25,36 @@ #include namespace x2w_startup { +struct PackedTargetMapping { + DXGI_FORMAT requested; + DXGI_FORMAT replacement; + UINT trigger_flags; +}; + +inline constexpr PackedTargetMapping packed_target_mappings[] = { + {DXGI_FORMAT_B5G5R5A1_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET}, + {DXGI_FORMAT_B4G4R4A4_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET}, + {DXGI_FORMAT_B5G6R5_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET}, + {DXGI_FORMAT_R9G9B9E5_SHAREDEXP, DXGI_FORMAT_R11G11B10_FLOAT, + UINT{D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET} | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS}, +}; + +inline const PackedTargetMapping* packed_target_mapping(DXGI_FORMAT format) +{ + for (const auto& mapping : packed_target_mappings) + if (mapping.requested == format) return &mapping; + return nullptr; +} + inline bool packed_target_format(DXGI_FORMAT format) { - return format == DXGI_FORMAT_B5G5R5A1_UNORM || format == DXGI_FORMAT_B4G4R4A4_UNORM || format == DXGI_FORMAT_B5G6R5_UNORM; + return packed_target_mapping(format) != nullptr; } // The format to create a texture with. support1(format) returns the adapter's -// D3D12_FORMAT_SUPPORT1 bits for a format, or 0 when the query fails. The format changes only for -// the roles that were measured: a single-sample 2D texture or 2D array that is a render -// target, alone or with unordered access, in a packed format for which the adapter lacks one of -// those uses while R8G8B8A8_UNORM has them all. Anything else keeps its format. +// D3D12_FORMAT_SUPPORT1 bits for a format, or 0 when the query fails. For a single-sample 2D +// texture or array the mapping's trigger flags enable the check. Substitute only if the adapter +// lacks a requested target use for the original format and the replacement supports them all. // // An array of 2 to D3D12_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION slices is covered the same way: the // stand-in changes the format and nothing else, so slice count, mip count and view dimensions of @@ -38,16 +63,18 @@ inline bool packed_target_format(DXGI_FORMAT format) template DXGI_FORMAT packed_target_stand_in(const D3D12_RESOURCE_DESC& desc, Support1&& support1) { - if (desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || desc.DepthOrArraySize == 0 || + const auto* mapping = packed_target_mapping(desc.Format); + if (!mapping || !(desc.Flags & mapping->trigger_flags) || + desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D || desc.DepthOrArraySize == 0 || desc.DepthOrArraySize > D3D12_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION || desc.SampleDesc.Count != 1 || - !packed_target_format(desc.Format) || !(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) || (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) return desc.Format; - UINT required = D3D12_FORMAT_SUPPORT1_RENDER_TARGET; + UINT required = 0; + if (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) required |= D3D12_FORMAT_SUPPORT1_RENDER_TARGET; if (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS) required |= D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW; if ((static_cast(support1(desc.Format)) & required) == required) return desc.Format; - if ((static_cast(support1(DXGI_FORMAT_R8G8B8A8_UNORM)) & required) != required) return desc.Format; - return DXGI_FORMAT_R8G8B8A8_UNORM; + if ((static_cast(support1(mapping->replacement)) & required) != required) return desc.Format; + return mapping->replacement; } // Provenance: a texture created as a stand-in carries the format it was asked for as private data. @@ -57,7 +84,8 @@ inline constexpr GUID packed_stand_in_tag = {0x6b1d3c52, 0x8e0a, 0x4f1b, {0x9c, // Tags `created` when it is a stand-in for `requested` (the description's desktop format). inline void mark_packed_stand_in(ID3D12Resource* created, DXGI_FORMAT requested) { - if (created && packed_target_format(requested) && created->GetDesc().Format == DXGI_FORMAT_R8G8B8A8_UNORM) + const auto* mapping = packed_target_mapping(requested); + if (created && mapping && created->GetDesc().Format == mapping->replacement) created->SetPrivateData(packed_stand_in_tag, sizeof(requested), &requested); } @@ -73,16 +101,17 @@ inline DXGI_FORMAT packed_stand_in_of(ID3D12Resource* resource) enum class PackedView { unchanged, // not a packed view, or a packed view of a texture of that very format - stand_in, // a packed view of the stand-in for that format: create it as R8G8B8A8_UNORM - refused, // a packed view of any other texture; the two packed formats have no compatible format + stand_in, // use the stand-in texture's format + refused, // a packed view of a texture with no compatible format }; // What to do with a view described with view_format on a texture of resource_format that stands in // for stands_in_for (DXGI_FORMAT_UNKNOWN: an ordinary texture). inline PackedView packed_view(DXGI_FORMAT view_format, DXGI_FORMAT resource_format, DXGI_FORMAT stands_in_for) { - if (!packed_target_format(view_format)) return PackedView::unchanged; + const auto* mapping = packed_target_mapping(view_format); + if (!mapping) return PackedView::unchanged; if (stands_in_for == DXGI_FORMAT_UNKNOWN) return resource_format == view_format ? PackedView::unchanged : PackedView::refused; - return stands_in_for == view_format && resource_format == DXGI_FORMAT_R8G8B8A8_UNORM ? PackedView::stand_in : PackedView::refused; + return stands_in_for == view_format && resource_format == mapping->replacement ? PackedView::stand_in : PackedView::refused; } } // namespace x2w_startup diff --git a/tests/offscreen_check.cpp b/tests/offscreen_check.cpp index 12b394a58..57f64c8cf 100644 --- a/tests/offscreen_check.cpp +++ b/tests/offscreen_check.cpp @@ -1362,6 +1362,15 @@ OperationResult run_startup_image_store(const OperationContext& ctx) return {OutcomeKind::pass, nullptr}; } +const char* check_startup_sharedexp_target(ID3D12Device* device, ID3D12InfoQueue* messages); + +OperationResult run_startup_sharedexp_target(const OperationContext& ctx) +{ + if (const char* reason = check_startup_sharedexp_target(ctx.device, ctx.messages)) + return {OutcomeKind::skip, reason}; + return {OutcomeKind::pass, nullptr}; +} + OperationResult run_startup_packed_position(const OperationContext& ctx) { constexpr UINT width = 13, height = 7; @@ -1580,6 +1589,7 @@ const OperationSpec kOperations[] = { {"--startup-image-2d-array-mips", 7, 8, nullptr, kNoGate, run_startup_image_3d}, {"--startup-image-load-d16", 7, 7, nullptr, kNoGate, run_startup_image_load_d16}, {"--startup-packed-position", 5, 5, nullptr, kNoGate, run_startup_packed_position}, + {"--startup-sharedexp-target", 3, 3, nullptr, kNoGate, run_startup_sharedexp_target}, {"--startup-flat-param", 6, 6, nullptr, kNoGate, run_startup_flat_param}, {"--startup-pixel-words", 8, 8, validate_pixel_words_args, kNoGate, run_startup_pixel_words}, {"--raster-reference", 10, 10, validate_raster_args, kNoGate, run_startup_pixel_words}, diff --git a/tests/startup_packed_target_check.cpp b/tests/startup_packed_target_check.cpp index 5cbf361d3..fb23070b7 100644 --- a/tests/startup_packed_target_check.cpp +++ b/tests/startup_packed_target_check.cpp @@ -1,8 +1,10 @@ // Checks src/startup_stubs/packed_target_format.h without a device, against adapters described // by their format-support bits. -// --mutate stand-in | array | b565 | view | tag: the helper's answer perturbed the way a wrong helper -// would answer (a stand-in in another format, arrays left alone as before, B5G6R5_UNORM left alone -// as before, the view format of the stand-in kept packed, the stand-in's tag read back as no tag); +// --mutate stand-in | array | b565 | sharedexp | sharedexp-uav | view | tag: the helper's answer +// perturbed the way a wrong helper would answer (a stand-in in another format, arrays left alone as +// before, B5G6R5_UNORM left alone as before, R9G9B9E5_SHAREDEXP left alone as before, a UAV-only +// R9G9B9E5_SHAREDEXP texture left alone as under a render-target-only trigger, the view format of the +// stand-in kept packed, the stand-in's tag read back as no tag); // each must fail on the rows it concerns. #include "startup_stubs/packed_target_format.h" @@ -18,8 +20,11 @@ constexpr UINT rt = D3D12_FORMAT_SUPPORT1_RENDER_TARGET, uav = D3D12_FORMAT_SUPP struct Adapter { UINT b5g5r5a1, b4g4r4a4, rgba8, b5g6r5; + UINT sharedexp = 0, hdr = 0; UINT operator()(DXGI_FORMAT format) const { + if (format == DXGI_FORMAT_R9G9B9E5_SHAREDEXP) return sharedexp; + if (format == DXGI_FORMAT_R11G11B10_FLOAT) return hdr; return format == DXGI_FORMAT_B5G5R5A1_UNORM ? b5g5r5a1 : format == DXGI_FORMAT_B4G4R4A4_UNORM ? b4g4r4a4 : format == DXGI_FORMAT_B5G6R5_UNORM ? b5g6r5 : format == DXGI_FORMAT_R8G8B8A8_UNORM ? rgba8 : rt | uav; } @@ -32,6 +37,10 @@ DXGI_FORMAT stand_in(const D3D12_RESOURCE_DESC& desc, const Adapter& adapter) if (!std::strcmp(mutation, "stand-in") && got == DXGI_FORMAT_R8G8B8A8_UNORM) return DXGI_FORMAT_B8G8R8A8_UNORM; if (!std::strcmp(mutation, "array") && desc.DepthOrArraySize != 1) return desc.Format; if (!std::strcmp(mutation, "b565") && desc.Format == DXGI_FORMAT_B5G6R5_UNORM) return desc.Format; + if (!std::strcmp(mutation, "sharedexp") && desc.Format == DXGI_FORMAT_R9G9B9E5_SHAREDEXP) return desc.Format; + if (!std::strcmp(mutation, "sharedexp-uav") && desc.Format == DXGI_FORMAT_R9G9B9E5_SHAREDEXP && + !(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)) + return desc.Format; return got; } @@ -162,7 +171,7 @@ int main(int argc, char** argv) { if (argc == 3 && !std::strcmp(argv[1], "--mutate")) mutation = argv[2]; else if (argc != 1) { - std::printf("usage: startup_packed_target_check [--mutate stand-in|array|b565|view|tag]\n"); + std::printf("usage: startup_packed_target_check [--mutate stand-in|array|b565|sharedexp|sharedexp-uav|view|tag]\n"); return 2; } const auto packed_target_stand_in = stand_in; @@ -245,6 +254,35 @@ int main(int argc, char** argv) shaped.DepthOrArraySize = 6; expect("rgba8 RT array", packed_target_stand_in(shaped, none), rgba); + constexpr auto sharedexp = DXGI_FORMAT_R9G9B9E5_SHAREDEXP; + constexpr auto hdr = DXGI_FORMAT_R11G11B10_FLOAT; + constexpr UINT sample = D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE; + const Adapter sharedexp_missing{0, 0, rt | uav, 0, sample, rt | uav}; + const Adapter sharedexp_rt_only{0, 0, rt | uav, 0, sample | rt, rt | uav}; + const Adapter sharedexp_supported{0, 0, rt | uav, 0, sample | rt | uav, rt | uav}; + const Adapter hdr_no_rt{0, 0, rt | uav, 0, sample, uav}; + const Adapter hdr_no_uav{0, 0, rt | uav, 0, sample, rt}; + expect("shared exponent RT only", packed_target_stand_in(texture(sharedexp, RT), hdr_no_uav), hdr); + expect("shared exponent UAV only", packed_target_stand_in(texture(sharedexp, UA), hdr_no_rt), hdr); + expect("shared exponent RT|UAV", packed_target_stand_in(texture(sharedexp, RT | UA), sharedexp_missing), hdr); + expect("shared exponent RT supported", packed_target_stand_in(texture(sharedexp, RT), sharedexp_rt_only), sharedexp); + expect("shared exponent RT supported, UAV missing", + packed_target_stand_in(texture(sharedexp, RT | UA), sharedexp_rt_only), hdr); + expect("shared exponent RT|UAV supported", + packed_target_stand_in(texture(sharedexp, RT | UA), sharedexp_supported), sharedexp); + expect("shared exponent RT|UAV replacement missing RT", packed_target_stand_in(texture(sharedexp, RT | UA), hdr_no_rt), sharedexp); + expect("shared exponent RT|UAV replacement missing UAV", packed_target_stand_in(texture(sharedexp, RT | UA), hdr_no_uav), sharedexp); + expect("shared exponent sampled only", packed_target_stand_in(texture(sharedexp, 0), sharedexp_missing), sharedexp); + expect("shared exponent depth stencil", + packed_target_stand_in(texture(sharedexp, RT | UA | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL), sharedexp_missing), sharedexp); + expect("shared exponent 3D", packed_target_stand_in(texture(sharedexp, RT | UA, D3D12_RESOURCE_DIMENSION_TEXTURE3D), + sharedexp_missing), sharedexp); + shaped = texture(sharedexp, RT | UA); + shaped.DepthOrArraySize = 6; + expect("shared exponent RT|UAV array", packed_target_stand_in(shaped, sharedexp_missing), hdr); + shaped.SampleDesc.Count = 4; + expect("shared exponent multisampled", packed_target_stand_in(shaped, sharedexp_missing), sharedexp); + // Views: a packed view becomes RGBA8 only on the stand-in for that very format. using x2w_startup::PackedView; constexpr DXGI_FORMAT plain = DXGI_FORMAT_UNKNOWN; @@ -269,9 +307,12 @@ int main(int argc, char** argv) // back that packed format and its packed view becomes the stand-in; a texture created in the // packed format itself carries no tag, so its own view is unchanged; an untagged R8G8B8A8_UNORM // texture refuses the packed view. - for (const DXGI_FORMAT packed : {b5, b4, b565}) { + view("shared exponent view of a tagged rgba8 texture", packed_view(sharedexp, rgba, sharedexp), PackedView::refused); + view("shared exponent view of another stand-in", packed_view(sharedexp, rgba, b5), PackedView::refused); + for (const DXGI_FORMAT packed : {b5, b4, b565, sharedexp}) { + const auto replacement = packed == sharedexp ? hdr : rgba; char name[64]; - Texture stand_in_texture(rgba); + Texture stand_in_texture(replacement); x2w_startup::mark_packed_stand_in(&stand_in_texture, packed); std::snprintf(name, sizeof(name), "format %d stand-in tag", packed); expect(name, packed_stand_in_of(&stand_in_texture), packed); @@ -283,8 +324,8 @@ int main(int argc, char** argv) expect(name, packed_stand_in_of(&described), plain); std::snprintf(name, sizeof(name), "format %d view of a texture created as described", packed); view(name, packed_view(packed, described.GetDesc().Format, packed_stand_in_of(&described)), PackedView::unchanged); - Texture untagged(rgba); - std::snprintf(name, sizeof(name), "format %d view of an untagged rgba8 texture", packed); + Texture untagged(replacement); + std::snprintf(name, sizeof(name), "format %d view of an untagged replacement texture", packed); view(name, packed_view(packed, untagged.GetDesc().Format, packed_stand_in_of(&untagged)), PackedView::refused); } Texture b4_stand_in(rgba); diff --git a/tests/startup_sharedexp_target.cpp b/tests/startup_sharedexp_target.cpp new file mode 100644 index 000000000..97edcdd63 --- /dev/null +++ b/tests/startup_sharedexp_target.cpp @@ -0,0 +1,186 @@ +// Create the shared-exponent target and views through the startup device, then +// clear and read the backing texture on the desktop device. +#include "desktop_gpu.h" +#include "startup_stubs/packed_target_format.h" +#include "startup_stubs/placed_build.h" + +#include +#include +#include +#include +#include + +using Microsoft::WRL::ComPtr; + +namespace { +constexpr auto sharedexp = DXGI_FORMAT_R9G9B9E5_SHAREDEXP; +constexpr auto hdr = DXGI_FORMAT_R11G11B10_FLOAT; +constexpr UINT width = 4, height = 2; +constexpr float rt_color[]{0.5f, 1.0f, 2.0f, 1.0f}; +constexpr float uav_color[]{-1.0f, 0.5f, 996.0f, 1.0f}; + +// The fixture's positive inputs are finite normal values away from rounding ties. +// The biased exponent is 15 for values in [1, 2); quantize the significand at +// that exponent, then pack the red, green and blue fields into a little-endian word. +std::uint32_t encode_channel(float value, int mantissa_bits) +{ + if (value <= 0.0f) return 0; + int exponent; + const double significand = std::frexp(value, &exponent); + const auto rounded = static_cast(std::round(std::ldexp(significand, mantissa_bits + 1))); + return ((exponent + 14u) << mantissa_bits) + rounded - (1u << mantissa_bits); +} + +std::uint32_t encode_rgb(const float (&color)[4]) +{ + return encode_channel(color[0], 6) | (encode_channel(color[1], 6) << 11) | + (encode_channel(color[2], 5) << 22); +} + +ComPtr native_texture(ID3D12Device* device, ID3D12Device* guest, ID3D12Resource* texture) +{ + // The device wrapper unwraps objects passed to SetPrivateDataInterface. + constexpr GUID key{0x029d497b, 0xc94c, 0x42ca, {0xa1, 0xad, 0x0e, 0xe3, 0xa5, 0xd8, 0x66, 0xd1}}; + check(guest->SetPrivateDataInterface(key, texture), "Publish underlying texture"); + ComPtr stored; + UINT bytes = sizeof(IUnknown*); + const HRESULT read = device->GetPrivateData(key, &bytes, stored.GetAddressOf()); + check(guest->SetPrivateDataInterface(key, nullptr), "Clear underlying texture"); + check(read, "Retrieve underlying texture"); + ComPtr result; + check(stored.As(&result), "Query underlying texture"); + return result; +} + +ComPtr heap(ID3D12Device* device, D3D12_DESCRIPTOR_HEAP_TYPE type, UINT count, + D3D12_DESCRIPTOR_HEAP_FLAGS flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE) +{ + const D3D12_DESCRIPTOR_HEAP_DESC desc{type, count, flags}; + ComPtr result; + check(device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&result)), "Create fixture descriptor heap"); + return result; +} + +template +void create_view(ID3D12InfoQueue* messages, Create&& create, const char* stage) +{ + const UINT64 before = messages ? messages->GetNumStoredMessagesAllowedByRetrievalFilter() : 0; + create(); + if (messages && messages->GetNumStoredMessagesAllowedByRetrievalFilter() != before) + throw std::runtime_error(stage); // offscreen_check prints the debug diagnostics +} + +void check_texture(ID3D12Device* device, ID3D12Device* guest, ID3D12InfoQueue* messages, UINT16 slices) +{ + const D3D12_HEAP_PROPERTIES props{D3D12_HEAP_TYPE_DEFAULT}; + const auto flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; + const D3D12_RESOURCE_DESC desc{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, width, height, slices, 1, sharedexp, + {1, 0}, D3D12_TEXTURE_LAYOUT_UNKNOWN, flags}; + const D3D12_CLEAR_VALUE clear{sharedexp, {rt_color[0], rt_color[1], rt_color[2], rt_color[3]}}; + ComPtr wrapped; + check(guest->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_RENDER_TARGET, + &clear, IID_PPV_ARGS(&wrapped)), "Create shared-exponent RT|UAV texture"); + const auto texture = native_texture(device, guest, wrapped.Get()); + const auto actual = texture->GetDesc(); + const auto tag = x2w_startup::packed_stand_in_of(wrapped.Get()); + if (actual.Format != hdr || actual.Flags != flags || actual.DepthOrArraySize != slices || tag != sharedexp) + throw std::runtime_error("Shared-exponent resource shape, actual format or tag differs"); + const auto targets = heap(device, D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1); + const auto views = heap(device, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 2); + const auto shader_views = heap(device, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 1, + D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE); + const auto rtv_handle = targets->GetCPUDescriptorHandleForHeapStart(); + const auto uav_handle = views->GetCPUDescriptorHandleForHeapStart(); + const auto shader_cpu = shader_views->GetCPUDescriptorHandleForHeapStart(); + const auto shader_gpu = shader_views->GetGPUDescriptorHandleForHeapStart(); + const D3D12_CPU_DESCRIPTOR_HANDLE srv_handle{ + uav_handle.ptr + device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV)}; + + const bool array = slices > 1; + D3D12_RENDER_TARGET_VIEW_DESC rtv{sharedexp, array ? D3D12_RTV_DIMENSION_TEXTURE2DARRAY : D3D12_RTV_DIMENSION_TEXTURE2D}; + D3D12_UNORDERED_ACCESS_VIEW_DESC uav{sharedexp, array ? D3D12_UAV_DIMENSION_TEXTURE2DARRAY : D3D12_UAV_DIMENSION_TEXTURE2D}; + D3D12_SHADER_RESOURCE_VIEW_DESC srv{sharedexp, array ? D3D12_SRV_DIMENSION_TEXTURE2DARRAY : D3D12_SRV_DIMENSION_TEXTURE2D, + D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING}; + srv.Texture2D.MipLevels = 1; + if (array) { + rtv.Texture2DArray.ArraySize = slices; + uav.Texture2DArray.ArraySize = slices; + srv.Texture2DArray.ArraySize = slices; + } + create_view(messages, [&] { guest->CreateRenderTargetView(wrapped.Get(), &rtv, rtv_handle); }, + "Create format-67 RTV reported a debug diagnostic"); + create_view(messages, [&] { + guest->CreateUnorderedAccessView(wrapped.Get(), nullptr, &uav, uav_handle); + guest->CreateUnorderedAccessView(wrapped.Get(), nullptr, &uav, shader_cpu); + }, "Create format-67 UAV reported a debug diagnostic"); + create_view(messages, [&] { guest->CreateShaderResourceView(wrapped.Get(), &srv, srv_handle); }, + "Create format-67 SRV reported a debug diagnostic"); + + std::array footprints{}; + UINT64 bytes; + device->GetCopyableFootprints(&actual, 0, slices, 0, footprints.data(), nullptr, nullptr, &bytes); + auto readback = make_buffer(device, bytes, D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST); + ComputeCommands commands(device, nullptr, D3D12_COMMAND_LIST_TYPE_DIRECT); + auto* list = commands.list.Get(); + list->ClearRenderTargetView(rtv_handle, rt_color, 0, nullptr); + transition(list, texture.Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + ID3D12DescriptorHeap* const bound[] = {shader_views.Get()}; + list->SetDescriptorHeaps(1, bound); + const D3D12_RECT right{width / 2, 0, width, height}; + list->ClearUnorderedAccessViewFloat(shader_gpu, uav_handle, texture.Get(), uav_color, 1, &right); + transition(list, texture.Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE); + for (UINT slice = 0; slice < slices; ++slice) { + D3D12_TEXTURE_COPY_LOCATION dst{readback.Get(), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT}; + dst.PlacedFootprint = footprints[slice]; + D3D12_TEXTURE_COPY_LOCATION src{texture.Get(), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX}; + src.SubresourceIndex = slice; + list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr); + } + commands.submit_and_wait(device); + + void* data = nullptr; + const D3D12_RANGE range{0, static_cast(bytes)}, no_writes{}; + check(readback->Map(0, &range, &data), "Map shared-exponent target readback"); + const auto rt_word = encode_rgb(rt_color), uav_word = encode_rgb(uav_color); + for (UINT slice = 0; slice < slices; ++slice) { + const auto& footprint = footprints[slice]; + for (UINT y = 0; y < height; ++y) { + const auto* row = static_cast(data) + footprint.Offset + y * footprint.Footprint.RowPitch; + for (UINT x = 0; x < width; ++x) { + std::uint32_t word; + std::memcpy(&word, row + x * sizeof(word), sizeof(word)); + const auto expected = x < width / 2 ? rt_word : uav_word; + if (word != expected) { + readback->Unmap(0, &no_writes); + std::fprintf(stderr, "slice %u (%u,%u): expected %08x, got %08x\n", slice, x, y, expected, word); + throw std::runtime_error("Shared-exponent stand-in readback differs"); + } + } + } + } + readback->Unmap(0, &no_writes); + std::printf("slices=%u format=%d tag=%d rtv=%08x uav=%08x words=%u\n", + slices, actual.Format, tag, rt_word, uav_word, width * height * slices); +} +} // namespace + +const char* check_startup_sharedexp_target(ID3D12Device* device, ID3D12InfoQueue* messages) +{ + constexpr UINT uses = D3D12_FORMAT_SUPPORT1_RENDER_TARGET | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW; + D3D12_FEATURE_DATA_FORMAT_SUPPORT original{sharedexp}, fallback{hdr}; + check(check_feature(device, D3D12_FEATURE_FORMAT_SUPPORT, &original, sizeof(original)), "Query format 67"); + check(check_feature(device, D3D12_FEATURE_FORMAT_SUPPORT, &fallback, sizeof(fallback)), "Query format 26"); + std::printf("support1_67=0x%x support1_26=0x%x support2_26=0x%x\n", original.Support1, fallback.Support1, fallback.Support2); + if ((original.Support1 & uses) == uses) return "adapter supports native format-67 RT and UAV views"; + if ((fallback.Support1 & uses) != uses || !(fallback.Support2 & D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE)) + return "adapter lacks format-26 render-target or typed UAV stores"; + + device->AddRef(); + void* wrapped = nullptr; + x2w_startup::attach_device(device, &wrapped); + ComPtr guest; + guest.Attach(static_cast(wrapped)); + check_texture(device, guest.Get(), messages, 1); + check_texture(device, guest.Get(), messages, 2); + return nullptr; +}