Cut a mesh fallback draw's workgroup count that the GPU wrote to what the fallback's buffers hold before a dispatch reads it.

The record's copies put the count the game's culling pass wrote into the first dispatch and, without a task stage, into the draw's instances as it was. A garbage count launched up to millions of workgroups that write nothing and instances that draw nothing, enough to keep the GPU past the driver's timeout; with the fallback now the default on AMD's driver for RDNA 1 and 2, such a count would have moved a hang there instead of removing it. A single-thread compute pass (shaders/meshfb_clamp.comp) cuts the count to the buffers' room, which changes nothing that is drawn; the report word keeps the count asked for, so record_check still logs a mesh-only draw that asked for more than the buffers hold. On for every device that uses the fallback: one dispatch and one barrier per indirect fallback draw.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 12:32:02 -05:00
parent 46d71da8aa
commit edd5d04e8c
4 changed files with 68 additions and 3 deletions

View File

@@ -0,0 +1,29 @@
#version 450
// The mesh fallback's pass between a draw's record and its first dispatch, for a draw whose workgroup count the GPU
// wrote (vk_mesh_fb.c vk_meshfb_clamp, vk_exec_draw.c mfb_draw). The record's copies put that count into the first
// dispatch and, without a task stage, into the draw's instances as it is; a garbage count (the game's culling pass
// reading memory nothing wrote) would launch up to millions of workgroups that write nothing and instances that draw
// nothing, past the buffers' end, and keep the GPU busy past the driver's timeout. The count is cut to what the
// buffers hold, which changes nothing that is drawn; the record's report word keeps the count asked for, so the log
// names such a draw (vk_mesh_fb.c record_check).
layout(local_size_x = 1) in;
layout(push_constant) uniform Params {
uint rec; // the record's first word in the record buffer
uint task; // 1: the count is the task dispatch's (word 0); 0: the mesh dispatch's and the draw's instances (4, 9)
uint room; // workgroups the buffers hold, at most what one dispatch may have
} pc;
// The record (meshfb_scan.comp): words 0-2 the task dispatch, 4-6 the mesh dispatch, 8-11 the draw (vertices,
// instances, first vertex, first instance), 12 the mesh workgroups asked for, 13 the task workgroups.
layout(set = 0, binding = 0) buffer Rec { uint w[]; } rec;
void main() {
if (pc.task != 0u) {
rec.w[pc.rec] = min(rec.w[pc.rec], pc.room);
} else {
uint m = min(rec.w[pc.rec + 4u], pc.room);
rec.w[pc.rec + 4u] = m;
rec.w[pc.rec + 9u] = m;
}
}

View File

@@ -6042,7 +6042,7 @@ static void mfb_draw(VkExec *ex, uint32_t count, const VkMemSpan *ind) {
if (!vs || !vk_meshfb_init()) { draw_skipped(ex, 1, SK_PIPE_FAILED); return; }
uint32_t verts = c->mi.maxp * c->mi.ipp, slot_words = meshfb_slot_words(&c->mi);
uint32_t most = vk_ctx()->props.limits.maxComputeWorkGroupCount[0];
if (count > most) count = most; // a count the GPU wrote is not looked at here: the dispatch reads it as it is
if (count > most) count = most; // a count the GPU wrote is cut on the GPU, below
VkBuffer rec;
int cmds;
vk_exec_barrier(ex); // ends the pass; the culling's record and the last draw's reads come before what follows
@@ -6050,6 +6050,12 @@ static void mfb_draw(VkExec *ex, uint32_t count, const VkMemSpan *ind) {
verts, slot_words, &rec, &cmds);
ex->recorded += (uint32_t)cmds;
vk_exec_barrier(ex);
if (ind) { // to what the buffers hold, before a dispatch reads it (meshfb_clamp.comp)
vk_meshfb_clamp(ex->cb, at, ts ? c->ti.payload_bytes / 4 : 0);
cx->B.pipe[1] = VK_NULL_HANDLE; // the clamp bound its own
ex->recorded += 4;
vk_exec_barrier(ex);
}
if (ts) {
if (!mfb_dispatch(ex, c->cs_task, NVN_STAGE_TASK, rec, at)) { draw_skipped(ex, 1, SK_PIPE_FAILED); return; }
vk_exec_barrier(ex);

View File

@@ -20,6 +20,7 @@
#include "vk.h"
#include "vk_mesh_fb.h"
#include "meshfb_scan_spv.h"
#include "meshfb_clamp_spv.h"
enum { MESHFB_RECS = 4096, REC_BYTES = 64 };
@@ -36,7 +37,7 @@ static struct {
PFN_vkCmdPushDescriptorSetKHR push;
VkDescriptorSetLayout dsl;
VkPipelineLayout layout;
VkPipeline scan;
VkPipeline scan, clamp;
} M;
static int make_buffer(VkCtx *c, VkDeviceSize size, VkBufferUsageFlags usage, int host, VkBuffer *b, VmaAllocation *a, void **map) {
@@ -100,11 +101,18 @@ int vk_meshfb_init(void) {
VkResult res = vkCreateComputePipelines(c->device, VK_NULL_HANDLE, 1, &ci, NULL, &M.scan);
vkDestroyShaderModule(c->device, m, NULL);
if (res != VK_SUCCESS) goto fail;
// the clamp of a count the GPU wrote (vk_meshfb_clamp): binding 0 and three push constants of the same layout
mi.codeSize = sizeof meshfb_clamp_spv, mi.pCode = meshfb_clamp_spv, m = VK_NULL_HANDLE;
if (vkCreateShaderModule(c->device, &mi, NULL, &m) != VK_SUCCESS) goto fail;
ci.stage.module = m;
res = vkCreateComputePipelines(c->device, VK_NULL_HANDLE, 1, &ci, NULL, &M.clamp);
vkDestroyShaderModule(c->device, m, NULL);
if (res != VK_SUCCESS) goto fail;
rt_log("vk: mesh fallback: %llu MiB for task payloads, %llu MiB for mesh output (mesh_fallback_mb = %llu)",
(unsigned long long)(M.task_bytes >> 20), (unsigned long long)(M.mesh_bytes >> 20), (unsigned long long)mb);
return M.ok = 1;
fail:
rt_log("vk: mesh fallback: the scan pipeline could not be created: task and mesh draws are skipped");
rt_log("vk: mesh fallback: the scan or clamp pipeline could not be created: task and mesh draws are skipped");
return 0;
}
@@ -183,6 +191,23 @@ void vk_meshfb_scan(VkCommandBuffer cb, VkDeviceSize at, uint32_t task_words, ui
vkCmdDispatch(cb, 1, 1, 1);
}
// A count the GPU wrote, copied into record `at` (vk_meshfb_record with a source), cut to what the buffers hold before a
// dispatch reads it (meshfb_clamp.comp says why). task_words: the task stage's payload words, 0 without a task stage.
// One single-thread dispatch; the caller orders it after the record's copies and before the first dispatch.
void vk_meshfb_clamp(VkCommandBuffer cb, VkDeviceSize at, uint32_t task_words) {
uint32_t i = (uint32_t)(at / REC_BYTES), most = vk_ctx()->props.limits.maxComputeWorkGroupCount[0];
uint32_t room = task_words ? (uint32_t)(M.task_bytes / 4 / task_words) : M.cap_slots[i];
if (room > most) room = most;
VkDescriptorBufferInfo bi = {M.rec, 0, VK_WHOLE_SIZE};
VkWriteDescriptorSet w = {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 0, .descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, .pBufferInfo = &bi};
uint32_t pc[3] = {(uint32_t)(at / 4), task_words != 0, room};
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, M.clamp);
rec_push(M.push, cb, VK_PIPELINE_BIND_POINT_COMPUTE, M.layout, 0, 1, &w);
vkCmdPushConstants(cb, M.layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof pc, pc);
vkCmdDispatch(cb, 1, 1, 1);
}
void vk_meshfb_note(VkCommandBuffer cb, VkDeviceSize at) {
uint32_t i = (uint32_t)(at / REC_BYTES);
VkBufferCopy cp = {at + 48, (VkDeviceSize)i * 8, 8};
@@ -204,6 +229,7 @@ void vk_meshfb_shutdown(void) {
"hold the largest)", (unsigned long long)M.short_draws, (unsigned long long)M.draws,
(unsigned long long)((M.worst_bytes * 8 / 7 >> 20) + 1));
if (M.scan) vkDestroyPipeline(c->device, M.scan, NULL);
if (M.clamp) vkDestroyPipeline(c->device, M.clamp, NULL);
if (M.layout) vkDestroyPipelineLayout(c->device, M.layout, NULL);
if (M.dsl) vkDestroyDescriptorSetLayout(c->device, M.dsl, NULL);
if (M.task) vmaDestroyBuffer(c->vma, M.task, M.task_a);

View File

@@ -18,6 +18,10 @@ uint64_t vk_meshfb_mesh_words(void); // the mesh buffer's size as a binding se
// was too small for. Returns the number of commands recorded in *cmds.
VkDeviceSize vk_meshfb_record(VkCommandBuffer cb, VkBuffer src, VkDeviceSize src_off, uint32_t count, int has_task,
uint32_t verts, uint32_t slot_words, VkBuffer *rec, int *cmds);
// For a record whose count came from src (a count the GPU wrote): cuts it to what the buffers hold, in the task dispatch
// (task_words, the task stage's payload words) or without a task stage (0) in the mesh dispatch and the instances
// (shaders/meshfb_clamp.comp). Binds its own compute pipeline and set; four commands, between two barriers.
void vk_meshfb_clamp(VkCommandBuffer cb, VkDeviceSize at, uint32_t task_words);
// The pass between the task and the mesh dispatch (shaders/meshfb_scan.comp): binds its own compute pipeline and set.
void vk_meshfb_scan(VkCommandBuffer cb, VkDeviceSize at, uint32_t task_words, uint32_t fan, uint32_t slot_words, uint32_t verts);
// After the record is complete (the copies or the scan, and a barrier): keeps what the draw asked for where the host