-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtriangle_rotate.html
161 lines (146 loc) · 5.17 KB
/
triangle_rotate.html
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
<html>
<head>
<title>WebGPU triangle rotate example</title>
<style>
body {
margin: 0;
background-color: #000;
color: #fff;
overscroll-behavior: none;
}
</style>
</head>
<body>
<script type="module">
import glslangModule from './js/glslang.js';
import { mat4 } from './js/glmatrix.js';
// @TODO: Detect WebGPU capability
const run = async () => {
const glslang = await glslangModule();
const vertexShaderCode = `#version 450
layout(set=0, binding=0) uniform Uniforms {
mat4 modelMatrix;
} uniforms;
const vec2 pos[3] = vec2[3](
vec2(-0.5f, -0.5f),
vec2(0.5f, -0.5f),
vec2(0.5f, 0.5f)
);
void main() {
gl_Position = uniforms.modelMatrix * vec4(pos[gl_VertexIndex], 0.0, 1.0);
}
`;
const fragmentShaderCode = `#version 450
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4(1.0, 0.0, 0.0, 1.0);
}
`;
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
const context = canvas.getContext('gpupresent');
const format = 'bgra8unorm'
const swapChain = context.configureSwapChain({
device,
format
});
const uniformsBindGroupLayout = device.createBindGroupLayout({
bindings: [{
binding: 0,
visibility: 1,
type: 'uniform-buffer'
}]
});
const uniformBuffer = device.createBuffer({
size: 4 * 16, // 4x4 float32 matrix
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
const uniformBindGroup = device.createBindGroup({
layout: uniformsBindGroupLayout,
bindings: [{
binding: 0,
resource: {
buffer: uniformBuffer
}
}]
});
const modelMatrix = mat4.identity(mat4.create());
let rotation = 0;
const sampleCount = 4;
const pipeline = device.createRenderPipeline({
layout: device.createPipelineLayout({bindGroupLayouts: [uniformsBindGroupLayout]}),
vertexStage: {
module: device.createShaderModule({
code: glslang.compileGLSL(vertexShaderCode, 'vertex'),
source: vertexShaderCode,
transform: source => glslang.compileGLSL(source, 'vertex')
}),
entryPoint: 'main'
},
fragmentStage: {
module: device.createShaderModule({
code: glslang.compileGLSL(fragmentShaderCode, 'fragment'),
source: fragmentShaderCode,
transform: source => glslang.compileGLSL(source, 'fragment')
}),
entryPoint: 'main'
},
primitiveTopology: 'triangle-list',
colorStates: [{
format
}],
sampleCount
});
const texture = device.createTexture({
size: {
width: Math.floor(window.innerWidth * window.devicePixelRatio),
height: Math.floor(window.innerHeight * window.devicePixelRatio),
depth: 1
},
sampleCount,
format,
usage: GPUTextureUsage.OUTPUT_ATTACHMENT
});
const attachment = texture.createView();
const frame = () => {
requestAnimationFrame(frame);
rotate(modelMatrix);
uniformBuffer.setSubData(0, modelMatrix);
const commandEncoder = device.createCommandEncoder({});
const renderPassDescriptor = {
colorAttachments: [{
attachment: attachment,
resolveTarget: swapChain.getCurrentTexture().createView(),
loadValue: {r: 0.0, g: 0.0, b: 0.0, a: 1.0}
}]
};
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
passEncoder.setPipeline(pipeline);
passEncoder.setBindGroup(0, uniformBindGroup);
passEncoder.draw(3, 1, 0, 0);
passEncoder.endPass();
device.defaultQueue.submit([commandEncoder.finish()]);
};
const rotate = matrix => {
mat4.fromZRotation(matrix, rotation * Math.PI / 180);
rotation++;
return matrix;
};
const onResize = event => {
const width = window.innerWidth;
const height = window.innerHeight;
canvas.width = Math.floor(window.innerWidth * window.devicePixelRatio);
canvas.height = Math.floor(window.innerHeight * window.devicePixelRatio);
canvas.style.width = width + 'px';
canvas.style.height = height + 'px';
};
window.addEventListener('resize', onResize, false);
onResize();
frame();
};
run();
</script>
</body>
</html>