-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
300 lines (245 loc) · 10.4 KB
/
index.ts
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
import { mat4, ReadonlyMat4, vec3 } from "gl-matrix";
import { DebugDraw, DebugDrawFlags, gpuShaderCode, Green, Red, White } from "./DebugDraw";
class Plane {
private vertexBuffer: GPUBuffer;
private indexBuffer: GPUBuffer;
private texture: GPUTexture;
private sampler: GPUSampler;
private pso: GPURenderPipeline;
private uniformBuffer: GPUBuffer;
private bindGroup: GPUBindGroup;
private worldFromModelMatrix = mat4.create();
constructor(device: GPUDevice, colorTextureFormat: GPUTextureFormat, debugDraw: DebugDraw) {
this.vertexBuffer = device.createBuffer({ size: 5 * 4 * 4, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST, label: `Plane` });
this.indexBuffer = device.createBuffer({ size: 6 * 2, usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST, label: `Plane` });
const vertexData = new Float32Array([
-1, 0, -1, 0, 0,
1, 0, -1, 1, 0,
1, 0, 1, 1, 1,
-1, 0, 1, 0, 1,
]);
device.queue.writeBuffer(this.vertexBuffer, 0, vertexData);
const indexData = new Uint16Array([ 0, 1, 2, 0, 2, 3 ]);
device.queue.writeBuffer(this.indexBuffer, 0, indexData);
const textureData = new Uint8Array([
0xCC, 0xCC, 0xCC, 0xCC,
0x77, 0x77, 0x77, 0x77,
0x77, 0x77, 0x77, 0x77,
0xCC, 0xCC, 0xCC, 0xCC,
]);
this.texture = device.createTexture({
size: [2, 2],
format: 'rgba8unorm',
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
label: `Checkerboard`,
});
device.queue.writeTexture({ texture: this.texture }, textureData, { bytesPerRow: 8 }, [2, 2]);
const shaderModule = this.createShaderModule(device);
this.pso = device.createRenderPipeline({
layout: 'auto',
vertex: {
module: shaderModule,
entryPoint: 'main_vs',
buffers: [{
attributes: [
{ shaderLocation: 0, format: 'float32x3', offset: 0 },
{ shaderLocation: 1, format: 'float32x2', offset: 3 * 4 },
],
arrayStride: 5 * 4,
}],
},
fragment: {
module: shaderModule,
entryPoint: 'main_ps',
targets: [{ format: colorTextureFormat }],
},
depthStencil: {
format: 'depth24plus',
depthCompare: 'greater',
depthWriteEnabled: true,
},
label: `Plane`,
});
this.sampler = device.createSampler({
addressModeU: `repeat`,
addressModeV: `repeat`,
minFilter: `nearest`,
magFilter: `nearest`,
label: `Plane`,
})
this.uniformBuffer = device.createBuffer({ size: 64*2, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });
this.bindGroup = device.createBindGroup({
layout: this.pso.getBindGroupLayout(0),
entries: [
{ binding: 0, resource: { buffer: this.uniformBuffer } },
{ binding: 1, resource: this.texture.createView() },
{ binding: 2, resource: this.sampler },
{ binding: 3, resource: { buffer: debugDraw.getGPUBuffer() } },
],
});
mat4.scale(this.worldFromModelMatrix, this.worldFromModelMatrix, [10, 10, 10]);
}
public draw(device: GPUDevice, clipFromWorldMatrix: ReadonlyMat4, pass: GPURenderPassEncoder): void {
const data = new Float32Array(this.uniformBuffer.size / 4);
data.set(clipFromWorldMatrix, 0);
data.set(this.worldFromModelMatrix, 16);
device.queue.writeBuffer(this.uniformBuffer, 0, data);
pass.setPipeline(this.pso);
pass.setBindGroup(0, this.bindGroup);
pass.setVertexBuffer(0, this.vertexBuffer);
pass.setIndexBuffer(this.indexBuffer, 'uint16');
pass.drawIndexed(6);
}
private createShaderModule(device: GPUDevice): GPUShaderModule {
const code = `
struct ViewData {
clip_from_world: mat4x4f,
world_from_model: mat4x4f,
};
@group(0) @binding(0) var<uniform> view_data: ViewData;
@group(0) @binding(1) var plane_texture: texture_2d<f32>;
@group(0) @binding(2) var point_sampler: sampler;
@group(0) @binding(3) var<storage, read_write> gDebugDraw_buffer: DebugDraw_Buffer;
struct VertexOutput {
@builtin(position) position: vec4f,
@location(0) uv: vec2f,
@location(1) world_position: vec3f,
};
@vertex
fn main_vs(@location(0) position: vec3f, @location(1) uv: vec2f) -> VertexOutput {
var out: VertexOutput;
out.position = view_data.clip_from_world * view_data.world_from_model * vec4f(position, 1.0f);
out.uv = uv * vec2f(10.0f);
out.world_position = (view_data.world_from_model * vec4f(position, 1.0f)).xyz;
return out;
}
${gpuShaderCode}
@fragment
fn main_ps(vertex: VertexOutput) -> @location(0) vec4f {
var color = textureSample(plane_texture, point_sampler, vertex.uv);
if (all(DebugDraw_getMouseHoverPos().xy == vec2i(vertex.position.xy))) {
DebugDraw_drawSphere(vertex.world_position.xyz, 1.0f, vec4f(0.0f, 1.0f, 0.0f, 1.0f));
// DebugDraw_screenPrintFloat3(vertex.world_position.xyz);
DebugDraw_screenPrintFloat4(color);
}
if (all(DebugDraw_getMousePressPos().xy == vec2i(vertex.position.xy))) {
DebugDraw_drawLocator(vertex.world_position.xyz, 0.5f, vec4f(0.0f, 0.0f, 1.0f, 1.0f));
}
return color;
}
`;
return device.createShaderModule({ code, label: `Plane `});
}
}
class MouseTracker {
public x = -1;
public y = -1;
public buttons = 0;
constructor(private canvas: HTMLCanvasElement) {
this.canvas.addEventListener('mousemove', this.update.bind(this));
this.canvas.addEventListener('mousedown', this.update.bind(this));
this.canvas.addEventListener('mouseup', this.update.bind(this));
}
private update(e: MouseEvent): void {
this.x = e.offsetX * window.devicePixelRatio;
this.y = e.offsetY * window.devicePixelRatio;
this.buttons = e.buttons;
}
}
export function resizeCanvas(canvas: HTMLCanvasElement, width: number, height: number, devicePixelRatio: number): void {
const nw = width * devicePixelRatio;
const nh = height * devicePixelRatio;
if (canvas.width === nw && canvas.height === nh)
return;
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
canvas.width = nw;
canvas.height = nh;
}
class Main {
private canvas: HTMLCanvasElement;
private device: GPUDevice;
private ctx: GPUCanvasContext;
private depthBuffer: GPUTexture;
private plane: Plane;
private clipFromViewMatrix = mat4.create();
private viewFromWorldMatrix = mat4.create();
private clipFromWorldMatrix = mat4.create();
private debugDraw: DebugDraw;
private mouseTracker: MouseTracker;
constructor() {
this.canvas = document.createElement('canvas');
document.body.appendChild(this.canvas);
this.mouseTracker = new MouseTracker(this.canvas);
this.init();
}
private _onResize() {
resizeCanvas(this.canvas, window.innerWidth, window.innerHeight, window.devicePixelRatio);
this.depthBuffer = this.device.createTexture({
size: [this.canvas.width, this.canvas.height],
format: 'depth24plus',
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
})
}
private async init() {
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter?.requestDevice();
if (device === undefined)
throw "whoops";
const colorTextureFormat = navigator.gpu.getPreferredCanvasFormat();
this.device = device;
this.ctx = this.canvas.getContext('webgpu') as GPUCanvasContext;
this.ctx.configure({ device, format: colorTextureFormat });
this.debugDraw = new DebugDraw(device, colorTextureFormat);
this.plane = new Plane(device, colorTextureFormat, this.debugDraw);
window.onresize = this._onResize.bind(this);
this._onResize();
requestAnimationFrame(this.update);
}
private updateCamera(): void {
mat4.lookAt(this.viewFromWorldMatrix, [0, 10, 20], [0, 0, 0], [0, 1, 0]);
mat4.perspectiveZO(this.clipFromViewMatrix, Math.PI / 3, this.canvas.width/this.canvas.height, 0.1, Infinity);
// reverse depth
mat4.mul(this.clipFromViewMatrix, mat4.fromValues(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, -1, 0,
0, 0, 1, 1,
), this.clipFromViewMatrix);
mat4.mul(this.clipFromWorldMatrix, this.clipFromViewMatrix, this.viewFromWorldMatrix);
}
private update = () => {
this.updateCamera();
this.debugDraw.beginFrame(this.canvas.width, this.canvas.height, this.mouseTracker.x, this.mouseTracker.y, this.mouseTracker.buttons);
const colorTexture = this.ctx.getCurrentTexture();
const renderPass: GPURenderPassDescriptor = {
colorAttachments: [{
view: colorTexture.createView(),
clearValue: [0.5, 0.5, 0.75, 1.0],
loadOp: 'clear',
storeOp: 'store',
}],
depthStencilAttachment: {
view: this.depthBuffer.createView(),
depthClearValue: 0.0,
depthLoadOp: 'clear',
depthStoreOp: 'store',
},
};
const cmd = this.device.createCommandEncoder();
const pass = cmd.beginRenderPass(renderPass);
this.plane.draw(this.device, this.clipFromWorldMatrix, pass);
pass.end();
// DebugDraw example.
const time = window.performance.now();
this.debugDraw.drawSphereLine(vec3.fromValues(Math.sin(time / 200) * 1.5, Math.sin(time / 300) * 0.2, Math.sin(time / 300 + 400)), 1, Red, 32, { flags: DebugDrawFlags.DepthTint });
this.debugDraw.endFrame(cmd, this.clipFromViewMatrix, this.viewFromWorldMatrix, colorTexture.createView(), this.depthBuffer.createView());
this.device.queue.submit([cmd.finish()]);
requestAnimationFrame(this.update);
};
}
function main() {
const main = new Main();
(window as any).main = main;
}
main();