@@ -14,13 +14,13 @@ const vertices = new Float32Array([
1414
1515const shaders = `
1616struct 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