How do variable declarations work in C2 shaders?

  • 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!

  • 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.

  • R0J0hound thanks!

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

    EDIT: Never mind, turns out I need to add the line

    precision mediump float;[/code:19tugnqz] to the beginning of the program.
  • 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.

    for (int i = START_VALUE; i <  END_VALUE; i++)[/code:36gm9ilc] 
    
    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 [url=http://gmc.yoyogames.com/index.php?showtopic=616107]this hack[/url], but I have a bad feeling that on some hardware I'd pay dearly
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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.

  • 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.

  • 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..

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)