diff options
| author | ArielG-NV <159081215+ArielG-NV@users.noreply.github.com> | 2025-06-28 21:15:48 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-29 04:15:48 +0000 |
| commit | 67af8c718ce5e3f95a25e5188840f63b41a33ecc (patch) | |
| tree | bf5d1c7309e136223f1c62a5ec7d215274445b0c | |
| parent | 7349dc5cff49cf22c82eb912813e47f30cd7a757 (diff) | |
Support the GLSL/SPIR-V Built-in variable `DeviceIndex` (#7552)
* Support DeviceIndex
* format code
* regenerate command line reference
---------
Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
| -rw-r--r-- | docs/command-line-slangc-reference.md | 3 | ||||
| -rw-r--r-- | docs/user-guide/a2-01-spirv-target-specific.md | 1 | ||||
| -rw-r--r-- | docs/user-guide/a3-02-reference-capability-atoms.md | 9 | ||||
| -rw-r--r-- | source/slang/glsl.meta.slang | 13 | ||||
| -rw-r--r-- | source/slang/slang-capabilities.capdef | 13 | ||||
| -rw-r--r-- | source/slang/slang-emit-spirv.cpp | 6 | ||||
| -rw-r--r-- | source/slang/slang-ir-glsl-legalize.cpp | 6 | ||||
| -rw-r--r-- | source/slang/slang-ir-legalize-varying-params.h | 1 | ||||
| -rw-r--r-- | source/slang/slang-language-server-completion.cpp | 1 | ||||
| -rw-r--r-- | tests/spirv/device-index-glsl-syntax.slang | 16 | ||||
| -rw-r--r-- | tests/spirv/device-index-hlsl-syntax.slang | 17 |
11 files changed, 85 insertions, 1 deletions
diff --git a/docs/command-line-slangc-reference.md b/docs/command-line-slangc-reference.md index 1c7645f75..624663cb2 100644 --- a/docs/command-line-slangc-reference.md +++ b/docs/command-line-slangc-reference.md @@ -1133,6 +1133,7 @@ A capability describes an optional feature that a target may or may not support. * `SPV_KHR_quad_control` : enables the SPV_KHR_quad_control extension * `SPV_KHR_fragment_shader_barycentric` : enables the SPV_KHR_fragment_shader_barycentric extension * `SPV_KHR_non_semantic_info` : enables the SPV_KHR_non_semantic_info extension +* `SPV_KHR_device_group` : enables the SPV_KHR_device_group extension * `SPV_KHR_ray_tracing` : enables the SPV_KHR_ray_tracing extension * `SPV_KHR_ray_query` : enables the SPV_KHR_ray_query extension * `SPV_KHR_ray_tracing_position_fetch` : enables the SPV_KHR_ray_tracing_position_fetch extension @@ -1152,6 +1153,7 @@ A capability describes an optional feature that a target may or may not support. * `SPV_KHR_cooperative_matrix` : enables the SPV_KHR_cooperative_matrix extension * `SPV_NV_tensor_addressing` : enables the SPV_NV_tensor_addressing extension * `SPV_NV_cooperative_matrix2` : enables the SPV_NV_cooperative_matrix2 extension +* `spvDeviceGroup` * `spvAtomicFloat32AddEXT` * `spvAtomicFloat16AddEXT` * `spvAtomicFloat64AddEXT` @@ -1254,6 +1256,7 @@ A capability describes an optional feature that a target may or may not support. * `GL_EXT_demote_to_helper_invocation` : enables the GL_EXT_demote_to_helper_invocation extension * `GL_EXT_maximal_reconvergence` : enables the GL_EXT_maximal_reconvergence extension * `GL_EXT_shader_quad_control` : enables the GL_EXT_shader_quad_control extension +* `GL_EXT_device_group` : enables the GL_EXT_device_group extension * `GL_EXT_fragment_shader_barycentric` : enables the GL_EXT_fragment_shader_barycentric extension * `GL_EXT_mesh_shader` : enables the GL_EXT_mesh_shader extension * `GL_EXT_nonuniform_qualifier` : enables the GL_EXT_nonuniform_qualifier extension diff --git a/docs/user-guide/a2-01-spirv-target-specific.md b/docs/user-guide/a2-01-spirv-target-specific.md index dee19cd28..7686f7312 100644 --- a/docs/user-guide/a2-01-spirv-target-specific.md +++ b/docs/user-guide/a2-01-spirv-target-specific.md @@ -66,6 +66,7 @@ The system-value semantics are translated to the following SPIR-V code. | `SV_DispatchThreadID` | `BuiltIn GlobalInvocationId` | | `SV_DomainLocation` | `BuiltIn TessCoord` | | `SV_DrawIndex`<sup>*</sup> | `Builtin DrawIndex` | +| `SV_DeviceIndex` | `Builtin DeviceIndex` | | `SV_GSInstanceID` | `BuiltIn InvocationId` | | `SV_GroupID` | `BuiltIn WorkgroupId` | | `SV_GroupIndex` | `BuiltIn LocalInvocationIndex` | diff --git a/docs/user-guide/a3-02-reference-capability-atoms.md b/docs/user-guide/a3-02-reference-capability-atoms.md index 4ef207ea1..db7de8cb1 100644 --- a/docs/user-guide/a3-02-reference-capability-atoms.md +++ b/docs/user-guide/a3-02-reference-capability-atoms.md @@ -403,6 +403,9 @@ Extensions `SPV_KHR_non_semantic_info` > Represents the SPIR-V extension for non-semantic information. +`SPV_KHR_device_group` +> Represents the SPIR-V extension for device-group information. + `SPV_KHR_ray_tracing` > Represents the SPIR-V extension for ray tracing. @@ -465,6 +468,9 @@ Extensions `SPV_NV_cooperative_matrix2` > Represents the SPIR-V extension for SPV_NV_cooperative_matrix2. +`spvDeviceGroup` +> Represents the SPIR-V capability for DeviceGroup. + `spvAtomicFloat32AddEXT` > Represents the SPIR-V capability for atomic float 32 add operations. @@ -636,6 +642,9 @@ Extensions `GL_EXT_shader_quad_control` > Represents the GL_EXT_shader_quad_control extension. +`GL_EXT_device_group` +> Represents the GL_EXT_device_group extension. + `GL_EXT_fragment_shader_barycentric` > Represents the GL_EXT_fragment_shader_barycentric extension. diff --git a/source/slang/glsl.meta.slang b/source/slang/glsl.meta.slang index 9b0a28a31..b3323a327 100644 --- a/source/slang/glsl.meta.slang +++ b/source/slang/glsl.meta.slang @@ -127,8 +127,19 @@ public in uvec3 gl_WorkGroupID : SV_GroupID; public in uint gl_LocalInvocationIndex : SV_GroupIndex; public in uvec3 gl_LocalInvocationID : SV_GroupThreadID; -public property uint3 gl_NumWorkGroups { +internal in int _gl_DeviceIndex : SV_DeviceIndex; +public property int gl_DeviceIndex +{ + [__unsafeForceInlineEarly] + [require(glsl_spirv, GL_EXT_device_group)] + get + { + return _gl_DeviceIndex; + } +} +public property uint3 gl_NumWorkGroups +{ [require(glsl_spirv, GLSL_430_SPIRV_1_0_compute)] get { diff --git a/source/slang/slang-capabilities.capdef b/source/slang/slang-capabilities.capdef index 312777340..941ad7ebf 100644 --- a/source/slang/slang-capabilities.capdef +++ b/source/slang/slang-capabilities.capdef @@ -511,6 +511,10 @@ def SPV_KHR_fragment_shader_barycentric : _spirv_1_0; /// [EXT] def SPV_KHR_non_semantic_info : _spirv_1_0; +/// Represents the SPIR-V extension for device-group information. +/// [EXT] +def SPV_KHR_device_group : _spirv_1_0; + /// Represents the SPIR-V extension for ray tracing. /// [EXT] def SPV_KHR_ray_tracing : _spirv_1_4; @@ -596,6 +600,10 @@ def SPV_NV_cooperative_matrix2 : SPV_NV_tensor_addressing + SPV_KHR_cooperative_ // SPIRV Capabilities. +/// Represents the SPIR-V capability for DeviceGroup. +/// [EXT] +def spvDeviceGroup : SPV_KHR_device_group; + /// Represents the SPIR-V capability for atomic float 32 add operations. /// [EXT] def spvAtomicFloat32AddEXT : SPV_EXT_shader_atomic_float_add; @@ -840,6 +848,7 @@ def _GL_EXT_texture_query_lod : glsl; def _GL_EXT_texture_shadow_lod : _GLSL_130; def _GL_EXT_maximal_reconvergence : _GLSL_140; def _GL_EXT_shader_quad_control : _GLSL_140; +def _GL_EXT_device_group : _GLSL_140; def _GL_ARB_derivative_control : _GLSL_400; def _GL_ARB_fragment_shader_interlock : _GLSL_450; @@ -905,6 +914,10 @@ alias GL_EXT_maximal_reconvergence = _GL_EXT_maximal_reconvergence | spvMaximalR /// [EXT] alias GL_EXT_shader_quad_control = _GL_EXT_shader_quad_control | spvQuadControlKHR; +/// Represents the GL_EXT_device_group extension. +/// [EXT] +alias GL_EXT_device_group = _GL_EXT_device_group | spvDeviceGroup; + /// Represents the GL_EXT_fragment_shader_barycentric extension. /// [EXT] alias GL_EXT_fragment_shader_barycentric = _GL_EXT_fragment_shader_barycentric | spvFragmentBarycentricKHR; diff --git a/source/slang/slang-emit-spirv.cpp b/source/slang/slang-emit-spirv.cpp index 82fb39d7d..0a3eb2bf1 100644 --- a/source/slang/slang-emit-spirv.cpp +++ b/source/slang/slang-emit-spirv.cpp @@ -6023,6 +6023,12 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex requireSPIRVCapability(SpvCapabilityDrawParameters); return getBuiltinGlobalVar(inst->getFullType(), SpvBuiltInDrawIndex, inst); } + else if (semanticName == "sv_deviceindex") + { + ensureExtensionDeclaration(UnownedStringSlice("SPV_KHR_device_group")); + requireSPIRVCapability(SpvCapabilityDeviceGroup); + return getBuiltinGlobalVar(inst->getFullType(), SpvBuiltInDeviceIndex, inst); + } else if (semanticName == "sv_primitiveid") { auto entryPoints = m_referencingEntryPoints.tryGetValue(inst); diff --git a/source/slang/slang-ir-glsl-legalize.cpp b/source/slang/slang-ir-glsl-legalize.cpp index 3cf547c17..1a34a9877 100644 --- a/source/slang/slang-ir-glsl-legalize.cpp +++ b/source/slang/slang-ir-glsl-legalize.cpp @@ -666,6 +666,12 @@ GLSLSystemValueInfo* getGLSLSystemValueInfo( name = "gl_DrawID"; requiredType = builder->getBasicType(BaseType::Int); } + else if (semanticName == "sv_deviceindex") + { + name = "gl_DeviceIndex"; + requiredType = builder->getBasicType(BaseType::Int); + context->requireGLSLExtension(UnownedStringSlice::fromLiteral("GL_EXT_device_group")); + } else if (semanticName == "sv_primitiveid") { // uint in hlsl, int in glsl diff --git a/source/slang/slang-ir-legalize-varying-params.h b/source/slang/slang-ir-legalize-varying-params.h index 233acaf94..fcb4b8b81 100644 --- a/source/slang/slang-ir-legalize-varying-params.h +++ b/source/slang/slang-ir-legalize-varying-params.h @@ -59,6 +59,7 @@ void depointerizeInputParams(IRFunc* entryPoint); M(PointCoord, SV_PointCoord) \ M(PrimitiveID, SV_PrimitiveID) \ M(DrawIndex, SV_DrawIndex) \ + M(DeviceIndex, SV_DeviceIndex) \ M(RenderTargetArrayIndex, SV_RenderTargetArrayIndex) \ M(SampleIndex, SV_SampleIndex) \ M(StencilRef, SV_StencilRef) \ diff --git a/source/slang/slang-language-server-completion.cpp b/source/slang/slang-language-server-completion.cpp index 59aa3e4d8..9a88fbaa7 100644 --- a/source/slang/slang-language-server-completion.cpp +++ b/source/slang/slang-language-server-completion.cpp @@ -121,6 +121,7 @@ static const char* hlslSemanticNames[] = { "SV_PointCoord", "SV_PrimitiveID", "SV_DrawIndex", + "SV_DeviceIndex", "SV_RenderTargetArrayIndex", "SV_SampleIndex", "SV_StencilRef", diff --git a/tests/spirv/device-index-glsl-syntax.slang b/tests/spirv/device-index-glsl-syntax.slang new file mode 100644 index 000000000..3a374f038 --- /dev/null +++ b/tests/spirv/device-index-glsl-syntax.slang @@ -0,0 +1,16 @@ +//TEST:SIMPLE(filecheck=SPIRV): -target spirv -entry main -stage fragment -allow-glsl +//TEST:SIMPLE(filecheck=GLSL): -target glsl -entry main -stage fragment -allow-glsl + +// SPIRV: OpDecorate %{{.*}} BuiltIn DeviceIndex +// GLSL: gl_DeviceIndex + +struct PSIn +{ + float3 color; +} + +[shader("fragment")] +float4 main(PSIn pin) +{ + return float4(pin.color, (float)gl_DeviceIndex); +} diff --git a/tests/spirv/device-index-hlsl-syntax.slang b/tests/spirv/device-index-hlsl-syntax.slang new file mode 100644 index 000000000..756f1bf80 --- /dev/null +++ b/tests/spirv/device-index-hlsl-syntax.slang @@ -0,0 +1,17 @@ +//TEST:SIMPLE(filecheck=SPIRV): -target spirv -entry main +//TEST:SIMPLE(filecheck=GLSL): -target glsl -entry main + +// SPIRV: OpDecorate %{{.*}} BuiltIn DeviceIndex +// GLSL: gl_DeviceIndex + +struct PSIn +{ + float3 color; + int deviceIndex : SV_DeviceIndex; +} + +[shader("fragment")] +float4 main(PSIn pin) +{ + return float4(pin.color, (float)pin.deviceIndex); +} |
