upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorsanuk369 <224479491+quackottu@users.noreply.github.com>2026-11-22 11:53:58 +0530
committersanuk369 <224479491+quackottu@users.noreply.github.com>2026-11-22 11:53:58 +0530
commit7d764b9e0d1a31366738b568659d9bf044efb60e (patch)
tree9cede113d571ef8bc60df04c2c36f67f65dab2eb /runtime/src
parent43d470316ec6cbcd64002bc3792998d9f5745dac (diff)
uav_clear: float clears to 16-bit and 11/10-bit floats round toward zero and saturate, as D3D does
small_float rounded to nearest even and turned any too-large value into infinity. D3D11.3 3.2.2 says float-to-float conversion rounds toward zero and a finite value too large becomes the largest finite (only infinity stays infinity); ClearUnorderedAccessViewFloat on WARP and on NVIDIA gives exactly that: R16 70000 or 65520 -> 0x7bff, 1e-7 -> 0x0001, R11G11B10 (1e6, 1e6, 1e6) -> 0xf7fdffbf, B = 1/3 -> 0x1aa. The layer wrote infinity (which a shader then turns into NaN) and rounded up. d3d12_uav_clear_test's conversion checks asserted the old rule for 65520 and 1e-7; they now hold the host values, with overflow, infinity and R11G11B10 cases added.
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/uav_clear.h20
1 files changed, 10 insertions, 10 deletions
diff --git a/runtime/src/uav_clear.h b/runtime/src/uav_clear.h
index 6b5c53fc3..73ec54dea 100644
--- a/runtime/src/uav_clear.h
+++ b/runtime/src/uav_clear.h
@@ -10,7 +10,8 @@
10// whatever the format's type: 0x3f800000 into an R32_FLOAT channel is 1.0, 0xff into an R8_UNORM one is 1.0. 10// whatever the format's type: 0x3f800000 into an R32_FLOAT channel is 1.0, 0xff into an R8_UNORM one is 1.0.
11// A raw or structured buffer view takes value 0 for every 32-bit word. 11// A raw or structured buffer view takes value 0 for every 32-bit word.
12// - Float converts to the format: UNORM and SNORM clamp to [0, 1] / [-1, 1] and round to nearest (NaN is 0); 12// - Float converts to the format: UNORM and SNORM clamp to [0, 1] / [-1, 1] and round to nearest (NaN is 0);
13// 16-bit and the packed 11/10-bit floats round to nearest even (the small floats have no sign: negative is 0). 13// 16-bit and the packed 11/10-bit floats round toward zero, a finite value too large becoming the largest finite
14// (D3D11.3 3.2.2; WARP and NVIDIA agree), and the small floats have no sign: negative is 0.
14// D3D11 calls Float on an integer view invalid and D3D12 does not say; here the value is converted to the 15// D3D11 calls Float on an integer view invalid and D3D12 does not say; here the value is converted to the
15// integer (rounded toward zero, clamped, NaN is 0) so the result is defined. A raw or structured buffer view 16// integer (rounded toward zero, clamped, NaN is 0) so the result is defined. A raw or structured buffer view
16// takes value 0's bits. 17// takes value 0's bits.
@@ -123,7 +124,7 @@ inline std::uint32_t get(const Element& e, Channel c) {
123} 124}
124 125
125// A float32 as a float with `mantissa` fraction bits, `exponent` exponent bits and an optional sign bit (IEEE 126// A float32 as a float with `mantissa` fraction bits, `exponent` exponent bits and an optional sign bit (IEEE
126// half: 10, 5, signed; the packed formats: 6 or 5, 5, unsigned), rounded to nearest even. Too large is infinity. 127// half: 10, 5, signed; the packed formats: 6 or 5, 5, unsigned), rounded toward zero. Too large is the largest finite; only infinity stays infinity.
127inline std::uint32_t small_float(float value, unsigned mantissa, unsigned exponent, bool sign) { 128inline std::uint32_t small_float(float value, unsigned mantissa, unsigned exponent, bool sign) {
128 const std::uint32_t f = std::bit_cast<std::uint32_t>(value); 129 const std::uint32_t f = std::bit_cast<std::uint32_t>(value);
129 const std::uint32_t negative = f >> 31; 130 const std::uint32_t negative = f >> 31;
@@ -132,19 +133,18 @@ inline std::uint32_t small_float(float value, unsigned mantissa, unsigned expone
132 if (std::isnan(value)) return sign_bit | (max_exponent << mantissa) | (1u << (mantissa - 1)); 133 if (std::isnan(value)) return sign_bit | (max_exponent << mantissa) | (1u << (mantissa - 1));
133 if (!sign && negative) return 0; 134 if (!sign && negative) return 0;
134 const std::uint32_t magnitude = f & 0x7fffffffu; 135 const std::uint32_t magnitude = f & 0x7fffffffu;
136 if (magnitude == 0x7f800000u) return sign_bit | (max_exponent << mantissa); // infinity stays infinity
137 // D3D11.3 3.2.2: float-to-float conversion rounds toward zero, so a finite value too large is the largest finite.
138 const std::uint32_t largest = (max_exponent << mantissa) - 1;
135 const int bias = int(max_exponent >> 1); 139 const int bias = int(max_exponent >> 1);
136 const int e = int(magnitude >> 23) - 127 + bias; // the exponent field in the small format 140 const int e = int(magnitude >> 23) - 127 + bias; // the exponent field in the small format
137 if (e >= int(max_exponent)) return sign_bit | (max_exponent << mantissa); // overflow, or infinity 141 if (e >= int(max_exponent)) return sign_bit | largest;
138 const std::uint32_t full = (magnitude & 0x7fffffu) | 0x800000u; // 24-bit significand 142 const std::uint32_t full = (magnitude & 0x7fffffu) | 0x800000u; // 24-bit significand
139 // A normal result keeps `mantissa` bits; a subnormal one shifts further right.
140 const int shift = (e > 0 ? 0 : 1 - e) + int(23 - mantissa); 143 const int shift = (e > 0 ? 0 : 1 - e) + int(23 - mantissa);
141 if (shift > 24) return sign_bit; // rounds to zero 144 if (shift > 24) return sign_bit;
142 std::uint32_t kept = full >> shift; 145 const std::uint32_t kept = full >> shift;
143 const std::uint32_t rest = full & mask(unsigned(shift));
144 const std::uint32_t half = 1u << (shift - 1);
145 if (rest > half || (rest == half && (kept & 1))) ++kept; // may carry into the exponent, which is right
146 const std::uint32_t biased = e > 0 ? (std::uint32_t(e) << mantissa) + (kept - (1u << mantissa)) : kept; 146 const std::uint32_t biased = e > 0 ? (std::uint32_t(e) << mantissa) + (kept - (1u << mantissa)) : kept;
147 return sign_bit | std::min(biased, max_exponent << mantissa); 147 return sign_bit | biased;
148} 148}
149 149
150inline float from_small_float(std::uint32_t bits, unsigned mantissa, unsigned exponent, bool sign) { 150inline float from_small_float(std::uint32_t bits, unsigned mantissa, unsigned exponent, bool sign) {