Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Corruption in Stencil textures with glTexImage2DMultisample

  1. #1
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    207

    Corruption in Stencil textures with glTexImage2DMultisample

    I am using depth multisampled textures. I have 2 attachments to FBO: 1. Depth-stencil attachment of a texture 2. Color attachment of render buffer. I am rendering a cube on non default FBO with attachment mentioned above and applying this FBO as a texture to a each face of the cube on default FBO. But i am getting corruption with this code. while rendering on non default FBO i am clearing stencil buffer by glclear() call. here is my code:

    Code :
         *target11=GL_TEXTURE_2D_MULTISAMPLE;
          *samples=1;
     
     
          glGenTextures(1, &id1);
          nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glGenTextures(1, &id1);"));
     
          glBindTexture(*target11, id1);
          nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindTexture(*target11, id1);"));
     
          glGenFramebuffers(1, &Fboid);
          nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glGenFramebuffers(1, &Fboid);"));
     
          GLint framebuffer;
     
          glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING,&framebuffer);
          nResult |= GL_ERROR_CHECK((GL_NO_ERROR,"glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING,&framebuffer);"));
     
          glGenRenderbuffers(1, &depth);
          nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glGenRenderbuffers(1, &depth);"));
     
          glPixelStorei(GL_UNPACK_ALIGNMENT,1);
          nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glPixelStorei(GL_UNPACK_ALIGNMENT,1);"));
     
          glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, *samples,GL_DEPTH32F_STENCIL8, 32, 32,true);  
          nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4 ,GL_RGBA, 32, 32,true);"));         
     
          glBindFramebuffer(GL_FRAMEBUFFER, Fboid);
          nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindFramebuffer(GL_FRAMEBUFFER, Fboid);"));
     
          glBindRenderbuffer(GL_RENDERBUFFER, depth);
          nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindFramebuffer(GL_FRAMEBUFFER, Fboid);"));
     
          glRenderbufferStorageMultisample(GL_RENDERBUFFER,*samples, GL_RGBA, 32, 32);
          nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindRenderbuffer(GL_RENDERBUFFER, depth);"));
     
          glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, depth);
          nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);"));
     
          glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,GL_DEPTH_STENCIL_ATTACHMENT,*target11,id1,0);
          nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,*target11,id1,0);"));
     
          glEnable(GL_MULTISAMPLE);
          nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glEnable(GL_MULTISAMPLE);"));              
     
          draw_cube(0);
     
    glDisable(GL_MULTISAMPLE);    
          nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glDisable(GL_MULTISAMPLE);"));
     
          status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
          switch(status)
          {
                case GL_FRAMEBUFFER_COMPLETE:                       tdkPrintf("GL_FRAMEBUFFER_COMPLETE\n");                        break;
                case 0x8CDB:                                        tdkPrintf("GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER\n");          break;
                case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:          tdkPrintf("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT\n");           break;
                case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:  tdkPrintf("GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT\n");   break;        
                case GL_FRAMEBUFFER_UNSUPPORTED:                    tdkPrintf("GL_FRAMEBUFFER_UNSUPPORTED\n");                     break;  
                default:                                            tdkPrintf("Unknown issue (%X).\n",status);                     break;
          }
     
          glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
          nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindFramebuffer(GL_FRAMEBUFFER, 0);"));
          draw_geometry();

    Frag shader:
    Code :
     
    Frag shader:
     
     uniform sampler2DMS tk_diffuseMap;
    in vec3 ps_texCoord;
    out vec4 fragColor;
    uniform int samples;
     
    void main(void)
    {
     
        vec2 iTmp = textureSize(tk_diffuseMap);
        vec2 tmp =floor(iTmp * ps_texCoord.xy);
     
        vec4 color;
        for(int i = 0; i < samples; ++i)
        {
            color= color+texelFetch(tk_diffuseMap, ivec2(tmp), i);
     
        }
     
        fragColor = vec4(color/samples);
    }

  2. #2
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    207
    Anyone? I need to solve this urgently..

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    Code :
     
        vec4 color;     for(int i = 0; i < samples; ++i)     {         color= color+texelFetch(tk_diffuseMap, ivec2(tmp), i);       }
    You are using color without initialising it - not a good idea!

  4. #4
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    207
    Quote Originally Posted by tonyo_au View Post
    Code :
     
        vec4 color;     for(int i = 0; i < samples; ++i)     {         color= color+texelFetch(tk_diffuseMap, ivec2(tmp), i);       }
    You are using color without initialising it - not a good idea!
    I modified vec4 color=vec4(0); but still same issue.
    Do i need to do something different for writting to stencll textures. In my case, i am just rendering a cube on non default FBO and not doing any stencil operations while rendering.
    Also, while rendering on default FBO i tried by doing stencil operations:

    Code :
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glStencilFunc( GL_ALWAYS, 1, 1 );
    glClearStencil( 1 );
     glEnable( GL_STENCIL_TEST );  //Replace where rendered
     glStencilFunc( GL_ALWAYS, 1, 1 );
     glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE );
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.52f, 0.32f, 0.87f, 1.0f);
    glClearDepth(1.0f);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);

    I am getting same output with or without above stencil code. Its really frustrating

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    I would look at your creation code. Your bindings look mixed up and your error messages bare no relationship to the error you are checking for. If you clean this up you might be able to solve your problem.

  6. #6
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    207
    Quote Originally Posted by tonyo_au View Post
    I would look at your creation code. Your bindings look mixed up and your error messages bare no relationship to the error you are checking for. If you clean this up you might be able to solve your problem.
    Here is function, in which i am writing on FBO to which stencil texture is attached.
    Code :
    draw_cube(int fl)
    {
    	GLint x_mat,y_mat;
    	glEnable(GL_DEPTH_TEST);
    	nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glViewport(0, 0, width1, width1);"));
     
    	glViewport(0, 0, width1, width1);
    	nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glViewport(0, 0, width1, width1);"));
     
     
    	glClearStencil(0.0f);
    glEnable(GL_STENCIL_TEST);
     
     
    	if(fl==0)
                glClearColor(0.4f, 0.5f, 0.4f, 1.0f);
          else
                glClearColor(1.0f, 0.0f, 0.2f, 1.0f);
     
     
          glClearDepth(1.0f);
     
     
    	glClear(GL_DEPTH_BUFFER_BIT| GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
     
    	nResult |= tdkShaderGetUniformLocation(temp, "rot_x", &x_mat);
    	nResult |= tdkShaderGetUniformLocation(temp, "rot_y", &y_mat);
     
    	glUniformMatrix4fv(x_mat, 1, GL_FALSE, x_rot);
     
    	glUniformMatrix4fv(y_mat, 1, GL_FALSE, y_rot);
     
     
    	nResult |= tdkShaderGetUniformLocation(temp, "rot_x", &x_mat);
    	nResult |= tdkShaderGetUniformLocation(temp, "rot_y", &y_mat);
     
    	glUniformMatrix4fv(x_mat, 1, GL_FALSE, x_rot);
     
     
    	glUniformMatrix4fv(y_mat, 1, GL_FALSE, y_rot);
     
     
    	glUniform1i(glGetUniformLocation(temp.psId,"basetexture"), 0);
     
     
    	// enable attrib arrays
    	glEnableVertexAttribArray(glGetAttribLocation(temp.psId,"position"));
     
    	glVertexAttribPointer(glGetAttribLocation(temp.psId,"position"), 3, GL_FLOAT, GL_FALSE, 0,vertices);
     
     
    	glDrawArrays (GL_TRIANGLE_FAN, 0, 24);
     
     
    }

    Rest of the code you can find in my post 1 above.

  7. #7
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    I have not seen people use this GL_DEPTH32F_STENCIL8 before.

    What stencil/depth setting did you created you device and do they match this?
    It might be a problem in copying this to your screen.

  8. #8
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    207
    Quote Originally Posted by tonyo_au View Post
    I have not seen people use this GL_DEPTH32F_STENCIL8 before.

    What stencil/depth setting did you created you device and do they match this?
    It might be a problem in copying this to your screen.
    I tried with other depth stencil formats such as GL_DEPTH24_STENCIL8..
    Am i writting stencil textures correctly in draw_cube() ? or anything else needed in my rendering on FBO with 0 id. ?

  9. #9
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    What stencil/depth setting did you created you device?

    I use
    Code :
      pfd.cDepthBits = 24;
      pfd.cStencilBits  = 8;

    I use this code to set a stencil if a pixel is rendered an odd number of times

    Code :
        glEnable(GL_STENCIL_TEST);
        glClear(GL_STENCIL_BUFFER_BIT);
        glStencilFunc (GL_ALWAYS, 0x0, 0x1);
        glStencilOp (GL_INVERT, GL_INVERT, GL_INVERT);
        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
     
      VAO->Bind();
      glDrawArrays(GL_TRIANGLE_FAN,Vertex_Offset,Vertex_Count);
      VAO->Unbind();

    PS I will be out of town for a week so I might not be abe to continue trying to help.

  10. #10
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    207
    Quote Originally Posted by tonyo_au View Post
    What stencil/depth setting did you created you device?

    I use
    Code :
      pfd.cDepthBits = 24;
      pfd.cStencilBits  = 8;

    I use this code to set a stencil if a pixel is rendered an odd number of times
    I am using the same bits.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •