-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageGpuResourceCache.cpp
More file actions
160 lines (144 loc) · 4.09 KB
/
Copy pathImageGpuResourceCache.cpp
File metadata and controls
160 lines (144 loc) · 4.09 KB
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
#include "ImageGpuResourceCache.h"
#include <algorithm>
ImageGpuResourceCache& ImageGpuResourceCache::Instance()
{
static ImageGpuResourceCache instance;
return instance;
}
void ImageGpuResourceCache::ClearUnlocked()
{
m_cache.clear();
m_lru.clear();
m_bytesInUse = 0;
}
size_t ImageGpuResourceCache::EstimateBytes(UINT w, UINT h, DXGI_FORMAT format)
{
auto blocks = [](UINT x) -> size_t
{
return (static_cast<size_t>(x) + 3) / 4;
};
switch (format)
{
case DXGI_FORMAT_BC1_TYPELESS:
case DXGI_FORMAT_BC1_UNORM:
case DXGI_FORMAT_BC1_UNORM_SRGB:
case DXGI_FORMAT_BC4_TYPELESS:
case DXGI_FORMAT_BC4_UNORM:
case DXGI_FORMAT_BC4_SNORM:
return blocks(w) * blocks(h) * 8;
case DXGI_FORMAT_BC2_TYPELESS:
case DXGI_FORMAT_BC2_UNORM:
case DXGI_FORMAT_BC2_UNORM_SRGB:
case DXGI_FORMAT_BC3_TYPELESS:
case DXGI_FORMAT_BC3_UNORM:
case DXGI_FORMAT_BC3_UNORM_SRGB:
case DXGI_FORMAT_BC5_TYPELESS:
case DXGI_FORMAT_BC5_UNORM:
case DXGI_FORMAT_BC5_SNORM:
case DXGI_FORMAT_BC6H_TYPELESS:
case DXGI_FORMAT_BC6H_UF16:
case DXGI_FORMAT_BC6H_SF16:
case DXGI_FORMAT_BC7_TYPELESS:
case DXGI_FORMAT_BC7_UNORM:
case DXGI_FORMAT_BC7_UNORM_SRGB:
return blocks(w) * blocks(h) * 16;
default:
return static_cast<size_t>(w) * static_cast<size_t>(h) * 4;
}
}
void ImageGpuResourceCache::EnsureDeviceGenerationUnlocked(uint64_t deviceGeneration)
{
if (m_deviceGeneration != deviceGeneration)
{
ClearUnlocked();
m_deviceGeneration = deviceGeneration;
}
}
void ImageGpuResourceCache::Clear()
{
std::lock_guard<std::mutex> lock(m_mutex);
ClearUnlocked();
}
bool ImageGpuResourceCache::TryGet(
const std::wstring& path,
uint32_t mipLevel,
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>& outSrv,
UINT& outW,
UINT& outH,
DXGI_FORMAT& outFmt,
ImageAlphaInfo& outAlpha,
uint32_t& outSourceMipLevels,
uint32_t& outSourceMipIndex,
uint64_t deviceGeneration)
{
std::lock_guard<std::mutex> lock(m_mutex);
EnsureDeviceGenerationUnlocked(deviceGeneration);
const Key key { path, mipLevel };
auto it = m_cache.find(key);
if (it == m_cache.end() || !it->second.srv)
{
return false;
}
m_lru.erase(it->second.lruIt);
m_lru.push_front(key);
it->second.lruIt = m_lru.begin();
outSrv = it->second.srv;
outW = it->second.width;
outH = it->second.height;
outFmt = it->second.format;
outAlpha = it->second.alpha;
outSourceMipLevels = it->second.sourceMipLevels;
outSourceMipIndex = it->second.sourceMipIndex;
return true;
}
void ImageGpuResourceCache::Put(
const std::wstring& path,
uint32_t mipLevel,
const Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>& srv,
UINT w,
UINT h,
DXGI_FORMAT format,
const ImageAlphaInfo& alpha,
uint32_t sourceMipLevels,
uint32_t sourceMipIndex,
uint64_t deviceGeneration)
{
if (!srv)
{
return;
}
std::lock_guard<std::mutex> lock(m_mutex);
EnsureDeviceGenerationUnlocked(deviceGeneration);
const Key key { path, mipLevel };
if (auto it = m_cache.find(key); it != m_cache.end())
{
m_bytesInUse -= it->second.bytes;
m_lru.erase(it->second.lruIt);
m_cache.erase(it);
}
m_lru.push_front(key);
Entry entry {};
entry.srv = srv;
entry.width = w;
entry.height = h;
entry.format = format;
entry.alpha = alpha;
entry.sourceMipLevels = (std::max)(1u, sourceMipLevels);
entry.sourceMipIndex = sourceMipIndex;
entry.bytes = EstimateBytes(w, h, format);
entry.lruIt = m_lru.begin();
m_bytesInUse += entry.bytes;
m_cache.emplace(key, std::move(entry));
while (m_cache.size() > 1 &&
(m_cache.size() > m_capacity || m_bytesInUse > m_byteBudget) &&
!m_lru.empty())
{
const Key victimKey = m_lru.back();
m_lru.pop_back();
if (auto vit = m_cache.find(victimKey); vit != m_cache.end())
{
m_bytesInUse -= vit->second.bytes;
m_cache.erase(vit);
}
}
}