Search code examples
directxdirectx-11direct3ddirect3d11

How do I clear only part of a depth/stencil view?


If I want to clear an entire depth/stencil view in Direct3D 11, I can easily call ID3D11DeviceContext::ClearDepthStencilView.

Direct3D 11.1 adds support for clearing rectangular portions of render target views using ID3D11DeviceContext1::ClearView.

But I see no way to clear only a portion of a depth/stencil view in Direct3D 11, short of rendering a quad over the desired area? This seems like an odd regression from Direct3D 9, where this was trivially easy. Am I missing something, or is this really not supported?


Solution

  • There is no such function that can clear only a part of depth/stencil view. This is my way to solve the problem:

    1. make a texture. Set Alpha of the part to clear to 1,and other part to 0.
    2. open AlphaTest, only the pixel whose alpha is 1.
    3. open AlphaBlend,set BlendOP to Add,set SrcBlend factor to 0,set DestBlend factor to 1.
    4. set StencilTest and DepthTest to Always, set StencilRef to the value you want to clear.
    5. use orthogonal projection matrix.
    6. draw a rectangle that just covers the screen( z-coordinate/(ZFar-ZNear) will convert to depth),and paste the texture on it.