copy app generated stencil data to stencil buffer

Hello All,

I am new to this forum, hoping someone can help me with my problem.

I am looking for fastest method to copy application generated stencil data to stencil buffer? My OpenGL app uses ATI Radeon 5970 video card and when I used glDrawPixels to copy the data as below the performance was terribly slow about 3 FPS.

glDrawPixels(768, 768, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, (const GLvoid* )data);

What is interesting is the same app runs 25 FPS on ATI Radeon 3800 video card.

After browsing the forum, I even tried using the pixel buffer objects in conjunction with glDrawPixels, the performance seems to be fine but the stencil buffer is corrupted.
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, m_pbo);
glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, size, (const GLvoid *) plane_data);
glDrawPixels(768, 768, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, 0);

Please let me know if anyone needs a sample code.

thanks in advance

Well, if it’s not multisampled, one way would be to upload it to a ALPHA texture (say where value is 0 if you don’t want to set stencil and non-zero if you do). Then enable stencil writes and render that texture with a full-screen quad batch. Use ALPHA_TEST (or frag shader discard) to avoid setting stencil for texels that are 0.

If it is multisampled, you can run the frag shader at sample rate and presumably do something similar.

Another approach: if you have newer hardware that supports ARB_shader_stencil_export, you can write the stencil value directly in the frag shader.

Dark Photon,
Thanks for the response. My non-zero stencil value is not same for all of the pixels that I want to set in stencil buffer. It is different values for different pixels. If I use the alpha texture and alpha test, the stencil buffer will be set for those passed the alpha test with a stencil value fixed and same for all of these pixels, as determined by stencil ref and mask values. Afraid your suggestion does not fit my problem, suggest otherwise.


Hello All,
Is there an alternate to write to stencil buffer without using shaders? see my earlier post on what I currently have implemented.

thanks in advance

Can you check how many bits of stencil you’re getting on the 5970? Stencil buffers normally come as 4-bit or 8-bit, and it sounds like the 5970 may be giving you a suboptimal format that requires conversion by the driver in software before it can be copied to hardware.

Its 8-bit stencil buffer.

Dark Photon,

May be too much to ask, could you provide me shader sample code using the ARB_shader_stencil_export extension to write to stencil buffer.
thanks in advance

I don’t have any. Don’t have a GPU that supports this extension. However, the extension spec tells you what to do:

What part is confusing?