Jump to content

  • Log In with Google      Sign In   
  • Create Account


DirectX11 Depth Buffer Problem (solved)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 RudiMedia   Members   -  Reputation: 116

Like
0Likes
Like

Posted 29 April 2012 - 03:57 PM

Hello everyone,
I have this problem with my depth buffer and all the solutions from the countless other threads with this problem do not seem to work for me.

Posted Image

I use the same code as shown here: http://www.rastertek.../dx11tut03.html

Global Variables:
extern ID3D11Device* pDevice;
extern ID3D11DeviceContext* pDevCon;
extern ID3D11RenderTargetView* pRenderTargetView;
extern ID3D11DepthStencilState* pDepthStencilState;
extern ID3D11DepthStencilView* pDepthStencilView;
extern ID3D11Texture2D* pDepthStencilBuffer;
extern ID3D11RasterizerState* pRasterState;

Direct3D initialisation:
HRESULT result;

D3D11_TEXTURE2D_DESC depthBufferDesc;
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;

result = pDevice->CreateTexture2D(&depthBufferDesc, NULL, &pDepthStencilBuffer);

if(FAILED(result))
{
  MessageBox(NULL, L"Error1", L"Error", MB_OK | MB_ICONERROR);
}

D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
ZeroMemory(&depthStencilDesc, sizeof(D3D11_DEPTH_STENCIL_DESC));
// Depth test parameters
depthStencilDesc.DepthEnable = true;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
// Stencil test parameters
depthStencilDesc.StencilEnable = true;
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;

result = pDevice->CreateDepthStencilState(&depthStencilDesc, &pDepthStencilState);
pDevCon->OMSetDepthStencilState(pDepthStencilState, 1);

if(FAILED(result))
{
  MessageBox(NULL, L"Error2", L"Error", MB_OK | MB_ICONERROR);
}

D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
ZeroMemory(&depthStencilViewDesc, sizeof(D3D11_DEPTH_STENCIL_VIEW_DESC));
depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0;
result = pDevice->CreateDepthStencilView(pDepthStencilBuffer, &depthStencilViewDesc, &pDepthStencilView);
pDevCon->OMSetRenderTargets(1, &pRenderTargetView, pDepthStencilView);

if(FAILED(result))
{
  MessageBox(NULL, L"Error3", L"Error", MB_OK | MB_ICONERROR);
}

D3D11_RASTERIZER_DESC rasterDesc;
rasterDesc.AntialiasedLineEnable = false;
rasterDesc.CullMode = D3D11_CULL_BACK;
rasterDesc.DepthBias = 0;
rasterDesc.DepthBiasClamp = 0.0f;
rasterDesc.DepthClipEnable = true;
rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.FrontCounterClockwise = false;
rasterDesc.MultisampleEnable = false;
rasterDesc.ScissorEnable = false;
rasterDesc.SlopeScaledDepthBias = 0.0f;

result = pDevice->CreateRasterizerState(&rasterDesc, &pRasterState);
pDevCon->RSSetState(pRasterState);

if(FAILED(result))
{
  MessageBox(NULL, L"Error4", L"Error", MB_OK | MB_ICONERROR);
}

D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = Width;
viewport.Height = Height;
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;

pDevCon->RSSetViewports(1, &viewport);

Camera Code:
m_pV3Pos = &Position;
m_pV3LookAt = &LookAt;
m_pV3View = (D3DXVECTOR3*)malloc(sizeof(D3DXVECTOR3));
m_pV3Up = (D3DXVECTOR3*)malloc(sizeof(D3DXVECTOR3));
*m_pV3View = *m_pV3Pos - *m_pV3LookAt;
D3DXVec3Normalize(m_pV3View, m_pV3View);
D3DXVec3Cross(m_pV3Up, &D3DXVECTOR3(0,1,0), m_pV3View);
D3DXVec3Cross(m_pV3Up, m_pV3View, m_pV3Up);
m_pMatView = (D3DXMATRIX*)malloc(sizeof(D3DXMATRIX));
m_pMatProj = (D3DXMATRIX*)malloc(sizeof(D3DXMATRIX));

D3DXVec3Normalize(m_pV3Up, m_pV3Up);
D3DXMatrixLookAtLH(m_pMatView, m_pV3Pos, m_pV3LookAt, m_pV3Up);
D3DXMatrixPerspectiveFovLH(m_pMatProj, FOV, (float)Width/Height, 1.0f, 1000.0f);

Draw Code:
D3DXCOLOR bgColor( 0, 0.2f, 0.4f, 1.0f );
pDevCon->ClearRenderTargetView(pRenderTargetView, bgColor);
pDevCon->ClearDepthStencilView(pDepthStencilView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);
model->Draw();
pSwapChain->Present(0, 0);

I realy have no ideas left so if anyone could help me with this issue I would be most grateful Posted Image

Edited by Rudi Media, 01 May 2012 - 06:50 AM.


Sponsor:

#2 Muzzy A   Members   -  Reputation: 633

Like
0Likes
Like

Posted 29 April 2012 - 06:42 PM

Wow D3D11 looks alot different than D3D9 o.O. In the link you gave, he didnt use the flag 'D3D11_CLEAR_STENCIL', maybe take that out and try again.

Draw Code:
D3DXCOLOR bgColor( 0, 0.2f, 0.4f, 1.0f );
pDevCon->ClearRenderTargetView(pRenderTargetView, bgColor);
pDevCon->ClearDepthStencilView(pDepthStencilView, D3D11_CLEAR_DEPTH| D3D11_CLEAR_STENCIL, 1.0f, 0);
model->Draw();
pSwapChain->Present(0, 0);

Edited by Muzzy A, 29 April 2012 - 06:53 PM.


#3 RudiMedia   Members   -  Reputation: 116

Like
0Likes
Like

Posted 30 April 2012 - 04:47 AM

Sadly, that doesn't solve the problem.

#4 kauna   Crossbones+   -  Reputation: 2338

Like
0Likes
Like

Posted 30 April 2012 - 05:29 AM

pDevCon->OMSetDepthStencilState(pDepthStencilState, 1);

if(FAILED(result))
{
MessageBox(NULL, L"Error2", L"Error", MB_OK | MB_ICONERROR);
}



Your error handling code at least doens't work as it should. You don't seems to update your result variable.
Also, enable d3d debugging to see any error messages in the debug window.

Otherwise, it seems that you z-buffer doesn't work as intended. As if it wasn't bound or z-testing is disabled.

You can also disable stencil testing at this point since you don't use it.

Best regards!

#5 RudiMedia   Members   -  Reputation: 116

Like
0Likes
Like

Posted 01 May 2012 - 05:44 AM

Well OMSetDepthStencilState doesn't return a HRESULT value, which is why I don't update my result variable at this place.
Regarding d3d debugging I have actually no experience at all. I'm currently using the VS11 beta and heard that such a feature is already built-in but it seems like you're only able to use it if you have Windows 8 installed. Is there any other way to debug your d3d applications?

#6 mhagain   Crossbones+   -  Reputation: 7822

Like
1Likes
Like

Posted 01 May 2012 - 06:10 AM

Go to the DirectX control panel (it should be findable by just typing "DirectX Control Panel" in your Start menu) and enable debugging from there.

One potential problem however that I can see is that you're creating and setting your depth testing state before you create your depth/stencil view - maybe try doing it after instead and see what happens?

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#7 kauna   Crossbones+   -  Reputation: 2338

Like
1Likes
Like

Posted 01 May 2012 - 06:22 AM

Hi,

Other things to verify:

- by any chance, are you using a multisampled back buffer? if so, AFAIK, the depth buffer has to be multisampled too
- depth buffer resolution has to be same as back buffer resolution

- Enable the d3d debug as instructed earlier (works on Win7 too). If there is that kind of problem, it should be able to tell you about it.

Cheers!

#8 RudiMedia   Members   -  Reputation: 116

Like
0Likes
Like

Posted 01 May 2012 - 06:40 AM

- by any chance, are you using a multisampled back buffer? if so, AFAIK, the depth buffer has to be multisampled too


Hey thank you that was my problem Posted Image

#9 kauna   Crossbones+   -  Reputation: 2338

Like
0Likes
Like

Posted 01 May 2012 - 10:57 AM

Hi,

glad to be able to help. I got the idea from looking your screenshot which seemed to be multisampled :)

Best regards!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS