Walt D
Walt D

Reputation: 4731

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?

Upvotes: 1

Views: 2915

Answers (2)

galop1n
galop1n

Reputation: 8824

There is an excellent reason at removing the partial clears in the API. First, it is always possible to emulate them by drawing quads with proper render states, and second, all GPUs have fast clear and resolve hardware. Using them in the intend logic greatly improve performance of the rendering.

With the DX11 clear API, it is possible to use the fast clear and the latter GPU optimisation. A depth buffer fast clear also prepare for an early depth test ( prior to pixel shading, because yes, the real depth test is post pixel shading ), and some bandwidth optimisation on access to the depth buffer will rendering. If you clear with a quad, you lost all that and draw cost will rise.

Upvotes: 0

zhangbaochong
zhangbaochong

Reputation: 170

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.

Upvotes: 2

Related Questions