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:
author73N1X <115502401+73N1X@users.noreply.github.com>2026-11-22 06:33:39 -0400
committer73N1X <115502401+73N1X@users.noreply.github.com>2026-11-22 06:33:39 -0400
commit8f898932125cffec6f35eaf5d0aae217550abfa7 (patch)
treea010b5f9b0707c8aee4332f32604279fd22c144f /runtime/src
parenta03e155134e986d4f7360cbc519586baf121fd11 (diff)
parentf85582e23d4ab9e19acbf694358f137d6b8531b1 (diff)
Merge current main into depth-stencil copy fix
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/uav_clear.h55
-rw-r--r--runtime/src/vulkan_backend.h7
2 files changed, 57 insertions, 5 deletions
diff --git a/runtime/src/uav_clear.h b/runtime/src/uav_clear.h
index 73ec54dea..a27da8d7a 100644
--- a/runtime/src/uav_clear.h
+++ b/runtime/src/uav_clear.h
@@ -8,10 +8,13 @@
8// and ...Float, which the D3D12 methods keep): 8// and ...Float, which the D3D12 methods keep):
9// - Uint copies the low n bits of value i into channel i, where n is the channel's width, with no conversion, 9// - Uint copies the low n bits of value i into channel i, where n is the channel's width, with no conversion,
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. R9G9B9E5_SHAREDEXP has no independent
12// per-channel integer bit layout and WARP clears it to 0 for any uint input.
12// - Float converts to the format: UNORM and SNORM clamp to [0, 1] / [-1, 1] and round to nearest (NaN is 0); 13// - 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 toward zero, a finite value too large becoming the largest finite 14// 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. 15// (D3D11.3 3.2.2; WARP and NVIDIA agree), and the small floats have no sign: negative is 0.
16// R9G9B9E5_SHAREDEXP clamps negative/NaN to 0 and +Inf/overflow to 65408.0, shares the largest channel's
17// exponent and rounds mantissas half-up (bumping the exponent on 512), matching WARP.
15// D3D11 calls Float on an integer view invalid and D3D12 does not say; here the value is converted to the 18// D3D11 calls Float on an integer view invalid and D3D12 does not say; here the value is converted to the
16// integer (rounded toward zero, clamped, NaN is 0) so the result is defined. A raw or structured buffer view 19// integer (rounded toward zero, clamped, NaN is 0) so the result is defined. A raw or structured buffer view
17// takes value 0's bits. 20// takes value 0's bits.
@@ -35,7 +38,7 @@
35 38
36namespace leonida::uav_clear { 39namespace leonida::uav_clear {
37 40
38enum class Kind : std::uint8_t { unorm, snorm, uint, sint, sfloat, ufloat }; 41enum class Kind : std::uint8_t { unorm, snorm, uint, sint, sfloat, ufloat, shared_exp };
39 42
40// One channel: where its bits are in the element (little-endian, from bit 0 of byte 0) and how many. 43// One channel: where its bits are in the element (little-endian, from bit 0 of byte 0) and how many.
41struct Channel { 44struct Channel {
@@ -66,6 +69,7 @@ inline constexpr Layout layouts[] = {
66 {VK_FORMAT_A2B10G10R10_UNORM_PACK32, Kind::unorm, 4, {{{0, 10}, {10, 10}, {20, 10}, {30, 2}}}}, 69 {VK_FORMAT_A2B10G10R10_UNORM_PACK32, Kind::unorm, 4, {{{0, 10}, {10, 10}, {20, 10}, {30, 2}}}},
67 {VK_FORMAT_A2B10G10R10_UINT_PACK32, Kind::uint, 4, {{{0, 10}, {10, 10}, {20, 10}, {30, 2}}}}, 70 {VK_FORMAT_A2B10G10R10_UINT_PACK32, Kind::uint, 4, {{{0, 10}, {10, 10}, {20, 10}, {30, 2}}}},
68 {VK_FORMAT_B10G11R11_UFLOAT_PACK32, Kind::ufloat, 4, {{{0, 11}, {11, 11}, {22, 10}, {}}}}, 71 {VK_FORMAT_B10G11R11_UFLOAT_PACK32, Kind::ufloat, 4, {{{0, 11}, {11, 11}, {22, 10}, {}}}},
72 {VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, Kind::shared_exp, 4, {{{0, 9}, {9, 9}, {18, 9}, {27, 5}}}},
69 {VK_FORMAT_R8G8B8A8_UNORM, Kind::unorm, 4, {{{0, 8}, {8, 8}, {16, 8}, {24, 8}}}}, 73 {VK_FORMAT_R8G8B8A8_UNORM, Kind::unorm, 4, {{{0, 8}, {8, 8}, {16, 8}, {24, 8}}}},
70 {VK_FORMAT_R8G8B8A8_SNORM, Kind::snorm, 4, {{{0, 8}, {8, 8}, {16, 8}, {24, 8}}}}, 74 {VK_FORMAT_R8G8B8A8_SNORM, Kind::snorm, 4, {{{0, 8}, {8, 8}, {16, 8}, {24, 8}}}},
71 {VK_FORMAT_R8G8B8A8_UINT, Kind::uint, 4, {{{0, 8}, {8, 8}, {16, 8}, {24, 8}}}}, 75 {VK_FORMAT_R8G8B8A8_UINT, Kind::uint, 4, {{{0, 8}, {8, 8}, {16, 8}, {24, 8}}}},
@@ -184,6 +188,33 @@ inline std::uint32_t to_integer(float value, bool is_signed, unsigned bits) {
184 return std::uint32_t(std::clamp(std::trunc(double(value)), 0.0, std::ldexp(1.0, int(bits)) - 1)); 188 return std::uint32_t(std::clamp(std::trunc(double(value)), 0.0, std::ldexp(1.0, int(bits)) - 1));
185} 189}
186 190
191// Shared-exponent 9:9:9:5 unsigned float (EXT_texture_shared_exponent / D3D11.3).
192// Clamps negative and NaN to 0 and overflow/+Inf to 65408.0 ((511/512) * 2^16), shares the largest channel's
193// exponent, rounds mantissas half-up (floor(c / denom + 0.5)), and bumps the exponent if the largest mantissa
194// rounds to 512. Matches ClearUnorderedAccessViewFloat on WARP.
195inline std::uint32_t to_rgb9e5(float r, float g, float b) {
196 constexpr float max_value = 65408.0f;
197 const auto clamp_channel = [](float v) { return !(v > 0.0f) ? 0.0f : (v > max_value ? max_value : v); };
198 r = clamp_channel(r);
199 g = clamp_channel(g);
200 b = clamp_channel(b);
201 const float max_c = std::max(r, std::max(g, b));
202 if (max_c == 0.0f) return 0u;
203 int exp_raw = 0;
204 std::frexp(max_c, &exp_raw); // floor(log2(max_c)) == exp_raw - 1
205 int exp_shared = std::max(0, exp_raw + 15);
206 double denom = std::ldexp(1.0, exp_shared - 24); // 2^(exp_shared - 15 - 9)
207 const int max_m = static_cast<int>(std::floor(double(max_c) / denom + 0.5));
208 if (max_m == 512) {
209 ++exp_shared;
210 denom *= 2.0;
211 }
212 const auto rm = static_cast<std::uint32_t>(std::floor(double(r) / denom + 0.5));
213 const auto gm = static_cast<std::uint32_t>(std::floor(double(g) / denom + 0.5));
214 const auto bm = static_cast<std::uint32_t>(std::floor(double(b) / denom + 0.5));
215 return rm | (gm << 9) | (bm << 18) | (static_cast<std::uint32_t>(exp_shared) << 27);
216}
217
187} // namespace detail 218} // namespace detail
188 219
189// The element a clear writes into a view of `format`. `values` are the call's four UINTs, or the bits of its four 220// The element a clear writes into a view of `format`. `values` are the call's four UINTs, or the bits of its four
@@ -193,6 +224,14 @@ inline bool encode(VkFormat format, bool is_float, const std::array<std::uint32_
193 if (!l) return false; 224 if (!l) return false;
194 out = {}; 225 out = {};
195 out.size = l->bytes; 226 out.size = l->bytes;
227 if (l->kind == Kind::shared_exp) {
228 const std::uint32_t word = is_float ? detail::to_rgb9e5(std::bit_cast<float>(values[0]),
229 std::bit_cast<float>(values[1]),
230 std::bit_cast<float>(values[2]))
231 : 0u;
232 std::memcpy(out.bytes.data(), &word, 4);
233 return true;
234 }
196 for (unsigned i = 0; i < 4; ++i) { 235 for (unsigned i = 0; i < 4; ++i) {
197 const Channel c = l->rgba[i]; 236 const Channel c = l->rgba[i];
198 if (!c.bits) continue; 237 if (!c.bits) continue;
@@ -215,6 +254,7 @@ inline bool encode(VkFormat format, bool is_float, const std::array<std::uint32_
215 case Kind::sint: detail::put(out, c, detail::to_integer(v, true, c.bits)); break; 254 case Kind::sint: detail::put(out, c, detail::to_integer(v, true, c.bits)); break;
216 case Kind::sfloat: 255 case Kind::sfloat:
217 case Kind::ufloat: detail::put(out, c, detail::to_float_bits(v, *l, c.bits)); break; 256 case Kind::ufloat: detail::put(out, c, detail::to_float_bits(v, *l, c.bits)); break;
257 case Kind::shared_exp: break;
218 } 258 }
219 } 259 }
220 return true; 260 return true;
@@ -226,6 +266,16 @@ inline bool decode(VkFormat format, const Element& element, VkClearColorValue& o
226 const Layout* l = layout(format); 266 const Layout* l = layout(format);
227 if (!l || l->bytes != element.size) return false; 267 if (!l || l->bytes != element.size) return false;
228 out = {}; 268 out = {};
269 if (l->kind == Kind::shared_exp) {
270 std::uint32_t word = 0;
271 std::memcpy(&word, element.bytes.data(), 4);
272 const float scale = std::ldexp(1.0f, static_cast<int>(word >> 27) - 24);
273 out.float32[0] = float(word & 0x1ffu) * scale;
274 out.float32[1] = float((word >> 9) & 0x1ffu) * scale;
275 out.float32[2] = float((word >> 18) & 0x1ffu) * scale;
276 out.float32[3] = 1.0f;
277 return true;
278 }
229 for (unsigned i = 0; i < 4; ++i) { 279 for (unsigned i = 0; i < 4; ++i) {
230 const Channel c = l->rgba[i]; 280 const Channel c = l->rgba[i];
231 if (!c.bits) continue; 281 if (!c.bits) continue;
@@ -246,6 +296,7 @@ inline bool decode(VkFormat format, const Element& element, VkClearColorValue& o
246 } 296 }
247 case Kind::sfloat: 297 case Kind::sfloat:
248 case Kind::ufloat: out.float32[i] = detail::from_float_bits(raw, *l, c.bits); break; 298 case Kind::ufloat: out.float32[i] = detail::from_float_bits(raw, *l, c.bits); break;
299 case Kind::shared_exp: break;
249 } 300 }
250 } 301 }
251 return true; 302 return true;
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index ce1b56635..8a222c86f 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -908,11 +908,12 @@ private:
908 info.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY; 908 info.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
909 info.format = r.format; 909 info.format = r.format;
910 info.subresourceRange = range; 910 info.subresourceRange = range;
911 // Reinterpreted RTVs need only color-attachment usage. Inheriting STORAGE usage from the resource 911 // Attachment views need only color-attachment usage. Inheriting STORAGE usage from a mutable/extended
912 // would incorrectly require it from an sRGB view, even though this view is never used as storage. 912 // resource (typeless RGBA8 sRGB views, or R32_UINT-backed R9G9B9E5 UAV images) would require unrelated usages.
913 VkImageViewUsageCreateInfo usage{VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO}; 913 VkImageViewUsageCreateInfo usage{VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO};
914 usage.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; 914 usage.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
915 if (r.rgba8_typeless) info.pNext = &usage; 915 if (r.rgba8_typeless || (range.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT && r.color_attachment))
916 info.pNext = &usage;
916 VkImageView view = VK_NULL_HANDLE; 917 VkImageView view = VK_NULL_HANDLE;
917 if (vk.vkCreateImageView(device_, &info, nullptr, &view) != VK_SUCCESS) return VK_NULL_HANDLE; 918 if (vk.vkCreateImageView(device_, &info, nullptr, &view) != VK_SUCCESS) return VK_NULL_HANDLE;
918 views_[key] = view; 919 views_[key] = view;