Advertisement

DirectX 11: Enable Depth Buffer View hides all geometry

Started by March 15, 2020 06:19 PM
5 comments, last by KarimIO 4 years, 10 months ago

I'm adding support for dx11 and I ran into some issues. It renders fine without a depth buffer view, but when I attach it, my triangle gets hidden and I see only a blank screen. In the debugger, the triangle is rendered at a depth of 0.5. It should not be culled. But it is visible in the pipeline. Viewport min/max depth are 0/1, depth is attached, and cleared. I don't know why I just have a blank cleared screen.

enter image description here
device_->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer_);
pBackBuffer->Release();

unsigned int width, height;
window_->getWindowSize(width, height);

D3D11_TEXTURE2D_DESC depth_stencil_desc;
depth_stencil_desc.Width = width;
depth_stencil_desc.Height = height;
depth_stencil_desc.MipLevels = 1;
depth_stencil_desc.ArraySize = 1;
depth_stencil_desc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depth_stencil_desc.SampleDesc.Count = 1;
depth_stencil_desc.SampleDesc.Quality = 0;
depth_stencil_desc.Usage = D3D11_USAGE_DEFAULT;
depth_stencil_desc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depth_stencil_desc.CPUAccessFlags = 0;
depth_stencil_desc.MiscFlags = 0;

//Create the Depth/Stencil View
if (device_->CreateTexture2D(&amp;depth_stencil_desc, NULL, &amp;depth_stencil_buffer_) < 0)
    throw std::runtime_error("DirectX: Could not CreateTexture2D!");

if (device_->CreateDepthStencilView(depth_stencil_buffer_, NULL, &amp;depth_stencil_view_) < 0)
    throw std::runtime_error("DirectX: Could not CreateDepthStencilView!");

// set the render target as the back buffer
device_context_->OMSetRenderTargets(1, &amp;backbuffer_, depth_stencil_view_);

D3D11_VIEWPORT viewport;
ZeroMemory(&amp;viewport, sizeof(D3D11_VIEWPORT));

viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = 800;
viewport.Height = 600;
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;

device_context_->RSSetViewports(1, &amp;viewport);

// Loop {

    // Bind shader

    float col[4] = { 0.0f, 0.2f, 0.4f, 1.0f };
    device_context_->ClearRenderTargetView(backbuffer_, col);
    device_context_->ClearDepthStencilView(depth_stencil_view_, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

    // Bind vertex buffer, constant buffer, texture
    device_context_->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    device_context_->Draw(count, base);
    swap_chain_->Present(0, 0);

// End Loop

did you CreateDepthStencilState

Advertisement

Tried adding it, to no avail:

D3D11_TEXTURE2D_DESC depth_stencil_desc;
  depth_stencil_desc.Width = width;
  depth_stencil_desc.Height = height;
  depth_stencil_desc.MipLevels = 1;
  depth_stencil_desc.ArraySize = 1;
  depth_stencil_desc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  depth_stencil_desc.SampleDesc.Count = 1;
  depth_stencil_desc.SampleDesc.Quality = 0;
  depth_stencil_desc.Usage = D3D11_USAGE_DEFAULT;
  depth_stencil_desc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  depth_stencil_desc.CPUAccessFlags = 0;
  depth_stencil_desc.MiscFlags = 0;

  //Create the Depth/Stencil View
  if (device_->CreateTexture2D(&depth_stencil_desc, NULL, &depth_stencil_buffer_) < 0)
   throw std::runtime_error("DirectX: Could not CreateTexture2D!");

  D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
  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 = 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;

  ID3D11DepthStencilState *depthStencilState;

  // Create the depth stencil state.
  if (device_->CreateDepthStencilState(&depthStencilDesc, &depthStencilState) < 0)
  {
   return false;
  }

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

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

  if (device_->CreateDepthStencilView(depth_stencil_buffer_, &depthStencilViewDesc, &depth_stencil_view_) < 0)
   throw std::runtime_error("DirectX: Could not CreateDepthStencilView!");



  // Set the depth stencil state.
  device_context_->OMSetDepthStencilState(depthStencilState, 1);

  // set the render target as the back buffer
  device_context_->OMSetRenderTargets(1, &backbuffer_, depth_stencil_view_);

  D3D11_VIEWPORT viewport;
  ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

  viewport.TopLeftX = 0;
  viewport.TopLeftY = 0;
  viewport.Width = 800;
  viewport.Height = 600;
  viewport.MinDepth = 0.0f;
  viewport.MaxDepth = 1.0f;

  device_context_->RSSetViewports(1, &viewport);

Or perhaps there is something wrong with your matrices

Aerodactyl55 said:

Or perhaps there is something wrong with your matrices

I tested the glm matrices in another project and it is going fine. Not sure what it could be.

This is the state information from the Graphics Debugger

enter image description here

This topic is closed to new replies.

Advertisement