-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd_buffer.ts
More file actions
61 lines (52 loc) · 1.73 KB
/
Copy pathadd_buffer.ts
File metadata and controls
61 lines (52 loc) · 1.73 KB
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
import {Glslang} from '@webgpu/glslang/dist/web-devel/glslang.onefile';
import {BufferOp} from './buffer';
export class AddBufferOp extends BufferOp {
workGroupSize: [number, number, number];
constructor(
device: GPUDevice, glslang: Glslang,
firstMatrix: Float32Array|Uint32Array,
secondMatrix: Float32Array|Uint32Array, shape: Uint32Array) {
// Compute shader code (GLSL)
super(device, glslang);
// const TS = 32;
this.workGroupSize = [128, 1, 1];
this.compile(firstMatrix, secondMatrix, shape, this.getShader());
}
async execute() {
const result = await this.compileAndRun(this.workGroupSize);
return result;
}
executeSync() {
const result = this.compileAndRunSync(this.workGroupSize);
return result;
}
getShader() {
// Compute shader code (GLSL)
const computeShaderCode = `#version 450
layout(set = 0, binding = 0) uniform Uniforms {
int inputWidth;
int inputHeight;
int filterWidth;
int filterHeight;
int outputWidth;
int outputHeight;
} uniforms;
layout(set = 0, binding = 1) readonly buffer FirstMatrix {
float firstMatrix[];
} ;
layout(set = 0, binding = 2) readonly buffer SecondMatrix {
float secondMatrix[];
} ;
layout(set = 0, binding = 3) buffer ResultMatrix {
float resultMatrix[];
} ;
layout(local_size_x = ${this.workGroupSize[0]}, local_size_y = ${
this.workGroupSize[1]}, local_size_z = 1) in;
void main() {
uint index = gl_GlobalInvocationID.x;
resultMatrix[index] = firstMatrix[index]+secondMatrix[index];
}
`;
return computeShaderCode;
}
}