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:


     *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
");                        break;
            case 0x8CDB:                                        tdkPrintf("GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER
");          break;
            case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:          tdkPrintf("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
");           break;
            case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:  tdkPrintf("GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
");   break;        
            case GL_FRAMEBUFFER_UNSUPPORTED:                    tdkPrintf("GL_FRAMEBUFFER_UNSUPPORTED
");                     break;  
            default:                                            tdkPrintf("Unknown issue (%X).
",status);                     break;
      }

      glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
      nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindFramebuffer(GL_FRAMEBUFFER, 0);"));
      draw_geometry();


Frag shader:



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);
}

Anyone? I need to solve this urgently…



    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!

[QUOTE=tonyo_au;1248272]



    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![/QUOTE]

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:


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 :frowning:

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.


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.

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.

[QUOTE=tonyo_au;1248324]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.[/QUOTE]

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. ?

What stencil/depth setting did you created you device?

I use


  pfd.cDepthBits = 24;
  pfd.cStencilBits  = 8;


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


    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.

[QUOTE=tonyo_au;1248349]What stencil/depth setting did you created you device?

I use


  pfd.cDepthBits = 24;
  pfd.cStencilBits  = 8;


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

I am using the same bits.

I read some of the posts regarding depth_stencil textures and found that it is just same as depth textures without shadow mapping (normal sampler2D) and no need to add any special code regarding stencil test though i tried with that too. I am using AMD catalyst on windows, i guess it might be buggy.

Here is my render buffer


  glGenFramebuffers(1, &c_ID);
  glBindFramebuffer(GL_FRAMEBUFFER, c_ID);

  glGenTextures(1, &c_RenderTexture);
  glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, c_RenderTexture);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, c_Samples, GL_RGBA, c_Width, p_Height,GL_FALSE);
  glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, c_RenderTexture, 0);
            
  if (c_HasDepthBuffer)
  {
      glGenTextures(1, &c_RenderDepthTexture);
      glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, c_RenderDepthTexture);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
      glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, c_Samples, GL_DEPTH24_STENCIL8, c_Width, c_Height, GL_FALSE);                
      glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, c_RenderDepthTexture, 0);
  }

Hope that helps.

Thanks all for your replies.
I tried my code on other driver and it works perfectly fine. AMD really need to fix lot of issues.

Just to confirm, i am getting same output for GL_DEPTH_STENCIL_ATTACHMENT and GL_DEPTH_ATTACHMENT i.e. same output for depth texture and depth-stencil textures, which is correct right?

The depth values will be the same but they have different uses

form the specs

If the base internal format is DEPTH_STENCIL and format is not DEPTH_-
STENCIL, then the values of the stencil index texture components are undefined.