2019-04-24 21:35:54 +02:00
|
|
|
// Copyright 2019 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
#include "common/alignment.h"
|
2019-05-13 02:33:52 +02:00
|
|
|
#include "common/bit_util.h"
|
2019-04-24 21:35:54 +02:00
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "video_core/engines/fermi_2d.h"
|
|
|
|
#include "video_core/engines/maxwell_3d.h"
|
2019-05-07 16:56:45 +02:00
|
|
|
#include "video_core/shader/shader_ir.h"
|
2019-05-09 00:27:29 +02:00
|
|
|
#include "video_core/surface.h"
|
|
|
|
#include "video_core/textures/decoders.h"
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
namespace VideoCommon {
|
|
|
|
|
2019-05-13 02:33:52 +02:00
|
|
|
using VideoCore::Surface::SurfaceCompression;
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
class SurfaceParams {
|
2019-04-24 21:35:54 +02:00
|
|
|
public:
|
|
|
|
/// Creates SurfaceCachedParams from a texture configuration.
|
|
|
|
static SurfaceParams CreateForTexture(Core::System& system,
|
2019-05-07 16:56:45 +02:00
|
|
|
const Tegra::Texture::FullTextureInfo& config,
|
|
|
|
const VideoCommon::Shader::Sampler& entry);
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
/// Creates SurfaceCachedParams for a depth buffer configuration.
|
|
|
|
static SurfaceParams CreateForDepthBuffer(
|
|
|
|
Core::System& system, u32 zeta_width, u32 zeta_height, Tegra::DepthFormat format,
|
|
|
|
u32 block_width, u32 block_height, u32 block_depth,
|
|
|
|
Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout type);
|
|
|
|
|
|
|
|
/// Creates SurfaceCachedParams from a framebuffer configuration.
|
|
|
|
static SurfaceParams CreateForFramebuffer(Core::System& system, std::size_t index);
|
|
|
|
|
|
|
|
/// Creates SurfaceCachedParams from a Fermi2D surface configuration.
|
|
|
|
static SurfaceParams CreateForFermiCopySurface(
|
|
|
|
const Tegra::Engines::Fermi2D::Regs::Surface& config);
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
std::size_t Hash() const;
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
bool operator==(const SurfaceParams& rhs) const;
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
bool operator!=(const SurfaceParams& rhs) const {
|
|
|
|
return !operator==(rhs);
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t GetGuestSizeInBytes() const {
|
2019-05-07 16:56:45 +02:00
|
|
|
return GetInnerMemorySize(false, false, false);
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t GetHostSizeInBytes() const {
|
2019-05-07 16:56:45 +02:00
|
|
|
std::size_t host_size_in_bytes;
|
2019-05-13 02:33:52 +02:00
|
|
|
if (GetCompressionType() == SurfaceCompression::Converted) {
|
2019-05-09 00:27:29 +02:00
|
|
|
constexpr std::size_t rgb8_bpp = 4ULL;
|
2019-05-07 16:56:45 +02:00
|
|
|
// ASTC is uncompressed in software, in emulated as RGBA8
|
2019-05-09 00:27:29 +02:00
|
|
|
host_size_in_bytes = 0;
|
2019-05-10 09:17:48 +02:00
|
|
|
for (u32 level = 0; level < num_levels; ++level) {
|
2019-05-13 02:33:52 +02:00
|
|
|
host_size_in_bytes += GetConvertedMipmapSize(level);
|
2019-05-09 00:27:29 +02:00
|
|
|
}
|
2019-05-07 16:56:45 +02:00
|
|
|
} else {
|
|
|
|
host_size_in_bytes = GetInnerMemorySize(true, false, false);
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
return host_size_in_bytes;
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
u32 GetBlockAlignedWidth() const {
|
|
|
|
return Common::AlignUp(width, 64 / GetBytesPerPixel());
|
2019-04-24 21:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the width of a given mipmap level.
|
2019-05-09 00:27:29 +02:00
|
|
|
u32 GetMipWidth(u32 level) const {
|
|
|
|
return std::max(1U, width >> level);
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
/// Returns the height of a given mipmap level.
|
2019-05-09 00:27:29 +02:00
|
|
|
u32 GetMipHeight(u32 level) const {
|
|
|
|
return std::max(1U, height >> level);
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
/// Returns the depth of a given mipmap level.
|
2019-05-09 00:27:29 +02:00
|
|
|
u32 GetMipDepth(u32 level) const {
|
|
|
|
return is_layered ? depth : std::max(1U, depth >> level);
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
/// Returns the block height of a given mipmap level.
|
|
|
|
u32 GetMipBlockHeight(u32 level) const;
|
|
|
|
|
|
|
|
/// Returns the block depth of a given mipmap level.
|
|
|
|
u32 GetMipBlockDepth(u32 level) const;
|
|
|
|
|
2019-05-13 02:33:52 +02:00
|
|
|
u32 GetRowAlignment(u32 level) const {
|
|
|
|
const u32 bpp =
|
|
|
|
GetCompressionType() == SurfaceCompression::Converted ? 4 : GetBytesPerPixel();
|
|
|
|
return 1U << Common::CountTrailingZeroes32(GetMipWidth(level) * bpp);
|
|
|
|
}
|
|
|
|
|
2019-05-09 00:27:29 +02:00
|
|
|
// Helper used for out of class size calculations
|
|
|
|
static std::size_t AlignLayered(const std::size_t out_size, const u32 block_height,
|
|
|
|
const u32 block_depth) {
|
2019-05-10 09:17:48 +02:00
|
|
|
return Common::AlignBits(out_size,
|
|
|
|
Tegra::Texture::GetGOBSizeShift() + block_height + block_depth);
|
2019-05-09 00:27:29 +02:00
|
|
|
}
|
|
|
|
|
2019-04-24 21:35:54 +02:00
|
|
|
/// Returns the offset in bytes in guest memory of a given mipmap level.
|
|
|
|
std::size_t GetGuestMipmapLevelOffset(u32 level) const;
|
|
|
|
|
|
|
|
/// Returns the offset in bytes in host memory (linear) of a given mipmap level.
|
|
|
|
std::size_t GetHostMipmapLevelOffset(u32 level) const;
|
|
|
|
|
2019-05-13 02:33:52 +02:00
|
|
|
std::size_t GetConvertedMipmapOffset(u32 level) const;
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
/// Returns the size in bytes in guest memory of a given mipmap level.
|
|
|
|
std::size_t GetGuestMipmapSize(u32 level) const;
|
|
|
|
|
2019-04-24 21:35:54 +02:00
|
|
|
/// Returns the size in bytes in host memory (linear) of a given mipmap level.
|
|
|
|
std::size_t GetHostMipmapSize(u32 level) const;
|
|
|
|
|
2019-05-13 02:33:52 +02:00
|
|
|
std::size_t GetConvertedMipmapSize(u32 level) const;
|
|
|
|
|
2019-04-24 21:35:54 +02:00
|
|
|
/// Returns the size of a layer in bytes in guest memory.
|
|
|
|
std::size_t GetGuestLayerSize() const;
|
|
|
|
|
|
|
|
/// Returns the size of a layer in bytes in host memory for a given mipmap level.
|
|
|
|
std::size_t GetHostLayerSize(u32 level) const;
|
|
|
|
|
2019-05-18 10:57:49 +02:00
|
|
|
static u32 ConvertWidth(u32 width, VideoCore::Surface::PixelFormat pixel_format_from,
|
|
|
|
VideoCore::Surface::PixelFormat pixel_format_to) {
|
|
|
|
const u32 bw1 = VideoCore::Surface::GetDefaultBlockWidth(pixel_format_from);
|
|
|
|
const u32 bw2 = VideoCore::Surface::GetDefaultBlockWidth(pixel_format_to);
|
|
|
|
return (width * bw2 + bw1 - 1) / bw1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 ConvertHeight(u32 height, VideoCore::Surface::PixelFormat pixel_format_from,
|
|
|
|
VideoCore::Surface::PixelFormat pixel_format_to) {
|
|
|
|
const u32 bh1 = VideoCore::Surface::GetDefaultBlockHeight(pixel_format_from);
|
|
|
|
const u32 bh2 = VideoCore::Surface::GetDefaultBlockHeight(pixel_format_to);
|
|
|
|
return (height * bh2 + bh1 - 1) / bh1;
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:35:54 +02:00
|
|
|
/// Returns the default block width.
|
2019-05-09 00:27:29 +02:00
|
|
|
u32 GetDefaultBlockWidth() const {
|
|
|
|
return VideoCore::Surface::GetDefaultBlockWidth(pixel_format);
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
/// Returns the default block height.
|
2019-05-09 00:27:29 +02:00
|
|
|
u32 GetDefaultBlockHeight() const {
|
|
|
|
return VideoCore::Surface::GetDefaultBlockHeight(pixel_format);
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
/// Returns the bits per pixel.
|
2019-05-09 00:27:29 +02:00
|
|
|
u32 GetBitsPerPixel() const {
|
|
|
|
return VideoCore::Surface::GetFormatBpp(pixel_format);
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
/// Returns the bytes per pixel.
|
2019-05-09 00:27:29 +02:00
|
|
|
u32 GetBytesPerPixel() const {
|
|
|
|
return VideoCore::Surface::GetBytesPerPixel(pixel_format);
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
/// Returns true if the pixel format is a depth and/or stencil format.
|
|
|
|
bool IsPixelFormatZeta() const;
|
|
|
|
|
2019-05-13 02:33:52 +02:00
|
|
|
SurfaceCompression GetCompressionType() const {
|
|
|
|
return VideoCore::Surface::GetFormatCompressionType(pixel_format);
|
|
|
|
}
|
|
|
|
|
2019-05-21 18:48:28 +02:00
|
|
|
bool IsBuffer() const {
|
|
|
|
return target == VideoCore::Surface::SurfaceTarget::TextureBuffer;
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
std::string TargetName() const;
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
bool is_tiled;
|
|
|
|
bool srgb_conversion;
|
|
|
|
bool is_layered;
|
|
|
|
u32 block_width;
|
|
|
|
u32 block_height;
|
|
|
|
u32 block_depth;
|
|
|
|
u32 tile_width_spacing;
|
|
|
|
u32 width;
|
|
|
|
u32 height;
|
|
|
|
u32 depth;
|
|
|
|
u32 pitch;
|
|
|
|
u32 num_levels;
|
|
|
|
VideoCore::Surface::PixelFormat pixel_format;
|
|
|
|
VideoCore::Surface::ComponentType component_type;
|
|
|
|
VideoCore::Surface::SurfaceType type;
|
|
|
|
VideoCore::Surface::SurfaceTarget target;
|
2019-04-24 21:35:54 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Returns the size of a given mipmap level inside a layer.
|
|
|
|
std::size_t GetInnerMipmapMemorySize(u32 level, bool as_host_size, bool uncompressed) const;
|
|
|
|
|
|
|
|
/// Returns the size of all mipmap levels and aligns as needed.
|
|
|
|
std::size_t GetInnerMemorySize(bool as_host_size, bool layer_only, bool uncompressed) const;
|
|
|
|
|
|
|
|
/// Returns the size of a layer
|
|
|
|
std::size_t GetLayerSize(bool as_host_size, bool uncompressed) const;
|
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
std::size_t GetNumLayers() const {
|
|
|
|
return is_layered ? depth : 1;
|
|
|
|
}
|
2019-04-24 21:35:54 +02:00
|
|
|
|
2019-05-07 16:56:45 +02:00
|
|
|
/// Returns true if these parameters are from a layered surface.
|
|
|
|
bool IsLayered() const;
|
2019-04-24 21:35:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace VideoCommon
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct hash<VideoCommon::SurfaceParams> {
|
|
|
|
std::size_t operator()(const VideoCommon::SurfaceParams& k) const noexcept {
|
|
|
|
return k.Hash();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace std
|