Skip to content
Closed
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: 9 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2184,14 +2185,23 @@ class Resource final
return S_OK;
}

HRESULT create_texture(const Format& format, const std::vector<VkFormat>& castable = {}) {
HRESULT create_texture(const Format& format, std::vector<VkFormat> castable = {}) {
auto& vk = device_.context->vk;
const bool volume = desc_.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D;
const UINT depth = volume ? desc_.DepthOrArraySize : 1;
const bool one_d = desc_.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D;
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.
Expand Down Expand Up @@ -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; }

Expand Down
98 changes: 97 additions & 1 deletion runtime/test/d3d12_draw_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<ID3D12DescriptorHeap> heap;
EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&console_heap_desc, IID_PPV_ARGS(&heap.p))));
const D3D12_CPU_DESCRIPTOR_HANDLE console_rtv{
SIZE_T(reinterpret_cast<XboxHeapHandleFn>(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<ID3D12Resource> 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<ID3D12Resource> 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<ID3D12PipelineState> 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<const std::uint8_t*>(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);
Expand Down
Loading