[Solved] Clear g-buffer (MRT)

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!
mant
Posts: 125
Joined: Sun Jan 27, 2013 3:38 pm

[Solved] Clear g-buffer (MRT)

Post by mant »

Hi, I'm implementing derferred rendering and have encounted this error. (My model is a blue cube).

Image

The quad scene node doesn't render the final image, and the buffers don't clear the previous render, plus the depth buffer is not rendered.
Here are some code of mine, I only implement solild and ambient shader first.

Code: Select all

    
  createMRT("mrt_color_buffer", irr::video::ECF_A8R8G8B8);
  createMRT("mrt_normal_buffer", irr::video::ECF_A8R8G8B8);
  createMRT("mrt_depth_buffer", irr::video::ECF_G16R16F);
  
  m_lightRenderer = new LightRenderer(m_renderer, &m_MRTs);
  loadMaterials();
  m_lightRenderer->setAmbientMaterial(getMaterial(MaterialType::AmbientLight));
 
  m_renderer->getRenderDevice()->getSceneManager()->setLightManager(m_lightRenderer);
  m_renderer->getRenderDevice()->getSceneManager()->setAmbientLight(irr::video::SColorf(0.5f,0.5f,0.5f,0.5f))
 

Code: Select all

 
LightRenderer::LightRenderer(Renderer* renderer, MRTs* mrt)
  :  m_renderer(renderer), m_quadLight(renderer), m_MRTs(mrt)
{
  auto driver = m_renderer->getRenderDevice()->getVideoDriver();
  auto dimension = driver->getCurrentRenderTargetSize();
  m_solidFrameBuffer = driver->addRenderTargetTexture(dimension, "solid_buffer");
  auto smgr = m_renderer->getRenderDevice()->getSceneManager();
  m_ambientCB = new AmbientLightShaderCallback(smgr);
 
    for (irr::u32 i= 0; i < m_MRTs->size(); i++) {
    m_quadLight.setMaterialTexture(i, (*m_MRTs)[i].RenderTexture);
  }
}
 
void LightRenderer::OnPreRender(array<ISceneNode*>& lightList) {
  m_renderer->getRenderDevice()->getVideoDriver()->setRenderTarget(*m_MRTs, false, true);
}
 
void LightRenderer::OnPostRender() {
  if (m_MRTs->size()) {
    auto  gbuffRect= irr::core::recti(irr::core::vector2di(0, 0), (*m_MRTs)[0].RenderTexture->getSize());
    for(irr::u32 i = 0; i < m_MRTs->size(); i++) {
      auto gbuffRectSmall = irr::core::recti(irr::core::vector2di((*m_MRTs)[i].RenderTexture->getSize().Width/4.0*i, 0),
        irr::core::vector2di((*m_MRTs)[i].RenderTexture->getSize().Width/4.0*(i+1), (*m_MRTs)[i].RenderTexture->getSize().Height/4.0));
      m_renderer->getRenderDevice()->getVideoDriver()->draw2DImage((*m_MRTs)[i].RenderTexture, gbuffRectSmall, gbuffRect);
    }
  }
}
 
void LightRenderer::OnRenderPassPreRender(E_SCENE_NODE_RENDER_PASS renderPass) {
 
}
 
void LightRenderer::OnRenderPassPostRender(E_SCENE_NODE_RENDER_PASS renderPass) {
    if (renderPass == irr::scene::ESNRP_SOLID) {
    auto device = m_renderer->getRenderDevice();
        device->getVideoDriver()->setRenderTarget(m_solidFrameBuffer, true, false);
    deferredRender();
 
    device->getVideoDriver()->setRenderTarget(0, false, false);
    device->getVideoDriver()->draw2DImage(m_solidFrameBuffer, irr::core::position2d<s32> (0,0));
  }
}
 
void LightRenderer::OnNodePreRender(ISceneNode *node) {
 
}
 
void LightRenderer::OnNodePostRender(ISceneNode *node) {
 
}
 
void LightRenderer::deferredRender() {
  // render ambient light
  m_quadLight.setMaterialType((irr::video::E_MATERIAL_TYPE) m_ambientLightMaterial);
  for (irr::u32 i = 0; i < m_MRTs->size(); i++) {
    m_quadLight.setMaterialTexture(i, (*m_MRTs)[i].RenderTexture);
  }
  m_quadLight.render();
}
 

Code: Select all

 
QuadSceneNode::QuadSceneNode(Renderer* renderer, irr::scene::ISceneNode* parent)
  :  irr::scene::ISceneNode(parent, renderer->getRenderDevice()->getSceneManager())
{
  m_buffer = new irr::scene::SMeshBuffer();
 
  m_buffer->Vertices.push_back(irr::video::S3DVertex(
    irr::core::vector3df(-1.0, 1.0, 0.0),
    irr::core::vector3df(0.0, 0.0, 1.0), irr::video::SColor(255,255,255,255),
    irr::core::vector2df(0.0, 0.0)));
 
  m_buffer->Vertices.push_back(irr::video::S3DVertex(
    irr::core::vector3df(1.0, 1.0, 0.0),
    irr::core::vector3df(0.0, 0.0, 1.0), irr::video::SColor(255,255,255,255),
    irr::core::vector2df(1.0, 0.0)));
 
  m_buffer->Vertices.push_back(irr::video::S3DVertex(
    irr::core::vector3df(1.0, -1.0, 0.0),
    irr::core::vector3df(0.0, 0.0, 1.0), irr::video::SColor(255,255,255,255),
    irr::core::vector2df(1.0, 1.0)));
 
  m_buffer->Vertices.push_back(irr::video::S3DVertex(
    irr::core::vector3df(-1.0, -1.0, 0.0),
    irr::core::vector3df(0.0, 0.0, 1.0), irr::video::SColor(255,255,255,255),
    irr::core::vector2df(0.0, 1.0)));
 
  m_buffer->Indices.push_back(0);
  m_buffer->Indices.push_back(1);
  m_buffer->Indices.push_back(2);
  m_buffer->Indices.push_back(3);
  m_buffer->Indices.push_back(0);
  m_buffer->Indices.push_back(2);
  m_buffer->recalculateBoundingBox();
 
  updateAbsolutePosition();
  m_material.ZBuffer = false;
  m_material.ZWriteEnable = false;
  setMaterialFlag(irr::video::EMF_ZWRITE_ENABLE, false);
  setMaterialFlag(irr::video::EMF_ZBUFFER, false);
  setVisible(false);
}
 
void QuadSceneNode::render() {
  auto driver = SceneManager->getVideoDriver();
  driver->setTransform(irr::video::ETS_WORLD, AbsoluteTransformation);
  driver->setMaterial(m_material);
  driver->drawMeshBuffer(m_buffer); 
}
 
QuadSceneNode::~QuadSceneNode() {
  delete m_buffer;
}
 
Last edited by mant on Wed Aug 02, 2017 8:21 pm, edited 2 times in total.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Clear g-buffer (MRT)

Post by CuteAlien »

Sorry, figuring out what's going on with just a few code-snippets and no working example is too hard for me (that kind of stuff is hard even if you have already something reproducible...). But Irrlicht svn trunk has more parameters (like setting the value to be used on clear) with setRenderTargetEx, maybe that helps in some way.

If you suspect setRenderTarget clear flags are not working please try to give me a minimal example of them going wrong.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
mant
Posts: 125
Joined: Sun Jan 27, 2013 3:38 pm

Re: Clear g-buffer (MRT)

Post by mant »

Update:
There is nothing on the screen because I forgot the quad shader.
The depth buffer (render target) is missing too, I need to assign the Depth value in the shader.
Actually I'm studying the code here to apply to my project: https://github.com/osense/irrRenderer

Image
CuteAlien wrote:Sorry, figuring out what's going on with just a few code-snippets and no working example is too hard for me (that kind of stuff is hard even if you have already something reproducible...). But Irrlicht svn trunk has more parameters (like setting the value to be used on clear) with setRenderTargetEx, maybe that helps in some way.

If you suspect setRenderTarget clear flags are not working please try to give me a minimal example of them going wrong.
My current rendering code is dependent on the rest of project so I'm very lazy to re-make it into something like one source file.
I assume there are many ways to implement deferred rendering so even experienced person thinks it's hard to figure out what is going on?
Anyways I will compare the code more to spot the missing steps.
Last edited by mant on Sun Jul 30, 2017 7:09 pm, edited 1 time in total.
mant
Posts: 125
Joined: Sun Jan 27, 2013 3:38 pm

Re: Clear g-buffer (MRT)

Post by mant »

About the not clearing problem, I have an interesting finding.
That renderer (https://github.com/osense/irrRenderer) also has the same clearing issue if it loads another mesh.
So it means that there's something in the original scene that fixes the clearing.
I will brute-force these material settings to find out.

Code: Select all

                <bool name="Wireframe" value="false" />
                <bool name="GouraudShading" value="true" />
                <bool name="Lighting" value="true" />
                <bool name="ZWriteEnable" value="true" />
                <int name="ZBuffer" value="1" />
                <bool name="BackfaceCulling" value="true" />
                <bool name="FrontfaceCulling" value="false" />
                <bool name="FogEnable" value="false" />
                <bool name="NormalizeNormals" value="false" />
                <bool name="BilinearFilter1" value="true" />
                <bool name="BilinearFilter2" value="true" />
                <bool name="BilinearFilter3" value="true" />
                <bool name="BilinearFilter4" value="true" />
                <bool name="TrilinearFilter1" value="true" />
                <bool name="TrilinearFilter2" value="true" />
                <bool name="TrilinearFilter3" value="false" />
                <bool name="TrilinearFilter4" value="false" />
                <bool name="AnisotropicFilter1" value="true" />
                <bool name="AnisotropicFilter2" value="true" />
                <bool name="AnisotropicFilter3" value="false" />
                <bool name="AnisotropicFilter4" value="false" />
                <enum name="TextureWrap1" value="texture_clamp_repeat" />
                <enum name="TextureWrap2" value="texture_clamp_repeat" />
                <enum name="TextureWrap3" value="texture_clamp_repeat" />
                <enum name="TextureWrap4" value="texture_clamp_repeat" />
Last edited by mant on Sun Jul 30, 2017 7:10 pm, edited 1 time in total.
mant
Posts: 125
Joined: Sun Jan 27, 2013 3:38 pm

Re: Clear g-buffer (MRT)

Post by mant »

Unfortunately it's not about the material settings.

I added a working code here (Linux): https://github.com/zer0-x/irrRenderer
Please build the irrlicht SDK, put the folder in and run build.sh (SDK folder should be named irrlicht)
Then run bin/RenderTest
From line 71-77 is my modified code with description to prove that there's something wrong with mesh loading that introduces the clearing problem:
https://github.com/zer0-x/irrRenderer/b ... rk.cpp#L71
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Clear g-buffer (MRT)

Post by CuteAlien »

Thanks, I'll try to run it when I got some time this week.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Clear g-buffer (MRT)

Post by CuteAlien »

Can you also add your .irr files? As it looks like they are necessary to reproduce the problem (as it you say it works with some and not others).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
mant
Posts: 125
Joined: Sun Jan 27, 2013 3:38 pm

Re: Clear g-buffer (MRT)

Post by mant »

I already added the .irr and other needed files
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Clear g-buffer (MRT)

Post by CuteAlien »

OK, found it now (thought github is full of unicorns atm...).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Clear g-buffer (MRT)

Post by CuteAlien »

I'm getting a crash. Not debugging it (sorry, too much code for me to go through). But maybe my output helps you in some way:

Irrlicht Engine version 1.8.4
Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.43-2+deb8u2 (2017-06-26) x86_64
Creating X window...
Visual chosen: : 39
Using renderer: OpenGL 4.5.0
GeForce GTX 550 Ti/PCIe/SSE2: NVIDIA Corporation
OpenGL driver version is 1.2 or better.
Dedicated video memory (kB): 1048576
Total video memory (kB): 1048576
Available video memory (kB): 959552
GLSL version: 4.5
GCC Version: 4.9.2
Irrlicht Version: 1.9.0
Vsync: off
Depth: 0
Resolution: 1280 x 720
compile shader: solid
compile shader: transparent_alpha_ref
compile shader: transparent_alpha
compile shader: transparent_alpha_soft
compile shader: normal
compile shader: normalAnimated
compile shader: parallax
GLSL shader failed to compile
0(8) : warning C7555: 'varying' is deprecated, use 'in/out' instead
0(9) : warning C7555: 'varying' is deprecated, use 'in/out' instead
0(10) : warning C7555: 'varying' is deprecated, use 'in/out' instead
0(11) : warning C7555: 'varying' is deprecated, use 'in/out' instead
0(13) : warning C7555: 'varying' is deprecated, use 'in/out' instead
0(17) : error C7616: global variable gl_Vertex is removed after version 140
0(22) : error C7616: global variable gl_NormalMatrix is removed after version 140
0(22) : error C7616: global variable gl_Normal is removed after version 140
0(23) : error C7616: global variable gl_MultiTexCoord1 is removed after version 140
0(24) : error C7616: global variable gl_MultiTexCoord2 is removed after version 140
0(31) : error C7616: global variable gl_TexCoord is removed after version 140
0(31) : error C7616: global variable gl_MultiTexCoord0 is removed after version 140

compile shader: detail
compile shader: light_point
compile shader: light_spot
compile shader: light_directional
compile shader: light_ambient

Program received signal SIGSEGV, Segmentation fault.
0x000000000071506c in irr::video::CMaterialSwapper::swapMaterialsOnNode (this=this@entry=0x1bf9860,
node=0xcfc6f8) at source/CMaterialSwapper.cpp:84
84 if(node->getMaterial(i).MaterialType == Entries[ii]->SwapFrom) node->getMaterial(i).MaterialType= Entries[ii]->SwapTo;
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
mant
Posts: 125
Joined: Sun Jan 27, 2013 3:38 pm

Re: Clear g-buffer (MRT)

Post by mant »

I can provide a fix for you. It was the parallax shader, maybe your GPU capability isn't compatible.
Please pull and run build.sh.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Clear g-buffer (MRT)

Post by CuteAlien »

No more crash, but still compile errors in shaders (in all but first I think):

compile shader: solid
compile shader:
GLSL shader program failed to link
Vertex info
-----------
(0) : error C5145: must write to gl_Position

...

Same error repeats a few more times. Getting a black screen as result (aside from gui-options).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
mant
Posts: 125
Joined: Sun Jan 27, 2013 3:38 pm

Re: Clear g-buffer (MRT)

Post by mant »

did you uncomment scene loading commands? Please check the CTestFramework.cpp
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Clear g-buffer (MRT)

Post by CuteAlien »

Right, when I uncomment those lines I see models. Main difference between uncommenting the first and third line seems to be that the first has more to draw. But as soon as you go outside it also looks wrong like it's not clearing the back-buffer.

Sorry, I have no experience with deferred rendering. I can only help if you can show me a concrete bug with setRenderTarget not clearing stuff or something like that. But I need a test-case for that.

Right now I can only guess that at one call of setRenderTarget the clear flags are wrong - but without digging deeper into that code (which I won't find time for) I don't know what's going on. Maybe the guy who wrote that code originally can help.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
mant
Posts: 125
Joined: Sun Jan 27, 2013 3:38 pm

Re: Clear g-buffer (MRT)

Post by mant »

Oh my ...
I had no ideas when you mentioned the flags.
Setting them to true, true solved the problem.
What a time consuming way of resolving my issues. My bad.
Thank you Cute Alien.
Image
Post Reply