Skip to content
Open
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
10 changes: 10 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ What is implemented (all of it testable without the game):
depth with stencil (two planes), a D24 stored as D32, and multisampled textures; they are still refused. A float
written into a `D32_SFLOAT` image outside [0, 1] is undefined in Vulkan without `VK_EXT_depth_range_unrestricted`,
where D3D12 keeps the bits; `d3d12_copy_xfmt_test` uses values in [0, 1).
**The Xbox D16_UNORM_S8_UINT** (DXGI_FORMAT 118, past B4G4R4A4_UNORM; plane views 119 R16_UNORM_X8_TYPELESS
and 120 X16_TYPELESS_G8_UINT): the retail executable sizes one with `GetResourceAllocationInfo` at start-up,
asserts when the answer is the invalid one, and names 118 as a pipeline `DSVFormat`. Vulkan's
`D16_UNORM_S8_UINT` is missing on NVIDIA and RADV, so the layer stores `D32_SFLOAT_S8_UINT` on every GPU: each
16-bit depth value is held as a float and read back through the depth view as the same [0, 1] value.
`CheckFeatureSupport` answers the three formats with the D32S8 family's answers (two planes; depth-stencil for 118,
shader-readable for the views) and gives the caller its format back. Not done: a footprint copy of the depth
plane has the D32 texel size, 4 bytes, not the console's 2 (no conversion pass as for D24); nothing measured
copies one yet. `d3d12_xbox_d16s8_test` checks the queries, the allocation size, creation with a 118 clear value
and depth-and-stencil clears through a typed and an UNKNOWN DSV.
**D24_UNORM_S8_UINT without a native D24** (the Radeon 880M, AMD hardware generally, RADV): the layer stores
`D32_SFLOAT_S8_UINT` and makes it behave as D24 (`src/depth24.h`, `src/vulkan_depth24.h`). A footprint
of the depth plane holds 24-bit UNORM words (bits 0-23; the top byte is unspecified, the PC runtime reads it as
Expand Down
8 changes: 8 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1935,6 +1935,14 @@ if(TARGET Vulkan::Headers)
target_compile_options(d3d12_d24_support_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME d3d12_d24_support COMMAND d3d12_d24_support_test)
set_tests_properties(d3d12_d24_support PROPERTIES SKIP_RETURN_CODE 77)
# The Xbox D16_UNORM_S8_UINT depth format (118) and its plane views (119, 120): format queries, allocation size,
# creation and depth-stencil clears on the Vulkan-backed device.
add_executable(d3d12_xbox_d16s8_test test/d3d12_xbox_d16s8_test.cpp)
target_link_libraries(d3d12_xbox_d16s8_test PRIVATE gta6rt_d3d12_com d3d12 dxgi)
target_compile_definitions(d3d12_xbox_d16s8_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(d3d12_xbox_d16s8_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME d3d12_xbox_d16s8 COMMAND d3d12_xbox_d16s8_test)
set_tests_properties(d3d12_xbox_d16s8 PROPERTIES SKIP_RETURN_CODE 77)
add_executable(d3d12_b5g5r5a1_uav_test test/d3d12_b5g5r5a1_uav_test.cpp)
target_link_libraries(d3d12_b5g5r5a1_uav_test PRIVATE gta6rt_d3d12_com d3d12 dxgi)
target_compile_definitions(d3d12_b5g5r5a1_uav_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
Expand Down
43 changes: 40 additions & 3 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,28 @@ constexpr Format rgb96_format(VkFormat wide, bool typeless = false) {
// DXGI -> Vulkan for the formats render targets, depth buffers and common textures use. R32 and R16 typeless become
// depth formats when the resource allows depth-stencil; R32G8X24 and R24G8 always do, as Vulkan has no colour format
// with their layout.
// Xbox Series extensions to DXGI_FORMAT, past B4G4R4A4_UNORM (115), in the console's numbering:
// 118 D16_UNORM_S8_UINT and its two plane views, 119 R16_UNORM_X8_TYPELESS (depth) and 120 X16_TYPELESS_G8_UINT
// (stencil). The 1.0.2.1 executable names 118 as a pipeline DSVFormat and in GetResourceAllocationInfo at start-up,
// and asserts when the allocation answer is the invalid one. Vulkan's D16_UNORM_S8_UINT is missing on NVIDIA and
// RADV, so the texture is a D32_SFLOAT_S8_UINT image: every 16-bit depth value is held without loss and read back
// through the depth view as the same [0, 1] value. A copy of its depth plane to a buffer has the D32 footprint (4
// bytes a texel, not 2); nothing measured makes one yet.
constexpr DXGI_FORMAT kXboxD16UnormS8Uint = static_cast<DXGI_FORMAT>(118);
constexpr DXGI_FORMAT kXboxR16UnormX8Typeless = static_cast<DXGI_FORMAT>(119);
constexpr DXGI_FORMAT kXboxX16TypelessG8Uint = static_cast<DXGI_FORMAT>(120);

// The PC format whose answers stand in for an Xbox depth-stencil format in CheckFeatureSupport; the format itself
// otherwise.
DXGI_FORMAT pc_alias_of_xbox_depth_format(DXGI_FORMAT format) {
switch (format) {
case kXboxD16UnormS8Uint: return DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
case kXboxR16UnormX8Typeless: return DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS;
case kXboxX16TypelessG8Uint: return DXGI_FORMAT_X32_TYPELESS_G8X24_UINT;
default: return format;
}
}

Format vulkan_format(DXGI_FORMAT format, bool depth) {
constexpr VkImageAspectFlags color = VK_IMAGE_ASPECT_COLOR_BIT, d = VK_IMAGE_ASPECT_DEPTH_BIT,
ds = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
Expand Down Expand Up @@ -359,6 +381,7 @@ Format vulkan_format(DXGI_FORMAT format, bool depth) {
case DXGI_FORMAT_D16_UNORM: return {VK_FORMAT_D16_UNORM, d};
case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: return {VK_FORMAT_D32_SFLOAT_S8_UINT, ds};
case DXGI_FORMAT_D24_UNORM_S8_UINT: return {VK_FORMAT_D24_UNORM_S8_UINT, ds};
case kXboxD16UnormS8Uint: return {VK_FORMAT_D32_SFLOAT_S8_UINT, ds};
case DXGI_FORMAT_R32_TYPELESS:
return depth ? Format{VK_FORMAT_D32_SFLOAT, d, true} : Format{VK_FORMAT_R32_SFLOAT, color, true};
case DXGI_FORMAT_R16_TYPELESS:
Expand Down Expand Up @@ -1449,6 +1472,7 @@ DXGI_FORMAT format_family(DXGI_FORMAT format) {
if (in(15, 18)) return DXGI_FORMAT_R32G32_TYPELESS;
if (in(19, 22)) return DXGI_FORMAT_R32G8X24_TYPELESS;
if (in(23, 25)) return DXGI_FORMAT_R10G10B10A2_TYPELESS;
if (in(118, 120)) return kXboxD16UnormS8Uint; // the Xbox D16S8 format and its two plane views
if (in(27, 32)) return DXGI_FORMAT_R8G8B8A8_TYPELESS;
if (in(33, 38)) return DXGI_FORMAT_R16G16_TYPELESS;
if (in(39, 43)) return DXGI_FORMAT_R32_TYPELESS;
Expand All @@ -1473,7 +1497,8 @@ DXGI_FORMAT format_family(DXGI_FORMAT format) {
bool depth_stencil_family(DXGI_FORMAT format) {
const DXGI_FORMAT family = format_family(format);
return family == DXGI_FORMAT_R32_TYPELESS || family == DXGI_FORMAT_R16_TYPELESS ||
family == DXGI_FORMAT_R24G8_TYPELESS || family == DXGI_FORMAT_R32G8X24_TYPELESS;
family == DXGI_FORMAT_R24G8_TYPELESS || family == DXGI_FORMAT_R32G8X24_TYPELESS ||
family == kXboxD16UnormS8Uint;
}

// ALLOW_RENDER_TARGET: the host refuses the families below and accepts the rest, D32_FLOAT, D16_UNORM,
Expand Down Expand Up @@ -4681,7 +4706,8 @@ bool image_descriptor(const Resource& resource, const TextureView& view, leonida
(view.format == DXGI_FORMAT_X24_TYPELESS_G8_UINT &&
(desc.Format == DXGI_FORMAT_R24G8_TYPELESS || desc.Format == DXGI_FORMAT_D24_UNORM_S8_UINT)) ||
(view.format == DXGI_FORMAT_X32_TYPELESS_G8X24_UINT &&
(desc.Format == DXGI_FORMAT_R32G8X24_TYPELESS || desc.Format == DXGI_FORMAT_D32_FLOAT_S8X24_UINT));
(desc.Format == DXGI_FORMAT_R32G8X24_TYPELESS || desc.Format == DXGI_FORMAT_D32_FLOAT_S8X24_UINT)) ||
(view.format == kXboxX16TypelessG8Uint && desc.Format == kXboxD16UnormS8Uint);
if (stencil && !stencil_format_fits) {
log_once("a stencil-plane view needs its texture family's stencil view format; format", int(view.format));
return false;
Expand Down Expand Up @@ -4741,7 +4767,9 @@ bool image_descriptor(const Resource& resource, const TextureView& view, leonida
(desc.Format == DXGI_FORMAT_R24G8_TYPELESS && view.format == DXGI_FORMAT_R24_UNORM_X8_TYPELESS) ||
(desc.Format == DXGI_FORMAT_D32_FLOAT_S8X24_UINT &&
(view.format == DXGI_FORMAT_UNKNOWN || view.format == DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS)) ||
(desc.Format == DXGI_FORMAT_R32G8X24_TYPELESS && view.format == DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS);
(desc.Format == DXGI_FORMAT_R32G8X24_TYPELESS && view.format == DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS) ||
(desc.Format == kXboxD16UnormS8Uint &&
(view.format == DXGI_FORMAT_UNKNOWN || view.format == kXboxR16UnormX8Typeless));
if (!depth_format_fits) {
log_once("incompatible depth shader-resource view format:", int(view.format));
return false;
Expand Down Expand Up @@ -7830,6 +7858,9 @@ class Device final
case DXGI_FORMAT_D24_UNORM_S8_UINT:
case DXGI_FORMAT_R24_UNORM_X8_TYPELESS:
case DXGI_FORMAT_X24_TYPELESS_G8_UINT:
case kXboxD16UnormS8Uint:
case kXboxR16UnormX8Typeless:
case kXboxX16TypelessG8Uint:
case DXGI_FORMAT_NV12:
case DXGI_FORMAT_P010:
case DXGI_FORMAT_P016:
Expand All @@ -7852,6 +7883,12 @@ class Device final
case D3D12_FEATURE_FORMAT_SUPPORT: {
if (size != sizeof(D3D12_FEATURE_DATA_FORMAT_SUPPORT)) return d3d12_trace::result(method, E_INVALIDARG);
auto* fs = static_cast<D3D12_FEATURE_DATA_FORMAT_SUPPORT*>(data);
struct RestoreFormat {
D3D12_FEATURE_DATA_FORMAT_SUPPORT* fs;
DXGI_FORMAT asked;
~RestoreFormat() { fs->Format = asked; }
} restore{fs, fs->Format};
fs->Format = pc_alias_of_xbox_depth_format(fs->Format); // what the texture is stored as
fs->Support1 = D3D12_FORMAT_SUPPORT1_NONE;
fs->Support2 = D3D12_FORMAT_SUPPORT2_NONE;
// The host runtime's format table (compare_invalid_arguments, "format support:"): a value it has no entry
Expand Down
185 changes: 185 additions & 0 deletions runtime/test/d3d12_xbox_d16s8_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// The Xbox Series depth-stencil format D16_UNORM_S8_UINT (DXGI_FORMAT 118, past B4G4R4A4_UNORM) and its plane views
// R16_UNORM_X8_TYPELESS (119) and X16_TYPELESS_G8_UINT (120) on the Vulkan-backed device.
//
// The retail executable sizes a 118 texture with GetResourceAllocationInfo at start-up and asserts that the answer is
// not the invalid one; it also names 118 as a pipeline DSVFormat. The layer stores the texture as D32_SFLOAT_S8_UINT
// (NVIDIA and RADV have no Vulkan D16_UNORM_S8_UINT). This checks, on whatever GPU runs it:
// * FORMAT_INFO gives two planes for the three formats;
// * FORMAT_SUPPORT answers 118 as a 2D depth-stencil format and the two plane views as shader-readable, and gives
// the caller its own format back in the structure;
// * GetResourceAllocationInfo sizes a full-screen 118 depth buffer (not UINT64_MAX), at least two bytes a texel
// for the depth plane;
// * a 118 depth buffer is created with a 118 clear value, takes DSVs in its own format and in UNKNOWN, and a
// depth-and-stencil clear through them runs to its fence.
//
// Synthetic resources only; no game files. LEONIDA_VK_VALIDATION=1 checks Vulkan usage.
#include "vk_test_device.h"
#include "../shims/d3d12_com.h"

#include <cstdint>
#include <cstdio>

namespace {

constexpr auto kD16S8 = static_cast<DXGI_FORMAT>(118);
constexpr auto kR16X8 = static_cast<DXGI_FORMAT>(119);
constexpr auto kX16G8 = static_cast<DXGI_FORMAT>(120);

int g_failures = 0;

void check(bool ok, const char* what) {
std::printf("%s %s\n", ok ? "PASS" : "FAIL", what);
if (!ok) ++g_failures;
}

template <class T>
struct Ref {
T* p = nullptr;
~Ref() { if (p) p->Release(); }
T* operator->() const { return p; }
};

D3D12_RESOURCE_DESC depth_texture(UINT64 width, UINT height) {
D3D12_RESOURCE_DESC desc{};
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
desc.Width = width;
desc.Height = height;
desc.DepthOrArraySize = 1;
desc.MipLevels = 1;
desc.Format = kD16S8;
desc.SampleDesc.Count = 1;
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
return desc;
}

void format_queries(ID3D12Device* device) {
for (const DXGI_FORMAT format : {kD16S8, kR16X8, kX16G8}) {
D3D12_FEATURE_DATA_FORMAT_INFO info{format};
const HRESULT hr = device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, &info, sizeof(info));
std::printf("format %d: FORMAT_INFO hr=0x%08lx planes %u\n", static_cast<int>(format),
static_cast<unsigned long>(hr), static_cast<unsigned>(info.PlaneCount));
check(SUCCEEDED(hr) && info.PlaneCount == 2, "FORMAT_INFO: two planes");
}

D3D12_FEATURE_DATA_FORMAT_SUPPORT fs{kD16S8};
HRESULT hr = device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &fs, sizeof(fs));
std::printf("format 118: FORMAT_SUPPORT hr=0x%08lx Support1=0x%08x\n", static_cast<unsigned long>(hr), fs.Support1);
check(SUCCEEDED(hr), "FORMAT_SUPPORT answers D16_UNORM_S8_UINT");
check((fs.Support1 & D3D12_FORMAT_SUPPORT1_DEPTH_STENCIL) && (fs.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE2D),
"D16_UNORM_S8_UINT is a 2D depth-stencil format");
check(fs.Format == kD16S8, "FORMAT_SUPPORT leaves the caller's format in the structure");

for (const DXGI_FORMAT plane : {kR16X8, kX16G8}) {
D3D12_FEATURE_DATA_FORMAT_SUPPORT view{plane};
hr = device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &view, sizeof(view));
std::printf("format %d: FORMAT_SUPPORT hr=0x%08lx Support1=0x%08x\n", static_cast<int>(plane),
static_cast<unsigned long>(hr), view.Support1);
check(SUCCEEDED(hr) && (view.Support1 & D3D12_FORMAT_SUPPORT1_SHADER_LOAD) && view.Format == plane,
"a D16_UNORM_S8_UINT plane view format is shader-readable");
}
}

void allocation(ID3D12Device* device) {
const D3D12_RESOURCE_DESC desc = depth_texture(2560, 1440);
const D3D12_RESOURCE_ALLOCATION_INFO info = device->GetResourceAllocationInfo(0, 1, &desc);
std::printf("format 118 2560x1440: GetResourceAllocationInfo size %llu alignment %llu\n",
static_cast<unsigned long long>(info.SizeInBytes), static_cast<unsigned long long>(info.Alignment));
check(info.SizeInBytes != UINT64_MAX, "GetResourceAllocationInfo sizes a D16_UNORM_S8_UINT depth buffer");
check(info.SizeInBytes != UINT64_MAX && info.SizeInBytes >= UINT64(2560) * 1440 * 2,
"the size holds two bytes of depth a texel");
}

bool clear_runs(ID3D12Device* device) {
Ref<ID3D12CommandQueue> queue;
Ref<ID3D12CommandAllocator> allocator;
Ref<ID3D12GraphicsCommandList> list;
Ref<ID3D12Fence> fence;
Ref<ID3D12DescriptorHeap> heap;
Ref<ID3D12Resource> depth;
D3D12_COMMAND_QUEUE_DESC queue_desc{};
if (FAILED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p))) ||
FAILED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p))) ||
FAILED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr,
IID_PPV_ARGS(&list.p))) ||
FAILED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p))))
return false;
const D3D12_DESCRIPTOR_HEAP_DESC heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 2, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
if (FAILED(device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&heap.p)))) return false;

D3D12_HEAP_PROPERTIES heap_props{D3D12_HEAP_TYPE_DEFAULT};
const D3D12_RESOURCE_DESC desc = depth_texture(64, 32);
D3D12_CLEAR_VALUE clear{};
clear.Format = kD16S8;
clear.DepthStencil.Depth = 1.0f;
const HRESULT made = device->CreateCommittedResource(&heap_props, D3D12_HEAP_FLAG_NONE, &desc,
D3D12_RESOURCE_STATE_DEPTH_WRITE, &clear,
IID_PPV_ARGS(&depth.p));
check(SUCCEEDED(made), "a D16_UNORM_S8_UINT depth buffer is created with its own clear value");
if (FAILED(made)) return false;

const UINT step = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV);
D3D12_CPU_DESCRIPTOR_HANDLE typed = heap->GetCPUDescriptorHandleForHeapStart();
D3D12_CPU_DESCRIPTOR_HANDLE implicit{typed.ptr + step};
D3D12_DEPTH_STENCIL_VIEW_DESC view{};
view.Format = kD16S8;
view.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
device->CreateDepthStencilView(depth.p, &view, typed);
device->CreateDepthStencilView(depth.p, nullptr, implicit);
const auto both = static_cast<D3D12_CLEAR_FLAGS>(D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL);
list->ClearDepthStencilView(typed, both, 0.25f, 0x5a, 0, nullptr);
const D3D12_RECT rect{4, 4, 20, 12};
list->ClearDepthStencilView(implicit, both, 0.75f, 0x3c, 1, &rect);
if (FAILED(list->Close())) return false;
ID3D12CommandList* lists[] = {list.p};
queue->ExecuteCommandLists(1, lists);
if (FAILED(queue->Signal(fence.p, 1))) return false;
HANDLE event = CreateEventW(nullptr, FALSE, FALSE, nullptr);
const bool done = event && SUCCEEDED(fence->SetEventOnCompletion(1, event)) &&
WaitForSingleObject(event, 5000) == WAIT_OBJECT_0;
if (event) CloseHandle(event);
return done;
}

} // namespace

int main() {
vktest::Device d;
const auto status = d.initialize();
if (status == vktest::DeviceStatus::unavailable) return 77;
if (status != vktest::DeviceStatus::ready) return 1;

VkFormatProperties d16s8{};
d.instance_fn<PFN_vkGetPhysicalDeviceFormatProperties>("vkGetPhysicalDeviceFormatProperties")(
d.physical, VK_FORMAT_D16_UNORM_S8_UINT, &d16s8);
std::printf("Vulkan D16_UNORM_S8_UINT depth attachment: %s (the layer stores D32_SFLOAT_S8_UINT either way)\n",
(d16s8.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) ? "native" : "no");

rt::d3d12::VulkanDeviceInfo info{};
info.get_instance_proc = d.instance_proc;
info.instance = d.instance;
info.physical = d.physical;
info.device = d.device;
info.queue = d.queue;
info.queue_family = d.family;
info.bindless = d.bindless;
info.bindless_partially_bound = d.partially_bound;
info.image_view_min_lod = d.image_view_min_lod;
info.host_image_copy = d.host_image_copy;
ID3D12Device* device = nullptr;
if (FAILED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&device))) || !device) {
std::printf("FAIL Vulkan-backed D3D12 device created\n");
return 1;
}

format_queries(device);
allocation(device);
check(clear_runs(device), "depth-and-stencil clears through a typed and an UNKNOWN DSV run to the fence");
device->Release();

if (g_failures) {
std::printf("d3d12_xbox_d16s8_test: %d checks failed\n", g_failures);
return 1;
}
std::printf("d3d12_xbox_d16s8_test: OK\n");
return 0;
}