Yggdrasil
Posts: 138
Joined: Sun Aug 26, 2012 8:45 pm

OpenGLES problem with GL_OES_EGL_image_external extension

Tue May 20, 2014 6:54 pm

Hello,

I'm using raspivid with the OpenGLES output variant. This variant uses a shader with the GL_OES_EGL_image_external extension. I changed the example shader from raspivid/gl_scenes/mirror.c to
the following shader ( the shader is probably not the core of my problem)

Code: Select all

static RASPITEXUTIL_SHADER_PROGRAM_T pong_shader = { 
    .vertex_source =
    "attribute vec2 vertex;\n"
    "varying vec2 texcoord;\n"
    "varying vec2 scorecoordLeft;\n"
    "varying vec2 scorecoordRight;\n"
    "uniform vec2 score;\n"
    "uniform vec4 scorePosLeft;\n"
    "uniform vec4 scorePosRight;\n"
    "void main(void) {\n"
    "   texcoord = 0.5 * (1.0 - vertex );\n"
    "   vec2 leftWH = vec2( scorePosLeft.z-scorePosLeft.x, scorePosLeft.a - scorePosLeft.y );\n"
    "   vec2 leftPos = vec2( (texcoord.x-scorePosLeft.x)/(leftWH.x), (texcoord.y-scorePosLeft.y)/(leftWH.y) );\n"
    "   scorecoordLeft.x = score.x*0.1+leftPos.x*0.1;\n"
    "   scorecoordLeft.y = leftPos.y;\n"
    "   vec2 rightWH = vec2( scorePosRight.z-scorePosRight.x, scorePosRight.a - scorePosRight.y );\n"
    "   vec2 rightPos = vec2( (texcoord.x-scorePosRight.x)/(rightWH.x), (texcoord.y-scorePosRight.y)/(rightWH.y) );\n"
    "   scorecoordRight.x = score.y*0.1+rightPos.x*0.1;\n"
    "   scorecoordRight.y = rightPos.y;\n"
    "   gl_Position = vec4(vertex, 0.0, 1.0);\n"
    "}\n",
    .fragment_source =
    "#extension GL_OES_EGL_image_external : require\n"
    "uniform samplerExternalOES aaatex;\n"
    "uniform sampler2D numerals;\n"
    "uniform float offset;\n"
    "uniform vec2 border;\n"
    "uniform vec4 scorePosLeft;\n"
    "uniform vec4 scorePosRight;\n"
    "const float waves = 2.0;\n"
    "varying vec2 texcoord;\n"
    "varying vec2 scorecoordLeft;\n"
    "varying vec2 scorecoordRight;\n"
    "void main(void) {\n"
    "    float x = texcoord.x + 0.00 * sin(offset + (texcoord.y * waves * 2.0 * 3.141592));\n"
    "    float y = texcoord.y + 0.00 * sin(offset + (texcoord.x * waves * 2.0 * 3.141592));\n"
    "    if (y < 1.0 && y > 0.0 && x < 1.0 && x > 0.0) {\n"
    "     vec4 ret = texture2D(aaatex, texcoord);\n"
    "     if( x < border.x || x > border.y ){\n"
    "      ret.r += 0.2;\n"
    "     }\n"
    "     if( x < scorePosLeft.z && x > scorePosLeft.x && y < scorePosLeft.w && y > scorePosLeft.y ){\n"
    "       ret = ret + 0.3*texture2D(numerals, scorecoordLeft);\n"
    "     }\n"
    "     if( x < scorePosRight.z && x > scorePosRight.x && y < scorePosRight.w && y > scorePosRight.y ){\n"
    "       ret = ret + 0.3*texture2D(numerals, scorecoordRight);\n"
    "     }\n"
    "     gl_FragColor = ret;\n"
    "    }\n"
    "    else {\n"
    "       gl_FragColor = vec4(0.0, 0.0, 0.5, 1.0);\n"
    "    }\n"
    "     gl_FragColor.a = 1.0;\n"
    "}\n",
    .uniform_names = {"aaatex", "offset", "numerals", "border", "score", "scorePosLeft", "scorePosRight"},
    .attribute_names = {"vertex"},
};
This shader runs, but it occurs a strange problem: I can not call any other shader (glUseProgram) after the first call of this shader! The application stops with "glGetError 0x502".
Did anyone know the pitfalls in the usage of the extension? I've found a thread http://stackoverflow.com/questions/1337 ... nary-textu , with a similar problem description, but it doesn't helps me to fix the problem.

Please ask, If you need more information. I can't strip the issue to simple example code, but my full code can be found on https://github.com/YggdrasiI/RPIMotionD ... /tree/pong (pong branch) .
It requires the raspi camera .

In the above version the pong app should be running after, the call of
cd build/apps/pong
./pong -x /dev/null -t 0 --preview '0,0,800,600' --gl --glwin '0,0,800,600' --glscene pong -o /dev/null

After commenting in Line 260 in apps/raspicam/gl_scenes/pong.c (RedrawTextures() ), which calls the other shaders, the program should crash.

Regards Yggdrasil

lagurus
Posts: 46
Joined: Wed Aug 07, 2013 8:02 am

Re: OpenGLES problem with GL_OES_EGL_image_external extensio

Fri May 23, 2014 9:01 am

I have modified your code (see attachment). It's only one file (pong.c) which
causes the problems (I hope).

You can also look at https://github.com/lagurus/userland_bgs where I have used some shaders together.
Attachments
pong.tar
(20 KiB) Downloaded 184 times

Yggdrasil
Posts: 138
Joined: Sun Aug 26, 2012 8:45 pm

Re: OpenGLES problem with GL_OES_EGL_image_external extensio

Fri May 23, 2014 11:00 am

Hello Lagurus,

many thanks! You found out the solution of my issue. :)
If somebody made the same mistake: The shader variables for textures has to be placed in front of all other variables.

Code: Select all

Wrong: .uniform_names = {"texture1", "uniform1", "texture2", ...}
Right: .uniform_names = {"texture1", "texture2", "uniform1",...}
An other pitfall are uniform names which will be removed by the shader compiler. => You need at least one definition of each variable. A declaration is not enough..
The lazy variant, to add a statement like 'foo = foo + 0.0*bar', won't help and bar will be removed by the compiler.


@Lagures: In the Readme of your project is a tiny issue for users. At the beginning you recommend 'cmake . ; make' but the following
commands in the files matched to 'mkdir build; cd build ; cmake ..'.

Yggdrasil
Posts: 138
Joined: Sun Aug 26, 2012 8:45 pm

Re: OpenGLES problem with GL_OES_EGL_image_external extensio

Fri May 23, 2014 12:04 pm

In my excitement I was a little too hasty. I has to invest some more time to find out why above code runs and my shaders not.

Edit: Some further tests confirms that the uniform ordering was the culprit.

Return to “OpenGLES”