Reusing the depth buffer across render targets

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Reusing the depth buffer across render targets

Post by ent1ty »

Hi, how can you force Irrlicht/OpenGL to reuse the same zbuffer for all your rendering, be it the screen or texture(s)? It seems like certain gpu drivers reuse these by default whereas others have different zbuffers at least for the frame buffer and MRTs.
Thanks
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Reusing the depth buffer across render targets

Post by mongoose7 »

I thought Irrlicht always used the same depth buffer. Isn't this what Nadro is working on at the moment (setting the depth buffer). I'm guessing that you are clearing the depth buffer yourself when you set a render target.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Reusing the depth buffer across render targets

Post by hendu »

If you check the code, you see the main window's Z buffer is different from the MRT Z buffers. It cannot be easily shared either. The Z buffer of a MRT is shared with other MRTs that have matching resolution (and in my fork, matching stencil).
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Re: Reusing the depth buffer across render targets

Post by ent1ty »

Unfortunate - talking about deferred rendering here, I suppose that I will have to render the transparent objects into the MRTs as well then... make sure *not* to render into the depth texture this time if I want to read it in the shader for soft particles. Does this make sense? The plus side is that now I'd get lighting for the transparent materials as well... neat :)
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: Reusing the depth buffer across render targets

Post by Nadro »

@ent1ty
Plase wait a few more days, I'll send big commit related for these things.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Re: Reusing the depth buffer across render targets

Post by ent1ty »

That's awesome, thanks - I'll wait :)
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: Reusing the depth buffer across render targets

Post by Nadro »

Patch is already merged with trunk. If you will notice any bug please report it.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Re: Reusing the depth buffer across render targets

Post by ent1ty »

Can I do something like this:

Code: Select all

 
video::IRenderTarget *RT = Device->getVideoDriver()->addRenderTarget();
RT->setTexture(0, MRTRenderTarget->getDepthStencil());
 
I.e. reuse the depth buffer between the MRTs and the screen? I'm only getting black screen, not sure if I'm supposed to be messing with the depth buffer this much :) I added some getters inside IRenderTarget.h, as you could notice:

Code: Select all

 
                core::array<ITexture*> getTextures() const
                {
                    return Texture;
                }
 
                ITexture* getDepthStencil() const
                {
                    return DepthStencil;
                }
 
Maybe exposing the depth buffer is a bad idea (is it?), but at least the textures should be fine I think.


Another thing - before, you could reset the render target to the screen by calling setRenderTarget(video::ERT_FRAME_BUFFER) but with the current API this isn't possible anymore - is setting the render target to 0 the same thing? The docs seems to suggest that it, in fact, sets the target to the previously used target - so if you're rendering into 2 sets of MRTs, it wouldn't be possible for you to render onto the screen anymore :)
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Reusing the depth buffer across render targets

Post by hendu »

No, you cannot reuse the window's buffer with any RTT. It's special.
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: Reusing the depth buffer across render targets

Post by Nadro »

setRenderTarget with null pointer will allow you to render on screen again, however D3D9 may have a bug related to this case. I'll check that.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Re: Reusing the depth buffer across render targets

Post by ent1ty »

Thanks, but I can't get the MRTs working. Here's how I set the render target:

Code: Select all

 
    core::array<u32> texIndices;
    texIndices.push_back(0);
    texIndices.push_back(1);
    Device->getVideoDriver()->setRenderTarget(RenderTarget, texIndices,
                                              false, true, false,
                                              video::SColor(0, 0, 0, 0));
 
But I only get output in the first MRT. Any clue what could be wrong? I tried to use the layout syntax (layout(location = 0) out vec3 OutColor; ...) in the fragment shader but this is only supported since OpenGL 3.0. Here's the whole shader:

Code: Select all

 
varying vec3 Normal;
 
uniform float Lighting;
uniform sampler2D Tex0;
 
void main()
{
    vec3 vNormal= normalize(Normal);
    vNormal*= 0.5;
    vNormal+= 0.5;
 
 
    gl_FragData[0] = texture2D(Tex0, gl_TexCoord[0].xy);
    // set normal to 0 for objects that should not receive lighting
    gl_FragData[1] = vec4(vNormal * Lighting, 0.0);
}
 
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: Reusing the depth buffer across render targets

Post by Nadro »

Can you post a full code of IRenderTarget creation process?
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Re: Reusing the depth buffer across render targets

Post by ent1ty »

Here goes:

Code: Select all

 
irr::core::array<irr::video::ITexture*> MRTs;
// [...]
 
video::IVideoDriver *video = Device->getVideoDriver();
 
createMRT("deferred-mrt-color", video::ECF_A8R8G8B8);
createMRT("deferred-mrt-normal", video::ECF_A8R8G8B8);
 
DepthBuffer = video->addRenderTargetTexture(video->getCurrentRenderTargetSize(),
                                            "deferred-depth",
                                            depth);
RenderTarget = video->addRenderTarget();
RenderTarget->setTexture(MRTs, DepthBuffer);
 
// where createMRT:
void video::CRenderer::createMRT(const c8* name, video::ECOLOR_FORMAT format)
{
    if(MRTs.size() <= 4)
    {
        dimension= Device->getVideoDriver()->getCurrentRenderTargetSize();
        MRTs.push_back(Device->getVideoDriver()->addRenderTargetTexture(dimension, name, format));
    }
}
 
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: Reusing the depth buffer across render targets

Post by Nadro »

There is a bug in Irrlicht. I should fix it tomorrow.

UPDATE:
Sorry, I'll not have an access to my dev PC for few upcoming days, so I will not be able to fix it until next week. I'll send fix immediately after back to home.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: Reusing the depth buffer across render targets

Post by Nadro »

Please check the latest revision.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
Post Reply