PDA

View Full Version : Depth buffer doesnt work when using Multisampled Antialiasing



McStones
08-14-2017, 07:58 AM
I already posted this on gamedev.stackexchange but its my third post about openGL stuff without any attention so I would like to try here.

I'm trying to implement Multisampled Antialiasing with 4 samples. This also works for the color attachment when I resolve it. The depth buffer doesnt seem to work with multisampled textures, but exactly the same scenario works if I use default 2D textures without multisampling. Here is how it looks like when I draw with enabled Depth Maskand Depth Test. (Antialiasing works, but depth testing doesnt...)

24462447

I created a few multisampled textures for Color, Position, TexCoord, Normal and Depth for now. This is an abstract example of how I created these textures:


GLXTexture2DMultisample finalImageColor = new GLXTexture2DMultisample(GBufferWidth, GBufferHeight, 4);
finalImageColor.CreateMutable(PixelInternalFormat. Rgba8);
finalImageColor.GetError(false);
// A few more textures will follow...

None of them throws an error. Now I will pass them to the Framebuffer, but for simplicity I only show my depth attachment line because everything else is working well.


GL.FramebufferTexture2D(FramebufferTarget.Framebuf fer, FramebufferAttachment.DepthStencilAttachment, Depth24Stencil8, depthTexture.ID, 0);

Note that I'm not using a renderbuffer. Is this a problem? I'm doint this because I want to use the depth texture later. (As I said, it works good without multisampled textures)

Here is how I resolve the color texture:


#version 330
uniform sampler2DMS finalImage;
uniform vec2 samplerResolution;
uniform int samples;
in vec2 texCoords;
out vec4 fragColor;
void main(){
vec4 color = vec4(0.0);
ivec2 coords = ivec2(texCoords.x * samplerResolution.x, samplerResolution.y - texCoords.y * samplerResolution.y);
for(int i = 0; i < 4; i++)
color += texelFetch(finalImage, coords, i);
fragColor = color / samples;
}

I searched for that issue at google but I didnt even find an image which looks like a depth test issue caused by multisampled textures. Is there anything to keep in mind when using Multisampled Textures as a render target?

Dark Photon
08-15-2017, 06:37 AM
Depth buffer doesnt work when using Multisampled Antialiasing
...
I'm trying to implement Multisampled Antialiasing with 4 samples. This also works for the color attachment when I resolve it. The depth buffer doesnt seem to work with multisampled textures,

"Doesnt work" doesn't tell us much. Please tell us what evidence you have that the "depth buffer doesnt seem to work". Moreover, how do you know it's just the depth buffer?

Your pictures look fine to me. Is it possible you are misunderstanding what MSAA provides and how it works?


Here is how I resolve the color texture:
Why are you doing your own manual resolve? One of the features of hardware MSAA is that it supports doing the resolve for you, and likely more efficiently than you're going to be able to do.

McStones
08-16-2017, 04:20 AM
"Doesnt work" doesn't tell us much. Please tell us what evidence you have that the "depth buffer doesnt seem to work".

I`ll try to explain it a little bit better.
My problem is, that it seems like depth values are not written to the depth buffer, which is also a Texture2DMultisample.
The first attachment, where you can see several quads rendered front to back, let me assume, that depth values are not written correctly. These quads have all the same geometrical size.


"Moreover, how do you know it's just the depth buffer?"

I think it is, because when I run the same scene without MSAA, the depth seems to be written correctly. The first attachment would show a big yellow quad in the foreground covering the other quads.


"Why are you doing your own manual resolve?"
The manual resolve is done, because I will implement deferred lighting later. In another shader I will reject similar samples from my multisampled texture and calculate light multiple times where needed (e.g. on edges).

Dark Photon
08-16-2017, 06:59 AM
My problem is, that it seems like depth values are not written to the depth buffer, which is also a Texture2DMultisample.

Ok, that's a start. How are you determining this? For instance, by using texelFetch on the MS depth buffer in a shader run in a subsequent pass on the same multisample render target, and rendering a representation of the Nth depth sample in each pixel to the color buffer? That should work.

Also, what GPU and GPU driver version are you using?


The manual resolve is done, because I will implement deferred lighting later.

Ok, makes sense.

McStones
08-16-2017, 09:30 AM
For instance, by using texelFetch on the MS depth buffer in a shader run in a subsequent pass on the same multisample render target, and rendering a representation of the Nth depth sample in each pixel to the color buffer? That should work.

Also, what GPU and GPU driver version are you using?

I tried to read from the Multisampled depth buffer... but these results are not supposed to be depth values, at least not for that scene.

It was basically the same procedure like before, but I bound the depth attachment instead of the color attachment. (Also used sampler2DMS in my shader)
Here is an image of 3rd depth sample. (I used the 3rd because 1st was empty(black) and 2nd was showing an inverted part of my outlook email window)
2454
(Some areas are white, but the whole recangle is my viewport)

I have an Intel HD Graphics processor. Driver version 9.17.10.2884. This is an office PC, do you think it could be a driver bug?


Thanks in advance!

McStones
08-17-2017, 02:55 AM
I finally got it to work...

It seemed like I`ve made the mistake... As you can see, my depth attachment was not valid, so the shader has read somewhere in memory it wasnt supposed to. I did some research and found a tiny little casting function, which blocked my depth texture from being loaded correctly.


Create(colorAttachments, depthAttachment as GLXTexture2D, stencilAttachment as GLXTexture2D);

I dont know If you are familiar with c#, but the "as" keyword casts an object to the desired type. If it fails, it returns NULL. This was ignored by the framebuffer... :doh:


Thank you, Dark Photon, for that hint to texelFetch from the depth texture.