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
|
|
|
|
|
|
|
|
#include <memory>
|
2019-05-11 05:50:01 +02:00
|
|
|
#include <mutex>
|
2019-04-24 21:35:54 +02:00
|
|
|
#include <set>
|
|
|
|
#include <tuple>
|
|
|
|
#include <unordered_map>
|
2019-05-10 06:10:16 +02:00
|
|
|
#include <vector>
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
#include <boost/icl/interval_map.hpp>
|
|
|
|
#include <boost/range/iterator_range.hpp>
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_types.h"
|
2019-05-08 05:13:05 +02:00
|
|
|
#include "common/math_util.h"
|
2019-04-24 21:35:54 +02:00
|
|
|
#include "core/memory.h"
|
|
|
|
#include "video_core/engines/fermi_2d.h"
|
|
|
|
#include "video_core/engines/maxwell_3d.h"
|
|
|
|
#include "video_core/gpu.h"
|
|
|
|
#include "video_core/memory_manager.h"
|
|
|
|
#include "video_core/rasterizer_interface.h"
|
|
|
|
#include "video_core/surface.h"
|
2019-05-07 16:57:16 +02:00
|
|
|
#include "video_core/texture_cache/copy_params.h"
|
2019-04-24 21:35:54 +02:00
|
|
|
#include "video_core/texture_cache/surface_base.h"
|
|
|
|
#include "video_core/texture_cache/surface_params.h"
|
|
|
|
#include "video_core/texture_cache/surface_view.h"
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
class System;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Tegra::Texture {
|
|
|
|
struct FullTextureInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace VideoCore {
|
|
|
|
class RasterizerInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace VideoCommon {
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
using VideoCore::Surface::SurfaceTarget;
|
|
|
|
using RenderTargetConfig = Tegra::Engines::Maxwell3D::Regs::RenderTargetConfig;
|
|
|
|
|
2019-04-25 18:41:57 +02:00
|
|
|
template <typename TSurface, typename TView>
|
2019-04-24 21:35:54 +02:00
|
|
|
class TextureCache {
|
2019-05-07 16:57:16 +02:00
|
|
|
using IntervalMap = boost::icl::interval_map<CacheAddr, std::set<TSurface>>;
|
2019-04-24 21:35:54 +02:00
|
|
|
using IntervalType = typename IntervalMap::interval_type;
|
|
|
|
|
|
|
|
public:
|
2019-05-07 16:57:16 +02:00
|
|
|
void InitMemoryMananger(Tegra::MemoryManager& memory_manager) {
|
|
|
|
this->memory_manager = &memory_manager;
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:35:54 +02:00
|
|
|
void InvalidateRegion(CacheAddr addr, std::size_t size) {
|
2019-05-11 05:50:01 +02:00
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
|
2019-04-24 21:35:54 +02:00
|
|
|
for (const auto& surface : GetSurfacesInRegion(addr, size)) {
|
|
|
|
Unregister(surface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 16:32:30 +02:00
|
|
|
void FlushRegion(CacheAddr addr, std::size_t size) {
|
2019-05-11 05:50:01 +02:00
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
|
2019-05-08 16:32:30 +02:00
|
|
|
auto surfaces = GetSurfacesInRegion(addr, size);
|
|
|
|
if (surfaces.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::sort(surfaces.begin(), surfaces.end(),
|
|
|
|
[](const TSurface& a, const TSurface& b) -> bool {
|
|
|
|
return a->GetModificationTick() < b->GetModificationTick();
|
|
|
|
});
|
|
|
|
for (const auto& surface : surfaces) {
|
|
|
|
FlushSurface(surface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
TView GetTextureSurface(const Tegra::Texture::FullTextureInfo& config,
|
|
|
|
const VideoCommon::Shader::Sampler& entry) {
|
2019-04-24 21:35:54 +02:00
|
|
|
const auto gpu_addr{config.tic.Address()};
|
|
|
|
if (!gpu_addr) {
|
2019-04-25 18:41:57 +02:00
|
|
|
return {};
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
2019-05-07 16:57:16 +02:00
|
|
|
const auto params{SurfaceParams::CreateForTexture(system, config, entry)};
|
|
|
|
return GetSurface(gpu_addr, params, true).second;
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
TView GetDepthBufferSurface(bool preserve_contents) {
|
2019-05-11 04:26:46 +02:00
|
|
|
auto& maxwell3d = system.GPU().Maxwell3D();
|
|
|
|
|
|
|
|
if (!maxwell3d.dirty_flags.zeta_buffer) {
|
|
|
|
return depth_buffer.view;
|
|
|
|
}
|
|
|
|
maxwell3d.dirty_flags.zeta_buffer = false;
|
|
|
|
|
|
|
|
const auto& regs{maxwell3d.regs};
|
2019-04-24 21:35:54 +02:00
|
|
|
const auto gpu_addr{regs.zeta.Address()};
|
|
|
|
if (!gpu_addr || !regs.zeta_enable) {
|
2019-05-11 04:26:46 +02:00
|
|
|
SetEmptyDepthBuffer();
|
2019-04-25 18:41:57 +02:00
|
|
|
return {};
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
const auto depth_params{SurfaceParams::CreateForDepthBuffer(
|
|
|
|
system, regs.zeta_width, regs.zeta_height, regs.zeta.format,
|
|
|
|
regs.zeta.memory_layout.block_width, regs.zeta.memory_layout.block_height,
|
|
|
|
regs.zeta.memory_layout.block_depth, regs.zeta.memory_layout.type)};
|
2019-05-07 16:57:16 +02:00
|
|
|
auto surface_view = GetSurface(gpu_addr, depth_params, preserve_contents);
|
|
|
|
if (depth_buffer.target)
|
2019-05-11 05:42:08 +02:00
|
|
|
depth_buffer.target->MarkAsRenderTarget(false);
|
2019-05-11 04:26:46 +02:00
|
|
|
depth_buffer.target = surface_view.first;
|
|
|
|
depth_buffer.view = surface_view.second;
|
2019-05-07 16:57:16 +02:00
|
|
|
if (depth_buffer.target)
|
2019-05-11 05:42:08 +02:00
|
|
|
depth_buffer.target->MarkAsRenderTarget(true);
|
2019-05-07 16:57:16 +02:00
|
|
|
return surface_view.second;
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
TView GetColorBufferSurface(std::size_t index, bool preserve_contents) {
|
2019-04-24 21:35:54 +02:00
|
|
|
ASSERT(index < Tegra::Engines::Maxwell3D::Regs::NumRenderTargets);
|
2019-05-11 04:26:46 +02:00
|
|
|
auto& maxwell3d = system.GPU().Maxwell3D();
|
|
|
|
if (!maxwell3d.dirty_flags.color_buffer[index]) {
|
|
|
|
return render_targets[index].view;
|
|
|
|
}
|
|
|
|
maxwell3d.dirty_flags.color_buffer.reset(index);
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-11 04:26:46 +02:00
|
|
|
const auto& regs{maxwell3d.regs};
|
2019-04-24 21:35:54 +02:00
|
|
|
if (index >= regs.rt_control.count || regs.rt[index].Address() == 0 ||
|
|
|
|
regs.rt[index].format == Tegra::RenderTargetFormat::NONE) {
|
2019-05-07 16:57:16 +02:00
|
|
|
SetEmptyColorBuffer(index);
|
2019-04-25 18:41:57 +02:00
|
|
|
return {};
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
const auto& config{regs.rt[index]};
|
|
|
|
const auto gpu_addr{config.Address()};
|
2019-04-24 21:35:54 +02:00
|
|
|
if (!gpu_addr) {
|
2019-05-07 16:57:16 +02:00
|
|
|
SetEmptyColorBuffer(index);
|
2019-04-25 18:41:57 +02:00
|
|
|
return {};
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
auto surface_view = GetSurface(gpu_addr, SurfaceParams::CreateForFramebuffer(system, index),
|
|
|
|
preserve_contents);
|
|
|
|
if (render_targets[index].target)
|
2019-05-11 05:42:08 +02:00
|
|
|
render_targets[index].target->MarkAsRenderTarget(false);
|
2019-05-07 16:57:16 +02:00
|
|
|
render_targets[index].target = surface_view.first;
|
2019-05-11 04:26:46 +02:00
|
|
|
render_targets[index].view = surface_view.second;
|
2019-05-07 16:57:16 +02:00
|
|
|
if (render_targets[index].target)
|
2019-05-11 05:42:08 +02:00
|
|
|
render_targets[index].target->MarkAsRenderTarget(true);
|
2019-05-07 16:57:16 +02:00
|
|
|
return surface_view.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MarkColorBufferInUse(std::size_t index) {
|
|
|
|
if (render_targets[index].target)
|
|
|
|
render_targets[index].target->MarkAsModified(true, Tick());
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
void MarkDepthBufferInUse() {
|
|
|
|
if (depth_buffer.target)
|
|
|
|
depth_buffer.target->MarkAsModified(true, Tick());
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
void SetEmptyDepthBuffer() {
|
|
|
|
if (depth_buffer.target != nullptr) {
|
2019-05-11 05:42:08 +02:00
|
|
|
depth_buffer.target->MarkAsRenderTarget(false);
|
2019-05-07 16:57:16 +02:00
|
|
|
depth_buffer.target = nullptr;
|
|
|
|
depth_buffer.view = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetEmptyColorBuffer(std::size_t index) {
|
|
|
|
if (render_targets[index].target != nullptr) {
|
2019-05-11 05:42:08 +02:00
|
|
|
render_targets[index].target->MarkAsRenderTarget(false);
|
2019-05-07 16:57:16 +02:00
|
|
|
render_targets[index].target = nullptr;
|
|
|
|
render_targets[index].view = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 05:13:05 +02:00
|
|
|
void DoFermiCopy(const Tegra::Engines::Fermi2D::Regs::Surface& src_config,
|
|
|
|
const Tegra::Engines::Fermi2D::Regs::Surface& dst_config,
|
|
|
|
const Common::Rectangle<u32>& src_rect,
|
|
|
|
const Common::Rectangle<u32>& dst_rect) {
|
2019-05-09 00:27:29 +02:00
|
|
|
TSurface dst_surface = GetFermiSurface(dst_config);
|
|
|
|
ImageBlit(GetFermiSurface(src_config), dst_surface, src_rect, dst_rect);
|
|
|
|
dst_surface->MarkAsModified(true, Tick());
|
2019-05-07 16:57:16 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 23:30:36 +02:00
|
|
|
TSurface TryFindFramebufferSurface(const u8* host_ptr) {
|
|
|
|
const CacheAddr cache_addr = ToCacheAddr(host_ptr);
|
|
|
|
if (!cache_addr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const CacheAddr page = cache_addr >> registry_page_bits;
|
2019-05-10 06:10:16 +02:00
|
|
|
std::vector<TSurface>& list = registry[page];
|
2019-05-07 23:30:36 +02:00
|
|
|
for (auto& s : list) {
|
|
|
|
if (s->GetCacheAddr() == cache_addr) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 Tick() {
|
|
|
|
return ++ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
TextureCache(Core::System& system, VideoCore::RasterizerInterface& rasterizer)
|
2019-05-07 16:57:16 +02:00
|
|
|
: system{system}, rasterizer{rasterizer} {
|
|
|
|
for (std::size_t i = 0; i < Tegra::Engines::Maxwell3D::Regs::NumRenderTargets; i++) {
|
|
|
|
SetEmptyColorBuffer(i);
|
|
|
|
}
|
|
|
|
SetEmptyDepthBuffer();
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
~TextureCache() = default;
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
virtual TSurface CreateSurface(GPUVAddr gpu_addr, const SurfaceParams& params) = 0;
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
virtual void ImageCopy(TSurface src_surface, TSurface dst_surface,
|
|
|
|
const CopyParams& copy_params) = 0;
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-08 05:13:05 +02:00
|
|
|
virtual void ImageBlit(TSurface src, TSurface dst, const Common::Rectangle<u32>& src_rect,
|
|
|
|
const Common::Rectangle<u32>& dst_rect) = 0;
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
void Register(TSurface surface) {
|
2019-05-11 05:50:01 +02:00
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
const GPUVAddr gpu_addr = surface->GetGpuAddr();
|
2019-05-07 23:30:36 +02:00
|
|
|
const CacheAddr cache_ptr = ToCacheAddr(memory_manager->GetPointer(gpu_addr));
|
2019-05-07 16:57:16 +02:00
|
|
|
const std::size_t size = surface->GetSizeInBytes();
|
|
|
|
const std::optional<VAddr> cpu_addr = memory_manager->GpuToCpuAddress(gpu_addr);
|
2019-05-07 23:30:36 +02:00
|
|
|
if (!cache_ptr || !cpu_addr) {
|
2019-05-07 16:57:16 +02:00
|
|
|
LOG_CRITICAL(HW_GPU, "Failed to register surface with unmapped gpu_address 0x{:016x}",
|
|
|
|
gpu_addr);
|
|
|
|
return;
|
|
|
|
}
|
2019-05-07 23:30:36 +02:00
|
|
|
surface->SetCacheAddr(cache_ptr);
|
2019-05-07 16:57:16 +02:00
|
|
|
surface->SetCpuAddr(*cpu_addr);
|
|
|
|
RegisterInnerCache(surface);
|
|
|
|
surface->MarkAsRegistered(true);
|
2019-05-07 23:30:36 +02:00
|
|
|
rasterizer.UpdatePagesCachedCount(*cpu_addr, size, 1);
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-11 05:42:08 +02:00
|
|
|
void Unregister(TSurface surface) {
|
2019-05-11 05:50:01 +02:00
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
|
2019-05-11 05:42:08 +02:00
|
|
|
if (surface->IsProtected()) {
|
2019-05-07 16:57:16 +02:00
|
|
|
return;
|
2019-05-08 23:45:59 +02:00
|
|
|
}
|
2019-05-07 16:57:16 +02:00
|
|
|
const GPUVAddr gpu_addr = surface->GetGpuAddr();
|
2019-05-07 23:30:36 +02:00
|
|
|
const CacheAddr cache_ptr = surface->GetCacheAddr();
|
2019-05-07 16:57:16 +02:00
|
|
|
const std::size_t size = surface->GetSizeInBytes();
|
|
|
|
const VAddr cpu_addr = surface->GetCpuAddr();
|
|
|
|
rasterizer.UpdatePagesCachedCount(cpu_addr, size, -1);
|
|
|
|
UnregisterInnerCache(surface);
|
|
|
|
surface->MarkAsRegistered(false);
|
|
|
|
ReserveSurface(surface->GetSurfaceParams(), surface);
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
TSurface GetUncachedSurface(const GPUVAddr gpu_addr, const SurfaceParams& params) {
|
|
|
|
if (const auto surface = TryGetReservedSurface(params); surface) {
|
|
|
|
surface->SetGpuAddr(gpu_addr);
|
2019-04-24 21:35:54 +02:00
|
|
|
return surface;
|
2019-05-07 16:57:16 +02:00
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
// No reserved surface available, create a new one and reserve it
|
2019-05-07 16:57:16 +02:00
|
|
|
auto new_surface{CreateSurface(gpu_addr, params)};
|
2019-04-24 21:35:54 +02:00
|
|
|
return new_surface;
|
|
|
|
}
|
|
|
|
|
2019-05-08 05:13:05 +02:00
|
|
|
TSurface GetFermiSurface(const Tegra::Engines::Fermi2D::Regs::Surface& config) {
|
|
|
|
SurfaceParams params = SurfaceParams::CreateForFermiCopySurface(config);
|
|
|
|
const GPUVAddr gpu_addr = config.Address();
|
|
|
|
return GetSurface(gpu_addr, params, true).first;
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:35:54 +02:00
|
|
|
Core::System& system;
|
|
|
|
|
|
|
|
private:
|
2019-05-07 16:57:16 +02:00
|
|
|
enum class RecycleStrategy : u32 {
|
|
|
|
Ignore = 0,
|
|
|
|
Flush = 1,
|
|
|
|
BufferCopy = 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
RecycleStrategy PickStrategy(std::vector<TSurface>& overlaps, const SurfaceParams& params,
|
|
|
|
const GPUVAddr gpu_addr, const bool untopological) {
|
|
|
|
// 3D Textures decision
|
|
|
|
if (params.block_depth > 1 || params.target == SurfaceTarget::Texture3D) {
|
|
|
|
return RecycleStrategy::Flush;
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
2019-05-07 16:57:16 +02:00
|
|
|
for (auto s : overlaps) {
|
|
|
|
const auto& s_params = s->GetSurfaceParams();
|
|
|
|
if (s_params.block_depth > 1 || s_params.target == SurfaceTarget::Texture3D) {
|
|
|
|
return RecycleStrategy::Flush;
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 00:27:29 +02:00
|
|
|
// Untopological decision
|
|
|
|
if (untopological) {
|
|
|
|
return RecycleStrategy::Ignore;
|
|
|
|
}
|
2019-05-07 16:57:16 +02:00
|
|
|
return RecycleStrategy::Ignore;
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
std::pair<TSurface, TView> RecycleSurface(std::vector<TSurface>& overlaps,
|
|
|
|
const SurfaceParams& params, const GPUVAddr gpu_addr,
|
2019-05-09 00:27:29 +02:00
|
|
|
const bool preserve_contents,
|
2019-05-07 16:57:16 +02:00
|
|
|
const bool untopological) {
|
|
|
|
for (auto surface : overlaps) {
|
|
|
|
Unregister(surface);
|
|
|
|
}
|
|
|
|
RecycleStrategy strategy = !Settings::values.use_accurate_gpu_emulation
|
|
|
|
? PickStrategy(overlaps, params, gpu_addr, untopological)
|
|
|
|
: RecycleStrategy::Flush;
|
|
|
|
switch (strategy) {
|
|
|
|
case RecycleStrategy::Ignore: {
|
|
|
|
return InitializeSurface(gpu_addr, params, preserve_contents);
|
|
|
|
}
|
|
|
|
case RecycleStrategy::Flush: {
|
|
|
|
std::sort(overlaps.begin(), overlaps.end(),
|
|
|
|
[](const TSurface& a, const TSurface& b) -> bool {
|
|
|
|
return a->GetModificationTick() < b->GetModificationTick();
|
|
|
|
});
|
|
|
|
for (auto surface : overlaps) {
|
|
|
|
FlushSurface(surface);
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
2019-05-07 16:57:16 +02:00
|
|
|
return InitializeSurface(gpu_addr, params, preserve_contents);
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
2019-05-07 16:57:16 +02:00
|
|
|
default: {
|
|
|
|
UNIMPLEMENTED_MSG("Unimplemented Texture Cache Recycling Strategy!");
|
|
|
|
return InitializeSurface(gpu_addr, params, preserve_contents);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 23:30:36 +02:00
|
|
|
std::pair<TSurface, TView> RebuildSurface(TSurface current_surface,
|
2019-05-08 02:28:31 +02:00
|
|
|
const SurfaceParams& params) {
|
2019-05-07 16:57:16 +02:00
|
|
|
const auto gpu_addr = current_surface->GetGpuAddr();
|
|
|
|
TSurface new_surface = GetUncachedSurface(gpu_addr, params);
|
2019-05-07 19:58:37 +02:00
|
|
|
std::vector<CopyParams> bricks = current_surface->BreakDown(params);
|
2019-05-07 16:57:16 +02:00
|
|
|
for (auto& brick : bricks) {
|
|
|
|
ImageCopy(current_surface, new_surface, brick);
|
|
|
|
}
|
|
|
|
Unregister(current_surface);
|
|
|
|
Register(new_surface);
|
2019-05-09 00:27:29 +02:00
|
|
|
new_surface->MarkAsModified(current_surface->IsModified(), Tick());
|
2019-05-07 16:57:16 +02:00
|
|
|
return {new_surface, new_surface->GetMainView()};
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
std::pair<TSurface, TView> ManageStructuralMatch(TSurface current_surface,
|
|
|
|
const SurfaceParams& params) {
|
|
|
|
const bool is_mirage = !current_surface->MatchFormat(params.pixel_format);
|
|
|
|
if (is_mirage) {
|
2019-05-07 23:30:36 +02:00
|
|
|
return RebuildSurface(current_surface, params);
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
2019-05-07 16:57:16 +02:00
|
|
|
const bool matches_target = current_surface->MatchTarget(params.target);
|
|
|
|
if (matches_target) {
|
|
|
|
return {current_surface, current_surface->GetMainView()};
|
|
|
|
}
|
|
|
|
return {current_surface, current_surface->EmplaceOverview(params)};
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
std::optional<std::pair<TSurface, TView>> ReconstructSurface(std::vector<TSurface>& overlaps,
|
|
|
|
const SurfaceParams& params,
|
|
|
|
const GPUVAddr gpu_addr,
|
|
|
|
const u8* host_ptr) {
|
2019-05-08 13:09:02 +02:00
|
|
|
if (params.target == SurfaceTarget::Texture3D) {
|
2019-05-07 16:57:16 +02:00
|
|
|
return {};
|
|
|
|
}
|
2019-05-09 00:27:29 +02:00
|
|
|
bool modified = false;
|
2019-05-07 16:57:16 +02:00
|
|
|
TSurface new_surface = GetUncachedSurface(gpu_addr, params);
|
|
|
|
for (auto surface : overlaps) {
|
|
|
|
const SurfaceParams& src_params = surface->GetSurfaceParams();
|
|
|
|
if (src_params.is_layered || src_params.num_levels > 1) {
|
|
|
|
// We send this cases to recycle as they are more complex to handle
|
|
|
|
return {};
|
|
|
|
}
|
2019-05-09 00:27:29 +02:00
|
|
|
const std::size_t candidate_size = surface->GetSizeInBytes();
|
2019-05-08 02:28:31 +02:00
|
|
|
auto mipmap_layer{new_surface->GetLayerMipmap(surface->GetGpuAddr())};
|
2019-05-07 16:57:16 +02:00
|
|
|
if (!mipmap_layer) {
|
|
|
|
return {};
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
2019-05-08 02:28:31 +02:00
|
|
|
const u32 layer{mipmap_layer->first};
|
|
|
|
const u32 mipmap{mipmap_layer->second};
|
2019-05-07 16:57:16 +02:00
|
|
|
if (new_surface->GetMipmapSize(mipmap) != candidate_size) {
|
|
|
|
return {};
|
|
|
|
}
|
2019-05-09 00:27:29 +02:00
|
|
|
modified |= surface->IsModified();
|
2019-05-07 16:57:16 +02:00
|
|
|
// Now we got all the data set up
|
2019-05-08 02:28:31 +02:00
|
|
|
const u32 dst_width{params.GetMipWidth(mipmap)};
|
|
|
|
const u32 dst_height{params.GetMipHeight(mipmap)};
|
|
|
|
const CopyParams copy_params(0, 0, 0, 0, 0, layer, 0, mipmap,
|
|
|
|
std::min(src_params.width, dst_width),
|
|
|
|
std::min(src_params.height, dst_height), 1);
|
2019-05-07 16:57:16 +02:00
|
|
|
ImageCopy(surface, new_surface, copy_params);
|
|
|
|
}
|
|
|
|
for (auto surface : overlaps) {
|
2019-05-11 05:42:08 +02:00
|
|
|
Unregister(surface);
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
2019-05-09 00:27:29 +02:00
|
|
|
new_surface->MarkAsModified(modified, Tick());
|
2019-05-07 16:57:16 +02:00
|
|
|
Register(new_surface);
|
|
|
|
return {{new_surface, new_surface->GetMainView()}};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<TSurface, TView> GetSurface(const GPUVAddr gpu_addr, const SurfaceParams& params,
|
|
|
|
bool preserve_contents) {
|
|
|
|
|
|
|
|
const auto host_ptr{memory_manager->GetPointer(gpu_addr)};
|
|
|
|
const auto cache_addr{ToCacheAddr(host_ptr)};
|
2019-05-10 23:59:18 +02:00
|
|
|
|
|
|
|
if (l1_cache.count(cache_addr) > 0) {
|
|
|
|
TSurface current_surface = l1_cache[cache_addr];
|
|
|
|
if (!current_surface->MatchesTopology(params)) {
|
|
|
|
std::vector<TSurface> overlaps{current_surface};
|
|
|
|
return RecycleSurface(overlaps, params, gpu_addr, preserve_contents, true);
|
|
|
|
}
|
|
|
|
MatchStructureResult s_result = current_surface->MatchesStructure(params);
|
|
|
|
if (s_result != MatchStructureResult::None &&
|
|
|
|
current_surface->GetGpuAddr() == gpu_addr &&
|
|
|
|
(params.target != SurfaceTarget::Texture3D ||
|
|
|
|
current_surface->MatchTarget(params.target))) {
|
|
|
|
if (s_result == MatchStructureResult::FullMatch) {
|
|
|
|
return ManageStructuralMatch(current_surface, params);
|
|
|
|
} else {
|
|
|
|
return RebuildSurface(current_surface, params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
const std::size_t candidate_size = params.GetGuestSizeInBytes();
|
2019-05-07 23:30:36 +02:00
|
|
|
auto overlaps{GetSurfacesInRegion(cache_addr, candidate_size)};
|
2019-05-08 23:45:59 +02:00
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
if (overlaps.empty()) {
|
|
|
|
return InitializeSurface(gpu_addr, params, preserve_contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto surface : overlaps) {
|
|
|
|
if (!surface->MatchesTopology(params)) {
|
2019-05-09 00:27:29 +02:00
|
|
|
return RecycleSurface(overlaps, params, gpu_addr, preserve_contents, true);
|
2019-05-07 16:57:16 +02:00
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
if (overlaps.size() == 1) {
|
|
|
|
TSurface current_surface = overlaps[0];
|
2019-05-08 23:45:59 +02:00
|
|
|
if (!current_surface->IsInside(gpu_addr, gpu_addr + candidate_size)) {
|
2019-05-09 00:27:29 +02:00
|
|
|
return RecycleSurface(overlaps, params, gpu_addr, preserve_contents, false);
|
2019-05-07 16:57:16 +02:00
|
|
|
}
|
2019-05-09 00:27:29 +02:00
|
|
|
std::optional<TView> view =
|
|
|
|
current_surface->EmplaceView(params, gpu_addr, candidate_size);
|
2019-05-07 16:57:16 +02:00
|
|
|
if (view.has_value()) {
|
|
|
|
const bool is_mirage = !current_surface->MatchFormat(params.pixel_format);
|
|
|
|
if (is_mirage) {
|
|
|
|
LOG_CRITICAL(HW_GPU, "Mirage View Unsupported");
|
2019-05-09 00:27:29 +02:00
|
|
|
return RecycleSurface(overlaps, params, gpu_addr, preserve_contents, false);
|
2019-05-07 16:57:16 +02:00
|
|
|
}
|
|
|
|
return {current_surface, *view};
|
|
|
|
}
|
2019-05-09 00:27:29 +02:00
|
|
|
return RecycleSurface(overlaps, params, gpu_addr, preserve_contents, false);
|
2019-05-07 16:57:16 +02:00
|
|
|
} else {
|
|
|
|
std::optional<std::pair<TSurface, TView>> view =
|
|
|
|
ReconstructSurface(overlaps, params, gpu_addr, host_ptr);
|
|
|
|
if (view.has_value()) {
|
|
|
|
return *view;
|
|
|
|
}
|
2019-05-09 00:27:29 +02:00
|
|
|
return RecycleSurface(overlaps, params, gpu_addr, preserve_contents, false);
|
2019-05-07 16:57:16 +02:00
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
std::pair<TSurface, TView> InitializeSurface(GPUVAddr gpu_addr, const SurfaceParams& params,
|
|
|
|
bool preserve_contents) {
|
|
|
|
auto new_surface{GetUncachedSurface(gpu_addr, params)};
|
|
|
|
Register(new_surface);
|
2019-04-24 21:35:54 +02:00
|
|
|
if (preserve_contents) {
|
2019-04-25 18:41:57 +02:00
|
|
|
LoadSurface(new_surface);
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
2019-05-07 16:57:16 +02:00
|
|
|
return {new_surface, new_surface->GetMainView()};
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
void LoadSurface(const TSurface& surface) {
|
|
|
|
staging_buffer.resize(surface->GetHostSizeInBytes());
|
|
|
|
surface->LoadBuffer(*memory_manager, staging_buffer);
|
|
|
|
surface->UploadTexture(staging_buffer);
|
|
|
|
surface->MarkAsModified(false, Tick());
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
void FlushSurface(const TSurface& surface) {
|
2019-04-24 21:35:54 +02:00
|
|
|
if (!surface->IsModified()) {
|
2019-04-25 18:41:57 +02:00
|
|
|
return;
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
2019-05-07 16:57:16 +02:00
|
|
|
staging_buffer.resize(surface->GetHostSizeInBytes());
|
|
|
|
surface->DownloadTexture(staging_buffer);
|
2019-05-07 23:30:36 +02:00
|
|
|
surface->FlushBuffer(*memory_manager, staging_buffer);
|
2019-05-07 16:57:16 +02:00
|
|
|
surface->MarkAsModified(false, Tick());
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
void RegisterInnerCache(TSurface& surface) {
|
2019-05-10 23:59:18 +02:00
|
|
|
const CacheAddr cache_addr = surface->GetCacheAddr();
|
|
|
|
CacheAddr start = cache_addr >> registry_page_bits;
|
2019-05-07 23:30:36 +02:00
|
|
|
const CacheAddr end = (surface->GetCacheAddrEnd() - 1) >> registry_page_bits;
|
2019-05-10 23:59:18 +02:00
|
|
|
l1_cache[cache_addr] = surface;
|
2019-05-07 16:57:16 +02:00
|
|
|
while (start <= end) {
|
2019-05-07 23:30:36 +02:00
|
|
|
registry[start].push_back(surface);
|
2019-05-07 16:57:16 +02:00
|
|
|
start++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnregisterInnerCache(TSurface& surface) {
|
2019-05-10 23:59:18 +02:00
|
|
|
const CacheAddr cache_addr = surface->GetCacheAddr();
|
|
|
|
CacheAddr start = cache_addr >> registry_page_bits;
|
2019-05-07 23:30:36 +02:00
|
|
|
const CacheAddr end = (surface->GetCacheAddrEnd() - 1) >> registry_page_bits;
|
2019-05-10 23:59:18 +02:00
|
|
|
l1_cache.erase(cache_addr);
|
2019-05-07 16:57:16 +02:00
|
|
|
while (start <= end) {
|
2019-05-10 06:10:16 +02:00
|
|
|
auto& reg{registry[start]};
|
|
|
|
reg.erase(std::find(reg.begin(), reg.end(), surface));
|
2019-05-07 16:57:16 +02:00
|
|
|
start++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 23:30:36 +02:00
|
|
|
std::vector<TSurface> GetSurfacesInRegion(const CacheAddr cache_addr, const std::size_t size) {
|
2019-05-07 16:57:16 +02:00
|
|
|
if (size == 0) {
|
|
|
|
return {};
|
|
|
|
}
|
2019-05-07 23:30:36 +02:00
|
|
|
const CacheAddr cache_addr_end = cache_addr + size;
|
|
|
|
CacheAddr start = cache_addr >> registry_page_bits;
|
|
|
|
const CacheAddr end = (cache_addr_end - 1) >> registry_page_bits;
|
2019-05-07 16:57:16 +02:00
|
|
|
std::vector<TSurface> surfaces;
|
|
|
|
while (start <= end) {
|
2019-05-10 06:10:16 +02:00
|
|
|
std::vector<TSurface>& list = registry[start];
|
2019-05-07 16:57:16 +02:00
|
|
|
for (auto& s : list) {
|
2019-05-07 23:30:36 +02:00
|
|
|
if (!s->IsPicked() && s->Overlaps(cache_addr, cache_addr_end)) {
|
2019-05-07 16:57:16 +02:00
|
|
|
s->MarkAsPicked(true);
|
|
|
|
surfaces.push_back(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
start++;
|
|
|
|
}
|
|
|
|
for (auto& s : surfaces) {
|
|
|
|
s->MarkAsPicked(false);
|
|
|
|
}
|
|
|
|
return surfaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReserveSurface(const SurfaceParams& params, TSurface surface) {
|
2019-04-24 21:35:54 +02:00
|
|
|
surface_reserve[params].push_back(std::move(surface));
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:57:16 +02:00
|
|
|
TSurface TryGetReservedSurface(const SurfaceParams& params) {
|
2019-04-24 21:35:54 +02:00
|
|
|
auto search{surface_reserve.find(params)};
|
|
|
|
if (search == surface_reserve.end()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
for (auto& surface : search->second) {
|
|
|
|
if (!surface->IsRegistered()) {
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-05-11 04:26:46 +02:00
|
|
|
struct FramebufferTargetInfo {
|
2019-05-07 16:57:16 +02:00
|
|
|
TSurface target;
|
|
|
|
TView view;
|
|
|
|
};
|
|
|
|
|
2019-04-24 21:35:54 +02:00
|
|
|
VideoCore::RasterizerInterface& rasterizer;
|
2019-05-07 16:57:16 +02:00
|
|
|
Tegra::MemoryManager* memory_manager;
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
u64 ticks{};
|
|
|
|
|
2019-05-07 23:30:36 +02:00
|
|
|
// The internal Cache is different for the Texture Cache. It's based on buckets
|
|
|
|
// of 1MB. This fits better for the purpose of this cache as textures are normaly
|
|
|
|
// large in size.
|
|
|
|
static constexpr u64 registry_page_bits{20};
|
|
|
|
static constexpr u64 registry_page_size{1 << registry_page_bits};
|
2019-05-10 06:10:16 +02:00
|
|
|
std::unordered_map<CacheAddr, std::vector<TSurface>> registry;
|
2019-05-07 16:57:16 +02:00
|
|
|
|
2019-05-10 23:59:18 +02:00
|
|
|
// The L1 Cache is used for fast texture lookup before checking the overlaps
|
|
|
|
// This avoids calculating size and other stuffs.
|
|
|
|
std::unordered_map<CacheAddr, TSurface> l1_cache;
|
|
|
|
|
2019-04-24 21:35:54 +02:00
|
|
|
/// The surface reserve is a "backup" cache, this is where we put unique surfaces that have
|
|
|
|
/// previously been used. This is to prevent surfaces from being constantly created and
|
|
|
|
/// destroyed when used with different surface parameters.
|
2019-05-10 06:10:16 +02:00
|
|
|
std::unordered_map<SurfaceParams, std::vector<TSurface>> surface_reserve;
|
2019-05-11 04:26:46 +02:00
|
|
|
std::array<FramebufferTargetInfo, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets>
|
|
|
|
render_targets;
|
|
|
|
FramebufferTargetInfo depth_buffer;
|
2019-05-07 16:57:16 +02:00
|
|
|
|
|
|
|
std::vector<u8> staging_buffer;
|
2019-05-11 05:50:01 +02:00
|
|
|
std::recursive_mutex mutex;
|
2019-04-24 21:35:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace VideoCommon
|