From 3973ce8c2072ecde6ae13723ae6a9826ddca6dcb Mon Sep 17 00:00:00 2001 From: Vonsek <45567075+Vonsek@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:47:40 +0100 Subject: [PATCH] feat(runtime): support sampled RGB9E5 textures Map the shared-exponent format through resource creation and buffer-image copies so shaders can resolve its existing GFX10 view. Check the sampled channels and compare the mip-array footprints with the host runtime. --- docs/d3d12_vulkan.md | 1 + runtime/shims/d3d12_vulkan_device.cpp | 1 + runtime/src/vulkan_replay.h | 1 + runtime/test/d3d12_descriptor_test.cpp | 56 +++++++++++++++++++---- runtime/test/d3d12_vulkan_device_test.cpp | 1 + 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 8769de102..eeba9b3fe 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -205,6 +205,7 @@ What is implemented (all of it testable without the game): render-target, depth and texture formats (typeless families become mutable-format images), plus non-attachment 3D textures with mipmapped upload, image copy and readback, and multisampled 2D textures (single-mip render targets or depth-stencils, in the sample counts the GPU has for the format); + - packed `R9G9B9E5_SHAREDEXP` textures map to `VK_FORMAT_E5B9G9R9_UFLOAT_PACK32` for uploads and sampled views; - `CheckFeatureSupport` for feature levels (up to 12_1) and architecture. Every other query is logged and refused until the trace shows what the game asks (question 11); - reserved resources and `GetResourceTiling` (section 4.2); diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 0d89d4ae5..e1849393c 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -59,6 +59,7 @@ Format vulkan_format(DXGI_FORMAT format, bool depth) { case DXGI_FORMAT_R32G32_FLOAT: return {VK_FORMAT_R32G32_SFLOAT, color}; case DXGI_FORMAT_R10G10B10A2_TYPELESS: return {VK_FORMAT_A2B10G10R10_UNORM_PACK32, color, true}; case DXGI_FORMAT_R10G10B10A2_UNORM: return {VK_FORMAT_A2B10G10R10_UNORM_PACK32, color}; + case DXGI_FORMAT_R9G9B9E5_SHAREDEXP: return {VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, color}; case DXGI_FORMAT_R11G11B10_FLOAT: return {VK_FORMAT_B10G11R11_UFLOAT_PACK32, color}; case DXGI_FORMAT_R8G8B8A8_TYPELESS: return {VK_FORMAT_R8G8B8A8_UNORM, color, true}; case DXGI_FORMAT_R8G8B8A8_UNORM: return {VK_FORMAT_R8G8B8A8_UNORM, color}; diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 81e15f2db..4c7676815 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -365,6 +365,7 @@ inline BlockFormat block_format(VkFormat format, VkImageAspectFlags aspect) { case VK_FORMAT_R16G16B16A16_UNORM: case VK_FORMAT_R32G32_SFLOAT: return {8, 1}; case VK_FORMAT_A2B10G10R10_UNORM_PACK32: + case VK_FORMAT_E5B9G9R9_UFLOAT_PACK32: case VK_FORMAT_B10G11R11_UFLOAT_PACK32: case VK_FORMAT_R8G8B8A8_UNORM: case VK_FORMAT_R8G8B8A8_SRGB: diff --git a/runtime/test/d3d12_descriptor_test.cpp b/runtime/test/d3d12_descriptor_test.cpp index fed05a635..ec357e963 100644 --- a/runtime/test/d3d12_descriptor_test.cpp +++ b/runtime/test/d3d12_descriptor_test.cpp @@ -4,8 +4,8 @@ // CreateUnorderedAccessView and CreateSampler write a T# or an S# into the heap slot and put the view or the // sampler into the device's bindless set 0. The slots are decoded and checked. Then a compute shader samples // an uploaded 8x8 texture by those bytes: it looks the T# and the S# up in the descriptor hash table, as a -// translated shader does. It runs three times: with the view's own slot, with a copy of it in another slot, -// and with a slot holding bytes no view was made for, which must give the fallback image. The sampler wraps +// translated shader does. It checks the view's own slot, a copied slot, a slot that must give the fallback +// image, and a packed RGB9E5 view with distinct channels and varying shared exponents. The sampler wraps // in U and the shader samples one tile to the right, so a missed S# would show as well (the fallback // sampler clamps). // The device has no pipelines yet, so the test makes that one pipeline in Vulkan itself and binds the device's set @@ -189,7 +189,7 @@ int main() { EXPECT(d.create()); constexpr UINT size = 8, texels = size * size, bytes = texels * 4; - std::vector results(3 * bytes), after_destruction(bytes); + std::vector results(4 * bytes), after_destruction(bytes); bool sampled_through_set = false; { rt::d3d12::VulkanDeviceInfo info{}; @@ -460,6 +460,28 @@ int main() { cube.reset(); writable.reset(); + // A packed RGB9E5 SRV uses the same four-byte copy footprint as RGBA8. Its shared exponent + // changes on each diagonal. The last pattern permutes the mantissas to detect channel swaps. + Ref packed; + D3D12_RESOURCE_DESC packed_desc = texture_desc; + packed_desc.Format = DXGI_FORMAT_R9G9B9E5_SHAREDEXP; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &packed_desc, + D3D12_RESOURCE_STATE_COPY_DEST, nullptr, + IID_PPV_ARGS(&packed.p)))); + constexpr UINT packed_slot = 0; + device->CreateShaderResourceView(packed.p, nullptr, view_slot(packed_slot)); + D3D12_PLACED_SUBRESOURCE_FOOTPRINT packed_placed{}; + device->GetCopyableFootprints(&packed_desc, 0, 1, 4096, &packed_placed, nullptr, nullptr, nullptr); + EXPECT(packed_placed.Offset == 4096 && packed_placed.Footprint.RowPitch == D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); + constexpr auto rgb9e5 = [](UINT r, UINT g, UINT b, UINT e) { return r | (g << 9) | (b << 18) | (e << 27); }; + constexpr UINT packed_values[] = {rgb9e5(64, 128, 256, 14), rgb9e5(64, 128, 256, 15), + rgb9e5(64, 128, 256, 16), rgb9e5(256, 64, 128, 14)}; + for (UINT y = 0; y < size; ++y) { + auto* row = static_cast(mapped) + packed_placed.Offset + y * packed_placed.Footprint.RowPitch; + for (UINT x = 0; x < size; ++x) + std::memcpy(row + x * 4, &packed_values[(x + y) % 4], 4); + } + D3D12_COMMAND_QUEUE_DESC queue_desc{}; Ref queue; Ref allocator; @@ -482,6 +504,12 @@ int main() { const auto sampled = transition(texture.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE); list->ResourceBarrier(1, &sampled); + to_texture.pResource = packed.p; + from_upload.PlacedFootprint = packed_placed; + list->CopyTextureRegion(&to_texture, 0, 0, 0, &from_upload, nullptr); + const auto packed_sampled = transition(packed.p, D3D12_RESOURCE_STATE_COPY_DEST, + D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE); + list->ResourceBarrier(1, &packed_sampled); EXPECT(list->Close() == S_OK); ID3D12CommandList* lists[] = {list.p}; queue->ExecuteCommandLists(1, lists); @@ -489,15 +517,15 @@ int main() { EXPECT(SUCCEEDED(queue->Signal(fence.p, 1))); EXPECT(SUCCEEDED(fence->SetEventOnCompletion(1, nullptr))); - // Set 0 now holds what the views and the sampler named. Three runs of the shader: the view's own slot, its - // copy, and the slot of bytes no view was made for; each with the sampler's S#. + // Set 0 now holds what the views and the sampler named. Four runs of the shader: the view's own slot, its + // copy, the slot of bytes no view was made for, and the packed RGB view; each with the sampler's S#. const VkDescriptorSet set0 = rt::d3d12::bindless_set(device.p); const VkDescriptorSetLayout set0_layout = rt::d3d12::bindless_set_layout(device.p); EXPECT(set0 && set0_layout); #ifdef LEONIDA_TEST_CS_SAMPLE_SPV - SampleKeys keys[3] = {}; - const UINT tables[3] = {own, copied, unknown}; - for (UINT k = 0; k < 3; ++k) { + const UINT tables[] = {own, copied, unknown, packed_slot}; + SampleKeys keys[std::size(tables)] = {}; + for (UINT k = 0; k < std::size(tables); ++k) { std::memcpy(keys[k].tsharp, view_bytes(tables[k]), sizeof(keys[k].tsharp)); std::memcpy(keys[k].ssharp, sampler_bytes, sizeof(keys[k].ssharp)); keys[k].first = k * texels; @@ -508,6 +536,7 @@ int main() { // A destroyed texture takes its views out of the set: the next ExecuteCommandLists writes that, and nothing // may still name the destroyed image views (the validation layer reports a set that does). + packed.reset(); texture.reset(); EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr))); EXPECT(list->Close() == S_OK); @@ -546,6 +575,15 @@ int main() { // image, one colour everywhere. unsigned mismatches = 0; for (UINT i = 0; i < texels; ++i) { + // Each RGB channel is mantissa * 2^(exponent - 24). The shader packs the result to RGBA8. + constexpr std::uint8_t packed_expected[][4] = {{16, 32, 64, 255}, {32, 64, 128, 255}, + {64, 128, 255, 255}, {64, 16, 32, 255}}; + const auto* packed_want = packed_expected[(i % size + i / size) % 4]; + const auto* packed_got = results.data() + 3 * bytes + i * 4; + if (std::memcmp(packed_got, packed_want, 4) != 0 && mismatches++ < 4) + std::fprintf(stderr, "RGB9E5 texel %u: got %u,%u,%u,%u, want %u,%u,%u,%u\n", i, + packed_got[0], packed_got[1], packed_got[2], packed_got[3], + packed_want[0], packed_want[1], packed_want[2], packed_want[3]); const auto want = texel(i % size, i / size); for (UINT k = 0; k < 2; ++k) { const std::uint8_t* got = results.data() + k * bytes + i * 4; @@ -570,7 +608,7 @@ int main() { return 1; } std::printf("descriptors: copies matched; a T# and an S# from CreateShaderResourceView and CreateSampler were " - "found in the device's bindless set by a shader, %u texels matched twice, and an unknown descriptor " + "found in the device's bindless set by a shader, %u RGBA8 and RGB9E5 texels matched, and an unknown descriptor " "and a destroyed texture's gave the fallback\n", texels); return 0; } diff --git a/runtime/test/d3d12_vulkan_device_test.cpp b/runtime/test/d3d12_vulkan_device_test.cpp index 9982af851..0aa69a99f 100644 --- a/runtime/test/d3d12_vulkan_device_test.cpp +++ b/runtime/test/d3d12_vulkan_device_test.cpp @@ -139,6 +139,7 @@ int compare_footprints(ID3D12Device* ours) { {volume_desc(5), 5}, {buffer_desc(300), 1}, {texture_desc(8, 8, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 4, 2), 8}, + {texture_desc(13, 7, DXGI_FORMAT_R9G9B9E5_SHAREDEXP, D3D12_RESOURCE_FLAG_NONE, 4, 2), 8}, {texture_desc(13, 7, DXGI_FORMAT_R16G16B16A16_FLOAT, D3D12_RESOURCE_FLAG_NONE, 4), 4}, {texture_desc(10, 6, DXGI_FORMAT_BC1_UNORM, D3D12_RESOURCE_FLAG_NONE, 4), 4}, {texture_desc(4, 4, DXGI_FORMAT_BC7_UNORM_SRGB, D3D12_RESOURCE_FLAG_NONE, 1, 3), 3},