|
| 1 | +import React from "react"; |
| 2 | +import { StyleSheet, View } from "react-native"; |
| 3 | +import { Canvas } from "react-native-wgpu"; |
| 4 | + |
| 5 | +import { useWebGPU } from "../components/useWebGPU"; |
| 6 | + |
| 7 | +import { shaderCode } from "./shaders"; |
| 8 | + |
| 9 | +const rand = (min?: number, max?: number) => { |
| 10 | + if (min === undefined) { |
| 11 | + min = 0; |
| 12 | + max = 1; |
| 13 | + } else if (max === undefined) { |
| 14 | + max = min; |
| 15 | + min = 0; |
| 16 | + } |
| 17 | + return min + Math.random() * (max - min); |
| 18 | +}; |
| 19 | + |
| 20 | +function createCircleVertices({ |
| 21 | + radius = 1, |
| 22 | + numSubdivisions = 24, |
| 23 | + innerRadius = 0, |
| 24 | + startAngle = 0, |
| 25 | + endAngle = Math.PI * 2, |
| 26 | +} = {}) { |
| 27 | + const numVertices = numSubdivisions * 3 * 2; |
| 28 | + const vertexData = new Float32Array(numSubdivisions * 2 * 3 * 2); |
| 29 | + |
| 30 | + let offset = 0; |
| 31 | + const addVertex = (x: number, y: number) => { |
| 32 | + vertexData[offset++] = x; |
| 33 | + vertexData[offset++] = y; |
| 34 | + }; |
| 35 | + |
| 36 | + for (let i = 0; i < numSubdivisions; ++i) { |
| 37 | + const angle1 = |
| 38 | + startAngle + ((i + 0) * (endAngle - startAngle)) / numSubdivisions; |
| 39 | + const angle2 = |
| 40 | + startAngle + ((i + 1) * (endAngle - startAngle)) / numSubdivisions; |
| 41 | + |
| 42 | + const c1 = Math.cos(angle1); |
| 43 | + const s1 = Math.sin(angle1); |
| 44 | + const c2 = Math.cos(angle2); |
| 45 | + const s2 = Math.sin(angle2); |
| 46 | + |
| 47 | + // first triangle |
| 48 | + addVertex(c1 * radius, s1 * radius); |
| 49 | + addVertex(c2 * radius, s2 * radius); |
| 50 | + addVertex(c1 * innerRadius, s1 * innerRadius); |
| 51 | + |
| 52 | + // second triangle |
| 53 | + addVertex(c1 * innerRadius, s1 * innerRadius); |
| 54 | + addVertex(c2 * radius, s2 * radius); |
| 55 | + addVertex(c2 * innerRadius, s2 * innerRadius); |
| 56 | + } |
| 57 | + |
| 58 | + return { |
| 59 | + vertexData, |
| 60 | + numVertices, |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +export function StorageBufferVertices() { |
| 65 | + const ref = useWebGPU(({ context, device, presentationFormat, canvas }) => { |
| 66 | + const module = device.createShaderModule({ |
| 67 | + code: shaderCode, |
| 68 | + }); |
| 69 | + |
| 70 | + const pipeline = device.createRenderPipeline({ |
| 71 | + label: "storage buffer vertices", |
| 72 | + layout: "auto", |
| 73 | + vertex: { |
| 74 | + module, |
| 75 | + }, |
| 76 | + fragment: { |
| 77 | + module, |
| 78 | + targets: [{ format: presentationFormat }], |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + const kNumObjects = 100; |
| 83 | + const objectInfos: { scale: number }[] = []; |
| 84 | + |
| 85 | + // create 2 storage buffers |
| 86 | + const staticUnitSize = |
| 87 | + 4 * 4 + // color is 4 32bit floats (4bytes each) |
| 88 | + 2 * 4 + // offset is 2 32bit floats (4bytes each) |
| 89 | + 2 * 4; // padding |
| 90 | + const changingUnitSize = 2 * 4; // scale is 2 32bit floats (4bytes each) |
| 91 | + const staticStorageBufferSize = staticUnitSize * kNumObjects; |
| 92 | + const changingStorageBufferSize = changingUnitSize * kNumObjects; |
| 93 | + |
| 94 | + const staticStorageBuffer = device.createBuffer({ |
| 95 | + label: "static storage for objects", |
| 96 | + size: staticStorageBufferSize, |
| 97 | + usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST, |
| 98 | + }); |
| 99 | + |
| 100 | + const changingStorageBuffer = device.createBuffer({ |
| 101 | + label: "changing storage for objects", |
| 102 | + size: changingStorageBufferSize, |
| 103 | + usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST, |
| 104 | + }); |
| 105 | + |
| 106 | + // offsets to the various uniform values in float32 indices |
| 107 | + const kColorOffset = 0; |
| 108 | + const kOffsetOffset = 4; |
| 109 | + const kScaleOffset = 0; |
| 110 | + |
| 111 | + const staticStorageValues = new Float32Array(staticStorageBufferSize / 4); |
| 112 | + for (let i = 0; i < kNumObjects; ++i) { |
| 113 | + const staticOffset = i * (staticUnitSize / 4); |
| 114 | + |
| 115 | + // These are only set once so set them now |
| 116 | + staticStorageValues.set( |
| 117 | + [rand(), rand(), rand(), 1], |
| 118 | + staticOffset + kColorOffset, |
| 119 | + ); // set the color |
| 120 | + staticStorageValues.set( |
| 121 | + [rand(-0.9, 0.9), rand(-0.9, 0.9)], |
| 122 | + staticOffset + kOffsetOffset, |
| 123 | + ); // set the offset |
| 124 | + |
| 125 | + objectInfos.push({ |
| 126 | + scale: rand(0.2, 0.5), |
| 127 | + }); |
| 128 | + } |
| 129 | + device.queue.writeBuffer(staticStorageBuffer, 0, staticStorageValues); |
| 130 | + |
| 131 | + // a typed array we can use to update the changingStorageBuffer |
| 132 | + const storageValues = new Float32Array(changingStorageBufferSize / 4); |
| 133 | + |
| 134 | + // setup a storage buffer with vertex data |
| 135 | + const { vertexData, numVertices } = createCircleVertices({ |
| 136 | + radius: 0.5, |
| 137 | + innerRadius: 0.25, |
| 138 | + }); |
| 139 | + const vertexStorageBuffer = device.createBuffer({ |
| 140 | + label: "storage buffer vertices", |
| 141 | + size: vertexData.byteLength, |
| 142 | + usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST, |
| 143 | + }); |
| 144 | + device.queue.writeBuffer(vertexStorageBuffer, 0, vertexData); |
| 145 | + |
| 146 | + const bindGroup = device.createBindGroup({ |
| 147 | + label: "bind group for objects", |
| 148 | + layout: pipeline.getBindGroupLayout(0), |
| 149 | + entries: [ |
| 150 | + { binding: 0, resource: staticStorageBuffer }, |
| 151 | + { binding: 1, resource: changingStorageBuffer }, |
| 152 | + { binding: 2, resource: vertexStorageBuffer }, |
| 153 | + ], |
| 154 | + }); |
| 155 | + |
| 156 | + const renderPassDescriptor: GPURenderPassDescriptor = { |
| 157 | + label: "our basic canvas renderPass", |
| 158 | + colorAttachments: [ |
| 159 | + { |
| 160 | + view: context.getCurrentTexture().createView(), |
| 161 | + clearValue: [0.3, 0.3, 0.3, 1], |
| 162 | + loadOp: "clear", |
| 163 | + storeOp: "store", |
| 164 | + }, |
| 165 | + ], |
| 166 | + }; |
| 167 | + |
| 168 | + // Set the uniform values in our JavaScript side Float32Array |
| 169 | + const aspect = canvas.width / canvas.height; |
| 170 | + |
| 171 | + // set the scales for each object |
| 172 | + objectInfos.forEach(({ scale }, ndx) => { |
| 173 | + const offset = ndx * (changingUnitSize / 4); |
| 174 | + storageValues.set([scale / aspect, scale], offset + kScaleOffset); |
| 175 | + }); |
| 176 | + // upload all scales at once |
| 177 | + device.queue.writeBuffer(changingStorageBuffer, 0, storageValues); |
| 178 | + |
| 179 | + const encoder = device.createCommandEncoder(); |
| 180 | + const pass = encoder.beginRenderPass(renderPassDescriptor); |
| 181 | + pass.setPipeline(pipeline); |
| 182 | + pass.setBindGroup(0, bindGroup); |
| 183 | + pass.draw(numVertices, kNumObjects); |
| 184 | + pass.end(); |
| 185 | + |
| 186 | + const commandBuffer = encoder.finish(); |
| 187 | + device.queue.submit([commandBuffer]); |
| 188 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 189 | + (context as any).present(); |
| 190 | + }); |
| 191 | + |
| 192 | + return ( |
| 193 | + <View style={style.container}> |
| 194 | + <Canvas ref={ref} style={style.webgpu} /> |
| 195 | + </View> |
| 196 | + ); |
| 197 | +} |
| 198 | + |
| 199 | +const style = StyleSheet.create({ |
| 200 | + container: { |
| 201 | + flex: 1, |
| 202 | + }, |
| 203 | + webgpu: { |
| 204 | + flex: 1, |
| 205 | + }, |
| 206 | +}); |
0 commit comments