2019-04-24 21:35:54 +02:00
|
|
|
// Copyright 2019 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
#include <algorithm>
|
2019-04-24 21:35:54 +02:00
|
|
|
#include <unordered_map>
|
2019-05-07 16:56:45 +02:00
|
|
|
#include <vector>
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "video_core/gpu.h"
|
2019-05-07 16:56:45 +02:00
|
|
|
#include "video_core/morton.h"
|
|
|
|
#include "video_core/texture_cache/copy_params.h"
|
2019-04-24 21:35:54 +02:00
|
|
|
#include "video_core/texture_cache/surface_params.h"
|
|
|
|
#include "video_core/texture_cache/surface_view.h"
|
|
|
|
|
2019-05-07 19:58:37 +02:00
|
|
|
template <class ForwardIt, class T, class Compare = std::less<>>
|
|
|
|
ForwardIt binary_find(ForwardIt first, ForwardIt last, const T& value, Compare comp = {}) {
|
2019-05-07 16:56:45 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:35:54 +02:00
|
|
|
namespace VideoCommon {
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
using VideoCore::MortonSwizzleMode;
|
2019-05-07 19:58:37 +02:00
|
|
|
using VideoCore::Surface::SurfaceTarget;
|
|
|
|
|
|
|
|
enum class MatchStructureResult : u32 {
|
|
|
|
FullMatch = 0,
|
|
|
|
SemiMatch = 1,
|
|
|
|
None = 2,
|
|
|
|
};
|
2019-05-07 16:56:45 +02:00
|
|
|
|
2019-04-24 21:35:54 +02:00
|
|
|
class SurfaceBaseImpl {
|
|
|
|
public:
|
2019-05-07 16:56:45 +02:00
|
|
|
void LoadBuffer(Tegra::MemoryManager& memory_manager, std::vector<u8>& staging_buffer);
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
void FlushBuffer(std::vector<u8>& staging_buffer);
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
GPUVAddr GetGpuAddr() const {
|
|
|
|
return gpu_addr;
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
GPUVAddr GetGpuAddrEnd() const {
|
|
|
|
return gpu_addr_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Overlaps(const GPUVAddr start, const GPUVAddr end) const {
|
|
|
|
return (gpu_addr < end) && (gpu_addr_end > start);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use only when recycling a surface
|
|
|
|
void SetGpuAddr(const GPUVAddr new_addr) {
|
|
|
|
gpu_addr = new_addr;
|
|
|
|
gpu_addr_end = new_addr + memory_size;
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:35:54 +02:00
|
|
|
VAddr GetCpuAddr() const {
|
2019-05-07 16:56:45 +02:00
|
|
|
return gpu_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetCpuAddr(const VAddr new_addr) {
|
|
|
|
cpu_addr = new_addr;
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u8* GetHostPtr() const {
|
|
|
|
return host_ptr;
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
void SetHostPtr(u8* new_addr) {
|
|
|
|
host_ptr = new_addr;
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const SurfaceParams& GetSurfaceParams() const {
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
std::size_t GetSizeInBytes() const {
|
|
|
|
return memory_size;
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
std::size_t GetHostSizeInBytes() const {
|
|
|
|
return host_memory_size;
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
std::size_t GetMipmapSize(const u32 level) const {
|
|
|
|
return mipmap_sizes[level];
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-05-07 19:58:37 +02:00
|
|
|
MatchStructureResult MatchesStructure(const SurfaceParams& rhs) const {
|
2019-05-07 16:56:45 +02:00
|
|
|
if (params.is_tiled) {
|
2019-05-07 19:58:37 +02:00
|
|
|
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;
|
2019-05-07 16:56:45 +02:00
|
|
|
} else {
|
2019-05-07 19:58:37 +02:00
|
|
|
if (std::tie(params.width, params.height, params.pitch) ==
|
|
|
|
std::tie(rhs.width, rhs.height, rhs.pitch)) {
|
|
|
|
return MatchStructureResult::FullMatch;
|
|
|
|
}
|
|
|
|
return MatchStructureResult::None;
|
2019-05-07 16:56:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-05-07 19:58:37 +02:00
|
|
|
const auto mipmap_it =
|
|
|
|
binary_find(mipmap_offsets.begin(), mipmap_offsets.end(), mipmap_address);
|
2019-05-07 16:56:45 +02:00
|
|
|
if (mipmap_it != mipmap_offsets.end()) {
|
|
|
|
return {{layer, std::distance(mipmap_offsets.begin(), mipmap_it)}};
|
|
|
|
}
|
|
|
|
return {};
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 19:58:37 +02:00
|
|
|
std::vector<CopyParams> BreakDown(const SurfaceParams& in_params) const {
|
|
|
|
auto set_up_copy = [](CopyParams& cp, const u32 width, const u32 height, const u32 depth,
|
2019-05-07 16:56:45 +02:00
|
|
|
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;
|
2019-05-07 19:58:37 +02:00
|
|
|
cp.width = width;
|
|
|
|
cp.height = height;
|
2019-05-07 16:56:45 +02:00
|
|
|
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];
|
2019-05-07 19:58:37 +02:00
|
|
|
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);
|
2019-05-07 16:56:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
std::vector<CopyParams> result{mipmaps};
|
|
|
|
for (std::size_t level = 0; level < mipmaps; level++) {
|
|
|
|
CopyParams& cp = result[level];
|
2019-05-07 19:58:37 +02:00
|
|
|
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);
|
2019-05-07 16:56:45 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2019-05-07 16:56:45 +02:00
|
|
|
explicit SurfaceBaseImpl(const GPUVAddr gpu_vaddr, const SurfaceParams& params);
|
|
|
|
~SurfaceBaseImpl() = default;
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
virtual void DecorateSurfaceName() = 0;
|
|
|
|
|
|
|
|
const SurfaceParams params;
|
|
|
|
GPUVAddr gpu_addr{};
|
2019-05-07 16:56:45 +02:00
|
|
|
GPUVAddr gpu_addr_end{};
|
|
|
|
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;
|
|
|
|
u8* host_ptr;
|
|
|
|
VAddr cpu_addr;
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
private:
|
|
|
|
void SwizzleFunc(MortonSwizzleMode mode, u8* memory, const SurfaceParams& params, u8* buffer,
|
|
|
|
u32 level);
|
2019-04-24 21:35:54 +02:00
|
|
|
};
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
template <typename TView>
|
2019-04-24 21:35:54 +02:00
|
|
|
class SurfaceBase : public SurfaceBaseImpl {
|
|
|
|
public:
|
2019-05-07 16:56:45 +02:00
|
|
|
virtual void UploadTexture(std::vector<u8>& staging_buffer) = 0;
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
virtual void DownloadTexture(std::vector<u8>& staging_buffer) = 0;
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
void MarkAsModified(const bool is_modified_, const u64 tick) {
|
|
|
|
is_modified = is_modified_ || is_protected;
|
|
|
|
modification_tick = tick;
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
void MarkAsProtected(const bool is_protected) {
|
|
|
|
this->is_protected = is_protected;
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
void MarkAsPicked(const bool is_picked) {
|
|
|
|
this->is_picked = is_picked;
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
bool IsModified() const {
|
|
|
|
return is_modified;
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
bool IsProtected() const {
|
|
|
|
return is_protected;
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
bool IsRegistered() const {
|
|
|
|
return is_registered;
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
bool IsPicked() const {
|
|
|
|
return is_picked;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MarkAsRegistered(bool is_reg) {
|
|
|
|
is_registered = is_reg;
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 GetModificationTick() const {
|
|
|
|
return modification_tick;
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
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 {};
|
2019-05-07 19:58:37 +02:00
|
|
|
if (params.target == SurfaceTarget::Texture3D ||
|
|
|
|
view_params.target == SurfaceTarget::Texture3D) {
|
2019-05-07 16:56:45 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const std::size_t size = view_params.GetGuestSizeInBytes();
|
|
|
|
const GPUVAddr relative_address = view_addr - gpu_addr;
|
|
|
|
auto layer_mipmap = GetLayerMipmap(relative_address);
|
|
|
|
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 = params.target;
|
|
|
|
return {GetView(vp)};
|
|
|
|
}
|
|
|
|
|
|
|
|
TView GetMainView() const {
|
|
|
|
return main_view;
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:35:54 +02:00
|
|
|
protected:
|
2019-05-07 16:56:45 +02:00
|
|
|
explicit SurfaceBase(const GPUVAddr gpu_addr, const SurfaceParams& params)
|
|
|
|
: SurfaceBaseImpl(gpu_addr, params) {}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
~SurfaceBase() = default;
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
virtual TView CreateView(const ViewParams& view_key) = 0;
|
|
|
|
|
|
|
|
std::unordered_map<ViewParams, TView> views;
|
|
|
|
TView main_view;
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
private:
|
2019-05-07 16:56:45 +02:00
|
|
|
TView GetView(const ViewParams& key) {
|
2019-04-24 21:35:54 +02:00
|
|
|
const auto [entry, is_cache_miss] = views.try_emplace(key);
|
|
|
|
auto& view{entry->second};
|
|
|
|
if (is_cache_miss) {
|
|
|
|
view = CreateView(key);
|
|
|
|
}
|
2019-05-07 16:56:45 +02:00
|
|
|
return view;
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_modified{};
|
2019-05-07 16:56:45 +02:00
|
|
|
bool is_protected{};
|
|
|
|
bool is_registered{};
|
|
|
|
bool is_picked{};
|
2019-04-24 21:35:54 +02:00
|
|
|
u64 modification_tick{};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace VideoCommon
|