dan sinclair | d2fa57d | 2020-10-29 03:39:34 +0000 | [diff] [blame] | 1 | # Translations |
| 2 | |
| 3 | This document attempts to document how WGSL translates into the various backends |
| 4 | for 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 |
| 16 | There are two ways this can be achieved in SPIR-V. Either the variable can be |
| 17 | decorated 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 sinclair | 62bbc6f | 2020-10-29 12:44:32 +0000 | [diff] [blame] | 28 | | Name | SPIR-V | MSL | HLSL | |
| 29 | |------|--------|-----|------| |
| 30 | | position | SpvBuiltInPosition |position | SV_Position | |
dan sinclair | d7335fa | 2021-01-18 15:51:13 +0000 | [diff] [blame] | 31 | | vertex_index | SpvBuiltInVertexIndex |vertex_id | SV_VertexID | |
| 32 | | instance_index | SpvBuiltInInstanceIndex | instance_id| SV_InstanceID | |
dan sinclair | 62bbc6f | 2020-10-29 12:44:32 +0000 | [diff] [blame] | 33 | | 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 sinclair | d7335fa | 2021-01-18 15:51:13 +0000 | [diff] [blame] | 37 | | local_invocation_index | SpvBuiltInLocalInvocationIndex | thread_index_in_threadgroup | SV_GroupIndex | |
dan sinclair | 62bbc6f | 2020-10-29 12:44:32 +0000 | [diff] [blame] | 38 | | global_invocation_id | SpvBuiltInGlobalInvocationId | thread_position_in_grid | SV_DispatchThreadID | |
dan sinclair | d2fa57d | 2020-10-29 03:39:34 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | # Builtins Methods |
dan sinclair | 62bbc6f | 2020-10-29 12:44:32 +0000 | [diff] [blame] | 42 | | 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 sinclair | 62bbc6f | 2020-10-29 12:44:32 +0000 | [diff] [blame] | 78 | | 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 sinclair | 62bbc6f | 2020-10-29 12:44:32 +0000 | [diff] [blame] | 87 | | 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 Price | 0384932 | 2022-04-14 19:49:10 +0000 | [diff] [blame] | 95 | | smoothstep | GLSLstd450SmoothStep | smoothstep | smoothstep | |
dan sinclair | 62bbc6f | 2020-10-29 12:44:32 +0000 | [diff] [blame] | 96 | | 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 sinclair | d2fa57d | 2020-10-29 03:39:34 +0000 | [diff] [blame] | 101 | |
dan sinclair | 2e6dc60 | 2020-11-10 22:10:56 +0000 | [diff] [blame] | 102 | # Types |
| 103 | ## Sampler Types |
| 104 | | WGSL | SPIR-V | MSL | HLSL | |
| 105 | |------|--------|-----|------| |
dan sinclair | f7e152a | 2020-11-11 00:58:19 +0000 | [diff] [blame] | 106 | | sampler | OpTypeSampler | sampler | SamplerState | |
| 107 | | sampler_comparison | OpTypeSampler | sampler | SamplerComparisonState | |
dan sinclair | 2e6dc60 | 2020-11-10 22:10:56 +0000 | [diff] [blame] | 108 | |
| 109 | ## Texture Types |
| 110 | | WGSL | SPIR-V | MSL | HLSL | |
| 111 | |------|--------|-----|------| |
dan sinclair | 6526bd4 | 2020-11-11 01:49:28 +0000 | [diff] [blame] | 112 | | texture_1d<type> | OpTypeImage 1D Sampled=1 | texture1d<type, access::sample> | Texture1D | |
dan sinclair | 6526bd4 | 2020-11-11 01:49:28 +0000 | [diff] [blame] | 113 | | 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 sinclair | 2e6dc60 | 2020-11-10 22:10:56 +0000 | [diff] [blame] | 118 | | | | | |
dan sinclair | 6526bd4 | 2020-11-11 01:49:28 +0000 | [diff] [blame] | 119 | | texture_multisampled_2d<type> | OpTypeImage 2D MS=1 Sampled=1 | texture2d_ms<type, access::sample> | Texture2D | |
dan sinclair | 2e6dc60 | 2020-11-10 22:10:56 +0000 | [diff] [blame] | 120 | | | | | |
Jiawei Shao | 9d27aaa | 2022-04-08 08:33:28 +0000 | [diff] [blame] | 121 | | 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 sinclair | 2e6dc60 | 2020-11-10 22:10:56 +0000 | [diff] [blame] | 126 | | | | | |
dan sinclair | 904c5b9 | 2021-01-14 08:34:46 +0000 | [diff] [blame] | 127 | | texture_storage_1d<image_storage_type> | OpTypeImage 1D Sampled=2| texture1d<type, access::read> | RWTexture1D | |
dan sinclair | 904c5b9 | 2021-01-14 08:34:46 +0000 | [diff] [blame] | 128 | | 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 sinclair | 2e6dc60 | 2020-11-10 22:10:56 +0000 | [diff] [blame] | 131 | | | | | |
dan sinclair | 904c5b9 | 2021-01-14 08:34:46 +0000 | [diff] [blame] | 132 | | texture_storage_1d<image_storage_type> | OpTypeImage 1D Sampled=2 | texture1d<type, access::write> | RWTexture1D | |
dan sinclair | 904c5b9 | 2021-01-14 08:34:46 +0000 | [diff] [blame] | 133 | | 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 sinclair | 2e6dc60 | 2020-11-10 22:10:56 +0000 | [diff] [blame] | 136 | |
dan sinclair | d2fa57d | 2020-10-29 03:39:34 +0000 | [diff] [blame] | 137 | # Short-circuting |
| 138 | ## HLSL |
| 139 | TODO(dsinclair): Nested if's |
| 140 | |
| 141 | ## SPIR-V |
| 142 | TODO(dsinclair): Nested if's |
| 143 | |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 144 | # Address spaces |
dan sinclair | d2fa57d | 2020-10-29 03:39:34 +0000 | [diff] [blame] | 145 | TODO(dsinclair): do ... |
| 146 | |
| 147 | # Storage buffers |
| 148 | ## HLSL |
| 149 | TODO(dsinclair): Rewriting of accessors to loads |
| 150 | |
| 151 | # Loop blocks |
| 152 | ## HLSL |
| 153 | TODO(dsinclair): Rewrite with bools |
| 154 | |
| 155 | ## MSL |
| 156 | TODO(dsinclair): Rewrite with bools |
| 157 | |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 158 | # Input / Output address spaces |
dan sinclair | d2fa57d | 2020-10-29 03:39:34 +0000 | [diff] [blame] | 159 | ## HLSL |
| 160 | TODO(dsinclair): Structs and params |
| 161 | |
| 162 | ## MSL |
| 163 | TODO(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 |
| 183 | static const <type> <name> = WGSL_SPEC_CONSTANT_<id> |
| 184 | ``` |
| 185 | |
| 186 | ## MSL |
Ben Clayton | 01e4b6f | 2022-01-19 22:46:57 +0000 | [diff] [blame] | 187 | `@function_constant(<id>)` |