diff options
| author | Darren Wihandi <65404740+fairywreath@users.noreply.github.com> | 2025-04-22 14:04:56 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-22 20:04:56 +0000 |
| commit | ed5940a629ae05e9571bfe355d22f0728347dcb4 (patch) | |
| tree | 90a36c6543f0ee3748b80112a478897b027dddab /source/slang/hlsl.meta.slang | |
| parent | d5220b327632a8aeeb9a89494bb37bd82fec30cb (diff) | |
Implement shader subgroup rotate intrinsics (#6878)
* Initial implementation for SPIRV, GLSL and Metal
* test add bool test
* Fix and improve subgroup rotate tests
* Add proper GLSL extensions and proper Metal type checking
* Clean up tests and add diagnostics test for subgroup type for Metal
* Update wave-intrinsics docs
Diffstat (limited to 'source/slang/hlsl.meta.slang')
| -rw-r--r-- | source/slang/hlsl.meta.slang | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang index c8a2c8c58..03321bfaf 100644 --- a/source/slang/hlsl.meta.slang +++ b/source/slang/hlsl.meta.slang @@ -16368,6 +16368,155 @@ bool IsHelperLane() } } +//@hidden: + +__generic<T : __BuiltinType> +[ForceInline] +[require(glsl)] +void __requireGLSLShaderSubgroupTypeExtension() +{ + // the following is a seperate function call, since else the `__requireTargetExtension` and associated __intrinsic_asm is ignored if the calling function also calls an __intrinsic_asm + if (__type_equals<T, half>() + || __type_equals<T, float16_t>() + ) __requireTargetExtension("GL_EXT_shader_subgroup_extended_types_float16"); + else if (__type_equals<T, uint8_t>() + || __type_equals<T, int8_t>() + ) __requireTargetExtension("GL_EXT_shader_subgroup_extended_types_int8"); + else if (__type_equals<T, uint16_t>() + || __type_equals<T, int16_t>() + ) __requireTargetExtension("GL_EXT_shader_subgroup_extended_types_int16"); + else if (__type_equals<T, uint64_t>() + || __type_equals<T, int64_t>() + ) __requireTargetExtension("GL_EXT_shader_subgroup_extended_types_int64"); + + __intrinsic_asm ""; +} + +__generic<T : __BuiltinType> +[ForceInline] +[require(metal)] +void __checkMetalShaderSubgroupType() +{ + // These builtin types are not supported for Metal's `simd` operations. + if (__type_equals<T, uint8_t>() + || __type_equals<T, int8_t>() + || __type_equals<T, uint64_t>() + || __type_equals<T, int64_t>() + || __isBool<T>() + ) + { + static_assert(false, "Unsupported type for subgroup operations in Metal. Valid types include scalars and vectors of uint/uint32_t, int/int32_t, uint16_t, int16_t, float, and half."); + } +} + +__generic<T : __BuiltinType> +void shader_subgroup_preamble() +{ + // checks needed for shader_subgroup functions; __requireTargetExtension does not work + // (does not add the ext specified correctly to the compile output; using extended type + // will result in error for using the type) + __target_switch + { + case glsl: + __requireGLSLShaderSubgroupTypeExtension<T>(); + case metal: + __checkMetalShaderSubgroupType<T>(); + default: + return; + } +} + +//@public: + +// +// Wave Rotate intrinsics. +// These are Slang specific intrinsics to rotate values within a subgroup. +// + +__generic<T : __BuiltinType> +__glsl_extension(GL_KHR_shader_subgroup_rotate) +[require(glsl_metal_spirv, subgroup_rotate)] +T WaveRotate(T value, uint delta) +{ + shader_subgroup_preamble<T>(); + __target_switch + { + case glsl: + __intrinsic_asm "subgroupRotate"; + case metal: + __intrinsic_asm "simd_shuffle_rotate_down"; + case spirv: + return spirv_asm + { + OpExtension "SPV_KHR_subgroup_rotate"; + OpCapability GroupNonUniformRotateKHR; + result:$$T = OpGroupNonUniformRotateKHR Subgroup $value $delta; + }; + } +} + +__generic<T : __BuiltinType, let N : int> +__glsl_extension(GL_KHR_shader_subgroup_rotate) +[require(glsl_metal_spirv, subgroup_rotate)] +vector<T, N> WaveRotate(vector<T, N> value, uint delta) +{ + shader_subgroup_preamble<T>(); + __target_switch + { + case glsl: + __intrinsic_asm "subgroupRotate"; + case metal: + __intrinsic_asm "simd_shuffle_rotate_down"; + case spirv: + return spirv_asm + { + OpExtension "SPV_KHR_subgroup_rotate"; + OpCapability GroupNonUniformRotateKHR; + result:$$vector<T,N> = OpGroupNonUniformRotateKHR Subgroup $value $delta; + }; + } +} + +__generic<T : __BuiltinType> +__glsl_extension(GL_KHR_shader_subgroup_rotate) +[require(glsl_spirv, subgroup_rotate)] +T WaveClusteredRotate(T value, uint delta, constexpr uint clusterSize) +{ + shader_subgroup_preamble<T>(); + __target_switch + { + case glsl: + __intrinsic_asm "subgroupClusteredRotate"; + case spirv: + return spirv_asm + { + OpExtension "SPV_KHR_subgroup_rotate"; + OpCapability GroupNonUniformRotateKHR; + result:$$T = OpGroupNonUniformRotateKHR Subgroup $value $delta $clusterSize; + }; + } +} + +__generic<T : __BuiltinType, let N : int> +__glsl_extension(GL_KHR_shader_subgroup_rotate) +[require(glsl_spirv, subgroup_rotate)] +vector<T, N> WaveClusteredRotate(vector<T, N> value, uint delta, constexpr uint clusterSize) +{ + shader_subgroup_preamble<T>(); + __target_switch + { + case glsl: + __intrinsic_asm "subgroupClusteredRotate"; + case spirv: + return spirv_asm + { + OpExtension "SPV_KHR_subgroup_rotate"; + OpCapability GroupNonUniformRotateKHR; + result:$$vector<T,N> = OpGroupNonUniformRotateKHR Subgroup $value $delta $clusterSize; + }; + } +} + // // Quad Control intrinsics // |
