I am starting to play with shaders. I’ve read Ignatz tuto, the quisheet, the spec. (quick read).
I cant find how to pass an array to my fragment shader from codea.
What is the point? I want to make a generic convolution shader and pass the parameters from codea. For example, a 5x5 filter means passing 25 vec2 relative positions, and 25 float weights. So i want to put those into an array, init the array once, and use it inside the fragment shader main.
Solution 1: i could compute them in the shader main, as a local array, but then they will be computed again and again for each displayed pixel? This wont be very efficient, i want to compute them once only.
Solution 2: i could declare the array and init it before the main. No this is not possible, as states the GLSL spec itself.
Solution 3: i could generate the shader code automatically form lua and then pass this to the mesh. This will work but is very un-elegant, that will be my worst case fallback.
Has anyone a solution for my problem?
thanks in advance!
@Jmv38 - my recollection is you can have 1D arrays but not 2D in shaders (but it is a while since I read that)
Solution 4: put the array in a dummy image, you can use the R values for positions and G for weights. That is what I would do. The blend shader in the shader lab shows how to include a second image, it is easy.
@Ignatz good suggestion, thanks.
So there doesn’t seem to be a direct way to pass a 1D array, right? (the array fetching with ‘attribute’ doesnt count because it is only for the vertex shader, and it is interpolated too when passing to the fragment shader, so not really ‘elegant’).
@Jmv38 - I never really used arrays with shaders, so I don’t know the answer.
@Jmv38 I’m not too sure about arrays but the electricity shader (one of the standards) uses a grid and rasters lines between randomly chosen points to create the effect.
Although try this,
uniform float arraytest
arraytest = 0.1;
arraytest = 0.2;
arraytest = 0.3;
arraytest is defined by the number 3 meaning it has a size of 3 but starts at 0, so we only go to 2. I haven’t used arrays myself with shaders either, never really found a use for them in shaders.
i know about array definition in shaders.
What i dont know is how to fill them from lua?
@ignatz i’ve just tried your solution4.
it works fine (as long as i am really careful with type casting).
but the ipad performance is disappointing:
- 3x3 : 60 Hz.
- 5x5 : 30 Hz.
- 7x7 : 15 Hz. (with 1s lag).
that was interesting to do anyway.
@Jmv38 Ahh sorry I thought you wanted the definition. Afaik you will struggle, have you tried indexing it like other uniforms, using shader.arraytest if it’s possible?
@luatee i have tried this:
in my fragment shader:
uniform highp float pos;
in my lua program :
ms.shader.pos = 5.0
generates the error: "attemp to index field 'pos' (a nil value)"
so it doesnt work.
Okay thanks for clearing that for me
@Jmv38 I believe the syntax is
uniform highp float pos;.
@skythecoder no, the spec is:
Some examples are:
uniform vec4 lightPosition;