[D3D12] Enabling the depth test in d3d12

Started by
7 comments, last by kretash 8 years, 7 months ago

Hello, first post in gamedev.net! ph34r.png

I have been building on top of the HelloTriangle demo and got a working obj loader. And its working fine, but i can't figure out how to enable the depth test in d3d12. My previous experience comes from opengl, so I'm just expecting to call glEnable(GL_DEPTH_TEST). Is it that simple in DirectX12?

I have looked in the multi-threading example and the instance example and couldn't find anything similar. What I did see is that both examples have a depth map. The multi-threading has shadows so it might just be rendering the depth to calculate that, but I'm not sure.

Cheers

Here's what it looks like: http://i.imgur.com/AQsEkeE.png

Advertisement
It isn't that simple, in fact that abstraction is a bit of a lie ;)

In D3D12, for something like that, you need to have a Pipeline State Object setup which contains details on everything needed for a draw operation; These docs show how it is setup

Cool, I'm guessing you'll need to set this up.


psoDesc.DepthStencilState.DepthEnable = FALSE;

Thanks, i'll look into it in the morning.

I have added this depth stencil description to the PSO and is doing the same thing.


  CD3DX12_DEPTH_STENCIL_DESC depthStencilDesc(D3D12_DEFAULT);
  depthStencilDesc.DepthEnable = true;
  depthStencilDesc.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
  depthStencilDesc.DepthFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL;
  depthStencilDesc.StencilEnable = FALSE;

Is the anything else i have to set up?

Do you have a depth buffer bound?

I have added a depth buffer but I is not rendering to it.


    D3D12_DESCRIPTOR_HEAP_DESC dsvHeapDesc = {};
    dsvHeapDesc.NumDescriptors = 1 + FrameCount * 1;
    dsvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
    dsvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
    result = m_device->CreateDescriptorHeap( &dsvHeapDesc, IID_PPV_ARGS( &m_dsvHeap ) );
    assert( result == S_OK && "ERROR CREATING THE DSV HEAP" );

{//Creating the depth texture
    CD3DX12_RESOURCE_DESC depth_texture( D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0,
      static_cast< UINT >( m_viewport.Width ), static_cast< UINT >( m_viewport.Height ), 1, 1,
      DXGI_FORMAT_D32_FLOAT, 1, 0, D3D12_TEXTURE_LAYOUT_UNKNOWN,
      D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE );

    D3D12_CLEAR_VALUE clear_value;
    clear_value.Format = DXGI_FORMAT_D32_FLOAT;
    clear_value.DepthStencil.Depth = 1.0f;
    clear_value.DepthStencil.Stencil = 0;

    result = m_device->CreateCommittedResource( &CD3DX12_HEAP_PROPERTIES( D3D12_HEAP_TYPE_DEFAULT ),
      D3D12_HEAP_FLAG_NONE, &depth_texture, D3D12_RESOURCE_STATE_DEPTH_WRITE, &clear_value,
      IID_PPV_ARGS( &m_depthStencil ) );
    assert( result == S_OK && "CREATING THE DEPTH STENCIL FAILED" );

    m_device->CreateDepthStencilView( m_depthStencil.Get(), nullptr, m_dsvHeap->GetCPUDescriptorHandleForHeapStart() );
  }

m_commandList->ClearDepthStencilView( m_dsvHeap->GetCPUDescriptorHandleForHeapStart(),
    D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);

BZ8C7le.png

The only thing that i think its missing is that the resource is not bound, but i dont know how to bind it.

Create a texture with D3D12_RESOURCE_DIMENSION_TEXTURE2D and D3D12_TEXTURE_LAYOUT_UNKNOWN. after that, create a view on a DSV heap. while recording, get the D3D12_CPU_DESCRIPTOR_HANDLE of the dsv and set it at the last parameter of OMSetRenderTargets. Before that, ofc clear the view.

Looking at your code above, you have forgotten to set it in the OMSetRenderTargets function? like this: OMSetRenderTargets(1, &rtvHandle, false, &dsvHandle);

I was missing this


CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle( m_rtvHeap->GetCPUDescriptorHandleForHeapStart(),
    m_frameIndex, m_rtvDescriptorSize );
  CD3DX12_CPU_DESCRIPTOR_HANDLE dsvHandle( m_dsvHeap->GetCPUDescriptorHandleForHeapStart() );
  m_commandList->OMSetRenderTargets( 1, &rtvHandle, FALSE, &dsvHandle );

Now it works!

Yes, you were right. I just saw your post.

This topic is closed to new replies.

Advertisement