Advertisement

Z fighting in skybox

Started by May 05, 2020 06:07 PM
4 comments, last by RobM 4 years, 6 months ago

Hi

After porting my game engine from DX9 to DX11, I seem to have an issue with my sky box. Previously it worked fine, I do the usual box around the player and set position.w to position.z so they're both the same which should result in a z of 1.0f in the next stage.

My depth buffer works for all other graphics but when I render my sky box, depending on my view and position, I get some (what looks like) z-fighting against - nothing. Sometimes the entire skybox triangle displays correctly until I move my camera and then another one may appear with some z-fighting artifacts, sometimes they don't display at all.

If I turn depth off, the skybox renders fine. My depth buffer creation is standard I believe - I've turned off the depth stencil in the state (turning it on makes no difference). I create the depth stencil buffer like this:

		D3D11_TEXTURE2D_DESC depthBufferDesc;
		D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
		D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;

		ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc));

		// Set up the description of the depth buffer.
		depthBufferDesc.Width = width;
		depthBufferDesc.Height = height;
		depthBufferDesc.MipLevels = 1;
		depthBufferDesc.ArraySize = 1;
		depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
		depthBufferDesc.SampleDesc.Count = 1;
		depthBufferDesc.SampleDesc.Quality = 0;
		depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
		depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
		depthBufferDesc.CPUAccessFlags = 0;
		depthBufferDesc.MiscFlags = 0;

		// Create the texture for the depth buffer using the filled out description.
		HRESULT result = direct3DDevice->CreateTexture2D(&depthBufferDesc, NULL, &depthStencilBuffer);
		if (FAILED(result))
		{
			bool b = true;
		}

		// Initialize the description of the stencil state.
		ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc));

		// Set up the description of the stencil state.
		depthStencilDesc.DepthEnable = true;
		depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
		depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;

		depthStencilDesc.StencilEnable = false;
		depthStencilDesc.StencilReadMask = 0xFF;
		depthStencilDesc.StencilWriteMask = 0xFF;

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

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

		// Create the depth stencil state.
		result = direct3DDevice->CreateDepthStencilState(&depthStencilDesc, &depthStencilState);
		if (FAILED(result))
		{
			bool b = true;
		}

		// Set the depth stencil state.
		deviceContext->OMSetDepthStencilState(depthStencilState, 0);

		// Initailze the depth stencil view.
		ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));

		// Set up the depth stencil view description.
		depthStencilViewDesc.Format = DXGI_FORMAT_D32_FLOAT;
		depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
		depthStencilViewDesc.Texture2D.MipSlice = 0;

		// Create the depth stencil view.
		result = direct3DDevice->CreateDepthStencilView(depthStencilBuffer, NULL, &depthStencilView);
		if (FAILED(result))
		{
			bool b = true;
		}

Any thoughts? Here's a pic:

This is looking at the top right corner of the skybox

Thanks

What size is your box around the player? I remember having a similar problem once, and IIRC it was because the box was too small, so the near clipping plane was cutting it. Pushing it out to something like 100x100x100 resolved if nicely.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Advertisement

@undefined thanks for the post, but coincidentally, the cube size is 100 x 100 x 100

This happened to me when a set the depth compare to less. Setting it to lessorequal calmed the fighting …

Green_Baron said:

This happened to me when a set the depth compare to less. Setting it to lessorequal calmed the fighting …

That fixed it. Thank you.

This topic is closed to new replies.

Advertisement