diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 5882f9478e0..75b04609b9c 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1309,7 +1309,8 @@ if(TARGET Vulkan::Headers) target_compile_options(d3d12_clear_test PRIVATE /W4 /permissive- /utf-8) add_test(NAME d3d12_rgb9e5_rtv COMMAND d3d12_clear_test --rgb9e5) set_tests_properties(d3d12_rgb9e5_rtv PROPERTIES SKIP_RETURN_CODE 77) - # Not in ctest yet: on NVIDIA it fails on the 0.5 tie rounding #521 fixes; register it once that lands. + add_test(NAME d3d12_clear COMMAND d3d12_clear_test) + set_tests_properties(d3d12_clear PROPERTIES SKIP_RETURN_CODE 77) # Synthetic typed-view readbacks against Windows WARP; run explicitly on a Vulkan GPU. add_executable(d3d12_typeless_rtv_test test/d3d12_typeless_rtv_test.cpp) @@ -1890,6 +1891,14 @@ if(TARGET Vulkan::Headers) message(STATUS "llvm-mc (set LLVM_MC) or glslangValidator not found: d3d12_subgroup_test is not built") endif() + # Clears on every format class against WARP (needs a Vulkan GPU; exit 77 without one). + add_executable(d3d12_clear_formats_test test/d3d12_clear_formats_test.cpp) + target_link_libraries(d3d12_clear_formats_test PRIVATE gta6rt_d3d12_com d3d12 dxgi) + target_compile_definitions(d3d12_clear_formats_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + target_compile_options(d3d12_clear_formats_test PRIVATE /W4 /permissive- /utf-8) + add_test(NAME d3d12_clear_formats COMMAND d3d12_clear_formats_test) + set_tests_properties(d3d12_clear_formats PROPERTIES SKIP_RETURN_CODE 77) + # The loader can create this device instead of the host's (D3D12XboxCreateDevice, LEONIDA_D3D12=vulkan). if(TARGET gta6rt) target_link_libraries(gta6rt PRIVATE gta6rt_d3d12_com) diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 4185d6ff860..f8f62cc3220 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -40,6 +40,7 @@ #include #include +#include #include #include #include @@ -410,8 +411,8 @@ inline BlockFormat block_format(VkFormat format, VkImageAspectFlags aspect) { if (aspect == VK_IMAGE_ASPECT_STENCIL_BIT) return {1, 1}; switch (format) { case VK_FORMAT_R32G32B32A32_SFLOAT: - case VK_FORMAT_R32G32B32A32_UINT: - case VK_FORMAT_R32G32B32A32_SINT: return {16, 1}; + case VK_FORMAT_R32G32B32A32_SINT: + case VK_FORMAT_R32G32B32A32_UINT: return {16, 1}; case VK_FORMAT_R16G16B16A16_SFLOAT: case VK_FORMAT_R16G16B16A16_UNORM: case VK_FORMAT_R16G16B16A16_SNORM: @@ -1009,9 +1010,37 @@ struct PreparedDraw { } }; +// D3D converts a float clear value to a normalised format by rounding to nearest even, so an exact tie goes to the +// even neighbour (0.5 on 8 bits is 128). Vulkan leaves the rounding of ties to the GPU, and NVIDIA writes 127, one +// step below WARP and AMD. The value is rounded here and handed over as the exact fraction, which no GPU rounds +// differently. NaN is 0 and values outside 0..1 clamp, as in D3D. +// +// Ties go to even whatever the thread's floating-point rounding mode: std::nearbyint follows it, so a game that +// changes the mode would round differently. floor() does not depend on it, and x + 0.5 is exact here (x is at most +// a float times 65535, well inside a double's 53 bits). +inline double round_half_even(double x) { + const double r = std::floor(x + 0.5); + return r - x == 0.5 && std::fmod(r, 2.0) != 0.0 ? r - 1.0 : r; +} + +inline float quantize_unorm(float value, double maximum) { + if (!(value > 0.0f)) return 0.0f; + if (value >= 1.0f) return 1.0f; + return static_cast(round_half_even(double(value) * maximum) / maximum); +} + +// The same for a signed normalised format: -1..1 on a scale of 127 (8 bits) or 32767 (16 bits), where D3D writes -1.0 +// as -127 and ties go to the even neighbour. +inline float quantize_snorm(float value, double maximum) { + if (!(value > -1.0f)) return value != value ? 0.0f : -1.0f; + if (value >= 1.0f) return 1.0f; + return static_cast(round_half_even(double(value) * maximum) / maximum); +} + // ClearRenderTargetView takes FLOATs even for integer RTVs. Windows D3D12 (WARP and AMD) clamps to the // component range and truncates fractions. Vulkan instead interprets the matching union member as an integer -// already; copying float bits changes the value without a validation error. +// already; copying float bits changes the value without a validation error. The normalised formats are rounded like +// D3D (quantize_unorm, quantize_snorm). inline VkClearColorValue render_target_clear_color(VkFormat format, const ClearColor& input) { // Integer formats convert the float numerically: NaN maps to zero, values below the range clamp to the low // end, positive overflow (and +infinity) to the high end, and fractions truncate toward zero. Unsigned uses @@ -1019,6 +1048,8 @@ inline VkClearColorValue render_target_clear_color(VkFormat format, const ClearC constexpr double u32_max = 4294967295.0, s32_min = -2147483648.0, s32_max = 2147483647.0; double low = 0, high = 0, alpha_high = 0; bool is_integer = true, is_signed = false; + std::array unorm{}; // the largest value of each normalised channel; 0 leaves the channel to the GPU + double snorm = 0; // the largest value of a signed normalised format switch (format) { case VK_FORMAT_R8_UINT: case VK_FORMAT_R8G8_UINT: @@ -1039,13 +1070,33 @@ inline VkClearColorValue render_target_clear_color(VkFormat format, const ClearC case VK_FORMAT_R32_SINT: case VK_FORMAT_R32G32_SINT: case VK_FORMAT_R32G32B32A32_SINT: low = s32_min; high = s32_max; is_signed = true; break; + case VK_FORMAT_R8_SNORM: + case VK_FORMAT_R8G8_SNORM: + case VK_FORMAT_R8G8B8A8_SNORM: is_integer = false; snorm = 127; break; + case VK_FORMAT_R16_SNORM: + case VK_FORMAT_R16G16_SNORM: + case VK_FORMAT_R16G16B16A16_SNORM: is_integer = false; snorm = 32767; break; + case VK_FORMAT_R8_UNORM: + case VK_FORMAT_R8G8_UNORM: + case VK_FORMAT_R8G8B8A8_UNORM: + case VK_FORMAT_B8G8R8A8_UNORM: is_integer = false; unorm = {255, 255, 255, 255}; break; + case VK_FORMAT_R8G8B8A8_SRGB: + case VK_FORMAT_B8G8R8A8_SRGB: // the colour channels go through the sRGB curve on the GPU + is_integer = false; + unorm = {0, 0, 0, 255}; + break; + case VK_FORMAT_A2B10G10R10_UNORM_PACK32: is_integer = false; unorm = {1023, 1023, 1023, 3}; break; + case VK_FORMAT_R16_UNORM: + case VK_FORMAT_R16G16_UNORM: + case VK_FORMAT_R16G16B16A16_UNORM: is_integer = false; unorm = {65535, 65535, 65535, 65535}; break; default: is_integer = false; break; } if (format != VK_FORMAT_A2B10G10R10_UINT_PACK32) alpha_high = high; VkClearColorValue value{}; for (std::size_t i = 0; i < input.size(); ++i) { if (!is_integer) { - value.float32[i] = input[i]; + if (snorm != 0) value.float32[i] = quantize_snorm(input[i], snorm); + else value.float32[i] = unorm[i] != 0.0 ? quantize_unorm(input[i], unorm[i]) : input[i]; continue; } const double maximum = i == 3 ? alpha_high : high; @@ -3237,7 +3288,9 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c auto pieces = detail::current(tracker, id, range, image_layout(p.binding, state.layout)); detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker); const float depth = p.binding.depth24 ? depth24_clear_value(depth_clear->depth) // see depth24.h - : depth_clear->depth; + : p.binding.format == VK_FORMAT_D16_UNORM + ? detail::quantize_unorm(depth_clear->depth, 65535.0) // see quantize_unorm + : depth_clear->depth; const VkClearDepthStencilValue value{depth, depth_clear->stencil}; if (p.rects.empty()) { detail::round_trip(vk, command_buffer, p.binding, pieces, image_layout(p.binding, transfer_dst.layout), diff --git a/runtime/test/clear_color_test.cpp b/runtime/test/clear_color_test.cpp index 940ecb28770..fd350f6f4aa 100644 --- a/runtime/test/clear_color_test.cpp +++ b/runtime/test/clear_color_test.cpp @@ -1,7 +1,9 @@ -// Value-level replay regression: full and rectangular integer clears must carry integer values, not float bits. -// No Vulkan loader, GPU, shaders or game data. Expected conversions also match the WARP differential GPU test. +// Value-level replay regression: full and rectangular integer clears must carry integer values, not float bits, +// and normalised clears are rounded to nearest even like D3D (0.5 on 8 bits is 128). +// No Vulkan loader, GPU, shaders or game data. Expected conversions also match the WARP differential GPU tests. #include "../src/vulkan_replay.h" #include +#include #include #include #include @@ -116,7 +118,7 @@ int main() { if (!format_ok) ++failed_formats; } for (const auto& c : cases) { - for (const VkFormat format : {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R32_SFLOAT}) { + for (const VkFormat format : {VK_FORMAT_R32_SFLOAT, VK_FORMAT_R16G16B16A16_SFLOAT}) { for (const bool rect : {false, true}) { if (!replay_color(format, c, rect) || std::memcmp(captured.float32, c.data(), sizeof(captured)) != 0) { std::fprintf(stderr, "float clear: format %d rect %d changed the value\n", int(format), int(rect)); @@ -126,10 +128,102 @@ int main() { } } } + // Normalised formats: D3D rounds to nearest even, the clamp and NaN rules are 0..1 and 0, and an exact tie goes to + // the even neighbour. NVIDIA rounds 0.5 on 8 bits down to 127 by itself, so the value is rounded before Vulkan sees it. + struct Norm { + VkFormat format; + leonida::ClearColor color; + std::array steps; // what D3D writes, in units of the channel's largest value + std::array maximum; + }; + const Norm norms[] = { + {VK_FORMAT_R8G8B8A8_UNORM, {0.5f, 0.499f, 0.501f, 1.0f}, {128, 127, 128, 255}, {255, 255, 255, 255}}, + {VK_FORMAT_B8G8R8A8_UNORM, {-1, 2, nan, 0.5f}, {0, 255, 0, 128}, {255, 255, 255, 255}}, + {VK_FORMAT_R8G8_UNORM, {0.5f, 1.5f, 0, 0}, {128, 255, 0, 0}, {255, 255, 255, 255}}, + {VK_FORMAT_A2B10G10R10_UNORM_PACK32, {0.5f, 0.5f, 0.5f, 0.5f}, {512, 512, 512, 2}, {1023, 1023, 1023, 3}}, + {VK_FORMAT_R16G16B16A16_UNORM, {0.5f, 0.25f, 0.75f, 1.0f}, {32768, 16384, 49151, 65535}, + {65535, 65535, 65535, 65535}}, + {VK_FORMAT_R8G8B8A8_SRGB, {0.2f, 0.7f, 0.5f, 0.5f}, {0, 0, 0, 128}, {0, 0, 0, 255}}, // colour: the GPU's sRGB curve + {VK_FORMAT_B8G8R8A8_SRGB, {0.5f, 0.25f, 0.75f, 0.499f}, {0, 0, 0, 127}, {0, 0, 0, 255}}, + {VK_FORMAT_R16_UNORM, {0.5f, 0, 0, 0}, {32768, 0, 0, 0}, {65535, 65535, 65535, 65535}}, + // Signed normalised: -1.0 is -127 (-32767), the scale is 127 (32767) and ties go to the even neighbour. + {VK_FORMAT_R8G8B8A8_SNORM, {-1, -0.5f, 0.5f, 1}, {-127, -64, 64, 127}, {127, 127, 127, 127}}, + {VK_FORMAT_R8G8_SNORM, {nan, inf, -inf, -0.5f}, {0, 127, -127, -64}, {127, 127, 127, 127}}, + {VK_FORMAT_R16G16B16A16_SNORM, {-2, 2, 0.001f, -0.001f}, {-32767, 32767, 33, -33}, + {32767, 32767, 32767, 32767}}, + }; + for (const auto& n : norms) { + for (const bool rect : {false, true}) { + if (!replay_color(n.format, n.color, rect)) return 1; + for (unsigned i = 0; i < 4; ++i) { + const float expected = n.maximum[i] != 0.0 ? static_cast(double(n.steps[i]) / n.maximum[i]) : n.color[i]; + if (captured.float32[i] != expected) { + std::fprintf(stderr, "normalised clear: format %d rect %d channel %u: got %.9g, expected %.9g\n", + int(n.format), int(rect), i, double(captured.float32[i]), double(expected)); + return 1; + } + } + ++checks; + } + } + // The other integer formats clamp to the component range and truncate toward zero (what WARP writes). + struct Integer { + VkFormat format; + leonida::ClearColor color; + std::array expected; + }; + const Integer integers[] = { + {VK_FORMAT_R8_SINT, {-1.5f, -2.5f, 2.5f, 3.5f}, {-1, -2, 2, 3}}, + {VK_FORMAT_R8G8B8A8_SINT, {-129, 128, -32769, 32768}, {-128, 127, -128, 127}}, + {VK_FORMAT_R8G8B8A8_SINT, {2147483648.0f, -2147483904.0f, nan, inf}, {127, -128, 0, 127}}, + {VK_FORMAT_R16_SINT, {-129, 128, -32769, 32768}, {-129, 128, -32768, 32767}}, + {VK_FORMAT_R32_SINT, {-129, 128, -32769, 32768}, {-129, 128, -32769, 32768}}, + {VK_FORMAT_R32_SINT, {2147483648.0f, -2147483904.0f, nan, inf}, {2147483647, -2147483647 - 1, 0, 2147483647}}, + {VK_FORMAT_R32G32B32A32_SINT, {-inf, 0.5f, -0.5f, 100000.0f}, {-2147483647 - 1, 0, 0, 100000}}, + {VK_FORMAT_R8_UINT, {256, 1.5f, -1, nan}, {255, 1, 0, 0}}, + {VK_FORMAT_R16_UINT, {65536, 65535.5f, -0.0f, 2.5f}, {65535, 65535, 0, 2}}, + }; + for (const auto& c : integers) { + for (const bool rect : {false, true}) { + if (!replay_color(c.format, c.color, rect)) return 1; + for (unsigned i = 0; i < 4; ++i) { + if (captured.int32[i] != c.expected[i]) { + std::fprintf(stderr, "integer clear: format %d rect %d channel %u: got %d, expected %d\n", + int(c.format), int(rect), i, captured.int32[i], c.expected[i]); + return 1; + } + } + ++checks; + } + } + // Ties go to even whatever the thread's floating-point rounding mode (std::nearbyint would follow it: 127.5 is + // 127 when rounding down). Converted under each mode, checked after the default mode is back. + { + const int mode = std::fegetround(); + VkClearColorValue unorm[3]{}, snorm[3]{}; + const int modes[3] = {FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO}; + for (int m = 0; m < 3; ++m) { + std::fesetround(modes[m]); + unorm[m] = leonida::detail::render_target_clear_color(VK_FORMAT_R8G8B8A8_UNORM, {0.5f, 0.5f, 0.5f, 0.5f}); + snorm[m] = leonida::detail::render_target_clear_color(VK_FORMAT_R8G8B8A8_SNORM, {-0.5f, 0.5f, -0.5f, 0.5f}); + } + std::fesetround(mode); + for (int m = 0; m < 3; ++m) { + const long u = std::lround(double(unorm[m].float32[0]) * 255.0); + const long s0 = std::lround(double(snorm[m].float32[0]) * 127.0); + const long s1 = std::lround(double(snorm[m].float32[1]) * 127.0); + if (u != 128 || s0 != -64 || s1 != 64) { + std::fprintf(stderr, "tie rounding under FP mode %d: UNORM %ld (128), SNORM %ld %ld (-64 64)\n", + modes[m], u, s0, s1); + return 1; + } + ++checks; + } + } if (failed_formats) { std::fprintf(stderr, "clear_color_test: %u integer format(s) failed\n", failed_formats); return 1; } - std::printf("clear_color_test: %u full/rectangle integer and float replay checks passed\n", checks); + std::printf("clear_color_test: %u full/rectangle integer, normalised and float replay checks passed\n", checks); return 0; } diff --git a/runtime/test/d3d12_clear_formats_test.cpp b/runtime/test/d3d12_clear_formats_test.cpp new file mode 100644 index 00000000000..2c67d5563f2 --- /dev/null +++ b/runtime/test/d3d12_clear_formats_test.cpp @@ -0,0 +1,397 @@ +// Clears compared with native Windows D3D12/WARP across the format classes (D3D-45): ClearRenderTargetView on +// UNORM, SNORM, SRGB, FLOAT, UINT and SINT formats and ClearDepthStencilView on every depth format, each with and +// without rectangles. Every case clears, copies the result into a readback buffer through D3D12 and compares the +// bytes with what WARP wrote. Extends d3d12_uint_clear_test.cpp (UINT only). +// No game data or shaders. Run explicitly (needs Vulkan hardware and WARP); LEONIDA_VK_VALIDATION=1 checks Vulkan +// usage. A format the layer cannot create yet is listed as "not exposed" and does not fail the test; any other +// difference does. +#include "vk_test_device.h" +#include "../shims/d3d12_com.h" +#include +#include +#include +#include +#include +#include +#include +#define CHECK(condition) do { \ + if (!(condition)) { std::fprintf(stderr, " line %d: %s\n", __LINE__, #condition); return false; } \ +} while (0) + +namespace { +template +struct Ref { + T* p = nullptr; + ~Ref() { if (p) p->Release(); } + T* operator->() const { return p; } +}; +struct Event { + HANDLE handle = CreateEventW(nullptr, FALSE, FALSE, nullptr); + ~Event() { if (handle) CloseHandle(handle); } +}; + +constexpr UINT kWidth = 5, kHeight = 3; // odd sizes expose a wrong treatment of footprint padding +using Color = std::array; +constexpr float kNaN = std::numeric_limits::quiet_NaN(); +constexpr float kInf = std::numeric_limits::infinity(); + +// Records the copy of one subresource into a readback buffer, to be read after the list has run. +struct Plane { + Ref buffer; + D3D12_PLACED_SUBRESOURCE_FOOTPRINT placed{}; + UINT64 total = 0; + unsigned bytes = 0; // per pixel in this plane +}; + +struct Run { + Ref queue; + Ref allocator; + Ref list; + Ref fence; + + bool create(ID3D12Device* device) { + const D3D12_COMMAND_QUEUE_DESC queue_desc{}; + CHECK(SUCCEEDED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p)))); + CHECK(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p)))); + CHECK(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr, + IID_PPV_ARGS(&list.p)))); + CHECK(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); + return true; + } + bool execute() { + CHECK(SUCCEEDED(list->Close())); + ID3D12CommandList* lists[] = {list.p}; + queue->ExecuteCommandLists(1, lists); + CHECK(SUCCEEDED(queue->Signal(fence.p, 1))); + Event event; + CHECK(event.handle); + CHECK(SUCCEEDED(fence->SetEventOnCompletion(1, event.handle))); + CHECK(WaitForSingleObject(event.handle, 5000) == WAIT_OBJECT_0); + return true; + } +}; + +D3D12_RESOURCE_DESC texture_desc(DXGI_FORMAT format, D3D12_RESOURCE_FLAGS flags) { + D3D12_RESOURCE_DESC texture{}; + texture.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + texture.Width = kWidth; + texture.Height = kHeight; + texture.DepthOrArraySize = texture.MipLevels = 1; + texture.Format = format; + texture.SampleDesc.Count = 1; + texture.Flags = flags; + return texture; +} + +void transition(ID3D12GraphicsCommandList* list, ID3D12Resource* image, D3D12_RESOURCE_STATES before, + D3D12_RESOURCE_STATES after) { + D3D12_RESOURCE_BARRIER barrier{}; + barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + barrier.Transition.pResource = image; + barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + barrier.Transition.StateBefore = before; + barrier.Transition.StateAfter = after; + list->ResourceBarrier(1, &barrier); +} + +bool add_copy(ID3D12Device* device, ID3D12GraphicsCommandList* list, ID3D12Resource* image, + const D3D12_RESOURCE_DESC& texture, UINT subresource, unsigned bytes, Plane& plane) { + plane.bytes = bytes; + device->GetCopyableFootprints(&texture, subresource, 1, 0, &plane.placed, nullptr, nullptr, &plane.total); + D3D12_RESOURCE_DESC buffer{}; + buffer.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + buffer.Width = plane.total; + buffer.Height = buffer.SampleDesc.Count = 1; + buffer.DepthOrArraySize = buffer.MipLevels = 1; + buffer.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + D3D12_HEAP_PROPERTIES heap{}; + heap.Type = D3D12_HEAP_TYPE_READBACK; + CHECK(SUCCEEDED(device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &buffer, + D3D12_RESOURCE_STATE_COPY_DEST, nullptr, + IID_PPV_ARGS(&plane.buffer.p)))); + D3D12_TEXTURE_COPY_LOCATION src{}, dst{}; + src.pResource = image; + src.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; + src.SubresourceIndex = subresource; + dst.pResource = plane.buffer.p; + dst.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; + dst.PlacedFootprint = plane.placed; + list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr); + return true; +} + +bool read_plane(Plane& plane, std::vector& pixels) { + void* mapped = nullptr; + const D3D12_RANGE range{0, SIZE_T(plane.total)}; + CHECK(SUCCEEDED(plane.buffer->Map(0, &range, &mapped)) && mapped); + pixels.resize(kWidth * kHeight * plane.bytes); + for (UINT y = 0; y < kHeight; ++y) + std::memcpy(pixels.data() + y * kWidth * plane.bytes, + static_cast(mapped) + y * plane.placed.Footprint.RowPitch, kWidth * plane.bytes); + const D3D12_RANGE none{}; + plane.buffer->Unmap(0, &none); + return true; +} + +// The two results of a case: false when the device could not run it at all. +enum class Result { ok, not_created, failed }; + +Result clear_color(ID3D12Device* device, DXGI_FORMAT format, unsigned bytes, const Color& full, const Color& part, + std::vector& pixels) { + Run run; + if (!run.create(device)) return Result::failed; + Ref image; + D3D12_HEAP_PROPERTIES heap{}; + heap.Type = D3D12_HEAP_TYPE_DEFAULT; + const D3D12_RESOURCE_DESC texture = texture_desc(format, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET); + if (FAILED(device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &texture, + D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr, IID_PPV_ARGS(&image.p)))) + return Result::not_created; + Ref rtv_heap; + const D3D12_DESCRIPTOR_HEAP_DESC desc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; + if (FAILED(device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&rtv_heap.p)))) return Result::failed; + const auto handle = rtv_heap->GetCPUDescriptorHandleForHeapStart(); + device->CreateRenderTargetView(image.p, nullptr, handle); + run.list->ClearRenderTargetView(handle, full.data(), 0, nullptr); + const D3D12_RECT rect{1, 1, 4, 3}; + run.list->ClearRenderTargetView(handle, part.data(), 1, &rect); + transition(run.list.p, image.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE); + Plane plane; + if (!add_copy(device, run.list.p, image.p, texture, 0, bytes, plane) || !run.execute() || + !read_plane(plane, pixels)) + return Result::failed; + return Result::ok; +} + +struct DepthValues { + float depth_full; + UINT8 stencil_full; + float depth_part; + UINT8 stencil_part; +}; + +// Depth and stencil planes after: a full clear of both, a depth-only clear of one rectangle and a stencil-only +// clear of another. planes[1] is empty for a depth-only format. +Result clear_depth(ID3D12Device* device, DXGI_FORMAT format, bool stencil, unsigned depth_bytes, + const DepthValues& v, std::vector planes[2]) { + Run run; + if (!run.create(device)) return Result::failed; + Ref image; + D3D12_HEAP_PROPERTIES heap{}; + heap.Type = D3D12_HEAP_TYPE_DEFAULT; + const D3D12_RESOURCE_DESC texture = texture_desc(format, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL); + if (FAILED(device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &texture, + D3D12_RESOURCE_STATE_DEPTH_WRITE, nullptr, IID_PPV_ARGS(&image.p)))) + return Result::not_created; + Ref dsv_heap; + const D3D12_DESCRIPTOR_HEAP_DESC desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; + if (FAILED(device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&dsv_heap.p)))) return Result::failed; + const auto handle = dsv_heap->GetCPUDescriptorHandleForHeapStart(); + device->CreateDepthStencilView(image.p, nullptr, handle); + const D3D12_CLEAR_FLAGS both = stencil ? D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL : D3D12_CLEAR_FLAG_DEPTH; + run.list->ClearDepthStencilView(handle, both, v.depth_full, v.stencil_full, 0, nullptr); + const D3D12_RECT depth_rect{1, 1, 4, 3}; + run.list->ClearDepthStencilView(handle, D3D12_CLEAR_FLAG_DEPTH, v.depth_part, 0, 1, &depth_rect); + if (stencil) { + const D3D12_RECT stencil_rect{0, 0, 3, 2}; + run.list->ClearDepthStencilView(handle, D3D12_CLEAR_FLAG_STENCIL, 0.0f, v.stencil_part, 1, &stencil_rect); + } + transition(run.list.p, image.p, D3D12_RESOURCE_STATE_DEPTH_WRITE, D3D12_RESOURCE_STATE_COPY_SOURCE); + Plane depth, stencil_plane; + if (!add_copy(device, run.list.p, image.p, texture, 0, depth_bytes, depth) || + (stencil && !add_copy(device, run.list.p, image.p, texture, 1, 1, stencil_plane)) || !run.execute() || + !read_plane(depth, planes[0]) || (stencil && !read_plane(stencil_plane, planes[1]))) + return Result::failed; + return Result::ok; +} + +struct ColorCase { + const char* group; + const char* name; + DXGI_FORMAT format; + unsigned bytes; + const std::vector* colors; +}; + +// Out-of-range values clamp in the normalised classes; NaN is 0 there. NaN is left out of the float classes, whose +// payload bits are not defined. +const std::vector kUnorm = {{0, 0.25f, 0.5f, 1}, {1, 0.75f, 0.125f, 0.5f}, {-1, 2, 0.5f, 1.5f}, + {0.0039f, 0.5019f, 0.9961f, 0.3333f}, {kNaN, kInf, -kInf, 0.5f}}; +const std::vector kSnorm = {{-1, -0.5f, 0.5f, 1}, {-2, 2, 0.001f, -0.001f}, {0, 0.25f, -0.25f, 0.7f}, + {kNaN, kInf, -kInf, -0.5f}}; +const std::vector kFloat = {{0.1f, 1.5f, -3.25f, 65504.0f}, {1e-8f, 70000.0f, -70000.0f, 0.5f}, + {0, 1, 2, 3}, {kInf, -kInf, 1e-5f, 123456.0f}}; +const std::vector kUint = {{1, 2, 3, 4}, {1.5f, 2.5f, 3.5f, 4.5f}, {-1, -0.0f, 254.5f, 255.5f}, + {256, 65536, 16777216, 4294967296.0f}, {kNaN, kInf, -kInf, 0.5f}, + {0.499f, 0.501f, 1.499f, 1.501f}, {4294967040.0f, 3.4e38f, 255, 0}}; +const std::vector kSint = {{-1, -2, -128, 127}, {-1.5f, -2.5f, 2.5f, 3.5f}, {-129, 128, -32769, 32768}, + {2147483648.0f, -2147483904.0f, kNaN, kInf}, {-kInf, 0.5f, -0.5f, 100000.0f}}; + +const ColorCase kColorCases[] = { + {"UNORM", "R8_UNORM", DXGI_FORMAT_R8_UNORM, 1, &kUnorm}, + {"UNORM", "R8G8_UNORM", DXGI_FORMAT_R8G8_UNORM, 2, &kUnorm}, + {"UNORM", "R8G8B8A8_UNORM", DXGI_FORMAT_R8G8B8A8_UNORM, 4, &kUnorm}, + {"UNORM", "B8G8R8A8_UNORM", DXGI_FORMAT_B8G8R8A8_UNORM, 4, &kUnorm}, + {"UNORM", "R10G10B10A2_UNORM", DXGI_FORMAT_R10G10B10A2_UNORM, 4, &kUnorm}, + {"UNORM", "R16_UNORM", DXGI_FORMAT_R16_UNORM, 2, &kUnorm}, + {"UNORM", "R16G16_UNORM", DXGI_FORMAT_R16G16_UNORM, 4, &kUnorm}, + {"UNORM", "R16G16B16A16_UNORM", DXGI_FORMAT_R16G16B16A16_UNORM, 8, &kUnorm}, + {"SNORM", "R8_SNORM", DXGI_FORMAT_R8_SNORM, 1, &kSnorm}, + {"SNORM", "R8G8_SNORM", DXGI_FORMAT_R8G8_SNORM, 2, &kSnorm}, + {"SNORM", "R8G8B8A8_SNORM", DXGI_FORMAT_R8G8B8A8_SNORM, 4, &kSnorm}, + {"SNORM", "R16_SNORM", DXGI_FORMAT_R16_SNORM, 2, &kSnorm}, + {"SNORM", "R16G16_SNORM", DXGI_FORMAT_R16G16_SNORM, 4, &kSnorm}, + {"SNORM", "R16G16B16A16_SNORM", DXGI_FORMAT_R16G16B16A16_SNORM, 8, &kSnorm}, + {"SRGB", "R8G8B8A8_UNORM_SRGB", DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, 4, &kUnorm}, + {"SRGB", "B8G8R8A8_UNORM_SRGB", DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, 4, &kUnorm}, + {"FLOAT", "R16_FLOAT", DXGI_FORMAT_R16_FLOAT, 2, &kFloat}, + {"FLOAT", "R16G16_FLOAT", DXGI_FORMAT_R16G16_FLOAT, 4, &kFloat}, + {"FLOAT", "R16G16B16A16_FLOAT", DXGI_FORMAT_R16G16B16A16_FLOAT, 8, &kFloat}, + {"FLOAT", "R32_FLOAT", DXGI_FORMAT_R32_FLOAT, 4, &kFloat}, + {"FLOAT", "R32G32_FLOAT", DXGI_FORMAT_R32G32_FLOAT, 8, &kFloat}, + {"FLOAT", "R32G32B32A32_FLOAT", DXGI_FORMAT_R32G32B32A32_FLOAT, 16, &kFloat}, + {"FLOAT", "R11G11B10_FLOAT", DXGI_FORMAT_R11G11B10_FLOAT, 4, &kFloat}, + {"UINT", "R8_UINT", DXGI_FORMAT_R8_UINT, 1, &kUint}, + {"UINT", "R16_UINT", DXGI_FORMAT_R16_UINT, 2, &kUint}, + {"UINT", "R8G8B8A8_UINT", DXGI_FORMAT_R8G8B8A8_UINT, 4, &kUint}, + {"UINT", "R32_UINT", DXGI_FORMAT_R32_UINT, 4, &kUint}, + {"UINT", "R32G32B32A32_UINT", DXGI_FORMAT_R32G32B32A32_UINT, 16, &kUint}, + {"SINT", "R8_SINT", DXGI_FORMAT_R8_SINT, 1, &kSint}, + {"SINT", "R16_SINT", DXGI_FORMAT_R16_SINT, 2, &kSint}, + {"SINT", "R8G8B8A8_SINT", DXGI_FORMAT_R8G8B8A8_SINT, 4, &kSint}, + {"SINT", "R32_SINT", DXGI_FORMAT_R32_SINT, 4, &kSint}, + {"SINT", "R32G32B32A32_SINT", DXGI_FORMAT_R32G32B32A32_SINT, 16, &kSint}, +}; + +struct DepthCase { + const char* name; + DXGI_FORMAT format; + bool stencil; + unsigned depth_bytes; +}; + +const DepthCase kDepthCases[] = { + {"D16_UNORM", DXGI_FORMAT_D16_UNORM, false, 2}, + {"D32_FLOAT", DXGI_FORMAT_D32_FLOAT, false, 4}, + {"D24_UNORM_S8_UINT", DXGI_FORMAT_D24_UNORM_S8_UINT, true, 4}, + {"D32_FLOAT_S8X24_UINT", DXGI_FORMAT_D32_FLOAT_S8X24_UINT, true, 4}, +}; + +const DepthValues kDepthValues[] = {{0.25f, 0x5A, 0.75f, 0xA5}, {1.0f, 0xFF, 0.0f, 0x00}, + {0.3333f, 0x01, 0.9999f, 0x80}, {0.5f, 0x7F, 0.0001f, 0x10}}; + +struct Tally { + unsigned matched = 0, differ = 0, not_exposed = 0, failed = 0, skipped = 0; +}; + +// Names the first byte that differs. +void report(const char* group, const char* name, unsigned index, const char* what, + const std::vector& actual, const std::vector& expected) { + for (std::size_t b = 0; b < actual.size() && b < expected.size(); ++b) { + if (actual[b] != expected[b]) { + std::printf("DIFF %-6s %-22s case %u %s byte %zu: layer %02x, WARP %02x\n", group, name, index, what, b, + unsigned(actual[b]), unsigned(expected[b])); + return; + } + } + std::printf("DIFF %-6s %-22s case %u %s: sizes %zu and %zu\n", group, name, index, what, actual.size(), + expected.size()); +} +} // namespace + +int main() { + vktest::Device vk; + const auto device_status = vk.initialize(); + if (device_status == vktest::DeviceStatus::unavailable) return 77; // no Vulkan GPU: ctest skips + if (device_status != vktest::DeviceStatus::ready) return 1; + const unsigned errors_before = vk.validation_errors(); + Tally tally; + { + Ref ours, host; + rt::d3d12::VulkanDeviceInfo info{}; + info.get_instance_proc = vk.instance_proc; + info.instance = vk.instance; + info.physical = vk.physical; + info.device = vk.device; + info.queue = vk.queue; + info.queue_family = vk.family; + if (FAILED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&ours.p)))) return 1; + Ref factory; + Ref warp; + if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p))) || + FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&warp.p))) || + FAILED(D3D12CreateDevice(warp.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p)))) { + std::fprintf(stderr, "WARP reference unavailable: differential test cannot run\n"); + return 1; + } + for (const ColorCase& c : kColorCases) { + const auto& colors = *c.colors; + bool reported_exposure = false; + for (unsigned i = 0; i < colors.size(); ++i) { + // Different colours outside and inside the rectangle exercise both Vulkan clear paths. + const Color& full = colors[i]; + const Color& part = colors[(i + 1) % colors.size()]; + std::vector actual, expected; + const Result want = clear_color(host.p, c.format, c.bytes, full, part, expected); + if (want != Result::ok) { + ++tally.skipped; // WARP itself does not offer the format as a render target here + continue; + } + const Result got = clear_color(ours.p, c.format, c.bytes, full, part, actual); + if (got == Result::not_created) { + ++tally.not_exposed; + if (!reported_exposure) std::printf("not exposed: %-6s %s\n", c.group, c.name); + reported_exposure = true; + continue; + } + if (got != Result::ok) { + ++tally.failed; + std::printf("FAIL %-6s %-22s case %u: the layer could not run it\n", c.group, c.name, i); + } else if (actual != expected) { + ++tally.differ; + report(c.group, c.name, i, "colour", actual, expected); + } else { + ++tally.matched; + } + } + } + for (const DepthCase& c : kDepthCases) { + bool reported_exposure = false; + for (unsigned i = 0; i < std::size(kDepthValues); ++i) { + std::vector actual[2], expected[2]; + const Result want = clear_depth(host.p, c.format, c.stencil, c.depth_bytes, kDepthValues[i], expected); + if (want != Result::ok) { + ++tally.skipped; + continue; + } + const Result got = clear_depth(ours.p, c.format, c.stencil, c.depth_bytes, kDepthValues[i], actual); + if (got == Result::not_created) { + ++tally.not_exposed; + if (!reported_exposure) std::printf("not exposed: DEPTH %s\n", c.name); + reported_exposure = true; + continue; + } + if (got != Result::ok) { + ++tally.failed; + std::printf("FAIL DEPTH %-22s case %u: the layer could not run it\n", c.name, i); + continue; + } + bool same = true; + for (int plane = 0; plane < (c.stencil ? 2 : 1); ++plane) { + if (actual[plane] != expected[plane]) { + same = false; + report("DEPTH", c.name, i, plane ? "stencil" : "depth", actual[plane], expected[plane]); + } + } + if (same) ++tally.matched; + else ++tally.differ; + } + } + } // Destroy the D3D12 objects before checking the Vulkan validation counter. + std::printf("clears: %u matched WARP, %u differ, %u failed, %u in formats the layer does not expose, " + "%u skipped (WARP does not offer the format)\n", + tally.matched, tally.differ, tally.failed, tally.not_exposed, tally.skipped); + if (tally.differ || tally.failed) return 1; + if (vk.validation_errors() != errors_before) return 1; + return 0; +} diff --git a/runtime/test/d3d12_vulkan_device_test.cpp b/runtime/test/d3d12_vulkan_device_test.cpp index 788b5c77223..b58e972850e 100644 --- a/runtime/test/d3d12_vulkan_device_test.cpp +++ b/runtime/test/d3d12_vulkan_device_test.cpp @@ -1702,6 +1702,13 @@ bool within_06_ulp(std::uint8_t a, std::uint8_t b, double ideal) { return std::abs(double(a) - ideal) <= 0.6 && std::abs(double(b) - ideal) <= 0.6; } +// The code nearest to `ideal`, a tie going to the even one: what the layer writes for a normalised clear (#521's +// quantize_unorm), whichever way the GPU's own driver rounds the tie. +double nearest_even_code(double ideal) { + const double r = std::floor(ideal + 0.5); + return r - ideal == 0.5 && std::fmod(r, 2.0) != 0.0 ? r - 1.0 : r; +} + bool create_host_device_for_physical(VkInstance instance, VkPhysicalDevice physical, PFN_vkGetInstanceProcAddr instance_proc, Ref& host, std::string& adapter_name) { @@ -7483,6 +7490,9 @@ int main() { // WARP and NVIDIA's Vulkan driver give 188, Intel's Vulkan driver 187 (UHD 770 101.7092), both within // 0.6 ULP. On the layer's own adapter (same LUID) the comparison stays exact; against another adapter // or WARP a byte may differ when both sides are within 0.6 ULP of the ideal value. + // The plain clear's normalised values are rounded by the layer itself (nearest even, WARP's and AMD's + // code), so on its own NVIDIA adapter the tie byte may be the step above the driver's: accepted only + // when it is exactly that nearest-even code and within 0.6 ULP. const FLOAT clear_value[4] = {0.25f, 0.5f, 0.75f, 1.0f}; // placed_cast_scenario's clear colour const auto within_tolerance = [&](std::uint8_t ours_byte, std::uint8_t host_byte, double ideal) { return !same_adapter && within_06_ulp(ours_byte, host_byte, ideal); @@ -7495,7 +7505,9 @@ int main() { const double plain_ideal = c * 255.0; const double srgb_ideal = (i % 4 == 3 ? c : ideal_srgb(c)) * 255.0; // alpha stays linear if (ours[b] != host[b]) { - if (within_tolerance(ours[b], host[b], plain_ideal)) + const bool layer_tie = double(ours[b]) == nearest_even_code(plain_ideal) && + within_06_ulp(ours[b], host[b], plain_ideal); + if (within_tolerance(ours[b], host[b], plain_ideal) || layer_tie) ++plain_within; else if (differences++ < 8) std::fprintf(stderr, "placed plain clear row %u byte %u: ours %02x, host %02x\n", row, i, diff --git a/runtime/test/typeless_rtv_test.cpp b/runtime/test/typeless_rtv_test.cpp index a653770da86..83e7d6d9338 100644 --- a/runtime/test/typeless_rtv_test.cpp +++ b/runtime/test/typeless_rtv_test.cpp @@ -102,7 +102,9 @@ int main() { } else if (format == ColorViewFormat::rgba8_sint) { EXPECT(captured.int32[0] == 1 && captured.int32[1] == 2 && captured.int32[2] == 127 && captured.int32[3] == -1); } else { - EXPECT(std::memcmp(captured.float32, color.data(), sizeof(captured)) == 0); + // UNORM and sRGB views round and clamp like D3D (render_target_clear_color): 1.5 and 256 are 1.0, -1 is 0. + const VkClearColorValue converted = leonida::detail::render_target_clear_color(expected, color); + EXPECT(std::memcmp(captured.float32, converted.float32, sizeof(captured)) == 0); } ++checks; }