Skip to content

Commit

Permalink
Add SV_DrawIndex. (#5787)
Browse files Browse the repository at this point in the history
Co-authored-by: Ellie Hermaszewska <ellieh@nvidia.com>
  • Loading branch information
csyonghe and expipiplus1 authored Dec 9, 2024
1 parent ef81bde commit ebfbe58
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/user-guide/a2-01-spirv-target-specific.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The system-value semantics are translated to the following SPIR-V code.
| `SV_DepthLessEqual` | `BuiltIn FragDepth` |
| `SV_DispatchThreadID` | `BuiltIn GlobalInvocationId` |
| `SV_DomainLocation` | `BuiltIn TessCoord` |
| `SV_DrawIndex` | `Builtin DrawIndex` |
| `SV_GSInstanceID` | `BuiltIn InvocationId` |
| `SV_GroupID` | `BuiltIn WorkgroupId` |
| `SV_GroupIndex` | `BuiltIn LocalInvocationIndex` |
Expand All @@ -68,7 +69,7 @@ The system-value semantics are translated to the following SPIR-V code.
| `SV_ViewID` | `BuiltIn ViewIndex` |
| `SV_ViewportArrayIndex` | `BuiltIn ViewportIndex` |

*Note* that `SV_PointSize` is a Slang-specific semantic that is not defined in HLSL.
*Note* that `SV_DrawIndex` and `SV_PointSize` are Slang-specific semantics that are not defined in HLSL.


Behavior of `discard` after SPIR-V 1.6
Expand Down
4 changes: 4 additions & 0 deletions source/slang/slang-emit-glsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,10 @@ void GLSLSourceEmitter::_maybeEmitGLSLBuiltin(IRGlobalParam* var, UnownedStringS
{
_requireGLSLExtension(toSlice("GL_EXT_fragment_shading_rate_primitive"));
}
else if (name == "gl_DrawID")
{
_requireGLSLVersion(460);
}
}

void GLSLSourceEmitter::_requireBaseType(BaseType baseType)
Expand Down
5 changes: 5 additions & 0 deletions source/slang/slang-emit-spirv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5137,6 +5137,11 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
// float in hlsl & glsl
return getBuiltinGlobalVar(inst->getFullType(), SpvBuiltInPointSize, inst);
}
else if (semanticName == "sv_drawindex")
{
requireSPIRVCapability(SpvCapabilityDrawParameters);
return getBuiltinGlobalVar(inst->getFullType(), SpvBuiltInDrawIndex, inst);
}
else if (semanticName == "sv_primitiveid")
{
auto entryPoints = m_referencingEntryPoints.tryGetValue(inst);
Expand Down
5 changes: 5 additions & 0 deletions source/slang/slang-ir-glsl-legalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,11 @@ GLSLSystemValueInfo* getGLSLSystemValueInfo(
name = "gl_PointSize";
requiredType = builder->getBasicType(BaseType::Float);
}
else if (semanticName == "sv_drawindex")
{
name = "gl_DrawID";
requiredType = builder->getBasicType(BaseType::Int);
}
else if (semanticName == "sv_primitiveid")
{
// uint in hlsl, int in glsl
Expand Down
1 change: 1 addition & 0 deletions source/slang/slang-ir-legalize-varying-params.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ IRInst* emitCalcGroupIndex(IRBuilder& builder, IRInst* groupThreadID, IRInst* gr
M(OutputControlPointID, SV_OutputControlPointID) \
M(PointSize, SV_PointSize) \
M(PrimitiveID, SV_PrimitiveID) \
M(DrawIndex, SV_DrawIndex) \
M(RenderTargetArrayIndex, SV_RenderTargetArrayIndex) \
M(SampleIndex, SV_SampleIndex) \
M(StencilRef, SV_StencilRef) \
Expand Down
1 change: 1 addition & 0 deletions source/slang/slang-language-server-completion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ static const char* hlslSemanticNames[] = {
"SV_Position",
"SV_PointSize",
"SV_PrimitiveID",
"SV_DrawIndex",
"SV_RenderTargetArrayIndex",
"SV_SampleIndex",
"SV_StencilRef",
Expand Down
18 changes: 18 additions & 0 deletions tests/spirv/draw-index.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//TEST:SIMPLE(filecheck=SPIRV): -target spirv
//TEST:SIMPLE(filecheck=SPIRV): -target spirv -entry vsMain -stage vertex -emit-spirv-via-glsl
//TEST:SIMPLE(filecheck=GLSL): -target glsl -entry vsMain -stage vertex

// SPIRV: OpDecorate %{{.*}} BuiltIn DrawIndex
// GLSL: gl_DrawID

struct VertexIn
{
float3 position;
int drawIndex : SV_DrawIndex;
}

[shader("vertex")]
float4 vsMain(VertexIn vin) : SV_Position
{
return float4(vin.position, vin.drawIndex);
}

0 comments on commit ebfbe58

Please sign in to comment.