diff options
| author | Aslan2142 <jan.trencansky@protonmail.com> | 2026-11-26 19:23:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-26 19:23:25 +0100 |
| commit | aa663c4c5d14cd6d23da5dd5e598f874839a8577 (patch) | |
| tree | 81ac99a1a6d61841cca3562fab529cf38348c73f /runtime/src | |
| parent | bdbc7e9236465c5d6a1305de6edd3fc09c2d6fd9 (diff) | |
| parent | bbf861fbb4ea51ca18cd6f372797111984b6f8aa (diff) | |
Merge pull request #1764 from IlyassRS3/fix/depth16-quantise-fallback
runtime: fix D16S8 fragment depth quantisation and pc_format fallback gating
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/depth24_fragment.h | 56 | ||||
| -rw-r--r-- | runtime/src/vulkan_replay.h | 12 |
2 files changed, 55 insertions, 13 deletions
diff --git a/runtime/src/depth24_fragment.h b/runtime/src/depth24_fragment.h index d32f975b2..4c212ee5c 100644 --- a/runtime/src/depth24_fragment.h +++ b/runtime/src/depth24_fragment.h | |||
| @@ -55,7 +55,8 @@ enum : std::uint32_t { | |||
| 55 | op_function = 54, op_function_end = 56, op_variable = 59, op_load = 61, op_store = 62, op_decorate = 71, | 55 | op_function = 54, op_function_end = 56, op_variable = 59, op_load = 61, op_store = 62, op_decorate = 71, |
| 56 | op_composite_extract = 81, op_convert_f_to_u = 109, op_convert_u_to_f = 112, op_bitcast = 124, op_i_add = 128, | 56 | op_composite_extract = 81, op_convert_f_to_u = 109, op_convert_u_to_f = 112, op_bitcast = 124, op_i_add = 128, |
| 57 | op_f_add = 129, op_i_sub = 130, op_f_mul = 133, op_select = 169, op_i_equal = 170, op_u_greater_than = 172, | 57 | op_f_add = 129, op_i_sub = 130, op_f_mul = 133, op_select = 169, op_i_equal = 170, op_u_greater_than = 172, |
| 58 | op_u_greater_than_equal = 174, op_shift_left_logical = 196, op_dpdx = 207, op_dpdy = 208, op_label = 248, | 58 | op_u_greater_than_equal = 174, op_shift_right_logical = 194, op_shift_left_logical = 196, op_dpdx = 207, |
| 59 | op_dpdy = 208, op_label = 248, | ||
| 59 | op_return = 253, op_no_line = 317, op_execution_mode_id = 331, | 60 | op_return = 253, op_no_line = 317, op_execution_mode_id = 331, |
| 60 | }; | 61 | }; |
| 61 | enum : std::uint32_t { storage_input = 1, storage_output = 3 }; | 62 | enum : std::uint32_t { storage_input = 1, storage_output = 3 }; |
| @@ -182,12 +183,18 @@ public: | |||
| 182 | constants_.push_back({t, value, id}); | 183 | constants_.push_back({t, value, id}); |
| 183 | return id; | 184 | return id; |
| 184 | } | 185 | } |
| 186 | bool is_depth16() const { return levels_ == 65535.0f; } | ||
| 187 | |||
| 185 | // Declares what quantise() uses, so it can run inside apply() (at returns) without adding globals. | 188 | // Declares what quantise() uses, so it can run inside apply() (at returns) without adding globals. |
| 186 | void prime_quantise() { | 189 | void prime_quantise() { |
| 187 | t_bool(); | 190 | t_bool(); |
| 188 | glsl(); | 191 | glsl(); |
| 189 | for (float f : {levels_, 0.0f}) f32(f); | 192 | for (float f : {levels_, 0.0f}) f32(f); |
| 190 | for (std::uint32_t u : {0x3f800000u, 0xffffffu, 0x7f800000u, 0u, 23u, 1u, 80u}) u32(u); | 193 | if (is_depth16()) { |
| 194 | for (std::uint32_t u : {0x3f800000u, 0xffffu, 0x7f800000u, 0u, 15u, 8u, 128u, 88u, 23u}) u32(u); | ||
| 195 | } else { | ||
| 196 | for (std::uint32_t u : {0x3f800000u, 0xffffffu, 0x7f800000u, 0u, 23u, 1u, 80u}) u32(u); | ||
| 197 | } | ||
| 191 | } | 198 | } |
| 192 | std::uint32_t f32(float f) { return constant(t_float(), bits(f)); } | 199 | std::uint32_t f32(float f) { return constant(t_float(), bits(f)); } |
| 193 | std::uint32_t u32(std::uint32_t u) { return constant(t_uint(), u); } | 200 | std::uint32_t u32(std::uint32_t u) { return constant(t_uint(), u); } |
| @@ -245,9 +252,11 @@ public: | |||
| 245 | return id; | 252 | return id; |
| 246 | } | 253 | } |
| 247 | 254 | ||
| 248 | // depth24_to_float(depth24_from_bits(z)) of depth24_convert.comp, branch-free. | 255 | // Quantise z to D24 (levels_ == 16777215.0f) or D16 (levels_ == 65535.0f) and reconstruct the nearest float |
| 256 | // to d / levels_ without floating-point division (SPIR-V OpFDiv only guarantees 2.5 ULP), branch-free. | ||
| 249 | std::uint32_t quantise(std::vector<Inst>& code, std::uint32_t z) { | 257 | std::uint32_t quantise(std::vector<Inst>& code, std::uint32_t z) { |
| 250 | const std::uint32_t tf = t_float(), tu = t_uint(), tb = t_bool(); | 258 | const std::uint32_t tf = t_float(), tu = t_uint(), tb = t_bool(); |
| 259 | const std::uint32_t max_code = is_depth16() ? 0xffffu : 0xffffffu; | ||
| 251 | const std::uint32_t b = emit(code, op_bitcast, tu, {z}); | 260 | const std::uint32_t b = emit(code, op_bitcast, tu, {z}); |
| 252 | const std::uint32_t product = emit(code, op_f_mul, tf, {z, f32(levels_)}); | 261 | const std::uint32_t product = emit(code, op_f_mul, tf, {z, f32(levels_)}); |
| 253 | const std::uint32_t rounded = ext(code, tf, glsl_round_even, {product}); | 262 | const std::uint32_t rounded = ext(code, tf, glsl_round_even, {product}); |
| @@ -256,17 +265,36 @@ public: | |||
| 256 | f32(levels_)}); | 265 | f32(levels_)}); |
| 257 | const std::uint32_t code_mid = emit(code, op_convert_f_to_u, tu, {safe}); | 266 | const std::uint32_t code_mid = emit(code, op_convert_f_to_u, tu, {safe}); |
| 258 | const std::uint32_t at_least_one = emit(code, op_u_greater_than_equal, tb, {b, u32(0x3f800000u)}); | 267 | const std::uint32_t at_least_one = emit(code, op_u_greater_than_equal, tb, {b, u32(0x3f800000u)}); |
| 259 | const std::uint32_t code_hi = emit(code, op_select, tu, {at_least_one, u32(0xffffffu), code_mid}); | 268 | const std::uint32_t code_hi = emit(code, op_select, tu, {at_least_one, u32(max_code), code_mid}); |
| 260 | const std::uint32_t negative_or_nan = emit(code, op_u_greater_than, tb, {b, u32(0x7f800000u)}); | 269 | const std::uint32_t negative_or_nan = emit(code, op_u_greater_than, tb, {b, u32(0x7f800000u)}); |
| 261 | const std::uint32_t d = emit(code, op_select, tu, {negative_or_nan, u32(0), code_hi}); | 270 | const std::uint32_t d = emit(code, op_select, tu, {negative_or_nan, u32(0), code_hi}); |
| 262 | // float(m + 1) * 2^(e - 47) with e = findMSB(d) and m = d << (23 - e); d == 0 gives 0. | ||
| 263 | const std::uint32_t e = ext(code, tu, glsl_find_umsb, {d}); | 271 | const std::uint32_t e = ext(code, tu, glsl_find_umsb, {d}); |
| 264 | const std::uint32_t m = emit(code, op_shift_left_logical, tu, {d, emit(code, op_i_sub, tu, {u32(23), e})}); | 272 | std::uint32_t value = 0; |
| 265 | const std::uint32_t mf = emit(code, op_convert_u_to_f, tf, {emit(code, op_i_add, tu, {m, u32(1)})}); | 273 | if (is_depth16()) { |
| 266 | const std::uint32_t exponent = emit(code, op_shift_left_logical, tu, {emit(code, op_i_add, tu, {e, u32(80)}), | 274 | // Nearest float to d / (2^16 - 1) for d in 1..65535: |
| 267 | u32(23)}); | 275 | // n = d << (15 - e) puts d's MSB at bit 15; repeating n in binary gives (n << 8) | (n >> 8) as the |
| 268 | const std::uint32_t scale = emit(code, op_bitcast, tf, {exponent}); | 276 | // top 24 bits, and the tail below bit 8 is strictly > 0.5 iff bit 7 of n is set (never a tie), so |
| 269 | const std::uint32_t value = emit(code, op_f_mul, tf, {mf, scale}); | 277 | // m = (n << 8) + ((n + 128) >> 8) is the exact rounded 24-bit mantissa (0x1000000 for d == 65535). |
| 278 | // Multiplying float(m) by 2^(e - 39) (biased exponent e + 88) matches float(double(d) / 65535.0) | ||
| 279 | // for every 16-bit code, and for d == 0 (e == 0xffffffff) shifts stay < 32 and zero selects 0.0f. | ||
| 280 | const std::uint32_t n = emit(code, op_shift_left_logical, tu, {d, emit(code, op_i_sub, tu, {u32(15), e})}); | ||
| 281 | const std::uint32_t n_hi = emit(code, op_shift_left_logical, tu, {n, u32(8)}); | ||
| 282 | const std::uint32_t n_lo = | ||
| 283 | emit(code, op_shift_right_logical, tu, {emit(code, op_i_add, tu, {n, u32(128)}), u32(8)}); | ||
| 284 | const std::uint32_t mf = emit(code, op_convert_u_to_f, tf, {emit(code, op_i_add, tu, {n_hi, n_lo})}); | ||
| 285 | const std::uint32_t exponent = | ||
| 286 | emit(code, op_shift_left_logical, tu, {emit(code, op_i_add, tu, {e, u32(88)}), u32(23)}); | ||
| 287 | const std::uint32_t scale = emit(code, op_bitcast, tf, {exponent}); | ||
| 288 | value = emit(code, op_f_mul, tf, {mf, scale}); | ||
| 289 | } else { | ||
| 290 | // float(m + 1) * 2^(e - 47) with e = findMSB(d) and m = d << (23 - e); d == 0 gives 0. | ||
| 291 | const std::uint32_t m = emit(code, op_shift_left_logical, tu, {d, emit(code, op_i_sub, tu, {u32(23), e})}); | ||
| 292 | const std::uint32_t mf = emit(code, op_convert_u_to_f, tf, {emit(code, op_i_add, tu, {m, u32(1)})}); | ||
| 293 | const std::uint32_t exponent = emit(code, op_shift_left_logical, tu, {emit(code, op_i_add, tu, {e, u32(80)}), | ||
| 294 | u32(23)}); | ||
| 295 | const std::uint32_t scale = emit(code, op_bitcast, tf, {exponent}); | ||
| 296 | value = emit(code, op_f_mul, tf, {mf, scale}); | ||
| 297 | } | ||
| 270 | const std::uint32_t zero = emit(code, op_i_equal, tb, {d, u32(0)}); | 298 | const std::uint32_t zero = emit(code, op_i_equal, tb, {d, u32(0)}); |
| 271 | return emit(code, op_select, tf, {zero, f32(0.0f), value}); | 299 | return emit(code, op_select, tf, {zero, f32(0.0f), value}); |
| 272 | } | 300 | } |
| @@ -328,9 +356,9 @@ public: | |||
| 328 | } | 356 | } |
| 329 | 357 | ||
| 330 | private: | 358 | private: |
| 331 | float levels_; | ||
| 332 | Module& m_; | 359 | Module& m_; |
| 333 | std::uint32_t entry_; | 360 | std::uint32_t entry_; |
| 361 | float levels_; | ||
| 334 | std::vector<Inst> existing_; | 362 | std::vector<Inst> existing_; |
| 335 | std::vector<Inst> globals_, decorations_, modes_; | 363 | std::vector<Inst> globals_, decorations_, modes_; |
| 336 | Inst import_; | 364 | Inst import_; |
| @@ -346,6 +374,10 @@ private: | |||
| 346 | inline std::vector<std::uint32_t> depth24_patch_fragment(const std::vector<std::uint32_t>& words, | 374 | inline std::vector<std::uint32_t> depth24_patch_fragment(const std::vector<std::uint32_t>& words, |
| 347 | const Depth24Bias& bias, std::string& why) { | 375 | const Depth24Bias& bias, std::string& why) { |
| 348 | using namespace spirv24; | 376 | using namespace spirv24; |
| 377 | if (bias.levels != 16777215.0f && bias.levels != 65535.0f) { | ||
| 378 | why = "unsupported depth quantisation level count"; | ||
| 379 | return {}; | ||
| 380 | } | ||
| 349 | Module m; | 381 | Module m; |
| 350 | if (!m.parse(words, why)) return {}; | 382 | if (!m.parse(words, why)) return {}; |
| 351 | std::uint32_t entry = 0; | 383 | std::uint32_t entry = 0; |
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 8889560e9..548fb4c17 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h | |||
| @@ -1559,6 +1559,15 @@ inline float quantize_unorm(float value, double maximum) { | |||
| 1559 | return static_cast<float>(round_half_even(double(value) * maximum) / maximum); | 1559 | return static_cast<float>(round_half_even(double(value) * maximum) / maximum); |
| 1560 | } | 1560 | } |
| 1561 | 1561 | ||
| 1562 | // 16-bit UNORM depth clear quantisation matching depth24_fragment.h's Patch::quantise(levels = 65535.0f): | ||
| 1563 | // float multiply by 65535.0f, round-half-even, and nearest float to d / 65535, with NaN and negatives -> 0.0f. | ||
| 1564 | inline float quantize_depth16(float value) { | ||
| 1565 | if (!(value > 0.0f)) return 0.0f; | ||
| 1566 | if (value >= 1.0f) return 1.0f; | ||
| 1567 | const float product = value * 65535.0f; | ||
| 1568 | return static_cast<float>(round_half_even(double(product)) / 65535.0); | ||
| 1569 | } | ||
| 1570 | |||
| 1562 | // 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 | 1571 | // 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 |
| 1563 | // as -127 and ties go to the even neighbour. | 1572 | // as -127 and ties go to the even neighbour. |
| 1564 | inline float quantize_snorm(float value, double maximum) { | 1573 | inline float quantize_snorm(float value, double maximum) { |
| @@ -4880,7 +4889,8 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_ | |||
| 4880 | const VulkanAccess state = vulkan_access(d3d12_state::depth_write, false); | 4889 | const VulkanAccess state = vulkan_access(d3d12_state::depth_write, false); |
| 4881 | auto pieces = detail::current(tracker, id, range, image_layout(p.binding, state.layout)); | 4890 | auto pieces = detail::current(tracker, id, range, image_layout(p.binding, state.layout)); |
| 4882 | detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker); | 4891 | detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker); |
| 4883 | float depth = p.binding.xbox_depth16_stencil8 ? std::round(std::clamp(depth_clear->depth,0.0f,1.0f)*65535.0f)/65535.0f : p.binding.depth24 ? depth24_clear_value(depth_clear->depth) // see depth24.h | 4892 | float depth = p.binding.xbox_depth16_stencil8 ? detail::quantize_depth16(depth_clear->depth) |
| 4893 | : p.binding.depth24 ? depth24_clear_value(depth_clear->depth) // see depth24.h | ||
| 4884 | : p.binding.format == VK_FORMAT_D16_UNORM | 4894 | : p.binding.format == VK_FORMAT_D16_UNORM |
| 4885 | ? detail::quantize_unorm(depth_clear->depth, 65535.0) // see quantize_unorm | 4895 | ? detail::quantize_unorm(depth_clear->depth, 65535.0) // see quantize_unorm |
| 4886 | : depth_clear->depth; | 4896 | : depth_clear->depth; |