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-06-22 01:36:01 +02:00
|
|
|
|
2018-03-19 22:45:22 +01:00
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/math_util.h"
|
|
|
|
#include "video_core/renderer_opengl/gl_resource_manager.h"
|
2018-03-27 04:46:11 +02:00
|
|
|
#include "video_core/textures/texture.h"
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-06-22 01:36:01 +02:00
|
|
|
class CachedSurface;
|
2018-03-19 22:45:22 +01:00
|
|
|
using Surface = std::shared_ptr<CachedSurface>;
|
|
|
|
using SurfaceSurfaceRect_Tuple = std::tuple<Surface, Surface, MathUtil::Rectangle<u32>>;
|
|
|
|
|
|
|
|
struct SurfaceParams {
|
|
|
|
enum class PixelFormat {
|
2018-04-18 20:51:09 +02:00
|
|
|
ABGR8 = 0,
|
2018-04-19 01:11:14 +02:00
|
|
|
B5G6R5 = 1,
|
2018-04-22 00:32:25 +02:00
|
|
|
A2B10G10R10 = 2,
|
2018-05-27 16:02:05 +02:00
|
|
|
A1B5G5R5 = 3,
|
2018-05-30 04:49:37 +02:00
|
|
|
R8 = 4,
|
2018-05-31 04:24:07 +02:00
|
|
|
RGBA16F = 5,
|
2018-06-06 04:57:16 +02:00
|
|
|
R11FG11FB10F = 6,
|
|
|
|
DXT1 = 7,
|
|
|
|
DXT23 = 8,
|
|
|
|
DXT45 = 9,
|
|
|
|
DXN1 = 10, // This is also known as BC4
|
2018-06-18 05:50:44 +02:00
|
|
|
ASTC_2D_4X4 = 11,
|
2018-04-19 01:11:14 +02:00
|
|
|
|
|
|
|
Max,
|
2018-03-19 22:45:22 +01:00
|
|
|
Invalid = 255,
|
|
|
|
};
|
|
|
|
|
2018-04-19 01:11:14 +02:00
|
|
|
static constexpr size_t MaxPixelFormat = static_cast<size_t>(PixelFormat::Max);
|
|
|
|
|
2018-04-18 21:17:05 +02:00
|
|
|
enum class ComponentType {
|
|
|
|
Invalid = 0,
|
|
|
|
SNorm = 1,
|
|
|
|
UNorm = 2,
|
|
|
|
SInt = 3,
|
|
|
|
UInt = 4,
|
|
|
|
Float = 5,
|
|
|
|
};
|
|
|
|
|
2018-03-19 22:45:22 +01:00
|
|
|
enum class SurfaceType {
|
2018-04-18 20:54:10 +02:00
|
|
|
ColorTexture = 0,
|
|
|
|
Depth = 1,
|
|
|
|
DepthStencil = 2,
|
|
|
|
Fill = 3,
|
|
|
|
Invalid = 4,
|
2018-03-19 22:45:22 +01:00
|
|
|
};
|
|
|
|
|
2018-04-24 05:45:47 +02:00
|
|
|
/**
|
|
|
|
* Gets the compression factor for the specified PixelFormat. This applies to just the
|
|
|
|
* "compressed width" and "compressed height", not the overall compression factor of a
|
2018-06-22 01:36:01 +02:00
|
|
|
* compressed image. This is used for maintaining proper surface sizes for compressed
|
|
|
|
* texture formats.
|
2018-04-24 05:45:47 +02:00
|
|
|
*/
|
2018-06-22 01:36:01 +02:00
|
|
|
static constexpr u32 GetCompressionFactor(PixelFormat format) {
|
2018-03-27 04:46:11 +02:00
|
|
|
if (format == PixelFormat::Invalid)
|
|
|
|
return 0;
|
|
|
|
|
2018-04-24 05:45:47 +02:00
|
|
|
constexpr std::array<u32, MaxPixelFormat> compression_factor_table = {{
|
|
|
|
1, // ABGR8
|
|
|
|
1, // B5G6R5
|
|
|
|
1, // A2B10G10R10
|
2018-05-27 16:02:05 +02:00
|
|
|
1, // A1B5G5R5
|
2018-05-30 04:49:37 +02:00
|
|
|
1, // R8
|
2018-06-06 04:55:17 +02:00
|
|
|
1, // RGBA16F
|
2018-06-06 04:57:16 +02:00
|
|
|
1, // R11FG11FB10F
|
2018-04-24 05:45:47 +02:00
|
|
|
4, // DXT1
|
|
|
|
4, // DXT23
|
|
|
|
4, // DXT45
|
2018-06-02 20:17:09 +02:00
|
|
|
4, // DXN1
|
2018-06-18 05:50:44 +02:00
|
|
|
1, // ASTC_2D_4X4
|
2018-04-24 05:45:47 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
ASSERT(static_cast<size_t>(format) < compression_factor_table.size());
|
|
|
|
return compression_factor_table[static_cast<size_t>(format)];
|
|
|
|
}
|
2018-06-22 01:36:01 +02:00
|
|
|
u32 GetCompressionFactor() const {
|
|
|
|
return GetCompressionFactor(pixel_format);
|
2018-04-24 05:45:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr u32 GetFormatBpp(PixelFormat format) {
|
|
|
|
if (format == PixelFormat::Invalid)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
constexpr std::array<u32, MaxPixelFormat> bpp_table = {{
|
2018-04-19 03:48:53 +02:00
|
|
|
32, // ABGR8
|
|
|
|
16, // B5G6R5
|
2018-04-22 00:32:25 +02:00
|
|
|
32, // A2B10G10R10
|
2018-05-27 16:02:05 +02:00
|
|
|
16, // A1B5G5R5
|
2018-05-30 04:49:37 +02:00
|
|
|
8, // R8
|
2018-05-31 04:24:07 +02:00
|
|
|
64, // RGBA16F
|
2018-06-06 04:57:16 +02:00
|
|
|
32, // R11FG11FB10F
|
2018-04-19 03:48:53 +02:00
|
|
|
64, // DXT1
|
|
|
|
128, // DXT23
|
|
|
|
128, // DXT45
|
2018-06-02 20:17:09 +02:00
|
|
|
64, // DXN1
|
2018-06-18 05:50:44 +02:00
|
|
|
32, // ASTC_2D_4X4
|
2018-04-24 05:45:47 +02:00
|
|
|
}};
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-03-27 04:46:11 +02:00
|
|
|
ASSERT(static_cast<size_t>(format) < bpp_table.size());
|
2018-03-19 22:45:22 +01:00
|
|
|
return bpp_table[static_cast<size_t>(format)];
|
|
|
|
}
|
2018-04-24 05:45:47 +02:00
|
|
|
u32 GetFormatBpp() const {
|
2018-03-19 22:45:22 +01:00
|
|
|
return GetFormatBpp(pixel_format);
|
|
|
|
}
|
|
|
|
|
2018-03-24 05:47:33 +01:00
|
|
|
static PixelFormat PixelFormatFromRenderTargetFormat(Tegra::RenderTargetFormat format) {
|
|
|
|
switch (format) {
|
|
|
|
case Tegra::RenderTargetFormat::RGBA8_UNORM:
|
2018-05-27 16:02:05 +02:00
|
|
|
case Tegra::RenderTargetFormat::RGBA8_SRGB:
|
2018-04-18 20:51:09 +02:00
|
|
|
return PixelFormat::ABGR8;
|
2018-04-23 17:50:28 +02:00
|
|
|
case Tegra::RenderTargetFormat::RGB10_A2_UNORM:
|
|
|
|
return PixelFormat::A2B10G10R10;
|
2018-05-31 04:24:07 +02:00
|
|
|
case Tegra::RenderTargetFormat::RGBA16_FLOAT:
|
|
|
|
return PixelFormat::RGBA16F;
|
2018-06-06 04:57:16 +02:00
|
|
|
case Tegra::RenderTargetFormat::R11G11B10_FLOAT:
|
|
|
|
return PixelFormat::R11FG11FB10F;
|
2018-03-24 05:47:33 +01:00
|
|
|
default:
|
2018-04-18 03:44:36 +02:00
|
|
|
NGLOG_CRITICAL(HW_GPU, "Unimplemented format={}", static_cast<u32>(format));
|
2018-03-24 05:47:33 +01:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-27 04:46:11 +02:00
|
|
|
static PixelFormat PixelFormatFromTextureFormat(Tegra::Texture::TextureFormat format) {
|
|
|
|
// TODO(Subv): Properly implement this
|
|
|
|
switch (format) {
|
|
|
|
case Tegra::Texture::TextureFormat::A8R8G8B8:
|
2018-04-18 20:51:09 +02:00
|
|
|
return PixelFormat::ABGR8;
|
2018-04-19 01:11:14 +02:00
|
|
|
case Tegra::Texture::TextureFormat::B5G6R5:
|
|
|
|
return PixelFormat::B5G6R5;
|
2018-04-22 00:32:25 +02:00
|
|
|
case Tegra::Texture::TextureFormat::A2B10G10R10:
|
|
|
|
return PixelFormat::A2B10G10R10;
|
2018-05-27 16:02:05 +02:00
|
|
|
case Tegra::Texture::TextureFormat::A1B5G5R5:
|
|
|
|
return PixelFormat::A1B5G5R5;
|
2018-05-30 04:49:37 +02:00
|
|
|
case Tegra::Texture::TextureFormat::R8:
|
|
|
|
return PixelFormat::R8;
|
2018-05-31 04:24:07 +02:00
|
|
|
case Tegra::Texture::TextureFormat::R16_G16_B16_A16:
|
|
|
|
return PixelFormat::RGBA16F;
|
2018-06-06 04:57:16 +02:00
|
|
|
case Tegra::Texture::TextureFormat::BF10GF11RF11:
|
|
|
|
return PixelFormat::R11FG11FB10F;
|
2018-03-27 04:46:11 +02:00
|
|
|
case Tegra::Texture::TextureFormat::DXT1:
|
|
|
|
return PixelFormat::DXT1;
|
2018-04-19 03:48:53 +02:00
|
|
|
case Tegra::Texture::TextureFormat::DXT23:
|
|
|
|
return PixelFormat::DXT23;
|
|
|
|
case Tegra::Texture::TextureFormat::DXT45:
|
|
|
|
return PixelFormat::DXT45;
|
2018-06-02 20:17:09 +02:00
|
|
|
case Tegra::Texture::TextureFormat::DXN1:
|
|
|
|
return PixelFormat::DXN1;
|
2018-06-18 05:50:44 +02:00
|
|
|
case Tegra::Texture::TextureFormat::ASTC_2D_4X4:
|
|
|
|
return PixelFormat::ASTC_2D_4X4;
|
2018-03-27 04:46:11 +02:00
|
|
|
default:
|
2018-04-18 03:44:36 +02:00
|
|
|
NGLOG_CRITICAL(HW_GPU, "Unimplemented format={}", static_cast<u32>(format));
|
2018-03-27 04:46:11 +02:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2018-04-16 02:54:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Tegra::Texture::TextureFormat TextureFormatFromPixelFormat(PixelFormat format) {
|
|
|
|
// TODO(Subv): Properly implement this
|
|
|
|
switch (format) {
|
2018-04-18 20:51:09 +02:00
|
|
|
case PixelFormat::ABGR8:
|
2018-04-16 02:54:38 +02:00
|
|
|
return Tegra::Texture::TextureFormat::A8R8G8B8;
|
2018-04-19 01:11:14 +02:00
|
|
|
case PixelFormat::B5G6R5:
|
|
|
|
return Tegra::Texture::TextureFormat::B5G6R5;
|
2018-04-22 00:32:25 +02:00
|
|
|
case PixelFormat::A2B10G10R10:
|
|
|
|
return Tegra::Texture::TextureFormat::A2B10G10R10;
|
2018-05-27 16:02:05 +02:00
|
|
|
case PixelFormat::A1B5G5R5:
|
|
|
|
return Tegra::Texture::TextureFormat::A1B5G5R5;
|
2018-05-30 04:49:37 +02:00
|
|
|
case PixelFormat::R8:
|
|
|
|
return Tegra::Texture::TextureFormat::R8;
|
2018-05-31 04:24:07 +02:00
|
|
|
case PixelFormat::RGBA16F:
|
|
|
|
return Tegra::Texture::TextureFormat::R16_G16_B16_A16;
|
2018-06-06 04:57:16 +02:00
|
|
|
case PixelFormat::R11FG11FB10F:
|
|
|
|
return Tegra::Texture::TextureFormat::BF10GF11RF11;
|
2018-04-16 02:54:38 +02:00
|
|
|
case PixelFormat::DXT1:
|
|
|
|
return Tegra::Texture::TextureFormat::DXT1;
|
2018-04-19 03:48:53 +02:00
|
|
|
case PixelFormat::DXT23:
|
|
|
|
return Tegra::Texture::TextureFormat::DXT23;
|
|
|
|
case PixelFormat::DXT45:
|
|
|
|
return Tegra::Texture::TextureFormat::DXT45;
|
2018-06-02 20:17:09 +02:00
|
|
|
case PixelFormat::DXN1:
|
|
|
|
return Tegra::Texture::TextureFormat::DXN1;
|
2018-06-18 05:50:44 +02:00
|
|
|
case PixelFormat::ASTC_2D_4X4:
|
|
|
|
return Tegra::Texture::TextureFormat::ASTC_2D_4X4;
|
2018-04-16 02:54:38 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2018-03-27 04:46:11 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 21:17:05 +02:00
|
|
|
static ComponentType ComponentTypeFromTexture(Tegra::Texture::ComponentType type) {
|
|
|
|
// TODO(Subv): Implement more component types
|
|
|
|
switch (type) {
|
|
|
|
case Tegra::Texture::ComponentType::UNORM:
|
|
|
|
return ComponentType::UNorm;
|
|
|
|
default:
|
|
|
|
NGLOG_CRITICAL(HW_GPU, "Unimplemented component type={}", static_cast<u32>(type));
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ComponentType ComponentTypeFromRenderTarget(Tegra::RenderTargetFormat format) {
|
|
|
|
// TODO(Subv): Implement more render targets
|
|
|
|
switch (format) {
|
|
|
|
case Tegra::RenderTargetFormat::RGBA8_UNORM:
|
2018-05-27 16:02:05 +02:00
|
|
|
case Tegra::RenderTargetFormat::RGBA8_SRGB:
|
2018-04-18 21:17:05 +02:00
|
|
|
case Tegra::RenderTargetFormat::RGB10_A2_UNORM:
|
|
|
|
return ComponentType::UNorm;
|
2018-05-31 04:24:07 +02:00
|
|
|
case Tegra::RenderTargetFormat::RGBA16_FLOAT:
|
2018-06-06 04:57:16 +02:00
|
|
|
case Tegra::RenderTargetFormat::R11G11B10_FLOAT:
|
2018-05-31 04:24:07 +02:00
|
|
|
return ComponentType::Float;
|
2018-04-18 21:17:05 +02:00
|
|
|
default:
|
|
|
|
NGLOG_CRITICAL(HW_GPU, "Unimplemented format={}", static_cast<u32>(format));
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-27 04:46:11 +02:00
|
|
|
static SurfaceType GetFormatType(PixelFormat pixel_format) {
|
2018-04-19 01:11:14 +02:00
|
|
|
if (static_cast<size_t>(pixel_format) < MaxPixelFormat) {
|
2018-04-18 20:54:10 +02:00
|
|
|
return SurfaceType::ColorTexture;
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
2018-03-27 04:46:11 +02:00
|
|
|
// TODO(Subv): Implement the other formats
|
|
|
|
ASSERT(false);
|
2018-03-19 22:45:22 +01:00
|
|
|
|
|
|
|
return SurfaceType::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
MathUtil::Rectangle<u32> GetRect() const {
|
|
|
|
return {0, height, width, 0};
|
|
|
|
}
|
|
|
|
|
2018-06-22 01:36:01 +02:00
|
|
|
size_t SizeInBytes() const {
|
|
|
|
const u32 compression_factor{GetCompressionFactor()};
|
|
|
|
ASSERT(width % compression_factor == 0);
|
|
|
|
ASSERT(height % compression_factor == 0);
|
|
|
|
return (width / compression_factor) * (height / compression_factor) *
|
|
|
|
GetFormatBpp(pixel_format) / CHAR_BIT;
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
2018-04-24 06:19:36 +02:00
|
|
|
VAddr GetCpuAddr() const;
|
|
|
|
|
2018-06-22 01:36:01 +02:00
|
|
|
Tegra::GPUVAddr addr;
|
|
|
|
u32 width;
|
|
|
|
u32 height;
|
|
|
|
u32 block_height;
|
|
|
|
bool is_tiled;
|
|
|
|
PixelFormat pixel_format;
|
|
|
|
SurfaceType type;
|
|
|
|
ComponentType component_type;
|
2018-03-19 22:45:22 +01:00
|
|
|
};
|
2018-06-22 01:36:01 +02:00
|
|
|
static_assert(std::is_pod<SurfaceParams>::value, "SurfaceParams is not POD");
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-06-22 01:36:01 +02:00
|
|
|
class CachedSurface final {
|
|
|
|
public:
|
|
|
|
CachedSurface(const SurfaceParams& params);
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-06-22 01:36:01 +02:00
|
|
|
const OGLTexture& Texture() const {
|
|
|
|
return texture;
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 01:36:01 +02:00
|
|
|
static constexpr unsigned int GetGLBytesPerPixel(SurfaceParams::PixelFormat format) {
|
|
|
|
if (format == SurfaceParams::PixelFormat::Invalid)
|
2018-03-27 04:46:11 +02:00
|
|
|
return 0;
|
|
|
|
|
2018-04-25 00:01:06 +02:00
|
|
|
return SurfaceParams::GetFormatBpp(format) / CHAR_BIT;
|
2018-03-19 22:45:22 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// Upload/Download data in gl_buffer in/to this surface's texture
|
2018-06-22 01:36:01 +02:00
|
|
|
void UploadGLTexture(GLuint read_fb_handle, GLuint draw_fb_handle);
|
|
|
|
void DownloadGLTexture(GLuint read_fb_handle, GLuint draw_fb_handle);
|
|
|
|
|
|
|
|
private:
|
|
|
|
OGLTexture texture;
|
|
|
|
std::unique_ptr<u8[]> gl_buffer;
|
|
|
|
size_t gl_buffer_size;
|
|
|
|
SurfaceParams params;
|
2018-03-19 22:45:22 +01:00
|
|
|
};
|
|
|
|
|
2018-06-22 01:36:01 +02:00
|
|
|
class RasterizerCacheOpenGL final : NonCopyable {
|
2018-03-19 22:45:22 +01:00
|
|
|
public:
|
|
|
|
RasterizerCacheOpenGL();
|
|
|
|
|
2018-03-27 04:46:11 +02:00
|
|
|
Surface GetTextureSurface(const Tegra::Texture::FullTextureInfo& config);
|
2018-03-19 22:45:22 +01:00
|
|
|
SurfaceSurfaceRect_Tuple GetFramebufferSurfaces(bool using_color_fb, bool using_depth_fb,
|
2018-03-24 05:47:33 +01:00
|
|
|
const MathUtil::Rectangle<s32>& viewport);
|
2018-06-22 01:36:01 +02:00
|
|
|
void LoadSurface(const Surface& surface);
|
|
|
|
void FlushSurface(const Surface& surface);
|
2018-03-19 22:45:22 +01:00
|
|
|
|
|
|
|
private:
|
2018-06-22 01:36:01 +02:00
|
|
|
Surface GetSurface(const SurfaceParams& params);
|
2018-03-19 22:45:22 +01:00
|
|
|
|
2018-06-22 01:36:01 +02:00
|
|
|
std::map<Tegra::GPUVAddr, Surface> surface_cache;
|
2018-03-19 22:45:22 +01:00
|
|
|
OGLFramebuffer read_framebuffer;
|
|
|
|
OGLFramebuffer draw_framebuffer;
|
|
|
|
};
|