(device: GPUDevice, options?: GridRendererOptions)
| 133 | }; |
| 134 | |
| 135 | export function createGridRenderer(device: GPUDevice, options?: GridRendererOptions): GridRenderer { |
| 136 | let disposed = false; |
| 137 | const targetFormat = options?.targetFormat ?? DEFAULT_TARGET_FORMAT; |
| 138 | |
| 139 | const bindGroupLayout = device.createBindGroupLayout({ |
| 140 | entries: [ |
| 141 | { binding: 0, visibility: GPUShaderStage.VERTEX, buffer: { type: 'uniform' } }, |
| 142 | { binding: 1, visibility: GPUShaderStage.FRAGMENT, buffer: { type: 'uniform' } }, |
| 143 | ], |
| 144 | }); |
| 145 | |
| 146 | const vsUniformBuffer = createUniformBuffer(device, 64, { label: 'gridRenderer/vsUniforms' }); |
| 147 | const fsUniformBuffer = createUniformBuffer(device, 16, { label: 'gridRenderer/fsUniforms' }); |
| 148 | |
| 149 | const bindGroup = device.createBindGroup({ |
| 150 | layout: bindGroupLayout, |
| 151 | entries: [ |
| 152 | { binding: 0, resource: { buffer: vsUniformBuffer } }, |
| 153 | { binding: 1, resource: { buffer: fsUniformBuffer } }, |
| 154 | ], |
| 155 | }); |
| 156 | |
| 157 | const pipeline = createRenderPipeline(device, { |
| 158 | label: 'gridRenderer/pipeline', |
| 159 | bindGroupLayouts: [bindGroupLayout], |
| 160 | vertex: { |
| 161 | code: gridWgsl, |
| 162 | label: 'grid.wgsl', |
| 163 | buffers: [ |
| 164 | { |
| 165 | arrayStride: 8, // vec2<f32> = 2 * 4 bytes |
| 166 | stepMode: 'vertex', |
| 167 | attributes: [{ shaderLocation: 0, format: 'float32x2', offset: 0 }], |
| 168 | }, |
| 169 | ], |
| 170 | }, |
| 171 | fragment: { |
| 172 | code: gridWgsl, |
| 173 | label: 'grid.wgsl', |
| 174 | formats: targetFormat, |
| 175 | // Enable standard alpha blending so `fsUniforms.color.a` behaves as expected |
| 176 | // (blends into the cleared background instead of making the canvas pixels transparent). |
| 177 | blend: { |
| 178 | color: { operation: 'add', srcFactor: 'src-alpha', dstFactor: 'one-minus-src-alpha' }, |
| 179 | alpha: { operation: 'add', srcFactor: 'one', dstFactor: 'one-minus-src-alpha' }, |
| 180 | }, |
| 181 | }, |
| 182 | primitive: { topology: 'line-list', cullMode: 'none' }, |
| 183 | multisample: { count: 1 }, |
| 184 | }); |
| 185 | |
| 186 | let vertexBuffer: GPUBuffer | null = null; |
| 187 | let vertexCount = 0; |
| 188 | |
| 189 | const assertNotDisposed = (): void => { |
| 190 | if (disposed) throw new Error('GridRenderer is disposed.'); |
| 191 | }; |
| 192 |
no test coverage detected