Skip to content

Commit 8aa11fb

Browse files
Code updates after SME review
1 parent f6bf944 commit 8aa11fb

2 files changed

Lines changed: 20 additions & 24 deletions

File tree

webgpu-compute-demo/script.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// Compute shader
22

33
const shader = `
4-
@group(0) @binding(1)
4+
@group(0) @binding(0)
55
var<storage, read_write> output: array<f32>;
66
77
@compute @workgroup_size(64)
88
fn main(
99
1010
@builtin(global_invocation_id)
11-
global_id : vec3<u32>,
11+
global_id : vec3u,
1212
1313
@builtin(local_invocation_id)
14-
local_id : vec3<u32>,
14+
local_id : vec3u,
1515
1616
) {
1717
output[global_id.x] =
@@ -63,22 +63,22 @@ async function init() {
6363
const bindGroupLayout =
6464
device.createBindGroupLayout({
6565
entries: [{
66-
binding: 1,
66+
binding: 0,
6767
visibility: GPUShaderStage.COMPUTE,
6868
buffer: {
69-
type: "storage",
69+
type: "storage"
7070
}
7171
}]
7272
});
7373

7474
const bindGroup = device.createBindGroup({
7575
layout: bindGroupLayout,
7676
entries: [{
77-
binding: 1,
77+
binding: 0,
7878
resource: {
79-
buffer: output,
80-
},
81-
}],
79+
buffer: output,
80+
}
81+
}]
8282
});
8383

8484
const computePipeline = device.createComputePipeline({

webgpu-render-demo/script.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ const vertices = new Float32Array([
1414

1515
const shaders = `
1616
struct VertexOut {
17-
@builtin(position) position : vec4<f32>,
18-
@location(0) color : vec4<f32>
17+
@builtin(position) position : vec4f,
18+
@location(0) color : vec4f
1919
}
2020
2121
@vertex
22-
fn vertex_main(@location(0) position: vec4<f32>,
23-
@location(1) color: vec4<f32>) -> VertexOut
22+
fn vertex_main(@location(0) position: vec4f,
23+
@location(1) color: vec4f) -> VertexOut
2424
{
2525
var output : VertexOut;
2626
output.position = position;
@@ -29,7 +29,7 @@ fn vertex_main(@location(0) position: vec4<f32>,
2929
}
3030
3131
@fragment
32-
fn fragment_main(fragData: VertexOut) -> @location(0) vec4<f32>
32+
fn fragment_main(fragData: VertexOut) -> @location(0) vec4f
3333
{
3434
return fragData.color;
3535
}
@@ -65,25 +65,21 @@ async function init() {
6565
const canvas = document.querySelector('#gpuCanvas');
6666
const context = canvas.getContext('webgpu');
6767

68-
// Configure swap chain, link the GPU to the canvas
6968
context.configure({
7069
device: device,
71-
format: 'bgra8unorm',
70+
format: navigator.gpu.getPreferredCanvasFormat(),
7271
alphaMode: 'premultiplied'
7372
});
7473

7574
// 4: Create vertex buffer to contain vertex data
7675
const vertexBuffer = device.createBuffer({
7776
size: vertices.byteLength, // make it big enough to store vertices in
7877
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
79-
mappedAtCreation: true // mapped at creation
80-
});
81-
82-
// Store mapped range of vertex buffer in a typed array, then copy the triangle vertex data into it
83-
new Float32Array(vertexBuffer.getMappedRange()).set(vertices);
78+
});
8479

85-
// Unmap the vertex buffer; we are done changing it, and the new contents are automatically copied to the GPU
86-
vertexBuffer.unmap();
80+
// Store vertices in a typed array, then copy the vertex data over to the GPUBuffer using writeBuffer() utility function
81+
const array = new Float32Array(vertices);
82+
device.queue.writeBuffer(vertexBuffer, 0, array, 0, array.length);
8783

8884
// 5: Create a GPUVertexBufferLayout and GPURenderPipelineDescriptor to provide a definition of our render pipline
8985
const vertexBuffers = [{
@@ -110,7 +106,7 @@ async function init() {
110106
module: shaderModule,
111107
entryPoint: 'fragment_main',
112108
targets: [{
113-
format: 'bgra8unorm'
109+
format: navigator.gpu.getPreferredCanvasFormat()
114110
}]
115111
},
116112
primitive: {

0 commit comments

Comments
 (0)