blob: e7f75769d9df74dd8cdbff52c644855c581e145d [file] [log] [blame] [view]
dan sinclaird2fa57d2020-10-29 03:39:34 +00001# Translations
2
3This document attempts to document how WGSL translates into the various backends
4for the cases where the translation is not a direct mapping.
5
6# Access Control
7
8## HLSL
9 * ReadOnly -> `ByteAddressBuffer`
10 * ReadWrite -> `RWByteAddressBuffer`
11
12## MSL
13 * ReadOnly -> `const`
14
15## SPIR-V
16There are two ways this can be achieved in SPIR-V. Either the variable can be
17decorated with `NonWritable` or each member of the struct can be decorated with
18`NonWritable`. We chose to go the struct member route.
19 * The read-only becomes part of the type in this case. Otherwise, you are
20 treating the readonly type information as part of the variable which is
21 confusing.
22 * Treating the readonly as part of the variable means we should be
23 deduplicating the types behind the access control, which causes confusing
24 with the type_names and various tracking systems within Tint.
25
26
27# Builtin Decorations
dan sinclair62bbc6f2020-10-29 12:44:32 +000028| Name | SPIR-V | MSL | HLSL |
29|------|--------|-----|------|
30| position | SpvBuiltInPosition |position | SV_Position |
dan sinclaird7335fa2021-01-18 15:51:13 +000031| vertex_index | SpvBuiltInVertexIndex |vertex_id | SV_VertexID |
32| instance_index | SpvBuiltInInstanceIndex | instance_id| SV_InstanceID |
dan sinclair62bbc6f2020-10-29 12:44:32 +000033| front_facing | SpvBuiltInFrontFacing | front_facing | SV_IsFrontFacing |
34| frag_coord | SpvBuiltInFragCoord | position | SV_Position |
35| frag_depth | SpvBuiltInFragDepth | depth(any) | SV_Depth |
36| local_invocation_id | SpvBuiltInLocalInvocationId | thread_position_in_threadgroup | SV_GroupThreadID |
dan sinclaird7335fa2021-01-18 15:51:13 +000037| local_invocation_index | SpvBuiltInLocalInvocationIndex | thread_index_in_threadgroup | SV_GroupIndex |
dan sinclair62bbc6f2020-10-29 12:44:32 +000038| global_invocation_id | SpvBuiltInGlobalInvocationId | thread_position_in_grid | SV_DispatchThreadID |
dan sinclaird2fa57d2020-10-29 03:39:34 +000039
40
41# Builtins Methods
dan sinclair62bbc6f2020-10-29 12:44:32 +000042| Name | SPIR-V | MSL | HLSL |
43| ------|--------|-----|------ |
44| abs | GLSLstd450FAbs or GLSLstd450SAbs| fabs or abs | abs |
45| acos | GLSLstd450Acos | acos | acos |
46| all | SpvOpAll | all | all |
47| any | SpvOpAny | any | any |
48| arrayLength | SpvOpArrayLength | | |
49| asin | GLSLstd450Asin | asin | asin |
50| atan | GLSLstd450Atan | atan | atan |
51| atan2 | GLSLstd450Atan2| atan2 | atan2 |
52| ceil | GLSLstd450Ceil| ceil | ceil |
53| clamp | GLSLstd450NClamp or GLSLstd450UClamp or GLSLstd450SClamp| clamp | clamp |
54| cos | GLSLstd450Cos | cos | cos |
55| cosh | GLSLstd450Cosh | cosh | cosh |
56| countOneBits | SpvOpBitCount | popcount | countbits |
57| cross | GLSLstd450Cross | cross | cross |
58| determinant | GLSLstd450Determinant | determinant | determinant |
59| distance | GLSLstd450Distance | distance | distance |
60| dot | SpOpDot | dot | dot |
61| dpdx | SpvOpDPdx | dpdx | ddx |
62| dpdxCoarse | SpvOpDPdxCoarse | dpdx | ddx_coarse |
63| dpdxFine | SpvOpDPdxFine | dpdx | ddx_fine |
64| dpdy | SpvOpDPdy | dpdy | ddy |
65| dpdyCoarse | SpvOpDPdyCoarse | dpdy | ddy_coarse |
66| dpdyFine | SpvOpDPdyFine | dpdy | ddy_fine |
67| exp | GLSLstd450Exp | exp | exp |
68| exp2 | GLSLstd450Exp2 | exp2 | exp2 |
69| faceForward | GLSLstd450FaceForward | faceforward | faceforward |
70| floor | GLSLstd450Floor | floor | floor |
71| fma | GLSLstd450Fma | fma | fma |
72| fract | GLSLstd450Fract | fract | frac |
73| frexp | GLSLstd450Frexp | | |
74| fwidth | SpvOpFwidth | fwidth | fwidth |
75| fwidthCoarse | SpvOpFwidthCoarse | fwidth | fwidth |
76| fwidthFine | SpvOpFwidthFine | fwidth | fwidth |
77| inverseSqrt | GLSLstd450InverseSqrt | rsqrt | rsqrt |
dan sinclair62bbc6f2020-10-29 12:44:32 +000078| ldexp | GLSLstd450Ldexp | | |
79| length | GLSLstd450Length | length | length |
80| log | GLSLstd450Log | log | log |
81| log2 | GLSLstd450Log2 | log2 | log2 |
82| max | GLSLstd450NMax or GLSLstd450SMax or GLSLstd450UMax | fmax or max | max |
83| min | GLSLstd450NMin or GLSLstd450SMin or GLSLstd450UMin | fmin or min | min |
84| mix | GLSLstd450FMix | mix | mix |
85| modf | GLSLstd450Modf | | |
86| normalize | GLSLstd450Normalize | normalize | normalize |
dan sinclair62bbc6f2020-10-29 12:44:32 +000087| pow | GLSLstd450Pow | pow | pow |
88| reflect | GLSLstd450Reflect | reflect | reflect |
89| reverseBits | SpvOpBitReverse | reverse_bits | reversebits |
90| round | GLSLstd450Round | round | round |
91| select | SpvOpSelect | select | |
92| sign | GLSLstd450FSign | sign | sign |
93| sin | GLSLstd450Sin | sin | sin |
94| sinh | GLSLstd450Sinh | sinh | sinh |
James Price03849322022-04-14 19:49:10 +000095| smoothstep | GLSLstd450SmoothStep | smoothstep | smoothstep |
dan sinclair62bbc6f2020-10-29 12:44:32 +000096| sqrt | GLSLstd450Sqrt | sqrt | sqrt |
97| step | GLSLstd450Step | step | step |
98| tan | GLSLstd450Tan | tan | tan |
99| tanh | GLSLstd450Tanh | tanh | tanh |
100| trunc | GLSLstd450Trunc | trunc | trunc |
dan sinclaird2fa57d2020-10-29 03:39:34 +0000101
dan sinclair2e6dc602020-11-10 22:10:56 +0000102# Types
103## Sampler Types
104| WGSL | SPIR-V | MSL | HLSL |
105|------|--------|-----|------|
dan sinclairf7e152a2020-11-11 00:58:19 +0000106| sampler | OpTypeSampler | sampler | SamplerState |
107| sampler_comparison | OpTypeSampler | sampler | SamplerComparisonState |
dan sinclair2e6dc602020-11-10 22:10:56 +0000108
109## Texture Types
110| WGSL | SPIR-V | MSL | HLSL |
111|------|--------|-----|------|
dan sinclair6526bd42020-11-11 01:49:28 +0000112| texture_1d<type> | OpTypeImage 1D Sampled=1 | texture1d<type, access::sample> | Texture1D |
dan sinclair6526bd42020-11-11 01:49:28 +0000113| texture_2d<type> | OpTypeImage 2D Sampled=1 | texture2d<type, access::sample> | Texture2D |
114| texture_2d_array<type> | OpTypeImage 2D Arrayed=1 Sampled=1 | texture2d_array<type, access::sample> | Texture2DArray |
115| texture_3d<type> | OpTypeImage 3D Sampled=1 | texture3d<type, access::sample> | Texture3D |
116| texture_cube<type> | OpTypeImage Cube Sampled=1 | texturecube<type, access::sample> | TextureCube |
117| texture_cube_array<type> | OpTypeImage Cube Arrayed=1 Sampled=1 | texturecube_array<type, access::sample> | TextureCubeArray |
dan sinclair2e6dc602020-11-10 22:10:56 +0000118| | | |
dan sinclair6526bd42020-11-11 01:49:28 +0000119| texture_multisampled_2d<type> | OpTypeImage 2D MS=1 Sampled=1 | texture2d_ms<type, access::sample> | Texture2D |
dan sinclair2e6dc602020-11-10 22:10:56 +0000120| | | |
Jiawei Shao9d27aaa2022-04-08 08:33:28 +0000121| texture_depth_2d | OpTypeImage 2D Sampled=1 | depth2d<float, access::sample>| Texture2D |
122| texture_depth_2d_array | OpTypeImage 2D Arrayed=1 Sampled=1 | depth2d_array<float, access::sample> | Texture2DArray |
123| texture_depth_cube | OpTypeImage Cube Sampled=1 | depthcube<float, access::sample> | TextureCube |
124| texture_depth_cube_array | OpTypeImage Cube Arrayed=1 Sampled=1 | depthcube_array<float, access::sample> | TextureCubeArray |
125| texture_depth_multisampled_2d | OpTypeImage 2D MS=1 Sampled=1 | depth2d<float, access::sample>| Texture2DMSArray |
dan sinclair2e6dc602020-11-10 22:10:56 +0000126| | | |
dan sinclair904c5b92021-01-14 08:34:46 +0000127| texture_storage_1d<image_storage_type> | OpTypeImage 1D Sampled=2| texture1d<type, access::read> | RWTexture1D |
dan sinclair904c5b92021-01-14 08:34:46 +0000128| texture_storage_2d<image_storage_type> | OpTypeImage 2D Sampled=2 | texture2d<type, access::read> | RWTexture2D |
129| texture_storage_2d_array<image_storage_type> | OpTypeImage 2D Arrayed=1 Sampled=2 | texture2d_array<type, access::read> | RWTexture2DArray |
130| texture_storage_3d<image_storage_type> | OpTypeImage 3D Sampled=2 | texture3d<type, access::read> | RWTexture3D |
dan sinclair2e6dc602020-11-10 22:10:56 +0000131| | | |
dan sinclair904c5b92021-01-14 08:34:46 +0000132| texture_storage_1d<image_storage_type> | OpTypeImage 1D Sampled=2 | texture1d<type, access::write> | RWTexture1D |
dan sinclair904c5b92021-01-14 08:34:46 +0000133| texture_storage_2d<image_storage_type> | OpTypeImage 2D Sampled=1 | texture2d<type, access::write> | RWTexture2D |
134| texture_storage_2d_array<image_storage_type> | OpTypeImage 2D Arrayed=1 Sampled=2 | texture2d_array<type, access::write> | RWTexture2DArray |
135| texture_storage_3d<image_storage_type> | OpTypeImage 3D Sampled=2 | texture3d<type, access::write> | RWTexture3D|
dan sinclair2e6dc602020-11-10 22:10:56 +0000136
dan sinclaird2fa57d2020-10-29 03:39:34 +0000137# Short-circuting
138## HLSL
139TODO(dsinclair): Nested if's
140
141## SPIR-V
142TODO(dsinclair): Nested if's
143
dan sinclairff7cf212022-10-03 14:05:23 +0000144# Address spaces
dan sinclaird2fa57d2020-10-29 03:39:34 +0000145TODO(dsinclair): do ...
146
147# Storage buffers
148## HLSL
149TODO(dsinclair): Rewriting of accessors to loads
150
151# Loop blocks
152## HLSL
153TODO(dsinclair): Rewrite with bools
154
155## MSL
156TODO(dsinclair): Rewrite with bools
157
dan sinclairff7cf212022-10-03 14:05:23 +0000158# Input / Output address spaces
dan sinclaird2fa57d2020-10-29 03:39:34 +0000159## HLSL
160TODO(dsinclair): Structs and params
161
162## MSL
163TODO(dsinclair): Structs and params
164
165# Discard
166## HLSL
167 * `discard`
168
169## MSL
170 * `discard_fragment()`
171
172
173# Specialization constants
174## HLSL
175```
176#ifndef WGSL_SPEC_CONSTANT_<id>
177-- if default provided
178#define WGSL_SPEC_CONSTANT_<id> default value
179-- else
180#error spec constant required for constant id
181--
182#endif
183static const <type> <name> = WGSL_SPEC_CONSTANT_<id>
184```
185
186## MSL
Ben Clayton01e4b6f2022-01-19 22:46:57 +0000187`@function_constant(<id>)`