Multisample textures

I assume your vertex shader has the texture coordinate in defined as a vec3?

yes.
Vertex shader:


in vec3 inputtexcoord;
in vec3 tk_position;
out vec3 ps_texCoord;

uniform mat4 tk_mvpMatrix;
void main(void)
{
        ps_texCoord = inputtexcoord;
gl_Position = tk_mvpMatrix * vec4(tk_position, 1.0); 
}


fragement


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 = color + texelFetch(tk_diffuseMap, ivec2(ps_texCoord), i);
	}
 
fragColor=vec4(color/4);
}

Sorry I should have seen this earlier

texelFetch texture coordinates are in pixels not 0-1; so you need to multiply uv by texture width and height

[QUOTE=tonyo_au;1246733]Sorry I should have seen this earlier

texelFetch texture coordinates are in pixels not 0-1; so you need to multiply uv by texture width and height[/QUOTE]

There was a mistake in above frag shader: for
texelFetch(tk_diffuseMap, ivec2(ps_texCoord), i);

Now,
vec2 iTmp = textureSize(tk_diffuseMap);
vec2 tmp =floor(iTmp * ps_texCoord.xy);
color = color + texelFetch(tk_diffuseMap, ivec2(tmp), i);

this also not working.

When I have errors like this that I can’t understand I modify the frag shader to display the uv values, eg



fragColor=vec4(ps_texCoord.xy,0,1);
fragColor=vec4(ps_texCoord.x,0,0,1); // to just see the u  values

Now you can see how the texture is being mapped onto the cube. It may give a clue as to what is wrong

[QUOTE=tonyo_au;1246740]When I have errors like this that I can’t understand I modify the frag shader to display the uv values, eg



fragColor=vec4(ps_texCoord.xy,0,1);
fragColor=vec4(ps_texCoord.x,0,0,1); // to just see the u  values

Now you can see how the texture is being mapped onto the cube. It may give a clue as to what is wrong[/QUOTE]

Many thanks for your help…
Got it working…

Great to here;)

When using multisampled textures, do i need to set samplers ( to number which i am using in glTexImage2DMultisample ) ? because i am assuming it needs to be set in MSAA. Please correct me if wrong. I have no idea how internally multisampling works…

When just reading from multisample textures in a shader (what it sounds like you want to do), you can forget anything you might know (or not know) about multisample rasterization – it doesn’t matter. Conceptually you just have a texture where instead of having 1 vector value per pixel you have N vector values per pixel, where N is the number of samples per pixel you allocated for that multisample texture.

To fetch the Nth sample from a particular pixel, just bind the multisample texture handle (what you get from glGenTextures) to a texture unit, and then in the shader, call texelFetch with the integer texcoord (“P”) addressing the specific pixel and the “sample” argument being the sample index within that pixel.

[QUOTE=Dark Photon;1247597]When just reading from multisample textures in a shader (what it sounds like you want to do), you can forget anything you might know (or not know) about multisample rasterization – it doesn’t matter. Conceptually you just have a texture where instead of having 1 vector value per pixel you have N vector values per pixel, where N is the number of samples per pixel you allocated for that multisample texture.

To fetch the Nth sample from a particular pixel, just bind the multisample texture handle (what you get from glGenTextures) to a texture unit, and then in the shader, call texelFetch with the integer texcoord (“P”) addressing the specific pixel and the “sample” argument being the sample index within that pixel.[/QUOTE]

Thanks, I mean to say do i need to set number of samples to 4 or 8 (instead of 1) while creating context in my config attributes?

Oh, I see. No. You can create and use multisample textures of any number of samples under-the-hood independent of how many samples per pixel you allocate for your system framebuffer.

One small point with that. If you want to blit your multisample texture to your system framebuffer that is also multisampled, they must have the same number of samples. If the system framebuffer is not multisampled, then you can use any sample count you want.

True. I hadn’t considered that use case. Typically you want to off-screen render in MSAA and downsample blit to the system FB.

This came up at one point because drivers often have a way to let users force multisampling on applications. And this usually manifests as forcing multisampling on the back buffer, thus breaking the blit from the internal FBO to the back buffer.

Ah! Good point!

Does GL_RGB32I support for 4x or 8x multisampled textures in call glTeximage2DMultisample? Are there any other formats which do not support 4x or 8x ?