Skip to content

Commit

Permalink
multi-image for standalone shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
Amorano committed Aug 1, 2024
1 parent 4b8ddb0 commit 1c7240a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
37 changes: 26 additions & 11 deletions core/create_glsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ async def jovimetrix_glsl(request) -> Any:

class GLSLNodeBase(JOVImageNode):
CATEGORY = f"JOVIMETRIX 🔺🟩🔵/GLSL"
FRAGMENT = None
VERTEX = None
VERTEX = GLSLShader.PROG_VERTEX
FRAGMENT = GLSLShader.PROG_FRAGMENT

@classmethod
def INPUT_TYPES(cls) -> dict:
Expand All @@ -108,33 +108,35 @@ def __init__(self, *arg, **kw) -> None:
self.__delta = 0

def run(self, ident, **kw) -> tuple[torch.Tensor]:
vertex = parse_param(kw, Lexicon.PROG_VERT, EnumConvertType.STRING, "")[0]
fragment = parse_param(kw, Lexicon.PROG_FRAG, EnumConvertType.STRING, "")[0]
batch = parse_param(kw, Lexicon.BATCH, EnumConvertType.INT, 0, 0, 1048576)[0]
delta = parse_param(kw, Lexicon.TIME, EnumConvertType.FLOAT, 0)[0]

self.__glsl.fps = parse_param(kw, Lexicon.FPS, EnumConvertType.INT, 24, 1, 120)[0]

# everybody wang comp tonight
mode = parse_param(kw, Lexicon.MODE, EnumConvertType.STRING, EnumScaleMode.NONE.name)[0]
mode = EnumScaleMode[mode]

wihi = parse_param(kw, Lexicon.WH, EnumConvertType.VEC2INT, [(512, 512)], MIN_IMAGE_SIZE)[0]
sample = parse_param(kw, Lexicon.SAMPLE, EnumConvertType.STRING, EnumInterpolation.LANCZOS4.name)[0]
sample = EnumInterpolation[sample]

matte = parse_param(kw, Lexicon.MATTE, EnumConvertType.VEC4INT, [(0, 0, 0, 255)], 0, 255)[0]

variables = kw.copy()
for p in [Lexicon.MODE, Lexicon.WH, Lexicon.SAMPLE, Lexicon.MATTE, Lexicon.PROG_VERT, Lexicon.PROG_FRAG, Lexicon.BATCH, Lexicon.TIME, Lexicon.FPS]:
variables.pop(p, None)

self.__glsl.size = wihi
try:
self.__glsl.vertex = vertex
self.__glsl.fragment = fragment
self.__glsl.vertex = self.VERTEX
self.__glsl.fragment = self.FRAGMENT
except CompileException as e:
comfy_message(ident, "jovi-glsl-error", {"id": ident, "e": str(e)})
logger.error(self.NAME)
logger.error(e)
return

self.__glsl.size = wihi
self.__glsl.fps = parse_param(kw, Lexicon.FPS, EnumConvertType.INT, 24, 1, 120)[0]

if batch > 0:
self.__delta = delta
step = 1. / self.__glsl.fps
Expand All @@ -155,14 +157,13 @@ def run(self, ident, **kw) -> tuple[torch.Tensor]:
vars[k] = var

w, h = wihi
mode = EnumScaleMode[mode]
if firstImage is not None and mode == EnumScaleMode.NONE:
h, w = firstImage.shape[:2]

self.__glsl.size = (w, h)
img = self.__glsl.render(self.__delta, **vars)
if mode != EnumScaleMode.NONE:
sample = EnumInterpolation[sample]

img = image_scalefit(img, w, h, mode, sample)
img = cv2tensor_full(img, matte)
images.append(img)
Expand Down Expand Up @@ -193,6 +194,13 @@ def INPUT_TYPES(cls) -> dict:
d['optional'] = opts
return Lexicon._parse(d, cls)

def __init__(self, *arg, **kw) -> None:
self.VERTEX = parse_param(kw, Lexicon.PROG_VERT, EnumConvertType.STRING, GLSLShader.PROG_VERTEX)[0]
self.FRAGMENT = parse_param(kw, Lexicon.PROG_FRAG, EnumConvertType.STRING, GLSLShader.PROG_FRAGMENT)[0]
super().__init__(*arg, **kw)



class GLSLNodeDynamic(GLSLNodeBase):

PARAM = None
Expand Down Expand Up @@ -229,6 +237,13 @@ def INPUT_TYPES(cls) -> dict:
original_params['optional'] = data
return Lexicon._parse(original_params, cls)

'''
def __init__(self, *arg, **kw) -> None:
kw[Lexicon.PROG_VERT] = self.VERTEX
kw[Lexicon.PROG_FRAG] = self.FRAGMENT
super().__init__(*arg, **kw)
'''

def import_dynamic() -> Tuple[str,...]:
ret = []
global GLSL_PROGRAMS
Expand Down
45 changes: 23 additions & 22 deletions sup/shader.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,31 @@ class CompileException(Exception): pass

class GLSLShader:
PROG_HEADER = """
#version 440
#version 440
precision highp float;
precision highp float;
uniform vec3 iResolution;
uniform vec4 iMouse;
uniform float iTime;
uniform float iTimeDelta;
uniform float iFrameRate;
uniform int iFrame;
uniform vec3 iResolution;
uniform vec4 iMouse;
uniform float iTime;
uniform float iTimeDelta;
uniform float iFrameRate;
uniform int iFrame;
#define texture2D texture
"""
#define texture2D texture
"""

PROG_FOOTER = """
layout(location = 0) out vec4 _fragColor;
layout(location = 0) out vec4 _fragColor;
void main()
{
mainImage(_fragColor, gl_FragCoord.xy);
}
"""
void main()
{
mainImage(_fragColor, gl_FragCoord.xy);
}
"""

PROG_FRAGMENT = """uniform sampler2D image;
PROG_FRAGMENT = """
uniform sampler2D image;
void mainImage( out vec4 fragColor, vec2 fragCoord ) {
vec2 uv = fragCoord.xy / iResolution.xy;
Expand All @@ -90,7 +91,9 @@ class GLSLShader:
}
"""

PROG_VERTEX = """#version 330 core
PROG_VERTEX = """
#version 330 core
void main()
{
vec2 verts[3] = vec2[](vec2(-1, -1), vec2(3, -1), vec2(-1, 3));
Expand Down Expand Up @@ -253,17 +256,15 @@ def vertex(self) -> str:

@vertex.setter
def vertex(self, program:str) -> None:
if program != self.__source_vertex_raw:
self.__init_program(vertex=program)
self.__init_program(vertex=program)

@property
def fragment(self) -> str:
return self.__source_fragment_raw

@fragment.setter
def fragment(self, program:str) -> None:
if program != self.__source_fragment_raw:
self.__init_program(fragment=program)
self.__init_program(fragment=program)

@property
def size(self) -> Tuple[int, int]:
Expand Down

0 comments on commit 1c7240a

Please sign in to comment.