(
inputInfo: InputInfo[], outputData: {dtype: DataType, shape: number[]},
program: WebGPUProgram)
| 196 | } |
| 197 | |
| 198 | function makeShader( |
| 199 | inputInfo: InputInfo[], outputData: {dtype: DataType, shape: number[]}, |
| 200 | program: WebGPUProgram): string { |
| 201 | const prefixSnippets: string[] = []; |
| 202 | const flatWorkgroupSize = program.workgroupSize[0] * |
| 203 | program.workgroupSize[1] * program.workgroupSize[2]; |
| 204 | program.outputComponent = |
| 205 | program.outputComponent ? program.outputComponent : 1; |
| 206 | prefixSnippets.push(` |
| 207 | |
| 208 | var<private> localId: vec3<u32>; |
| 209 | var<private> localIndex: u32; |
| 210 | var<private> globalId: vec3<u32>; |
| 211 | var<private> numWorkgroups: vec3<u32>; |
| 212 | var<private> workgroupId: vec3<u32>; |
| 213 | |
| 214 | // Only used when the y/z dimension of workgroup size is 1. |
| 215 | fn getGlobalIndex() -> i32 { |
| 216 | ${ |
| 217 | isFlatDispatch(program) ? |
| 218 | ` return i32(globalId.x);` : |
| 219 | ` return i32((workgroupId.z * numWorkgroups.x * numWorkgroups.y + |
| 220 | workgroupId.y * numWorkgroups.x + workgroupId.x) * ${ |
| 221 | flatWorkgroupSize}u + |
| 222 | localIndex); |
| 223 | `} |
| 224 | } |
| 225 | `); |
| 226 | |
| 227 | if (program.pixelsOpType != null) { |
| 228 | const inoutSnippet = program.pixelsOpType === PixelsOpType.FROM_PIXELS ? |
| 229 | `@group(0) @binding(0) var<storage, read_write> result: array<${ |
| 230 | dataTypeToGPUType(outputData.dtype, program.outputComponent)}>;` : |
| 231 | `@group(0) @binding(1) var<storage, read> inBuf : array<${ |
| 232 | dataTypeToGPUType(inputInfo[0].dtype, program.outputComponent)}>;`; |
| 233 | const outShapeStridesType = |
| 234 | outputData.shape.length === 3 ? 'vec2<i32>' : 'i32'; |
| 235 | prefixSnippets.push(` |
| 236 | struct Uniform { |
| 237 | outShapeStrides : ${outShapeStridesType}, |
| 238 | size : i32, |
| 239 | numChannels : i32, |
| 240 | alpha : f32, |
| 241 | }; |
| 242 | |
| 243 | ${inoutSnippet} |
| 244 | @group(0) @binding(2) var<uniform> uniforms: Uniform; |
| 245 | `); |
| 246 | const useGlobalIndex = isFlatDispatchLayout(program); |
| 247 | return [ |
| 248 | commonSnippet, |
| 249 | prefixSnippets.join('\n'), |
| 250 | getCoordsFromIndexSnippet(outputData.shape), |
| 251 | program.getUserCode(), |
| 252 | getStartHeaderString(useGlobalIndex, program), |
| 253 | ].join('\n'); |
| 254 | } |
| 255 |
no test coverage detected
searching dependent graphs…