Skip to content

gpuav: Clamp SetMeshOutputsEXT counts in safe mode - #13246

Merged
spencer-lunarg merged 1 commit into
KhronosGroup:mainfrom
apollo-2006:gpuav-mesh-safe-clamp
Sep 29, 2026
Merged

spencer-lunarg merged 1 commit into
KhronosGroup:mainfrom
apollo-2006:gpuav-mesh-safe-clamp

Conversation

@apollo-2006

@apollo-2006 apollo-2006 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

While working through the GPU-AV mesh shading pass, I came across the TODO in InjectFunctionPre about using the OutputVertices/OutputPrimitivesEXT values for OpSetMeshOutputsEXT in safe mode, and thought I'd have a go at it.

Right now, safe mode moves OpSetMeshOutputsEXT into the "valid" branch and puts SetMeshOutputsEXT(3, 1) in the "invalid" one. That works nicely for the common triangle case, but for a shader that declares something like max_vertices = 1, the 3 ends up being out of range itself.

This keeps the error reporting exactly as it is, and instead of branching, it rewrites the call's operands to min(count, max) using the pass's existing output_vertices_id_/output_primitives_id_ constants, so the call still happens exactly once with counts the driver can handle:

%51 = OpULessThanEqual %bool %14 %uint_1_0
%52 = OpSelect %uint %51 %14 %uint_1_0
%53 = OpULessThanEqual %bool %17 %uint_1_1
%54 = OpSelect %uint %53 %17 %uint_1_1
      OpSetMeshOutputsEXT %52 %54

With that in place, the OpSetMeshOutputsEXT special case in InjectFunctionPre isn't needed anymore, so I removed it.

A nice side effect of not having a branch: lavapipe runs both sides of an if/else under a mask and applies whichever SetMeshOutputsEXT comes last, so on main NegativeGpuAVMesh.PrimitiveCount (which runs in safe mode) segfaults it. That one is a lavapipe bug on the Mesa side, but this change avoids tripping over it here.

Tests:

  • New NegativeGpuAVMesh.PrimitiveCountPoints: a points shader with max_vertices = 1 and a count of 60 from a buffer. It reports 07332, and the instrumented shader clamps the count to 1.
  • New NegativeGpuAVMesh.MultipleSetMeshOutput: an if/else with a valid and an invalid SetMeshOutputsEXT, two draws taking one branch each, and exactly one 07332 in safe mode.
  • NegativeGpuAVMesh.PrimitiveCount and PrimitiveCountPoints pass on RADV (RX 9070 XT) both on main and with this change; on lavapipe both segfault on main and pass with it.
  • The instrumented SPIR-V passes spirv-val for both.
  • All GPU-AV suites on lavapipe and RADV, compared to main: the only change is PrimitiveCount no longer crashing lavapipe, and nothing new fails or crashes.

If you'd rather keep the branch and just use the execution-mode values in the else side instead, I'm very happy to switch it over to that.

@ci-tester-lunarg

Copy link
Copy Markdown
Collaborator

Author apollo-2006 not on autobuild list. Waiting for curator authorization before starting CI build.

@apollo-2006
apollo-2006 requested a review from a team as a code owner September 28, 2026 18:27

@spencer-lunarg spencer-lunarg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seem good, just 2 things

m_errorMonitor->VerifyFound();
}

TEST_F(NegativeGpuAVMesh, PrimitiveCountPoints) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you write a simple test (similar to MultipleSetMeshOutput) that does a

if (condition) {
    SetMeshOutputsEXT(v0, p0); // valid
} else {
    SetMeshOutputsEXT(v1, p1); // invalid
}

does two draws, and only gets one invalid error message (in safe mode)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added NegativeGpuAVMesh.MultipleSetMeshOutput for this: draw 0 takes the valid branch and draw 1 the invalid one, and safe mode reports exactly one 07332. I also checked that it fails when both draws go invalid, so it really does catch a second error.

const uint32_t uint32_type_id = type_manager_.GetTypeInt(32, false).Id();
const uint32_t bool_type_id = type_manager_.GetTypeBool().Id();

auto clamp = [&](uint32_t word_index, uint32_t max_id) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its only two times, rather no lambda please, easier to debug

// Clamp vertices
{
        const uint32_t count_id = set_mesh_outputs_inst.Word(1);
        const uint32_t in_range_id = module_.TakeNextId();
        const uint32_t clamped_id = module_.TakeNextId();
        block.CreateInstruction(spv::OpULessThanEqual, {bool_type_id, in_range_id, count_id, output_vertices_id_}, inst_it);
        block.CreateInstruction(spv::OpSelect, {uint32_type_id, clamped_id, in_range_id, count_id, output_vertices_id_}, inst_it);
        set_mesh_outputs_inst.UpdateWord(1, clamped_id);
 } 
 
 // Clamp primitives
{
        const uint32_t count_id = set_mesh_outputs_inst.Word(2);
        const uint32_t in_range_id = module_.TakeNextId();
        const uint32_t clamped_id = module_.TakeNextId();
        block.CreateInstruction(spv::OpULessThanEqual, {bool_type_id, in_range_id, count_id, output_primitives_id_}, inst_it);
        block.CreateInstruction(spv::OpSelect, {uint32_type_id, clamped_id, in_range_id, count_id, output_primitives_id_}, inst_it);
        set_mesh_outputs_inst.UpdateWord(2, clamped_id);
 } 

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, that does read a lot more clearly without it. Switched it over to the two explicit blocks.

Safe mode put OpSetMeshOutputsEXT behind the validation result and
called it with a hardcoded (3, 1) otherwise, with a TODO to use the
OutputVertices/OutputPrimitivesEXT values. (3, 1) is out of range for
shaders that declare fewer than 3 vertices.

Clamp the counts to OutputVertices/OutputPrimitivesEXT with OpSelect
instead, so the call is made exactly once with valid counts and
without a branch.
@ci-tester-lunarg

Copy link
Copy Markdown
Collaborator

Author apollo-2006 not on autobuild list. Waiting for curator authorization before starting CI build.

@ci-tester-lunarg

Copy link
Copy Markdown
Collaborator

CI Vulkan-ValidationLayers build queued with queue ID 135580.

@ci-tester-lunarg

Copy link
Copy Markdown
Collaborator

CI Vulkan-ValidationLayers build # 24696 running.

@ci-tester-lunarg

Copy link
Copy Markdown
Collaborator

CI Vulkan-ValidationLayers build # 24696 failed.

@spencer-lunarg
spencer-lunarg merged commit df23b4f into KhronosGroup:main Sep 29, 2026
20 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants