-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.py
More file actions
208 lines (174 loc) · 5.9 KB
/
Copy pathnodes.py
File metadata and controls
208 lines (174 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# ComfyUI GLSL Nodes
import json
from .src.shader.ui_schema import (
SHADER_NODE_SLOT_INPUTS,
SHADER_NODE_SLOT_UNIFORMS,
collect_extra_image_inputs,
collect_union_uniform_inputs,
enrich_metadata,
extract_uniform_values,
list_shader_relpaths,
load_shader_source,
)
from .src.runtime.executor import GLSLRuntime
glsl_runtime = None
DEFAULT_SIMPLE_SHADER = """/*
@name Inline
@description Inline GLSL shader
@version 1.0.0
@input image IMAGE
*/
vec4 process(vec4 color, ivec2 pixel)
{
// Node slots: params.float_1 / float_2, int_1 / int_2, vec2_1 / vec2_2
color.rgb = mix(color.rgb, 1.0 - color.rgb, clamp(params.float_1, 0.0, 1.0));
return color;
}
"""
# Also accepted: GLES fragment shaders (#version 300 es + fragColor)
def get_glsl_runtime():
global glsl_runtime
if glsl_runtime is None:
glsl_runtime = GLSLRuntime()
return glsl_runtime
def _merge_shader_node_slots(metadata, kwargs):
"""Attach fixed authoring slots and collect their values."""
by_name = {u["name"]: dict(u) for u in metadata.get("uniforms", [])}
for slot in SHADER_NODE_SLOT_UNIFORMS:
by_name.setdefault(slot["name"], dict(slot))
metadata = dict(metadata)
metadata["uniforms"] = list(by_name.values())
values = extract_uniform_values(metadata, kwargs)
return metadata, values
class GLSLGPUProcessor:
"""Production-oriented shader execution from file-based shaders."""
@classmethod
def INPUT_TYPES(cls):
shader_options = list_shader_relpaths()
if not shader_options:
shader_options = ["(no shaders found)"]
optional = {
"mask": ("MASK",),
}
optional.update(collect_extra_image_inputs())
optional.update(collect_union_uniform_inputs())
return {
"required": {
"image": ("IMAGE",),
"shader": (shader_options,),
"enabled": ("BOOLEAN", {"default": True}),
},
"optional": optional,
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "process"
CATEGORY = "GLSL"
def process(self, image, shader, enabled, mask=None, **kwargs):
if not enabled:
return (image,)
if shader == "(no shaders found)":
raise RuntimeError("GLSL GPU Processor: no .glsl files under shaders/")
runtime = get_glsl_runtime()
source = load_shader_source(shader)
metadata = enrich_metadata(source)
uniforms = extract_uniform_values(metadata, kwargs)
if mask is None:
for inp in metadata.get("inputs", []):
if inp.get("type") == "MASK" and inp.get("name") in kwargs:
mask = kwargs.get(inp["name"])
break
result = runtime.execute(
source,
metadata,
image,
mask,
mode="simple",
uniforms=uniforms,
)
return (result,)
class GLSLShaderNode:
"""Interactive GLSL authoring node (simple process(), compute, or GLES fragment)."""
@classmethod
def INPUT_TYPES(cls):
optional = {
"mask": ("MASK",),
}
optional.update(SHADER_NODE_SLOT_INPUTS)
return {
"required": {
"shader_source": ("STRING", {"multiline": True, "default": DEFAULT_SIMPLE_SHADER}),
"shader_mode": (["auto", "simple", "advanced", "fragment"], {"default": "auto"}),
"image": ("IMAGE",),
"enabled": ("BOOLEAN", {"default": True}),
"time": ("FLOAT", {"default": 0.0, "min": -1e6, "max": 1e6, "step": 0.01}),
"save_shader": ("BOOLEAN", {"default": False}),
"save_name": ("STRING", {"default": "Inline_Shader"}),
},
"optional": optional,
}
RETURN_TYPES = ("IMAGE", "STRING")
RETURN_NAMES = ("image", "status")
FUNCTION = "process"
CATEGORY = "GLSL"
def process(
self,
shader_source,
shader_mode,
image,
enabled,
time,
save_shader,
save_name,
mask=None,
**kwargs,
):
if not enabled:
return (image, "Skipped (disabled)")
if not shader_source.strip():
return (image, "Skipped (empty shader)")
runtime = get_glsl_runtime()
metadata = enrich_metadata(shader_source)
metadata, uniforms = _merge_shader_node_slots(metadata, kwargs)
mode = "simple" if shader_mode in ("auto", "fragment", "simple") else "advanced"
try:
result = runtime.execute(
shader_source,
metadata,
image,
mask,
mode=mode,
uniforms=uniforms,
time=float(time),
)
status = f"OK ({shader_mode})"
if save_shader:
runtime.save_shader(shader_source, f"user/{save_name}")
status = f"OK + saved user/{save_name}.glsl"
return (result, status)
except Exception as e:
raise RuntimeError(f"GLSL Shader Error: {e}") from e
class GLSLGPUDiagnostics:
"""Reports system diagnostics for GLSL GPU backend."""
@classmethod
def INPUT_TYPES(cls):
return {"required": {}}
RETURN_TYPES = ("STRING",)
FUNCTION = "report"
CATEGORY = "GLSL"
def report(self):
runtime = get_glsl_runtime()
try:
diagnostics = runtime.get_diagnostics()
return (json.dumps(diagnostics, indent=2),)
except Exception as e:
return (f"Error: {e}",)
NODE_CLASS_MAPPINGS = {
"GLSLGPUProcessor": GLSLGPUProcessor,
"GLSLShaderNode": GLSLShaderNode,
"GLSLGPUDiagnostics": GLSLGPUDiagnostics,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"GLSLGPUProcessor": "GLSL GPU Processor",
"GLSLShaderNode": "GLSL Shader",
"GLSLGPUDiagnostics": "GLSL GPU Diagnostics",
}