Multisample textures

I read some pages on google but still confused about working of multisample textures.
Can anyone explain this concept? If possible please explain it with source code.
Thanks.

Are you planning to use shaders or immediate mode?

i am going to use shaders.

Ok, that easier. You need to supply a uv for each texture in the vertex bindings (you can of course shared one uv for multiple textures. Then bind each texture to a sampler in the fragment shader. Fetch the texture pixels in the fragment shader and apply them as you see fit. I will post a little example - I am off to a boxing day do:D

example


vertex shader

layout(location = VERTEX_BINDINGS_POSITION) in  vec3  Position;
layout(location = VERTEX_BINDINGS_TEXTURE0) in  vec2  Texture0;
layout(location = VERTEX_BINDINGS_TEXTURE1) in  vec2  Texture1;

out vec2 texture0;
out vec2 texture1;

void main()
{
 texture0 = Texture0;
 texture1 = Texture1;
 
 gl_Position = CameraProjection * CameraModelView * vec4(Position,1);
}

fragment


in vec2 texture0;
in vec2 texture1;

uniform    sampler2D       Image0;
uniform    sampler2D       Image1;

layout(location = 0, index = 0) out vec4 fFragColour;



void main()
{

  vec4 c0 = texture2D(Image0,p_Texture0.st);
  vec4 c1 = texture2D(Image1,p_Texture1.st);

  vec4 colour = mix(c0,c1,c1.a); // some blend function

  fFragColour = colour;

}


[QUOTE=tonyo_au;1246362]example


vertex shader

layout(location = VERTEX_BINDINGS_POSITION) in  vec3  Position;
layout(location = VERTEX_BINDINGS_TEXTURE0) in  vec2  Texture0;
layout(location = VERTEX_BINDINGS_TEXTURE1) in  vec2  Texture1;

out vec2 texture0;
out vec2 texture1;

void main()
{
 texture0 = Texture0;
 texture1 = Texture1;
 
 gl_Position = CameraProjection * CameraModelView * vec4(Position,1);
}

fragment


in vec2 texture0;
in vec2 texture1;

uniform    sampler2D       Image0;
uniform    sampler2D       Image1;

layout(location = 0, index = 0) out vec4 fFragColour;



void main()
{

  vec4 c0 = texture2D(Image0,p_Texture0.st);
  vec4 c1 = texture2D(Image1,p_Texture1.st);

  vec4 colour = mix(c0,c1,c1.a); // some blend function

  fFragColour = colour;

}


[/QUOTE]

Thanks for the reply. But the code you have provided is for multi-texturing and not multisample texture :slight_smile:

i want to use multisampled textures.

What will be the fragment shader and how to pass multiple samplers to shader?

Also, which API can be used to load texel data ?

I want to use default FBO for this.

Typically you would pass these into a shader via a sampler2DMS and sample them via texelFetch(). Web search those and you’ll come up with some good sample code. There’s some in the archives of these forums as well (see the search box above).

And please don’t cross-post. I re-merged your threads.

wops - mis-read that here is a sample


uniform  sampler2DMS    u_2DMS; 
uniform  float          u_Translucency;

layout(location = 0, index = 0) out vec4 fFragColour;

void main()
{
  ivec2 st = ivec2(vData.texture0.st * u_Screen.xy);

  vec4 colour = texelFetch(u_2DMS,st,gl_SampleID);
    
  colour.w *= u_Translucency;

  fFragColour = colour;

}


[QUOTE=tonyo_au;1246451]wops - mis-read that here is a sample


uniform  sampler2DMS    u_2DMS; 
uniform  float          u_Translucency;

layout(location = 0, index = 0) out vec4 fFragColour;

void main()
{
  ivec2 st = ivec2(vData.texture0.st * u_Screen.xy);

  vec4 colour = texelFetch(u_2DMS,st,gl_SampleID);
    
  colour.w *= u_Translucency;

  fFragColour = colour;

}


[/QUOTE]

Thanks for the reply…
as gl_samplerID is supported on after 4.0 versions, what would replace it?

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.

[QUOTE=Dark Photon;1246607]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.[/QUOTE]

so, i should pass 3rd parameter as 0 ?
and if i want “per-sample rasterization” then ?

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:


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

What do you mean by “averaging them” ?
and if i am using texture unit 0 i.e. only 1 texture, uniform will be set to 0.

Correct me if wrong.

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:


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


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.


		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.