summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/api/operation/command_buffer/programmable/programmable_state_test.ts
blob: 19cf91419c16c96b8ab769430b887646abe76822 (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
import { unreachable } from '../../../../../common/util/util.js';
import { GPUTest } from '../../../../gpu_test.js';
import { EncoderType } from '../../../../util/command_buffer_maker.js';

interface BindGroupIndices {
  a: number;
  b: number;
  out: number;
}

export class ProgrammableStateTest extends GPUTest {
  private commonBindGroupLayouts: Map<string, GPUBindGroupLayout> = new Map();

  getBindGroupLayout(type: GPUBufferBindingType): GPUBindGroupLayout {
    if (!this.commonBindGroupLayouts.has(type)) {
      this.commonBindGroupLayouts.set(
        type,
        this.device.createBindGroupLayout({
          entries: [
            {
              binding: 0,
              visibility: GPUShaderStage.COMPUTE | GPUShaderStage.FRAGMENT,
              buffer: { type },
            },
          ],
        })
      );
    }
    return this.commonBindGroupLayouts.get(type)!;
  }

  getBindGroupLayouts(indices: BindGroupIndices): GPUBindGroupLayout[] {
    const bindGroupLayouts: GPUBindGroupLayout[] = [];
    bindGroupLayouts[indices.a] = this.getBindGroupLayout('read-only-storage');
    bindGroupLayouts[indices.b] = this.getBindGroupLayout('read-only-storage');
    bindGroupLayouts[indices.out] = this.getBindGroupLayout('storage');
    return bindGroupLayouts;
  }

  createBindGroup(buffer: GPUBuffer, type: GPUBufferBindingType): GPUBindGroup {
    return this.device.createBindGroup({
      layout: this.getBindGroupLayout(type),
      entries: [{ binding: 0, resource: { buffer } }],
    });
  }

  setBindGroup(
    encoder: GPUBindingCommandsMixin,
    index: number,
    factory: (index: number) => GPUBindGroup
  ) {
    encoder.setBindGroup(index, factory(index));
  }

  // Create a compute pipeline that performs an operation on data from two bind groups,
  // then writes the result to a third bind group.
  createBindingStatePipeline<T extends EncoderType>(
    encoderType: T,
    groups: BindGroupIndices,
    algorithm: string = 'a.value - b.value'
  ): GPUComputePipeline | GPURenderPipeline {
    switch (encoderType) {
      case 'compute pass': {
        const wgsl = `struct Data {
            value : i32
          };

          @group(${groups.a}) @binding(0) var<storage> a : Data;
          @group(${groups.b}) @binding(0) var<storage> b : Data;
          @group(${groups.out}) @binding(0) var<storage, read_write> out : Data;

          @compute @workgroup_size(1) fn main() {
            out.value = ${algorithm};
            return;
          }
        `;

        return this.device.createComputePipeline({
          layout: this.device.createPipelineLayout({
            bindGroupLayouts: this.getBindGroupLayouts(groups),
          }),
          compute: {
            module: this.device.createShaderModule({
              code: wgsl,
            }),
            entryPoint: 'main',
          },
        });
      }
      case 'render pass':
      case 'render bundle': {
        const wgslShaders = {
          vertex: `
            @vertex fn vert_main() -> @builtin(position) vec4<f32> {
              return vec4<f32>(0.5, 0.5, 0.0, 1.0);
            }
          `,

          fragment: `
            struct Data {
              value : i32
            };

            @group(${groups.a}) @binding(0) var<storage> a : Data;
            @group(${groups.b}) @binding(0) var<storage> b : Data;
            @group(${groups.out}) @binding(0) var<storage, read_write> out : Data;

            @fragment fn frag_main() -> @location(0) vec4<f32> {
              out.value = ${algorithm};
              return vec4<f32>(1.0, 0.0, 0.0, 1.0);
            }
          `,
        };

        return this.device.createRenderPipeline({
          layout: this.device.createPipelineLayout({
            bindGroupLayouts: this.getBindGroupLayouts(groups),
          }),
          vertex: {
            module: this.device.createShaderModule({
              code: wgslShaders.vertex,
            }),
            entryPoint: 'vert_main',
          },
          fragment: {
            module: this.device.createShaderModule({
              code: wgslShaders.fragment,
            }),
            entryPoint: 'frag_main',
            targets: [{ format: 'rgba8unorm' }],
          },
          primitive: { topology: 'point-list' },
        });
      }
      default:
        unreachable();
    }
  }

  setPipeline(pass: GPUBindingCommandsMixin, pipeline: GPUComputePipeline | GPURenderPipeline) {
    if (pass instanceof GPUComputePassEncoder) {
      pass.setPipeline(pipeline as GPUComputePipeline);
    } else if (pass instanceof GPURenderPassEncoder || pass instanceof GPURenderBundleEncoder) {
      pass.setPipeline(pipeline as GPURenderPipeline);
    }
  }

  dispatchOrDraw(pass: GPUBindingCommandsMixin) {
    if (pass instanceof GPUComputePassEncoder) {
      pass.dispatchWorkgroups(1);
    } else if (pass instanceof GPURenderPassEncoder) {
      pass.draw(1);
    } else if (pass instanceof GPURenderBundleEncoder) {
      pass.draw(1);
    }
  }
}