-
Notifications
You must be signed in to change notification settings - Fork 1
/
hello-blend.html
151 lines (132 loc) · 3.44 KB
/
hello-blend.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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<script src='webgpu.js'></script>
</head>
<body>
<canvas id='e_canvas'>
<script>
const VS_SRC = `
#version 300 es
layout(location = 0) in vec2 a_pos;
layout(location = 1) in lowp vec4 a_color;
out lowp vec4 v_color;
void main() {
gl_Position = vec4(a_pos, 0, 1);
v_color = a_color;
}
`.trim();
const FS_SRC = `
#version 300 es
in lowp vec4 v_color;
out lowp vec4 fd_color;
void main() {
fd_color = v_color;
}
`.trim();
(async () => {
const adapter = await navigator.gpu.requestAdapter();
const dev = await adapter.requestDevice({
extensions: adapter.extensions,
});
const q = dev.getQueue();
const sc_context = e_canvas.getContext('gpu');
const sc = sc_context.configureSwapChain({
device: dev,
format: 'rgba8unorm',
});
const tex = sc.getCurrentTexture();
const t_view = tex.createDefaultView();
// -
function make_buffer(dev, data, usage) {
let [buf, mab] = dev.createBufferMapped({
size: data.byteLength,
usage: usage,
});
new data.constructor(mab).set(data);
buf.unmap();
return buf;
}
const v_data = new Float32Array([
-0.5, -0.5, 0,
0.5, -0.5, 0,
-0.5, 0.5, 0,
0.5, 0.5, 0,
]);
const v_data_u8 = new Uint8Array(v_data.buffer);
v_data_u8.set([255, 0, 0, 127], 12*0+4*2);
v_data_u8.set([0, 255, 0, 127], 12*1+4*2);
v_data_u8.set([0, 0, 255, 127], 12*2+4*2);
v_data_u8.set([255, 255, 0, 127], 12*3+4*2);
const vbuf = make_buffer(dev, v_data, GPUBufferUsage.VERTEX);
const ibuf = make_buffer(dev, new Uint16Array([0, 1, 3]), GPUBufferUsage.INDEX);
// -
const v_mod = dev.createShaderModule({code: VS_SRC});
const f_mod = dev.createShaderModule({code: FS_SRC});
const layout = dev.createPipelineLayout({
bindGroupLayouts: [],
});
const rp = dev.createRenderPipeline({
layout: layout,
vertexStage: {
module: v_mod,
entryPoint: 'main',
},
fragmentStage: {
module: f_mod,
entryPoint: 'main',
},
primitiveTopology: 'triangle-list',
rasterizationState: {},
colorStates: [{
format: 'rgba8unorm',
alphaBlend: {
srcFactor: 'one',
dstFactor: 'one-minus-src-alpha',
},
colorBlend: {
srcFactor: 'src-alpha',
dstFactor: 'one-minus-src-alpha',
},
}],
vertexInput: {
indexFormat: 'uint16',
vertexBuffers: [{
stride: 4*3,
attributeSet: [{
format: 'float2',
shaderLocation: 0,
offset: 4*0,
},{
format: 'uchar4norm',
shaderLocation: 1,
offset: 4*2,
}],
}],
},
});
// -
const cmd_enc = dev.createCommandEncoder({});
const pass = cmd_enc.beginRenderPass({
colorAttachments: [
{
attachment: t_view,
loadOp: 'clear',
storeOp: 'store',
clearColor: [0.1, 0.3, 0.1, 1],
}
],
});
pass.setPipeline(rp);
pass.setVertexBuffers(0, [vbuf], [0]);
pass.draw(3, 1, 0, 0);
pass.setIndexBuffer(ibuf, 0);
pass.drawIndexed(3, 1, 0, 0, 0);
pass.endPass();
const cbuf = cmd_enc.finish();
q.submit([cbuf]);
})();
</script>
</body>
</html>