From bbf7c82c656b7cc808f93ec7204ac92a6a2bebbe Mon Sep 17 00:00:00 2001 From: DadaCap <167002618+DadaCap@users.noreply.github.com> Date: Tue, 24 Nov 2026 21:14:28 +0100 Subject: [PATCH] d3d12: RGBA8 render targets of a console device take UNORM and sRGB RTVs without a list (D3D-RGBA8-SRGB-UNORM-RTV) Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 9 +++ runtime/README.md | 4 +- runtime/shims/d3d12_vulkan_device.cpp | 17 ++++- runtime/test/d3d12_draw_test.cpp | 98 ++++++++++++++++++++++++++- 4 files changed, 123 insertions(+), 5 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 3cd9d6d5e8..a5adcc7d63 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2631,6 +2631,15 @@ The trace will enumerate them. `R8G8B8A8`, a `B8G8R8A8` and a `B8G8R8X8` texture each take their own and their listed format and refuse 3 formats their lists don't name, `d3d12_draw_test` draws through the same views, and `typeless_rtv_test` checks the replay's crossing and its refusals. +- **RGBA8 render targets on a console device:** the console views an RGBA8 render target as UNORM and as sRGB without + a castable-format list. The retail title draws its Bink movie through an `R8G8B8A8_UNORM` RTV of an + `R8G8B8A8_UNORM_SRGB` texture made with `ALLOW_RENDER_TARGET` and no list (measured on a retail boot, RTX 5080); + the RTV was refused, so the draw had no target. A device the Xbox-ABI proxy owns (`set_device_outer`) therefore + gives each such 2D `R8G8B8A8_UNORM` or `_SRGB` texture without a list of its own, and without unordered access, the + list {UNORM, SRGB} (`Resource::create_texture`), and views, clears and draws follow the castable-list rules above. + A device without the proxy still refuses the RTV, as the PC does. `d3d12_draw_test` clears and draws through both + casts, reads the bytes back and checks the refusal without the proxy. Textures with unordered access, the B8G8R8A8 + formats and other families are not covered: no such cast has been seen. - **Typed RTV clears:** `ClearRenderTargetView` still takes `FLOAT[4]`. For every integer RTV format the device exposes (`R8G8`, `R8G8B8A8`, `R16G16`, `R16G16B16A16`, `R32`, `R32G32` and `R32G32B32A32` in `_UINT` and `_SINT`, plus `R10G10B10A2_UINT`; `R8`/`R16` `_UINT`/`_SINT` are handled in replay too) convert those values numerically diff --git a/runtime/README.md b/runtime/README.md index 61794a2c2d..d569f34b42 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -526,8 +526,8 @@ $env:VK_VALIDATION_VALIDATE_SYNC = '1' RGBA8 typeless RTV clears support explicit UNORM/SRGB/UINT/SINT/SNORM views of 2D mip slices and contiguous array-layer ranges, and MSAA. Other typeless color families also accept same-family typed RTVs, subject to Vulkan -color-attachment support. Typed-resource RTV casts are limited to R8G8B8A8_UNORM resources whose castable-format -list allows the view. A 1D texture cannot be created as a render target or depth buffer (`CreateCommittedResource` +color-attachment support. Typed-resource RTV casts are limited to resources whose castable-format list allows +the view and to the UNORM and sRGB views of RGBA8 render targets on a console (Xbox-ABI proxy) device. A 1D texture cannot be created as a render target or depth buffer (`CreateCommittedResource` refuses `ALLOW_RENDER_TARGET`/`ALLOW_DEPTH_STENCIL` on it), and 3D typeless view reinterpretation remains refused. ### Resource descriptions diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index e9bc191952..b52d72bf4e 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -624,6 +624,7 @@ struct DeviceState { // GPUs), backed by a mutable image of the UNORM format of the same order. The host runtime makes such textures. bool rgba8_srgb_uav_emulated = false; bool bgra8_srgb_uav_emulated = false; + bool console = false; // the Xbox-ABI proxy owns the device (set_device_outer) // Cached physical-device no-attachment sample counts for D3D12_OPTIONS19. UINT no_output_sample_counts = 0; @@ -2184,7 +2185,7 @@ class Resource final return S_OK; } - HRESULT create_texture(const Format& format, const std::vector& castable = {}) { + HRESULT create_texture(const Format& format, std::vector castable = {}) { auto& vk = device_.context->vk; const bool volume = desc_.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D; const UINT depth = volume ? desc_.DepthOrArraySize : 1; @@ -2192,6 +2193,15 @@ class Resource final if (desc_.MipLevels == 0) desc_.MipLevels = full_mips(desc_.Width, desc_.Height, depth); TextureCreateInfo create(desc_, format, device_.color_attachment_usable(format.format)); VkImageCreateInfo& info = create.info; + if (castable.empty() && device_.console && desc_.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D && + (desc_.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) && + !(desc_.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS) && + (desc_.Format == DXGI_FORMAT_R8G8B8A8_UNORM || desc_.Format == DXGI_FORMAT_R8G8B8A8_UNORM_SRGB)) { + // The console views an RGBA8 render target as UNORM and as sRGB without a list (the retail title's movie + // draws through a UNORM RTV of an sRGB texture); the PC needs the list. + castable = {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_SRGB}; + castable_formats_ = {DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB}; + } if (!castable.empty()) { // D3D12 always allows a view in the resource's own format, whether or not the castable list names // it. Vulkan requires every view format to be in the image's format list, so add it when missing. @@ -5524,7 +5534,10 @@ class Device final } bool bind_sampler(const leonida::gfx10::SamplerWords& words) { return state_.bind_sampler(words); } void own(const OwnedRuntime& owned) { owned_ = owned; } - void set_outer(IUnknown* outer) { outer_ = outer; } + void set_outer(IUnknown* outer) { + outer_ = outer; + state_.console = rt::d3d12_xs::is_device_proxy(outer); + } UINT STDMETHODCALLTYPE GetNodeCount() override { return 1; } diff --git a/runtime/test/d3d12_draw_test.cpp b/runtime/test/d3d12_draw_test.cpp index 36e2f6e37a..a333d9b97d 100644 --- a/runtime/test/d3d12_draw_test.cpp +++ b/runtime/test/d3d12_draw_test.cpp @@ -11,7 +11,9 @@ // - full-screen blends that read destination alpha into B8G8R8X8 and B8G8R8A8 targets cleared with alpha 0.25: // B8G8R8X8 has no alpha, so destination alpha reads 1 there, as on WARP and AMD's D3D12 driver; // - draws through RTVs in a format the texture's castable list names, in the other channel order (RGBA8 and BGRA8) -// or across B8G8R8X8 and B8G8R8A8: the bytes land in the view's order and encoding. +// or across B8G8R8X8 and B8G8R8A8: the bytes land in the view's order and encoding; +// - a clear and a draw through the UNORM RTV of an sRGB render target (and the reverse) made without a castable list: +// the console allows them, so the proxied device does; a device without the proxy refuses the view, as the PC does. // The shaders are GLSL written to the translator's contract (test/glsl/vs_draw.vert, ps_draw.frag). No game data. // Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation. #include "shader_fixture_path.h" @@ -1009,6 +1011,100 @@ int main() { unsigned(std::size(casts))); } + // Without a castable list: the RGBA8 render targets of a console device take RTVs in both encodings. The + // retail title's movie is drawn through a UNORM RTV of an sRGB texture this way. + { + struct ConsoleCast { DXGI_FORMAT resource, view; std::uint8_t clear[4], draw[4]; }; + const ConsoleCast casts[] = { + {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, DXGI_FORMAT_R8G8B8A8_UNORM, {51, 102, 153, 255}, {204, 153, 0, 255}}, + {DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, {124, 170, 203, 255}, {231, 203, 0, 255}}, + }; + const D3D12_DESCRIPTOR_HEAP_DESC console_heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1, + D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; + Ref heap; + EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&console_heap_desc, IID_PPV_ARGS(&heap.p)))); + const D3D12_CPU_DESCRIPTOR_HANDLE console_rtv{ + SIZE_T(reinterpret_cast(slot(heap.p, 9))(heap.p))}; + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + desc.Width = desc.Height = 4; + desc.DepthOrArraySize = 1; + desc.MipLevels = 1; + desc.SampleDesc.Count = 1; + desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; + const D3D12_VIEWPORT cast_viewport{0, 0, 4, 4, 0, 1}; + const D3D12_RECT cast_scissor{0, 0, 4, 4}; + const float clear_value[4] = {0.2f, 0.4f, 0.6f, 1.0f}; + UINT64 cast_fence = 90; // after the castable draws' 71..78 + for (const ConsoleCast& c : casts) { + desc.Format = c.resource; + D3D12_RENDER_TARGET_VIEW_DESC view{}; + view.Format = c.view; + view.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D; + + // The same device without the proxy as its outer is a PC device: the RTV is refused. + rt::d3d12::set_device_outer(inner, nullptr); + Ref pc_target; + const HRESULT pc_created = inner->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &desc, + D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr, IID_PPV_ARGS(&pc_target.p)); + rt::d3d12::set_device_outer(inner, device.p); + EXPECT(SUCCEEDED(pc_created)); + device->CreateRenderTargetView(pc_target.p, &view, console_rtv); + EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr))); + list->ClearRenderTargetView(console_rtv, clear_value, 0, nullptr); + EXPECT(FAILED(list->Close())); + + Ref console_target; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &desc, + D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr, IID_PPV_ARGS(&console_target.p)))); + device->CreateRenderTargetView(console_target.p, &view, console_rtv); + D3D12_GRAPHICS_PIPELINE_STATE_DESC console_pso_desc = fullscreen_desc; + console_pso_desc.RTVFormats[0] = c.view; + Ref console_pso; + EXPECT(SUCCEEDED(device->CreateGraphicsPipelineState(&console_pso_desc, IID_PPV_ARGS(&console_pso.p)))); + for (const bool draw : {false, true}) { + EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, console_pso.p))); + list->SetGraphicsRootSignature(root.p); + list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + list->RSSetViewports(1, &cast_viewport); + list->RSSetScissorRects(1, &cast_scissor); + list->OMSetRenderTargets(1, &console_rtv, FALSE, nullptr); + if (draw) { + list->SetGraphicsRootConstantBufferView(1, upload_va + 2048); // the castable draws' tint + list->DrawInstanced(3, 1, 0, 0); + } else { + list->ClearRenderTargetView(console_rtv, clear_value, 0, nullptr); + } + const auto console_to_copy = transition(console_target.p, D3D12_RESOURCE_STATE_RENDER_TARGET, + D3D12_RESOURCE_STATE_COPY_SOURCE); + list->ResourceBarrier(1, &console_to_copy); + D3D12_TEXTURE_COPY_LOCATION to{readback.p, D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, {}}; + to.PlacedFootprint = {0, {c.resource, 4, 4, 1, 256}}; + D3D12_TEXTURE_COPY_LOCATION from{console_target.p, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, {}}; + list->CopyTextureRegion(&to, 0, 0, 0, &from, nullptr); + const auto console_to_target = transition(console_target.p, D3D12_RESOURCE_STATE_COPY_SOURCE, + D3D12_RESOURCE_STATE_RENDER_TARGET); + list->ResourceBarrier(1, &console_to_target); + EXPECT(list->Close() == S_OK); + queue->ExecuteCommandLists(1, lists); + EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success); + ++cast_fence; + EXPECT(SUCCEEDED(queue->Signal(fence.p, cast_fence)) && + SUCCEEDED(fence->SetEventOnCompletion(cast_fence, nullptr))); + const auto* got = static_cast(mapped); + const std::uint8_t* want = draw ? c.draw : c.clear; + for (unsigned b = 0; b < 4; ++b) { + if (std::abs(int(got[b]) - int(want[b])) <= 1) continue; // the encoding's rounding + std::fprintf(stderr, "console cast, %s through RTV format %d of format %d: got %u,%u,%u,%u, " + "byte %u wants %u\n", draw ? "draw" : "clear", int(c.view), int(c.resource), + got[0], got[1], got[2], got[3], b, unsigned(want[b])); + return 1; + } + } + } + std::printf("console RGBA8 render targets: clears and draws through both encodings without a list\n"); + } + // A draw without a topology is invalid; Close reports it. EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, pso.p))); list->SetGraphicsRootSignature(root.p);