Advertisement

Draw just to get stencil buffer updated

Started by January 15, 2019 11:13 PM
8 comments, last by Promit 6 years ago

Is there an easy way to draw to just update the stencil buffer, but not actually draw the object?

 

I was thinking I could make a blend state that allows transparency and pass the object through a pixel shader that returns pixels with alphas of 0, but I was wondering if there was a better/more efficient approach.

 

For reference, I'm trying to make a silhouette effect where i'll draw the shaded smaller area first, and then the larger visible area after and using the stencil buffer to clip out all of the pixels inside the silhouette.

 

Thanks

Configure the depth-stencil state as required, and set D3D11_RENDER_TARGET_BLEND_DESC::RenderTargetWriteMask to 0. Color output by your pixel shader should be ignored. I'm not sure if you can drop color writes from your shader code entirely... The debug layer should be your friend when trying to find out ;) (For my own use-case I was lazy and just reused a pixel shader with color writes)

I guess this way should be more performant, since no blending operations are required.

Advertisement

Setting the renderTargetWriteMask to 0 is a good solution as Koen said. You can also just bind a null pixel shader or have your pixel shader output nothing by having void as return type - that way you can still do alpha testing for example.

You can also pass 0 for the number of views in OMSetRenderTargets -- no need to have a color-render-target bound at all, just use a depth one.

On top of that, having a NULL pixel shader bound can improve performance further, as mentioned above :D 

Thanks all of you! Amazing suggestions!

 

I did not even consider either of these options.

6 hours ago, Hodgman said:

You can also pass 0 for the number of views in OMSetRenderTargets -- no need to have a color-render-target bound at all, just use a depth one.

On top of that, having a NULL pixel shader bound can improve performance further, as mentioned above :D 

Will setting a NULL pixel shader improve performance even if no color RT is bound? That seems like an easy optimization for a driver to integrate automatically.

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Advertisement
1 hour ago, Promit said:

Will setting a NULL pixel shader improve performance even if no color RT is bound? That seems like an easy optimization for a driver to integrate automatically.

Depends on the driver ;)

On D3D11 I mostly see no difference, but have on one PC, so I do it explicitly now. On D3D12 I haven't seen any driver miss this automatic opportunity - maybe due to PSOs, they put a bit more effort in? 

There's also the obvious case where the pixel shader has side effects - contains a clip/discard instruction, writes depth, or writes to a UAV... Drivers won't automatically kill that work :)

2 hours ago, Promit said:

Will setting a NULL pixel shader improve performance even if no color RT is bound? That seems like an easy optimization for a driver to integrate automatically.

Should be handled by the driver, but an other thing is that if you still have a pixel shader which wants to write output and no render target is bound, the DX debug layer will begin spamming warning messages, so it's still a good idea to have a null or void PS. You can supress warning messages, but I wouldn't recommend it.

It might be also interesting to compare which is the cheapest operation and which is the most expensive. I believe from cheapest to most expensive would be:

set pixel shader < set blend state < set render target

1 hour ago, turanszkij said:

Should be handled by the driver, but an other thing is that if you still have a pixel shader which wants to write output and no render target is bound, the DX debug layer will begin spamming warning messages, so it's still a good idea to have a null or void PS.

Oh yeah I forgot about that. I'd blank the shader for that reason alone ?

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

This topic is closed to new replies.

Advertisement