yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
e23b5fa99
master
layout: user-guide permalink: /user-guide/wgsl-target-specific
WGSL-Specific Functionalities
This chapter provides information for WGSL (WebGPU Shading Language)-specific functionalities and behaviors.
System-Value semantics
The system-value semantics are translated to the following WGSL code.
| SV semantic name | WGSL code |
|---|---|
| SV_Barycentrics | Not supported |
| SV_ClipDistance<N> | Not supported |
| SV_CullDistance<N> | Not supported |
| SV_Coverage | @builtin(sample_mask) |
| SV_CullPrimitive | Not supported |
| SV_Depth | @builtin(frag_depth) |
| SV_DepthGreaterEqual | Not supported |
| SV_DepthLessEqual | Not supported |
| SV_DispatchThreadID | @builtin(global_invocation_id) |
| SV_DomainLocation | Not supported |
| SV_FragInvocationCount | Not supported |
| SV_FragSize | Not supported |
| SV_GSInstanceID | Not supported |
| SV_GroupID | @builtin(workgroup_id) |
| SV_GroupIndex | @builtin(local_invocation_index) |
| SV_GroupThreadID | @builtin(local_invocation_id) |
| SV_InnerCoverage | Not supported |
| SV_InsideTessFactor | Not supported |
| SV_InstanceID | @builtin(instance_index) |
| SV_IntersectionAttributes | Not supported |
| SV_IsFrontFace | @builtin(front_facing) |
| SV_OutputControlPointID | Not supported |
| SV_PointSize | Not supported |
| SV_Position | @builtin(position) |
| SV_PrimitiveID | Not supported |
| SV_RenderTargetArrayIndex | Not supported |
| SV_SampleIndex | @builtin(sample_index) |
| SV_ShadingRate | Not supported |
| SV_StartVertexLocation | Not supported |
| SV_StartInstanceLocation | Not supported |
| SV_StencilRef | Not supported |
| SV_Target<N> | Not supported |
| SV_TessFactor | Not supported |
| SV_VertexID | @builtin(vertex_index) |
| SV_ViewID | Not supported |
| SV_ViewportArrayIndex | Not supported |
| SV_VulkanInstanceID | @builtin(instance_index) |
| SV_VulkanSamplePosition | Not supported |
| SV_VulkanVertexID | @builtin(vertex_index) |
Supported HLSL features when targeting WGSL
The following table lists Slang's support for various HLSL feature sets, when targeting WGSL.
| Feature set | Supported |
|---|---|
| ray tracing | No |
| inline ray tracing | No |
| mesh shader | No |
| tessellation shader | No |
| geometry shader | No |
| wave intrinsics | No |
| barriers | Yes |
| atomics | Yes |
Supported atomic types
The following table shows what is supported when targeting WGSL:
| 32-bit integer | 64-bit integer | 32-bit float | 64-bit float | 16-bit float | |
|---|---|---|---|---|---|
| Supported? | Yes | No | No | No | No |
ConstantBuffer, (RW/RasterizerOrdered)StructuredBuffer, (RW/RasterizerOrdered)ByteAddressBuffer
ConstantBuffer translates to the uniform address space with read access mode in WGSL.
ByteAddressBuffer and RWByteAddressBuffer translate to array<u32> in the storage address space, with the read and read_write access modes in WGSL, respectively.
StructuredBuffer and RWStructuredBuffer with struct type T translate to array<T> in the storage address space, with with the read and read_write access modes in WGSL, respectively.
Interlocked operations
The InterlockedAdd, InterlockedAnd, etc... functions are not supported when targeting WGSL.
Instead, operations on Atomic<T> types should be used.
Entry Point Parameter Handling
Slang performs several transformations on entry point parameters when targeting WGSL:
- Struct parameters and returned structs are flattened to eliminate nested structures.
- System value semantics are translated to WGSL built-ins. (See the
@builtinattribute, and the table above.) - Parameters without semantics are given automatic location indices. (See the
@locationattribute.)
Parameter blocks
Each ParameterBlock is assigned its own bind group in WGSL.
Write-only Textures
Many image formats supported by WebGPU can only be accessed in compute shader as a write-only image.
Use WTexture2D type (similar to RWTexture2D) to write to an image when possible.
The write-only texture types are also supported when targeting HLSL/GLSL/SPIR-V/Metal and CUDA.
Pointers
out and inout parameters in Slang are translated to pointer-typed parameters in WGSL.
At callsites, a pointer value is formed and passed as argument using the & operator in WGSL.
Since WGSL cannot form pointers to fields of structs (or fields of fields of structs, etc...), the described transformation cannot be done in a direct way when a function argument expression is an "access chain" like myStruct.myField or myStruct.myStructField.someField.
In those cases, the argument is copied to a local variable, the address of the local variable is passed to the function, and then the local
variable is written back to the struct field after the function call.
Address Space Assignment
WGSL requires explicit address space qualifiers. Slang automatically assigns appropriate address spaces:
| Variable Type | WGSL Address Space |
|---|---|
| Local Variables | function |
| Global Variables | private |
| Uniform Buffers | uniform |
| RW/Structured Buffers | storage |
| Group Shared | workgroup |
| Parameter Blocks | uniform |
Matrix type translation
A m-row-by-n-column matrix in Slang, represented as floatmxn or matrix<T, m, n>, is translated to mat[n]x[m] in WGSL, i.e. a matrix with n columns and m rows.
The rationale for this inversion of terminology is the same as the rationale for SPIR-V.
Since the WGSL matrix multiplication convention is the normal one, where inner products of rows of the matrix on the left are taken with columns of the matrix on the right, the order of matrix products is also reversed in WGSL. This is relying on the fact that the transpose of a matrix product equals the product of the transposed matrix operands in reverse order.
Explicit Parameter Binding
The [vk::binding(index,set)] attribute is respected when emitting WGSL code, and will translate to @binding(index) @group(set) in WGSL.
If the [vk::binding()] attribute is not specified but a :register() semantic is present, Slang will derive the binding from the register semantic the same way as the SPIR-V and GLSL backends.
The [vk::location(N)] attributes on stage input/output parameters are respected.
Specialization Constants
Specialization constants declared with the [SpecializationConstant] or [vk::constant_id] attribute will be translated into a global override declaration when generating WGSL source.
For example:
[ vk ::constant_id ( 7 )] const int a = 2 ;
Translates to:
@id(7) override a : i32 = 2;
1--- 2layout : user-guide 3permalink : /user-guide/wgsl-target-specific 4--- 5 6WGSL-Specific Functionalities 7============================= 8 9This chapter provides information for WGSL (WebGPU Shading Language)-specific functionalities and behaviors. 10 11 12System-Value semantics 13---------------------- 14 15The system-value semantics are translated to the following WGSL code. 16 17| SV semantic name | WGSL code | 18|--|--| 19| SV_Barycentrics | *Not supported* | 20| SV_ClipDistance<N> | *Not supported* | 21| SV_CullDistance<N> | *Not supported* | 22| SV_Coverage | `@builtin(sample_mask)` | 23| SV_CullPrimitive | *Not supported* | 24| SV_Depth | `@builtin(frag_depth)` | 25| SV_DepthGreaterEqual | *Not supported* | 26| SV_DepthLessEqual | *Not supported* | 27| SV_DispatchThreadID | `@builtin(global_invocation_id)` | 28| SV_DomainLocation | *Not supported* | 29| SV_FragInvocationCount | *Not supported* | 30| SV_FragSize | *Not supported* | 31| SV_GSInstanceID | *Not supported* | 32| SV_GroupID | `@builtin(workgroup_id)` | 33| SV_GroupIndex | `@builtin(local_invocation_index)` | 34| SV_GroupThreadID | `@builtin(local_invocation_id)` | 35| SV_InnerCoverage | *Not supported* | 36| SV_InsideTessFactor | *Not supported* | 37| SV_InstanceID | `@builtin(instance_index)` | 38| SV_IntersectionAttributes | *Not supported* | 39| SV_IsFrontFace | `@builtin(front_facing)` | 40| SV_OutputControlPointID | *Not supported* | 41| SV_PointSize | *Not supported* | 42| SV_Position | `@builtin(position)` | 43| SV_PrimitiveID | *Not supported* | 44| SV_RenderTargetArrayIndex | *Not supported* | 45| SV_SampleIndex | `@builtin(sample_index)` | 46| SV_ShadingRate | *Not supported* | 47| SV_StartVertexLocation | *Not supported* | 48| SV_StartInstanceLocation | *Not supported* | 49| SV_StencilRef | *Not supported* | 50| SV_Target<N> | *Not supported* | 51| SV_TessFactor | *Not supported* | 52| SV_VertexID | `@builtin(vertex_index)` | 53| SV_ViewID | *Not supported* | 54| SV_ViewportArrayIndex | *Not supported* | 55| SV_VulkanInstanceID | `@builtin(instance_index)` | 56| SV_VulkanSamplePosition | *Not supported* | 57| SV_VulkanVertexID | `@builtin(vertex_index)` | 58 59 60Supported HLSL features when targeting WGSL 61------------------------------------------- 62 63The following table lists Slang's support for various HLSL feature sets, when targeting WGSL. 64 65| Feature set | Supported | 66| -- | -- | 67| ray tracing | No | 68| inline ray tracing | No | 69| mesh shader | No | 70| tessellation shader | No | 71| geometry shader | No | 72| wave intrinsics | No | 73| barriers | Yes | 74| atomics | Yes | 75 76 77Supported atomic types 78---------------------- 79 80The following table shows what is supported when targeting WGSL: 81 82| | 32-bit integer | 64-bit integer | 32-bit float | 64-bit float | 16-bit float | 83|--------------|-----------------|-----------------|-----------------------|------------------|------------------| 84| Supported? | Yes | No | No | No | No | 85 86 87ConstantBuffer, (RW/RasterizerOrdered)StructuredBuffer, (RW/RasterizerOrdered)ByteAddressBuffer 88----------------------------------------------------------------------------------------------- 89 90ConstantBuffer translates to the `uniform` address space with `read` access mode in WGSL. 91ByteAddressBuffer and RWByteAddressBuffer translate to `array<u32>` in the `storage` address space, with the `read` and `read_write` access modes in WGSL, respectively. 92StructuredBuffer and RWStructuredBuffer with struct type T translate to `array<T>` in the `storage` address space, with with the `read` and `read_write` access modes in WGSL, respectively. 93 94Interlocked operations 95---------------------- 96 97The InterlockedAdd, InterlockedAnd, etc... functions are not supported when targeting WGSL. 98Instead, operations on [`Atomic<T>`](https://shader-slang.com/stdlib-reference/types/atomic-0/index) types should be used. 99 100 101Entry Point Parameter Handling 102------------------------------ 103 104Slang performs several transformations on entry point parameters when targeting WGSL: 105 106- Struct parameters and returned structs are flattened to eliminate nested structures. 107- System value semantics are translated to WGSL built-ins. (See the `@builtin` attribute, and the table above.) 108- Parameters without semantics are given automatic location indices. (See the `@location` attribute.) 109 110 111Parameter blocks 112---------------- 113 114Each `ParameterBlock` is assigned its own bind group in WGSL. 115 116 117Write-only Textures 118--------------- 119 120Many image formats supported by WebGPU can only be accessed in compute shader as a write-only image. 121Use `WTexture2D` type (similar to `RWTexture2D`) to write to an image when possible. 122The write-only texture types are also supported when targeting HLSL/GLSL/SPIR-V/Metal and CUDA. 123 124 125Pointers 126-------- 127 128`out` and `inout` parameters in Slang are translated to pointer-typed parameters in WGSL. 129At callsites, a pointer value is formed and passed as argument using the `&` operator in WGSL. 130 131Since WGSL cannot form pointers to fields of structs (or fields of fields of structs, etc...), the described transformation cannot be done in a direct way when a function argument expression is an "access chain" like `myStruct.myField` or `myStruct.myStructField.someField`. 132In those cases, the argument is copied to a local variable, the address of the local variable is passed to the function, and then the local 133variable is written back to the struct field after the function call. 134 135Address Space Assignment 136------------------------ 137 138WGSL requires explicit address space qualifiers. Slang automatically assigns appropriate address spaces: 139 140| Variable Type | WGSL Address Space | 141| --------------------- | ------------------- | 142| Local Variables | `function` | 143| Global Variables | `private` | 144| Uniform Buffers | `uniform` | 145| RW/Structured Buffers | `storage` | 146| Group Shared | `workgroup` | 147| Parameter Blocks | `uniform` | 148 149 150Matrix type translation 151----------------------- 152 153A m-row-by-n-column matrix in Slang, represented as float`m`x`n` or matrix<T, m, n>, is translated to `mat[n]x[m]` in WGSL, i.e. a matrix with `n` columns and `m` rows. 154The rationale for this inversion of terminology is the same as [the rationale for SPIR-V](a2-01-spirv-target-specific.md#matrix-type-translation). 155Since the WGSL matrix multiplication convention is the normal one, where inner products of rows of the matrix on the left are taken with columns of the matrix on the right, the order of matrix products is also reversed in WGSL. This is relying on the fact that the transpose of a matrix product equals the product of the transposed matrix operands in reverse order. 156 157## Explicit Parameter Binding 158 159The `[vk::binding(index,set)]` attribute is respected when emitting WGSL code, and will translate to `@binding(index) @group(set)` in WGSL. 160 161If the `[vk::binding()]` attribute is not specified but a `:register()` semantic is present, Slang will derive the binding from the `register` semantic the same way as the SPIR-V and GLSL backends. 162 163The `[vk::location(N)]` attributes on stage input/output parameters are respected. 164 165## Specialization Constants 166 167Specialization constants declared with the `[SpecializationConstant]` or `[vk::constant_id]` attribute will be translated into a global `override` declaration when generating WGSL source. 168For example: 169 170``` csharp 171[vk::constant_id(7)] 172const int a = 2; 173``` 174 175Translates to: 176 177``` wgsl 178@id(7) override a : i32 = 2; 179```