1
1
// ported from https://github.com/webgpu/webgpu-samples/blob/main/src/sample/rotatingCube/main.ts
2
2
import { mat4 , vec3 } from "npm:wgpu-matrix" ;
3
- import { createWindow , mainloop } from "../mod.ts" ;
4
-
5
- const adapter = await navigator . gpu . requestAdapter ( ) ;
6
- const device = await adapter ! . requestDevice ( ) ;
3
+ import { createWindowGPU , mainloop } from "../ext/webgpu.ts" ;
7
4
8
5
const cubeVertexSize = 4 * 10 ;
9
6
const cubePositionOffset = 0 ;
@@ -56,7 +53,7 @@ export const cubeVertexArray = new Float32Array([
56
53
- 1 , 1 , - 1 , 1 , 0 , 1 , 0 , 1 , 1 , 0 ,
57
54
] ) ;
58
55
59
- const window = createWindow ( {
56
+ const window = await createWindowGPU ( {
60
57
title : "Deno Window Manager" ,
61
58
width : 512 ,
62
59
height : 512 ,
@@ -66,29 +63,25 @@ const window = createWindow({
66
63
floating : true ,
67
64
} ) ;
68
65
69
- const { width, height } = window . framebufferSize ;
70
-
71
- const surface = window . windowSurface ( ) ;
72
-
73
- const context = surface . getContext ( "webgpu" ) ;
66
+ const context = window . getContext ( "webgpu" ) ;
74
67
75
68
context . configure ( {
76
- device,
69
+ device : window . device ,
77
70
format : "bgra8unorm" ,
78
71
} ) ;
79
72
80
- const verticesBuffer = device . createBuffer ( {
73
+ const verticesBuffer = window . device . createBuffer ( {
81
74
size : cubeVertexArray . byteLength ,
82
75
usage : GPUBufferUsage . VERTEX ,
83
76
mappedAtCreation : true ,
84
77
} ) ;
85
78
new Float32Array ( verticesBuffer . getMappedRange ( ) ) . set ( cubeVertexArray ) ;
86
79
verticesBuffer . unmap ( ) ;
87
80
88
- const pipeline = device . createRenderPipeline ( {
81
+ const pipeline = window . device . createRenderPipeline ( {
89
82
layout : "auto" ,
90
83
vertex : {
91
- module : device . createShaderModule ( {
84
+ module : window . device . createShaderModule ( {
92
85
code : `
93
86
struct Uniforms {
94
87
modelViewProjectionMatrix : mat4x4<f32>,
@@ -136,7 +129,7 @@ const pipeline = device.createRenderPipeline({
136
129
] ,
137
130
} ,
138
131
fragment : {
139
- module : device . createShaderModule ( {
132
+ module : window . device . createShaderModule ( {
140
133
code : `@fragment
141
134
fn main(
142
135
@location(0) fragUV: vec2<f32>,
@@ -170,19 +163,22 @@ const pipeline = device.createRenderPipeline({
170
163
} ,
171
164
} ) ;
172
165
173
- const depthTexture = device . createTexture ( {
174
- size : [ width , height ] ,
166
+ const depthTexture = window . device . createTexture ( {
167
+ size : [
168
+ window . window . framebufferSize . width ,
169
+ window . window . framebufferSize . height ,
170
+ ] ,
175
171
format : "depth24plus" ,
176
172
usage : GPUTextureUsage . RENDER_ATTACHMENT ,
177
173
} ) ;
178
174
179
175
const uniformBufferSize = 4 * 16 ; // 4x4 matrix
180
- const uniformBuffer = device . createBuffer ( {
176
+ const uniformBuffer = window . device . createBuffer ( {
181
177
size : uniformBufferSize ,
182
178
usage : GPUBufferUsage . UNIFORM | GPUBufferUsage . COPY_DST ,
183
179
} ) ;
184
180
185
- const uniformBindGroup = device . createBindGroup ( {
181
+ const uniformBindGroup = window . device . createBindGroup ( {
186
182
layout : pipeline . getBindGroupLayout ( 0 ) ,
187
183
entries : [
188
184
{
@@ -213,7 +209,8 @@ const renderPassDescriptor: GPURenderPassDescriptor = {
213
209
depthStoreOp : "store" ,
214
210
} ,
215
211
} ;
216
- const aspect = width / height ;
212
+ const aspect = window . window . framebufferSize . width /
213
+ window . window . framebufferSize . height ;
217
214
const projectionMatrix = mat4 . perspective (
218
215
( 2 * Math . PI ) / 5 ,
219
216
aspect ,
@@ -240,7 +237,7 @@ function getTransformationMatrix() {
240
237
241
238
mainloop ( ( ) => {
242
239
const transformationMatrix = getTransformationMatrix ( ) ;
243
- device . queue . writeBuffer (
240
+ window . device . queue . writeBuffer (
244
241
uniformBuffer ,
245
242
0 ,
246
243
transformationMatrix . buffer ,
@@ -251,13 +248,13 @@ mainloop(() => {
251
248
. getCurrentTexture ( )
252
249
. createView ( ) ;
253
250
254
- const commandEncoder = device . createCommandEncoder ( ) ;
251
+ const commandEncoder = window . device . createCommandEncoder ( ) ;
255
252
const passEncoder = commandEncoder . beginRenderPass ( renderPassDescriptor ) ;
256
253
passEncoder . setPipeline ( pipeline ) ;
257
254
passEncoder . setBindGroup ( 0 , uniformBindGroup ) ;
258
255
passEncoder . setVertexBuffer ( 0 , verticesBuffer ) ;
259
256
passEncoder . draw ( cubeVertexCount ) ;
260
257
passEncoder . end ( ) ;
261
- device . queue . submit ( [ commandEncoder . finish ( ) ] ) ;
262
- surface . present ( ) ;
258
+ window . device . queue . submit ( [ commandEncoder . finish ( ) ] ) ;
259
+ window . surface . present ( ) ;
263
260
} , false ) ;
0 commit comments