2018-06-22 01:36:01 +02:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
2018-03-19 22:45:22 +01:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <array>
|
2018-06-22 01:36:01 +02:00
|
|
|
#include <map>
|
2018-03-19 22:45:22 +01:00
|
|
|
#include <memory>
|
2018-10-29 02:14:25 +01:00
|
|
|
#include <string>
|
2019-02-09 00:28:01 +01:00
|
|
|
#include <unordered_set>
|
2018-06-26 21:05:13 +02:00
|
|
|
#include <vector>
|
2018-08-06 05:30:18 +02:00
|
|
|
|
2018-09-21 05:41:25 +02:00
|
|
|
#include "common/alignment.h"
|
2018-03-19 22:45:22 +01:00
|
|
|
#include "common/common_types.h"
|
2018-08-22 04:04:54 +02:00
|
|
|
#include "common/hash.h"
|
2018-03-19 22:45:22 +01:00
|
|
|
#include "common/math_util.h"
|
2018-10-06 05:39:03 +02:00
|
|
|
#include "video_core/engines/fermi_2d.h"
|
2018-06-24 15:50:08 +02:00
|
|
|
#include "video_core/engines/maxwell_3d.h"
|
2018-08-23 21:44:41 +02:00
|
|
|
#include "video_core/rasterizer_cache.h"
|
2018-03-19 22:45:22 +01:00
|
|
|
#include "video_core/renderer_opengl/gl_resource_manager.h"
|
2018-09-14 17:42:28 +02:00
|
|
|
#include "video_core/renderer_opengl/gl_shader_gen.h"
|
2018-10-29 02:14:25 +01:00
|
|
|
#include "video_core/surface.h"
|
2018-10-13 08:08:04 +02:00
|
|
|
#include "video_core/textures/decoders.h"
|
2018-03-27 04:46:11 +02:00
|
|
|
#include "video_core/textures/texture.h"
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-08-21 10:18:27 +02:00
|
|
|
namespace OpenGL {
|
|
|
|
|
2018-06-22 01:36:01 +02:00
|
|
|
class CachedSurface;
|
2018-03-19 22:45:22 +01:00
|
|
|
using Surface = std::shared_ptr<CachedSurface>;
|
2019-02-27 04:47:49 +01:00
|
|
|
using SurfaceSurfaceRect_Tuple = std::tuple<Surface, Surface, Common::Rectangle<u32>>;
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-10-29 02:14:25 +01:00
|
|
|
using SurfaceTarget = VideoCore::Surface::SurfaceTarget;
|
|
|
|
using SurfaceType = VideoCore::Surface::SurfaceType;
|
|
|
|
using PixelFormat = VideoCore::Surface::PixelFormat;
|
|
|
|
using ComponentType = VideoCore::Surface::ComponentType;
|
2019-02-09 00:31:35 +01:00
|
|
|
using Maxwell = Tegra::Engines::Maxwell3D::Regs;
|
2018-09-01 08:42:43 +02:00
|
|
|
|
2018-10-29 02:14:25 +01:00
|
|
|
struct SurfaceParams {
|
2018-10-13 04:06:04 +02:00
|
|
|
enum class SurfaceClass {
|
|
|
|
Uploaded,
|
|
|
|
RenderTarget,
|
|
|
|
DepthBuffer,
|
|
|
|
Copy,
|
|
|
|
};
|
|
|
|
|
2018-09-23 23:55:41 +02:00
|
|
|
static std::string SurfaceTargetName(SurfaceTarget target) {
|
|
|
|
switch (target) {
|
|
|
|
case SurfaceTarget::Texture1D:
|
|
|
|
return "Texture1D";
|
|
|
|
case SurfaceTarget::Texture2D:
|
|
|
|
return "Texture2D";
|
|
|
|
case SurfaceTarget::Texture3D:
|
|
|
|
return "Texture3D";
|
|
|
|
case SurfaceTarget::Texture1DArray:
|
|
|
|
return "Texture1DArray";
|
|
|
|
case SurfaceTarget::Texture2DArray:
|
|
|
|
return "Texture2DArray";
|
|
|
|
case SurfaceTarget::TextureCubemap:
|
|
|
|
return "TextureCubemap";
|
2018-10-19 02:04:43 +02:00
|
|
|
case SurfaceTarget::TextureCubeArray:
|
|
|
|
return "TextureCubeArray";
|
2018-09-23 23:55:41 +02:00
|
|
|
default:
|
|
|
|
LOG_CRITICAL(HW_GPU, "Unimplemented surface_target={}", static_cast<u32>(target));
|
|
|
|
UNREACHABLE();
|
|
|
|
return fmt::format("TextureUnknown({})", static_cast<u32>(target));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 05:45:47 +02:00
|
|
|
u32 GetFormatBpp() const {
|
2018-10-29 02:14:25 +01:00
|
|
|
return VideoCore::Surface::GetFormatBpp(pixel_format);
|
2018-10-13 05:44:18 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 22:14:14 +02:00
|
|
|
/// Returns the rectangle corresponding to this surface
|
2019-02-27 04:47:49 +01:00
|
|
|
Common::Rectangle<u32> GetRect(u32 mip_level = 0) const;
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-10-13 08:08:04 +02:00
|
|
|
/// Returns the total size of this surface in bytes, adjusted for compression
|
|
|
|
std::size_t SizeInBytesRaw(bool ignore_tiled = false) const {
|
2018-06-24 15:50:08 +02:00
|
|
|
const u32 compression_factor{GetCompressionFactor(pixel_format)};
|
2018-10-13 08:08:04 +02:00
|
|
|
const u32 bytes_per_pixel{GetBytesPerPixel(pixel_format)};
|
|
|
|
const size_t uncompressed_size{
|
|
|
|
Tegra::Texture::CalculateSize((ignore_tiled ? false : is_tiled), bytes_per_pixel, width,
|
|
|
|
height, depth, block_height, block_depth)};
|
|
|
|
|
|
|
|
// Divide by compression_factor^2, as height and width are factored by this
|
|
|
|
return uncompressed_size / (compression_factor * compression_factor);
|
2018-09-14 17:33:55 +02:00
|
|
|
}
|
|
|
|
|
2018-10-13 08:08:04 +02:00
|
|
|
/// Returns the size of this surface as an OpenGL texture in bytes
|
|
|
|
std::size_t SizeInBytesGL() const {
|
|
|
|
return SizeInBytesRaw(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the size of this surface as a cube face in bytes
|
|
|
|
std::size_t SizeInBytesCubeFace() const {
|
|
|
|
return size_in_bytes / 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the size of this surface as an OpenGL cube face in bytes
|
|
|
|
std::size_t SizeInBytesCubeFaceGL() const {
|
|
|
|
return size_in_bytes_gl / 6;
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
2018-10-18 21:04:33 +02:00
|
|
|
/// Returns the exact size of memory occupied by the texture in VRAM, including mipmaps.
|
|
|
|
std::size_t MemorySize() const {
|
2018-10-25 00:30:27 +02:00
|
|
|
std::size_t size = InnerMemorySize(false, is_layered);
|
2018-10-18 21:04:33 +02:00
|
|
|
if (is_layered)
|
|
|
|
return size * depth;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the exact size of the memory occupied by a layer in a texture in VRAM, including
|
|
|
|
/// mipmaps.
|
|
|
|
std::size_t LayerMemorySize() const {
|
2018-10-25 00:30:27 +02:00
|
|
|
return InnerMemorySize(false, true);
|
2018-10-18 21:04:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the size of a layer of this surface in OpenGL.
|
2018-10-25 00:30:27 +02:00
|
|
|
std::size_t LayerSizeGL(u32 mip_level) const {
|
|
|
|
return InnerMipmapMemorySize(mip_level, true, is_layered, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t GetMipmapSizeGL(u32 mip_level, bool ignore_compressed = true) const {
|
|
|
|
std::size_t size = InnerMipmapMemorySize(mip_level, true, is_layered, ignore_compressed);
|
|
|
|
if (is_layered)
|
|
|
|
return size * depth;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t GetMipmapLevelOffset(u32 mip_level) const {
|
|
|
|
std::size_t offset = 0;
|
|
|
|
for (u32 i = 0; i < mip_level; i++)
|
|
|
|
offset += InnerMipmapMemorySize(i, false, is_layered);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t GetMipmapLevelOffsetGL(u32 mip_level) const {
|
|
|
|
std::size_t offset = 0;
|
|
|
|
for (u32 i = 0; i < mip_level; i++)
|
|
|
|
offset += InnerMipmapMemorySize(i, true, is_layered);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2019-02-09 00:51:31 +01:00
|
|
|
std::size_t GetMipmapSingleSize(u32 mip_level) const {
|
|
|
|
return InnerMipmapMemorySize(mip_level, false, is_layered);
|
|
|
|
}
|
|
|
|
|
2018-10-25 00:30:27 +02:00
|
|
|
u32 MipWidth(u32 mip_level) const {
|
|
|
|
return std::max(1U, width >> mip_level);
|
|
|
|
}
|
|
|
|
|
2019-02-09 00:51:31 +01:00
|
|
|
u32 MipWidthGobAligned(u32 mip_level) const {
|
|
|
|
return std::max(64U*8U / GetFormatBpp(), width >> mip_level);
|
|
|
|
}
|
|
|
|
|
2018-10-25 00:30:27 +02:00
|
|
|
u32 MipHeight(u32 mip_level) const {
|
|
|
|
return std::max(1U, height >> mip_level);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 MipDepth(u32 mip_level) const {
|
2018-10-30 03:46:09 +01:00
|
|
|
return is_layered ? depth : std::max(1U, depth >> mip_level);
|
2018-10-25 00:30:27 +02:00
|
|
|
}
|
|
|
|
|
2018-10-25 18:24:10 +02:00
|
|
|
// Auto block resizing algorithm from:
|
|
|
|
// https://cgit.freedesktop.org/mesa/mesa/tree/src/gallium/drivers/nouveau/nv50/nv50_miptree.c
|
2018-10-27 01:18:03 +02:00
|
|
|
u32 MipBlockHeight(u32 mip_level) const {
|
2018-10-25 18:24:10 +02:00
|
|
|
if (mip_level == 0)
|
|
|
|
return block_height;
|
2018-10-27 01:18:03 +02:00
|
|
|
u32 alt_height = MipHeight(mip_level);
|
|
|
|
u32 h = GetDefaultBlockHeight(pixel_format);
|
|
|
|
u32 blocks_in_y = (alt_height + h - 1) / h;
|
|
|
|
u32 bh = 16;
|
|
|
|
while (bh > 1 && blocks_in_y <= bh * 4) {
|
2018-10-25 18:24:10 +02:00
|
|
|
bh >>= 1;
|
2018-10-25 00:30:27 +02:00
|
|
|
}
|
|
|
|
return bh;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 MipBlockDepth(u32 mip_level) const {
|
2019-02-12 22:46:37 +01:00
|
|
|
if (mip_level == 0) {
|
2018-10-25 18:24:10 +02:00
|
|
|
return block_depth;
|
2019-02-12 22:46:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_layered) {
|
2018-10-27 01:18:03 +02:00
|
|
|
return 1;
|
2019-02-12 22:46:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const u32 mip_depth = MipDepth(mip_level);
|
2018-10-25 18:24:10 +02:00
|
|
|
u32 bd = 32;
|
2019-02-12 22:46:37 +01:00
|
|
|
while (bd > 1 && mip_depth * 2 <= bd) {
|
2018-10-27 01:18:03 +02:00
|
|
|
bd >>= 1;
|
|
|
|
}
|
2019-02-12 22:46:37 +01:00
|
|
|
|
2018-10-27 01:18:03 +02:00
|
|
|
if (bd == 32) {
|
2019-02-12 22:46:37 +01:00
|
|
|
const u32 bh = MipBlockHeight(mip_level);
|
|
|
|
if (bh >= 4) {
|
2018-10-27 01:18:03 +02:00
|
|
|
return 16;
|
2019-02-12 22:46:37 +01:00
|
|
|
}
|
2018-10-25 00:30:27 +02:00
|
|
|
}
|
2019-02-12 22:46:37 +01:00
|
|
|
|
2018-10-25 00:30:27 +02:00
|
|
|
return bd;
|
2018-10-18 21:04:33 +02:00
|
|
|
}
|
|
|
|
|
2018-07-04 05:32:59 +02:00
|
|
|
/// Creates SurfaceParams from a texture configuration
|
2018-09-14 17:42:28 +02:00
|
|
|
static SurfaceParams CreateForTexture(const Tegra::Texture::FullTextureInfo& config,
|
|
|
|
const GLShader::SamplerEntry& entry);
|
2018-06-26 20:59:45 +02:00
|
|
|
|
2018-07-04 05:32:59 +02:00
|
|
|
/// Creates SurfaceParams from a framebuffer configuration
|
2018-09-15 15:21:06 +02:00
|
|
|
static SurfaceParams CreateForFramebuffer(std::size_t index);
|
2018-06-26 20:59:45 +02:00
|
|
|
|
2018-07-04 05:32:59 +02:00
|
|
|
/// Creates SurfaceParams for a depth buffer configuration
|
2018-10-10 04:28:19 +02:00
|
|
|
static SurfaceParams CreateForDepthBuffer(
|
|
|
|
u32 zeta_width, u32 zeta_height, Tegra::GPUVAddr zeta_address, Tegra::DepthFormat format,
|
|
|
|
u32 block_width, u32 block_height, u32 block_depth,
|
|
|
|
Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout type);
|
2018-07-04 05:32:59 +02:00
|
|
|
|
2018-10-06 05:39:03 +02:00
|
|
|
/// Creates SurfaceParams for a Fermi2D surface copy
|
|
|
|
static SurfaceParams CreateForFermiCopySurface(
|
|
|
|
const Tegra::Engines::Fermi2D::Regs::Surface& config);
|
|
|
|
|
2018-08-06 05:30:18 +02:00
|
|
|
/// Checks if surfaces are compatible for caching
|
|
|
|
bool IsCompatibleSurface(const SurfaceParams& other) const {
|
2018-11-16 18:01:54 +01:00
|
|
|
if (std::tie(pixel_format, type, width, height, target, depth, is_tiled) ==
|
|
|
|
std::tie(other.pixel_format, other.type, other.width, other.height, other.target,
|
|
|
|
other.depth, other.is_tiled)) {
|
|
|
|
if (!is_tiled)
|
|
|
|
return true;
|
|
|
|
return std::tie(block_height, block_depth, tile_width_spacing) ==
|
|
|
|
std::tie(other.block_height, other.block_depth, other.tile_width_spacing);
|
|
|
|
}
|
|
|
|
return false;
|
2018-08-06 05:30:18 +02:00
|
|
|
}
|
|
|
|
|
2018-10-13 04:24:40 +02:00
|
|
|
/// Initializes parameters for caching, should be called after everything has been initialized
|
|
|
|
void InitCacheParameters(Tegra::GPUVAddr gpu_addr);
|
|
|
|
|
2018-10-13 04:06:04 +02:00
|
|
|
std::string TargetName() const {
|
|
|
|
switch (target) {
|
|
|
|
case SurfaceTarget::Texture1D:
|
|
|
|
return "1D";
|
|
|
|
case SurfaceTarget::Texture2D:
|
|
|
|
return "2D";
|
|
|
|
case SurfaceTarget::Texture3D:
|
|
|
|
return "3D";
|
|
|
|
case SurfaceTarget::Texture1DArray:
|
|
|
|
return "1DArray";
|
|
|
|
case SurfaceTarget::Texture2DArray:
|
|
|
|
return "2DArray";
|
|
|
|
case SurfaceTarget::TextureCubemap:
|
|
|
|
return "Cube";
|
|
|
|
default:
|
|
|
|
LOG_CRITICAL(HW_GPU, "Unimplemented surface_target={}", static_cast<u32>(target));
|
|
|
|
UNREACHABLE();
|
|
|
|
return fmt::format("TUK({})", static_cast<u32>(target));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ClassName() const {
|
|
|
|
switch (identity) {
|
|
|
|
case SurfaceClass::Uploaded:
|
|
|
|
return "UP";
|
|
|
|
case SurfaceClass::RenderTarget:
|
|
|
|
return "RT";
|
|
|
|
case SurfaceClass::DepthBuffer:
|
|
|
|
return "DB";
|
|
|
|
case SurfaceClass::Copy:
|
|
|
|
return "CP";
|
|
|
|
default:
|
|
|
|
LOG_CRITICAL(HW_GPU, "Unimplemented surface_class={}", static_cast<u32>(identity));
|
|
|
|
UNREACHABLE();
|
|
|
|
return fmt::format("CUK({})", static_cast<u32>(identity));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string IdentityString() const {
|
|
|
|
return ClassName() + '_' + TargetName() + '_' + (is_tiled ? 'T' : 'L');
|
|
|
|
}
|
|
|
|
|
2018-06-26 20:59:45 +02:00
|
|
|
bool is_tiled;
|
2018-10-08 20:34:55 +02:00
|
|
|
u32 block_width;
|
2018-06-26 20:59:45 +02:00
|
|
|
u32 block_height;
|
2018-10-08 20:34:55 +02:00
|
|
|
u32 block_depth;
|
2018-11-16 18:01:54 +01:00
|
|
|
u32 tile_width_spacing;
|
2018-06-26 20:59:45 +02:00
|
|
|
PixelFormat pixel_format;
|
|
|
|
ComponentType component_type;
|
|
|
|
SurfaceType type;
|
|
|
|
u32 width;
|
|
|
|
u32 height;
|
2018-09-05 04:00:24 +02:00
|
|
|
u32 depth;
|
2018-06-26 21:05:13 +02:00
|
|
|
u32 unaligned_height;
|
2019-02-09 00:28:01 +01:00
|
|
|
u32 pitch;
|
2018-09-01 08:42:43 +02:00
|
|
|
SurfaceTarget target;
|
2018-10-13 04:06:04 +02:00
|
|
|
SurfaceClass identity;
|
2018-09-21 05:41:25 +02:00
|
|
|
u32 max_mip_level;
|
2018-10-18 21:04:33 +02:00
|
|
|
bool is_layered;
|
2019-01-30 00:33:56 +01:00
|
|
|
bool is_array;
|
2018-10-24 22:09:40 +02:00
|
|
|
bool srgb_conversion;
|
2018-10-13 04:24:40 +02:00
|
|
|
// Parameters used for caching
|
|
|
|
VAddr addr;
|
2018-10-16 03:24:34 +02:00
|
|
|
Tegra::GPUVAddr gpu_addr;
|
2018-10-13 08:08:04 +02:00
|
|
|
std::size_t size_in_bytes;
|
|
|
|
std::size_t size_in_bytes_gl;
|
2018-10-13 04:24:40 +02:00
|
|
|
|
2018-09-21 05:41:25 +02:00
|
|
|
// Render target specific parameters, not used in caching
|
|
|
|
struct {
|
|
|
|
u32 index;
|
|
|
|
u32 array_mode;
|
2018-10-16 03:04:20 +02:00
|
|
|
u32 volume;
|
2018-09-21 05:41:25 +02:00
|
|
|
u32 layer_stride;
|
|
|
|
u32 base_layer;
|
|
|
|
} rt;
|
2018-10-18 21:04:33 +02:00
|
|
|
|
|
|
|
private:
|
2018-10-25 00:30:27 +02:00
|
|
|
std::size_t InnerMipmapMemorySize(u32 mip_level, bool force_gl = false, bool layer_only = false,
|
|
|
|
bool uncompressed = false) const;
|
|
|
|
std::size_t InnerMemorySize(bool force_gl = false, bool layer_only = false,
|
|
|
|
bool uncompressed = false) const;
|
2018-06-26 20:59:45 +02:00
|
|
|
};
|
|
|
|
|
2018-08-22 04:04:54 +02:00
|
|
|
}; // namespace OpenGL
|
|
|
|
|
|
|
|
/// Hashable variation of SurfaceParams, used for a key in the surface cache
|
|
|
|
struct SurfaceReserveKey : Common::HashableStruct<OpenGL::SurfaceParams> {
|
|
|
|
static SurfaceReserveKey Create(const OpenGL::SurfaceParams& params) {
|
|
|
|
SurfaceReserveKey res;
|
|
|
|
res.state = params;
|
2018-10-13 04:06:04 +02:00
|
|
|
res.state.identity = {}; // Ignore the origin of the texture
|
2018-10-16 03:24:34 +02:00
|
|
|
res.state.gpu_addr = {}; // Ignore GPU vaddr in caching
|
|
|
|
res.state.rt = {}; // Ignore rt config in caching
|
2018-08-22 04:04:54 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
namespace std {
|
|
|
|
template <>
|
|
|
|
struct hash<SurfaceReserveKey> {
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t operator()(const SurfaceReserveKey& k) const {
|
2018-08-22 04:04:54 +02:00
|
|
|
return k.Hash();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
namespace OpenGL {
|
|
|
|
|
2018-11-08 12:08:00 +01:00
|
|
|
class RasterizerOpenGL;
|
|
|
|
|
2018-10-16 22:51:53 +02:00
|
|
|
class CachedSurface final : public RasterizerCacheObject {
|
2018-06-22 01:36:01 +02:00
|
|
|
public:
|
|
|
|
CachedSurface(const SurfaceParams& params);
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-10-16 22:51:53 +02:00
|
|
|
VAddr GetAddr() const override {
|
2018-08-23 21:44:41 +02:00
|
|
|
return params.addr;
|
|
|
|
}
|
|
|
|
|
2018-10-16 22:51:53 +02:00
|
|
|
std::size_t GetSizeInBytes() const override {
|
2018-10-16 03:24:34 +02:00
|
|
|
return cached_size_in_bytes;
|
2018-08-23 21:44:41 +02:00
|
|
|
}
|
|
|
|
|
2019-02-09 00:40:32 +01:00
|
|
|
std::size_t GetMemorySize() const {
|
|
|
|
return memory_size;
|
|
|
|
}
|
|
|
|
|
2018-10-16 22:51:53 +02:00
|
|
|
void Flush() override {
|
2018-10-10 01:15:05 +02:00
|
|
|
FlushGLBuffer();
|
|
|
|
}
|
|
|
|
|
2018-06-22 01:36:01 +02:00
|
|
|
const OGLTexture& Texture() const {
|
|
|
|
return texture;
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 21:51:32 +01:00
|
|
|
const OGLTexture& TextureLayer() {
|
2019-01-30 00:33:56 +01:00
|
|
|
if (params.is_array) {
|
2018-12-29 21:51:32 +01:00
|
|
|
return Texture();
|
|
|
|
}
|
|
|
|
EnsureTextureView();
|
|
|
|
return texture_view;
|
|
|
|
}
|
|
|
|
|
2018-09-01 08:42:43 +02:00
|
|
|
GLenum Target() const {
|
|
|
|
return gl_target;
|
|
|
|
}
|
|
|
|
|
2018-12-29 21:51:32 +01:00
|
|
|
GLenum TargetLayer() const {
|
|
|
|
using VideoCore::Surface::SurfaceTarget;
|
|
|
|
switch (params.target) {
|
|
|
|
case SurfaceTarget::Texture1D:
|
|
|
|
return GL_TEXTURE_1D_ARRAY;
|
|
|
|
case SurfaceTarget::Texture2D:
|
|
|
|
return GL_TEXTURE_2D_ARRAY;
|
|
|
|
case SurfaceTarget::TextureCubemap:
|
|
|
|
return GL_TEXTURE_CUBE_MAP_ARRAY;
|
|
|
|
}
|
|
|
|
return Target();
|
|
|
|
}
|
|
|
|
|
2018-06-22 01:36:01 +02:00
|
|
|
const SurfaceParams& GetSurfaceParams() const {
|
|
|
|
return params;
|
|
|
|
}
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-03-23 04:06:54 +01:00
|
|
|
// Read/Write data in Switch memory to/from gl_buffer
|
2018-06-22 01:36:01 +02:00
|
|
|
void LoadGLBuffer();
|
|
|
|
void FlushGLBuffer();
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-09-01 08:42:43 +02:00
|
|
|
// Upload data in gl_buffer to this surface's texture
|
2018-06-22 01:36:01 +02:00
|
|
|
void UploadGLTexture(GLuint read_fb_handle, GLuint draw_fb_handle);
|
|
|
|
|
2019-01-07 03:02:27 +01:00
|
|
|
void UpdateSwizzle(Tegra::Texture::SwizzleSource swizzle_x,
|
|
|
|
Tegra::Texture::SwizzleSource swizzle_y,
|
|
|
|
Tegra::Texture::SwizzleSource swizzle_z,
|
|
|
|
Tegra::Texture::SwizzleSource swizzle_w);
|
|
|
|
|
2019-02-09 00:40:32 +01:00
|
|
|
void MarkReinterpreted() {
|
|
|
|
reinterpreted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsReinterpreted() {
|
|
|
|
return reinterpreted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MarkForReload(bool reload) {
|
|
|
|
must_reload = reload;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MustReload() {
|
|
|
|
return must_reload;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsUploaded() {
|
|
|
|
return params.identity == SurfaceParams::SurfaceClass::Uploaded;
|
|
|
|
}
|
|
|
|
|
2018-06-22 01:36:01 +02:00
|
|
|
private:
|
2018-10-25 00:30:27 +02:00
|
|
|
void UploadGLMipmapTexture(u32 mip_map, GLuint read_fb_handle, GLuint draw_fb_handle);
|
|
|
|
|
2018-12-29 21:51:32 +01:00
|
|
|
void EnsureTextureView();
|
|
|
|
|
2018-06-22 01:36:01 +02:00
|
|
|
OGLTexture texture;
|
2018-12-29 21:51:32 +01:00
|
|
|
OGLTexture texture_view;
|
2018-10-25 00:30:27 +02:00
|
|
|
std::vector<std::vector<u8>> gl_buffer;
|
2018-12-29 21:51:32 +01:00
|
|
|
SurfaceParams params{};
|
|
|
|
GLenum gl_target{};
|
|
|
|
GLenum gl_internal_format{};
|
|
|
|
std::size_t cached_size_in_bytes{};
|
2019-01-07 03:02:27 +01:00
|
|
|
std::array<GLenum, 4> swizzle{GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA};
|
2019-02-09 00:40:32 +01:00
|
|
|
std::size_t memory_size;
|
|
|
|
bool reinterpreted = false;
|
|
|
|
bool must_reload = false;
|
2018-03-19 22:45:22 +01:00
|
|
|
};
|
|
|
|
|
2018-08-23 21:44:41 +02:00
|
|
|
class RasterizerCacheOpenGL final : public RasterizerCache<Surface> {
|
2018-03-19 22:45:22 +01:00
|
|
|
public:
|
2018-11-08 12:08:00 +01:00
|
|
|
explicit RasterizerCacheOpenGL(RasterizerOpenGL& rasterizer);
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-06-26 22:14:14 +02:00
|
|
|
/// Get a surface based on the texture configuration
|
2018-09-14 17:42:28 +02:00
|
|
|
Surface GetTextureSurface(const Tegra::Texture::FullTextureInfo& config,
|
|
|
|
const GLShader::SamplerEntry& entry);
|
2018-06-26 22:14:14 +02:00
|
|
|
|
2018-09-10 01:01:21 +02:00
|
|
|
/// Get the depth surface based on the framebuffer configuration
|
|
|
|
Surface GetDepthBufferSurface(bool preserve_contents);
|
|
|
|
|
|
|
|
/// Get the color surface based on the framebuffer configuration and the specified render target
|
2018-09-15 15:21:06 +02:00
|
|
|
Surface GetColorBufferSurface(std::size_t index, bool preserve_contents);
|
2018-06-26 22:14:14 +02:00
|
|
|
|
2018-08-28 03:35:15 +02:00
|
|
|
/// Tries to find a framebuffer using on the provided CPU address
|
|
|
|
Surface TryFindFramebufferSurface(VAddr addr) const;
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-10-06 05:39:03 +02:00
|
|
|
/// Copies the contents of one surface to another
|
|
|
|
void FermiCopySurface(const Tegra::Engines::Fermi2D::Regs::Surface& src_config,
|
2018-12-15 06:20:00 +01:00
|
|
|
const Tegra::Engines::Fermi2D::Regs::Surface& dst_config,
|
2019-02-27 04:47:49 +01:00
|
|
|
const Common::Rectangle<u32>& src_rect,
|
|
|
|
const Common::Rectangle<u32>& dst_rect);
|
2018-10-06 05:39:03 +02:00
|
|
|
|
2019-02-09 01:06:49 +01:00
|
|
|
void SignalPreDrawCall();
|
|
|
|
void SignalPostDrawCall();
|
|
|
|
void SignalSurfaceParameter(Surface& surface);
|
|
|
|
void SignalPostFramebufferSetup();
|
|
|
|
|
2018-03-19 22:45:22 +01:00
|
|
|
private:
|
2018-06-26 22:14:14 +02:00
|
|
|
void LoadSurface(const Surface& surface);
|
2018-08-20 22:18:51 +02:00
|
|
|
Surface GetSurface(const SurfaceParams& params, bool preserve_contents = true);
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-08-31 02:08:43 +02:00
|
|
|
/// Gets an uncached surface, creating it if need be
|
|
|
|
Surface GetUncachedSurface(const SurfaceParams& params);
|
|
|
|
|
2018-07-21 22:04:01 +02:00
|
|
|
/// Recreates a surface with new parameters
|
2018-09-14 17:33:55 +02:00
|
|
|
Surface RecreateSurface(const Surface& old_surface, const SurfaceParams& new_params);
|
2018-07-21 22:04:01 +02:00
|
|
|
|
2018-08-22 04:04:54 +02:00
|
|
|
/// Reserves a unique surface that can be reused later
|
|
|
|
void ReserveSurface(const Surface& surface);
|
|
|
|
|
|
|
|
/// Tries to get a reserved surface for the specified parameters
|
|
|
|
Surface TryGetReservedSurface(const SurfaceParams& params);
|
|
|
|
|
2019-02-09 00:31:35 +01:00
|
|
|
/// When a render target is changed, this method is called with the previous render target
|
|
|
|
void NotifyFrameBufferChange(Surface triggering_surface);
|
|
|
|
|
2019-02-09 00:51:31 +01:00
|
|
|
// Partialy reinterpret a surface based on a triggering_surface that collides with it.
|
|
|
|
bool PartialReinterpretSurface(Surface triggering_surface, Surface intersect);
|
|
|
|
|
2018-10-16 23:20:49 +02:00
|
|
|
/// Performs a slow but accurate surface copy, flushing to RAM and reinterpreting the data
|
|
|
|
void AccurateCopySurface(const Surface& src_surface, const Surface& dst_surface);
|
2018-11-19 17:04:07 +01:00
|
|
|
void FastLayeredCopySurface(const Surface& src_surface, const Surface& dst_surface);
|
2019-02-03 04:41:39 +01:00
|
|
|
void FastCopySurface(const Surface& src_surface, const Surface& dst_surface);
|
|
|
|
void CopySurface(const Surface& src_surface, const Surface& dst_surface,
|
|
|
|
const GLuint copy_pbo_handle, const GLenum src_attachment = 0,
|
|
|
|
const GLenum dst_attachment = 0, const std::size_t cubemap_face = 0);
|
2018-10-16 23:20:49 +02:00
|
|
|
|
2018-08-22 04:04: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.
|
|
|
|
std::unordered_map<SurfaceReserveKey, Surface> surface_reserve;
|
|
|
|
|
2018-03-19 22:45:22 +01:00
|
|
|
OGLFramebuffer read_framebuffer;
|
|
|
|
OGLFramebuffer draw_framebuffer;
|
2018-09-08 07:04:47 +02:00
|
|
|
|
2019-02-09 00:40:32 +01:00
|
|
|
bool run_texception_pass = false;
|
|
|
|
bool texception = false;
|
|
|
|
|
2018-09-08 07:04:47 +02:00
|
|
|
/// Use a Pixel Buffer Object to download the previous texture and then upload it to the new one
|
|
|
|
/// using the new format.
|
|
|
|
OGLBuffer copy_pbo;
|
2019-01-07 05:11:03 +01:00
|
|
|
|
2019-02-09 00:31:35 +01:00
|
|
|
std::array<Surface, Maxwell::NumRenderTargets> last_color_buffers;
|
|
|
|
std::array<Surface, Maxwell::NumRenderTargets> current_color_buffers;
|
2019-01-07 06:09:39 +01:00
|
|
|
Surface last_depth_buffer;
|
2019-02-09 00:40:32 +01:00
|
|
|
|
|
|
|
using SurfaceIntervalCache = boost::icl::interval_map<VAddr, Surface>;
|
|
|
|
using SurfaceInterval = typename IntervalCache::interval_type;
|
|
|
|
|
|
|
|
static auto GetReinterpretInterval(const Surface& object) {
|
|
|
|
return SurfaceInterval::right_open(object->GetAddr() + 1,
|
|
|
|
object->GetAddr() + object->GetMemorySize() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reinterpreted surfaces are very fragil as the game may keep rendering into them.
|
|
|
|
SurfaceIntervalCache reinterpreted_surfaces;
|
|
|
|
|
|
|
|
void RegisterReinterpretSurface(Surface r_surface) {
|
|
|
|
auto interval = GetReinterpretInterval(r_surface);
|
|
|
|
reinterpreted_surfaces.insert({interval, r_surface});
|
|
|
|
r_surface->MarkReinterpreted();
|
|
|
|
run_texception_pass = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Surface CollideOnReinterpretedSurface(VAddr addr) const {
|
|
|
|
const SurfaceInterval interval{addr};
|
|
|
|
for (auto& pair :
|
|
|
|
boost::make_iterator_range(reinterpreted_surfaces.equal_range(interval))) {
|
|
|
|
return pair.second;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void Register(const Surface& object) {
|
|
|
|
RasterizerCache<Surface>::Register(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unregisters an object from the cache
|
|
|
|
void Unregister(const Surface& object) {
|
|
|
|
const auto& params = object->GetSurfaceParams();
|
|
|
|
if (object->IsReinterpreted()) {
|
|
|
|
auto interval = GetReinterpretInterval(object);
|
|
|
|
reinterpreted_surfaces.erase(interval);
|
|
|
|
}
|
|
|
|
RasterizerCache<Surface>::Unregister(object);
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:45:22 +01:00
|
|
|
};
|
2018-08-21 10:18:27 +02:00
|
|
|
|
|
|
|
} // namespace OpenGL
|