Help converting a shader from v120 to v150

#1

Hi,
I am experimenting with ofxHapPlayer and it works well, but there is an issue that I would like to fix. When decoding HAPQ movies the addon uses a shader, that is written for the standard renderer with GLSL version 1.2. My app uses the programmable renderer and hence needs the shaders to be written with GLSL 1.5 (that may not be true, my knowledge of the matter is quite shaky).

I tried porting the shaders to the new version but I am missing something. Can someone help me with this?

The current shaders are these (vertex and fragment):

void main(void)
{
    gl_FrontColor = gl_Color;
    gl_Position = ftransform();
    gl_TexCoord[0] = gl_MultiTexCoord0;
}
uniform sampler2D cocgsy_src;
const vec4 offsets = vec4(-0.50196078431373, -0.50196078431373, 0.0, 0.0);

void main()
{
    vec4 CoCgSY = texture2D(cocgsy_src, gl_TexCoord[0].xy);\
    CoCgSY += offsets;\
    float scale = ( CoCgSY.z * ( 255.0 / 8.0 ) ) + 1.0;\
    float Co = CoCgSY.x / scale;\
    float Cg = CoCgSY.y / scale;\
    float Y = CoCgSY.w;\
    vec4 rgba = vec4(Y + Co - Cg, Y + Cg, Y - Co - Cg, 1.0);\
    gl_FragColor = rgba * gl_Color;\
}

The (not working) ones I wrote trying to convert them to GLSL 1.5 are like this:

#version 150

uniform mat4 modelViewProjectionMatrix;
in vec4 position;

void main()
{
    gl_Position = modelViewProjectionMatrix * position;
}
#version 150

uniform sampler2DRect cocgsy_src;
const vec4 offsets = vec4(-0.50196078431373, -0.50196078431373, 0.0, 0.0);
out vec4 outputColor;
in vec4 color;

void main()
{
    vec4 CoCgSY = texture(cocgsy_src, vec2(gl_FragCoord.x, gl_FragCoord.y));
    CoCgSY += offsets;
    float scale = ( CoCgSY.z * ( 255.0 / 8.0 ) ) + 1.0;
    float Co = CoCgSY.x / scale;
    float Cg = CoCgSY.y / scale;
    float Y = CoCgSY.w;
    vec4 rgba = vec4(Y + Co - Cg, Y + Cg, Y - Co - Cg, 1.0);
    outputColor = rgba * color;
}

Thanks for helping
Davide

#2

Hello Davide, a couple of questions:

  • Do you have any error in the IDE?
  • What if you print to screen the CoCGSY variable with outputColor = CoCgSY ? do you see something?
  • How are you setting the texture in your shader?

btw, gl_FragCoord it is already a vec2 to, therefore:

vec4 CoCgSY = texture(cocgsy_src, vec2(gl_FragCoord.x, gl_FragCoord.y));
can be

vec4 CoCgSY = texture(cocgsy_src, gl_FragCoord);

Also, when working with shaders, I find very useful the ofxAutoReloaded plugin, because you can edit the shaders and it automatically recompile and reload them.

#3

I will definitely have to learn better how GLSL work. A question… in OF examples the GLSL version is often 150, but with OpenGL 3.3 version 330 should work as well. Since I wouldn’t like to start learning something obsolete, is there any drawback about using #version 330 in an OF shader?

#4

In my opinion, the only drawback is that 330 syntax it’s more different than the shader’s syntax that you usually use with webGL (openGL ES, version 2.0), that is the most portable syntax because it runs everywhere. But except from that, I do not see any drawback. If you are on win/linux, probably it make sense to have a look at 430, so that you can use compute shaders. 430 is not available on mac, that supports only openGL until 4.1.

Anyway, things change fast, my suggestion is to start from one version learning the concepts, and then learn how to adapt your code from one version to another.

Here a list with all the syntax and a brief intro about the differences: