Files
mandy/runtime/src/depth24_convert.comp
AlxcBTW 0e63a76da0 D24 conversion: predication, large copies, tests, spec note (review)
- A dispatch honours conditional rendering and the layer's copies do not, so a
  SetPredication that skips copies also skipped the D24 conversion while the
  copy ran. Conditional rendering is ended around the dispatch and begun
  again; a test runs an upload and readback under a skipping predicate and
  compares with the host's unpredicated run (it fails without the fix).
- A copy larger than maxStorageBufferRange (queried; 128 MiB assumed if not)
  is split into dispatches by slices, or by rows of one slice; the descriptors
  bind 256-byte-aligned windows of both buffers. depth24_convert_test splits
  the 4096x4096 copy with a 1 MiB range and a pitched one with a 1 KiB range.
- make_pipeline destroys the layouts it made when a later step fails.
- depth24_convert_test is in the CI tool list; the docs say which GPU each test
  exercises.
- R24G8_TYPELESS with a typed D24 DSV is compared with the host. SRVs of
  R24_UNORM_X8_TYPELESS and X24_TYPELESS_G8_UINT are an empty slot in the
  layer (no format mapping; stencil-plane views refused): reported by the
  test and documented as a known gap.
- depth24.h: the D3D11.3 spec's FLOAT -> UNORM wording (3.2.3.6, 3.2.2) next to
  what WARP and AMD were measured to do; "159" is 5215.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 03:15:13 +01:00

53 lines
2.8 KiB
Plaintext

// D24_UNORM_S8_UINT on a GPU without D24: the layer stores D32_SFLOAT_S8_UINT, and the depth words of a copy
// footprint (24-bit UNORM in bits 0-23, bits 24-31 unspecified) are converted here. depth24_convert_spv.h is this file
// compiled with `glslc --target-env=vulkan1.1 -O -mfmt=c` (see the header for the command); keep them together.
//
// to_float != 0 (upload): scratch[s] = float bits of the float nearest to d / 16777215, d = app[a] & 0xffffff
// to_float == 0 (readback): app[a] = D3D's quantisation of scratch[s] (depth24.h; round to nearest even of f * 16777215,
// NaN and negatives 0, 1 and above 16777215), the unspecified top byte written as 0
// The upload direction is integer arithmetic (no float division, whose rounding is the GPU's choice); the readback
// uses one float multiply, which Vulkan requires to be correctly rounded, and roundEven.
#version 450
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
layout(set = 0, binding = 0) buffer App { uint app[]; };
layout(set = 0, binding = 1) buffer Scratch { uint scratch[]; };
layout(push_constant) uniform Push {
uint width, height, slices;
uint to_float;
uint app_base; // first word of the region in `app`, within the bound window
uint app_row; // words between rows and between slices of the app buffer
uint app_slice;
uint scratch_base; // first word of the region in `scratch`, within the bound window
} pc;
// d / (2^24 - 1) as a float. With m the 24-bit mantissa of d (d = m * 2^(e-23)) the quotient is
// (m + m / (2^24 - 1)) * 2^(e-47), and m / (2^24 - 1) lies in (0.5, 1] for every m in [2^23, 2^24], so the nearest
// 24-bit mantissa is m + 1 and the result is exactly float(m + 1) * 2^(e-47).
float depth24_to_float(uint d) {
if (d == 0u) return 0.0;
int e = findMSB(d);
uint m = d << uint(23 - e);
return float(m + 1u) * uintBitsToFloat(uint(e - 47 + 127) << 23);
}
// The 24-bit UNORM of f as WARP and AMD's D3D12 drivers write it: the product with 2^24 - 1 is rounded to a float
// first, then to the nearest integer with ties to even (NVIDIA's hardware breaks some ties the other way; see
// depth24.h). NaN and negatives give 0, 1 and above the maximum.
uint depth24_from_float(float f) {
if (!(f > 0.0)) return 0u;
if (f >= 1.0) return 0xffffffu;
return uint(roundEven(f * 16777215.0));
}
void main() {
uvec3 g = gl_GlobalInvocationID;
if (g.x >= pc.width || g.y >= pc.height || g.z >= pc.slices) return;
uint a = pc.app_base + g.z * pc.app_slice + g.y * pc.app_row + g.x;
uint s = pc.scratch_base + (g.z * pc.height + g.y) * pc.width + g.x;
if (pc.to_float != 0u) scratch[s] = floatBitsToUint(depth24_to_float(app[a] & 0xffffffu));
else app[a] = depth24_from_float(uintBitsToFloat(scratch[s]));
}