summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/shader/validation/resource_interface/util.ts
blob: a4d0889932f0e37464c0ccddb939dc5f612502d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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}
}`;
  }
}