aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2025-03-18 18:39:23 +0000
committers-ol <s+removethis@s-ol.nu>2025-03-19 16:57:47 +0000
commit5bfe21f49b3e9f0a29d3642a2200a8d5ee15d4ef (patch)
treeeca49378ffa746d1ca0c4128077ecac4bf612821
parentlib: clean up glsl-view uniform type override logic (diff)
downloadalive-5bfe21f49b3e9f0a29d3642a2200a8d5ee15d4ef.tar.gz
alive-5bfe21f49b3e9f0a29d3642a2200a8d5ee15d4ef.zip
lib: new glsl-view OSC address space
-rw-r--r--alv-lib/glsl-view.moon87
1 files changed, 66 insertions, 21 deletions
diff --git a/alv-lib/glsl-view.moon b/alv-lib/glsl-view.moon
index eb690b7..4b859b3 100644
--- a/alv-lib/glsl-view.moon
+++ b/alv-lib/glsl-view.moon
@@ -13,6 +13,17 @@ for i=2,4
for j=2,4
GLSL_TYPES["num[#{j}][#{i}]"] = "mat#{i}x#{j}"
+GLSL_SAMPLER_TYPES =
+ "1D": 'sampler1D'
+ "2D": 'sampler2D'
+ "3D": 'sampler3D'
+ "CUBE_MAP": 'samplerCube'
+ "RECTANGLE": 'sampler2DRect'
+ "1D_ARRAY": 'sampler1DArray'
+ "2D_ARRAY": 'sampler2DArray'
+ "CUBE_MAP_ARRAY": 'samplerCubeArray'
+ "BUFFER": 'samplerBuffer'
+
uniform = Constant.meta
meta:
name: 'uniform'
@@ -36,6 +47,40 @@ uniform = Constant.meta
tick: =>
@out\set @unwrap_all!
+texture = Constant.meta
+ meta:
+ name: 'texture'
+ summary: "override uniform binding type."
+ examples: { '(texture [socket] name filename [type])' }
+ description: "Generates a struct as received by [shader][]:
+
+`(uniform 'sampler2D' 'image.png')` is equivalent to `{'type' 'sampler2d' 'val' 'image.png'}`."
+
+ value: class extends Op
+ pattern = -sig['osc/out'] + const.str + const.str + -const.str
+ setup: (inputs, scope) =>
+ { socket, name, filename, type } = pattern\match inputs
+
+ super
+ socket: Input.hot socket or scope\get '*oscout*'
+ name: Input.hot name
+ filename: Input.hot filename
+ type: Input.hot type or Constant.str "2D"
+
+ samplertype = assert GLSL_SAMPLER_TYPES[@inputs.type!]
+ @setup_out '=', (Struct type: T.str, val: T.str), type: samplertype, val: @inputs.name!
+
+ tick: =>
+ { :socket, :name, :type, :filename } = @unwrap_all!
+
+ with @inputs.socket!
+ \send Message.pack { address: "/texture/#{name}", types: "ss", "TEXTURE_#{type}", filename }
+
+ -- destroy: =>
+ -- name = @inputs.name!
+ -- with @inputs.socket!
+ -- \send Message.pack { address: "/texture/#{name}/destroy", types: "" }
+
shader = Constant.meta
meta:
name: 'shader'
@@ -59,7 +104,7 @@ shader = Constant.meta
get_code: =>
uniforms = { '#version 330 core' }
- @state = names: {}, types: {}
+ @state = {}
code = TemplateString.subst @inputs.code!, (i) ->
param = @inputs.params[i]
type = param\type!
@@ -70,15 +115,16 @@ shader = Constant.meta
typestr = GLSL_TYPES[tostring type]
+ -- NOTE: can't close over @ or input or breaks when forked
if type.__class == Struct
assert type.types.type == T.str, "struct substituton needs type key"
val_type = assert type.types.val, "struct substituton needs val key"
typestr = param!.type
- @state[i] = -> "/#{name}", val_type, param!.val
+ @state[i] = => "/uniform/#{name}", val_type, @inputs.params[i]!.val
else
assert typestr, "unsupported type #{type}"
- @state[i] = -> "/#{name}", type, param!
+ @state[i] = => "/uniform/#{name}", type, @inputs.params[i]!
table.insert uniforms, "uniform #{typestr} #{name};"
@@ -97,25 +143,23 @@ shader = Constant.meta
tick: (setup) =>
bundle = Bundle.new Timetag.new!
-
- compile = if setup or @inputs.code\dirty! then @get_code!
- all = compile or @inputs.socket\dirty!
+ recompile = setup or @inputs.code\dirty! or @inputs.socket\dirty!
+ if recompile
+ bundle\add content: { address: '/shader', types: 's', @get_code! }
for i, input in ipairs @inputs.params
- if all or input\dirty!
- if sender = @state[i]
- addr, type, val = sender!
- bundle\add with msg = Message.new addr
- print addr, type, val
- add_item msg, type, val
- else
- compile or= @get_code!
-
- if compile
- msg = { address: '/-/shader', types: 's', compile }
- table.insert bundle.content, 1, msg
-
- with sock = @inputs.socket!
+ continue unless recompile or input\dirty!
+
+ if sender = @state[i]
+ addr, type, val = sender @
+ bundle\add with msg = Message.new addr
+ add_item msg, type, val
+ elseif not recompile
+ -- text-only piece is dirty, force recompile
+ return @tick true
+
+ return unless #bundle.content > 0
+ with @inputs.socket!
\send Bundle.pack bundle.content
Constant.meta
@@ -124,5 +168,6 @@ Constant.meta
summary: "glsl-view integration via OSC."
value:
- :shader
:uniform
+ :texture
+ :shader