Line data Source code
1 : /// <reference types="@webgpu/types" />
2 : import { onUnmounted, ref, watch } from 'vue'
3 : import { tgpu } from 'typegpu'
4 :
5 : import { BrushShape } from './types'
6 : import type {
7 : Point2D as Point,
8 : Extents as DirtyRect
9 : } from '@comfyorg/shared-frontend-utils/geometry'
10 : import { useMaskEditorStore } from './maskEditorStore'
11 : import { parseToRgb } from './color'
12 :
13 : import {
14 : premultiplyData,
15 : resetDirtyRect,
16 : updateDirtyRect
17 : } from './brushDrawingUtils'
18 : import { getEffectiveBrushSize, getEffectiveHardness } from './brushUtils'
19 : import { GPUBrushRenderer } from './gpu/GPUBrushRenderer'
20 : import { buildStrokePoints, clampDirtyRect } from './gpuUtils'
21 :
22 29 : export function useGPUResources() {
23 29 : const store = useMaskEditorStore()
24 :
25 : // GPU state — plain variables, not reactive, as Vue doesn't need to track them
26 29 : let maskTexture: GPUTexture | null = null
27 29 : let rgbTexture: GPUTexture | null = null
28 29 : let device: GPUDevice | null = null
29 29 : let renderer: GPUBrushRenderer | null = null
30 29 : let previewContext: GPUCanvasContext | null = null
31 :
32 : // Readback buffers
33 29 : let readbackStorageMask: GPUBuffer | null = null
34 29 : let readbackStorageRgb: GPUBuffer | null = null
35 29 : let readbackStagingMask: GPUBuffer | null = null
36 29 : let readbackStagingRgb: GPUBuffer | null = null
37 29 : let currentBufferSize = 0
38 :
39 : // Reactive state shared with useBrushDrawing
40 29 : const previewCanvas = ref<HTMLCanvasElement | null>(null)
41 29 : const isSavingHistory = ref(false)
42 29 : const dirtyRect = ref<DirtyRect>(resetDirtyRect())
43 :
44 29 : const hasRenderer = ref(false)
45 :
46 29 : const isRecreatingTextures = ref(false)
47 :
48 : // ── Watchers ────────────────────────────────────────────────────────────────
49 :
50 29 : watch(
51 : () => store.clearTrigger,
52 : () => clearGPU()
53 : )
54 :
55 29 : watch(
56 : () => store.canvasHistory.currentStateIndex,
57 : async () => {
58 55 : if (isSavingHistory.value) return
59 36 : await updateGPUFromCanvas()
60 55 : if (renderer && previewContext) renderer.clearPreview(previewContext)
61 : }
62 : )
63 :
64 29 : watch(
65 : () => store.gpuTexturesNeedRecreation,
66 : async (needsRecreation) => {
67 0 : if (
68 : !needsRecreation ||
69 : !device ||
70 : !store.maskCanvas ||
71 : isRecreatingTextures.value
72 : )
73 0 : return
74 :
75 : /* c8 ignore start */
76 : isRecreatingTextures.value = true
77 :
78 : const width = store.gpuTextureWidth
79 : const height = store.gpuTextureHeight
80 :
81 : try {
82 : maskTexture?.destroy()
83 : maskTexture = null
84 : rgbTexture?.destroy()
85 : rgbTexture = null
86 :
87 : maskTexture = createTexture(device, width, height)
88 : rgbTexture = createTexture(device, width, height)
89 :
90 : if (store.pendingGPUMaskData && store.pendingGPURgbData) {
91 : device.queue.writeTexture(
92 : { texture: maskTexture },
93 : store.pendingGPUMaskData,
94 : { bytesPerRow: width * 4 },
95 : { width, height }
96 : )
97 : device.queue.writeTexture(
98 : { texture: rgbTexture },
99 : store.pendingGPURgbData,
100 : { bytesPerRow: width * 4 },
101 : { width, height }
102 : )
103 : } else {
104 : await updateGPUFromCanvas()
105 : }
106 :
107 : if (previewCanvas.value && renderer) {
108 : previewCanvas.value.width = width
109 : previewCanvas.value.height = height
110 : }
111 :
112 : resizeReadbackBuffers(device, width, height)
113 : } catch (error) {
114 : console.error(
115 : '[useGPUResources] Failed to recreate GPU textures:',
116 : error
117 : )
118 : } finally {
119 : store.gpuTexturesNeedRecreation = false
120 : store.gpuTextureWidth = 0
121 : store.gpuTextureHeight = 0
122 : store.pendingGPUMaskData = null
123 : store.pendingGPURgbData = null
124 : isRecreatingTextures.value = false
125 : }
126 : /* c8 ignore stop */
127 : }
128 : )
129 :
130 29 : onUnmounted(() => {
131 : // c8 ignore start
132 : renderer?.destroy()
133 : renderer = null
134 : hasRenderer.value = false
135 : maskTexture?.destroy()
136 : maskTexture = null
137 : rgbTexture?.destroy()
138 : rgbTexture = null
139 : readbackStorageMask?.destroy()
140 : readbackStorageMask = null
141 : readbackStorageRgb?.destroy()
142 : readbackStorageRgb = null
143 : readbackStagingMask?.destroy()
144 : readbackStagingMask = null
145 : readbackStagingRgb?.destroy()
146 : readbackStagingRgb = null
147 : previewContext = null
148 : previewCanvas.value = null
149 : dirtyRect.value = resetDirtyRect()
150 : // Device is managed by TGPU root; do not destroy it here
151 : // c8 ignore stop
152 : })
153 :
154 : // ── Helpers ─────────────────────────────────────────────────────────────────
155 :
156 : /* c8 ignore start — requires a live GPUDevice */
157 : function createTexture(
158 : gpuDevice: GPUDevice,
159 : width: number,
160 : height: number
161 : ): GPUTexture {
162 : return gpuDevice.createTexture({
163 : size: [width, height],
164 : format: 'rgba8unorm',
165 : usage:
166 : GPUTextureUsage.TEXTURE_BINDING |
167 : GPUTextureUsage.STORAGE_BINDING |
168 : GPUTextureUsage.RENDER_ATTACHMENT |
169 : GPUTextureUsage.COPY_DST |
170 : GPUTextureUsage.COPY_SRC
171 : })
172 : }
173 :
174 : function resizeReadbackBuffers(
175 : gpuDevice: GPUDevice,
176 : width: number,
177 : height: number
178 : ): void {
179 : const bufferSize = width * height * 4
180 : if (currentBufferSize === bufferSize) return
181 :
182 : readbackStorageMask?.destroy()
183 : readbackStorageRgb?.destroy()
184 : readbackStagingMask?.destroy()
185 : readbackStagingRgb?.destroy()
186 :
187 : readbackStorageMask = gpuDevice.createBuffer({
188 : size: bufferSize,
189 : usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC
190 : })
191 : readbackStorageRgb = gpuDevice.createBuffer({
192 : size: bufferSize,
193 : usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC
194 : })
195 : readbackStagingMask = gpuDevice.createBuffer({
196 : size: bufferSize,
197 : usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
198 : })
199 : readbackStagingRgb = gpuDevice.createBuffer({
200 : size: bufferSize,
201 : usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
202 : })
203 : currentBufferSize = bufferSize
204 : }
205 : /* c8 ignore stop */
206 :
207 : // ── Internal functions ───────────────────────────────────────────────────────
208 :
209 29 : async function initTypeGPU(): Promise<void> {
210 29 : if (store.tgpuRoot) {
211 : /* c8 ignore start */
212 : device = store.tgpuRoot.device
213 : return
214 : /* c8 ignore stop */
215 : }
216 29 : try {
217 : /* c8 ignore start — requires functional WebGPU hardware */
218 : const root = await tgpu.init()
219 : store.tgpuRoot = root
220 : device = root.device
221 : console.warn('✅ TypeGPU initialized! Root:', root)
222 : console.warn('Device info:', root.device.limits)
223 : /* c8 ignore stop */
224 : } catch (error) {
225 29 : const message = error instanceof Error ? error.message : String(error)
226 29 : console.warn('Failed to initialize TypeGPU:', message)
227 : }
228 : }
229 :
230 36 : async function updateGPUFromCanvas(): Promise<void> {
231 36 : if (
232 : !device ||
233 : !maskTexture ||
234 : !rgbTexture ||
235 : !store.maskCanvas ||
236 : !store.maskCtx ||
237 : !store.rgbCtx
238 : )
239 36 : return
240 :
241 : /* c8 ignore start — requires live GPU device and textures */
242 : const w = store.maskCanvas.width
243 : const h = store.maskCanvas.height
244 :
245 : const maskData = store.maskCtx.getImageData(0, 0, w, h)
246 : premultiplyData(maskData.data)
247 : device.queue.writeTexture(
248 : { texture: maskTexture },
249 : maskData.data,
250 : { bytesPerRow: w * 4 },
251 : { width: w, height: h }
252 : )
253 :
254 : const rgbData = store.rgbCtx.getImageData(0, 0, w, h)
255 : premultiplyData(rgbData.data)
256 : device.queue.writeTexture(
257 : { texture: rgbTexture },
258 : rgbData.data,
259 : { bytesPerRow: w * 4 },
260 : { width: w, height: h }
261 : )
262 : /* c8 ignore stop */
263 : }
264 :
265 : // ── Public API ───────────────────────────────────────────────────────────────
266 :
267 29 : async function initGPUResources(): Promise<void> {
268 29 : await initTypeGPU()
269 :
270 29 : if (!store.tgpuRoot || !device) {
271 29 : console.warn('TypeGPU not initialized, skipping GPU resource setup')
272 29 : return
273 : }
274 29 : if (
275 : !store.maskCanvas ||
276 : !store.rgbCanvas ||
277 : !store.maskCtx ||
278 : !store.rgbCtx
279 : ) {
280 0 : console.warn('Canvas contexts not ready, skipping GPU resource setup')
281 0 : return
282 : }
283 :
284 0 : const w = store.maskCanvas.width
285 0 : const h = store.maskCanvas.height
286 :
287 : /* c8 ignore start — requires functional WebGPU hardware */
288 : try {
289 : console.warn(`🎨 Initializing GPU resources for ${w}x${h} canvas`)
290 : maskTexture = createTexture(device, w, h)
291 : rgbTexture = createTexture(device, w, h)
292 : await updateGPUFromCanvas()
293 : console.warn('✅ GPU resources initialized successfully')
294 : renderer = new GPUBrushRenderer(
295 : device,
296 : navigator.gpu.getPreferredCanvasFormat()
297 : )
298 : hasRenderer.value = true
299 : console.warn('✅ Brush renderer initialized')
300 : } catch (error) {
301 : console.error('Failed to initialize GPU resources:', error)
302 : maskTexture = null
303 : rgbTexture = null
304 : }
305 : /* c8 ignore stop */
306 : }
307 :
308 29 : function initPreviewCanvas(canvas: HTMLCanvasElement): void {
309 29 : if (!device) return
310 : /* c8 ignore start — requires live GPUDevice and WebGPU canvas context */
311 : const ctx = canvas.getContext('webgpu')
312 : if (!ctx) return
313 : ctx.configure({
314 : device,
315 : format: navigator.gpu.getPreferredCanvasFormat(),
316 : alphaMode: 'premultiplied'
317 : })
318 : previewContext = ctx
319 : previewCanvas.value = canvas
320 : console.warn('✅ Preview Canvas Initialized')
321 : /* c8 ignore stop */
322 : }
323 :
324 1 : function clearGPU(): void {
325 1 : if (!device || !maskTexture || !rgbTexture || !store.maskCanvas) return
326 : /* c8 ignore start — requires live GPUDevice and textures */
327 : const w = store.maskCanvas.width
328 : const h = store.maskCanvas.height
329 : const zeros = new Uint8Array(w * h * 4)
330 : device.queue.writeTexture(
331 : { texture: maskTexture },
332 : zeros,
333 : { bytesPerRow: w * 4 },
334 : { width: w, height: h }
335 : )
336 : device.queue.writeTexture(
337 : { texture: rgbTexture },
338 : zeros,
339 : { bytesPerRow: w * 4 },
340 : { width: w, height: h }
341 : )
342 : /* c8 ignore stop */
343 : }
344 :
345 0 : function destroy(): void {
346 0 : renderer?.destroy()
347 0 : maskTexture?.destroy()
348 0 : rgbTexture?.destroy()
349 0 : readbackStorageMask?.destroy()
350 0 : readbackStorageRgb?.destroy()
351 0 : readbackStagingMask?.destroy()
352 0 : readbackStagingRgb?.destroy()
353 0 : renderer = null
354 0 : hasRenderer.value = false
355 0 : maskTexture = null
356 0 : rgbTexture = null
357 0 : readbackStorageMask = null
358 0 : readbackStorageRgb = null
359 0 : readbackStagingMask = null
360 0 : readbackStagingRgb = null
361 0 : currentBufferSize = 0
362 0 : previewContext = null
363 0 : previewCanvas.value = null
364 0 : dirtyRect.value = resetDirtyRect()
365 : /* c8 ignore next — tgpuRoot only exists after successful GPU init */
366 0 : if (store.tgpuRoot) {
367 0 : store.tgpuRoot.destroy()
368 0 : store.tgpuRoot = null
369 : }
370 0 : device = null
371 : }
372 :
373 : // ── Wrappers called by useBrushDrawing ──────────────────────────────────────
374 :
375 19 : function prepareStroke(): void {
376 19 : if (!renderer || !store.maskCanvas) return
377 : /* c8 ignore next */
378 : renderer.prepareStroke(store.maskCanvas.width, store.maskCanvas.height)
379 : }
380 :
381 19 : function clearPreview(): void {
382 19 : if (!renderer || !previewContext) return
383 : /* c8 ignore next */
384 : renderer.clearPreview(previewContext)
385 : }
386 :
387 19 : function compositeStroke(isRgb: boolean, isErasing: boolean): void {
388 19 : if (!renderer || !maskTexture || !rgbTexture || !store.maskCanvas) return
389 : /* c8 ignore start — requires live renderer */
390 : const targetTex = isRgb ? rgbTexture : maskTexture
391 : const { size, hardness, opacity, type } = store.brushSettings
392 : const effectiveSize = getEffectiveBrushSize(size, hardness)
393 : const effectiveHardness = getEffectiveHardness(
394 : size,
395 : hardness,
396 : effectiveSize
397 : )
398 : const brushShape = type === BrushShape.Rect ? 1 : 0
399 : renderer.compositeStroke(targetTex.createView(), {
400 : opacity,
401 : color: [0, 0, 0],
402 : hardness: effectiveHardness,
403 : screenSize: [store.maskCanvas.width, store.maskCanvas.height],
404 : brushShape,
405 : isErasing
406 : })
407 : /* c8 ignore stop */
408 : }
409 :
410 0 : async function copyGpuToCanvas(): Promise<{
411 : maskData: ImageData
412 : rgbData: ImageData
413 : }> {
414 0 : if (
415 : !device ||
416 : !maskTexture ||
417 : !rgbTexture ||
418 : !store.maskCanvas ||
419 : !store.rgbCanvas ||
420 : !store.maskCtx ||
421 : !store.rgbCtx ||
422 : !renderer
423 : )
424 0 : throw new Error('GPU resources not ready')
425 :
426 : /* c8 ignore start — requires live GPU device, textures and renderer */
427 : const width = store.maskCanvas.width
428 : const height = store.maskCanvas.height
429 :
430 : resizeReadbackBuffers(device, width, height)
431 :
432 : renderer.prepareReadback(maskTexture, readbackStorageMask!)
433 : renderer.prepareReadback(rgbTexture, readbackStorageRgb!)
434 :
435 : const encoder = device.createCommandEncoder()
436 : encoder.copyBufferToBuffer(
437 : readbackStorageMask!,
438 : 0,
439 : readbackStagingMask!,
440 : 0,
441 : currentBufferSize
442 : )
443 : encoder.copyBufferToBuffer(
444 : readbackStorageRgb!,
445 : 0,
446 : readbackStagingRgb!,
447 : 0,
448 : currentBufferSize
449 : )
450 : device.queue.submit([encoder.finish()])
451 :
452 : await Promise.all([
453 : readbackStagingMask!.mapAsync(GPUMapMode.READ),
454 : readbackStagingRgb!.mapAsync(GPUMapMode.READ)
455 : ])
456 :
457 : const maskDataArr = new Uint8ClampedArray(
458 : readbackStagingMask!.getMappedRange().slice(0)
459 : )
460 : const rgbDataArr = new Uint8ClampedArray(
461 : readbackStagingRgb!.getMappedRange().slice(0)
462 : )
463 : readbackStagingMask!.unmap()
464 : readbackStagingRgb!.unmap()
465 :
466 : const maskImageData = new ImageData(maskDataArr, width, height)
467 : const rgbImageData = new ImageData(rgbDataArr, width, height)
468 :
469 : const { dx, dy, dw, dh } = clampDirtyRect(dirtyRect.value, width, height)
470 : store.maskCtx.putImageData(maskImageData, 0, 0, dx, dy, dw, dh)
471 : store.rgbCtx.putImageData(rgbImageData, 0, 0, dx, dy, dw, dh)
472 :
473 : return { maskData: maskImageData, rgbData: rgbImageData }
474 : /* c8 ignore stop */
475 : }
476 :
477 0 : function gpuRender(points: Point[], skipResampling = false): void {
478 0 : if (!renderer || !maskTexture || !rgbTexture) return
479 :
480 : /* c8 ignore start — requires live renderer */
481 : const isRgb = store.activeLayer === 'rgb'
482 : const color = resolveColor(isRgb)
483 : const stepPercentage =
484 : Math.pow(100, store.brushSettings.stepSize / 100) / 100
485 : const gpuStepSize = Math.max(1.0, store.brushSettings.size * stepPercentage)
486 : const strokePoints = buildStrokePoints(points, skipResampling, gpuStepSize)
487 :
488 : const { size, hardness } = store.brushSettings
489 : const effectiveSize = getEffectiveBrushSize(size, hardness)
490 : const effectiveHardness = getEffectiveHardness(
491 : size,
492 : hardness,
493 : effectiveSize
494 : )
495 : const brushShape = store.brushSettings.type === BrushShape.Rect ? 1 : 0
496 :
497 : renderer.renderStrokeToAccumulator(strokePoints, {
498 : size: effectiveSize,
499 : coverage: 1,
500 : hardness: effectiveHardness,
501 : color,
502 : width: store.maskCanvas!.width,
503 : height: store.maskCanvas!.height,
504 : brushShape
505 : })
506 :
507 : for (const p of strokePoints) {
508 : dirtyRect.value = updateDirtyRect(
509 : dirtyRect.value,
510 : p.x,
511 : p.y,
512 : effectiveSize
513 : )
514 : }
515 :
516 : if (previewContext) {
517 : const isErasing =
518 : store.currentTool === 'eraser' ||
519 : store.maskCtx?.globalCompositeOperation === 'destination-out'
520 : const targetTex = isRgb ? rgbTexture : maskTexture
521 : renderer.blitToCanvas(
522 : previewContext,
523 : {
524 : opacity: store.brushSettings.opacity,
525 : color,
526 : hardness: effectiveHardness,
527 : screenSize: [store.maskCanvas!.width, store.maskCanvas!.height],
528 : brushShape,
529 : isErasing
530 : },
531 : targetTex
532 : )
533 : }
534 : /* c8 ignore stop */
535 : }
536 :
537 0 : async function gpuDrawPoint(point: Point, opacity = 1): Promise<void> {
538 0 : if (!renderer) return
539 :
540 : /* c8 ignore start — requires live renderer */
541 : const width = store.maskCanvas!.width
542 : const height = store.maskCanvas!.height
543 : const { size, hardness } = store.brushSettings
544 : const effectiveSize = getEffectiveBrushSize(size, hardness)
545 : const effectiveHardness = getEffectiveHardness(
546 : size,
547 : hardness,
548 : effectiveSize
549 : )
550 : const brushShape = store.brushSettings.type === BrushShape.Rect ? 1 : 0
551 :
552 : dirtyRect.value = updateDirtyRect(
553 : dirtyRect.value,
554 : point.x,
555 : point.y,
556 : effectiveSize
557 : )
558 :
559 : renderer.renderStrokeToAccumulator(
560 : [{ x: point.x, y: point.y, pressure: opacity }],
561 : {
562 : size: effectiveSize,
563 : coverage: 1,
564 : hardness: effectiveHardness,
565 : color: [1, 1, 1],
566 : width,
567 : height,
568 : brushShape
569 : }
570 : )
571 :
572 : if (maskTexture && previewContext) {
573 : const isRgb = store.activeLayer === 'rgb'
574 : const isErasing =
575 : store.currentTool === 'eraser' ||
576 : store.maskCtx?.globalCompositeOperation === 'destination-out'
577 : renderer.blitToCanvas(
578 : previewContext,
579 : {
580 : opacity: store.brushSettings.opacity,
581 : color: resolveColor(isRgb),
582 : hardness: effectiveHardness,
583 : screenSize: [width, height],
584 : brushShape,
585 : isErasing
586 : },
587 : undefined
588 : )
589 : }
590 : /* c8 ignore stop */
591 : }
592 :
593 : // ── Private helpers ─────────────────────────────────────────────────────────
594 :
595 : /* c8 ignore start — only reachable after successful GPU init */
596 : function resolveColor(isRgb: boolean): [number, number, number] {
597 : if (isRgb) {
598 : const c = parseToRgb(store.rgbColor)
599 : return [c.r / 255, c.g / 255, c.b / 255]
600 : }
601 : const c = store.maskColor
602 : return [c.r / 255, c.g / 255, c.b / 255]
603 : }
604 : /* c8 ignore stop */
605 :
606 29 : return {
607 : // Lifecycle — spread into useBrushDrawing's public return
608 : initGPUResources,
609 : initPreviewCanvas,
610 : clearGPU,
611 : destroy,
612 : // Rendering — called internally by useBrushDrawing
613 : gpuRender,
614 : gpuDrawPoint,
615 : copyGpuToCanvas,
616 : // Renderer wrappers — called internally by useBrushDrawing
617 : prepareStroke,
618 : clearPreview,
619 : compositeStroke,
620 : // Shared reactive state
621 : hasRenderer,
622 : previewCanvas,
623 : isSavingHistory,
624 : dirtyRect
625 : }
626 : }
|