core: Define VK_USE_PLATFORM_XCB_KHR before including vkd3d_utils.h.
[vkmodelviewer.git] / Core / DepthBuffer.cpp
blobb1616f98c3bc49626285ff7abfc585eebd468028
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // This code is licensed under the MIT License (MIT).
4 // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
5 // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
6 // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
7 // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
8 //
9 // Developed by Minigraph
11 // Author: James Stanard
14 #include "pch.h"
15 #include "DepthBuffer.h"
16 #include "GraphicsCore.h"
17 #include "EsramAllocator.h"
18 #include "DescriptorHeap.h"
20 using namespace Graphics;
22 void DepthBuffer::Create( const std::wstring& Name, uint32_t Width, uint32_t Height, DXGI_FORMAT Format, D3D12_GPU_VIRTUAL_ADDRESS VidMemPtr )
24 D3D12_RESOURCE_DESC ResourceDesc = DescribeTex2D(Width, Height, 1, 1, Format, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL);
26 D3D12_CLEAR_VALUE ClearValue = {};
27 ClearValue.Format = Format;
28 CreateTextureResource(Graphics::g_Device, Name, ResourceDesc, ClearValue, VidMemPtr);
29 CreateDerivedViews(Graphics::g_Device, Format);
32 void DepthBuffer::Create( const std::wstring& Name, uint32_t Width, uint32_t Height, uint32_t Samples, DXGI_FORMAT Format, D3D12_GPU_VIRTUAL_ADDRESS VidMemPtr )
34 D3D12_RESOURCE_DESC ResourceDesc = DescribeTex2D(Width, Height, 1, 1, Format, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL);
35 ResourceDesc.SampleDesc.Count = Samples;
37 D3D12_CLEAR_VALUE ClearValue = {};
38 ClearValue.Format = Format;
39 CreateTextureResource(Graphics::g_Device, Name, ResourceDesc, ClearValue, VidMemPtr);
40 CreateDerivedViews(Graphics::g_Device, Format);
43 void DepthBuffer::Create( const std::wstring& Name, uint32_t Width, uint32_t Height, DXGI_FORMAT Format, EsramAllocator& )
45 Create(Name, Width, Height, Format);
48 void DepthBuffer::Create( const std::wstring& Name, uint32_t Width, uint32_t Height, uint32_t Samples, DXGI_FORMAT Format, EsramAllocator& Allocator )
50 Create(Name, Width, Height, Samples, Format);
53 void DepthBuffer::CreateDerivedViews( ID3D12Device* Device, DXGI_FORMAT Format )
55 ID3D12Resource* Resource = m_pResource.Get();
57 D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc;
58 dsvDesc.Format = GetDSVFormat(Format);
59 if (Resource->GetDesc().SampleDesc.Count == 1)
61 dsvDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
62 dsvDesc.Texture2D.MipSlice = 0;
64 else
66 dsvDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DMS;
69 if (m_hDSV[0].ptr == D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN)
71 m_hDSV[0] = Graphics::AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE_DSV);
72 m_hDSV[1] = Graphics::AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE_DSV);
75 dsvDesc.Flags = D3D12_DSV_FLAG_NONE;
76 Device->CreateDepthStencilView(Resource, &dsvDesc, m_hDSV[0]);
78 dsvDesc.Flags = D3D12_DSV_FLAG_READ_ONLY_DEPTH;
79 Device->CreateDepthStencilView(Resource, &dsvDesc, m_hDSV[1]);
81 DXGI_FORMAT stencilReadFormat = GetStencilFormat(Format);
82 if (stencilReadFormat != DXGI_FORMAT_UNKNOWN)
84 if (m_hDSV[2].ptr == D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN)
86 m_hDSV[2] = Graphics::AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE_DSV);
87 m_hDSV[3] = Graphics::AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE_DSV);
90 dsvDesc.Flags = D3D12_DSV_FLAG_READ_ONLY_STENCIL;
91 Device->CreateDepthStencilView(Resource, &dsvDesc, m_hDSV[2]);
93 dsvDesc.Flags = D3D12_DSV_FLAG_READ_ONLY_DEPTH | D3D12_DSV_FLAG_READ_ONLY_STENCIL;
94 Device->CreateDepthStencilView(Resource, &dsvDesc, m_hDSV[3]);
96 else
98 m_hDSV[2] = m_hDSV[0];
99 m_hDSV[3] = m_hDSV[1];
102 if (m_hDepthSRV.ptr == D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN)
103 m_hDepthSRV = Graphics::AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
105 // Create the shader resource view
106 D3D12_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
107 SRVDesc.Format = GetDepthFormat(Format);
108 if (dsvDesc.ViewDimension == D3D12_DSV_DIMENSION_TEXTURE2D)
110 SRVDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
111 SRVDesc.Texture2D.MipLevels = 1;
113 else
115 SRVDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DMS;
117 SRVDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
118 Device->CreateShaderResourceView( Resource, &SRVDesc, m_hDepthSRV );
120 if (stencilReadFormat != DXGI_FORMAT_UNKNOWN)
122 if (m_hStencilSRV.ptr == D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN)
123 m_hStencilSRV = Graphics::AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
125 SRVDesc.Format = stencilReadFormat;
126 Device->CreateShaderResourceView( Resource, &SRVDesc, m_hStencilSRV );