|
| 1 | +import tgpu from 'typegpu'; |
| 2 | +import * as d from 'typegpu/data'; |
| 3 | + |
| 4 | +const root = await tgpu.init(); |
| 5 | +const canvas = document.querySelector('canvas') as HTMLCanvasElement; |
| 6 | +const context = canvas.getContext('webgpu') as GPUCanvasContext; |
| 7 | +const presentationFormat = navigator.gpu.getPreferredCanvasFormat(); |
| 8 | + |
| 9 | +context.configure({ |
| 10 | + device: root.device, |
| 11 | + format: presentationFormat, |
| 12 | +}); |
| 13 | + |
| 14 | +let stencilTexture = root['~unstable'].createTexture({ |
| 15 | + size: [canvas.width, canvas.height], |
| 16 | + format: 'stencil8', |
| 17 | +}).$usage('render'); |
| 18 | + |
| 19 | +const triangleData = { |
| 20 | + vertices: tgpu.const(d.arrayOf(d.vec2f, 3), [ |
| 21 | + d.vec2f(0, 0.5), |
| 22 | + d.vec2f(-0.5, -0.5), |
| 23 | + d.vec2f(0.5, -0.5), |
| 24 | + ]), |
| 25 | + uvs: tgpu.const(d.arrayOf(d.vec2f, 3), [ |
| 26 | + d.vec2f(0.5, 1), |
| 27 | + d.vec2f(0, 0), |
| 28 | + d.vec2f(1, 0), |
| 29 | + ]), |
| 30 | +}; |
| 31 | + |
| 32 | +const rotationUniform = root.createUniform(d.mat2x2f, d.mat2x2f.identity()); |
| 33 | + |
| 34 | +const vertexFn = tgpu['~unstable'].vertexFn({ |
| 35 | + in: { |
| 36 | + vid: d.builtin.vertexIndex, |
| 37 | + }, |
| 38 | + out: { |
| 39 | + position: d.builtin.position, |
| 40 | + uv: d.vec2f, |
| 41 | + }, |
| 42 | +})(({ vid }) => { |
| 43 | + const pos = triangleData.vertices.$[vid]; |
| 44 | + const uv = triangleData.uvs.$[vid]; |
| 45 | + |
| 46 | + const rotatedPos = rotationUniform.$.mul(pos); |
| 47 | + |
| 48 | + return { |
| 49 | + position: d.vec4f(rotatedPos, 0, 1), |
| 50 | + uv, |
| 51 | + }; |
| 52 | +}); |
| 53 | + |
| 54 | +const fragmentFn = tgpu['~unstable'].fragmentFn({ |
| 55 | + in: { |
| 56 | + uv: d.vec2f, |
| 57 | + }, |
| 58 | + out: d.vec4f, |
| 59 | +})(({ uv }) => d.vec4f(uv, 0, 1)); |
| 60 | + |
| 61 | +const basePipeline = root['~unstable'] |
| 62 | + .withVertex(vertexFn); |
| 63 | + |
| 64 | +const writeStencilPipeline = basePipeline |
| 65 | + .withDepthStencil({ |
| 66 | + format: 'stencil8', |
| 67 | + stencilFront: { passOp: 'replace' }, |
| 68 | + }) |
| 69 | + .createPipeline() |
| 70 | + .withStencilReference(1); |
| 71 | + |
| 72 | +const testStencilPipeline = basePipeline |
| 73 | + .withFragment(fragmentFn, { format: presentationFormat }) |
| 74 | + .withDepthStencil({ |
| 75 | + format: 'stencil8', |
| 76 | + stencilFront: { |
| 77 | + compare: 'equal', |
| 78 | + passOp: 'keep', |
| 79 | + }, |
| 80 | + }) |
| 81 | + .createPipeline() |
| 82 | + .withStencilReference(1); |
| 83 | + |
| 84 | +writeStencilPipeline |
| 85 | + .withDepthStencilAttachment({ |
| 86 | + view: stencilTexture, |
| 87 | + stencilClearValue: 0, |
| 88 | + stencilLoadOp: 'clear', |
| 89 | + stencilStoreOp: 'store', |
| 90 | + }).draw(3); |
| 91 | + |
| 92 | +let frameId: number; |
| 93 | +function frame(timestamp: number) { |
| 94 | + const rotationAngle = (timestamp / 1000) * Math.PI * 0.5; |
| 95 | + const cosA = Math.cos(rotationAngle); |
| 96 | + const sinA = Math.sin(rotationAngle); |
| 97 | + rotationUniform.write(d.mat2x2f(cosA, -sinA, sinA, cosA)); |
| 98 | + |
| 99 | + testStencilPipeline |
| 100 | + .withDepthStencilAttachment({ |
| 101 | + view: stencilTexture, |
| 102 | + stencilLoadOp: 'load', |
| 103 | + stencilStoreOp: 'store', |
| 104 | + }) |
| 105 | + .withColorAttachment({ |
| 106 | + view: context.getCurrentTexture().createView(), |
| 107 | + loadOp: 'clear', |
| 108 | + storeOp: 'store', |
| 109 | + }) |
| 110 | + .draw(3); |
| 111 | + |
| 112 | + frameId = requestAnimationFrame(frame); |
| 113 | +} |
| 114 | +frameId = requestAnimationFrame(frame); |
| 115 | + |
| 116 | +const resizeObserver = new ResizeObserver(() => { |
| 117 | + stencilTexture = root['~unstable'].createTexture({ |
| 118 | + size: [canvas.width, canvas.height], |
| 119 | + format: 'stencil8', |
| 120 | + }).$usage('render'); |
| 121 | + |
| 122 | + rotationUniform.write(d.mat2x2f.identity()); |
| 123 | + |
| 124 | + writeStencilPipeline.withDepthStencilAttachment({ |
| 125 | + view: stencilTexture, |
| 126 | + stencilClearValue: 0, |
| 127 | + stencilLoadOp: 'clear', |
| 128 | + stencilStoreOp: 'store', |
| 129 | + }).draw(3); |
| 130 | +}); |
| 131 | +resizeObserver.observe(canvas); |
| 132 | + |
| 133 | +export function onCleanup() { |
| 134 | + if (frameId) { |
| 135 | + cancelAnimationFrame(frameId); |
| 136 | + } |
| 137 | + root.destroy(); |
| 138 | +} |
0 commit comments