1- /* *****************************************************************************
2- Copyright (C) 2023 by Lain Bailey <[email protected] > 3-
4- This program is free software: you can redistribute it and/or modify
5- it under the terms of the GNU General Public License as published by
6- the Free Software Foundation, either version 2 of the License, or
7- (at your option) any later version.
8-
9- This program is distributed in the hope that it will be useful,
10- but WITHOUT ANY WARRANTY; without even the implied warranty of
11- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12- GNU General Public License for more details.
13-
14- You should have received a copy of the GNU General Public License
15- along with this program. If not, see <http://www.gnu.org/licenses/>.
16- ******************************************************************************/
17-
181#include " d3d12-command-context.hpp"
192
203#include < util/base.h>
@@ -264,12 +247,30 @@ D3D12_CPU_DESCRIPTOR_HANDLE DescriptorAllocator::Allocate(uint32_t Count)
264247 m_DescriptorSize = m_DeviceInstance->GetDevice ()->GetDescriptorHandleIncrementSize (m_Type);
265248 }
266249
250+ if (m_RemainingFreeHandles <= 0 ) {
251+ throw HRError (" DescriptorAllocator: No remaining free handles" );
252+ }
253+
267254 D3D12_CPU_DESCRIPTOR_HANDLE ret = m_CurrentHandle;
268255 m_CurrentHandle.ptr += Count * m_DescriptorSize;
269256 m_RemainingFreeHandles -= Count;
270257 return ret;
271258}
272259
260+ size_t DescriptorAllocator::RemainingFreeCount () {
261+ return m_RemainingFreeHandles;
262+ }
263+
264+ void DescriptorAllocator::DiscardAll ()
265+ {
266+ if (m_CurrentHeap == nullptr ) {
267+ return ;
268+ }
269+
270+ m_CurrentHandle = m_CurrentHeap->GetCPUDescriptorHandleForHeapStart ();
271+ m_RemainingFreeHandles = sm_NumDescriptorsPerHeap;
272+ }
273+
273274DescriptorHandle::DescriptorHandle ()
274275{
275276 m_CpuHandle.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
@@ -743,6 +744,19 @@ GpuBuffer::~GpuBuffer()
743744 Destroy ();
744745}
745746
747+ void GpuBuffer::Destroy ()
748+ {
749+ if (m_UAV.ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
750+ m_UAV.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
751+ }
752+
753+ if (m_SRV.ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
754+ m_SRV.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
755+ }
756+
757+ GpuResource::Destroy ();
758+ }
759+
746760void GpuBuffer::Create (const std::wstring &name, uint32_t NumElements, uint32_t ElementSize, const void *initialData)
747761{
748762 Destroy ();
@@ -1544,6 +1558,11 @@ DepthBuffer::DepthBuffer(D3D12DeviceInstance *DeviceInstance, float ClearDepth,
15441558 m_hStencilSRV.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
15451559}
15461560
1561+ DepthBuffer::~DepthBuffer ()
1562+ {
1563+ Destroy ();
1564+ }
1565+
15471566void DepthBuffer::Create (const std::wstring &Name, uint32_t Width, uint32_t Height, DXGI_FORMAT Format,
15481567 D3D12_GPU_VIRTUAL_ADDRESS VidMemPtr)
15491568{
@@ -1563,6 +1582,35 @@ void DepthBuffer::Create(const std::wstring &Name, uint32_t Width, uint32_t Heig
15631582 CreateDerivedViews (m_DeviceInstance->GetDevice ());
15641583}
15651584
1585+ void DepthBuffer::Destroy ()
1586+ {
1587+ if (m_hDSV[0 ].ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
1588+ m_hDSV[0 ].ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
1589+ }
1590+
1591+ if (m_hDSV[1 ].ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
1592+ m_hDSV[1 ].ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
1593+ }
1594+
1595+ if (m_hDSV[2 ].ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
1596+ m_hDSV[2 ].ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
1597+ }
1598+
1599+ if (m_hDSV[3 ].ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
1600+ m_hDSV[3 ].ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
1601+ }
1602+
1603+ if (m_hDepthSRV.ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
1604+ m_hDepthSRV.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
1605+ }
1606+
1607+ if (m_hStencilSRV.ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
1608+ m_hStencilSRV.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
1609+ }
1610+
1611+ GpuResource::Destroy ();
1612+ }
1613+
15661614const D3D12_CPU_DESCRIPTOR_HANDLE &DepthBuffer::GetDSV () const
15671615{
15681616 return m_hDSV[0 ];
@@ -3625,6 +3673,7 @@ void EngineProfiling::DisplayFrameRate()
36253673
36263674SamplerDesc::SamplerDesc (D3D12DeviceInstance *DeviceInstance) : m_DeviceInstance(DeviceInstance)
36273675{
3676+ Sampler.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
36283677 Filter = D3D12_FILTER_ANISOTROPIC;
36293678 AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
36303679 AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
@@ -3640,6 +3689,13 @@ SamplerDesc::SamplerDesc(D3D12DeviceInstance *DeviceInstance) : m_DeviceInstance
36403689 MaxLOD = D3D12_FLOAT32_MAX;
36413690}
36423691
3692+ SamplerDesc::~SamplerDesc ()
3693+ {
3694+ if (Sampler.ptr != D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN) {
3695+ Sampler.ptr = D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN;
3696+ }
3697+ }
3698+
36433699void SamplerDesc::SetTextureAddressMode (D3D12_TEXTURE_ADDRESS_MODE AddressMode)
36443700{
36453701 AddressU = AddressMode;
@@ -3655,17 +3711,10 @@ void SamplerDesc::SetBorderColor(Color Border)
36553711 BorderColor[3 ] = Border.w ;
36563712}
36573713
3658- D3D12_CPU_DESCRIPTOR_HANDLE SamplerDesc::CreateDescriptor (void )
3714+ void SamplerDesc::CreateDescriptor (void )
36593715{
3660- size_t hashValue = Utility::HashState (this );
3661- auto iter = m_DeviceInstance->GetSamplerCache ().find (hashValue);
3662- if (iter != m_DeviceInstance->GetSamplerCache ().end ()) {
3663- return iter->second ;
3664- }
3665-
3666- D3D12_CPU_DESCRIPTOR_HANDLE Handle = m_DeviceInstance->AllocateDescriptor (D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
3667- m_DeviceInstance->GetDevice ()->CreateSampler (this , Handle);
3668- return Handle;
3716+ Sampler = m_DeviceInstance->AllocateDescriptor (D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
3717+ m_DeviceInstance->GetDevice ()->CreateSampler (this , Sampler);
36693718}
36703719
36713720void SamplerDesc::CreateDescriptor (D3D12_CPU_DESCRIPTOR_HANDLE Handle)
@@ -3862,11 +3911,6 @@ DescriptorAllocator *D3D12DeviceInstance::GetDescriptorAllocator()
38623911 return m_DescriptorAllocator;
38633912}
38643913
3865- std::map<size_t , D3D12_CPU_DESCRIPTOR_HANDLE> &D3D12DeviceInstance::GetSamplerCache ()
3866- {
3867- return m_SamplerCache;
3868- }
3869-
38703914ID3D12DescriptorHeap *D3D12DeviceInstance::RequestCommonHeap (D3D12_DESCRIPTOR_HEAP_TYPE Type)
38713915{
38723916 D3D12_DESCRIPTOR_HEAP_DESC Desc;
@@ -3930,6 +3974,11 @@ D3D12_CPU_DESCRIPTOR_HANDLE D3D12DeviceInstance::AllocateDescriptor(D3D12_DESCRI
39303974 return m_DescriptorAllocator[Type].Allocate (Count);
39313975}
39323976
3977+ void D3D12DeviceInstance::DiscardDescriptorAll (D3D12_DESCRIPTOR_HEAP_TYPE Type, UINT Count)
3978+ {
3979+ m_DescriptorAllocator[Type].DiscardAll ();
3980+ }
3981+
39333982GraphicsContext *D3D12DeviceInstance::GetNewGraphicsContext (const std::wstring &ID)
39343983{
39353984 CommandContext *NewContext = m_ContextManager->AllocateContext (D3D12_COMMAND_LIST_TYPE_DIRECT);
0 commit comments