Problem with DirectX 11 - depthbuffer

Started by
7 comments, last by Jo Nsc 9 years, 10 months ago

Hi Guys!

I am new to DirectX, so excuse me if this is an easy to solve Problem, but i tried for hours now, and i just dont get it.

I try to render some stuff with DirectX 11 (c++). Rendering works fine, but it just ignores the depth,

It draws everything in order of my draw call, meaning the last drawn stuff is visible.

I enabled the DirectX debug mode, to get some Error-Information in my IDE, this it what it tells me:


D3D11 WARNING: ID3D11DeviceContext::DrawIndexed: The Pixel Shader unit expects a Sampler to be set at Slot 0, but none is bound. This is perfectly valid, as a NULL Sampler maps to default Sampler state. However, the developer may not want to rely on the defaults.  [ EXECUTION WARNING #352: DEVICE_DRAW_SAMPLER_NOT_SET]
Der Thread 0x98c hat mit Code 0 (0x0) geendet.
Der Thread 0x1bf0 hat mit Code 0 (0x0) geendet.
Der Thread 0x53c hat mit Code 0 (0x0) geendet.
D3D11 WARNING: Process is terminating. Using simple reporting. Please call ReportLiveObjects() at runtime for standard reporting. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Producer at 0x00CD310C, Refcount: 16. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CD8F28, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CE8208, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CEE3FC, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CEE544, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CEE6B4, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CEE904, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CEEA4C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CEEB98, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CEF24C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CEF5FC, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CEF944, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CEFBBC, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CEFD54, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CEFFC4, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CF0234, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CF044C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CF05E4, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CF084C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00D00184, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CF4C7C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00D003EC, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00D00DCC, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CFDAE4, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00CFF71C, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00D01044, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00D012AC, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00D57D44, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00D50784, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00D5091C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00D50B94, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00D50D2C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00D50FA4, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00D93594, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00D937FC, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00D0F594, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live                         Object :     35 [ STATE_CREATION WARNING #0: UNKNOWN]
DXGI WARNING: Live Producer at 0x00CCF5F8, Refcount: 4. [ STATE_CREATION WARNING #0: ]
DXGI WARNING:  Live Object at 0x00CD1E20, Refcount: 2. [ STATE_CREATION WARNING #0: ]
DXGI WARNING: Live                         Object :      1 [ STATE_CREATION WARNING #0: ]
Das Programm "[916] tst.exe" wurde mit Code 0 (0x0) beendet.

I dont think this warnings have anything to do with my Problem, but i am not 100% sure about that.

This is the Code for creating the backbuffer, depthstencilbuffer etc...


void InitD3D(HWND hWnd)
{
 
   //Describe our SwapChain Buffer
   DXGI_MODE_DESC bufferDesc;
 
   ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));
 
   bufferDesc.Width = SCREEN_WIDTH;
   bufferDesc.Height = SCREEN_HEIGHT;
   bufferDesc.RefreshRate.Numerator = 60;
   bufferDesc.RefreshRate.Denominator = 1;
   bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
   bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
   bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
 
   // create a struct to hold information about the swap chain
   DXGI_SWAP_CHAIN_DESC scd;
 
   // clear out the struct for use
   ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
 
   // fill the swap chain description struct
   scd.BufferDesc = bufferDesc;
   scd.BufferCount = 1;                                   // one back buffer
   scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;     // how swap chain is to be used
   scd.OutputWindow = hWnd;                               // the window to be used
   scd.SampleDesc.Count = 1;                              // how many multisamples
   scd.SampleDesc.Quality = 0;
   scd.Windowed = TRUE;                                   // windowed/full-screen mode
   scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;    // allow full-screen switching
 
   scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // Discard the back buffer contents after presenting.
 
 
   // create a device, device context and swap chain using the information in the scd struct
   hr = D3D11CreateDeviceAndSwapChain(NULL,
                                  D3D_DRIVER_TYPE_HARDWARE,
                                  NULL,
                                  D3D11_CREATE_DEVICE_DEBUG,
                                  NULL,
                                  NULL,
                                  D3D11_SDK_VERSION,
                                  &scd,
                                  &swapchain,
                                  &dev,
                                  NULL,
                                  &devcon);
   if(FAILED(hr))
   {
      MessageBox(NULL, L"D3D11CreateDeviceAndSwapChain failed", NULL, MB_OK);
   }
 
   // get the address of the back buffer
   ID3D11Texture2D *pBackBuffer;
   swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
 
   // use the back buffer address to create the render target
   hr = dev->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer);
   if(FAILED(hr))
   {
      MessageBox(NULL, L"CreateRenderTargetView (backbuffer) failed", NULL, MB_OK);
   }
   pBackBuffer->Release();
   pBackBuffer = 0;
 
   // depth + stencil
   // Initialize the description of the depth buffer.
   D3D11_TEXTURE2D_DESC depthBufferDesc;
   ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc));
 
   // Set up the description of the depth buffer.
   depthBufferDesc.Width = SCREEN_WIDTH;
   depthBufferDesc.Height = SCREEN_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.
   hr = dev->CreateTexture2D(&depthBufferDesc, NULL, &depthStencilBuffer);
   if(FAILED(hr))
   {
      MessageBox(NULL, L"CreadeTexture2D (depthstencilbuffer) failed", NULL, MB_OK);
   }
   hr = dev->CreateDepthStencilView(depthStencilBuffer, NULL, &depthStencilView);
   if(FAILED(hr))
   {
      MessageBox(NULL, L"CreateDepthStencilView (depthstencilbuffer) failed", NULL, MB_OK);
   }
 
   //Set our Render Target
   devcon->OMSetRenderTargets( 1, &backbuffer, depthStencilView );
 

   // Set the viewport
   D3D11_VIEWPORT viewport;
   ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
 
   viewport.TopLeftX = 0;
   viewport.TopLeftY = 0;
   viewport.Width = SCREEN_WIDTH;
   viewport.Height = SCREEN_HEIGHT;
 
   devcon->RSSetViewports(1, &viewport);
 
 
   InitPipeline();
   InitGraphics();

}
 

I rly hope you can help me, since i rly start getting depressed over this.

Sorry for my bad english, not my mother language.

Advertisement

Don't know if this is the problem, but:

There is a quirk with OMSetRenderTargets - it seems to set the targetview pointer to NULL. Check the value of backbuffer after the OMSetRenderTargets call. If it's set to NULL, try:


//Set our Render Target
ID3D11RenderTargetView* tmpPtr = backbuffer;
devcon->OMSetRenderTargets( 1, &tmpPtr, depthStencilView );

With regard to the error messages, you don't say where in your code they occur. If you still get those error messages (which you should NOT be getting), you can create a debug object:


#ifdef _DEBUG
    hr = g_pd3dDevice->QueryInterface(IID_PPV_ARGS(&pDebug));
    if (FAILED(hr))
    {
        //MessageBox(NULL, DXGetErrorDescription(hr), "NON-FATAL: Failed to create debug device", MB_OK);
        MessageBox(NULL, "Failed to Create debug device", "NON-FATAL - Error", MB_OK);
    }
#endif

Then use:



if(pDebug) pDebug->ReportLiveDeviceObjects(D3D11_RLDO_DETAIL); // at the point you get the error messages

// in your destructor or cleanup, don't forget..
SafeRelease(pDebug);
// or
if(pDebug) pDebug->Release();

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I tryed your zero-pointer advice, but backbuffer isnt set to NULL after OMSetRenderTargets. I tried it anyway, but no improvement, Its still ignoring depth.

I also worked on that D3D-Debug messages, turned out i have some problems releasing all of my com-Objects. I am working on that, but i am pretty sure thats not the reason for my Problems with the depth-buffer.

I give you my render function too, maybe you can spot a mistake there. I realy tried everything, but i dont get the depthbuffer working


void RenderFrame(void)
{
   // Set the depth stencil state. 
   devcon->OMSetDepthStencilState(depthStencilState, 1);
 
   //Clear our backbuffer
   float bgColor[4] = {(1.0f, 0.0f, 0.0f, 1.0f)};
   devcon->ClearRenderTargetView(backbuffer, bgColor);
 
   //Refresh the Depth/Stencil view
   devcon->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);
 
 
   // set the shader objects
   devcon->VSSetShader(pVS, 0, 0);
   devcon->PSSetShader(pPS, 0, 0);
   devcon->IASetInputLayout(pLayout);
   devcon->PSSetShaderResources( 0, 1, &TestTexture );
 
   // render terrain
   WVP = kamera.GetViewMatrix() * kamera.GetProjectionMatrix();
   cbPerObj.WorldViewProjection = XMMatrixTranspose(WVP); 
   devcon->UpdateSubresource( cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0 );
   devcon->VSSetConstantBuffers( 0, 1, &cbPerObjectBuffer );
 
   terrain.render(devcon);
 
 
   // render cube
   WVP = CubeWorld * kamera.GetViewMatrix() * kamera.GetProjectionMatrix();
   cbPerObj.WorldViewProjection = XMMatrixTranspose(WVP); 
   devcon->UpdateSubresource( cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0 );
   devcon->VSSetConstantBuffers( 0, 1, &cbPerObjectBuffer );
 
   UINT stride = sizeof( VERTEX );
   UINT offset = 0;
   devcon->IASetVertexBuffers( 0, 1, &pVBuffer, &stride, &offset );
   devcon->IASetIndexBuffer( squareIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
   devcon->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
   devcon->DrawIndexed( 36, 0, 0 );
 
 
   swapchain->Present(0, 0);
}

My guess would be that you've not set up the depth stencil state correctly. A zero near clip plane can also mess things up.

http://msdn.microsoft.com/en-gb/library/windows/desktop/ff476506%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ff476463%28v=vs.85%29.aspx

You where right, i actually forgot to set a depthStencilState at all.

Unfortunately, it doesnt solve my problem. unsure.png

I added this Code to my InitD3D Function:


D3D11_DEPTH_STENCIL_DESC 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 = 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;
 
// Create the depth stencil state.
hr = dev->CreateDepthStencilState(&depthStencilDesc, &depthStencilState);
if(FAILED(hr))
{
   MessageBox(NULL, L"CreateDepthStencilState (depthstencilbuffer) failed", NULL, MB_OK);
}
// Set the depth stencil state.
devcon->OMSetDepthStencilState(depthStencilState, 1);

nothing changed. I tried a bit around and noticed something interesting:

If i change depthStencilDesc.DepthFunc from D3D11_COMPARISON_LESS to D3D11_COMPARISON_GREATER, nothing is drawn at all. If i change it to D3D11_COMPARISON_ALWAYS, it seems like early drawn faces stay in front of later drawn surfaces.

Changing this depthStancilState DOES have an effect! But unfortunately, depth still doesnt work like intended.

Edit:

One thing just came to my mind: maybe its terrible stupid but i dont care about depth in my shaders!

Maybe thats the Problem? Do i have to handle this myself? Maybe inside of the Pixelshader?

I haven't a clue if it matters, but try setting StencilEnable = false. Or, do you enable stenciling for a reason?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I haven't a clue if it matters, but try setting StencilEnable = false. Or, do you enable stenciling for a reason?

No, i just copied that from an example.
I just disabled Stencil - nothing changed.

I rly start to hate this shit. Already wasted over 20 Hours. sad.png

typedef struct D3D11_VIEWPORT {
FLOAT TopLeftX;
FLOAT TopLeftY;
FLOAT Width;
FLOAT Height;
FLOAT MinDepth;
FLOAT MaxDepth;
} D3D11_VIEWPORT;

Have you initialized your viewport correctly? seems to me that you don't set mindepth and maxdepth values - probably they contain some garbage.

Set min to 0.0f and max to 1.0f.

Cheers!

I would kiss you guys if i could, you where right!

I added MinDepth and MaxDepth to the D3D11_VIEWPORT struct, everything works like intended now smile.png .

TY! To everyone who wasted his time on a newbie like me :)

This topic is closed to new replies.

Advertisement