diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 16f029335c..c9cc8fed20 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -1810,6 +1810,10 @@ inline VkClearColorValue render_target_clear_color(VkFormat format, const ClearC unorm = {0, 0, 0, 255}; break; case VK_FORMAT_A2B10G10R10_UNORM_PACK32: is_integer = false; unorm = {1023, 1023, 1023, 3}; break; + case VK_FORMAT_A1R5G5B5_UNORM_PACK16: is_integer = false; unorm = {31, 31, 31, 1}; break; + case VK_FORMAT_A4R4G4B4_UNORM_PACK16: + case VK_FORMAT_R4G4B4A4_UNORM_PACK16: + case VK_FORMAT_A4B4G4R4_UNORM_PACK16: is_integer = false; unorm = {15, 15, 15, 15}; 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; @@ -6361,11 +6365,12 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_ if constexpr (Checkpoints) checkpoints->record(command_buffer, i, CheckpointKind::clear); const VulkanAccess state = vulkan_access(d3d12_state::render_target, false); auto pieces = detail::current(tracker, id, p.range, image_layout(p.binding, state.layout)); - detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker, active_samples); - auto clear_value=color_clear->color; - if(p.binding.rgba4_render_fallback)for(auto& channel:clear_value) - channel=std::nearbyint(std::clamp(channel,0.0f,1.0f)*15.0f)/15.0f; - const VkClearColorValue color=detail::render_target_clear_color(p.clear_format,clear_value); + detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker, + active_samples); + auto clear_value = color_clear->color; + if (p.binding.rgba4_render_fallback) + for (auto& channel : clear_value) channel = detail::quantize_unorm(channel, 15.0); + const VkClearColorValue color = detail::render_target_clear_color(p.clear_format, clear_value); if (p.rects.empty()) { detail::round_trip(vk, command_buffer, p.binding, pieces, image_layout(p.binding, transfer_dst.layout), state, transfer_dst.access, transfer_dst.stages, [&] { diff --git a/runtime/test/clear_color_test.cpp b/runtime/test/clear_color_test.cpp index f82a28032f..99838c7136 100644 --- a/runtime/test/clear_color_test.cpp +++ b/runtime/test/clear_color_test.cpp @@ -24,7 +24,7 @@ VKAPI_ATTR void VKAPI_CALL end(VkCommandBuffer) {} VKAPI_ATTR void VKAPI_CALL attachments(VkCommandBuffer, std::uint32_t n, const VkClearAttachment* a, std::uint32_t, const VkClearRect*) { if (n == 1) { captured = a[0].clearValue.color; ++calls; } } -bool replay_color(VkFormat format, const leonida::ClearColor& color, bool rectangle) { +bool replay_color(VkFormat format, const leonida::ClearColor& color, bool rectangle, bool rgba4_fallback = false) { leonida::CommandList list; const leonida::Rect rect{1, 1, 3, 3}; if (!list.clear_render_target({1, 0, 0, 1}, color, @@ -33,6 +33,7 @@ bool replay_color(VkFormat format, const leonida::ClearColor& color, bool rectan binding.image = reinterpret_cast(std::uintptr_t{1}); binding.format = format; binding.width = binding.height = 4; + binding.rgba4_render_fallback = rgba4_fallback; leonida::ReplayFunctions vk{}; vk.pipeline_barrier = barrier; vk.clear_color_image = clear; @@ -154,6 +155,11 @@ int main() { {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_A1R5G5B5_UNORM_PACK16, {0.5f, 0.25f, -1.0f, 0.5f}, {16, 8, 0, 0}, {31, 31, 31, 1}}, + {VK_FORMAT_A1R5G5B5_UNORM_PACK16, {nan, 2.0f, 0.75f, 0.51f}, {0, 31, 23, 1}, {31, 31, 31, 1}}, + {VK_FORMAT_A4R4G4B4_UNORM_PACK16, {0.5f, -1.0f, 2.0f, nan}, {8, 0, 15, 0}, {15, 15, 15, 15}}, + {VK_FORMAT_R4G4B4A4_UNORM_PACK16, {0.5f, 0.25f, 0.75f, 1.5f}, {8, 4, 11, 15}, {15, 15, 15, 15}}, + {VK_FORMAT_A4B4G4R4_UNORM_PACK16, {0.5f, 0.125f, -0.5f, nan}, {8, 2, 0, 0}, {15, 15, 15, 15}}, {VK_FORMAT_R16G16B16A16_UNORM, {0.5f, 0.25f, 0.75f, 1.0f}, {32768, 16384, 49151, 65535}, {65535, 65535, 65535, 65535}}, {VK_FORMAT_R16_UNORM, {0.5f, 0, 0, 0}, {32768, 0, 0, 0}, {65535, 65535, 65535, 65535}}, @@ -278,14 +284,26 @@ int main() { // 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]{}; + VkClearColorValue unorm[3]{}, snorm[3]{}, fallback[3][2]{}, fallback_undef[3]{}; + bool fallback_ok = true; const int modes[3] = {FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO}; + const leonida::ClearColor fallback_input{0.5f, -1.0f, 2.0f, nan}; 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}); + for (int r = 0; r < 2; ++r) { + if (!replay_color(VK_FORMAT_R8G8B8A8_UNORM, fallback_input, r != 0, true)) fallback_ok = false; + fallback[m][r] = captured; + } + if (!replay_color(VK_FORMAT_UNDEFINED, fallback_input, false, true)) fallback_ok = false; + fallback_undef[m] = captured; } std::fesetround(mode); + if (!fallback_ok) { + std::fprintf(stderr, "rgba4_render_fallback replay failed\n"); + return 1; + } 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); @@ -295,6 +313,24 @@ int main() { modes[m], u, s0, s1); return 1; } + for (int r = 0; r < 2; ++r) { + const long f0 = std::lround(double(fallback[m][r].float32[0]) * 255.0); + if (f0 != 136 || fallback[m][r].float32[1] != 0.0f || fallback[m][r].float32[2] != 1.0f || + fallback[m][r].float32[3] != 0.0f) { + std::fprintf(stderr, "rgba4_render_fallback under FP mode %d rect %d: got %.9g %.9g %.9g %.9g\n", + modes[m], r, double(fallback[m][r].float32[0]), double(fallback[m][r].float32[1]), + double(fallback[m][r].float32[2]), double(fallback[m][r].float32[3])); + return 1; + } + } + const long fu0 = std::lround(double(fallback_undef[m].float32[0]) * 15.0); + if (fu0 != 8 || fallback_undef[m].float32[1] != 0.0f || fallback_undef[m].float32[2] != 1.0f || + fallback_undef[m].float32[3] != 0.0f) { + std::fprintf(stderr, "rgba4_render_fallback (undef format) under FP mode %d: got %.9g %.9g %.9g %.9g\n", + modes[m], double(fallback_undef[m].float32[0]), double(fallback_undef[m].float32[1]), + double(fallback_undef[m].float32[2]), double(fallback_undef[m].float32[3])); + return 1; + } ++checks; } // The sRGB encoding too: every 12-bit value and the floats around each step between two of them give the diff --git a/runtime/test/d3d12_b4g4r4a4_target_test.cpp b/runtime/test/d3d12_b4g4r4a4_target_test.cpp index 6fce73d9e9..e3ed62ccd2 100644 --- a/runtime/test/d3d12_b4g4r4a4_target_test.cpp +++ b/runtime/test/d3d12_b4g4r4a4_target_test.cpp @@ -15,9 +15,11 @@ #include "../shims/d3d12_com.h" #include "../src/gfx10_descriptors.h" +#include #include #include #include +#include #include #define EXPECT(condition) do { \ @@ -168,9 +170,10 @@ int main() { Ref fence; EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); - // Values a 4-bit channel holds exactly (n / 15), so the RGBA8 bytes are n * 17. - const FLOAT outside_color[4] = {1.0f, 0.0f, 5.0f / 15, 1.0f}; // -> FF 00 55 FF - const FLOAT inside_color[4] = {0.0f, 10.0f / 15, 0.0f, 0.0f}; // -> 00 AA 00 00 + // Values a 4-bit channel holds exactly (n / 15), plus a 4-bit tie (0.5 -> 8/15 = 0x88), out-of-range and NaN + // executed under FE_DOWNWARD so FP-mode-dependent rounding (std::nearbyint -> 7/15 = 0x77) fails. + const FLOAT outside_color[4] = {1.0f, 10.0f / 15.0f, 5.0f / 15.0f, 1.0f}; + const FLOAT inside_color[4] = {0.5f, -1.0f, 2.0f, std::numeric_limits::quiet_NaN()}; // -> 88 00 FF 00 const D3D12_RECT rect{8, 4, 24, 12}; list->ClearRenderTargetView(rtv, outside_color, 0, nullptr); list->ClearRenderTargetView(rtv, inside_color, 1, &rect); @@ -183,7 +186,10 @@ int main() { list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr); EXPECT(SUCCEEDED(list->Close())); ID3D12CommandList* lists[] = {list.p}; + const int saved_round = std::fegetround(); + std::fesetround(FE_DOWNWARD); queue->ExecuteCommandLists(1, lists); + std::fesetround(saved_round); EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success); EXPECT(SUCCEEDED(queue->Signal(fence.p, 1)) && SUCCEEDED(fence->SetEventOnCompletion(1, nullptr))); @@ -196,8 +202,8 @@ int main() { std::memcpy(texel, static_cast(mapped) + y * kPitch + x * 4, sizeof(texel)); const bool inside = x >= UINT(rect.left) && x < UINT(rect.right) && y >= UINT(rect.top) && y < UINT(rect.bottom); - const std::uint8_t want[4] = {std::uint8_t(inside ? 0x00 : 0xFF), std::uint8_t(inside ? 0xAA : 0x00), - std::uint8_t(inside ? 0x00 : 0x55), std::uint8_t(inside ? 0x00 : 0xFF)}; + const std::uint8_t want[4] = {std::uint8_t(inside ? 0x88 : 0xFF), std::uint8_t(inside ? 0x00 : 0xAA), + std::uint8_t(inside ? 0xFF : 0x55), std::uint8_t(inside ? 0x00 : 0xFF)}; if (std::memcmp(texel, want, 4) != 0 && wrong++ < 4) std::fprintf(stderr, "texel (%u, %u) = %02x %02x %02x %02x, want %02x %02x %02x %02x\n", x, y, texel[0], texel[1], texel[2], texel[3], want[0], want[1], want[2], want[3]); diff --git a/runtime/test/d3d12_clear_formats_test.cpp b/runtime/test/d3d12_clear_formats_test.cpp index f270f62af7..c2fef829b2 100644 --- a/runtime/test/d3d12_clear_formats_test.cpp +++ b/runtime/test/d3d12_clear_formats_test.cpp @@ -265,6 +265,8 @@ const ColorCase kColorCases[] = { {"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", "B5G5R5A1_UNORM", DXGI_FORMAT_B5G5R5A1_UNORM, 2, &kUnorm}, + {"UNORM", "A4B4G4R4_UNORM", DXGI_FORMAT_A4B4G4R4_UNORM, 2, &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},