Elvis Lee
Posts: 2
Joined: Tue Jul 04, 2017 2:42 am

Seems glStencilMask(0x0) is not working.

Tue Jul 04, 2017 3:06 am

I'm a quite newbie for PI and not an expert for opengl stuffs.
But I feel glStencilMask is not working on an app to test stencil.
Could you check this is a bug or known issue or some misunderstanding?

*Environment
Raspberry PI 3
Mesa 12.0.2
DRI - vc4_dri

* Symptom
Stencil buffer is updated by glStencilOp(GL_INVERT, GL_KEEP, GL_KEEP) even after glStencilMask(0x0).
(I'm not quite sure that this is normal behaviour, but I feel it shouldn't as it didn't on other device.)

* Code

Code: Select all

...
    glStencilFunc(GL_EQUAL, 0x2, 0x1);
    glStencilOp(GL_INVERT, GL_KEEP, GL_KEEP);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[3]);
...
 // Save stencil mask
    GLint stencilMask;
    glGetIntegerv(GL_STENCIL_WRITEMASK, &stencilMask);

   glStencilMask(0x0);

   for (i = 0; i < NumTests; ++i)
   {
        glStencilFunc(GL_EQUAL, stencilValues[i], 0xff);
        glUniform4fv(colorLoc, 1, colors[i]);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[4]);
   }

 // Restore stencil mask
    glStencilMask(stencilMask);

    SDL_GL_SwapWindow(window);
indice[4] = whole window.
Seems stencil buffer is updated after first draw call in loop.
So the second call shows unexpected result.
Last edited by Elvis Lee on Thu Jul 06, 2017 8:35 am, edited 1 time in total.

User avatar
Paeryn
Posts: 1686
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Seems glStencilMask(0x0) is not working.

Tue Jul 04, 2017 4:38 pm

Are you setting the stencil mask back to ~0 before you do a glClear()? If not then the clear won't reset the stencil mask. Buffer write masks affect glClear().

Code: Select all

glStencilMask(~0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
Edit:
The glClear() call should write to the stencil buffer with the active StencilMask so if you want to clear the whole buffer to the ClearStencil value you need to have the StencilMask set to all ones before calling glClear().

I'm looking at the source code of the OpenGLES2.0 book (which the code snippet you gave looks like it came from), the code there never sets the StencilMask to ~0 before it starts drawing and it constantly loops drawing the same thing so in there the first frame gets drawn correctly but all subsequent frames will be drawn wrong because the StencilMask will always be zero.

I can't remember how the stencil buffer is supposed to operate over a swapbuffers. Looking at what is happening with the main gles2 library (not the experimental one, I don't use that) the stencil buffer looks to be being zeroed on an eglSwapBuffers() even if you don't explicitly clear it regardless of the StencilMask (which makes sense as you don't usually want to preserve the stencil buffer between frames, a potentially expensive thing for a mobile GPU to do). In the example code a zero in the stencil buffer will always draw the big quad in green from the second frame onward which is what I am seeing if you don't add in the glStencilMask(~0);
She who travels light — forgot something.

Elvis Lee
Posts: 2
Joined: Tue Jul 04, 2017 2:42 am

Re: Seems glStencilMask(0x0) is not working.

Thu Jul 06, 2017 8:39 am

Added some more codes.
Of course, the stencil mask restored to ~0x00 after the drawing. And I checked the value was default.
I feel this is a bug of Pi(brcm) or Mesa.
I have no idea how I can resolve this issue.

User avatar
Paeryn
Posts: 1686
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Seems glStencilMask(0x0) is not working.

Thu Jul 06, 2017 12:12 pm

Try setting glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP) at the same time as you do the glStencilMask(0) so that if the stencil is being incorrectly written to it should at least not be altering the values.

The OpenGLES driver is working fine so if this is a bug with the OpenGL driver you could post an issue on Eric Anholt's github (he being the person working on the driver) https://github.com/anholt/mesa I don't know how far behind the provided driver is from the current github code but I can't see an issue either open of closed that suggests glStencilMask isn't working correctly.
She who travels light — forgot something.

Return to “OpenGLES”

Who is online

Users browsing this forum: No registered users and 1 guest