forked from megayuchi/D3D12Lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleConstantBufferPool.cpp
115 lines (91 loc) · 2.71 KB
/
SimpleConstantBufferPool.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "pch.h"
#include <d3d12.h>
#include <d3dx12.h>
#include "D3D12Renderer.h"
#include "SimpleConstantBufferPool.h"
CSimpleConstantBufferPool::CSimpleConstantBufferPool()
{
}
BOOL CSimpleConstantBufferPool::Initialize(ID3D12Device* pD3DDevice, CONSTANT_BUFFER_TYPE type, UINT SizePerCBV, UINT MaxCBVNum)
{
m_ConstantBufferType = type;
m_MaxCBVNum = MaxCBVNum;
m_SizePerCBV = SizePerCBV;
UINT ByteWidth = SizePerCBV * m_MaxCBVNum;
// Create the constant buffer.
if (FAILED(pD3DDevice->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
D3D12_HEAP_FLAG_NONE,
&CD3DX12_RESOURCE_DESC::Buffer(ByteWidth),
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
IID_PPV_ARGS(&m_pResource))))
{
__debugbreak();
}
// create descriptor heap
D3D12_DESCRIPTOR_HEAP_DESC heapDesc = {};
heapDesc.NumDescriptors = m_MaxCBVNum;
heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
if (FAILED(pD3DDevice->CreateDescriptorHeap(&heapDesc, IID_PPV_ARGS(&m_pCBVHeap))))
{
__debugbreak();
}
CD3DX12_RANGE writeRange(0, 0); // We do not intend to write from this resource on the CPU.
m_pResource->Map(0, &writeRange, reinterpret_cast<void**>(&m_pSystemMemAddr));
m_pCBContainerList = new CB_CONTAINER[m_MaxCBVNum];
D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc = {};
cbvDesc.BufferLocation = m_pResource->GetGPUVirtualAddress();
cbvDesc.SizeInBytes = m_SizePerCBV;
UINT8* pSystemMemPtr = m_pSystemMemAddr;
CD3DX12_CPU_DESCRIPTOR_HANDLE heapHandle(m_pCBVHeap->GetCPUDescriptorHandleForHeapStart());
UINT DescriptorSize = pD3DDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
for (DWORD i = 0; i < m_MaxCBVNum; i++)
{
pD3DDevice->CreateConstantBufferView(&cbvDesc, heapHandle);
m_pCBContainerList[i].CBVHandle = heapHandle;
m_pCBContainerList[i].pGPUMemAddr = cbvDesc.BufferLocation;
m_pCBContainerList[i].pSystemMemAddr = pSystemMemPtr;
heapHandle.Offset(1, DescriptorSize);
cbvDesc.BufferLocation += m_SizePerCBV;
pSystemMemPtr += m_SizePerCBV;
}
return TRUE;
}
CB_CONTAINER* CSimpleConstantBufferPool::Alloc()
{
CB_CONTAINER* pCB = nullptr;
if (m_AllocatedCBVNum >= m_MaxCBVNum)
goto lb_return;
pCB = m_pCBContainerList + m_AllocatedCBVNum;
m_AllocatedCBVNum++;
lb_return:
return pCB;
}
void CSimpleConstantBufferPool::Reset()
{
m_AllocatedCBVNum = 0;
}
void CSimpleConstantBufferPool::Cleanup()
{
if (m_pCBContainerList)
{
delete[] m_pCBContainerList;
m_pCBContainerList = nullptr;
}
if (m_pResource)
{
m_pResource->Release();
m_pResource = nullptr;
}
if (m_pCBVHeap)
{
m_pCBVHeap->Release();
m_pCBVHeap = nullptr;
}
}
CSimpleConstantBufferPool::~CSimpleConstantBufferPool()
{
Cleanup();
}