Line data Source code
1 : import { debounce } from 'es-toolkit/compat'
2 : import { computed, effectScope, onScopeDispose, ref, toValue, watch } from 'vue'
3 :
4 : import type { ComputedRef, EffectScope, MaybeRefOrGetter, Ref } from 'vue'
5 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
6 : import type { Subgraph } from '@/lib/litegraph/src/subgraph/Subgraph'
7 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
8 : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
9 : import { useWidgetValueStore } from '@/stores/widgetValueStore'
10 : import { widgetId } from '@comfyorg/graph-model/widgetId'
11 :
12 : import { curveDataToFloatLUT } from '@/components/curve/curveUtils'
13 : import type { GLSLRendererConfig } from '@/renderer/glsl/useGLSLRenderer'
14 : import { useGLSLRenderer } from '@/renderer/glsl/useGLSLRenderer'
15 : import {
16 : extractUniformSources,
17 : getAutogrowLimits,
18 : useGLSLUniforms
19 : } from '@/renderer/glsl/useGLSLUniforms'
20 : import {
21 : createSharedObjectUrl,
22 : releaseSharedObjectUrl
23 : } from '@/utils/objectUrlUtil'
24 :
25 : import {
26 : clampResolution,
27 : DEBOUNCE_MS,
28 : DEFAULT_SIZE,
29 : getInputNodeThroughSubgraphBoundary,
30 : GLSL_NODE_TYPE,
31 : normalizeDimension
32 : } from '@/renderer/glsl/glslPreviewUtils'
33 :
34 : /**
35 : * Two-tier composable for GLSL live preview.
36 : *
37 : * Outer tier (always created): only 2 cheap computed refs to detect
38 : * whether the node is GLSL-related. For non-GLSL nodes this is the
39 : * only cost — no watchers, store subscriptions, or renderer.
40 : *
41 : * Inner tier (lazy): created via effectScope when the node is detected
42 : * as a GLSLShader or a subgraph containing one. Contains all the
43 : * expensive logic: store reads, watchers, debounce, WebGL renderer.
44 : */
45 8015 : export function useGLSLPreview(
46 : nodeMaybe: MaybeRefOrGetter<LGraphNode | null | undefined>
47 : ) {
48 8015 : const lastError = ref<string | null>(null)
49 8015 : const hideExecutedOutput = ref(false)
50 :
51 8015 : const nodeRef = computed(() => toValue(nodeMaybe) ?? null)
52 :
53 8015 : const isGLSLNode = computed(() => nodeRef.value?.type === GLSL_NODE_TYPE)
54 :
55 8015 : const isGLSLSubgraphNode = computed(() => {
56 8019 : const node = nodeRef.value
57 8019 : if (!node?.isSubgraphNode()) return false
58 : const subgraph = node.subgraph as Subgraph | undefined
59 8019 : return subgraph?.nodes.some((n) => n.type === GLSL_NODE_TYPE) ?? false
60 : })
61 :
62 8015 : const isGLSLRelated = computed(
63 : () => isGLSLNode.value || isGLSLSubgraphNode.value
64 : )
65 :
66 8015 : let innerScope: EffectScope | null = null
67 8015 : let innerDispose: (() => void) | null = null
68 8015 : const isActive = ref(false)
69 :
70 8015 : watch(
71 : isGLSLRelated,
72 : (related) => {
73 8015 : if (related && !innerScope) {
74 16 : innerScope = effectScope()
75 16 : innerDispose = innerScope.run(() =>
76 : createInnerPreview(
77 : nodeRef,
78 : isGLSLNode,
79 : isGLSLSubgraphNode,
80 : lastError,
81 : isActive,
82 : hideExecutedOutput
83 : )
84 : )!
85 7999 : } else if (!related && innerScope) {
86 0 : innerDispose?.()
87 0 : innerScope.stop()
88 0 : innerScope = null
89 0 : innerDispose = null
90 0 : isActive.value = false
91 : }
92 : },
93 : { immediate: true }
94 : )
95 :
96 8015 : onScopeDispose(() => {
97 5243 : innerDispose?.()
98 5243 : innerScope?.stop()
99 : })
100 :
101 8015 : return {
102 : isActive: computed(() => isActive.value),
103 : hideExecutedOutput: computed(() => hideExecutedOutput.value),
104 : lastError,
105 : dispose() {
106 0 : innerDispose?.()
107 0 : innerScope?.stop()
108 0 : innerScope = null
109 0 : innerDispose = null
110 : }
111 : }
112 : }
113 :
114 : /**
115 : * Inner tier: all expensive GLSL preview logic.
116 : * Runs inside its own effectScope so it can be created/destroyed
117 : * independently of the component lifecycle.
118 : * Returns a dispose function.
119 : */
120 : type InputImage = HTMLImageElement | ImageBitmap
121 :
122 0 : function loadImageFromUrl(url: string): Promise<HTMLImageElement | null> {
123 0 : return new Promise((resolve) => {
124 0 : const img = new Image()
125 0 : img.onload = () => resolve(img)
126 0 : img.onerror = () => resolve(null)
127 0 : img.src = url
128 : })
129 : }
130 :
131 16 : function createInnerPreview(
132 : nodeRef: ComputedRef<LGraphNode | null>,
133 : isGLSLNode: ComputedRef<boolean>,
134 : isGLSLSubgraphNode: ComputedRef<boolean>,
135 : lastError: Ref<string | null>,
136 : isActiveOut: Ref<boolean>,
137 : hideExecutedOutput: Ref<boolean>
138 : ): () => void {
139 16 : const widgetValueStore = useWidgetValueStore()
140 16 : const nodeOutputStore = useNodeOutputStore()
141 16 : const { nodeToNodeLocatorId } = useWorkflowStore()
142 :
143 16 : let renderer: ReturnType<typeof useGLSLRenderer> | null = null
144 16 : let rendererReady = false
145 16 : let renderRequestId = 0
146 :
147 16 : const innerGLSLNode = (() => {
148 16 : const node = nodeRef.value
149 16 : if (!node?.isSubgraphNode()) return null
150 : const subgraph = node.subgraph as Subgraph | undefined
151 16 : const shaders =
152 : subgraph?.nodes.filter((n) => n.type === GLSL_NODE_TYPE) ?? []
153 16 : return shaders.length === 1 ? shaders[0] : null
154 : })()
155 :
156 16 : const ownerSubgraphNode = (() => {
157 : const node = nodeRef.value
158 16 : const graph = node?.graph
159 16 : if (!graph) return null
160 16 : const rootGraph = graph.rootGraph
161 16 : if (graph === rootGraph) return null
162 :
163 16 : return (
164 : rootGraph._nodes.find(
165 : (n) => n.isSubgraphNode() && n.subgraph === graph
166 : ) ?? null
167 : )
168 : })()
169 :
170 16 : const graphId = computed(() => nodeRef.value?.graph?.rootGraph.id)
171 :
172 16 : const nodeId = computed(() => {
173 16 : const id = nodeRef.value?.id
174 16 : return id == null ? undefined : id
175 : })
176 :
177 16 : const hasExecutionOutput = computed(() => {
178 30 : const node = nodeRef.value
179 30 : if (!node) return false
180 :
181 30 : const outputs = nodeOutputStore.nodeOutputs
182 :
183 : const locatorId = nodeToNodeLocatorId(node)
184 30 : if (outputs[locatorId]?.images?.length) return true
185 :
186 16 : const inner = innerGLSLNode
187 28 : if (inner) {
188 : const innerLocatorId = nodeToNodeLocatorId(inner)
189 4 : if (outputs[innerLocatorId]?.images?.length) return true
190 : }
191 :
192 16 : return false
193 : })
194 :
195 16 : const shouldRender = computed(
196 : () =>
197 : (isGLSLNode.value || isGLSLSubgraphNode.value) && hasExecutionOutput.value
198 : )
199 :
200 16 : watch(
201 : shouldRender,
202 : (v) => {
203 30 : isActiveOut.value = v
204 : },
205 : { immediate: true }
206 : )
207 :
208 16 : const shaderSource = computed(() => {
209 21 : const gId = graphId.value
210 21 : if (!gId) return undefined
211 :
212 21 : if (isGLSLNode.value) {
213 17 : const nId = nodeId.value
214 17 : if (nId == null) return undefined
215 17 : return widgetValueStore.getWidget(widgetId(gId, nId, 'fragment_shader'))
216 : ?.value as string | undefined
217 : }
218 :
219 4 : const inner = innerGLSLNode
220 4 : if (inner) {
221 4 : return widgetValueStore.getWidget(
222 : widgetId(gId, inner.id, 'fragment_shader')
223 : )?.value as string | undefined
224 : }
225 :
226 : return undefined
227 : })
228 :
229 16 : const rendererConfig = computed(() => {
230 16 : const inner = innerGLSLNode
231 16 : if (inner) return getAutogrowLimits(inner)
232 :
233 12 : const node = nodeRef.value
234 14 : if (!node)
235 0 : return {
236 : maxInputs: 5,
237 : maxFloatUniforms: 20,
238 : maxIntUniforms: 20,
239 : maxBoolUniforms: 10,
240 : maxCurves: 4
241 : }
242 12 : return getAutogrowLimits(node)
243 : })
244 :
245 : const uniformSources = computed(() => {
246 16 : const node = nodeRef.value
247 16 : const inner = innerGLSLNode
248 16 : if (!node?.isSubgraphNode() || !inner) return null
249 4 : return extractUniformSources(inner, node.subgraph, node)
250 : })
251 :
252 16 : const { floatValues, intValues, boolValues, curveValues } = useGLSLUniforms(
253 : graphId,
254 : nodeId,
255 : nodeRef,
256 : uniformSources,
257 : rendererConfig
258 : )
259 :
260 44 : async function resolveSlotImage(
261 : node: LGraphNode,
262 : slot: number
263 : ): Promise<InputImage | null> {
264 44 : const upstreamNode =
265 : node.getInputNode(slot) ??
266 : (ownerSubgraphNode
267 : ? getInputNodeThroughSubgraphBoundary(node, slot, ownerSubgraphNode)
268 : : null)
269 44 : if (upstreamNode?.imgs?.length) return upstreamNode.imgs[0]
270 :
271 44 : if (upstreamNode) {
272 0 : const url = nodeOutputStore.getNodeImageUrls(upstreamNode)?.[0]
273 0 : if (url) return await loadImageFromUrl(url)
274 : }
275 :
276 42 : return null
277 : }
278 :
279 26 : async function resolveInputImages(): Promise<{
280 : images: (InputImage | null)[]
281 : expected: number
282 : }> {
283 26 : const node = nodeRef.value
284 26 : if (!node?.inputs) return { images: [], expected: 0 }
285 :
286 26 : const images: (InputImage | null)[] = []
287 26 : let expected = 0
288 26 : for (let slot = 0; slot < node.inputs.length; slot++) {
289 196 : const input = node.inputs[slot]
290 : const isImageInput = isGLSLSubgraphNode.value
291 : ? input.type === 'IMAGE'
292 : : input.name.startsWith('images.image')
293 196 : if (!isImageInput) continue
294 194 : if (input.link != null) expected++
295 :
296 44 : images.push(await resolveSlotImage(node, slot))
297 : }
298 26 : return { images, expected }
299 : }
300 :
301 16 : const customResolution = computed((): [number, number] | null => {
302 28 : const gId = graphId.value
303 28 : if (!gId) return null
304 :
305 28 : const sizeModeNodeId = innerGLSLNode ? innerGLSLNode.id : nodeId.value
306 28 : if (sizeModeNodeId == null) return null
307 :
308 : const sizeMode = widgetValueStore.getWidget(
309 : widgetId(gId, sizeModeNodeId, 'size_mode')
310 : )
311 28 : if (sizeMode?.value !== 'custom') return null
312 :
313 11 : const widthWidget = widgetValueStore.getWidget(
314 : widgetId(gId, sizeModeNodeId, 'size_mode.width')
315 : )
316 11 : const heightWidget = widgetValueStore.getWidget(
317 : widgetId(gId, sizeModeNodeId, 'size_mode.height')
318 : )
319 28 : if (!widthWidget || !heightWidget) return null
320 :
321 11 : return clampResolution(
322 : normalizeDimension(widthWidget.value),
323 : normalizeDimension(heightWidget.value)
324 : )
325 : })
326 :
327 26 : function getResolution(images: InputImage[]): [number, number] {
328 26 : const custom = customResolution.value
329 26 : if (custom) return custom
330 :
331 20 : const img = images.at(0)
332 22 : if (img) {
333 2 : const w = img instanceof ImageBitmap ? img.width : img.naturalWidth
334 2 : const h = img instanceof ImageBitmap ? img.height : img.naturalHeight
335 2 : return clampResolution(w || DEFAULT_SIZE, h || DEFAULT_SIZE)
336 : }
337 :
338 18 : return [DEFAULT_SIZE, DEFAULT_SIZE]
339 : }
340 :
341 16 : let disposed = false
342 16 : const isDisposed = (): boolean => disposed
343 16 : let lastRendererConfig: GLSLRendererConfig | null = null
344 :
345 2 : function revokePreview(): void {
346 2 : const inner = innerGLSLNode
347 2 : if (inner) {
348 1 : nodeOutputStore.revokePreviewsByLocatorId(nodeToNodeLocatorId(inner))
349 1 : return
350 : }
351 1 : const node = nodeRef.value
352 1 : if (node)
353 1 : nodeOutputStore.revokePreviewsByLocatorId(nodeToNodeLocatorId(node))
354 : }
355 :
356 26 : function ensureRenderer(): ReturnType<typeof useGLSLRenderer> {
357 26 : const config = rendererConfig.value
358 26 : if (renderer?.isContextLost()) {
359 0 : renderer.dispose()
360 0 : renderer = null
361 0 : rendererReady = false
362 : }
363 26 : if (renderer && lastRendererConfig) {
364 : const changed =
365 12 : config.maxInputs !== lastRendererConfig.maxInputs ||
366 : config.maxFloatUniforms !== lastRendererConfig.maxFloatUniforms ||
367 : config.maxIntUniforms !== lastRendererConfig.maxIntUniforms ||
368 : config.maxBoolUniforms !== lastRendererConfig.maxBoolUniforms ||
369 : config.maxCurves !== lastRendererConfig.maxCurves
370 : if (changed) {
371 0 : renderer.dispose()
372 0 : renderer = null
373 0 : rendererReady = false
374 : }
375 : }
376 26 : if (!renderer) {
377 14 : renderer = useGLSLRenderer(config)
378 14 : lastRendererConfig = { ...config }
379 : }
380 26 : return renderer
381 : }
382 :
383 26 : async function renderPreview(): Promise<void> {
384 26 : const requestId = ++renderRequestId
385 26 : const source = shaderSource.value
386 26 : if (!source || !shouldRender.value) return
387 :
388 26 : const { images, expected } = await resolveInputImages()
389 26 : if (requestId !== renderRequestId || disposed) return
390 :
391 26 : const resolved = images.filter((img): img is InputImage => img != null)
392 26 : if (expected > 0 && resolved.length === 0) {
393 0 : revokePreview()
394 0 : if (isGLSLNode.value) hideExecutedOutput.value = false
395 0 : return
396 : }
397 :
398 26 : const r = ensureRenderer()
399 :
400 26 : try {
401 26 : const [w, h] = getResolution(resolved)
402 :
403 26 : if (!rendererReady) {
404 14 : if (!r.init(w, h)) {
405 0 : lastError.value = 'WebGL2 not available'
406 0 : return
407 : }
408 14 : rendererReady = true
409 : }
410 :
411 26 : const result = r.compileFragment(source)
412 26 : if (!result.success) {
413 1 : lastError.value = result.log
414 1 : console.warn('[GLSL] shader compilation failed:', result.log)
415 1 : return
416 : }
417 25 : lastError.value = null
418 :
419 25 : r.setResolution(w, h)
420 :
421 26 : for (let i = 0; i < images.length; i++) {
422 42 : const img = images[i]
423 42 : if (img) r.bindInputImage(i, img)
424 40 : else r.clearInputImage(i)
425 : }
426 :
427 26 : for (let i = 0; i < floatValues.value.length; i++) {
428 5 : r.setFloatUniform(i, floatValues.value[i])
429 : }
430 26 : for (let i = 0; i < intValues.value.length; i++) {
431 2 : r.setIntUniform(i, intValues.value[i])
432 : }
433 26 : for (let i = 0; i < boolValues.value.length; i++) {
434 2 : r.setBoolUniform(i, boolValues.value[i])
435 : }
436 25 : const curves = curveValues.value
437 26 : for (let i = 0; i < curves.length; i++) {
438 0 : r.bindCurveTexture(i, curveDataToFloatLUT(curves[i]))
439 : }
440 :
441 25 : r.render()
442 :
443 25 : const blob = await r.toBlob()
444 26 : if (requestId !== renderRequestId || isDisposed()) return
445 25 : const blobUrl = createSharedObjectUrl(blob)
446 26 : try {
447 25 : const inner = innerGLSLNode
448 26 : if (inner) {
449 4 : const innerLocatorId = nodeToNodeLocatorId(inner)
450 4 : nodeOutputStore.setNodePreviewsByLocatorId(innerLocatorId, [blobUrl])
451 : } else {
452 21 : const nId = nodeId.value
453 21 : if (nId != null) {
454 21 : nodeOutputStore.setNodePreviewsByNodeId(nId, [blobUrl])
455 : }
456 : }
457 : } finally {
458 25 : releaseSharedObjectUrl(blobUrl)
459 : }
460 25 : if (isGLSLNode.value) hideExecutedOutput.value = true
461 : } catch (error) {
462 0 : if (requestId !== renderRequestId) return
463 0 : lastError.value =
464 : error instanceof Error ? error.message : 'Failed to render preview'
465 : }
466 : }
467 :
468 16 : const debouncedRender = debounce((): void => {
469 26 : void renderPreview()
470 : }, DEBOUNCE_MS)
471 :
472 16 : watch(
473 : shouldRender,
474 : (active) => {
475 30 : if (isGLSLNode.value) hideExecutedOutput.value = active
476 30 : if (active) debouncedRender()
477 : },
478 : { immediate: true }
479 : )
480 :
481 16 : watch(
482 : () =>
483 : [
484 : floatValues.value,
485 : intValues.value,
486 : boolValues.value,
487 : curveValues.value,
488 : customResolution.value
489 : ] as const,
490 : () => {
491 18 : if (shouldRender.value) debouncedRender()
492 : },
493 : { deep: true }
494 : )
495 :
496 16 : watch(shaderSource, () => {
497 5 : if (shouldRender.value) debouncedRender()
498 : })
499 :
500 16 : return () => {
501 2 : disposed = true
502 2 : hideExecutedOutput.value = false
503 2 : debouncedRender.cancel()
504 2 : renderer?.dispose()
505 2 : renderer = null
506 :
507 2 : revokePreview()
508 : }
509 : }
|