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.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <glad/glad.h>
|
2018-06-22 01:36:01 +02:00
|
|
|
|
2018-03-19 22:45:22 +01:00
|
|
|
#include "common/alignment.h"
|
2018-06-22 01:36:01 +02:00
|
|
|
#include "common/assert.h"
|
2018-09-08 07:04:47 +02:00
|
|
|
#include "common/logging/log.h"
|
2018-03-19 22:45:22 +01:00
|
|
|
#include "common/microprofile.h"
|
|
|
|
#include "common/scope_exit.h"
|
2018-03-24 05:47:33 +01:00
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/hle/kernel/process.h"
|
2018-03-19 22:45:22 +01:00
|
|
|
#include "core/memory.h"
|
2018-06-26 22:14:14 +02:00
|
|
|
#include "core/settings.h"
|
2018-03-24 05:47:33 +01:00
|
|
|
#include "video_core/engines/maxwell_3d.h"
|
2018-11-25 03:53:17 +01:00
|
|
|
#include "video_core/morton.h"
|
2018-11-08 12:08:00 +01:00
|
|
|
#include "video_core/renderer_opengl/gl_rasterizer.h"
|
2018-03-19 22:45:22 +01:00
|
|
|
#include "video_core/renderer_opengl/gl_rasterizer_cache.h"
|
2018-11-06 10:40:49 +01:00
|
|
|
#include "video_core/renderer_opengl/gl_state.h"
|
2018-10-29 01:54:08 +01:00
|
|
|
#include "video_core/renderer_opengl/utils.h"
|
2018-10-29 02:14:25 +01:00
|
|
|
#include "video_core/surface.h"
|
2018-06-18 05:50:44 +02:00
|
|
|
#include "video_core/textures/astc.h"
|
2018-03-27 04:48:05 +02:00
|
|
|
#include "video_core/textures/decoders.h"
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-08-21 10:18:27 +02:00
|
|
|
namespace OpenGL {
|
|
|
|
|
2018-11-25 03:53:17 +01:00
|
|
|
using VideoCore::MortonSwizzle;
|
|
|
|
using VideoCore::MortonSwizzleMode;
|
2018-10-29 02:14:25 +01:00
|
|
|
using VideoCore::Surface::ComponentTypeFromDepthFormat;
|
|
|
|
using VideoCore::Surface::ComponentTypeFromRenderTarget;
|
|
|
|
using VideoCore::Surface::ComponentTypeFromTexture;
|
|
|
|
using VideoCore::Surface::PixelFormatFromDepthFormat;
|
|
|
|
using VideoCore::Surface::PixelFormatFromRenderTargetFormat;
|
|
|
|
using VideoCore::Surface::PixelFormatFromTextureFormat;
|
|
|
|
using VideoCore::Surface::SurfaceTargetFromTextureType;
|
2018-03-19 22:45:22 +01:00
|
|
|
|
|
|
|
struct FormatTuple {
|
|
|
|
GLint internal_format;
|
|
|
|
GLenum format;
|
|
|
|
GLenum type;
|
2018-06-30 21:08:51 +02:00
|
|
|
ComponentType component_type;
|
2018-03-27 04:48:05 +02:00
|
|
|
bool compressed;
|
2018-03-19 22:45:22 +01:00
|
|
|
};
|
|
|
|
|
2018-10-16 03:24:34 +02:00
|
|
|
void SurfaceParams::InitCacheParameters(Tegra::GPUVAddr gpu_addr_) {
|
2018-10-13 04:24:40 +02:00
|
|
|
auto& memory_manager{Core::System::GetInstance().GPU().MemoryManager()};
|
2018-10-16 03:24:34 +02:00
|
|
|
const auto cpu_addr{memory_manager.GpuToCpuAddress(gpu_addr_)};
|
2018-10-13 04:24:40 +02:00
|
|
|
|
|
|
|
addr = cpu_addr ? *cpu_addr : 0;
|
2018-10-16 03:24:34 +02:00
|
|
|
gpu_addr = gpu_addr_;
|
2018-10-13 08:08:04 +02:00
|
|
|
size_in_bytes = SizeInBytesRaw();
|
|
|
|
|
|
|
|
if (IsPixelFormatASTC(pixel_format)) {
|
|
|
|
// ASTC is uncompressed in software, in emulated as RGBA8
|
|
|
|
size_in_bytes_gl = width * height * depth * 4;
|
|
|
|
} else {
|
|
|
|
size_in_bytes_gl = SizeInBytesGL();
|
|
|
|
}
|
2018-08-28 03:35:15 +02:00
|
|
|
}
|
|
|
|
|
2018-10-25 00:30:27 +02:00
|
|
|
std::size_t SurfaceParams::InnerMipmapMemorySize(u32 mip_level, bool force_gl, bool layer_only,
|
|
|
|
bool uncompressed) const {
|
2018-10-30 03:46:09 +01:00
|
|
|
const u32 tile_x{GetDefaultBlockWidth(pixel_format)};
|
|
|
|
const u32 tile_y{GetDefaultBlockHeight(pixel_format)};
|
2018-10-18 21:04:33 +02:00
|
|
|
const u32 bytes_per_pixel{GetBytesPerPixel(pixel_format)};
|
|
|
|
u32 m_depth = (layer_only ? 1U : depth);
|
2018-10-26 22:17:29 +02:00
|
|
|
u32 m_width = MipWidth(mip_level);
|
|
|
|
u32 m_height = MipHeight(mip_level);
|
2018-10-30 03:46:09 +01:00
|
|
|
m_width = uncompressed ? m_width : std::max(1U, (m_width + tile_x - 1) / tile_x);
|
|
|
|
m_height = uncompressed ? m_height : std::max(1U, (m_height + tile_y - 1) / tile_y);
|
2018-10-25 00:30:27 +02:00
|
|
|
m_depth = std::max(1U, m_depth >> mip_level);
|
2018-10-27 01:18:03 +02:00
|
|
|
u32 m_block_height = MipBlockHeight(mip_level);
|
2018-10-25 00:30:27 +02:00
|
|
|
u32 m_block_depth = MipBlockDepth(mip_level);
|
|
|
|
return Tegra::Texture::CalculateSize(force_gl ? false : is_tiled, bytes_per_pixel, m_width,
|
|
|
|
m_height, m_depth, m_block_height, m_block_depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t SurfaceParams::InnerMemorySize(bool force_gl, bool layer_only,
|
|
|
|
bool uncompressed) const {
|
2018-10-27 01:18:03 +02:00
|
|
|
std::size_t block_size_bytes = Tegra::Texture::GetGOBSize() * block_height * block_depth;
|
2018-10-25 00:30:27 +02:00
|
|
|
std::size_t size = 0;
|
|
|
|
for (u32 i = 0; i < max_mip_level; i++) {
|
|
|
|
size += InnerMipmapMemorySize(i, force_gl, layer_only, uncompressed);
|
|
|
|
}
|
|
|
|
if (!force_gl && is_tiled) {
|
|
|
|
size = Common::AlignUp(size, block_size_bytes);
|
2018-10-18 21:04:33 +02:00
|
|
|
}
|
2018-10-25 00:30:27 +02:00
|
|
|
return size;
|
2018-10-18 21:04:33 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 20:59:45 +02:00
|
|
|
/*static*/ SurfaceParams SurfaceParams::CreateForTexture(
|
2018-09-14 17:42:28 +02:00
|
|
|
const Tegra::Texture::FullTextureInfo& config, const GLShader::SamplerEntry& entry) {
|
2018-06-26 20:59:45 +02:00
|
|
|
SurfaceParams params{};
|
|
|
|
params.is_tiled = config.tic.IsTiled();
|
2018-10-08 20:34:55 +02:00
|
|
|
params.block_width = params.is_tiled ? config.tic.BlockWidth() : 0,
|
2018-06-26 20:59:45 +02:00
|
|
|
params.block_height = params.is_tiled ? config.tic.BlockHeight() : 0,
|
2018-10-08 20:34:55 +02:00
|
|
|
params.block_depth = params.is_tiled ? config.tic.BlockDepth() : 0,
|
2018-11-16 18:01:54 +01:00
|
|
|
params.tile_width_spacing = params.is_tiled ? (1 << config.tic.tile_width_spacing.Value()) : 1;
|
2018-10-24 22:09:40 +02:00
|
|
|
params.srgb_conversion = config.tic.IsSrgbConversionEnabled();
|
|
|
|
params.pixel_format = PixelFormatFromTextureFormat(config.tic.format, config.tic.r_type.Value(),
|
|
|
|
params.srgb_conversion);
|
2018-06-26 20:59:45 +02:00
|
|
|
params.component_type = ComponentTypeFromTexture(config.tic.r_type.Value());
|
|
|
|
params.type = GetFormatType(params.pixel_format);
|
|
|
|
params.width = Common::AlignUp(config.tic.Width(), GetCompressionFactor(params.pixel_format));
|
|
|
|
params.height = Common::AlignUp(config.tic.Height(), GetCompressionFactor(params.pixel_format));
|
2018-06-26 21:05:13 +02:00
|
|
|
params.unaligned_height = config.tic.Height();
|
2018-09-01 08:42:43 +02:00
|
|
|
params.target = SurfaceTargetFromTextureType(config.tic.texture_type);
|
2018-09-10 05:42:57 +02:00
|
|
|
|
|
|
|
switch (params.target) {
|
|
|
|
case SurfaceTarget::Texture1D:
|
|
|
|
case SurfaceTarget::Texture2D:
|
|
|
|
params.depth = 1;
|
|
|
|
break;
|
2018-09-14 18:17:38 +02:00
|
|
|
case SurfaceTarget::TextureCubemap:
|
|
|
|
params.depth = config.tic.Depth() * 6;
|
|
|
|
break;
|
2018-09-10 05:42:57 +02:00
|
|
|
case SurfaceTarget::Texture3D:
|
2018-09-14 17:42:28 +02:00
|
|
|
params.depth = config.tic.Depth();
|
|
|
|
break;
|
2018-09-10 05:42:57 +02:00
|
|
|
case SurfaceTarget::Texture2DArray:
|
|
|
|
params.depth = config.tic.Depth();
|
2018-09-14 17:42:28 +02:00
|
|
|
if (!entry.IsArray()) {
|
|
|
|
// TODO(bunnei): We have seen games re-use a Texture2D as Texture2DArray with depth of
|
|
|
|
// one, but sample the texture in the shader as if it were not an array texture. This
|
|
|
|
// probably is valid on hardware, but we still need to write a test to confirm this. In
|
|
|
|
// emulation, the workaround here is to continue to treat this as a Texture2D. An
|
|
|
|
// example game that does this is Super Mario Odyssey (in Cloud Kingdom).
|
|
|
|
ASSERT(params.depth == 1);
|
|
|
|
params.target = SurfaceTarget::Texture2D;
|
|
|
|
}
|
2018-09-10 05:42:57 +02:00
|
|
|
break;
|
2018-10-19 02:04:43 +02:00
|
|
|
case SurfaceTarget::TextureCubeArray:
|
|
|
|
params.depth = config.tic.Depth() * 6;
|
|
|
|
if (!entry.IsArray()) {
|
|
|
|
ASSERT(params.depth == 6);
|
|
|
|
params.target = SurfaceTarget::TextureCubemap;
|
|
|
|
}
|
|
|
|
break;
|
2018-09-10 05:42:57 +02:00
|
|
|
default:
|
|
|
|
LOG_CRITICAL(HW_GPU, "Unknown depth for target={}", static_cast<u32>(params.target));
|
|
|
|
UNREACHABLE();
|
|
|
|
params.depth = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-10-18 21:04:33 +02:00
|
|
|
params.is_layered = SurfaceTargetIsLayered(params.target);
|
2018-09-21 05:41:25 +02:00
|
|
|
params.max_mip_level = config.tic.max_mip_level + 1;
|
|
|
|
params.rt = {};
|
|
|
|
|
2018-10-13 04:24:40 +02:00
|
|
|
params.InitCacheParameters(config.tic.Address());
|
|
|
|
|
2018-06-26 20:59:45 +02:00
|
|
|
return params;
|
2018-06-24 15:50:08 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
/*static*/ SurfaceParams SurfaceParams::CreateForFramebuffer(std::size_t index) {
|
2018-09-10 01:01:21 +02:00
|
|
|
const auto& config{Core::System::GetInstance().GPU().Maxwell3D().regs.rt[index]};
|
2018-06-26 20:59:45 +02:00
|
|
|
SurfaceParams params{};
|
2018-10-13 04:24:40 +02:00
|
|
|
|
2018-10-10 04:28:19 +02:00
|
|
|
params.is_tiled =
|
|
|
|
config.memory_layout.type == Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout::BlockLinear;
|
|
|
|
params.block_width = 1 << config.memory_layout.block_width;
|
|
|
|
params.block_height = 1 << config.memory_layout.block_height;
|
|
|
|
params.block_depth = 1 << config.memory_layout.block_depth;
|
2018-11-16 18:01:54 +01:00
|
|
|
params.tile_width_spacing = 1;
|
2018-06-26 20:59:45 +02:00
|
|
|
params.pixel_format = PixelFormatFromRenderTargetFormat(config.format);
|
2018-10-24 22:09:40 +02:00
|
|
|
params.srgb_conversion = config.format == Tegra::RenderTargetFormat::BGRA8_SRGB ||
|
|
|
|
config.format == Tegra::RenderTargetFormat::RGBA8_SRGB;
|
2018-06-26 20:59:45 +02:00
|
|
|
params.component_type = ComponentTypeFromRenderTarget(config.format);
|
|
|
|
params.type = GetFormatType(params.pixel_format);
|
|
|
|
params.width = config.width;
|
|
|
|
params.height = config.height;
|
2018-06-26 21:05:13 +02:00
|
|
|
params.unaligned_height = config.height;
|
2018-09-01 08:42:43 +02:00
|
|
|
params.target = SurfaceTarget::Texture2D;
|
2018-09-10 05:42:57 +02:00
|
|
|
params.depth = 1;
|
2018-10-25 00:30:27 +02:00
|
|
|
params.max_mip_level = 1;
|
2018-10-18 21:04:33 +02:00
|
|
|
params.is_layered = false;
|
2018-09-21 05:41:25 +02:00
|
|
|
|
|
|
|
// Render target specific parameters, not used for caching
|
|
|
|
params.rt.index = static_cast<u32>(index);
|
|
|
|
params.rt.array_mode = config.array_mode;
|
|
|
|
params.rt.layer_stride = config.layer_stride;
|
2018-10-16 03:04:20 +02:00
|
|
|
params.rt.volume = config.volume;
|
2018-09-21 05:41:25 +02:00
|
|
|
params.rt.base_layer = config.base_layer;
|
|
|
|
|
2018-10-13 04:24:40 +02:00
|
|
|
params.InitCacheParameters(config.Address());
|
|
|
|
|
2018-06-26 20:59:45 +02:00
|
|
|
return params;
|
|
|
|
}
|
2018-06-24 15:50:08 +02:00
|
|
|
|
2018-10-10 04:28:19 +02:00
|
|
|
/*static*/ SurfaceParams 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
|
|
|
SurfaceParams params{};
|
2018-10-13 04:24:40 +02:00
|
|
|
|
2018-10-10 04:28:19 +02:00
|
|
|
params.is_tiled = type == Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout::BlockLinear;
|
2018-10-08 20:34:55 +02:00
|
|
|
params.block_width = 1 << std::min(block_width, 5U);
|
|
|
|
params.block_height = 1 << std::min(block_height, 5U);
|
|
|
|
params.block_depth = 1 << std::min(block_depth, 5U);
|
2018-11-16 18:01:54 +01:00
|
|
|
params.tile_width_spacing = 1;
|
2018-07-04 05:32:59 +02:00
|
|
|
params.pixel_format = PixelFormatFromDepthFormat(format);
|
|
|
|
params.component_type = ComponentTypeFromDepthFormat(format);
|
|
|
|
params.type = GetFormatType(params.pixel_format);
|
2018-10-24 22:09:40 +02:00
|
|
|
params.srgb_conversion = false;
|
2018-07-21 20:29:36 +02:00
|
|
|
params.width = zeta_width;
|
|
|
|
params.height = zeta_height;
|
|
|
|
params.unaligned_height = zeta_height;
|
2018-09-01 08:42:43 +02:00
|
|
|
params.target = SurfaceTarget::Texture2D;
|
2018-09-10 05:42:57 +02:00
|
|
|
params.depth = 1;
|
2018-10-25 00:30:27 +02:00
|
|
|
params.max_mip_level = 1;
|
2018-10-18 21:04:33 +02:00
|
|
|
params.is_layered = false;
|
2018-09-21 05:41:25 +02:00
|
|
|
params.rt = {};
|
|
|
|
|
2018-10-13 04:24:40 +02:00
|
|
|
params.InitCacheParameters(zeta_address);
|
|
|
|
|
2018-07-04 05:32:59 +02:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2018-10-06 05:39:03 +02:00
|
|
|
/*static*/ SurfaceParams SurfaceParams::CreateForFermiCopySurface(
|
|
|
|
const Tegra::Engines::Fermi2D::Regs::Surface& config) {
|
|
|
|
SurfaceParams params{};
|
2018-10-13 04:24:40 +02:00
|
|
|
|
2018-10-06 05:39:03 +02:00
|
|
|
params.is_tiled = !config.linear;
|
2018-10-10 04:28:19 +02:00
|
|
|
params.block_width = params.is_tiled ? std::min(config.BlockWidth(), 32U) : 0,
|
|
|
|
params.block_height = params.is_tiled ? std::min(config.BlockHeight(), 32U) : 0,
|
|
|
|
params.block_depth = params.is_tiled ? std::min(config.BlockDepth(), 32U) : 0,
|
2018-11-16 18:01:54 +01:00
|
|
|
params.tile_width_spacing = 1;
|
2018-10-06 05:39:03 +02:00
|
|
|
params.pixel_format = PixelFormatFromRenderTargetFormat(config.format);
|
2018-10-24 22:09:40 +02:00
|
|
|
params.srgb_conversion = config.format == Tegra::RenderTargetFormat::BGRA8_SRGB ||
|
|
|
|
config.format == Tegra::RenderTargetFormat::RGBA8_SRGB;
|
2018-10-06 05:39:03 +02:00
|
|
|
params.component_type = ComponentTypeFromRenderTarget(config.format);
|
|
|
|
params.type = GetFormatType(params.pixel_format);
|
|
|
|
params.width = config.width;
|
|
|
|
params.height = config.height;
|
|
|
|
params.unaligned_height = config.height;
|
|
|
|
params.target = SurfaceTarget::Texture2D;
|
|
|
|
params.depth = 1;
|
2018-10-25 00:30:27 +02:00
|
|
|
params.max_mip_level = 1;
|
2018-10-06 05:39:03 +02:00
|
|
|
params.rt = {};
|
|
|
|
|
2018-10-13 04:24:40 +02:00
|
|
|
params.InitCacheParameters(config.Address());
|
|
|
|
|
2018-10-06 05:39:03 +02:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2018-10-29 02:14:25 +01:00
|
|
|
static constexpr std::array<FormatTuple, VideoCore::Surface::MaxPixelFormat> tex_format_tuples = {{
|
2018-08-10 17:44:43 +02:00
|
|
|
{GL_RGBA8, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, ComponentType::UNorm, false}, // ABGR8U
|
|
|
|
{GL_RGBA8, GL_RGBA, GL_BYTE, ComponentType::SNorm, false}, // ABGR8S
|
2018-08-20 14:26:54 +02:00
|
|
|
{GL_RGBA8UI, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, ComponentType::UInt, false}, // ABGR8UI
|
2018-09-13 00:09:31 +02:00
|
|
|
{GL_RGB8, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, ComponentType::UNorm, false}, // B5G6R5U
|
2018-06-30 21:08:51 +02:00
|
|
|
{GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, ComponentType::UNorm,
|
2018-08-15 05:11:27 +02:00
|
|
|
false}, // A2B10G10R10U
|
|
|
|
{GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, ComponentType::UNorm, false}, // A1B5G5R5U
|
|
|
|
{GL_R8, GL_RED, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // R8U
|
2018-08-12 03:44:42 +02:00
|
|
|
{GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE, ComponentType::UInt, false}, // R8UI
|
2018-06-30 21:08:51 +02:00
|
|
|
{GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT, ComponentType::Float, false}, // RGBA16F
|
2018-08-13 06:34:20 +02:00
|
|
|
{GL_RGBA16, GL_RGBA, GL_UNSIGNED_SHORT, ComponentType::UNorm, false}, // RGBA16U
|
2018-08-13 06:04:52 +02:00
|
|
|
{GL_RGBA16UI, GL_RGBA, GL_UNSIGNED_SHORT, ComponentType::UInt, false}, // RGBA16UI
|
2018-06-30 21:08:51 +02:00
|
|
|
{GL_R11F_G11F_B10F, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, ComponentType::Float,
|
2018-06-30 21:23:13 +02:00
|
|
|
false}, // R11FG11FB10F
|
|
|
|
{GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT, ComponentType::UInt, false}, // RGBA32UI
|
2018-10-28 22:11:04 +01:00
|
|
|
{GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, ComponentType::UNorm,
|
2018-06-30 21:08:51 +02:00
|
|
|
true}, // DXT1
|
|
|
|
{GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, ComponentType::UNorm,
|
|
|
|
true}, // DXT23
|
|
|
|
{GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, ComponentType::UNorm,
|
|
|
|
true}, // DXT45
|
|
|
|
{GL_COMPRESSED_RED_RGTC1, GL_RED, GL_UNSIGNED_INT_8_8_8_8, ComponentType::UNorm, true}, // DXN1
|
2018-08-10 01:15:32 +02:00
|
|
|
{GL_COMPRESSED_RG_RGTC2, GL_RG, GL_UNSIGNED_INT_8_8_8_8, ComponentType::UNorm,
|
|
|
|
true}, // DXN2UNORM
|
|
|
|
{GL_COMPRESSED_SIGNED_RG_RGTC2, GL_RG, GL_INT, ComponentType::SNorm, true}, // DXN2SNORM
|
2018-11-18 01:28:14 +01:00
|
|
|
{GL_COMPRESSED_RGBA_BPTC_UNORM, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, ComponentType::UNorm,
|
2018-08-31 18:11:19 +02:00
|
|
|
true}, // BC7U
|
2018-11-18 01:28:14 +01:00
|
|
|
{GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT, GL_RGB, GL_UNSIGNED_INT_8_8_8_8, ComponentType::Float,
|
|
|
|
true}, // BC6H_UF16
|
|
|
|
{GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT, GL_RGB, GL_UNSIGNED_INT_8_8_8_8, ComponentType::Float,
|
2018-08-31 18:11:19 +02:00
|
|
|
true}, // BC6H_SF16
|
2018-07-26 02:01:29 +02:00
|
|
|
{GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_4X4
|
2018-08-15 02:31:19 +02:00
|
|
|
{GL_RG8, GL_RG, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // G8R8U
|
|
|
|
{GL_RG8, GL_RG, GL_BYTE, ComponentType::SNorm, false}, // G8R8S
|
2018-07-26 02:01:29 +02:00
|
|
|
{GL_RGBA8, GL_BGRA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // BGRA8
|
|
|
|
{GL_RGBA32F, GL_RGBA, GL_FLOAT, ComponentType::Float, false}, // RGBA32F
|
|
|
|
{GL_RG32F, GL_RG, GL_FLOAT, ComponentType::Float, false}, // RG32F
|
|
|
|
{GL_R32F, GL_RED, GL_FLOAT, ComponentType::Float, false}, // R32F
|
|
|
|
{GL_R16F, GL_RED, GL_HALF_FLOAT, ComponentType::Float, false}, // R16F
|
2018-08-15 05:11:27 +02:00
|
|
|
{GL_R16, GL_RED, GL_UNSIGNED_SHORT, ComponentType::UNorm, false}, // R16U
|
2018-08-11 20:01:50 +02:00
|
|
|
{GL_R16_SNORM, GL_RED, GL_SHORT, ComponentType::SNorm, false}, // R16S
|
|
|
|
{GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT, ComponentType::UInt, false}, // R16UI
|
|
|
|
{GL_R16I, GL_RED_INTEGER, GL_SHORT, ComponentType::SInt, false}, // R16I
|
2018-07-26 02:01:29 +02:00
|
|
|
{GL_RG16, GL_RG, GL_UNSIGNED_SHORT, ComponentType::UNorm, false}, // RG16
|
|
|
|
{GL_RG16F, GL_RG, GL_HALF_FLOAT, ComponentType::Float, false}, // RG16F
|
|
|
|
{GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT, ComponentType::UInt, false}, // RG16UI
|
|
|
|
{GL_RG16I, GL_RG_INTEGER, GL_SHORT, ComponentType::SInt, false}, // RG16I
|
|
|
|
{GL_RG16_SNORM, GL_RG, GL_SHORT, ComponentType::SNorm, false}, // RG16S
|
2018-08-02 20:56:38 +02:00
|
|
|
{GL_RGB32F, GL_RGB, GL_FLOAT, ComponentType::Float, false}, // RGB32F
|
2018-10-24 22:09:40 +02:00
|
|
|
{GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, ComponentType::UNorm,
|
|
|
|
false}, // RGBA8_SRGB
|
|
|
|
{GL_RG8, GL_RG, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // RG8U
|
|
|
|
{GL_RG8, GL_RG, GL_BYTE, ComponentType::SNorm, false}, // RG8S
|
|
|
|
{GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT, ComponentType::UInt, false}, // RG32UI
|
|
|
|
{GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT, ComponentType::UInt, false}, // R32UI
|
|
|
|
{GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_8X8
|
|
|
|
{GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_8X5
|
|
|
|
{GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_5X4
|
|
|
|
{GL_SRGB8_ALPHA8, GL_BGRA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // BGRA8
|
|
|
|
// Compressed sRGB formats
|
2018-10-28 22:11:04 +01:00
|
|
|
{GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, ComponentType::UNorm,
|
2018-10-24 22:09:40 +02:00
|
|
|
true}, // DXT1_SRGB
|
|
|
|
{GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, ComponentType::UNorm,
|
|
|
|
true}, // DXT23_SRGB
|
|
|
|
{GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, ComponentType::UNorm,
|
|
|
|
true}, // DXT45_SRGB
|
2018-11-18 01:28:14 +01:00
|
|
|
{GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, ComponentType::UNorm,
|
|
|
|
true}, // BC7U_SRGB
|
2018-10-24 22:09:40 +02:00
|
|
|
{GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_4X4_SRGB
|
|
|
|
{GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_8X8_SRGB
|
|
|
|
{GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_8X5_SRGB
|
|
|
|
{GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_5X4_SRGB
|
2018-10-29 23:23:32 +01:00
|
|
|
{GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_5X5
|
|
|
|
{GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_5X5_SRGB
|
2018-11-13 03:34:54 +01:00
|
|
|
{GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_10X8
|
|
|
|
{GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_10X8_SRGB
|
2018-07-02 19:42:04 +02:00
|
|
|
|
2018-08-16 03:24:04 +02:00
|
|
|
// Depth formats
|
|
|
|
{GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, ComponentType::Float, false}, // Z32F
|
|
|
|
{GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, ComponentType::UNorm,
|
|
|
|
false}, // Z16
|
|
|
|
|
2018-07-02 19:42:04 +02:00
|
|
|
// DepthStencil formats
|
|
|
|
{GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, ComponentType::UNorm,
|
|
|
|
false}, // Z24S8
|
2018-07-03 20:05:13 +02:00
|
|
|
{GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, ComponentType::UNorm,
|
2018-08-16 03:24:04 +02:00
|
|
|
false}, // S8Z24
|
2018-07-25 03:41:40 +02:00
|
|
|
{GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV,
|
|
|
|
ComponentType::Float, false}, // Z32FS8
|
2018-03-19 22:45:22 +01:00
|
|
|
}};
|
|
|
|
|
2018-10-29 02:14:25 +01:00
|
|
|
static GLenum SurfaceTargetToGL(SurfaceTarget target) {
|
2018-09-01 08:42:43 +02:00
|
|
|
switch (target) {
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture1D:
|
2018-09-01 08:42:43 +02:00
|
|
|
return GL_TEXTURE_1D;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture2D:
|
2018-09-01 08:42:43 +02:00
|
|
|
return GL_TEXTURE_2D;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture3D:
|
2018-09-01 08:42:43 +02:00
|
|
|
return GL_TEXTURE_3D;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture1DArray:
|
2018-09-01 08:42:43 +02:00
|
|
|
return GL_TEXTURE_1D_ARRAY;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture2DArray:
|
2018-09-01 08:42:43 +02:00
|
|
|
return GL_TEXTURE_2D_ARRAY;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::TextureCubemap:
|
2018-09-01 08:42:43 +02:00
|
|
|
return GL_TEXTURE_CUBE_MAP;
|
2018-10-19 02:04:43 +02:00
|
|
|
case SurfaceTarget::TextureCubeArray:
|
2018-11-18 01:28:14 +01:00
|
|
|
return GL_TEXTURE_CUBE_MAP_ARRAY;
|
2018-09-01 08:42:43 +02:00
|
|
|
}
|
|
|
|
LOG_CRITICAL(Render_OpenGL, "Unimplemented texture target={}", static_cast<u32>(target));
|
|
|
|
UNREACHABLE();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-04-18 21:17:05 +02:00
|
|
|
static const FormatTuple& GetFormatTuple(PixelFormat pixel_format, ComponentType component_type) {
|
2018-09-15 15:21:06 +02:00
|
|
|
ASSERT(static_cast<std::size_t>(pixel_format) < tex_format_tuples.size());
|
2018-07-02 19:42:04 +02:00
|
|
|
auto& format = tex_format_tuples[static_cast<unsigned int>(pixel_format)];
|
|
|
|
ASSERT(component_type == format.component_type);
|
2018-03-27 04:48:05 +02:00
|
|
|
|
2018-07-02 19:42:04 +02:00
|
|
|
return format;
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
2018-10-25 00:30:27 +02:00
|
|
|
MathUtil::Rectangle<u32> SurfaceParams::GetRect(u32 mip_level) const {
|
|
|
|
u32 actual_height{std::max(1U, unaligned_height >> mip_level)};
|
2018-06-26 21:05:13 +02:00
|
|
|
if (IsPixelFormatASTC(pixel_format)) {
|
|
|
|
// ASTC formats must stop at the ATSC block size boundary
|
|
|
|
actual_height = Common::AlignDown(actual_height, GetASTCBlockSize(pixel_format).second);
|
|
|
|
}
|
2018-10-25 00:30:27 +02:00
|
|
|
return {0, actual_height, MipWidth(mip_level), 0};
|
2018-06-26 21:05:13 +02:00
|
|
|
}
|
|
|
|
|
2018-11-25 03:53:17 +01:00
|
|
|
void SwizzleFunc(const MortonSwizzleMode& mode, const SurfaceParams& params,
|
2018-10-25 00:30:27 +02:00
|
|
|
std::vector<u8>& gl_buffer, u32 mip_level) {
|
|
|
|
u32 depth = params.MipDepth(mip_level);
|
2018-10-29 02:14:25 +01:00
|
|
|
if (params.target == SurfaceTarget::Texture2D) {
|
2018-10-18 21:04:33 +02:00
|
|
|
// TODO(Blinkhawk): Eliminate this condition once all texture types are implemented.
|
|
|
|
depth = 1U;
|
|
|
|
}
|
|
|
|
if (params.is_layered) {
|
2018-10-25 00:30:27 +02:00
|
|
|
u64 offset = params.GetMipmapLevelOffset(mip_level);
|
2018-10-18 21:04:33 +02:00
|
|
|
u64 offset_gl = 0;
|
2018-11-15 20:10:05 +01:00
|
|
|
const u64 layer_size = params.LayerMemorySize();
|
|
|
|
const u64 gl_size = params.LayerSizeGL(mip_level);
|
2018-10-25 00:30:27 +02:00
|
|
|
for (u32 i = 0; i < params.depth; i++) {
|
2018-11-25 03:53:17 +01:00
|
|
|
MortonSwizzle(mode, params.pixel_format, params.MipWidth(mip_level),
|
|
|
|
params.MipBlockHeight(mip_level), params.MipHeight(mip_level),
|
2018-11-16 18:01:54 +01:00
|
|
|
params.MipBlockDepth(mip_level), params.tile_width_spacing, 1,
|
|
|
|
gl_buffer.data() + offset_gl, gl_size, params.addr + offset);
|
2018-10-18 21:04:33 +02:00
|
|
|
offset += layer_size;
|
|
|
|
offset_gl += gl_size;
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-15 20:10:05 +01:00
|
|
|
const u64 offset = params.GetMipmapLevelOffset(mip_level);
|
2018-11-25 03:53:17 +01:00
|
|
|
MortonSwizzle(mode, params.pixel_format, params.MipWidth(mip_level),
|
|
|
|
params.MipBlockHeight(mip_level), params.MipHeight(mip_level),
|
2018-11-16 18:01:54 +01:00
|
|
|
params.MipBlockDepth(mip_level), depth, params.tile_width_spacing,
|
|
|
|
gl_buffer.data(), gl_buffer.size(), params.addr + offset);
|
2018-10-18 21:04:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-06 05:09:01 +02:00
|
|
|
static void FastCopySurface(const Surface& src_surface, const Surface& dst_surface) {
|
|
|
|
const auto& src_params{src_surface->GetSurfaceParams()};
|
|
|
|
const auto& dst_params{dst_surface->GetSurfaceParams()};
|
|
|
|
|
|
|
|
const u32 width{std::min(src_params.width, dst_params.width)};
|
|
|
|
const u32 height{std::min(src_params.height, dst_params.height)};
|
|
|
|
|
|
|
|
glCopyImageSubData(src_surface->Texture().handle, SurfaceTargetToGL(src_params.target), 0, 0, 0,
|
|
|
|
0, dst_surface->Texture().handle, SurfaceTargetToGL(dst_params.target), 0, 0,
|
|
|
|
0, 0, width, height, 1);
|
|
|
|
}
|
|
|
|
|
2018-11-06 13:25:01 +01:00
|
|
|
MICROPROFILE_DEFINE(OpenGL_CopySurface, "OpenGL", "CopySurface", MP_RGB(128, 192, 64));
|
2018-09-21 05:41:25 +02:00
|
|
|
static void CopySurface(const Surface& src_surface, const Surface& dst_surface,
|
2018-11-08 13:58:53 +01:00
|
|
|
const GLuint copy_pbo_handle, const GLenum src_attachment = 0,
|
|
|
|
const GLenum dst_attachment = 0, const std::size_t cubemap_face = 0) {
|
2018-11-06 13:25:01 +01:00
|
|
|
MICROPROFILE_SCOPE(OpenGL_CopySurface);
|
2018-09-21 05:41:25 +02:00
|
|
|
ASSERT_MSG(dst_attachment == 0, "Unimplemented");
|
|
|
|
|
|
|
|
const auto& src_params{src_surface->GetSurfaceParams()};
|
|
|
|
const auto& dst_params{dst_surface->GetSurfaceParams()};
|
|
|
|
|
2018-11-08 13:58:53 +01:00
|
|
|
const auto source_format = GetFormatTuple(src_params.pixel_format, src_params.component_type);
|
|
|
|
const auto dest_format = GetFormatTuple(dst_params.pixel_format, dst_params.component_type);
|
2018-09-21 05:41:25 +02:00
|
|
|
|
2018-11-08 13:58:53 +01:00
|
|
|
const std::size_t buffer_size = std::max(src_params.size_in_bytes, dst_params.size_in_bytes);
|
2018-09-21 05:41:25 +02:00
|
|
|
|
|
|
|
glBindBuffer(GL_PIXEL_PACK_BUFFER, copy_pbo_handle);
|
2018-11-18 01:28:14 +01:00
|
|
|
glBufferData(GL_PIXEL_PACK_BUFFER, buffer_size, nullptr, GL_STREAM_DRAW);
|
2018-09-21 05:41:25 +02:00
|
|
|
if (source_format.compressed) {
|
|
|
|
glGetCompressedTextureImage(src_surface->Texture().handle, src_attachment,
|
2018-10-13 08:08:04 +02:00
|
|
|
static_cast<GLsizei>(src_params.size_in_bytes), nullptr);
|
2018-09-21 05:41:25 +02:00
|
|
|
} else {
|
|
|
|
glGetTextureImage(src_surface->Texture().handle, src_attachment, source_format.format,
|
2018-10-13 08:08:04 +02:00
|
|
|
source_format.type, static_cast<GLsizei>(src_params.size_in_bytes),
|
2018-09-21 05:41:25 +02:00
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
// If the new texture is bigger than the previous one, we need to fill in the rest with data
|
|
|
|
// from the CPU.
|
2018-10-13 08:08:04 +02:00
|
|
|
if (src_params.size_in_bytes < dst_params.size_in_bytes) {
|
2018-09-21 05:41:25 +02:00
|
|
|
// Upload the rest of the memory.
|
|
|
|
if (dst_params.is_tiled) {
|
|
|
|
// TODO(Subv): We might have to de-tile the subtexture and re-tile it with the rest
|
|
|
|
// of the data in this case. Games like Super Mario Odyssey seem to hit this case
|
|
|
|
// when drawing, it re-uses the memory of a previous texture as a bigger framebuffer
|
|
|
|
// but it doesn't clear it beforehand, the texture is already full of zeros.
|
|
|
|
LOG_DEBUG(HW_GPU, "Trying to upload extra texture data from the CPU during "
|
|
|
|
"reinterpretation but the texture is tiled.");
|
|
|
|
}
|
2018-11-08 13:58:53 +01:00
|
|
|
const std::size_t remaining_size = dst_params.size_in_bytes - src_params.size_in_bytes;
|
2018-10-13 04:28:02 +02:00
|
|
|
|
2018-10-13 08:08:04 +02:00
|
|
|
glBufferSubData(GL_PIXEL_PACK_BUFFER, src_params.size_in_bytes, remaining_size,
|
2018-11-08 13:58:53 +01:00
|
|
|
Memory::GetPointer(dst_params.addr + src_params.size_in_bytes));
|
2018-09-21 05:41:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
|
|
|
|
|
|
|
|
const GLsizei width{static_cast<GLsizei>(
|
|
|
|
std::min(src_params.GetRect().GetWidth(), dst_params.GetRect().GetWidth()))};
|
|
|
|
const GLsizei height{static_cast<GLsizei>(
|
|
|
|
std::min(src_params.GetRect().GetHeight(), dst_params.GetRect().GetHeight()))};
|
|
|
|
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, copy_pbo_handle);
|
|
|
|
if (dest_format.compressed) {
|
|
|
|
LOG_CRITICAL(HW_GPU, "Compressed copy is unimplemented!");
|
|
|
|
UNREACHABLE();
|
|
|
|
} else {
|
|
|
|
switch (dst_params.target) {
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture1D:
|
2018-09-21 05:41:25 +02:00
|
|
|
glTextureSubImage1D(dst_surface->Texture().handle, 0, 0, width, dest_format.format,
|
|
|
|
dest_format.type, nullptr);
|
|
|
|
break;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture2D:
|
2018-09-21 05:41:25 +02:00
|
|
|
glTextureSubImage2D(dst_surface->Texture().handle, 0, 0, 0, width, height,
|
|
|
|
dest_format.format, dest_format.type, nullptr);
|
|
|
|
break;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture3D:
|
|
|
|
case SurfaceTarget::Texture2DArray:
|
2018-10-19 02:04:43 +02:00
|
|
|
case SurfaceTarget::TextureCubeArray:
|
2018-09-21 05:41:25 +02:00
|
|
|
glTextureSubImage3D(dst_surface->Texture().handle, 0, 0, 0, 0, width, height,
|
|
|
|
static_cast<GLsizei>(dst_params.depth), dest_format.format,
|
|
|
|
dest_format.type, nullptr);
|
|
|
|
break;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::TextureCubemap:
|
2018-09-21 05:41:25 +02:00
|
|
|
glTextureSubImage3D(dst_surface->Texture().handle, 0, 0, 0,
|
|
|
|
static_cast<GLint>(cubemap_face), width, height, 1,
|
|
|
|
dest_format.format, dest_format.type, nullptr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_CRITICAL(Render_OpenGL, "Unimplemented surface target={}",
|
|
|
|
static_cast<u32>(dst_params.target));
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-01 08:42:43 +02:00
|
|
|
CachedSurface::CachedSurface(const SurfaceParams& params)
|
2018-10-16 03:24:34 +02:00
|
|
|
: params(params), gl_target(SurfaceTargetToGL(params.target)),
|
|
|
|
cached_size_in_bytes(params.size_in_bytes) {
|
2018-06-22 01:36:01 +02:00
|
|
|
texture.Create();
|
2018-06-26 21:05:13 +02:00
|
|
|
const auto& rect{params.GetRect()};
|
2018-09-01 08:42:43 +02:00
|
|
|
|
2018-09-07 01:08:45 +02:00
|
|
|
// Keep track of previous texture bindings
|
2018-09-01 08:42:43 +02:00
|
|
|
OpenGLState cur_state = OpenGLState::GetCurState();
|
2018-09-07 01:08:45 +02:00
|
|
|
const auto& old_tex = cur_state.texture_units[0];
|
|
|
|
SCOPE_EXIT({
|
|
|
|
cur_state.texture_units[0] = old_tex;
|
|
|
|
cur_state.Apply();
|
|
|
|
});
|
2018-09-01 08:42:43 +02:00
|
|
|
|
|
|
|
cur_state.texture_units[0].texture = texture.handle;
|
2018-09-07 01:08:45 +02:00
|
|
|
cur_state.texture_units[0].target = SurfaceTargetToGL(params.target);
|
2018-09-01 08:42:43 +02:00
|
|
|
cur_state.Apply();
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
|
|
|
|
const auto& format_tuple = GetFormatTuple(params.pixel_format, params.component_type);
|
|
|
|
if (!format_tuple.compressed) {
|
|
|
|
// Only pre-create the texture for non-compressed textures.
|
2018-09-07 01:08:45 +02:00
|
|
|
switch (params.target) {
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture1D:
|
2018-10-25 00:30:27 +02:00
|
|
|
glTexStorage1D(SurfaceTargetToGL(params.target), params.max_mip_level,
|
|
|
|
format_tuple.internal_format, rect.GetWidth());
|
2018-09-07 01:08:45 +02:00
|
|
|
break;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture2D:
|
|
|
|
case SurfaceTarget::TextureCubemap:
|
2018-10-25 00:30:27 +02:00
|
|
|
glTexStorage2D(SurfaceTargetToGL(params.target), params.max_mip_level,
|
|
|
|
format_tuple.internal_format, rect.GetWidth(), rect.GetHeight());
|
2018-09-07 01:08:45 +02:00
|
|
|
break;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture3D:
|
|
|
|
case SurfaceTarget::Texture2DArray:
|
2018-10-19 02:04:43 +02:00
|
|
|
case SurfaceTarget::TextureCubeArray:
|
2018-10-25 00:30:27 +02:00
|
|
|
glTexStorage3D(SurfaceTargetToGL(params.target), params.max_mip_level,
|
|
|
|
format_tuple.internal_format, rect.GetWidth(), rect.GetHeight(),
|
|
|
|
params.depth);
|
2018-09-07 01:08:45 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_CRITICAL(Render_OpenGL, "Unimplemented surface target={}",
|
|
|
|
static_cast<u32>(params.target));
|
|
|
|
UNREACHABLE();
|
2018-10-25 00:30:27 +02:00
|
|
|
glTexStorage2D(GL_TEXTURE_2D, params.max_mip_level, format_tuple.internal_format,
|
|
|
|
rect.GetWidth(), rect.GetHeight());
|
2018-09-07 01:08:45 +02:00
|
|
|
}
|
2018-09-01 08:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
2018-10-25 00:30:27 +02:00
|
|
|
glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
2018-09-01 08:42:43 +02:00
|
|
|
glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
2018-10-28 23:58:07 +01:00
|
|
|
glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_MAX_LEVEL,
|
|
|
|
params.max_mip_level - 1);
|
2018-10-25 18:24:10 +02:00
|
|
|
if (params.max_mip_level == 1) {
|
|
|
|
glTexParameterf(SurfaceTargetToGL(params.target), GL_TEXTURE_LOD_BIAS, 1000.0);
|
|
|
|
}
|
2018-09-23 23:55:41 +02:00
|
|
|
|
2018-10-29 01:54:08 +01:00
|
|
|
LabelGLObject(GL_TEXTURE, texture.handle, params.addr,
|
|
|
|
SurfaceParams::SurfaceTargetName(params.target));
|
2018-10-16 03:24:34 +02:00
|
|
|
|
|
|
|
// Clamp size to mapped GPU memory region
|
|
|
|
// TODO(bunnei): Super Mario Odyssey maps a 0x40000 byte region and then uses it for a 0x80000
|
|
|
|
// R32F render buffer. We do not yet know if this is a game bug or something else, but this
|
|
|
|
// check is necessary to prevent flushing from overwriting unmapped memory.
|
|
|
|
|
|
|
|
auto& memory_manager{Core::System::GetInstance().GPU().MemoryManager()};
|
|
|
|
const u64 max_size{memory_manager.GetRegionEnd(params.gpu_addr) - params.gpu_addr};
|
|
|
|
if (cached_size_in_bytes > max_size) {
|
|
|
|
LOG_ERROR(HW_GPU, "Surface size {} exceeds region size {}", params.size_in_bytes, max_size);
|
|
|
|
cached_size_in_bytes = max_size;
|
|
|
|
}
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
2018-10-10 01:15:05 +02:00
|
|
|
static void ConvertS8Z24ToZ24S8(std::vector<u8>& data, u32 width, u32 height, bool reverse) {
|
2018-07-03 20:05:13 +02:00
|
|
|
union S8Z24 {
|
|
|
|
BitField<0, 24, u32> z24;
|
|
|
|
BitField<24, 8, u32> s8;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(S8Z24) == 4, "S8Z24 is incorrect size");
|
|
|
|
|
|
|
|
union Z24S8 {
|
|
|
|
BitField<0, 8, u32> s8;
|
|
|
|
BitField<8, 24, u32> z24;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(Z24S8) == 4, "Z24S8 is incorrect size");
|
|
|
|
|
2018-10-10 01:15:05 +02:00
|
|
|
S8Z24 s8z24_pixel{};
|
|
|
|
Z24S8 z24s8_pixel{};
|
2018-10-29 02:14:25 +01:00
|
|
|
constexpr auto bpp{GetBytesPerPixel(PixelFormat::S8Z24)};
|
2018-09-15 15:21:06 +02:00
|
|
|
for (std::size_t y = 0; y < height; ++y) {
|
|
|
|
for (std::size_t x = 0; x < width; ++x) {
|
|
|
|
const std::size_t offset{bpp * (y * width + x)};
|
2018-10-10 01:15:05 +02:00
|
|
|
if (reverse) {
|
|
|
|
std::memcpy(&z24s8_pixel, &data[offset], sizeof(Z24S8));
|
|
|
|
s8z24_pixel.s8.Assign(z24s8_pixel.s8);
|
|
|
|
s8z24_pixel.z24.Assign(z24s8_pixel.z24);
|
|
|
|
std::memcpy(&data[offset], &s8z24_pixel, sizeof(S8Z24));
|
|
|
|
} else {
|
|
|
|
std::memcpy(&s8z24_pixel, &data[offset], sizeof(S8Z24));
|
|
|
|
z24s8_pixel.s8.Assign(s8z24_pixel.s8);
|
|
|
|
z24s8_pixel.z24.Assign(s8z24_pixel.z24);
|
|
|
|
std::memcpy(&data[offset], &z24s8_pixel, sizeof(Z24S8));
|
|
|
|
}
|
2018-07-03 20:05:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-15 07:33:42 +02:00
|
|
|
|
|
|
|
static void ConvertG8R8ToR8G8(std::vector<u8>& data, u32 width, u32 height) {
|
2018-10-29 02:14:25 +01:00
|
|
|
constexpr auto bpp{GetBytesPerPixel(PixelFormat::G8R8U)};
|
2018-09-15 15:21:06 +02:00
|
|
|
for (std::size_t y = 0; y < height; ++y) {
|
|
|
|
for (std::size_t x = 0; x < width; ++x) {
|
|
|
|
const std::size_t offset{bpp * (y * width + x)};
|
2018-07-15 07:33:42 +02:00
|
|
|
const u8 temp{data[offset]};
|
|
|
|
data[offset] = data[offset + 1];
|
|
|
|
data[offset + 1] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 20:05:13 +02:00
|
|
|
/**
|
|
|
|
* Helper function to perform software conversion (as needed) when loading a buffer from Switch
|
|
|
|
* memory. This is for Maxwell pixel formats that cannot be represented as-is in OpenGL or with
|
|
|
|
* typical desktop GPUs.
|
|
|
|
*/
|
|
|
|
static void ConvertFormatAsNeeded_LoadGLBuffer(std::vector<u8>& data, PixelFormat pixel_format,
|
2018-10-30 03:46:09 +01:00
|
|
|
u32 width, u32 height, u32 depth) {
|
2018-07-03 20:05:13 +02:00
|
|
|
switch (pixel_format) {
|
2018-09-16 16:47:02 +02:00
|
|
|
case PixelFormat::ASTC_2D_4X4:
|
2018-10-13 16:10:26 +02:00
|
|
|
case PixelFormat::ASTC_2D_8X8:
|
|
|
|
case PixelFormat::ASTC_2D_8X5:
|
2018-10-24 22:09:40 +02:00
|
|
|
case PixelFormat::ASTC_2D_5X4:
|
2018-10-29 23:23:32 +01:00
|
|
|
case PixelFormat::ASTC_2D_5X5:
|
2018-10-24 22:09:40 +02:00
|
|
|
case PixelFormat::ASTC_2D_4X4_SRGB:
|
|
|
|
case PixelFormat::ASTC_2D_8X8_SRGB:
|
|
|
|
case PixelFormat::ASTC_2D_8X5_SRGB:
|
2018-10-29 23:23:32 +01:00
|
|
|
case PixelFormat::ASTC_2D_5X4_SRGB:
|
2018-11-13 03:34:54 +01:00
|
|
|
case PixelFormat::ASTC_2D_5X5_SRGB:
|
|
|
|
case PixelFormat::ASTC_2D_10X8:
|
|
|
|
case PixelFormat::ASTC_2D_10X8_SRGB: {
|
2018-07-03 20:05:13 +02:00
|
|
|
// Convert ASTC pixel formats to RGBA8, as most desktop GPUs do not support ASTC.
|
|
|
|
u32 block_width{};
|
|
|
|
u32 block_height{};
|
|
|
|
std::tie(block_width, block_height) = GetASTCBlockSize(pixel_format);
|
2018-10-30 03:46:09 +01:00
|
|
|
data =
|
|
|
|
Tegra::Texture::ASTC::Decompress(data, width, height, depth, block_width, block_height);
|
2018-07-03 20:05:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PixelFormat::S8Z24:
|
|
|
|
// Convert the S8Z24 depth format to Z24S8, as OpenGL does not support S8Z24.
|
2018-10-10 01:15:05 +02:00
|
|
|
ConvertS8Z24ToZ24S8(data, width, height, false);
|
2018-07-03 20:05:13 +02:00
|
|
|
break;
|
2018-07-15 07:33:42 +02:00
|
|
|
|
2018-08-15 02:31:19 +02:00
|
|
|
case PixelFormat::G8R8U:
|
|
|
|
case PixelFormat::G8R8S:
|
2018-07-15 07:33:42 +02:00
|
|
|
// Convert the G8R8 color format to R8G8, as OpenGL does not support G8R8.
|
|
|
|
ConvertG8R8ToR8G8(data, width, height);
|
|
|
|
break;
|
2018-07-03 20:05:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 01:15:05 +02:00
|
|
|
/**
|
|
|
|
* Helper function to perform software conversion (as needed) when flushing a buffer from OpenGL to
|
|
|
|
* Switch memory. This is for Maxwell pixel formats that cannot be represented as-is in OpenGL or
|
|
|
|
* with typical desktop GPUs.
|
|
|
|
*/
|
|
|
|
static void ConvertFormatAsNeeded_FlushGLBuffer(std::vector<u8>& data, PixelFormat pixel_format,
|
|
|
|
u32 width, u32 height) {
|
|
|
|
switch (pixel_format) {
|
|
|
|
case PixelFormat::G8R8U:
|
|
|
|
case PixelFormat::G8R8S:
|
|
|
|
case PixelFormat::ASTC_2D_4X4:
|
2018-10-24 22:09:40 +02:00
|
|
|
case PixelFormat::ASTC_2D_8X8:
|
|
|
|
case PixelFormat::ASTC_2D_4X4_SRGB:
|
2018-11-13 03:34:54 +01:00
|
|
|
case PixelFormat::ASTC_2D_8X8_SRGB:
|
|
|
|
case PixelFormat::ASTC_2D_5X5:
|
|
|
|
case PixelFormat::ASTC_2D_5X5_SRGB:
|
|
|
|
case PixelFormat::ASTC_2D_10X8:
|
|
|
|
case PixelFormat::ASTC_2D_10X8_SRGB: {
|
2018-10-10 01:15:05 +02:00
|
|
|
LOG_CRITICAL(HW_GPU, "Conversion of format {} after texture flushing is not implemented",
|
|
|
|
static_cast<u32>(pixel_format));
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PixelFormat::S8Z24:
|
|
|
|
// Convert the Z24S8 depth format to S8Z24, as OpenGL does not support S8Z24.
|
|
|
|
ConvertS8Z24ToZ24S8(data, width, height, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 13:25:01 +01:00
|
|
|
MICROPROFILE_DEFINE(OpenGL_SurfaceLoad, "OpenGL", "Surface Load", MP_RGB(128, 192, 64));
|
2018-06-22 01:36:01 +02:00
|
|
|
void CachedSurface::LoadGLBuffer() {
|
2018-03-19 22:45:22 +01:00
|
|
|
MICROPROFILE_SCOPE(OpenGL_SurfaceLoad);
|
2018-10-25 00:30:27 +02:00
|
|
|
gl_buffer.resize(params.max_mip_level);
|
|
|
|
for (u32 i = 0; i < params.max_mip_level; i++)
|
|
|
|
gl_buffer[i].resize(params.GetMipmapSizeGL(i));
|
2018-08-09 05:30:53 +02:00
|
|
|
if (params.is_tiled) {
|
2018-10-08 20:34:55 +02:00
|
|
|
ASSERT_MSG(params.block_width == 1, "Block width is defined as {} on texture type {}",
|
|
|
|
params.block_width, static_cast<u32>(params.target));
|
2018-10-25 00:30:27 +02:00
|
|
|
for (u32 i = 0; i < params.max_mip_level; i++)
|
2018-11-25 03:53:17 +01:00
|
|
|
SwizzleFunc(MortonSwizzleMode::MortonToLinear, params, gl_buffer[i], i);
|
2018-08-09 05:30:53 +02:00
|
|
|
} else {
|
2018-10-13 08:08:04 +02:00
|
|
|
const auto texture_src_data{Memory::GetPointer(params.addr)};
|
|
|
|
const auto texture_src_data_end{texture_src_data + params.size_in_bytes_gl};
|
2018-10-25 00:30:27 +02:00
|
|
|
gl_buffer[0].assign(texture_src_data, texture_src_data_end);
|
2018-06-26 21:05:13 +02:00
|
|
|
}
|
2018-10-25 00:30:27 +02:00
|
|
|
for (u32 i = 0; i < params.max_mip_level; i++)
|
|
|
|
ConvertFormatAsNeeded_LoadGLBuffer(gl_buffer[i], params.pixel_format, params.MipWidth(i),
|
2018-10-30 03:46:09 +01:00
|
|
|
params.MipHeight(i), params.MipDepth(i));
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MICROPROFILE_DEFINE(OpenGL_SurfaceFlush, "OpenGL", "Surface Flush", MP_RGB(128, 192, 64));
|
2018-06-22 01:36:01 +02:00
|
|
|
void CachedSurface::FlushGLBuffer() {
|
2018-10-10 01:15:05 +02:00
|
|
|
MICROPROFILE_SCOPE(OpenGL_SurfaceFlush);
|
2018-10-13 04:28:02 +02:00
|
|
|
|
2018-10-13 08:08:04 +02:00
|
|
|
ASSERT_MSG(!IsPixelFormatASTC(params.pixel_format), "Unimplemented");
|
|
|
|
|
|
|
|
// OpenGL temporary buffer needs to be big enough to store raw texture size
|
2018-10-25 00:30:27 +02:00
|
|
|
gl_buffer.resize(1);
|
|
|
|
gl_buffer[0].resize(GetSizeInBytes());
|
2018-10-13 08:08:04 +02:00
|
|
|
|
2018-10-10 01:15:05 +02:00
|
|
|
const FormatTuple& tuple = GetFormatTuple(params.pixel_format, params.component_type);
|
|
|
|
// Ensure no bad interactions with GL_UNPACK_ALIGNMENT
|
2018-10-29 02:14:25 +01:00
|
|
|
ASSERT(params.width * GetBytesPerPixel(params.pixel_format) % 4 == 0);
|
2018-10-10 01:15:05 +02:00
|
|
|
glPixelStorei(GL_PACK_ROW_LENGTH, static_cast<GLint>(params.width));
|
|
|
|
ASSERT(!tuple.compressed);
|
|
|
|
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
|
2018-10-26 13:26:08 +02:00
|
|
|
glGetTextureImage(texture.handle, 0, tuple.format, tuple.type,
|
2018-10-27 01:18:03 +02:00
|
|
|
static_cast<GLsizei>(gl_buffer[0].size()), gl_buffer[0].data());
|
2018-10-10 01:15:05 +02:00
|
|
|
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
|
2018-10-25 00:30:27 +02:00
|
|
|
ConvertFormatAsNeeded_FlushGLBuffer(gl_buffer[0], params.pixel_format, params.width,
|
2018-10-10 01:15:05 +02:00
|
|
|
params.height);
|
|
|
|
ASSERT(params.type != SurfaceType::Fill);
|
|
|
|
const u8* const texture_src_data = Memory::GetPointer(params.addr);
|
|
|
|
ASSERT(texture_src_data);
|
|
|
|
if (params.is_tiled) {
|
|
|
|
ASSERT_MSG(params.block_width == 1, "Block width is defined as {} on texture type {}",
|
|
|
|
params.block_width, static_cast<u32>(params.target));
|
|
|
|
|
2018-11-25 03:53:17 +01:00
|
|
|
SwizzleFunc(MortonSwizzleMode::LinearToMorton, params, gl_buffer[0], 0);
|
2018-10-10 01:15:05 +02:00
|
|
|
} else {
|
2018-10-25 00:30:27 +02:00
|
|
|
std::memcpy(Memory::GetPointer(GetAddr()), gl_buffer[0].data(), GetSizeInBytes());
|
2018-10-10 01:15:05 +02:00
|
|
|
}
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
2018-10-25 00:30:27 +02:00
|
|
|
void CachedSurface::UploadGLMipmapTexture(u32 mip_map, GLuint read_fb_handle,
|
|
|
|
GLuint draw_fb_handle) {
|
|
|
|
const auto& rect{params.GetRect(mip_map)};
|
2018-03-19 22:45:22 +01:00
|
|
|
|
|
|
|
// Load data from memory to the surface
|
2018-09-09 17:51:43 +02:00
|
|
|
const GLint x0 = static_cast<GLint>(rect.left);
|
|
|
|
const GLint y0 = static_cast<GLint>(rect.bottom);
|
2018-09-14 18:17:38 +02:00
|
|
|
std::size_t buffer_offset =
|
2018-10-25 00:30:27 +02:00
|
|
|
static_cast<std::size_t>(static_cast<std::size_t>(y0) * params.MipWidth(mip_map) +
|
2018-09-15 15:21:06 +02:00
|
|
|
static_cast<std::size_t>(x0)) *
|
2018-10-29 02:14:25 +01:00
|
|
|
GetBytesPerPixel(params.pixel_format);
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-06-22 01:36:01 +02:00
|
|
|
const FormatTuple& tuple = GetFormatTuple(params.pixel_format, params.component_type);
|
2018-09-09 17:51:43 +02:00
|
|
|
const GLuint target_tex = texture.handle;
|
2018-03-19 22:45:22 +01:00
|
|
|
OpenGLState cur_state = OpenGLState::GetCurState();
|
|
|
|
|
2018-09-07 01:08:45 +02:00
|
|
|
const auto& old_tex = cur_state.texture_units[0];
|
|
|
|
SCOPE_EXIT({
|
|
|
|
cur_state.texture_units[0] = old_tex;
|
|
|
|
cur_state.Apply();
|
|
|
|
});
|
2018-09-01 05:54:31 +02:00
|
|
|
cur_state.texture_units[0].texture = target_tex;
|
2018-09-07 01:08:45 +02:00
|
|
|
cur_state.texture_units[0].target = SurfaceTargetToGL(params.target);
|
2018-03-19 22:45:22 +01:00
|
|
|
cur_state.Apply();
|
|
|
|
|
|
|
|
// Ensure no bad interactions with GL_UNPACK_ALIGNMENT
|
2018-10-29 02:14:25 +01:00
|
|
|
ASSERT(params.MipWidth(mip_map) * GetBytesPerPixel(params.pixel_format) % 4 == 0);
|
2018-10-25 00:30:27 +02:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, static_cast<GLint>(params.MipWidth(mip_map)));
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-10-25 00:30:27 +02:00
|
|
|
GLsizei image_size = static_cast<GLsizei>(params.GetMipmapSizeGL(mip_map, false));
|
2018-03-19 22:45:22 +01:00
|
|
|
glActiveTexture(GL_TEXTURE0);
|
2018-03-27 04:49:05 +02:00
|
|
|
if (tuple.compressed) {
|
2018-09-07 01:08:45 +02:00
|
|
|
switch (params.target) {
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture2D:
|
2018-10-25 00:30:27 +02:00
|
|
|
glCompressedTexImage2D(SurfaceTargetToGL(params.target), mip_map, tuple.internal_format,
|
|
|
|
static_cast<GLsizei>(params.MipWidth(mip_map)),
|
|
|
|
static_cast<GLsizei>(params.MipHeight(mip_map)), 0, image_size,
|
|
|
|
&gl_buffer[mip_map][buffer_offset]);
|
2018-09-07 01:08:45 +02:00
|
|
|
break;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture3D:
|
2018-10-25 00:30:27 +02:00
|
|
|
glCompressedTexImage3D(SurfaceTargetToGL(params.target), mip_map, tuple.internal_format,
|
|
|
|
static_cast<GLsizei>(params.MipWidth(mip_map)),
|
|
|
|
static_cast<GLsizei>(params.MipHeight(mip_map)),
|
|
|
|
static_cast<GLsizei>(params.MipDepth(mip_map)), 0, image_size,
|
|
|
|
&gl_buffer[mip_map][buffer_offset]);
|
|
|
|
break;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture2DArray:
|
2018-10-19 02:04:43 +02:00
|
|
|
case SurfaceTarget::TextureCubeArray:
|
2018-10-25 00:30:27 +02:00
|
|
|
glCompressedTexImage3D(SurfaceTargetToGL(params.target), mip_map, tuple.internal_format,
|
|
|
|
static_cast<GLsizei>(params.MipWidth(mip_map)),
|
|
|
|
static_cast<GLsizei>(params.MipHeight(mip_map)),
|
|
|
|
static_cast<GLsizei>(params.depth), 0, image_size,
|
|
|
|
&gl_buffer[mip_map][buffer_offset]);
|
2018-09-07 01:08:45 +02:00
|
|
|
break;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::TextureCubemap: {
|
2018-10-25 00:30:27 +02:00
|
|
|
GLsizei layer_size = static_cast<GLsizei>(params.LayerSizeGL(mip_map));
|
2018-09-14 18:17:38 +02:00
|
|
|
for (std::size_t face = 0; face < params.depth; ++face) {
|
|
|
|
glCompressedTexImage2D(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face),
|
2018-10-25 00:30:27 +02:00
|
|
|
mip_map, tuple.internal_format,
|
|
|
|
static_cast<GLsizei>(params.MipWidth(mip_map)),
|
|
|
|
static_cast<GLsizei>(params.MipHeight(mip_map)), 0,
|
|
|
|
layer_size, &gl_buffer[mip_map][buffer_offset]);
|
|
|
|
buffer_offset += layer_size;
|
2018-09-14 18:17:38 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-10-25 00:30:27 +02:00
|
|
|
}
|
2018-09-07 01:08:45 +02:00
|
|
|
default:
|
|
|
|
LOG_CRITICAL(Render_OpenGL, "Unimplemented surface target={}",
|
|
|
|
static_cast<u32>(params.target));
|
|
|
|
UNREACHABLE();
|
2018-10-25 00:30:27 +02:00
|
|
|
glCompressedTexImage2D(GL_TEXTURE_2D, mip_map, tuple.internal_format,
|
|
|
|
static_cast<GLsizei>(params.MipWidth(mip_map)),
|
|
|
|
static_cast<GLsizei>(params.MipHeight(mip_map)), 0,
|
|
|
|
static_cast<GLsizei>(params.size_in_bytes_gl),
|
|
|
|
&gl_buffer[mip_map][buffer_offset]);
|
2018-09-07 01:08:45 +02:00
|
|
|
}
|
2018-03-27 04:49:05 +02:00
|
|
|
} else {
|
2018-09-07 01:08:45 +02:00
|
|
|
|
|
|
|
switch (params.target) {
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture1D:
|
2018-10-25 00:30:27 +02:00
|
|
|
glTexSubImage1D(SurfaceTargetToGL(params.target), mip_map, x0,
|
2018-09-07 01:08:45 +02:00
|
|
|
static_cast<GLsizei>(rect.GetWidth()), tuple.format, tuple.type,
|
2018-10-25 00:30:27 +02:00
|
|
|
&gl_buffer[mip_map][buffer_offset]);
|
2018-09-07 01:08:45 +02:00
|
|
|
break;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture2D:
|
2018-10-25 00:30:27 +02:00
|
|
|
glTexSubImage2D(SurfaceTargetToGL(params.target), mip_map, x0, y0,
|
2018-09-07 01:08:45 +02:00
|
|
|
static_cast<GLsizei>(rect.GetWidth()),
|
|
|
|
static_cast<GLsizei>(rect.GetHeight()), tuple.format, tuple.type,
|
2018-10-25 00:30:27 +02:00
|
|
|
&gl_buffer[mip_map][buffer_offset]);
|
2018-09-07 01:08:45 +02:00
|
|
|
break;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture3D:
|
2018-10-25 00:30:27 +02:00
|
|
|
glTexSubImage3D(SurfaceTargetToGL(params.target), mip_map, x0, y0, 0,
|
|
|
|
static_cast<GLsizei>(rect.GetWidth()),
|
|
|
|
static_cast<GLsizei>(rect.GetHeight()), params.MipDepth(mip_map),
|
|
|
|
tuple.format, tuple.type, &gl_buffer[mip_map][buffer_offset]);
|
|
|
|
break;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture2DArray:
|
2018-10-19 02:04:43 +02:00
|
|
|
case SurfaceTarget::TextureCubeArray:
|
2018-10-25 00:30:27 +02:00
|
|
|
glTexSubImage3D(SurfaceTargetToGL(params.target), mip_map, x0, y0, 0,
|
2018-09-07 01:08:45 +02:00
|
|
|
static_cast<GLsizei>(rect.GetWidth()),
|
|
|
|
static_cast<GLsizei>(rect.GetHeight()), params.depth, tuple.format,
|
2018-10-25 00:30:27 +02:00
|
|
|
tuple.type, &gl_buffer[mip_map][buffer_offset]);
|
2018-09-07 01:08:45 +02:00
|
|
|
break;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::TextureCubemap: {
|
2018-10-25 00:30:27 +02:00
|
|
|
std::size_t start = buffer_offset;
|
2018-09-14 18:17:38 +02:00
|
|
|
for (std::size_t face = 0; face < params.depth; ++face) {
|
2018-10-25 00:30:27 +02:00
|
|
|
glTexSubImage2D(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face), mip_map,
|
|
|
|
x0, y0, static_cast<GLsizei>(rect.GetWidth()),
|
2018-09-14 18:17:38 +02:00
|
|
|
static_cast<GLsizei>(rect.GetHeight()), tuple.format, tuple.type,
|
2018-10-25 00:30:27 +02:00
|
|
|
&gl_buffer[mip_map][buffer_offset]);
|
|
|
|
buffer_offset += params.LayerSizeGL(mip_map);
|
2018-09-14 18:17:38 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-10-25 00:30:27 +02:00
|
|
|
}
|
2018-09-07 01:08:45 +02:00
|
|
|
default:
|
|
|
|
LOG_CRITICAL(Render_OpenGL, "Unimplemented surface target={}",
|
|
|
|
static_cast<u32>(params.target));
|
|
|
|
UNREACHABLE();
|
2018-10-25 00:30:27 +02:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, mip_map, x0, y0, static_cast<GLsizei>(rect.GetWidth()),
|
2018-09-07 01:08:45 +02:00
|
|
|
static_cast<GLsizei>(rect.GetHeight()), tuple.format, tuple.type,
|
2018-10-25 00:30:27 +02:00
|
|
|
&gl_buffer[mip_map][buffer_offset]);
|
2018-09-07 01:08:45 +02:00
|
|
|
}
|
2018-03-27 04:49:05 +02:00
|
|
|
}
|
2018-03-19 22:45:22 +01:00
|
|
|
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
|
|
|
|
}
|
|
|
|
|
2018-11-06 13:25:01 +01:00
|
|
|
MICROPROFILE_DEFINE(OpenGL_TextureUL, "OpenGL", "Texture Upload", MP_RGB(128, 192, 64));
|
2018-10-25 00:30:27 +02:00
|
|
|
void CachedSurface::UploadGLTexture(GLuint read_fb_handle, GLuint draw_fb_handle) {
|
|
|
|
if (params.type == SurfaceType::Fill)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MICROPROFILE_SCOPE(OpenGL_TextureUL);
|
|
|
|
|
|
|
|
for (u32 i = 0; i < params.max_mip_level; i++)
|
|
|
|
UploadGLMipmapTexture(i, read_fb_handle, draw_fb_handle);
|
|
|
|
}
|
|
|
|
|
2018-11-08 12:08:00 +01:00
|
|
|
RasterizerCacheOpenGL::RasterizerCacheOpenGL(RasterizerOpenGL& rasterizer)
|
|
|
|
: RasterizerCache{rasterizer} {
|
2018-03-19 22:45:22 +01:00
|
|
|
read_framebuffer.Create();
|
|
|
|
draw_framebuffer.Create();
|
2018-09-08 07:04:47 +02:00
|
|
|
copy_pbo.Create();
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
2018-09-14 17:42:28 +02:00
|
|
|
Surface RasterizerCacheOpenGL::GetTextureSurface(const Tegra::Texture::FullTextureInfo& config,
|
|
|
|
const GLShader::SamplerEntry& entry) {
|
|
|
|
return GetSurface(SurfaceParams::CreateForTexture(config, entry));
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
2018-09-10 01:01:21 +02:00
|
|
|
Surface RasterizerCacheOpenGL::GetDepthBufferSurface(bool preserve_contents) {
|
|
|
|
const auto& regs{Core::System::GetInstance().GPU().Maxwell3D().regs};
|
|
|
|
if (!regs.zeta.Address() || !regs.zeta_enable) {
|
|
|
|
return {};
|
|
|
|
}
|
2018-03-24 05:47:33 +01:00
|
|
|
|
2018-09-10 01:01:21 +02:00
|
|
|
SurfaceParams depth_params{SurfaceParams::CreateForDepthBuffer(
|
2018-10-08 20:34:55 +02:00
|
|
|
regs.zeta_width, regs.zeta_height, regs.zeta.Address(), regs.zeta.format,
|
2018-10-10 04:28:19 +02:00
|
|
|
regs.zeta.memory_layout.block_width, regs.zeta.memory_layout.block_height,
|
|
|
|
regs.zeta.memory_layout.block_depth, regs.zeta.memory_layout.type)};
|
2018-03-24 05:47:33 +01:00
|
|
|
|
2018-09-10 01:01:21 +02:00
|
|
|
return GetSurface(depth_params, preserve_contents);
|
|
|
|
}
|
2018-07-04 05:32:59 +02:00
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
Surface RasterizerCacheOpenGL::GetColorBufferSurface(std::size_t index, bool preserve_contents) {
|
2018-09-10 01:01:21 +02:00
|
|
|
const auto& regs{Core::System::GetInstance().GPU().Maxwell3D().regs};
|
2018-03-24 05:47:33 +01:00
|
|
|
|
2018-09-10 01:01:21 +02:00
|
|
|
ASSERT(index < Tegra::Engines::Maxwell3D::Regs::NumRenderTargets);
|
2018-03-24 05:47:33 +01:00
|
|
|
|
2018-09-10 01:01:21 +02:00
|
|
|
if (index >= regs.rt_control.count) {
|
|
|
|
return {};
|
2018-06-22 01:36:01 +02:00
|
|
|
}
|
2018-03-24 05:47:33 +01:00
|
|
|
|
2018-09-10 01:01:21 +02:00
|
|
|
if (regs.rt[index].Address() == 0 || regs.rt[index].format == Tegra::RenderTargetFormat::NONE) {
|
|
|
|
return {};
|
2018-06-22 01:36:01 +02:00
|
|
|
}
|
2018-03-24 05:47:33 +01:00
|
|
|
|
2018-09-10 01:01:21 +02:00
|
|
|
const SurfaceParams color_params{SurfaceParams::CreateForFramebuffer(index)};
|
2018-03-24 05:47:33 +01:00
|
|
|
|
2018-09-10 01:01:21 +02:00
|
|
|
return GetSurface(color_params, preserve_contents);
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 01:36:01 +02:00
|
|
|
void RasterizerCacheOpenGL::LoadSurface(const Surface& surface) {
|
|
|
|
surface->LoadGLBuffer();
|
|
|
|
surface->UploadGLTexture(read_framebuffer.handle, draw_framebuffer.handle);
|
2018-10-16 22:51:53 +02:00
|
|
|
surface->MarkAsModified(false, *this);
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
2018-08-20 22:18:51 +02:00
|
|
|
Surface RasterizerCacheOpenGL::GetSurface(const SurfaceParams& params, bool preserve_contents) {
|
2018-06-22 01:36:01 +02:00
|
|
|
if (params.addr == 0 || params.height * params.width == 0) {
|
|
|
|
return {};
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
2018-07-21 22:04:01 +02:00
|
|
|
// Look up surface in the cache based on address
|
2018-08-23 21:44:41 +02:00
|
|
|
Surface surface{TryGet(params.addr)};
|
|
|
|
if (surface) {
|
2018-08-31 02:27:23 +02:00
|
|
|
if (surface->GetSurfaceParams().IsCompatibleSurface(params)) {
|
2018-07-21 22:04:01 +02:00
|
|
|
// Use the cached surface as-is
|
2018-07-21 20:36:32 +02:00
|
|
|
return surface;
|
2018-08-20 22:18:51 +02:00
|
|
|
} else if (preserve_contents) {
|
|
|
|
// If surface parameters changed and we care about keeping the previous data, recreate
|
|
|
|
// the surface from the old one
|
2018-08-23 00:46:05 +02:00
|
|
|
Surface new_surface{RecreateSurface(surface, params)};
|
2018-10-16 03:14:04 +02:00
|
|
|
Unregister(surface);
|
2018-08-23 21:44:41 +02:00
|
|
|
Register(new_surface);
|
2018-08-23 00:46:05 +02:00
|
|
|
return new_surface;
|
2018-08-20 22:18:51 +02:00
|
|
|
} else {
|
|
|
|
// Delete the old surface before creating a new one to prevent collisions.
|
2018-08-23 21:44:41 +02:00
|
|
|
Unregister(surface);
|
2018-06-26 22:14:14 +02:00
|
|
|
}
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
2018-08-31 02:08:43 +02:00
|
|
|
// No cached surface found - get a new one
|
|
|
|
surface = GetUncachedSurface(params);
|
|
|
|
Register(surface);
|
2018-08-22 04:04:54 +02:00
|
|
|
|
|
|
|
// Only load surface from memory if we care about the contents
|
|
|
|
if (preserve_contents) {
|
|
|
|
LoadSurface(surface);
|
|
|
|
}
|
2018-07-21 20:36:32 +02:00
|
|
|
|
2018-03-19 22:45:22 +01:00
|
|
|
return surface;
|
|
|
|
}
|
2018-06-24 23:42:29 +02:00
|
|
|
|
2018-08-31 02:08:43 +02:00
|
|
|
Surface RasterizerCacheOpenGL::GetUncachedSurface(const SurfaceParams& params) {
|
|
|
|
Surface surface{TryGetReservedSurface(params)};
|
|
|
|
if (!surface) {
|
|
|
|
// No reserved surface available, create a new one and reserve it
|
|
|
|
surface = std::make_shared<CachedSurface>(params);
|
|
|
|
ReserveSurface(surface);
|
|
|
|
}
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
2018-11-19 17:04:07 +01:00
|
|
|
void RasterizerCacheOpenGL::FastLayeredCopySurface(const Surface& src_surface,
|
|
|
|
const Surface& dst_surface) {
|
|
|
|
const auto& init_params{src_surface->GetSurfaceParams()};
|
|
|
|
const auto& dst_params{dst_surface->GetSurfaceParams()};
|
|
|
|
VAddr address = init_params.addr;
|
|
|
|
const std::size_t layer_size = dst_params.LayerMemorySize();
|
|
|
|
for (u32 layer = 0; layer < dst_params.depth; layer++) {
|
|
|
|
for (u32 mipmap = 0; mipmap < dst_params.max_mip_level; mipmap++) {
|
|
|
|
const VAddr sub_address = address + dst_params.GetMipmapLevelOffset(mipmap);
|
|
|
|
const Surface& copy = TryGet(sub_address);
|
|
|
|
if (!copy)
|
|
|
|
continue;
|
|
|
|
const auto& src_params{copy->GetSurfaceParams()};
|
|
|
|
const u32 width{std::min(src_params.width, dst_params.MipWidth(mipmap))};
|
|
|
|
const u32 height{std::min(src_params.height, dst_params.MipHeight(mipmap))};
|
|
|
|
|
|
|
|
glCopyImageSubData(copy->Texture().handle, SurfaceTargetToGL(src_params.target), 0, 0,
|
|
|
|
0, 0, dst_surface->Texture().handle,
|
|
|
|
SurfaceTargetToGL(dst_params.target), mipmap, 0, 0, layer, width,
|
|
|
|
height, 1);
|
|
|
|
}
|
|
|
|
address += layer_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-06 05:39:03 +02:00
|
|
|
void RasterizerCacheOpenGL::FermiCopySurface(
|
|
|
|
const Tegra::Engines::Fermi2D::Regs::Surface& src_config,
|
|
|
|
const Tegra::Engines::Fermi2D::Regs::Surface& dst_config) {
|
|
|
|
|
|
|
|
const auto& src_params = SurfaceParams::CreateForFermiCopySurface(src_config);
|
|
|
|
const auto& dst_params = SurfaceParams::CreateForFermiCopySurface(dst_config);
|
|
|
|
|
|
|
|
ASSERT(src_params.width == dst_params.width);
|
|
|
|
ASSERT(src_params.height == dst_params.height);
|
|
|
|
ASSERT(src_params.pixel_format == dst_params.pixel_format);
|
|
|
|
ASSERT(src_params.block_height == dst_params.block_height);
|
|
|
|
ASSERT(src_params.is_tiled == dst_params.is_tiled);
|
|
|
|
ASSERT(src_params.depth == dst_params.depth);
|
|
|
|
ASSERT(src_params.depth == 1); // Currently, FastCopySurface only works with 2D surfaces
|
|
|
|
ASSERT(src_params.target == dst_params.target);
|
|
|
|
ASSERT(src_params.rt.index == dst_params.rt.index);
|
|
|
|
|
|
|
|
FastCopySurface(GetSurface(src_params, true), GetSurface(dst_params, false));
|
|
|
|
}
|
|
|
|
|
2018-10-16 23:20:49 +02:00
|
|
|
void RasterizerCacheOpenGL::AccurateCopySurface(const Surface& src_surface,
|
|
|
|
const Surface& dst_surface) {
|
|
|
|
const auto& src_params{src_surface->GetSurfaceParams()};
|
|
|
|
const auto& dst_params{dst_surface->GetSurfaceParams()};
|
2018-10-18 21:04:33 +02:00
|
|
|
FlushRegion(src_params.addr, dst_params.MemorySize());
|
2018-10-16 23:20:49 +02:00
|
|
|
LoadSurface(dst_surface);
|
|
|
|
}
|
|
|
|
|
2018-09-14 17:33:55 +02:00
|
|
|
Surface RasterizerCacheOpenGL::RecreateSurface(const Surface& old_surface,
|
2018-07-21 22:04:01 +02:00
|
|
|
const SurfaceParams& new_params) {
|
|
|
|
// Verify surface is compatible for blitting
|
2018-09-21 05:41:25 +02:00
|
|
|
auto old_params{old_surface->GetSurfaceParams()};
|
2018-07-21 22:04:01 +02:00
|
|
|
|
2018-08-31 02:08:43 +02:00
|
|
|
// Get a new surface with the new parameters, and blit the previous surface to it
|
|
|
|
Surface new_surface{GetUncachedSurface(new_params)};
|
2018-08-20 22:19:59 +02:00
|
|
|
|
2018-10-16 23:20:49 +02:00
|
|
|
// With use_accurate_gpu_emulation enabled, do an accurate surface copy
|
|
|
|
if (Settings::values.use_accurate_gpu_emulation) {
|
|
|
|
AccurateCopySurface(old_surface, new_surface);
|
|
|
|
return new_surface;
|
|
|
|
}
|
|
|
|
|
2018-10-06 05:09:01 +02:00
|
|
|
// For compatible surfaces, we can just do fast glCopyImageSubData based copy
|
|
|
|
if (old_params.target == new_params.target && old_params.type == new_params.type &&
|
|
|
|
old_params.depth == new_params.depth && old_params.depth == 1 &&
|
2018-10-29 02:14:25 +01:00
|
|
|
GetFormatBpp(old_params.pixel_format) == GetFormatBpp(new_params.pixel_format)) {
|
2018-10-06 05:09:01 +02:00
|
|
|
FastCopySurface(old_surface, new_surface);
|
|
|
|
return new_surface;
|
|
|
|
}
|
|
|
|
|
2018-09-21 05:41:25 +02:00
|
|
|
switch (new_params.target) {
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture2D:
|
2018-11-29 03:56:21 +01:00
|
|
|
CopySurface(old_surface, new_surface, copy_pbo.handle);
|
2018-09-21 05:41:25 +02:00
|
|
|
break;
|
2018-10-29 02:14:25 +01:00
|
|
|
case SurfaceTarget::Texture3D:
|
2018-11-19 17:04:07 +01:00
|
|
|
AccurateCopySurface(old_surface, new_surface);
|
|
|
|
break;
|
|
|
|
case SurfaceTarget::TextureCubemap:
|
2018-11-02 14:28:28 +01:00
|
|
|
case SurfaceTarget::Texture2DArray:
|
2018-10-19 02:04:43 +02:00
|
|
|
case SurfaceTarget::TextureCubeArray:
|
2018-11-19 17:04:07 +01:00
|
|
|
FastLayeredCopySurface(old_surface, new_surface);
|
2018-10-16 03:14:04 +02:00
|
|
|
break;
|
2018-09-21 05:41:25 +02:00
|
|
|
default:
|
|
|
|
LOG_CRITICAL(Render_OpenGL, "Unimplemented surface target={}",
|
|
|
|
static_cast<u32>(new_params.target));
|
|
|
|
UNREACHABLE();
|
2018-08-31 02:27:23 +02:00
|
|
|
}
|
2018-07-21 22:04:01 +02:00
|
|
|
|
|
|
|
return new_surface;
|
2018-10-29 02:14:25 +01:00
|
|
|
}
|
2018-07-21 22:04:01 +02:00
|
|
|
|
2018-08-28 03:35:15 +02:00
|
|
|
Surface RasterizerCacheOpenGL::TryFindFramebufferSurface(VAddr addr) const {
|
|
|
|
return TryGet(addr);
|
2018-06-24 23:42:29 +02:00
|
|
|
}
|
2018-06-26 22:14:14 +02:00
|
|
|
|
2018-08-22 04:04:54 +02:00
|
|
|
void RasterizerCacheOpenGL::ReserveSurface(const Surface& surface) {
|
|
|
|
const auto& surface_reserve_key{SurfaceReserveKey::Create(surface->GetSurfaceParams())};
|
|
|
|
surface_reserve[surface_reserve_key] = surface;
|
|
|
|
}
|
|
|
|
|
|
|
|
Surface RasterizerCacheOpenGL::TryGetReservedSurface(const SurfaceParams& params) {
|
|
|
|
const auto& surface_reserve_key{SurfaceReserveKey::Create(params)};
|
|
|
|
auto search{surface_reserve.find(surface_reserve_key)};
|
|
|
|
if (search != surface_reserve.end()) {
|
|
|
|
return search->second;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-08-21 10:18:27 +02:00
|
|
|
} // namespace OpenGL
|