|
| 1 | +import tgpu, { prepareDispatch } from 'typegpu'; |
| 2 | +import * as d from 'typegpu/data'; |
| 3 | + |
| 4 | +const root = await tgpu.init({ |
| 5 | + unstable_logOptions: { |
| 6 | + logCountLimit: 32, |
| 7 | + logSizeLimit: 32, |
| 8 | + }, |
| 9 | +}); |
| 10 | + |
| 11 | +// #region Example controls and cleanup |
| 12 | + |
| 13 | +export const controls = { |
| 14 | + 'One argument': { |
| 15 | + onButtonClick: () => |
| 16 | + prepareDispatch(root, () => { |
| 17 | + 'kernel'; |
| 18 | + console.log(d.u32(321)); |
| 19 | + })(), |
| 20 | + }, |
| 21 | + 'Multiple arguments': { |
| 22 | + onButtonClick: () => |
| 23 | + prepareDispatch(root, () => { |
| 24 | + 'kernel'; |
| 25 | + console.log(d.u32(1), d.vec3u(2, 3, 4), d.u32(5), d.u32(6)); |
| 26 | + })(), |
| 27 | + }, |
| 28 | + 'String literals': { |
| 29 | + onButtonClick: () => |
| 30 | + prepareDispatch(root, () => { |
| 31 | + 'kernel'; |
| 32 | + console.log(d.u32(2), 'plus', d.u32(3), 'equals', d.u32(5)); |
| 33 | + })(), |
| 34 | + }, |
| 35 | + 'Different types': { |
| 36 | + onButtonClick: () => |
| 37 | + prepareDispatch(root, () => { |
| 38 | + 'kernel'; |
| 39 | + console.log(d.bool(true)); |
| 40 | + console.log(d.u32(3_000_000_000)); |
| 41 | + console.log(d.vec2u(1, 2)); |
| 42 | + console.log(d.vec3u(1, 2, 3)); |
| 43 | + console.log(d.vec4u(1, 2, 3, 4)); |
| 44 | + })(), |
| 45 | + }, |
| 46 | + 'Two logs': { |
| 47 | + onButtonClick: () => |
| 48 | + prepareDispatch(root, () => { |
| 49 | + 'kernel'; |
| 50 | + console.log('First log.'); |
| 51 | + console.log('Second log.'); |
| 52 | + })(), |
| 53 | + }, |
| 54 | + 'Two threads': { |
| 55 | + onButtonClick: () => |
| 56 | + prepareDispatch(root, (x) => { |
| 57 | + 'kernel'; |
| 58 | + console.log('Log from thread', x); |
| 59 | + })(2), |
| 60 | + }, |
| 61 | + '100 dispatches': { |
| 62 | + onButtonClick: async () => { |
| 63 | + const indexUniform = root.createUniform(d.u32); |
| 64 | + const dispatch = prepareDispatch(root, () => { |
| 65 | + 'kernel'; |
| 66 | + console.log('Log from dispatch', indexUniform.$); |
| 67 | + }); |
| 68 | + for (let i = 0; i < 100; i++) { |
| 69 | + indexUniform.write(i); |
| 70 | + dispatch(); |
| 71 | + console.log(`dispatched ${i}`); |
| 72 | + } |
| 73 | + }, |
| 74 | + }, |
| 75 | + 'Varying size logs': { |
| 76 | + onButtonClick: async () => { |
| 77 | + const logCountUniform = root.createUniform(d.u32); |
| 78 | + const dispatch = prepareDispatch(root, () => { |
| 79 | + 'kernel'; |
| 80 | + for (let i = d.u32(); i < logCountUniform.$; i++) { |
| 81 | + console.log('Log index', d.u32(i) + 1, 'out of', logCountUniform.$); |
| 82 | + } |
| 83 | + }); |
| 84 | + logCountUniform.write(3); |
| 85 | + dispatch(); |
| 86 | + logCountUniform.write(1); |
| 87 | + dispatch(); |
| 88 | + }, |
| 89 | + }, |
| 90 | + 'Render pipeline': { |
| 91 | + onButtonClick: () => { |
| 92 | + const mainVertex = tgpu['~unstable'].vertexFn({ |
| 93 | + in: { vertexIndex: d.builtin.vertexIndex }, |
| 94 | + out: { pos: d.builtin.position }, |
| 95 | + })((input) => { |
| 96 | + const positions = [ |
| 97 | + d.vec2f(0, 0.5), |
| 98 | + d.vec2f(-0.5, -0.5), |
| 99 | + d.vec2f(0.5, -0.5), |
| 100 | + ]; |
| 101 | + |
| 102 | + return { pos: d.vec4f(positions[input.vertexIndex], 0, 1) }; |
| 103 | + }); |
| 104 | + |
| 105 | + const mainFragment = tgpu['~unstable'].fragmentFn({ |
| 106 | + in: { pos: d.builtin.position }, |
| 107 | + out: d.vec4f, |
| 108 | + })(({ pos }) => { |
| 109 | + console.log('X:', d.u32(pos.x), 'Y:', d.u32(pos.y)); |
| 110 | + return d.vec4f(0.769, 0.392, 1.0, 1); |
| 111 | + }); |
| 112 | + |
| 113 | + const presentationFormat = navigator.gpu.getPreferredCanvasFormat(); |
| 114 | + const canvas = document.querySelector('canvas') as HTMLCanvasElement; |
| 115 | + const context = canvas.getContext('webgpu') as GPUCanvasContext; |
| 116 | + |
| 117 | + context.configure({ |
| 118 | + device: root.device, |
| 119 | + format: presentationFormat, |
| 120 | + alphaMode: 'premultiplied', |
| 121 | + }); |
| 122 | + |
| 123 | + const pipeline = root['~unstable'] |
| 124 | + .withVertex(mainVertex, {}) |
| 125 | + .withFragment(mainFragment, { format: presentationFormat }) |
| 126 | + .createPipeline(); |
| 127 | + |
| 128 | + pipeline |
| 129 | + .withColorAttachment({ |
| 130 | + view: context.getCurrentTexture().createView(), |
| 131 | + clearValue: [0, 0, 0, 0], |
| 132 | + loadOp: 'clear', |
| 133 | + storeOp: 'store', |
| 134 | + }) |
| 135 | + .draw(3); |
| 136 | + }, |
| 137 | + }, |
| 138 | + 'Too many logs': { |
| 139 | + onButtonClick: () => |
| 140 | + prepareDispatch(root, (x) => { |
| 141 | + 'kernel'; |
| 142 | + console.log('Log 1 from thread', x); |
| 143 | + console.log('Log 2 from thread', x); |
| 144 | + console.log('Log 3 from thread', x); |
| 145 | + })(16), |
| 146 | + }, |
| 147 | + 'Too much data': { |
| 148 | + onButtonClick: () => { |
| 149 | + const dispatch = prepareDispatch(root, () => { |
| 150 | + 'kernel'; |
| 151 | + console.log(d.vec3u(), d.vec3u(), d.vec3u()); |
| 152 | + }); |
| 153 | + try { |
| 154 | + dispatch(); |
| 155 | + } catch (err) { |
| 156 | + console.log(err); |
| 157 | + } |
| 158 | + }, |
| 159 | + }, |
| 160 | +}; |
| 161 | + |
| 162 | +export function onCleanup() { |
| 163 | + root.destroy(); |
| 164 | +} |
| 165 | + |
| 166 | +// #endregion |
0 commit comments