diff options
Diffstat (limited to 'runtime/src/indirect_clamp.comp')
| -rw-r--r-- | runtime/src/indirect_clamp.comp | 48 |
1 files changed, 38 insertions, 10 deletions
diff --git a/runtime/src/indirect_clamp.comp b/runtime/src/indirect_clamp.comp index 3b99da59a..37af7431f 100644 --- a/runtime/src/indirect_clamp.comp +++ b/runtime/src/indirect_clamp.comp | |||
| @@ -9,9 +9,19 @@ | |||
| 9 | // charging the list budget. The command list charges its work (vertices, indices, or groups * 64) against 2^27 units with | 9 | // charging the list budget. The command list charges its work (vertices, indices, or groups * 64) against 2^27 units with |
| 10 | // an atomic CAS, so thousands of garbage records cannot run for seconds either. | 10 | // an atomic CAS, so thousands of garbage records cannot run for seconds either. |
| 11 | // | 11 | // |
| 12 | // A draw record of 2^23 vertices (or indices) an instance or more is a slot the title has not filled, not a draw: it | ||
| 13 | // runs empty and charges nothing, so a few of them cannot use up the list budget and empty every real draw after them | ||
| 14 | // (RT-INDIRECT-DRAW-UNFILLED-COUNT; a census in codec-d/cool_fox's fork of VIctory found 90 records of 2^24 in one | ||
| 15 | // list, its docs/findings.md and crates/victory-gpu/src/indirect.rs; no code of it is used). | ||
| 16 | // | ||
| 12 | // The bounds cut legitimate work beyond them, so they are generous: 2^24 vertices (or indices), 2^26 vertex instances, | 17 | // The bounds cut legitimate work beyond them, so they are generous: 2^24 vertices (or indices), 2^26 vertex instances, |
| 13 | // 65 535 groups per dimension, 2^20 groups per dispatch, and mesh dispatches charged 128 threads per group. The | 18 | // the device's own maxComputeWorkGroupCount per dimension for a dispatch (65 535 for a mesh dispatch), 2^20 groups per |
| 14 | // console's mesh dispatch is one-dimensional; the output uses one group in y and z. | 19 | // dispatch, and mesh dispatches charged 128 threads per group. The console's mesh dispatch is one-dimensional; the |
| 20 | // output uses one group in y and z. | ||
| 21 | // | ||
| 22 | // A dispatch is bounded by the device's limit, not by Vulkan's guaranteed minimum of 65 535: NVIDIA, for one, allows | ||
| 23 | // 2^31 - 1 groups in x, and a fixed 65 535 cut a one-dimensional dispatch of more groups short, so the groups | ||
| 24 | // past it never ran and their output stayed stale (blocky patches; idea from codec-d/cool_fox docs/findings.md). | ||
| 15 | #version 460 | 25 | #version 460 |
| 16 | #extension GL_EXT_buffer_reference : require | 26 | #extension GL_EXT_buffer_reference : require |
| 17 | layout(local_size_x = 64) in; | 27 | layout(local_size_x = 64) in; |
| @@ -35,13 +45,19 @@ layout(push_constant) uniform Params { | |||
| 35 | uint first_record; // the record index this chunk starts at | 45 | uint first_record; // the record index this chunk starts at |
| 36 | uint max_records; // max_command_count of the ExecuteIndirect call | 46 | uint max_records; // max_command_count of the ExecuteIndirect call |
| 37 | uint flags; // bit 0: counted; bits 1..16: optional per-dispatch group budget | 47 | uint flags; // bit 0: counted; bits 1..16: optional per-dispatch group budget |
| 48 | uint max_x; // the device's maxComputeWorkGroupCount: a dispatch's x, y and z run up to these | ||
| 49 | uint max_y; // (three words, not a uvec3, whose 16-byte alignment would move it past the C++ struct) | ||
| 50 | uint max_z; | ||
| 51 | uint reserved; | ||
| 38 | } p; | 52 | } p; |
| 39 | 53 | ||
| 40 | // One ExecuteIndirect batch's limits. A record that asks for more runs empty instead of for seconds. | 54 | // One ExecuteIndirect batch's limits. A record that asks for more runs empty instead of for seconds. |
| 41 | const uint max_vertices = 1u << 24u; // vertices or indices in one draw | 55 | const uint max_vertices = 1u << 24u; // vertices or indices in one draw |
| 56 | const uint unfilled_draw = 1u << 23u; // vertices or indices an instance from which a draw record is unfilled | ||
| 42 | const uint max_vertex_instances = 1u << 26u; // vertex (or index) count times instance count | 57 | const uint max_vertex_instances = 1u << 26u; // vertex (or index) count times instance count |
| 43 | const uint max_group_dimension = 65535u; // groups in x, y or z | 58 | const uint max_mesh_dimension = 65535u; // groups in a mesh dispatch's x (dispatches: p.max_x, y, z) |
| 44 | const uint max_groups = 1u << 20u; // groups in one dispatch or mesh dispatch | 59 | const uint max_groups = 1u << 20u; // groups in one dispatch or mesh dispatch |
| 60 | const uint narrow_groups = 65535u; // the x a dispatch record was cut at before, for the counters | ||
| 45 | const uint budget = 1u << 27u; // work in the whole command list: vertices, indices, or groups * 64 | 61 | const uint budget = 1u << 27u; // work in the whole command list: vertices, indices, or groups * 64 |
| 46 | 62 | ||
| 47 | // Charges `cost` units to the batch's budget. Saturating: a record that no longer fits runs empty, so the batch never | 63 | // Charges `cost` units to the batch's budget. Saturating: a record that no longer fits runs empty, so the batch never |
| @@ -101,7 +117,8 @@ void main() { | |||
| 101 | // A draw or an indexed draw: count, instances, then the two (or three) locations, copied as they are. | 117 | // A draw or an indexed draw: count, instances, then the two (or three) locations, copied as they are. |
| 102 | const uint count = p.arguments.data[src]; | 118 | const uint count = p.arguments.data[src]; |
| 103 | const uint instances = p.arguments.data[src + 1u]; | 119 | const uint instances = p.arguments.data[src + 1u]; |
| 104 | uint index_count = min(count, max_vertices); | 120 | // An unfilled record (a count of 2^23 or more) runs empty, free: cost 0, so it leaves the budget alone. |
| 121 | uint index_count = count >= unfilled_draw ? 0u : min(count, max_vertices); | ||
| 105 | uint instance_count = min(instances, max_vertex_instances / max(index_count, 1u)); | 122 | uint instance_count = min(instances, max_vertex_instances / max(index_count, 1u)); |
| 106 | if (index_count == 0u) instance_count = 0u; | 123 | if (index_count == 0u) instance_count = 0u; |
| 107 | clamped = index_count != count || instance_count != instances ? 1u : 0u; | 124 | clamped = index_count != count || instance_count != instances ? 1u : 0u; |
| @@ -112,8 +129,8 @@ void main() { | |||
| 112 | words[3] = p.arguments.data[src + 3u]; | 129 | words[3] = p.arguments.data[src + 3u]; |
| 113 | if (p.kind == 1u) words[4] = p.arguments.data[src + 4u]; | 130 | if (p.kind == 1u) words[4] = p.arguments.data[src + 4u]; |
| 114 | } else { | 131 | } else { |
| 115 | // A dispatch or a mesh dispatch: three group counts. x * y fits 32 bits (each is at most 65 535); the whole | 132 | // A dispatch or a mesh dispatch: three group counts, each bounded by the device's limit. A product past |
| 116 | // group count may not, so an oversized product runs empty. | 133 | // max_groups runs empty; it is checked by division, because x alone may be up to 2^32 - 1. |
| 117 | const uint asked_x = p.arguments.data[src]; | 134 | const uint asked_x = p.arguments.data[src]; |
| 118 | const uint asked_y = p.arguments.data[src + 1u]; | 135 | const uint asked_y = p.arguments.data[src + 1u]; |
| 119 | const uint asked_z = p.arguments.data[src + 2u]; | 136 | const uint asked_z = p.arguments.data[src + 2u]; |
| @@ -127,21 +144,32 @@ void main() { | |||
| 127 | atomicMax(p.stats.data[11], asked_z); | 144 | atomicMax(p.stats.data[11], asked_z); |
| 128 | } | 145 | } |
| 129 | } | 146 | } |
| 130 | uint x = min(asked_x, max_group_dimension); | 147 | const uvec3 limit = p.kind == 3u ? uvec3(max_mesh_dimension, 1u, 1u) : max(uvec3(p.max_x, p.max_y, p.max_z), uvec3(1u)); |
| 131 | uint y = min(asked_y, max_group_dimension); | 148 | uint x = min(asked_x, limit.x); |
| 132 | uint z = min(asked_z, max_group_dimension); | 149 | uint y = min(asked_y, limit.y); |
| 150 | uint z = min(asked_z, limit.z); | ||
| 133 | if (p.kind == 3u) { y = 1u; z = 1u; } | 151 | if (p.kind == 3u) { y = 1u; z = 1u; } |
| 152 | // A dispatch past the device's limit in some dimension, cut there (stats word 14). | ||
| 153 | const bool past_device = p.kind != 3u && (x != asked_x || y != asked_y || z != asked_z); | ||
| 134 | const uint groups = p.flags >> 1u; | 154 | const uint groups = p.flags >> 1u; |
| 135 | if (groups != 0u && x != 0u && y != 0u && z != 0u) { | 155 | if (groups != 0u && x != 0u && y != 0u && z != 0u) { |
| 136 | x = min(x, groups); | 156 | x = min(x, groups); |
| 137 | y = min(y, groups / x); | 157 | y = min(y, groups / x); |
| 138 | z = min(z, groups / x / y); | 158 | z = min(z, groups / x / y); |
| 139 | } | 159 | } |
| 140 | if (z != 0u && x * y > max_groups / z) { | 160 | if (x != 0u && y != 0u && z != 0u && (x > max_groups || y > max_groups / x || z > max_groups / x / y)) { |
| 141 | x = 0u; | 161 | x = 0u; |
| 142 | y = 0u; | 162 | y = 0u; |
| 143 | z = 0u; | 163 | z = 0u; |
| 144 | } | 164 | } |
| 165 | if (p.kind != 3u) { | ||
| 166 | // Dispatch records (stats words 12-15): those that run wider than 65 535 groups in x, the widest x that | ||
| 167 | // runs, those cut at the device's limits (and still run), and all of them. | ||
| 168 | atomicAdd(p.stats.data[15], 1u); | ||
| 169 | if (x > narrow_groups) atomicAdd(p.stats.data[12], 1u); | ||
| 170 | atomicMax(p.stats.data[13], x); | ||
| 171 | if (past_device && x != 0u) atomicAdd(p.stats.data[14], 1u); | ||
| 172 | } | ||
| 145 | clamped = x != asked_x || y != asked_y || z != asked_z ? 1u : 0u; | 173 | clamped = x != asked_x || y != asked_y || z != asked_z ? 1u : 0u; |
| 146 | cost = x * y * z * (p.kind == 3u ? 128u : 64u); | 174 | cost = x * y * z * (p.kind == 3u ? 128u : 64u); |
| 147 | words[0] = x; | 175 | words[0] = x; |