| 233 | } |
| 234 | ` |
| 235 | class CanvasRenderManager implements Disposable { |
| 236 | private device: GPUDevice; |
| 237 | private canvasContext: GPUCanvasContext; |
| 238 | private stagingTexture: GPUTexture; |
| 239 | private renderSampler: GPUSampler; |
| 240 | private renderPipeline: GPURenderPipeline; |
| 241 | private clearPipeline: GPURenderPipeline; |
| 242 | private canvasTextureFormat: GPUTextureFormat; |
| 243 | |
| 244 | constructor(device: GPUDevice, canvas: HTMLCanvasElement) { |
| 245 | this.device = device; |
| 246 | const ctx = canvas.getContext("webgpu"); |
| 247 | if (ctx == null) { |
| 248 | throw Error("Cannot bind WebGPU context"); |
| 249 | } |
| 250 | // avoid possible ts complain |
| 251 | this.canvasContext = ctx as any; |
| 252 | this.canvasTextureFormat = navigator.gpu.getPreferredCanvasFormat(); |
| 253 | this.canvasContext.configure({ |
| 254 | device: this.device, |
| 255 | format: this.canvasTextureFormat, |
| 256 | alphaMode: "opaque", |
| 257 | }); |
| 258 | |
| 259 | this.renderPipeline = device.createRenderPipeline({ |
| 260 | layout: "auto", |
| 261 | vertex: { |
| 262 | module: device.createShaderModule({ |
| 263 | code: canvasRenderWGSL, |
| 264 | }), |
| 265 | entryPoint: "vertex_main", |
| 266 | }, |
| 267 | fragment: { |
| 268 | module: device.createShaderModule({ |
| 269 | code: canvasRenderWGSL, |
| 270 | }), |
| 271 | entryPoint: "fragment_main", |
| 272 | targets: [{ |
| 273 | format: this.canvasTextureFormat, |
| 274 | }], |
| 275 | }, |
| 276 | primitive: { |
| 277 | topology: "triangle-list", |
| 278 | }, |
| 279 | }); |
| 280 | |
| 281 | this.clearPipeline = device.createRenderPipeline({ |
| 282 | layout: "auto", |
| 283 | vertex: { |
| 284 | module: device.createShaderModule({ |
| 285 | code: canvasRenderWGSL, |
| 286 | }), |
| 287 | entryPoint: "vertex_main", |
| 288 | }, |
| 289 | fragment: { |
| 290 | module: device.createShaderModule({ |
| 291 | code: canvasRenderWGSL, |
| 292 | }), |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…