Skip to content

Implement the SetMeshOutputCounts HLSL Function #99231

Open
Listed in
@farzonl

Description

@farzonl
  • Implement SetMeshOutputCounts clang builtin,
    Link SetMeshOutputCounts clang builtin with hlsl_intrinsics.h
    Add sema checks for SetMeshOutputCounts to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp
    Add codegen for SetMeshOutputCounts to EmitHLSLBuiltinExpr in CGBuiltin.cpp
    Add codegen tests to clang/test/CodeGenHLSL/builtins/SetMeshOutputCounts.hlsl
    Add sema tests to clang/test/SemaHLSL/BuiltIns/SetMeshOutputCounts-errors.hlsl
    Create the int_dx_SetMeshOutputCounts intrinsic in IntrinsicsDirectX.td
    Create the DXILOpMapping of int_dx_SetMeshOutputCounts to 168 in DXIL.td
    Create the SetMeshOutputCounts.ll and SetMeshOutputCounts_errors.ll tests in llvm/test/CodeGen/DirectX/
    Create the int_spv_SetMeshOutputCounts intrinsic in IntrinsicsSPIRV.td
    In SPIRVInstructionSelector.cpp create the SetMeshOutputCounts lowering and map it to int_spv_SetMeshOutputCounts in SPIRVInstructionSelector::selectIntrinsic.
    Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/SetMeshOutputCounts.ll

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
168 SetMeshOutputCounts 6.5 ('mesh',)

SPIR-V

OpSetMeshOutputsEXT:

Description:

Reserved.

Capability:
MeshShadingEXT

Reserved.

Word Count Opcode Results Operands

3

5295

<id>
Vertex Count

<id>
Primitive Count

Test Case(s)

Example 1

//dxc SetMeshOutputCounts_test.hlsl -T lib_6_8 -enable-16bit-types -O0

[numthreads(1, 1, 1)]
[outputtopology("triangle")]
[shader("mesh")]
void fn(in uint gi : SV_GroupIndex, in uint vi : SV_ViewID) {

SetMeshOutputCounts(1, 1);
}

SPIRV Example(s):

Example 2

//dxc SetMeshOutputCounts_spirv_test.hlsl -T lib_6_8 -E fn -enable-16bit-types -spirv -fspv-target-env=universal1.5 -fcgl -O0

struct MeshPerVertex {
	float4 position : SV_Position;
};
[numthreads(1, 1, 1)]
[outputtopology("triangle")]
[shader("mesh")]
void fn(in uint gi : SV_GroupIndex, in uint vi : SV_ViewID,
	out vertices MeshPerVertex verts[3], out indices uint3 primitiveInd[3]) {

SetMeshOutputCounts(1, 1);
}

HLSL:

This function sets the actual number of outputs from the threadgroup.

Syntax

void SetMeshOutputCounts(
    uint numVertices,
    uint numPrimitives);

Remarks

At the beginning of the shader the implementation internally sets a
count of vertices and primitives to be exported from a threadgroup to 0.
It means that if a mesh shader returns without calling this function,
it will not output any mesh. This function sets the actual number of
outputs from the threadgroup.

Some restrictions on the function use and interactions with output arrays follow.

  1. This function can only be called once per shader.

  2. This call must occur before any writes to any of the
    shared output arrays.
    The validator will verify this is the case.

  3. If the compiler can prove that this function is not called,
    then the threadgroup doesn't have any output.
    If the shader writes to any of the
    shared output arrays,
    compilation and shader validation will fail.
    If the shader does not call any of these functions,
    the compiler will issue a warning,
    and no rasterization work will be issued.

  4. Only the input values from the first active thread are used.

  5. This call must dominate all writes to
    shared output arrays.
    In other words, there must not be any execution path
    that even appears to reach any writes to any output array
    without first having executed this call.

Activity

moved this to Ready in HLSL Supporton Oct 30, 2024
moved this from Ready to Planning in HLSL Supporton Oct 30, 2024
moved this from Planning to Designing in HLSL Supporton Jan 21, 2025
damyanp

damyanp commented on Jan 21, 2025

@damyanp
Contributor

@farzonl - think about grouping these with other mesh shader intrinsics?

moved this from Designing to No status in HLSL Supporton Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @farzonl@damyanp

        Issue actions

          Implement the `SetMeshOutputCounts` HLSL Function · Issue #99231 · llvm/llvm-project