   Help with Stencil Buffer masking Light Volume
  Hi allI'm trying to implement Stencil masking for light's volumes in a deferred pipeline, but i have some problems, i understand the idea, but i dont know at 100% how to tell DirectX lib what to do.Look, i have a bounding box like a volume for an omni light, first, i render front faces with a prior parameters on stencil and z-buffer. Then, i render back faces with another parameters to create a mask on stencil buffer.The result is all screen black, but i doubt about my actions with parameters :(The code://---------------------------------------------------------------------------------------------------------------/// \brief Renderiza las linias que contiene el Helper de lineas//---------------------------------------------------------------------------------------------------------------void CCore::RenderLightVolume(){unsigned int cPasses;D3DXMATRIX mTransform = m_pCameraActive->GetViewProjMatrix();UpdateLightVolume(D3DXVECTOR3(-10.0f,0.0f,0.0f),5.0f);m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);m_pD3DDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);m_pD3DDevice->SetRenderState(D3DRS_ZWRITEENABLE, false);m_pD3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);m_pD3DDevice->SetRenderState(D3DRS_STENCILENABLE, true);m_pD3DDevice->SetRenderState(D3DRS_STENCILFUNC,D3DCMP_ALWAYS);m_pD3DDevice->SetRenderState(D3DRS_STENCILPASS,D3DSTENCILOP_REPLACE);m_pD3DDevice->SetRenderState(D3DRS_STENCILREF, 1);// Render Front faces of LightVolumem_pLightVolumeEffect->SetTechnique("LightVolumeTech");m_pLightVolumeEffect->SetMatrix( "world_view_proj", &mTransform );m_pLightVolumeEffect->Begin( &cPasses, 0 );for( unsigned int p = 0; p < cPasses; ++p ){m_pLightVolumeEffect->BeginPass( p );// Render the vertex buffer contentsm_pD3DDevice->SetStreamSource( 0, m_pLightVolumeVB, 0, sizeof(SIMPLEVERTEX) );m_pD3DDevice->SetIndices(m_pLightVolumeIB);m_pD3DDevice->SetFVF( D3DFVF_SIMPLEVERTEX );m_pLightVolumeEffect->CommitChanges();m_pD3DDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,0, 0, 8, 0, 12 );m_pLightVolumeEffect->EndPass();}m_pLightVolumeEffect->End();m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CW);m_pD3DDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA |D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN |D3DCOLORWRITEENABLE_BLUE);m_pD3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_GREATEREQUAL);m_pD3DDevice->SetRenderState(D3DRS_STENCILFUNC,D3DCMP_EQUAL);m_pD3DDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);m_pD3DDevice->SetRenderState(D3DRS_STENCILREF, 1);// Render Back faces of LightVolumem_pLightVolumeEffect->SetTechnique("LightVolumeTech");m_pLightVolumeEffect->SetMatrix( "world_view_proj", &mTransform );m_pLightVolumeEffect->Begin( &cPasses, 0 );for( unsigned int p = 0; p < cPasses; ++p ){m_pLightVolumeEffect->BeginPass( p );// Render the vertex buffer contentsm_pD3DDevice->SetStreamSource( 0, m_pLightVolumeVB, 0, sizeof(SIMPLEVERTEX) );m_pD3DDevice->SetIndices(m_pLightVolumeIB);m_pD3DDevice->SetFVF( D3DFVF_SIMPLEVERTEX );m_pLightVolumeEffect->CommitChanges();m_pD3DDevice->DrawIndexedPrimitive ( D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12 );m_pLightVolumeEffect->EndPass();}m_pLightVolumeEffect->End();m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);m_pD3DDevice->SetRenderState(D3DRS_ZWRITEENABLE,true);}Thanks in advance for your help and time :)LLORENS   
  Hi,Perhaps you're not using D3DCLEAR_STENCIL flag and correct stencil value in your BeginScene() method. Also, depth stencil format is important. Make sure you're not using any depth-stencil format not containing stencil bits. Like these:D3DFMT_D16D3DFMT_D16_LOCKABLED3DFMT_D32D3DFMT_D24X8Hope this helps.   ;  HiAnd thanks for your fast respond.About what you said to me, i checked the format of stencil buffer, it's D3DFMT_D24S8, and as you can see, i clear the stencil buffer://---------------------------------------------------------------------------------------------------------------/// \brief Junta los Render Targets de la funcion RenderToMRT(...) para poderlos tratar y mostrarlos///\param inElapsedTime Tiempo transcurrido desde la ultima vez que se llamo a esta funcion//---------------------------------------------------------------------------------------------------------------void CCore::RenderToGatherer(double inElapsedTime){D3DXVECTOR4 vTexelAdjust;if( SUCCEEDED( m_pD3DDevice->BeginScene() ) )    {// Limpiamos el RenderTargetm_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,false);RenderLightVolume();//Ponemos una vision Orthonormal para que el quad llene toda la pantallam_pEffect2->SetMatrix( "world_view_proj", &m_mQuadMat );vTexelAdjust = D3DXVECTOR4(-1.0f / (float)m_iBackBufferWidth,1.0f / (float)m_iBackBufferHeight, 0.0f, 0.0f);m_pEffect2->SetVector("TexelToPixelAdjust",&vTexelAdjust);m_pEffect2->SetTechnique( "Gatherer" );unsigned int cPasses;m_pEffect2->Begin( &cPasses, 0 );m_pEffect2->SetTexture("LdrTexture", m_RT_LDR_Preview.GetTexture());m_pEffect2->SetTexture("LdrAfterTexture", m_RT_LDR.GetTexture());m_pEffect2->SetTexture("DepthTexture", m_RT_HDR.GetTexture());m_pEffect2->SetTexture("NormalRT", m_RT_Normal.GetTexture());m_pEffect2->SetTexture("NormalTexture", m_RT_Normal_Preview.GetTexture());m_pEffect2->SetTexture("BuffDistTexture", m_RT_BufferDist.GetTexture());D3DXVECTOR4 vTemp = D3DXVECTOR4(m_pCameraActive->GetPosition().x,m_pCameraActive->GetPosition().y,m_pCameraActive->GetPosition().z,0.0f);m_pEffect2->SetVector("eye_pos", &vTemp);for( unsigned int p = 0; p < cPasses; ++p ){m_pEffect2->BeginPass( p );m_pD3DDevice->SetFVF( D3DFVF_TEXTUREDVERTEX );m_pD3DDevice->SetStreamSource( 0, m_pFullScreenQuad, 0, sizeof(TEXTUREDVERTEX));m_pEffect->CommitChanges();m_pD3DDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2);m_pEffect2->EndPass();}m_pEffect2->End();m_pD3DDevice->EndScene();}}Note: The function RenderLightVolume() is posted on first message. Also, this function, RenderToGatherer(), only take a full screen quad, and mix all the data from G-Buffer of previous pass, and, i'm trying to activate stencil buffer to mask light volume to render only the pixels afected by lights.Any ideas ?LLORENS   ;  Hi againNeed i to set Stencil Mask to 0xFFFFFFFF in this process ? I'm comparing other codes and they use Mask with 0xFFFFFFFF value.;  I knew 0xFFFFFFFF is the default value for D3DRS_STENCILMASK. But perhaps you need to set it manually.   ;  Also, just to add, all your stencil work seems correct. But I want to say something.Sometimes some of render states set by user in Direct3D code (by using IDirect3DDevice9::SetRenderState()) doesn't work properly. Did you try to set'em in your Shader? But in this way, you need to write two or more seperate techniques or passes. Like this:technique LightVolumeMask{    pass FrontFace    {   VertexShader = ... ();   PixelShader = ... (); StencilEnable = true;   ColorWriteEnable = 0;   ZFunc = LESS;   StencilFunc = ALWAYS;   StencilPass = REPLACE;   StencilRef = 1;   StencilMask = 0xFFFFFFFF;   CullMode = CCW;    }    pass BackFace    {   VertexShader = ... ();   PixelShader = ... (); StencilEnable = true;   ColorWriteEnable = 0xFFFFFFFF;   ZFunc = GREATEREQUAL;   StencilFunc = EQUAL;   StencilPass = KEEP;   StencilRef = 1;   StencilMask = 0xFFFFFFFF;   CullMode = CW;    }}I recommend you to look at here. There's a nice demo shows stencil based clipping by using convex light volumes in deferred rendering.Hope this helps.Rohat.   