diff options
Diffstat (limited to 'dom/webgpu/tests/cts/checkout/src/webgpu/shader/validation/resource_interface/util.ts')
-rw-r--r-- | dom/webgpu/tests/cts/checkout/src/webgpu/shader/validation/resource_interface/util.ts | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/dom/webgpu/tests/cts/checkout/src/webgpu/shader/validation/resource_interface/util.ts b/dom/webgpu/tests/cts/checkout/src/webgpu/shader/validation/resource_interface/util.ts new file mode 100644 index 0000000000..a4d0889932 --- /dev/null +++ b/dom/webgpu/tests/cts/checkout/src/webgpu/shader/validation/resource_interface/util.ts @@ -0,0 +1,91 @@ +/** + * ResourceDeclarationEmitter is a function that emits the WGSL declaring a resource variable with + * the given group, binding and name. + */ +export type ResourceDeclarationEmitter = (name: string, group?: number, binding?: number) => string; + +/** Helper function for emitting a resource declaration's group and binding attributes */ +function groupAndBinding(group?: number, binding?: number): string { + return ( + `${group !== undefined ? `@group(${group})` : '/* no group */'} ` + + `${binding !== undefined ? `@binding(${binding})` : '/* no binding */'}` + ); +} + +/** Helper function for emitting a resource declaration for the given type */ +function basicEmitter(type: string): ResourceDeclarationEmitter { + return (name: string, group?: number, binding?: number) => + `${groupAndBinding(group, binding)} var ${name} : ${type};\n`; +} + +/** Map of resource declaration name, to an emitter. */ +export const kResourceEmitters = new Map<string, ResourceDeclarationEmitter>([ + ['texture_1d', basicEmitter('texture_1d<i32>')], + ['texture_2d', basicEmitter('texture_2d<i32>')], + ['texture_2d_array', basicEmitter('texture_2d_array<f32>')], + ['texture_3d', basicEmitter('texture_3d<i32>')], + ['texture_cube', basicEmitter('texture_cube<u32>')], + ['texture_cube_array', basicEmitter('texture_cube_array<u32>')], + ['texture_multisampled_2d', basicEmitter('texture_multisampled_2d<i32>')], + ['texture_external', basicEmitter('texture_external')], + ['texture_storage_1d', basicEmitter('texture_storage_1d<rgba8unorm, write>')], + ['texture_storage_2d', basicEmitter('texture_storage_2d<rgba8sint, write>')], + ['texture_storage_2d_array', basicEmitter('texture_storage_2d_array<r32uint, write>')], + ['texture_storage_3d', basicEmitter('texture_storage_3d<rg32uint, write>')], + ['texture_depth_2d', basicEmitter('texture_depth_2d')], + ['texture_depth_2d_array', basicEmitter('texture_depth_2d_array')], + ['texture_depth_cube', basicEmitter('texture_depth_cube')], + ['texture_depth_cube_array', basicEmitter('texture_depth_cube_array')], + ['texture_depth_multisampled_2d', basicEmitter('texture_depth_multisampled_2d')], + ['sampler', basicEmitter('sampler')], + ['sampler_comparison', basicEmitter('sampler_comparison')], + [ + 'uniform', + (name: string, group?: number, binding?: number) => + `${groupAndBinding(group, binding)} var<uniform> ${name} : array<vec4<f32>, 16>;\n`, + ], + [ + 'storage', + (name: string, group?: number, binding?: number) => + `${groupAndBinding(group, binding)} var<storage> ${name} : array<vec4<f32>, 16>;\n`, + ], +]); + +/** A small selection of resource declaration names, which can be used in test permutations */ +export const kResourceKindsA = ['storage', 'texture_2d', 'texture_external', 'uniform']; + +/** A small selection of resource declaration names, which can be used in test permutations */ +export const kResourceKindsB = ['texture_3d', 'texture_storage_1d', 'uniform']; + +/** An enumerator of shader stages */ +export type ShaderStage = 'vertex' | 'fragment' | 'compute'; + +/** + * declareEntrypoint emits the WGSL to declare an entry point with the given name, stage and body. + * The generated function will have an appropriate return type and return statement, so that @p body + * does not have to change between stage. + * @param name the entry point function name + * @param stage the entry point stage + * @param body the body of the function (excluding any automatically suffixed return statements) + * @returns the WGSL string for the entry point + */ +export function declareEntrypoint(name: string, stage: ShaderStage, body: string): string { + switch (stage) { + case 'vertex': + return `@vertex +fn ${name}() -> @builtin(position) vec4f { + ${body} + return vec4f(); +}`; + case 'fragment': + return `@fragment +fn ${name}() { + ${body} +}`; + case 'compute': + return `@compute @workgroup_size(1) +fn ${name}() { + ${body} +}`; + } +} |