Jump to content
  • Advertisement
Sign in to follow this  
Matt328

DX11 [DX11] Blending Lights

This topic is 2640 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm implementing deferred rendering, everything was going smooth until I got to the part where in order to support multiple directional lights. Just for testing purposes, I've created a red light coming from the positive x-axis, and a blue light coming from the negative x-axis. The problem I'm having is I render one after the other in the same pass before I call present(), but only the first color is drawn. In PIX, I've debugged a pixel that should be colored with the blue light, the debugger shows that the pixel would be blue as expected, but it's failed the depth test.

Here is my blend state description:
D3D11_BLEND_DESC omDesc;
ZeroMemory( &omDesc, sizeof( D3D11_BLEND_DESC ) );
omDesc.RenderTarget[0].BlendEnable = TRUE;
omDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
omDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
omDesc.RenderTarget[0].DestBlend = D3D11_BLEND_ONE;
omDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
omDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
omDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
omDesc.RenderTarget[0].RenderTargetWriteMask = 0x0F;


And here is my depth stencil description:
D3D11_DEPTH_STENCIL_DESC dsDesc;
dsDesc.DepthEnable = true;
dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
dsDesc.DepthFunc = D3D11_COMPARISON_LESS;

// Stencil test parameters
dsDesc.StencilEnable = true;
dsDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK;
dsDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK;

// Stencil operations if pixel is front-facing
dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_DECR;
dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;

// Stencil operations if pixel is back-facing
dsDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
dsDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
dsDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_DECR;
dsDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;



I am drawing this as a fullscreen quad, built from components of my g-buffer, so I'm not sure I fully understand how the depth buffer / stencil thing works. I'd appreciate any help anyone can provide, this has had me pretty stumped all afternoon.

Share this post


Link to post
Share on other sites
Advertisement
You don't want depth writes on on when rendering your lighting. Having depth writes on with LESS as your comparison is like saying "don't render anything else to these pixels, unless it has a closer depth value than me". So if you render two fullscreen quads that have the same depth value, only the first will render since the second will fail the depth test. So you should disable depth writes (set DepthWriteMask to 0) when rendering lighting, and you should also set the comparison to LESS_EQUAL.You probably also want to disable stencil for now...it can be useful for optimizing your lighting pass but you should get your lighting working correctly first before optimizing.

Share this post


Link to post
Share on other sites
Up until now I'd gotten by with just pasting in someone else's generic initialization code, but I think now I'm going to have to spend some time and understand the whole depth aspect. Thanks for the reply.

Follow up question going forward, how expensive is OMSetDepthStencilState()? Is it ok to be called a few times per frame, or is it pretty costly?

Share this post


Link to post
Share on other sites
Like any D3D call you'll want to minimize calling it, but you don't need to go crazy. You would probably need to call it several hundreds of times a frame for it to be a problem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!