Line data Source code
1 : import { computed } from 'vue'
2 :
3 : import type { ComputedRef } from 'vue'
4 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
5 : import { SUBGRAPH_INPUT_ID } from '@/lib/litegraph/src/constants'
6 : import type { Subgraph } from '@/lib/litegraph/src/subgraph/Subgraph'
7 : import type { UUID } from '@/utils/uuid'
8 : import { useWidgetValueStore } from '@/stores/widgetValueStore'
9 :
10 : import { isCurveData } from '@/components/curve/curveUtils'
11 : import type { CurveData } from '@comfyorg/graph-model/curve'
12 : import { createPromotedHostWidgetIdLookup } from '@/core/graph/subgraph/promotionUtils'
13 : import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode'
14 : import type { GLSLRendererConfig } from '@/renderer/glsl/useGLSLRenderer'
15 : import { hexToInt } from '@comfyorg/shared-frontend-utils/colorUtil'
16 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
17 : import type { WidgetId } from '@comfyorg/graph-model/widgetId'
18 : import { widgetId } from '@comfyorg/graph-model/widgetId'
19 :
20 : interface AutogrowGroup {
21 : max: number
22 : min: number
23 : prefix?: string
24 : }
25 :
26 : interface UniformSource {
27 : nodeId: NodeId
28 : widgetName: string
29 : hostWidgetId?: WidgetId
30 : /** Fallback getter for widgets not registered in widgetValueStore (e.g. hidden computed widgets). */
31 : directValue: () => unknown
32 : }
33 :
34 : interface UniformSources {
35 : floats: UniformSource[]
36 : ints: UniformSource[]
37 : bools: UniformSource[]
38 : curves: UniformSource[]
39 : }
40 :
41 16 : export function getAutogrowLimits(node: LGraphNode): GLSLRendererConfig {
42 16 : const defaults: GLSLRendererConfig = {
43 : maxInputs: 5,
44 : maxFloatUniforms: 20,
45 : maxIntUniforms: 20,
46 : maxBoolUniforms: 10,
47 : maxCurves: 4
48 : }
49 :
50 16 : if (!('comfyDynamic' in node)) return defaults
51 :
52 16 : const dynamic: unknown = node.comfyDynamic
53 16 : if (
54 : typeof dynamic !== 'object' ||
55 : dynamic === null ||
56 : !('autogrow' in dynamic)
57 : )
58 0 : return defaults
59 :
60 16 : const groups = dynamic.autogrow as
61 : | Record<string, AutogrowGroup | undefined>
62 : | undefined
63 16 : if (!groups) return defaults
64 :
65 16 : const images = groups['images']
66 16 : const floats = groups['floats']
67 16 : const ints = groups['ints']
68 16 : const bools = groups['bools']
69 16 : const curves = groups['curves']
70 :
71 16 : return {
72 : maxInputs: images?.max ?? defaults.maxInputs,
73 : maxFloatUniforms: floats?.max ?? defaults.maxFloatUniforms,
74 : maxIntUniforms: ints?.max ?? defaults.maxIntUniforms,
75 : maxBoolUniforms: bools?.max ?? defaults.maxBoolUniforms,
76 : maxCurves: curves?.max ?? defaults.maxCurves
77 : }
78 : }
79 :
80 4 : export function extractUniformSources(
81 : glslNode: LGraphNode,
82 : subgraph: Subgraph,
83 : subgraphNode?: SubgraphNode
84 : ): UniformSources {
85 4 : const floats: UniformSource[] = []
86 4 : const ints: UniformSource[] = []
87 4 : const bools: UniformSource[] = []
88 4 : const curves: UniformSource[] = []
89 :
90 4 : const hostWidgetIdForSource = subgraphNode
91 : ? createPromotedHostWidgetIdLookup(subgraphNode)
92 : : undefined
93 :
94 4 : for (const [index, input] of glslNode.inputs.entries()) {
95 35 : const link = glslNode.getInputLink(index)
96 35 : if (!link || link.origin_id === SUBGRAPH_INPUT_ID) continue
97 :
98 3 : const sourceNode = subgraph.getNodeById(link.origin_id)
99 35 : if (!sourceNode?.widgets?.length) continue
100 :
101 3 : const inputName = input.name
102 3 : const dotIndex = inputName.indexOf('.')
103 27 : if (dotIndex === -1) continue
104 :
105 3 : const prefix = inputName.slice(0, dotIndex)
106 27 : if (link.origin_slot >= sourceNode.widgets.length) continue
107 3 : const widget = sourceNode.widgets[link.origin_slot]
108 35 : const source: UniformSource = {
109 : nodeId: sourceNode.id,
110 : widgetName: widget.name,
111 : hostWidgetId: hostWidgetIdForSource?.(sourceNode.id, widget.name),
112 : directValue: () => widget.value
113 : }
114 :
115 35 : if (prefix === 'floats') floats.push(source)
116 0 : else if (prefix === 'ints') ints.push(source)
117 0 : else if (prefix === 'bools') bools.push(source)
118 0 : else if (prefix === 'curves') curves.push(source)
119 : }
120 :
121 4 : return { floats, ints, bools, curves }
122 : }
123 :
124 11 : export function toNumber(v: unknown): number {
125 11 : if (typeof v === 'string' && v.startsWith('#')) return hexToInt(v)
126 11 : return Number(v) || 0
127 : }
128 :
129 16 : export function useGLSLUniforms(
130 : graphId: ComputedRef<UUID | undefined>,
131 : nodeId: ComputedRef<NodeId | undefined>,
132 : nodeRef: ComputedRef<LGraphNode | null>,
133 : uniformSources: ComputedRef<UniformSources | null>,
134 : rendererConfig: ComputedRef<GLSLRendererConfig>
135 : ) {
136 16 : const widgetValueStore = useWidgetValueStore()
137 :
138 66 : function collectValues<T>(
139 : subgraphSources: UniformSource[] | undefined,
140 : groupName: string,
141 : uniformPrefix: string,
142 : maxCount: number,
143 : coerce: (value: unknown) => T,
144 : defaultValue: T
145 : ): T[] {
146 66 : const gId = graphId.value
147 66 : if (!gId) return []
148 :
149 66 : if (subgraphSources) {
150 13 : return subgraphSources.map(
151 : ({ nodeId: nId, widgetName, hostWidgetId, directValue }) => {
152 : const widget = widgetValueStore.getWidget(
153 : hostWidgetId ?? widgetId(gId, nId, widgetName)
154 : )
155 4 : return coerce(widget?.value ?? directValue() ?? defaultValue)
156 : }
157 : )
158 : }
159 :
160 53 : const nId = nodeId.value
161 53 : const node = nodeRef.value
162 66 : if (nId == null || !node) return []
163 :
164 53 : const values: T[] = []
165 60 : for (let i = 0; i < maxCount; i++) {
166 62 : const inputName = `${groupName}.${uniformPrefix}${i}`
167 62 : const widget = widgetValueStore.getWidget(widgetId(gId, nId, inputName))
168 62 : if (widget !== undefined) {
169 0 : values.push(coerce(widget.value))
170 0 : continue
171 : }
172 :
173 62 : const slot = node.inputs.findIndex((inp) => inp.name === inputName)
174 62 : if (slot < 0) break
175 :
176 62 : const link = node.getInputLink(slot)
177 62 : if (!link) break
178 9 : const upstreamNode = node.getInputNode(slot)
179 26 : if (!upstreamNode) break
180 9 : const upstreamWidgets = widgetValueStore.getNodeWidgets(
181 : gId,
182 : upstreamNode.id
183 : )
184 62 : if (
185 : upstreamWidgets.length === 0 ||
186 : link.origin_slot >= upstreamWidgets.length
187 : )
188 0 : break
189 9 : values.push(coerce(upstreamWidgets[link.origin_slot].value))
190 : }
191 62 : return values
192 : }
193 :
194 16 : const toBool = (v: unknown): boolean => Boolean(v)
195 :
196 : const floatValues = computed(() =>
197 : collectValues(
198 : uniformSources.value?.floats,
199 : 'floats',
200 : 'u_float',
201 : rendererConfig.value.maxFloatUniforms,
202 : toNumber,
203 : 0
204 : )
205 : )
206 :
207 : const intValues = computed(() =>
208 : collectValues(
209 : uniformSources.value?.ints,
210 : 'ints',
211 : 'u_int',
212 : rendererConfig.value.maxIntUniforms,
213 : toNumber,
214 : 0
215 : )
216 : )
217 :
218 : const boolValues = computed(() =>
219 : collectValues(
220 : uniformSources.value?.bools,
221 : 'bools',
222 : 'u_bool',
223 : rendererConfig.value.maxBoolUniforms,
224 : toBool,
225 : false
226 : )
227 : )
228 :
229 : const curveValues = computed((): CurveData[] => {
230 20 : const gId = graphId.value
231 20 : if (!gId) return []
232 :
233 20 : const sources = uniformSources.value?.curves
234 20 : if (sources && sources.length > 0) {
235 0 : return sources
236 : .map(({ nodeId: nId, widgetName, hostWidgetId, directValue }) => {
237 : const widget = widgetValueStore.getWidget(
238 : hostWidgetId ?? widgetId(gId, nId, widgetName)
239 : )
240 0 : const value = widget?.value ?? directValue()
241 0 : return isCurveData(value) ? value : null
242 : })
243 : .filter((v): v is CurveData => v !== null)
244 : }
245 :
246 20 : const node = nodeRef.value
247 20 : const nId = nodeId.value
248 20 : if (nId == null || !node?.inputs) return []
249 :
250 20 : const values: CurveData[] = []
251 20 : const max = rendererConfig.value.maxCurves
252 20 : for (let i = 0; i < max; i++) {
253 20 : const inputName = `curves.u_curve${i}`
254 :
255 20 : const widget = widgetValueStore.getWidget(widgetId(gId, nId, inputName))
256 20 : if (widget && isCurveData(widget.value)) {
257 0 : values.push(widget.value)
258 0 : continue
259 : }
260 :
261 20 : const slot = node.inputs.findIndex((inp) => inp.name === inputName)
262 20 : if (slot < 0) break
263 :
264 16 : const upstreamNode = node.getInputNode(slot)
265 16 : if (!upstreamNode) break
266 :
267 : const upstreamWidgets = widgetValueStore.getNodeWidgets(
268 : gId,
269 : upstreamNode.id
270 : )
271 0 : const curveWidget = upstreamWidgets.find((w) => isCurveData(w.value))
272 0 : if (!curveWidget) break
273 0 : values.push(curveWidget.value as CurveData)
274 : }
275 20 : return values
276 : })
277 :
278 16 : return {
279 : floatValues,
280 : intValues,
281 : boolValues,
282 : curveValues
283 : }
284 : }
|