|
| 1 | +import '@nativescript/macos-node-api'; |
| 2 | +import { createRequire } from 'node:module'; |
| 3 | +import utils from '../../utils'; |
| 4 | + |
| 5 | +//@ts-ignore |
| 6 | +const require = createRequire(import.meta.url); |
| 7 | + |
| 8 | +const { requestAnimationFrame } = utils; |
| 9 | + |
| 10 | +import helpers from '../helpers'; |
| 11 | + |
| 12 | +const { readTextSync } = helpers; |
| 13 | + |
| 14 | +const { Canvas, GPUBufferUsage, GPUCanvasContext, GPUDevice, GPUTexture, GPUTextureUsage, ImageAsset } = require('../../canvas-napi.darwin-arm64.node'); |
| 15 | + |
| 16 | +import { mat4, vec3 } from 'wgpu-matrix'; |
| 17 | + |
| 18 | +import { cubeVertexArray, cubeVertexSize, cubeUVOffset, cubePositionOffset, cubeVertexCount } from '../meshes/cube'; |
| 19 | + |
| 20 | +export async function run(canvas) { |
| 21 | + const adapter = await navigator.gpu?.requestAdapter(); |
| 22 | + const device = (await adapter?.requestDevice()) as never; |
| 23 | + |
| 24 | + const devicePixelRatio = window.devicePixelRatio; |
| 25 | + |
| 26 | + canvas.width = canvas.clientWidth * devicePixelRatio; |
| 27 | + canvas.height = canvas.clientHeight * devicePixelRatio; |
| 28 | + |
| 29 | + const context = canvas.getContext('webgpu'); |
| 30 | + |
| 31 | + const presentationFormat = navigator.gpu.getPreferredCanvasFormat(); |
| 32 | + |
| 33 | + const basicVertWGSL = readTextSync('./shaders/basic.vert.wgsl'); |
| 34 | + const sampleCubemapWGSL = readTextSync('./shaders/sampleCubemap.frag.wgsl'); |
| 35 | + |
| 36 | + context.configure({ |
| 37 | + device, |
| 38 | + format: presentationFormat, |
| 39 | + presentMode: 'fifo', |
| 40 | + }); |
| 41 | + |
| 42 | + // Create a vertex buffer from the cube data. |
| 43 | + const verticesBuffer = device.createBuffer({ |
| 44 | + size: cubeVertexArray.byteLength, |
| 45 | + usage: GPUBufferUsage.VERTEX, |
| 46 | + mappedAtCreation: true, |
| 47 | + }); |
| 48 | + |
| 49 | + new Float32Array(verticesBuffer.getMappedRange()).set(cubeVertexArray); |
| 50 | + verticesBuffer.unmap(); |
| 51 | + |
| 52 | + const pipeline = device.createRenderPipeline({ |
| 53 | + layout: 'auto', |
| 54 | + vertex: { |
| 55 | + module: device.createShaderModule({ |
| 56 | + code: basicVertWGSL, |
| 57 | + }), |
| 58 | + buffers: [ |
| 59 | + { |
| 60 | + arrayStride: cubeVertexSize, |
| 61 | + attributes: [ |
| 62 | + { |
| 63 | + // position |
| 64 | + shaderLocation: 0, |
| 65 | + offset: cubePositionOffset, |
| 66 | + format: 'float32x4', |
| 67 | + }, |
| 68 | + { |
| 69 | + // uv |
| 70 | + shaderLocation: 1, |
| 71 | + offset: cubeUVOffset, |
| 72 | + format: 'float32x2', |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + ], |
| 77 | + entryPoint: 'main', |
| 78 | + }, |
| 79 | + fragment: { |
| 80 | + module: device.createShaderModule({ |
| 81 | + code: sampleCubemapWGSL, |
| 82 | + }), |
| 83 | + targets: [ |
| 84 | + { |
| 85 | + format: presentationFormat, |
| 86 | + }, |
| 87 | + ], |
| 88 | + entryPoint: 'main', |
| 89 | + }, |
| 90 | + primitive: { |
| 91 | + topology: 'triangle-list', |
| 92 | + |
| 93 | + // Since we are seeing from inside of the cube |
| 94 | + // and we are using the regular cube geomtry data with outward-facing normals, |
| 95 | + // the cullMode should be 'front' or 'none'. |
| 96 | + cullMode: 'none', |
| 97 | + }, |
| 98 | + |
| 99 | + // Enable depth testing so that the fragment closest to the camera |
| 100 | + // is rendered in front. |
| 101 | + depthStencil: { |
| 102 | + depthWriteEnabled: true, |
| 103 | + depthCompare: 'less', |
| 104 | + format: 'depth24plus', |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + const depthTexture = device.createTexture({ |
| 109 | + size: [canvas.width as number, canvas.height as number], |
| 110 | + format: 'depth24plus', |
| 111 | + usage: GPUTextureUsage.RENDER_ATTACHMENT, |
| 112 | + }); |
| 113 | + |
| 114 | + // Fetch the 6 separate images for negative/positive x, y, z axis of a cubemap |
| 115 | + // and upload it into a GPUTexture. |
| 116 | + let cubemapTexture: GPUTexture; |
| 117 | + let imageBitmaps; |
| 118 | + { |
| 119 | + // The order of the array layers is [+X, -X, +Y, -Y, +Z, -Z] |
| 120 | + const imgSrcs = ['posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg']; |
| 121 | + const promises = imgSrcs.map(async (src) => { |
| 122 | + // const response = await fetch(src); |
| 123 | + // return createImageBitmap(await response.blob()); |
| 124 | + const asset = new ImageAsset(); |
| 125 | + await asset.fromFile(import.meta.resolve('../assets/cubemap/' + src).replace('file://', '')); |
| 126 | + return asset; |
| 127 | + }); |
| 128 | + imageBitmaps = await Promise.all(promises); |
| 129 | + |
| 130 | + cubemapTexture = device.createTexture({ |
| 131 | + dimension: '2d', |
| 132 | + // Create a 2d array texture. |
| 133 | + // Assume each image has the same size. |
| 134 | + size: [imageBitmaps[0].width, imageBitmaps[0].height, 6], |
| 135 | + format: 'rgba8unorm', |
| 136 | + usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT, |
| 137 | + }); |
| 138 | + |
| 139 | + for (let i = 0; i < imageBitmaps.length; i++) { |
| 140 | + const imageBitmap = imageBitmaps[i]; |
| 141 | + device.queue.copyExternalImageToTexture({ source: imageBitmap }, { texture: cubemapTexture, origin: [0, 0, i] }, [imageBitmap.width, imageBitmap.height]); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + const uniformBufferSize = 4 * 16; // 4x4 matrix |
| 146 | + const uniformBuffer = device.createBuffer({ |
| 147 | + size: uniformBufferSize, |
| 148 | + usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, |
| 149 | + }); |
| 150 | + |
| 151 | + const sampler = device.createSampler({ |
| 152 | + magFilter: 'linear', |
| 153 | + minFilter: 'linear', |
| 154 | + }); |
| 155 | + |
| 156 | + const uniformBindGroup = device.createBindGroup({ |
| 157 | + layout: pipeline.getBindGroupLayout(0), |
| 158 | + entries: [ |
| 159 | + { |
| 160 | + binding: 0, |
| 161 | + resource: { |
| 162 | + buffer: uniformBuffer, |
| 163 | + offset: 0, |
| 164 | + size: uniformBufferSize, |
| 165 | + }, |
| 166 | + }, |
| 167 | + { |
| 168 | + binding: 1, |
| 169 | + resource: sampler, |
| 170 | + }, |
| 171 | + { |
| 172 | + binding: 2, |
| 173 | + resource: cubemapTexture.createView({ |
| 174 | + dimension: 'cube', |
| 175 | + }), |
| 176 | + }, |
| 177 | + ], |
| 178 | + }); |
| 179 | + |
| 180 | + const renderPassDescriptor = { |
| 181 | + colorAttachments: [ |
| 182 | + { |
| 183 | + view: undefined, // Assigned later |
| 184 | + loadOp: 'clear', |
| 185 | + storeOp: 'store', |
| 186 | + }, |
| 187 | + ], |
| 188 | + depthStencilAttachment: { |
| 189 | + view: depthTexture.createView(), |
| 190 | + |
| 191 | + depthClearValue: 1.0, |
| 192 | + depthLoadOp: 'clear', |
| 193 | + depthStoreOp: 'store', |
| 194 | + }, |
| 195 | + }; |
| 196 | + |
| 197 | + const aspect = (canvas.width as number) / (canvas.height as number); |
| 198 | + const projectionMatrix = mat4.perspective((2 * Math.PI) / 5, aspect, 1, 3000); |
| 199 | + |
| 200 | + const modelMatrix = mat4.scaling(vec3.fromValues(1000, 1000, 1000)); |
| 201 | + const modelViewProjectionMatrix = mat4.create(); |
| 202 | + const viewMatrix = mat4.identity(); |
| 203 | + |
| 204 | + const tmpMat4 = mat4.create(); |
| 205 | + |
| 206 | + // Comppute camera movement: |
| 207 | + // It rotates around Y axis with a slight pitch movement. |
| 208 | + function updateTransformationMatrix() { |
| 209 | + const now = Date.now() / 800; |
| 210 | + |
| 211 | + mat4.rotate(viewMatrix, vec3.fromValues(1, 0, 0), (Math.PI / 10) * Math.sin(now), tmpMat4); |
| 212 | + mat4.rotate(tmpMat4, vec3.fromValues(0, 1, 0), now * 0.2, tmpMat4); |
| 213 | + |
| 214 | + mat4.multiply(tmpMat4, modelMatrix, modelViewProjectionMatrix); |
| 215 | + mat4.multiply(projectionMatrix, modelViewProjectionMatrix, modelViewProjectionMatrix); |
| 216 | + } |
| 217 | + |
| 218 | + function frame() { |
| 219 | + const texture = context.getCurrentTexture(); |
| 220 | + |
| 221 | + if (!texture) { |
| 222 | + requestAnimationFrame(frame); |
| 223 | + return; |
| 224 | + } |
| 225 | + |
| 226 | + updateTransformationMatrix(); |
| 227 | + device.queue.writeBuffer(uniformBuffer, 0, modelViewProjectionMatrix.buffer, modelViewProjectionMatrix.byteOffset, modelViewProjectionMatrix.byteLength); |
| 228 | + |
| 229 | + renderPassDescriptor.colorAttachments[0].view = texture.createView(); |
| 230 | + |
| 231 | + const commandEncoder = device.createCommandEncoder(); |
| 232 | + const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor as never); |
| 233 | + passEncoder.setPipeline(pipeline); |
| 234 | + passEncoder.setVertexBuffer(0, verticesBuffer); |
| 235 | + passEncoder.setBindGroup(0, uniformBindGroup); |
| 236 | + passEncoder.draw(cubeVertexCount); |
| 237 | + passEncoder.end(); |
| 238 | + device.queue.submit([commandEncoder.finish()]); |
| 239 | + |
| 240 | + context.presentSurface(); |
| 241 | + |
| 242 | + requestAnimationFrame(frame); |
| 243 | + } |
| 244 | + requestAnimationFrame(frame); |
| 245 | +} |
0 commit comments