Jump to content
  • Advertisement
Sign in to follow this  
lukabratzi

DX11 DX11 - Disabling depth testing?

This topic is 2138 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

Hey everyone,

 

I'm working on changing my DX11 forward renderer to be a deferred renderer and have everything implemented but am having difficulty figuring out how to disable the depth testing when it comes time to render my lights (which I'm rendering as billboarded quads that always face the camera).

 

Here's how I create my depth stencil view:

virtual DepthStencilView* CreateDepthStencilView( int width, int height )
{
	DepthStencilViewD3D* dsv = new DepthStencilViewD3D( );
			
	D3D11_TEXTURE2D_DESC desc =
	{
		width, height,
		1, 1,
		DXGI_FORMAT_D32_FLOAT,
		{ 1, 0 },
		D3D11_USAGE_DEFAULT,
		D3D11_BIND_DEPTH_STENCIL,
		0, 0
	};
	ID3D11Texture2D *tex = NULL;
	HV(device->CreateTexture2D(&desc, NULL, &tex), "Creating texture for depth/stencil buffers" );
	
	HV(device->CreateDepthStencilView(tex, NULL, &dsv->dsv), "Creating depth stencil view." );

	CreateDepthStencilState(true);

	return dsv;
}
virtual void CreateDepthStencilState(bool depthTestEnabled)
{
	D3D11_DEPTH_STENCIL_DESC dsDesc;

	dsDesc.DepthEnable = depthTestEnabled;
	dsDesc.DepthWriteMask = depthTestEnabled ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO;
	dsDesc.DepthFunc = depthTestEnabled ? D3D11_COMPARISON_LESS : D3D11_COMPARISON_ALWAYS;

	// Stencil test parameters
	dsDesc.StencilEnable = false;
	dsDesc.StencilReadMask = 0xFF;
	dsDesc.StencilWriteMask = 0xFF;

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

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

	// Create depth stencil state
	ID3D11DepthStencilState * pDSState;
	device->CreateDepthStencilState(&dsDesc, &pDSState);
}

And before I render the lights I call:

renderer.CreateDepthStencilState(false);

With the expected result being that my light quads will be drawn without any consideration of depth testing.  Unfortunately it looks like that's not the case and as far as I can tell, my changes are having no effect and depth testing is still being done.  Anyone have experience with this that can tell me what I'm doing wrong?

 

Thanks

 

Share this post


Link to post
Share on other sites
Advertisement
  1. Every call to CreateDepthStencilState() leaks a ID3D11DepthStencilState resource; you will exhaust memory within minutes.
  2. You create a depth/stencil state but you never bind it via OMSetDepthStencilState().

 

L. Spiro

Share this post


Link to post
Share on other sites

AFAIK, in DX11 depth-stencil testing is off by default. Unless you enable it somewhere else in your application and want to disable it again, you do not need to use a ID3D11DepthStencilState. Just pass NULL in the last parameter of OMSetRenderTargets. Otherwise, you have to create a ID3D11DepthStencilView from your ID3D11DepthStencilState, and pass that to OMSetRenderTargets.

And remember to Release every interface when you don't need them anymore, like L. Spiro said. To fully Release the ID3D11DepthStencilView, you have to call OMSetRenderTargets with NULL (or another ID3D11DepthStencilView pointer) as the last parameter, or use ID3D11DeviceContext::ClearState. So you can Release ID3D11DepthStencilView immediately after binding it, and let the next call to OMSetRenderTargets or ClearState destroy it. I can't remember if ID3D11DepthStencilView holds a reference to the ID3D11DepthStencilState... If it does, you can Release it immediately after creating the ID3D11DepthStencilView from it as well.

 

Better yet, you should create two ID3D11DepthStencilState/ID3D11DepthStencilView objects (one with depth enabled, one with it disabled) at the start of your program, switch between them during rendering, and then release them at the end.

 

(Also, don't assign a variable of C++'s bool type to D3D11_DEPTH_STENCIL_DESC.DepthEnable's Win32 BOOL type - can't remember why this is bad).

Edited by tonemgub

Share this post


Link to post
Share on other sites

Every call to CreateDepthStencilState() leaks a ID3D11DepthStencilState resource; you will exhaust memory within minutes.

 

From MSDN:

 

Remarks

4096 unique depth-stencil state objects can be created on a device at a time.

If an application attempts to create a depth-stencil-state interface with the same state as an existing interface, the same interface will be returned and the total number of unique depth-stencil state objects will stay the same.

 

So, no, you won't run out of memory within minutes even if you call create depth stencil state every frame. Most of state objects in D3D11 have the same quality : if you create a similar state, you'll get a pointer to an existing state.

 

[edit] Of course, for best performance, you should create states at start up.

 

Cheers!

Edited by kauna

Share this post


Link to post
Share on other sites

AFAIK, in DX11 depth-stencil testing is off by default

Wrong. Here is the default state (which applies to setting depth state with NULL). User still has to create a DepthStencilView and bind it, but no need to create a DepthStencilState to enable depth test.

 

 


Otherwise, you have to create a ID3D11DepthStencilView from your ID3D11DepthStencilState

I can't remember if ID3D11DepthStencilView holds a reference to the ID3D11DepthStencilState.

There's no connection between the two. You create each of them separately. To create a DSV, you first need to create a resource with D3D11_BIND_DEPTH_STENCIL flag, than create a DSV into that resource.

Edited by N.I.B.

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!