aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2025-03-18 14:29:33 +0000
committers-ol <s+removethis@s-ol.nu>2025-03-18 14:29:33 +0000
commit2b26cf145e9698c8d279cd38f447100388f05502 (patch)
tree4ffdab001ccc834674a12e604c098529f0db1ab8
parentexamples: add OSC in/out example (diff)
downloadalive-2b26cf145e9698c8d279cd38f447100388f05502.tar.gz
alive-2b26cf145e9698c8d279cd38f447100388f05502.zip
lib: clean up glsl-view uniform type override logic
-rw-r--r--alv-lib/glsl-view.moon66
1 files changed, 33 insertions, 33 deletions
diff --git a/alv-lib/glsl-view.moon b/alv-lib/glsl-view.moon
index 5438cc3..eb690b7 100644
--- a/alv-lib/glsl-view.moon
+++ b/alv-lib/glsl-view.moon
@@ -1,8 +1,7 @@
-import Constant, Op, TemplateString, Input, T, Array, sig, const from require 'alv.base'
+import Constant, Op, TemplateString, Input, T, Array, Struct, sig, const from require 'alv.base'
import Message, Bundle, Timetag, add_item from require 'alv-lib._osc'
import dns, udp from require 'socket'
-ALV_TYPES = {}
GLSL_TYPES =
num: 'float'
bool: 'bool'
@@ -14,35 +13,28 @@ for i=2,4
for j=2,4
GLSL_TYPES["num[#{j}][#{i}]"] = "mat#{i}x#{j}"
-for p in *{'', 'i', 'g'}
- ALV_TYPES["glsl/#{p}sampler2D"] = T.str
- GLSL_TYPES["glsl/#{p}sampler2D"] = "#{p}sampler2D"
- ALV_TYPES["glsl/#{p}sampler2DArray"] = T.str
- GLSL_TYPES["glsl/#{p}sampler2DArray"] = "#{p}sampler2DArray"
- ALV_TYPES["glsl/#{p}sampler3D"] = T.str
- GLSL_TYPES["glsl/#{p}sampler3D"] = "#{p}sampler3D"
-
uniform = Constant.meta
meta:
name: 'uniform'
summary: "override uniform binding type."
examples: { '(uniform glsl-type value)' }
- description: "Set a specific GLSL type for this value.
-
-For example to send a string to glsl-view and load an image into a sampler:
+ description: "Generates a struct as received by [shader][]:
- vec4 color = texture(#(uniform 'sampler2D' 'image.png'), uv);"
+`(uniform 'sampler2D' 'image.png')` is equivalent to `{'type' 'sampler2d' 'val' 'image.png'}`."
value: class extends Op
- pattern = const.str + sig!
+ pattern = sig.str + sig!
setup: (inputs, scope) =>
- { type, value } = pattern\match inputs
+ { type, val } = pattern\match inputs
- super Input.hot value
+ super
+ type: Input.hot type
+ val: Input.hot val
- @setup_out '~', T["glsl/#{type.result!}"]
+ @setup_out '~', Struct type: T.str, val: @inputs.val\type!
- tick: => @out\set @inputs!
+ tick: =>
+ @out\set @unwrap_all!
shader = Constant.meta
meta:
@@ -55,33 +47,40 @@ shader = Constant.meta
- `code-tpl-str` is a `str=` that can contain `~-stream` template substitutions
that are mapped as follows:
- `str`: substituted directly as GLSL
+ - `{type: str val: …}`: reference to uniform of type `type` (instead of infered as follows)
- `num`, `bool`: reference to `uniform float/bool`
- `num[I]`, `bool[I]`: reference to `uniform vecI/bvecI`
- - `num[I][J]`: reference to `uniform matIxJ`
- - `glsl-view/uniform`: reference to uniform of declared type
-
-`params` are substituted in `code` as uniforms using TemplateString logic"
+ - `num[I][J]`: reference to `uniform matIxJ`"
value: class extends Op
pattern = -sig['osc/out'] + const.str + sig!^0
+ default_sender = (input) -> input\type!, input!
get_code: =>
uniforms = { '#version 330 core' }
@state = names: {}, types: {}
code = TemplateString.subst @inputs.code!, (i) ->
param = @inputs.params[i]
- typename = tostring param\type!
- if typename == 'str'
- return param!
+ type = param\type!
+ return param! if type == T.str
id = tostring math.random!
name = "alv_param_#{id\sub 3}"
- typestr = assert GLSL_TYPES[typename], "unsupported type #{typename}"
+ typestr = GLSL_TYPES[tostring type]
+
+ 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
+ else
+ assert typestr, "unsupported type #{type}"
+ @state[i] = -> "/#{name}", type, param!
+
table.insert uniforms, "uniform #{typestr} #{name};"
- @state.types[i] = ALV_TYPES[typename] or param\type!
- @state.names[i] = name
name
@@ -104,10 +103,11 @@ shader = Constant.meta
for i, input in ipairs @inputs.params
if all or input\dirty!
- if name = @state.names[i]
- msg = Message.new "/#{name}"
- add_item msg, @state.types[i], input!
- bundle\add msg
+ 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!