summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/shader/execution/shader_io/shared_structs.spec.ts
blob: f2e8cf4eceac9ecd5d5267b1dae474cf92b26c26 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
export const description = `Test the shared use of structures containing entry point IO attributes`;

import { makeTestGroup } from '../../../../common/framework/test_group.js';
import { GPUTest } from '../../../gpu_test.js';
import { checkElementsEqual } from '../../../util/check_contents.js';

export const g = makeTestGroup(GPUTest);

g.test('shared_with_buffer')
  .desc(
    `Test sharing an entry point IO struct with a buffer.

     This test defines a structure that contains both builtin attributes and layout attributes,
     and uses that structure as both an entry point input and the store type of a storage buffer.
     The builtin attributes should be ignored when used for the storage buffer, and the layout
     attributes should be ignored when used as an entry point IO parameter.
    `
  )
  .fn(async t => {
    // Set the dispatch parameters such that we get some interesting (non-zero) built-in variables.
    const wgsize = new Uint32Array([8, 4, 2]);
    const numGroups = new Uint32Array([4, 2, 8]);

    // Pick a single invocation to copy the input structure to the output buffer.
    const targetLocalIndex = 13;
    const targetGroup = new Uint32Array([2, 1, 5]);

    // The test shader defines a structure that contains members decorated with built-in variable
    // attributes, and also layout attributes for the storage buffer.
    const wgsl = `
      struct S {
        /* byte offset:  0 */ @size(32)  @builtin(workgroup_id) group_id : vec3<u32>,
        /* byte offset: 32 */            @builtin(local_invocation_index) local_index : u32,
        /* byte offset: 64 */ @align(64) @builtin(num_workgroups) numGroups : vec3<u32>,
      };

      @group(0) @binding(0)
      var<storage, read_write> outputs : S;

      @compute @workgroup_size(${wgsize[0]}, ${wgsize[1]}, ${wgsize[2]})
      fn main(inputs : S) {
        if (inputs.group_id.x == ${targetGroup[0]}u &&
            inputs.group_id.y == ${targetGroup[1]}u &&
            inputs.group_id.z == ${targetGroup[2]}u &&
            inputs.local_index == ${targetLocalIndex}u) {
          outputs = inputs;
        }
      }
    `;

    const pipeline = t.device.createComputePipeline({
      layout: 'auto',
      compute: {
        module: t.device.createShaderModule({ code: wgsl }),
        entryPoint: 'main',
      },
    });

    // Allocate a buffer to hold the output structure.
    const bufferNumElements = 32;
    const outputBuffer = t.device.createBuffer({
      size: bufferNumElements * Uint32Array.BYTES_PER_ELEMENT,
      usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,
    });
    const bindGroup = t.device.createBindGroup({
      layout: pipeline.getBindGroupLayout(0),
      entries: [{ binding: 0, resource: { buffer: outputBuffer } }],
    });

    // Run the shader.
    const encoder = t.device.createCommandEncoder();
    const pass = encoder.beginComputePass();
    pass.setPipeline(pipeline);
    pass.setBindGroup(0, bindGroup);
    pass.dispatchWorkgroups(numGroups[0], numGroups[1], numGroups[2]);
    pass.end();
    t.queue.submit([encoder.finish()]);

    // Check the output values.
    const checkOutput = (outputs: Uint32Array) => {
      if (checkElementsEqual(outputs.slice(0, 3), targetGroup)) {
        return new Error(
          `group_id comparison failed\n` +
            `    expected: ${targetGroup}\n` +
            `    got:      ${outputs.slice(0, 3)}`
        );
      }
      if (outputs[8] !== targetLocalIndex) {
        return new Error(
          `local_index comparison failed\n` +
            `    expected: ${targetLocalIndex}\n` +
            `    got:      ${outputs[8]}`
        );
      }
      if (checkElementsEqual(outputs.slice(16, 19), numGroups)) {
        return new Error(
          `numGroups comparison failed\n` +
            `    expected: ${numGroups}\n` +
            `    got:      ${outputs.slice(16, 19)}`
        );
      }
      return undefined;
    };
    t.expectGPUBufferValuesPassCheck(outputBuffer, outputData => checkOutput(outputData), {
      type: Uint32Array,
      typedLength: bufferNumElements,
    });
  });

g.test('shared_between_stages')
  .desc(
    `Test sharing an entry point IO struct between different pipeline stages.

     This test defines an entry point IO structure, and uses it as both the output of a vertex
     shader and the input to a fragment shader.
    `
  )
  .fn(async t => {
    const size = [31, 31];
    const wgsl = `
      struct Interface {
        @builtin(position) position : vec4<f32>,
        @location(0) color : f32,
      };

      var<private> vertices : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
        vec2<f32>(-0.7, -0.7),
        vec2<f32>( 0.0,  0.7),
        vec2<f32>( 0.7, -0.7),
      );

      @vertex
      fn vert_main(@builtin(vertex_index) index : u32) -> Interface {
        return Interface(vec4<f32>(vertices[index], 0.0, 1.0), 1.0);
      }

      @fragment
      fn frag_main(inputs : Interface) -> @location(0) vec4<f32> {
        // Toggle red vs green based on the x position.
        var color = vec4<f32>(0.0, 0.0, 0.0, 1.0);
        if (inputs.position.x > f32(${size[0] / 2})) {
          color.r = inputs.color;
        } else {
          color.g = inputs.color;
        }
        return color;
      }
    `;

    // Set up the render pipeline.
    const module = t.device.createShaderModule({ code: wgsl });
    const pipeline = t.device.createRenderPipeline({
      layout: 'auto',
      vertex: {
        module,
        entryPoint: 'vert_main',
      },
      fragment: {
        module,
        entryPoint: 'frag_main',
        targets: [
          {
            format: 'rgba8unorm',
          },
        ],
      },
    });

    // Draw a red triangle.
    const renderTarget = t.device.createTexture({
      size,
      usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
      format: 'rgba8unorm',
    });
    const encoder = t.device.createCommandEncoder();
    const pass = encoder.beginRenderPass({
      colorAttachments: [
        {
          view: renderTarget.createView(),
          clearValue: [0, 0, 0, 0],
          loadOp: 'clear',
          storeOp: 'store',
        },
      ],
    });
    pass.setPipeline(pipeline);
    pass.draw(3);
    pass.end();
    t.queue.submit([encoder.finish()]);

    // Test a few points to make sure we rendered a half-red/half-green triangle.
    const redPixel = new Uint8Array([255, 0, 0, 255]);
    const greenPixel = new Uint8Array([0, 255, 0, 255]);
    for (const p of [
      { x: 16, y: 15 },
      { x: 16, y: 15 },
      { x: 22, y: 20 },
    ]) {
      t.expectSinglePixelIn2DTexture(renderTarget, 'rgba8unorm', p, {
        exp: redPixel,
      });
    }
    for (const p of [
      { x: 14, y: 15 },
      { x: 14, y: 8 },
      { x: 8, y: 20 },
    ]) {
      t.expectSinglePixelIn2DTexture(renderTarget, 'rgba8unorm', p, {
        exp: greenPixel,
      });
    }
    const blackPixel = new Uint8Array([0, 0, 0, 0]);
    for (const p of [
      { x: 2, y: 2 },
      { x: 2, y: 28 },
      { x: 28, y: 2 },
      { x: 28, y: 28 },
    ]) {
      t.expectSinglePixelIn2DTexture(renderTarget, 'rgba8unorm', p, {
        exp: blackPixel,
      });
    }
  });

g.test('shared_with_non_entry_point_function')
  .desc(
    `Test sharing an entry point IO struct with a non entry point function.

     This test defines structures that contain builtin and location attributes, and uses those
     structures as parameter and return types for entry point functions and regular functions.
    `
  )
  .fn(async t => {
    // The test shader defines structures that contain members decorated with built-in variable
    // attributes and user-defined IO. These structures are passed to and returned from regular
    // functions.
    const wgsl = `
      struct Inputs {
        @builtin(vertex_index) index : u32,
        @location(0) color : vec4<f32>,
      };
      struct Outputs {
        @builtin(position) position : vec4<f32>,
        @location(0) color : vec4<f32>,
      };

      var<private> vertices : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
        vec2<f32>(-0.7, -0.7),
        vec2<f32>( 0.0,  0.7),
        vec2<f32>( 0.7, -0.7),
      );

      fn process(in : Inputs) -> Outputs {
        var out : Outputs;
        out.position = vec4<f32>(vertices[in.index], 0.0, 1.0);
        out.color = in.color;
        return out;
      }

      @vertex
      fn vert_main(inputs : Inputs) -> Outputs {
        return process(inputs);
      }

      @fragment
      fn frag_main(@location(0) color : vec4<f32>) -> @location(0) vec4<f32> {
        return color;
      }
    `;

    // Set up the render pipeline.
    const module = t.device.createShaderModule({ code: wgsl });
    const pipeline = t.device.createRenderPipeline({
      layout: 'auto',
      vertex: {
        module,
        entryPoint: 'vert_main',
        buffers: [
          {
            attributes: [
              {
                shaderLocation: 0,
                format: 'float32x4',
                offset: 0,
              },
            ],
            arrayStride: 4 * Float32Array.BYTES_PER_ELEMENT,
          },
        ],
      },
      fragment: {
        module,
        entryPoint: 'frag_main',
        targets: [
          {
            format: 'rgba8unorm',
          },
        ],
      },
    });

    // Draw a triangle.
    // The vertex buffer contains the vertex colors (all red).
    const vertexBuffer = t.makeBufferWithContents(
      new Float32Array([1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0]),
      GPUBufferUsage.VERTEX
    );
    const renderTarget = t.device.createTexture({
      size: [31, 31],
      usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
      format: 'rgba8unorm',
    });
    const encoder = t.device.createCommandEncoder();
    const pass = encoder.beginRenderPass({
      colorAttachments: [
        {
          view: renderTarget.createView(),
          clearValue: [0, 0, 0, 0],
          loadOp: 'clear',
          storeOp: 'store',
        },
      ],
    });
    pass.setPipeline(pipeline);
    pass.setVertexBuffer(0, vertexBuffer);
    pass.draw(3);
    pass.end();
    t.queue.submit([encoder.finish()]);

    // Test a few points to make sure we rendered a red triangle.
    const redPixel = new Uint8Array([255, 0, 0, 255]);
    for (const p of [
      { x: 15, y: 15 },
      { x: 15, y: 8 },
      { x: 8, y: 20 },
      { x: 22, y: 20 },
    ]) {
      t.expectSinglePixelIn2DTexture(renderTarget, 'rgba8unorm', p, {
        exp: redPixel,
      });
    }
    const blackPixel = new Uint8Array([0, 0, 0, 0]);
    for (const p of [
      { x: 2, y: 2 },
      { x: 2, y: 28 },
      { x: 28, y: 2 },
      { x: 28, y: 28 },
    ]) {
      t.expectSinglePixelIn2DTexture(renderTarget, 'rgba8unorm', p, {
        exp: blackPixel,
      });
    }
  });