|
| 1 | +import shaderCode from './shader.wgsl' |
| 2 | +import getBoundingBox from './getBoundingBox' |
| 3 | + |
| 4 | +export default function getDrawShape( |
| 5 | + device: GPUDevice, |
| 6 | + presentationFormat: GPUTextureFormat, |
| 7 | + canvasMatrixBuffer: GPUBuffer |
| 8 | +) { |
| 9 | + const shaderModule = device.createShaderModule({ |
| 10 | + label: 'drawShape shader', |
| 11 | + code: shaderCode, |
| 12 | + }) |
| 13 | + |
| 14 | + const uniformBufferSize = |
| 15 | + (1 /*stroke width*/ + 4 /*stroke color*/ + 4 /*fill color*/ + /*padding*/ 3) * 4 |
| 16 | + |
| 17 | + const uniformBuffer = device.createBuffer({ |
| 18 | + label: 'drawShape uniforms', |
| 19 | + size: uniformBufferSize, |
| 20 | + usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, |
| 21 | + }) |
| 22 | + |
| 23 | + // Update uniforms |
| 24 | + const uniformValues = new Float32Array(uniformBufferSize / 4) |
| 25 | + |
| 26 | + // offsets to the various uniform values in float32 indices |
| 27 | + let start = 0 |
| 28 | + let end = 4 /* 1 of stroke + 3 of padding */ |
| 29 | + const strokeWidthValue = uniformValues.subarray(start, (start = end)) |
| 30 | + |
| 31 | + end += 4 |
| 32 | + const strokeColorValue = uniformValues.subarray(start, (start = end)) |
| 33 | + |
| 34 | + end += 4 |
| 35 | + const fillColorValue = uniformValues.subarray(start, (start = end)) |
| 36 | + |
| 37 | + const bindGroupLayout = device.createBindGroupLayout({ |
| 38 | + label: 'drawShape bind group layout', |
| 39 | + entries: [ |
| 40 | + { |
| 41 | + binding: 0, |
| 42 | + visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT, |
| 43 | + buffer: { type: 'uniform' }, |
| 44 | + }, |
| 45 | + { |
| 46 | + binding: 1, |
| 47 | + visibility: GPUShaderStage.FRAGMENT, |
| 48 | + buffer: { type: 'read-only-storage' }, |
| 49 | + }, |
| 50 | + { |
| 51 | + binding: 2, |
| 52 | + visibility: GPUShaderStage.VERTEX, |
| 53 | + buffer: { type: 'uniform' }, |
| 54 | + }, |
| 55 | + ], |
| 56 | + }) |
| 57 | + |
| 58 | + const renderPipeline = device.createRenderPipeline({ |
| 59 | + label: 'drawShape pipeline', |
| 60 | + layout: device.createPipelineLayout({ |
| 61 | + bindGroupLayouts: [bindGroupLayout], |
| 62 | + }), |
| 63 | + vertex: { |
| 64 | + module: shaderModule, |
| 65 | + entryPoint: 'vs', |
| 66 | + buffers: [ |
| 67 | + { |
| 68 | + arrayStride: 2 * 4, // position (2) + color (4) |
| 69 | + attributes: [ |
| 70 | + { |
| 71 | + shaderLocation: 0, |
| 72 | + offset: 0, |
| 73 | + format: 'float32x2', // position |
| 74 | + }, |
| 75 | + ], |
| 76 | + }, |
| 77 | + ], |
| 78 | + }, |
| 79 | + fragment: { |
| 80 | + module: shaderModule, |
| 81 | + entryPoint: 'fs', |
| 82 | + targets: [ |
| 83 | + { |
| 84 | + format: presentationFormat, |
| 85 | + blend: { |
| 86 | + color: { |
| 87 | + srcFactor: 'src-alpha', |
| 88 | + dstFactor: 'one-minus-src-alpha', |
| 89 | + }, |
| 90 | + alpha: { |
| 91 | + srcFactor: 'one', |
| 92 | + dstFactor: 'one-minus-src-alpha', |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + ], |
| 97 | + }, |
| 98 | + multisample: { |
| 99 | + count: 4, |
| 100 | + }, |
| 101 | + }) |
| 102 | + |
| 103 | + return function drawShape(passEncoder: GPURenderPassEncoder, curves: Point[]) { |
| 104 | + const strokeWidth = 20 |
| 105 | + const boundingBox = getBoundingBox(curves, strokeWidth / 2) |
| 106 | + |
| 107 | + // Create curves buffer |
| 108 | + const curvesData = new Float32Array(curves.length * 2) // x y per point |
| 109 | + |
| 110 | + for (let i = 0; i < curves.length; i++) { |
| 111 | + const point = curves[i] |
| 112 | + const offset = i * 2 |
| 113 | + curvesData[offset + 0] = point.x |
| 114 | + curvesData[offset + 1] = point.y |
| 115 | + } |
| 116 | + |
| 117 | + // Create vertex buffer |
| 118 | + // prettier-ignore |
| 119 | + const vertexData = new Float32Array([ |
| 120 | + boundingBox[0].x, boundingBox[0].y, |
| 121 | + boundingBox[1].x, boundingBox[1].y, |
| 122 | + boundingBox[2].x, boundingBox[2].y, |
| 123 | + boundingBox[2].x, boundingBox[2].y, |
| 124 | + boundingBox[3].x, boundingBox[3].y, |
| 125 | + boundingBox[0].x, boundingBox[0].y, |
| 126 | + ]) |
| 127 | + |
| 128 | + const vertexBuffer = device.createBuffer({ |
| 129 | + label: 'drawShape vertex buffer', |
| 130 | + size: vertexData.byteLength, |
| 131 | + usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST, |
| 132 | + }) |
| 133 | + device.queue.writeBuffer(vertexBuffer, 0, vertexData) |
| 134 | + |
| 135 | + const curvesBuffer = device.createBuffer({ |
| 136 | + label: 'drawShape curves buffer', |
| 137 | + size: curvesData.byteLength, |
| 138 | + usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST, |
| 139 | + }) |
| 140 | + device.queue.writeBuffer(curvesBuffer, 0, curvesData) |
| 141 | + |
| 142 | + strokeWidthValue.set([strokeWidth]) |
| 143 | + strokeColorValue.set([1, 0, 0, 1]) // Red stroke color |
| 144 | + fillColorValue.set([0, 1, 0, 1]) // Green fill color |
| 145 | + device.queue.writeBuffer(uniformBuffer, 0, uniformValues) |
| 146 | + |
| 147 | + passEncoder.setPipeline(renderPipeline) |
| 148 | + |
| 149 | + const bindGroup = device.createBindGroup({ |
| 150 | + label: 'drawShape bind group', |
| 151 | + layout: bindGroupLayout, |
| 152 | + entries: [ |
| 153 | + { binding: 0, resource: { buffer: uniformBuffer } }, |
| 154 | + { binding: 1, resource: { buffer: curvesBuffer } }, |
| 155 | + { binding: 2, resource: { buffer: canvasMatrixBuffer } }, |
| 156 | + ], |
| 157 | + }) |
| 158 | + |
| 159 | + passEncoder.setBindGroup(0, bindGroup) |
| 160 | + passEncoder.setVertexBuffer(0, vertexBuffer) |
| 161 | + passEncoder.draw(6) // Draw quad |
| 162 | + } |
| 163 | +} |
0 commit comments