-
Notifications
You must be signed in to change notification settings - Fork 2k
/
GpuBuffer.cpp
253 lines (199 loc) · 8.67 KB
/
GpuBuffer.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
// Developed by Minigraph
//
// Author: James Stanard
//
#include "pch.h"
#include "GpuBuffer.h"
#include "GraphicsCore.h"
#include "EsramAllocator.h"
#include "CommandContext.h"
#include "BufferManager.h"
#include "UploadBuffer.h"
using namespace Graphics;
void GpuBuffer::Create( const std::wstring& name, uint32_t NumElements, uint32_t ElementSize, const void* initialData )
{
Destroy();
m_ElementCount = NumElements;
m_ElementSize = ElementSize;
m_BufferSize = NumElements * ElementSize;
D3D12_RESOURCE_DESC ResourceDesc = DescribeBuffer();
m_UsageState = D3D12_RESOURCE_STATE_COMMON;
D3D12_HEAP_PROPERTIES HeapProps;
HeapProps.Type = D3D12_HEAP_TYPE_DEFAULT;
HeapProps.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
HeapProps.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
HeapProps.CreationNodeMask = 1;
HeapProps.VisibleNodeMask = 1;
ASSERT_SUCCEEDED( g_Device->CreateCommittedResource( &HeapProps, D3D12_HEAP_FLAG_NONE,
&ResourceDesc, m_UsageState, nullptr, MY_IID_PPV_ARGS(&m_pResource)) );
m_GpuVirtualAddress = m_pResource->GetGPUVirtualAddress();
if (initialData)
CommandContext::InitializeBuffer(*this, initialData, m_BufferSize);
#ifdef RELEASE
(name);
#else
m_pResource->SetName(name.c_str());
#endif
CreateDerivedViews();
}
void GpuBuffer::Create( const std::wstring& name, uint32_t NumElements, uint32_t ElementSize, const UploadBuffer& srcData, uint32_t srcOffset )
{
Destroy();
m_ElementCount = NumElements;
m_ElementSize = ElementSize;
m_BufferSize = NumElements * ElementSize;
D3D12_RESOURCE_DESC ResourceDesc = DescribeBuffer();
m_UsageState = D3D12_RESOURCE_STATE_COMMON;
D3D12_HEAP_PROPERTIES HeapProps;
HeapProps.Type = D3D12_HEAP_TYPE_DEFAULT;
HeapProps.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
HeapProps.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
HeapProps.CreationNodeMask = 1;
HeapProps.VisibleNodeMask = 1;
ASSERT_SUCCEEDED(
g_Device->CreateCommittedResource( &HeapProps, D3D12_HEAP_FLAG_NONE,
&ResourceDesc, m_UsageState, nullptr, MY_IID_PPV_ARGS(&m_pResource)) );
m_GpuVirtualAddress = m_pResource->GetGPUVirtualAddress();
CommandContext::InitializeBuffer(*this, srcData, srcOffset);
#ifdef RELEASE
(name);
#else
m_pResource->SetName(name.c_str());
#endif
CreateDerivedViews();
}
// Sub-Allocate a buffer out of a pre-allocated heap. If initial data is provided, it will be copied into the buffer using the default command context.
void GpuBuffer::CreatePlaced(const std::wstring& name, ID3D12Heap* pBackingHeap, uint32_t HeapOffset, uint32_t NumElements, uint32_t ElementSize,
const void* initialData)
{
m_ElementCount = NumElements;
m_ElementSize = ElementSize;
m_BufferSize = NumElements * ElementSize;
D3D12_RESOURCE_DESC ResourceDesc = DescribeBuffer();
m_UsageState = D3D12_RESOURCE_STATE_COMMON;
ASSERT_SUCCEEDED(g_Device->CreatePlacedResource(pBackingHeap, HeapOffset, &ResourceDesc, m_UsageState, nullptr, MY_IID_PPV_ARGS(&m_pResource)));
m_GpuVirtualAddress = m_pResource->GetGPUVirtualAddress();
if (initialData)
CommandContext::InitializeBuffer(*this, initialData, m_BufferSize);
#ifdef RELEASE
(name);
#else
m_pResource->SetName(name.c_str());
#endif
CreateDerivedViews();
}
void GpuBuffer::Create(const std::wstring& name, uint32_t NumElements, uint32_t ElementSize, EsramAllocator& Allocator, const void* initialData)
{
(void)Allocator;
Create(name, NumElements, ElementSize, initialData);
}
D3D12_CPU_DESCRIPTOR_HANDLE GpuBuffer::CreateConstantBufferView(uint32_t Offset, uint32_t Size) const
{
ASSERT(Offset + Size <= m_BufferSize);
Size = Math::AlignUp(Size, 16);
D3D12_CONSTANT_BUFFER_VIEW_DESC CBVDesc;
CBVDesc.BufferLocation = m_GpuVirtualAddress + (size_t)Offset;
CBVDesc.SizeInBytes = Size;
D3D12_CPU_DESCRIPTOR_HANDLE hCBV = AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
g_Device->CreateConstantBufferView(&CBVDesc, hCBV);
return hCBV;
}
D3D12_RESOURCE_DESC GpuBuffer::DescribeBuffer(void)
{
ASSERT(m_BufferSize != 0);
D3D12_RESOURCE_DESC Desc = {};
Desc.Alignment = 0;
Desc.DepthOrArraySize = 1;
Desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
Desc.Flags = m_ResourceFlags;
Desc.Format = DXGI_FORMAT_UNKNOWN;
Desc.Height = 1;
Desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
Desc.MipLevels = 1;
Desc.SampleDesc.Count = 1;
Desc.SampleDesc.Quality = 0;
Desc.Width = (UINT64)m_BufferSize;
return Desc;
}
void ByteAddressBuffer::CreateDerivedViews(void)
{
D3D12_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
SRVDesc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
SRVDesc.Format = DXGI_FORMAT_R32_TYPELESS;
SRVDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
SRVDesc.Buffer.NumElements = (UINT)m_BufferSize / 4;
SRVDesc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_RAW;
if (m_SRV.ptr == D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN)
m_SRV = AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
g_Device->CreateShaderResourceView(m_pResource.Get(), &SRVDesc, m_SRV);
D3D12_UNORDERED_ACCESS_VIEW_DESC UAVDesc = {};
UAVDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
UAVDesc.Format = DXGI_FORMAT_R32_TYPELESS;
UAVDesc.Buffer.NumElements = (UINT)m_BufferSize / 4;
UAVDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_RAW;
if (m_UAV.ptr == D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN)
m_UAV = AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
g_Device->CreateUnorderedAccessView( m_pResource.Get(), nullptr, &UAVDesc, m_UAV );
}
void StructuredBuffer::CreateDerivedViews(void)
{
D3D12_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
SRVDesc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
SRVDesc.Format = DXGI_FORMAT_UNKNOWN;
SRVDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
SRVDesc.Buffer.NumElements = m_ElementCount;
SRVDesc.Buffer.StructureByteStride = m_ElementSize;
SRVDesc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_NONE;
if (m_SRV.ptr == D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN)
m_SRV = AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
g_Device->CreateShaderResourceView(m_pResource.Get(), &SRVDesc, m_SRV);
D3D12_UNORDERED_ACCESS_VIEW_DESC UAVDesc = {};
UAVDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
UAVDesc.Format = DXGI_FORMAT_UNKNOWN;
UAVDesc.Buffer.CounterOffsetInBytes = 0;
UAVDesc.Buffer.NumElements = m_ElementCount;
UAVDesc.Buffer.StructureByteStride = m_ElementSize;
UAVDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_NONE;
m_CounterBuffer.Create(L"StructuredBuffer::Counter", 1, 4);
if (m_UAV.ptr == D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN)
m_UAV = AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
g_Device->CreateUnorderedAccessView(m_pResource.Get(), m_CounterBuffer.GetResource(), &UAVDesc, m_UAV);
}
void TypedBuffer::CreateDerivedViews(void)
{
D3D12_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
SRVDesc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
SRVDesc.Format = m_DataFormat;
SRVDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
SRVDesc.Buffer.NumElements = m_ElementCount;
SRVDesc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_NONE;
if (m_SRV.ptr == D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN)
m_SRV = AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
g_Device->CreateShaderResourceView(m_pResource.Get(), &SRVDesc, m_SRV);
D3D12_UNORDERED_ACCESS_VIEW_DESC UAVDesc = {};
UAVDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
UAVDesc.Format = m_DataFormat;
UAVDesc.Buffer.NumElements = m_ElementCount;
UAVDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_NONE;
if (m_UAV.ptr == D3D12_GPU_VIRTUAL_ADDRESS_UNKNOWN)
m_UAV = AllocateDescriptor(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
g_Device->CreateUnorderedAccessView(m_pResource.Get(), nullptr, &UAVDesc, m_UAV);
}
const D3D12_CPU_DESCRIPTOR_HANDLE& StructuredBuffer::GetCounterSRV(CommandContext& Context)
{
Context.TransitionResource(m_CounterBuffer, D3D12_RESOURCE_STATE_GENERIC_READ);
return m_CounterBuffer.GetSRV();
}
const D3D12_CPU_DESCRIPTOR_HANDLE& StructuredBuffer::GetCounterUAV(CommandContext& Context)
{
Context.TransitionResource(m_CounterBuffer, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
return m_CounterBuffer.GetUAV();
}