forked from megayuchi/D3D12Lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDescriptorPool.cpp
73 lines (64 loc) · 2.02 KB
/
DescriptorPool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "pch.h"
#include <dxgi.h>
#include <dxgi1_4.h>
#include <d3d12.h>
#include <dxgidebug.h>
#include <d3dx12.h>
#include "../D3D_Util/D3DUtil.h"
#include "DescriptorPool.h"
CDescriptorPool::CDescriptorPool()
{
}
BOOL CDescriptorPool::Initialize(ID3D12Device5* pD3DDevice, UINT MaxDescriptorCount)
{
BOOL bResult = FALSE;
m_pD3DDevice = pD3DDevice;
m_MaxDescriptorCount = MaxDescriptorCount;
m_srvDescriptorSize = m_pD3DDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
// create descriptor heap
D3D12_DESCRIPTOR_HEAP_DESC commonHeapDesc = {};
commonHeapDesc.NumDescriptors = m_MaxDescriptorCount;
commonHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
commonHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
if (FAILED(m_pD3DDevice->CreateDescriptorHeap(&commonHeapDesc, IID_PPV_ARGS(&m_pDescritorHeap))))
{
__debugbreak();
goto lb_return;
}
m_cpuDescriptorHandle = m_pDescritorHeap->GetCPUDescriptorHandleForHeapStart();
m_gpuDescriptorHandle = m_pDescritorHeap->GetGPUDescriptorHandleForHeapStart();
bResult = TRUE;
lb_return:
return bResult;
}
BOOL CDescriptorPool::AllocDescriptorTable(D3D12_CPU_DESCRIPTOR_HANDLE* pOutCPUDescriptor, D3D12_GPU_DESCRIPTOR_HANDLE* pOutGPUDescriptor, UINT DescriptorCount)
{
BOOL bResult = FALSE;
if (m_AllocatedDescriptorCount + DescriptorCount > m_MaxDescriptorCount)
{
goto lb_return;
}
UINT offset = m_AllocatedDescriptorCount + DescriptorCount;
*pOutCPUDescriptor = CD3DX12_CPU_DESCRIPTOR_HANDLE(m_cpuDescriptorHandle, m_AllocatedDescriptorCount, m_srvDescriptorSize);
*pOutGPUDescriptor = CD3DX12_GPU_DESCRIPTOR_HANDLE(m_gpuDescriptorHandle, m_AllocatedDescriptorCount, m_srvDescriptorSize);
m_AllocatedDescriptorCount += DescriptorCount;
bResult = TRUE;
lb_return:
return bResult;
}
void CDescriptorPool::Reset()
{
m_AllocatedDescriptorCount = 0;
}
void CDescriptorPool::Cleanup()
{
if (m_pDescritorHeap)
{
m_pDescritorHeap->Release();
m_pDescritorHeap = nullptr;
}
}
CDescriptorPool::~CDescriptorPool()
{
Cleanup();
}