Files
mandy/runtime/src/quad_index_expand.comp
QemmHD a9fb8058aa d3d12: indexed quad lists draw from triangle indices a compute pass expands on the GPU (OLD-9940)
An indexed quad list was one triangle fan per quad over the application's index buffer.
That is wrong in two ways, both measured on #207 (RTX 4070 Ti there, RTX 4070 SUPER here):

- A fan's triangles are (v1 v2 v0) and (v2 v3 v0), and Vulkan takes a flat input from a
  triangle's first vertex. So a quad showed its second and third vertex, never its first:
  a quad with a red first vertex came out black and blue, 0 of 4096 pixels red.
- One draw per quad runs every instance of quad 0 before quad 1. D3D12 draws instance by
  instance, so where instances overlap the wrong one ended on top (2048 of 4096 pixels).

Neither can be fixed inside that scheme (VK_EXT_provoking_vertex cannot name a fan's hub
either), so the indices are rewritten: a compute pass (quad_index_expand.comp) reads the
application's index buffer on the GPU and writes (i0 i1 i2) (i0 i2 i3) for each quad as
32-bit indices into copy scratch, and the draw is one indexed triangle list over that
scratch with the application's instance count, base vertex and first instance. Both
triangles start at the quad's first vertex, like the non-indexed path.

- The host never reads the indices: a copy or a shader earlier in the list can have
  written them. The pass runs before the draw's rendering scope, between a barrier for
  those writes and one for its own, and between two segments of an active
  pipeline-statistics query, as the other helper dispatches do (D3D-162).
- 16-bit and 32-bit indices, read as whole words, so a 16-bit view at an address of
  2 modulo 4 works. Index values are copied as they are: VertexIndex - BaseVertex is still
  the application's index.
- The strip-cut values are ordinary indices, as in every list topology. An index past the
  view reads 0, as in D3D12, and the pass reads nothing past the view. A draw's last one to
  three indices are dropped.
- It needs bufferDeviceAddress. Without it, or inside a statistics query that cannot be
  split, the list is refused as unsupported.
- vulkan_topology(19) is a triangle list for both paths; nothing draws a fan any more
  (MoltenVK has no fans).

replay_quad_list_test replays indexed draws through prepare() and the real expander on a
recording device: one dispatch before the rendering scope and one draw in it, the push
constants for both index sizes, the view's end, the split of a draw larger than one
dispatch, the refusals, and the order inside a statistics query.

AI-assisted (Claude Code; Claude Opus 5.5 and Claude Fable 5.2).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf
2026-11-24 05:10:18 -05:00

45 lines
1.8 KiB
Plaintext

// Expand the index buffer of a console quad list into triangle-list indices without reading it on the CPU
// (quad_index_expand.h). Each quad (i0 i1 i2 i3) becomes (i0 i1 i2) (i0 i2 i3) as six 32-bit indices: both
// triangles start at the quad's first vertex, the one a flat input is taken from.
#version 460
#extension GL_EXT_buffer_reference : require
layout(local_size_x = 64) in;
layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer WordsIn {
uint data[];
};
layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer WordsOut {
uint data[];
};
layout(push_constant) uniform Params {
WordsIn source; // the 4-byte aligned word that holds this dispatch's first index
WordsOut triangles; // six words for each quad of this dispatch
uint first_half; // 16-bit indices: 1 when the first index is the high half of its word
uint index_bits; // 16 or 32
uint limit; // how many of this dispatch's indices the index buffer view holds; the rest read 0
uint quads;
} p;
uint index_at(uint i) {
if (i >= p.limit) return 0u;
if (p.index_bits == 32u) return p.source.data[i];
const uint slot = i + p.first_half;
return (p.source.data[slot >> 1u] >> ((slot & 1u) * 16u)) & 0xffffu;
}
void main() {
const uint quad = gl_GlobalInvocationID.x;
if (quad >= p.quads) return;
const uint first = quad * 4u;
const uint i0 = index_at(first);
const uint i1 = index_at(first + 1u);
const uint i2 = index_at(first + 2u);
const uint i3 = index_at(first + 3u);
const uint target = quad * 6u;
p.triangles.data[target] = i0;
p.triangles.data[target + 1u] = i1;
p.triangles.data[target + 2u] = i2;
p.triangles.data[target + 3u] = i0;
p.triangles.data[target + 4u] = i2;
p.triangles.data[target + 5u] = i3;
}