You don't need it to access multisample textures. You only need it if you plan to do per-sample rasterization.
And while that is core in GL/GLSL 4.0, you can also get it through the ARB_sample_shading extension.
You don't need it to access multisample textures. You only need it if you plan to do per-sample rasterization.
And while that is core in GL/GLSL 4.0, you can also get it through the ARB_sample_shading extension.
The 3rd parameter is to select the sample. If for example you wanted to save each sample in a separate texture buffer, you could loop in your cpu code calling a whole screen render and set a uniform to say which sample to fetch. If you just want to merge the samples into a single texture you can use glBlitFramebuffer or do it manually in a shader by fetching all the samples and averaging them.
Now, if i choose to do it manually in shader with 4 samples, my shader will be:
What do you mean by "averaging them" ?Code :uniform sampler2DMS tk_diffuseMap; in vec3 ps_texCoord; out vec4 fragColor; void main(void) { vec2 iTmp = textureSize(tk_diffuseMap); vec2 tmp = floor(iTmp * ps_texCoord.xy); vec4 color; for(int i = 0; i < 4; ++i) { color+ = texelFetch(depthTex, ivec2(tmp), i); } fragColor=color; }
and if i am using texture unit 0 i.e. only 1 texture, uniform will be set to 0.
Correct me if wrong.
Last edited by debonair; Yesterday at 12:17 AM.
You logic outputs the sum of the samples not the average. To average you need to divide the colour by the number of samples. This is of course the simplest thing you can do. You could convert the rgb to a better colour space to do the averaging.
ohh. Thanks for pointing out.
Now, i am rendering white cube with green background in non default FBO and attaching the created multisampled texture to this FBO.
When i render a cube in default FBO using above texture, it gives corruption in texture.
Here is my code:
Code :viewport_width=32; viewport_height=32; glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textureId); nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindTexture(*target, textureId);")); glUniform1i(glGetUniformLocation(shader_data.psId,"tk_diffuseMap"), 0); nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glUniform1i(glGetUniformLocation(shader_data.psId,\"basetexture\"), 0);")); glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4 ,GL_RGBA, viewport_width, viewport_height ,true); nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4 ,GL_RGBA, 32, 32,true);")); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, Fboid); nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindFramebuffer(GL_FRAMEBUFFER, Fboid);")); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D_MULTISAMPLE,textureId,0); nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D_MULTISAMPLE,textureId,0);")); glEnable(GL_MULTISAMPLE); nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glEnable(GL_MULTISAMPLE);")); draw_cube(viewport_width, viewport_height); glBindFramebuffer(GL_FRAMEBUFFER, 0); nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindFramebuffer(GL_FRAMEBUFFER, Fboid);")); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textureId); nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindTexture(*target, textureId);")); glDrawElements ( GL_TRIANGLES, 36,GL_UNSIGNED_INT, indices );
fragment shader
Code :uniform sampler2DMS tk_diffuseMap; in vec3 ps_texCoord; out vec4 fragColor; void main(void) { vec2 iTmp = textureSize(tk_diffuseMap); vec2 tmp = iTmp * ps_texCoord.xy; vec4 color; for(int i = 0; i < 4; ++i) { color = color + texelFetch(tk_diffuseMap, ivec2(tmp), i); } fragColor=vec4(color/4); }
Your code looks all back to front.What are you exactly trying to do?
I am rendering a cube on non default FBO and creating a multisampled texture attached to this FBO. Now, i am applying this multisampled texture on a cube rendered on default FBO. I am using "render to texture" concept here.
I am getting diagonally half of one of 6 faces green. and rest of the faces black...
I would look at the uv values you are using to render the cube. Also have you dumped the texture out as an image to be sure your texture is ok since I assume you have created it programmatically. Also why are using a mutlisample for a texture on the cube rather than a mipmap.
Code :GLfloat tex_coords[]={ 0.0, 0.0,0.0, 1.0, 0.0,1.0, 1.0, 1.0,1.0, 0.0, 1.0,0.0, 0.0, 0.0,0.0, 1.0, 0.0,1.0, 1.0, 1.0,1.0, 0.0, 1.0,0.0, 0.0, 0.0,0.0, 1.0, 0.0,1.0, 1.0, 1.0,1.0, 0.0, 1.0,0.0, 0.0, 0.0,0.0, 1.0, 0.0,1.0, 1.0, 1.0,1.0, 0.0, 1.0,0.0, 0.0, 0.0,0.0, 1.0, 0.0,1.0, 1.0, 1.0,1.0, 0.0, 1.0,0.0, 0.0, 0.0,0.0, 1.0, 0.0,1.0, 1.0, 1.0,1.0, 0.0, 1.0,0.0 };
I am neglecting a third component of tex cords in a shader.
Yeah, i confirmed my texture is correct when i rendered that to default FBO.