How do variable declarations work in C2 shaders?

Share your Construct 2 effect files

Post » Fri Mar 27, 2015 3:35 am

Hi all,

I'm trying to write a shader for my game. I got it up and running in shadertoy, and now I'm trying to port it across to C2.
However, looking at C2 shaders, they seem to be formatted quite differently to the GLSL code on Shadertoy. Specifically, the variable declarations seem to need a lot more specifiers like 'varying', 'uniform', and 'mediump'. Do I really need to specify varying and precision for every single variable? And how do I specify things like 'const'?

Cheers!
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,699

Post » Fri Mar 27, 2015 6:10 pm

If the shader is working with shadertoy it should work fine as is I imagine, since they're both webgl.

Here's a helpful link:
http://www.shaderific.com/glsl-qualifiers/

"varying" is only for data shared with the vertex shader. In C2 those variables are fixed and usually I just find what values are varying from other shaders.
"uniform" is only for the shader parameters you can set later.

You can omit the precision specifiers and a default precision will be used. You can even change what that default is. So if you just have a variable you use in the shader it's fine to just write "float foo" or "vec2 bar".

You should be able to use "const" the same way as with shadertoy.
B
94
S
33
G
118
Posts: 5,394
Reputation: 75,849

Post » Sat Mar 28, 2015 7:19 am

@R0j0hound thanks!

I think I'm missing something though - the compiler doesn't seem to agree that you can omit the precision.

Image

EDIT: Never mind, turns out I need to add the line
Code: Select all
precision mediump float;
to the beginning of the program.
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,699

Post » Sat Mar 28, 2015 10:03 am

OK, I got it mostly working, however I think I'm in a spot of trouble. @R0j0hound if you have any ideas that would be fantastic.

My shader has a for loop that loops from a specific value to a specific value. e.g.

Code: Select all
for (int i = START_VALUE; i <  END_VALUE; i++)


In shadertoy, these were just set with consts and life was good. However, it's problematic in C2. Basically, I want to be able to see these loop values at edit-time, presumably via an effect parameter. However GLSL simply will not allow you to use non const loop values at all, even if they are in reality constant.

The reason these need to be set at edit time is that I will need a few instances of this shader, all with different loop values.

Ugh, I have a feeling this won't be possible without an addition to C2's effect system :(

EDIT: I suppose I could use something like this hack, but I have a bad feeling that on some hardware I'd pay dearly :(
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,699

Post » Sat Mar 28, 2015 6:38 pm

I'm pretty sure the effects are compiled before the start of the layout, so the constant become unmodifiable. I suppose if you could edit the source and re-compile the shader at runtime the constant could be changed, but there isn't anything in place to do that currently.

I'd go with the idea in your link and test how it performs.
B
94
S
33
G
118
Posts: 5,394
Reputation: 75,849

Post » Sun Mar 29, 2015 4:09 am

Yeah, you can't use anything besides a constant for loops, it's a limitation you have to deal with. The loops are unrolled when compiled, so it needs a well defined value.
Like Rojo said, the only way to make it work properly is to compile the shader at runtime with a different constant, but C2 doesn't allow that.

The trick you mentioned can work for convenience in some cases, but probably will impact performance at least in some hardwares.
If you only need a limited number of different values for the loop it's best to make multiple shaders with different preset constants. It's very inelegant and inconvenient, but it's what we have now.
Scirra Employee
B
159
S
55
G
17
Posts: 711
Reputation: 18,177

Post » Sun Mar 29, 2015 5:38 am

@Animmaniac thanks Paulo. Interesting that the loops are unrolled, but I guess it makes sense.

I guess I'll just go with this hack for now and if I hear reports of bad performance I'll resort to the multiple shaders method.
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,699

Post » Tue Mar 31, 2015 3:14 am

that shadertoy is a cool site, anyone know of a resource of where to start with shaders? I don't know much about them or where to start. I know how to code, but I don't totally have a grasp on what shaders actually do. I've loaded custom shaders in Minecraft and seen some here, but that's about it.

thanks..
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865


Return to Effects

Who is online

Users browsing this forum: No registered users and 0 guests