summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDarren Wihandi <65404740+fairywreath@users.noreply.github.com>2024-12-20 01:00:20 -0500
committerGitHub <noreply@github.com>2024-12-19 22:00:20 -0800
commita00db74d1afa717dd90dfcf3170c63d0d1c0d3d7 (patch)
tree0e286826852990c9fb5b4e531a5b3eeb1da48f96
parent2e9605e79c64315ecad7ae8297d996ae2ed4687b (diff)
Add base vertex and base instance system values (#5918)
* Add base vertex and base instance system values * Fixed incorrect stage in tests
-rw-r--r--docs/user-guide/a2-01-spirv-target-specific.md4
-rw-r--r--docs/user-guide/a2-02-metal-target-specific.md4
-rw-r--r--source/slang/glsl.meta.slang2
-rw-r--r--source/slang/slang-emit-spirv.cpp10
-rw-r--r--source/slang/slang-ir-glsl-legalize.cpp16
-rw-r--r--source/slang/slang-ir-legalize-varying-params.h2
-rw-r--r--source/slang/slang-ir-metal-legalize.cpp12
-rw-r--r--source/slang/slang-ir-wgsl-legalize.cpp2
-rw-r--r--source/slang/slang-language-server-completion.cpp2
-rw-r--r--tests/glsl-intrinsic/system-values-draw-parameters.slang19
-rw-r--r--tests/hlsl-intrinsic/system-values-draw-parameters.slang34
11 files changed, 104 insertions, 3 deletions
diff --git a/docs/user-guide/a2-01-spirv-target-specific.md b/docs/user-guide/a2-01-spirv-target-specific.md
index 4faf3bde5..d57cc8ce5 100644
--- a/docs/user-guide/a2-01-spirv-target-specific.md
+++ b/docs/user-guide/a2-01-spirv-target-specific.md
@@ -60,8 +60,8 @@ The system-value semantics are translated to the following SPIR-V code.
| `SV_RenderTargetArrayIndex` | `BuiltIn Layer` |
| `SV_SampleIndex` | `BuiltIn SampleId` |
| `SV_ShadingRate` | `BuiltIn PrimitiveShadingRateKHR` |
-| `SV_StartVertexLocation` | `*Not supported* |
-| `SV_StartInstanceLocation` | `*Not supported* |
+| `SV_StartVertexLocation` | `BuiltIn BaseVertex` |
+| `SV_StartInstanceLocation` | `BuiltIn BaseInstance` |
| `SV_StencilRef` | `BuiltIn FragStencilRefEXT` |
| `SV_Target<N>` | `Location` |
| `SV_TessFactor` | `BuiltIn TessLevelOuter` |
diff --git a/docs/user-guide/a2-02-metal-target-specific.md b/docs/user-guide/a2-02-metal-target-specific.md
index f63b985b5..48c6b4587 100644
--- a/docs/user-guide/a2-02-metal-target-specific.md
+++ b/docs/user-guide/a2-02-metal-target-specific.md
@@ -40,6 +40,8 @@ The system-value semantics are translated to the following Metal attributes:
| `SV_Target<N>` | `[[color(N)]]` |
| `SV_VertexID` | `[[vertex_id]]` |
| `SV_ViewportArrayIndex` | `[[viewport_array_index]]` |
+| `SV_StartVertexLocation` | `[[base_vertex]]` |
+| `SV_StartInstanceLocation` | `[[base_instance]]` |
Custom semantics are mapped to user attributes:
@@ -293,4 +295,4 @@ Translates to:
```metal
constant int fc_a_0 [[function_constant(7)]];
constant int a_0 = is_function_constant_defined(fc_a_0) ? fc_a_0 : 2;
-``` \ No newline at end of file
+```
diff --git a/source/slang/glsl.meta.slang b/source/slang/glsl.meta.slang
index 361a956f2..7bcc92f7d 100644
--- a/source/slang/glsl.meta.slang
+++ b/source/slang/glsl.meta.slang
@@ -146,6 +146,8 @@ public in int gl_SampleID : SV_SampleIndex;
public in int gl_VertexIndex : SV_VertexID;
public in int gl_ViewIndex : SV_ViewID;
public in int gl_ViewportIndex : SV_ViewportArrayIndex;
+public in int gl_BaseVertex : SV_StartVertexLocation;
+public in int gl_BaseInstance : SV_StartInstanceLocation;
// Override operator* behavior to compute algebric product of matrices and vectors.
diff --git a/source/slang/slang-emit-spirv.cpp b/source/slang/slang-emit-spirv.cpp
index 63ebfcf6e..51ff9066c 100644
--- a/source/slang/slang-emit-spirv.cpp
+++ b/source/slang/slang-emit-spirv.cpp
@@ -5303,6 +5303,16 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
SpvBuiltInShadingRateKHR,
inst);
}
+ else if (semanticName == "sv_startvertexlocation")
+ {
+ requireSPIRVCapability(SpvCapabilityDrawParameters);
+ return getBuiltinGlobalVar(inst->getFullType(), SpvBuiltInBaseVertex, inst);
+ }
+ else if (semanticName == "sv_startinstancelocation")
+ {
+ requireSPIRVCapability(SpvCapabilityDrawParameters);
+ return getBuiltinGlobalVar(inst->getFullType(), SpvBuiltInBaseInstance, inst);
+ }
SLANG_UNREACHABLE("Unimplemented system value in spirv emit.");
}
}
diff --git a/source/slang/slang-ir-glsl-legalize.cpp b/source/slang/slang-ir-glsl-legalize.cpp
index 619682692..09bf245df 100644
--- a/source/slang/slang-ir-glsl-legalize.cpp
+++ b/source/slang/slang-ir-glsl-legalize.cpp
@@ -831,6 +831,22 @@ GLSLSystemValueInfo* getGLSLSystemValueInfo(
name = "gl_PrimitiveShadingRateEXT";
}
}
+ else if (semanticName == "sv_startvertexlocation")
+ {
+ context->requireGLSLVersion(ProfileVersion::GLSL_460);
+
+ // uint in hlsl, int in glsl (https://www.khronos.org/opengl/wiki/Built-in_Variable_(GLSL))
+ requiredType = builder->getBasicType(BaseType::Int);
+ name = "gl_BaseVertex";
+ }
+ else if (semanticName == "sv_startinstancelocation")
+ {
+ context->requireGLSLVersion(ProfileVersion::GLSL_460);
+
+ // uint in hlsl, int in glsl (https://www.khronos.org/opengl/wiki/Built-in_Variable_(GLSL))
+ requiredType = builder->getBasicType(BaseType::Int);
+ name = "gl_BaseInstance";
+ }
if (name)
{
diff --git a/source/slang/slang-ir-legalize-varying-params.h b/source/slang/slang-ir-legalize-varying-params.h
index c8952f604..7604cb245 100644
--- a/source/slang/slang-ir-legalize-varying-params.h
+++ b/source/slang/slang-ir-legalize-varying-params.h
@@ -57,6 +57,8 @@ IRInst* emitCalcGroupIndex(IRBuilder& builder, IRInst* groupThreadID, IRInst* gr
M(ViewID, SV_ViewID) \
M(ViewportArrayIndex, SV_ViewportArrayIndex) \
M(Target, SV_Target) \
+ M(StartVertexLocation, SV_StartVertexLocation) \
+ M(StartInstanceLocation, SV_StartInstanceLocation) \
/* end */
/// A known system-value semantic name that can be applied to a parameter
diff --git a/source/slang/slang-ir-metal-legalize.cpp b/source/slang/slang-ir-metal-legalize.cpp
index 65c547d82..835041a59 100644
--- a/source/slang/slang-ir-metal-legalize.cpp
+++ b/source/slang/slang-ir-metal-legalize.cpp
@@ -660,6 +660,18 @@ struct LegalizeMetalEntryPointContext
break;
}
+ case SystemValueSemanticName::StartVertexLocation:
+ {
+ result.metalSystemValueName = toSlice("base_vertex");
+ result.permittedTypes.add(builder.getBasicType(BaseType::UInt));
+ break;
+ }
+ case SystemValueSemanticName::StartInstanceLocation:
+ {
+ result.metalSystemValueName = toSlice("base_instance");
+ result.permittedTypes.add(builder.getBasicType(BaseType::UInt));
+ break;
+ }
default:
m_sink->diagnose(
parentVar,
diff --git a/source/slang/slang-ir-wgsl-legalize.cpp b/source/slang/slang-ir-wgsl-legalize.cpp
index afdf412b1..907c2b8ba 100644
--- a/source/slang/slang-ir-wgsl-legalize.cpp
+++ b/source/slang/slang-ir-wgsl-legalize.cpp
@@ -359,6 +359,8 @@ struct LegalizeWGSLEntryPointContext
case SystemValueSemanticName::ViewID:
case SystemValueSemanticName::ViewportArrayIndex:
+ case SystemValueSemanticName::StartVertexLocation:
+ case SystemValueSemanticName::StartInstanceLocation:
{
result.isUnsupported = true;
break;
diff --git a/source/slang/slang-language-server-completion.cpp b/source/slang/slang-language-server-completion.cpp
index 03c6b88ed..77ed33002 100644
--- a/source/slang/slang-language-server-completion.cpp
+++ b/source/slang/slang-language-server-completion.cpp
@@ -126,6 +126,8 @@ static const char* hlslSemanticNames[] = {
"SV_ViewID",
"SV_ViewportArrayIndex",
"SV_ShadingRate",
+ "SV_StartVertexLocation",
+ "SV_StartInstanceLocation",
};
bool isDeclKeyword(const UnownedStringSlice& slice)
diff --git a/tests/glsl-intrinsic/system-values-draw-parameters.slang b/tests/glsl-intrinsic/system-values-draw-parameters.slang
new file mode 100644
index 000000000..a58305c13
--- /dev/null
+++ b/tests/glsl-intrinsic/system-values-draw-parameters.slang
@@ -0,0 +1,19 @@
+//TEST:SIMPLE(filecheck=CHECK_SPIRV): -entry main -stage vertex -target spirv
+//TEST:SIMPLE(filecheck=CHECK_GLSL): -entry main -stage vertex -target glsl
+//TEST:SIMPLE(filecheck=CHECK_HLSL): -entry main -stage vertex -target hlsl
+//TEST:SIMPLE(filecheck=CHECK_METAL): -entry main -stage vertex -target metal
+
+#version 460
+
+void main()
+{
+ float x = float(gl_VertexIndex + gl_BaseVertex) * 0.1f;
+ float y = float(gl_InstanceIndex + gl_BaseInstance) * 0.2f;
+ gl_Position = vec4(x, y, 0.0f, 1.0f); // Output 2D position with z=0 and w=1.
+
+ // CHECK_SPIRV: BuiltIn BaseInstance
+ // CHECK_GLSL: gl_BaseInstance
+ // CHECK_HLSL: SV_StartInstanceLocation
+ // CHECK_METAL: base_instance
+}
+
diff --git a/tests/hlsl-intrinsic/system-values-draw-parameters.slang b/tests/hlsl-intrinsic/system-values-draw-parameters.slang
new file mode 100644
index 000000000..ea84ac116
--- /dev/null
+++ b/tests/hlsl-intrinsic/system-values-draw-parameters.slang
@@ -0,0 +1,34 @@
+//TEST:SIMPLE(filecheck=CHECK_SPIRV): -entry main -stage vertex -target spirv
+//TEST:SIMPLE(filecheck=CHECK_GLSL): -entry main -stage vertex -target glsl
+//TEST:SIMPLE(filecheck=CHECK_HLSL): -entry main -stage vertex -target hlsl
+//TEST:SIMPLE(filecheck=CHECK_METAL): -entry main -stage vertex -target metal
+
+struct VSInput
+{
+ uint vertexID : SV_VertexID;
+ uint instanceID : SV_InstanceID;
+};
+
+struct VSOutput
+{
+ float4 position : SV_POSITION;
+};
+
+VSOutput main(VSInput input,
+ uint startVertexLocation : SV_StartVertexLocation,
+ uint startInstanceLocation : SV_StartInstanceLocation)
+{
+ VSOutput output;
+
+ float x = (float)(input.vertexID + startVertexLocation) * 0.1f;
+ float y = (float)(input.instanceID + startInstanceLocation) * 0.2f;
+ output.position = float4(x, y, 0.0f, 1.0f);
+
+ // CHECK_SPIRV: BuiltIn BaseInstance
+ // CHECK_GLSL: gl_BaseInstance
+ // CHECK_HLSL: SV_StartInstanceLocation
+ // CHECK_METAL: base_instance
+
+ return output;
+}
+