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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
// Copyright 2019 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <algorithm>
#include <unordered_map>
#include <vector>
#include "common/assert.h"
#include "common/common_types.h"
#include "video_core/gpu.h"
#include "video_core/morton.h"
#include "video_core/texture_cache/copy_params.h"
#include "video_core/texture_cache/surface_params.h"
#include "video_core/texture_cache/surface_view.h"
template <class ForwardIt, class T, class Compare = std::less<>>
ForwardIt binary_find(ForwardIt first, ForwardIt last, const T& value, Compare comp = {}) {
// Note: BOTH type T and the type after ForwardIt is dereferenced
// must be implicitly convertible to BOTH Type1 and Type2, used in Compare.
// This is stricter than lower_bound requirement (see above)
first = std::lower_bound(first, last, value, comp);
return first != last && !comp(value, *first) ? first : last;
}
namespace Tegra {
class MemoryManager;
}
namespace VideoCommon {
using VideoCore::MortonSwizzleMode;
using VideoCore::Surface::SurfaceTarget;
enum class MatchStructureResult : u32 {
FullMatch = 0,
SemiMatch = 1,
None = 2,
};
class SurfaceBaseImpl {
public:
void LoadBuffer(Tegra::MemoryManager& memory_manager, std::vector<u8>& staging_buffer);
void FlushBuffer(Tegra::MemoryManager& memory_manager, std::vector<u8>& staging_buffer);
GPUVAddr GetGpuAddr() const {
return gpu_addr;
}
bool Overlaps(const CacheAddr start, const CacheAddr end) const {
return (cache_addr < end) && (cache_addr_end > start);
}
// Use only when recycling a surface
void SetGpuAddr(const GPUVAddr new_addr) {
gpu_addr = new_addr;
}
VAddr GetCpuAddr() const {
return cpu_addr;
}
void SetCpuAddr(const VAddr new_addr) {
cpu_addr = new_addr;
}
CacheAddr GetCacheAddr() const {
return cache_addr;
}
CacheAddr GetCacheAddrEnd() const {
return cache_addr_end;
}
void SetCacheAddr(const CacheAddr new_addr) {
cache_addr = new_addr;
cache_addr_end = new_addr + memory_size;
}
const SurfaceParams& GetSurfaceParams() const {
return params;
}
std::size_t GetSizeInBytes() const {
return memory_size;
}
std::size_t GetHostSizeInBytes() const {
return host_memory_size;
}
std::size_t GetMipmapSize(const u32 level) const {
return mipmap_sizes[level];
}
bool MatchFormat(VideoCore::Surface::PixelFormat pixel_format) const {
return params.pixel_format == pixel_format;
}
bool MatchTarget(VideoCore::Surface::SurfaceTarget target) const {
return params.target == target;
}
bool MatchesTopology(const SurfaceParams& rhs) const {
const u32 src_bpp = params.GetBytesPerPixel();
const u32 dst_bpp = rhs.GetBytesPerPixel();
return std::tie(src_bpp, params.is_tiled) == std::tie(dst_bpp, rhs.is_tiled);
}
MatchStructureResult MatchesStructure(const SurfaceParams& rhs) const {
if (params.is_tiled) {
if (std::tie(params.height, params.depth, params.block_width, params.block_height,
params.block_depth, params.tile_width_spacing) ==
std::tie(rhs.height, rhs.depth, rhs.block_width, rhs.block_height, rhs.block_depth,
rhs.tile_width_spacing)) {
if (params.width == rhs.width) {
return MatchStructureResult::FullMatch;
}
if (params.GetBlockAlignedWidth() == rhs.GetBlockAlignedWidth()) {
return MatchStructureResult::SemiMatch;
}
}
return MatchStructureResult::None;
} else {
if (std::tie(params.width, params.height, params.pitch) ==
std::tie(rhs.width, rhs.height, rhs.pitch)) {
return MatchStructureResult::FullMatch;
}
return MatchStructureResult::None;
}
}
std::optional<std::pair<u32, u32>> GetLayerMipmap(const GPUVAddr candidate_gpu_addr) const {
if (candidate_gpu_addr < gpu_addr)
return {};
const GPUVAddr relative_address = candidate_gpu_addr - gpu_addr;
const u32 layer = relative_address / layer_size;
const GPUVAddr mipmap_address = relative_address - layer_size * layer;
const auto mipmap_it =
binary_find(mipmap_offsets.begin(), mipmap_offsets.end(), mipmap_address);
if (mipmap_it != mipmap_offsets.end()) {
return {{layer, std::distance(mipmap_offsets.begin(), mipmap_it)}};
}
return {};
}
std::vector<CopyParams> BreakDown(const SurfaceParams& in_params) const {
auto set_up_copy = [](CopyParams& cp, const u32 width, const u32 height, const u32 depth,
const u32 level) {
cp.source_x = 0;
cp.source_y = 0;
cp.source_z = 0;
cp.dest_x = 0;
cp.dest_y = 0;
cp.dest_z = 0;
cp.source_level = level;
cp.dest_level = level;
cp.width = width;
cp.height = height;
cp.depth = depth;
};
const u32 layers = params.depth;
const u32 mipmaps = params.num_levels;
if (params.is_layered) {
std::vector<CopyParams> result{layers * mipmaps};
for (std::size_t layer = 0; layer < layers; layer++) {
const u32 layer_offset = layer * mipmaps;
for (std::size_t level = 0; level < mipmaps; level++) {
CopyParams& cp = result[layer_offset + level];
const u32 width =
std::min(params.GetMipWidth(level), in_params.GetMipWidth(level));
const u32 height =
std::min(params.GetMipHeight(level), in_params.GetMipHeight(level));
set_up_copy(cp, width, height, layer, level);
}
}
return result;
} else {
std::vector<CopyParams> result{mipmaps};
for (std::size_t level = 0; level < mipmaps; level++) {
CopyParams& cp = result[level];
const u32 width = std::min(params.GetMipWidth(level), in_params.GetMipWidth(level));
const u32 height =
std::min(params.GetMipHeight(level), in_params.GetMipHeight(level));
const u32 depth = std::min(params.GetMipDepth(level), in_params.GetMipDepth(level));
set_up_copy(cp, width, height, depth, level);
}
return result;
}
}
protected:
explicit SurfaceBaseImpl(const GPUVAddr gpu_vaddr, const SurfaceParams& params);
~SurfaceBaseImpl() = default;
virtual void DecorateSurfaceName() = 0;
const SurfaceParams params;
GPUVAddr gpu_addr{};
std::vector<u32> mipmap_sizes;
std::vector<u32> mipmap_offsets;
const std::size_t layer_size;
const std::size_t memory_size;
const std::size_t host_memory_size;
CacheAddr cache_addr;
CacheAddr cache_addr_end{};
VAddr cpu_addr;
private:
void SwizzleFunc(MortonSwizzleMode mode, u8* memory, const SurfaceParams& params, u8* buffer,
u32 level);
};
template <typename TView>
class SurfaceBase : public SurfaceBaseImpl {
public:
virtual void UploadTexture(std::vector<u8>& staging_buffer) = 0;
virtual void DownloadTexture(std::vector<u8>& staging_buffer) = 0;
void MarkAsModified(const bool is_modified_, const u64 tick) {
is_modified = is_modified_ || is_protected;
modification_tick = tick;
}
void MarkAsProtected(const bool is_protected) {
this->is_protected = is_protected;
}
void MarkAsPicked(const bool is_picked) {
this->is_picked = is_picked;
}
bool IsModified() const {
return is_modified;
}
bool IsProtected() const {
return is_protected;
}
bool IsRegistered() const {
return is_registered;
}
bool IsPicked() const {
return is_picked;
}
void MarkAsRegistered(bool is_reg) {
is_registered = is_reg;
}
u64 GetModificationTick() const {
return modification_tick;
}
TView EmplaceOverview(const SurfaceParams& overview_params) {
ViewParams vp{};
vp.base_level = 0;
vp.num_levels = params.num_levels;
vp.target = overview_params.target;
if (params.is_layered && !overview_params.is_layered) {
vp.base_layer = 0;
vp.num_layers = 1;
} else {
vp.base_layer = 0;
vp.num_layers = params.depth;
}
return GetView(vp);
}
std::optional<TView> EmplaceView(const SurfaceParams& view_params, const GPUVAddr view_addr) {
if (view_addr < gpu_addr)
return {};
if (params.target == SurfaceTarget::Texture3D ||
view_params.target == SurfaceTarget::Texture3D) {
return {};
}
const std::size_t size = view_params.GetGuestSizeInBytes();
auto layer_mipmap = GetLayerMipmap(view_addr);
if (!layer_mipmap) {
return {};
}
const u32 layer = (*layer_mipmap).first;
const u32 mipmap = (*layer_mipmap).second;
if (GetMipmapSize(mipmap) != size) {
// TODO: the view may cover many mimaps, this case can still go on
return {};
}
ViewParams vp{};
vp.base_layer = layer;
vp.num_layers = 1;
vp.base_level = mipmap;
vp.num_levels = 1;
vp.target = view_params.target;
return {GetView(vp)};
}
TView GetMainView() const {
return main_view;
}
protected:
explicit SurfaceBase(const GPUVAddr gpu_addr, const SurfaceParams& params)
: SurfaceBaseImpl(gpu_addr, params) {}
~SurfaceBase() = default;
virtual TView CreateView(const ViewParams& view_key) = 0;
std::unordered_map<ViewParams, TView> views;
TView main_view;
private:
TView GetView(const ViewParams& key) {
const auto [entry, is_cache_miss] = views.try_emplace(key);
auto& view{entry->second};
if (is_cache_miss) {
view = CreateView(key);
}
return view;
}
bool is_modified{};
bool is_protected{};
bool is_registered{};
bool is_picked{};
u64 modification_tick{};
};
} // namespace VideoCommon
|