-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathTransientCache.h
104 lines (78 loc) · 3.11 KB
/
TransientCache.h
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
//--------------------------------------------------------------------------------------
// TransientCache.h
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#pragma once
#include "TransientResource.h"
namespace ATG
{
using namespace Microsoft::WRL;
struct PageRef;
//--------------------------------------------------
// CachedResource
enum class BindFlags
{
None = 0,
SRV = 0x1,
UAV = 0x2,
RTV = 0x4,
DSV = 0x8,
};
inline BindFlags operator|(BindFlags l, BindFlags r) { return BindFlags(size_t(l) | size_t(r)); }
inline BindFlags operator&(BindFlags l, BindFlags r) { return BindFlags(size_t(l) & size_t(r)); }
inline bool HasFlag(BindFlags state, BindFlags test) { return size_t(state & test) != 0; }
//--------------------------------------------------
// TransientDesc
struct TransientDesc
{
D3D12_RESOURCE_DESC d3dDesc;
D3D12_CLEAR_VALUE clear;
BindFlags flags;
int getPageCount() const;
};
//--------------------------------------------------
// CachedResource
struct CachedResource
{
VirtualMemPtr address;
int pageCount;
ComPtr<ID3D12Resource> resource;
bool hasMetadata;
size_t frameNumber;
std::vector<PageRef> pages;
D3D12_CPU_DESCRIPTOR_HANDLE RTV;
D3D12_CPU_DESCRIPTOR_HANDLE DSV;
D3D12_CPU_DESCRIPTOR_HANDLE SRV;
D3D12_CPU_DESCRIPTOR_HANDLE UAV;
D3D12_GPU_VIRTUAL_ADDRESS GpuAddress() const { return reinterpret_cast<D3D12_GPU_VIRTUAL_ADDRESS>(address.get()); }
};
//--------------------------------------------------
// TransientCache
class TransientCache
{
public:
TransientCache(ID3D12Device* device);
void Uninitialize();
void NextFrame();
CachedResource* Get(ResourceHandle handle);
CachedResource& GetChecked(ResourceHandle handle) { assert(handle != ResourceHandle::Invalid); return m_cache.at(handle.key)[handle.index]; }
// Finds or creates an available resource matching the resource description.
ResourceHandle Create(const TransientDesc& desc);
private:
void AcquireResource(const TransientDesc& desc, size_t& index, size_t& key);
CachedResource CreateResource(const TransientDesc& desc);
private:
// Hashed TransientDesc (size_t) to array of cached resource instances (only allocated once per frame.)
using Cache = std::unordered_map<size_t, std::vector<CachedResource>>;
private:
ID3D12Device* m_device;
size_t m_frameNumber = 0;
Cache m_cache;
// Descriptor heaps for the cached resource's associated views.
DirectX::DescriptorPile m_rtvHeap; // RTV
DirectX::DescriptorPile m_dsvHeap; // DSV
DirectX::DescriptorPile m_resHeap; // CBV_SRV_UAV
};
}