-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworkshop.js
144 lines (130 loc) · 4.92 KB
/
workshop.js
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
'use strict';
import { Sample } from './common/framework/sample.js';
import { Loader } from './common/framework/util/loader.js';
export class Workshop extends Sample {
async init() {
this.assetLoader = new Loader({basePath: './common/assets'});
this.translation = {
x: 0,
y: 0,
}
this.gui.add(this.translation, 'x', -1, 1);
this.gui.add(this.translation, 'y', -1, 1);
await this.#initResources();
await this.#initPipelines();
}
render() {
const uniformArray = new Float32Array([this.translation.x, this.translation.y]);
this.device.queue.writeBuffer(this.uniformBuffer, 0, uniformArray);
const commandEncoder = this.device.createCommandEncoder();
this.colorAttachment.view = this.context.getCurrentTexture().createView();
const renderPass = commandEncoder.beginRenderPass({colorAttachments: [this.colorAttachment]});
renderPass.setPipeline(this.pipeline);
renderPass.setBindGroup(0, this.bindGroup);
renderPass.setVertexBuffer(0, this.vertexBuffer);
renderPass.setIndexBuffer(this.indexBuffer, 'uint32');
renderPass.drawIndexed(3);
renderPass.end();
this.device.queue.submit([commandEncoder.finish()]);
}
async #initResources() {
// Prepare vertex buffer
const vertices = new Float32Array([
0.0, 0.5, 0.5, 1.0,
-0.5, -0.5, 0.0, 0.0,
0.5, -0.5, 1.0, 0.0,
]);
this.vertexBuffer = this.device.createBuffer({
size: vertices.byteLength,
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
mappedAtCreation: true,
});
new Float32Array(this.vertexBuffer.getMappedRange()).set(vertices);
this.vertexBuffer.unmap();
// Prepare index buffer
const indices = new Uint32Array([
0, 1, 2,
]);
this.indexBuffer = this.device.createBuffer({
size: indices.byteLength,
usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST,
mappedAtCreation: true,
});
new Uint32Array(this.indexBuffer.getMappedRange()).set(indices);
this.indexBuffer.unmap();
const image = await this.assetLoader.loadImage('images/brick.png');
this.texture = this.device.createTexture({
size: [image.width, image.height],
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT,
format: 'rgba8unorm',
});
this.device.queue.copyExternalImageToTexture(
{source: image},
{texture: this.texture},
[image.width, image.height]
);
// Create sampler
this.sampler = this.device.createSampler({
magFilter: 'linear',
minFilter: 'linear'
});
// Prepare uniform buffer
this.uniformBuffer = this.device.createBuffer({
size: 8,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
});
}
async #initPipelines() {
const code = await new Loader().loadText('shaders/shader.wgsl');
const shaderModule = this.device.createShaderModule({code});
this.pipeline = this.device.createRenderPipeline({
layout: 'auto',
vertex: {
module: shaderModule,
entryPoint: 'vertex',
buffers: [
{
attributes: [
{
shaderLocation: 0,
offset: 0,
format: 'float32x2',
},
{
shaderLocation: 1,
offset: 8,
format: 'float32x2',
}
],
arrayStride: 16,
},
],
},
fragment: {
module: shaderModule,
entryPoint: 'fragment',
targets: [
{
format: this.gpu.getPreferredCanvasFormat()
}
],
},
});
// Prepare bind group
this.bindGroup = this.device.createBindGroup({
layout: this.pipeline.getBindGroupLayout(0),
entries: [
{binding: 0, resource: {buffer: this.uniformBuffer}},
{binding: 1, resource: this.texture.createView()},
{binding: 2, resource: this.sampler}
]
});
this.colorAttachment = {
view: null, // Will be set in render()
clearValue: {r: 0, g: 0, b: 0, a: 1},
loadOp: 'clear',
loadValue: {r: 0, g: 0, b: 0, a: 1},
storeOp: 'store'
};
}
}