2018-11-25 03:53:17 +01:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cstring>
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "video_core/morton.h"
|
|
|
|
#include "video_core/surface.h"
|
|
|
|
#include "video_core/textures/decoders.h"
|
|
|
|
|
|
|
|
namespace VideoCore {
|
|
|
|
|
|
|
|
using Surface::GetBytesPerPixel;
|
|
|
|
using Surface::PixelFormat;
|
|
|
|
|
2019-02-24 06:15:35 +01:00
|
|
|
using MortonCopyFn = void (*)(u32, u32, u32, u32, u32, u32, u8*, u8*);
|
2018-11-25 03:53:17 +01:00
|
|
|
using ConversionArray = std::array<MortonCopyFn, Surface::MaxPixelFormat>;
|
|
|
|
|
|
|
|
template <bool morton_to_linear, PixelFormat format>
|
|
|
|
static void MortonCopy(u32 stride, u32 block_height, u32 height, u32 block_depth, u32 depth,
|
2019-02-24 06:15:35 +01:00
|
|
|
u32 tile_width_spacing, u8* buffer, u8* addr) {
|
2018-11-25 03:53:17 +01:00
|
|
|
constexpr u32 bytes_per_pixel = GetBytesPerPixel(format);
|
|
|
|
|
|
|
|
// With the BCn formats (DXT and DXN), each 4x4 tile is swizzled instead of just individual
|
|
|
|
// pixel values.
|
2019-08-21 07:17:14 +02:00
|
|
|
constexpr u32 tile_size_x{GetDefaultBlockWidth(format)};
|
|
|
|
constexpr u32 tile_size_y{GetDefaultBlockHeight(format)};
|
2018-11-25 03:53:17 +01:00
|
|
|
|
2018-11-25 04:12:09 +01:00
|
|
|
if constexpr (morton_to_linear) {
|
2018-11-25 03:53:17 +01:00
|
|
|
Tegra::Texture::UnswizzleTexture(buffer, addr, tile_size_x, tile_size_y, bytes_per_pixel,
|
2018-11-16 18:01:54 +01:00
|
|
|
stride, height, depth, block_height, block_depth,
|
|
|
|
tile_width_spacing);
|
2018-11-25 03:53:17 +01:00
|
|
|
} else {
|
2019-02-24 06:15:35 +01:00
|
|
|
Tegra::Texture::CopySwizzledData((stride + tile_size_x - 1) / tile_size_x,
|
|
|
|
(height + tile_size_y - 1) / tile_size_y, depth,
|
|
|
|
bytes_per_pixel, bytes_per_pixel, addr, buffer, false,
|
|
|
|
block_height, block_depth, tile_width_spacing);
|
2018-11-25 03:53:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr ConversionArray morton_to_linear_fns = {
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<true, PixelFormat::ABGR8U>,
|
|
|
|
MortonCopy<true, PixelFormat::ABGR8S>,
|
2020-06-30 09:31:48 +02:00
|
|
|
MortonCopy<true, PixelFormat::ABGR8I>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<true, PixelFormat::ABGR8UI>,
|
|
|
|
MortonCopy<true, PixelFormat::B5G6R5U>,
|
2020-06-30 22:32:36 +02:00
|
|
|
MortonCopy<true, PixelFormat::B5G5R5A1U>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<true, PixelFormat::A2B10G10R10U>,
|
2020-06-30 09:53:48 +02:00
|
|
|
MortonCopy<true, PixelFormat::A2B10G10R10UI>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<true, PixelFormat::A1B5G5R5U>,
|
|
|
|
MortonCopy<true, PixelFormat::R8U>,
|
2020-06-30 08:51:42 +02:00
|
|
|
MortonCopy<true, PixelFormat::R8S>,
|
2020-06-30 09:00:23 +02:00
|
|
|
MortonCopy<true, PixelFormat::R8I>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<true, PixelFormat::R8UI>,
|
|
|
|
MortonCopy<true, PixelFormat::RGBA16F>,
|
|
|
|
MortonCopy<true, PixelFormat::RGBA16U>,
|
2020-03-13 01:42:33 +01:00
|
|
|
MortonCopy<true, PixelFormat::RGBA16S>,
|
2020-06-30 09:38:29 +02:00
|
|
|
MortonCopy<true, PixelFormat::RGBA16I>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<true, PixelFormat::RGBA16UI>,
|
|
|
|
MortonCopy<true, PixelFormat::R11FG11FB10F>,
|
|
|
|
MortonCopy<true, PixelFormat::RGBA32UI>,
|
|
|
|
MortonCopy<true, PixelFormat::DXT1>,
|
|
|
|
MortonCopy<true, PixelFormat::DXT23>,
|
|
|
|
MortonCopy<true, PixelFormat::DXT45>,
|
|
|
|
MortonCopy<true, PixelFormat::DXN1>,
|
|
|
|
MortonCopy<true, PixelFormat::DXN2UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::DXN2SNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::BC7U>,
|
|
|
|
MortonCopy<true, PixelFormat::BC6H_UF16>,
|
|
|
|
MortonCopy<true, PixelFormat::BC6H_SF16>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_4X4>,
|
|
|
|
MortonCopy<true, PixelFormat::BGRA8>,
|
|
|
|
MortonCopy<true, PixelFormat::RGBA32F>,
|
2020-06-30 09:46:07 +02:00
|
|
|
MortonCopy<true, PixelFormat::RGBA32I>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<true, PixelFormat::RG32F>,
|
2020-06-30 09:23:03 +02:00
|
|
|
MortonCopy<true, PixelFormat::RG32I>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<true, PixelFormat::R32F>,
|
|
|
|
MortonCopy<true, PixelFormat::R16F>,
|
|
|
|
MortonCopy<true, PixelFormat::R16U>,
|
|
|
|
MortonCopy<true, PixelFormat::R16S>,
|
|
|
|
MortonCopy<true, PixelFormat::R16UI>,
|
|
|
|
MortonCopy<true, PixelFormat::R16I>,
|
|
|
|
MortonCopy<true, PixelFormat::RG16>,
|
|
|
|
MortonCopy<true, PixelFormat::RG16F>,
|
|
|
|
MortonCopy<true, PixelFormat::RG16UI>,
|
|
|
|
MortonCopy<true, PixelFormat::RG16I>,
|
|
|
|
MortonCopy<true, PixelFormat::RG16S>,
|
|
|
|
MortonCopy<true, PixelFormat::RGB32F>,
|
|
|
|
MortonCopy<true, PixelFormat::RGBA8_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::RG8U>,
|
|
|
|
MortonCopy<true, PixelFormat::RG8S>,
|
2020-06-30 09:13:46 +02:00
|
|
|
MortonCopy<true, PixelFormat::RG8I>,
|
2020-04-30 18:17:02 +02:00
|
|
|
MortonCopy<true, PixelFormat::RG8UI>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<true, PixelFormat::RG32UI>,
|
2019-09-22 01:40:46 +02:00
|
|
|
MortonCopy<true, PixelFormat::RGBX16F>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<true, PixelFormat::R32UI>,
|
2020-02-15 06:54:24 +01:00
|
|
|
MortonCopy<true, PixelFormat::R32I>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_8X8>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_8X5>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_5X4>,
|
|
|
|
MortonCopy<true, PixelFormat::BGRA8_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::DXT1_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::DXT23_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::DXT45_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::BC7U_SRGB>,
|
2019-10-08 18:54:59 +02:00
|
|
|
MortonCopy<true, PixelFormat::R4G4B4A4U>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_4X4_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_8X8_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_8X5_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_5X4_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_5X5>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_5X5_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_10X8>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_10X8_SRGB>,
|
2019-10-03 01:00:16 +02:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_6X6>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_6X6_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_10X10>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_10X10_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_12X12>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_12X12_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_8X6>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_8X6_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_6X5>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_6X5_SRGB>,
|
2019-10-27 21:44:09 +01:00
|
|
|
MortonCopy<true, PixelFormat::E5B9G9R9F>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<true, PixelFormat::Z32F>,
|
|
|
|
MortonCopy<true, PixelFormat::Z16>,
|
|
|
|
MortonCopy<true, PixelFormat::Z24S8>,
|
|
|
|
MortonCopy<true, PixelFormat::S8Z24>,
|
|
|
|
MortonCopy<true, PixelFormat::Z32FS8>,
|
2018-11-25 03:53:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr ConversionArray linear_to_morton_fns = {
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<false, PixelFormat::ABGR8U>,
|
|
|
|
MortonCopy<false, PixelFormat::ABGR8S>,
|
2020-06-30 09:31:48 +02:00
|
|
|
MortonCopy<false, PixelFormat::ABGR8I>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<false, PixelFormat::ABGR8UI>,
|
|
|
|
MortonCopy<false, PixelFormat::B5G6R5U>,
|
2020-06-30 22:32:36 +02:00
|
|
|
MortonCopy<false, PixelFormat::B5G5R5A1U>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<false, PixelFormat::A2B10G10R10U>,
|
2020-06-30 09:53:48 +02:00
|
|
|
MortonCopy<false, PixelFormat::A2B10G10R10UI>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<false, PixelFormat::A1B5G5R5U>,
|
|
|
|
MortonCopy<false, PixelFormat::R8U>,
|
2020-06-30 08:51:42 +02:00
|
|
|
MortonCopy<false, PixelFormat::R8S>,
|
2020-06-30 09:00:23 +02:00
|
|
|
MortonCopy<false, PixelFormat::R8I>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<false, PixelFormat::R8UI>,
|
|
|
|
MortonCopy<false, PixelFormat::RGBA16F>,
|
2020-03-13 01:42:33 +01:00
|
|
|
MortonCopy<false, PixelFormat::RGBA16S>,
|
2020-06-30 09:38:29 +02:00
|
|
|
MortonCopy<false, PixelFormat::RGBA16I>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<false, PixelFormat::RGBA16U>,
|
|
|
|
MortonCopy<false, PixelFormat::RGBA16UI>,
|
|
|
|
MortonCopy<false, PixelFormat::R11FG11FB10F>,
|
|
|
|
MortonCopy<false, PixelFormat::RGBA32UI>,
|
|
|
|
MortonCopy<false, PixelFormat::DXT1>,
|
|
|
|
MortonCopy<false, PixelFormat::DXT23>,
|
|
|
|
MortonCopy<false, PixelFormat::DXT45>,
|
|
|
|
MortonCopy<false, PixelFormat::DXN1>,
|
|
|
|
MortonCopy<false, PixelFormat::DXN2UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::DXN2SNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::BC7U>,
|
|
|
|
MortonCopy<false, PixelFormat::BC6H_UF16>,
|
|
|
|
MortonCopy<false, PixelFormat::BC6H_SF16>,
|
|
|
|
// TODO(Subv): Swizzling ASTC formats are not supported
|
|
|
|
nullptr,
|
|
|
|
MortonCopy<false, PixelFormat::BGRA8>,
|
|
|
|
MortonCopy<false, PixelFormat::RGBA32F>,
|
2020-06-30 09:46:07 +02:00
|
|
|
MortonCopy<false, PixelFormat::RGBA32I>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<false, PixelFormat::RG32F>,
|
2020-06-30 09:23:03 +02:00
|
|
|
MortonCopy<false, PixelFormat::RG32I>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<false, PixelFormat::R32F>,
|
|
|
|
MortonCopy<false, PixelFormat::R16F>,
|
|
|
|
MortonCopy<false, PixelFormat::R16U>,
|
|
|
|
MortonCopy<false, PixelFormat::R16S>,
|
|
|
|
MortonCopy<false, PixelFormat::R16UI>,
|
|
|
|
MortonCopy<false, PixelFormat::R16I>,
|
|
|
|
MortonCopy<false, PixelFormat::RG16>,
|
|
|
|
MortonCopy<false, PixelFormat::RG16F>,
|
|
|
|
MortonCopy<false, PixelFormat::RG16UI>,
|
|
|
|
MortonCopy<false, PixelFormat::RG16I>,
|
|
|
|
MortonCopy<false, PixelFormat::RG16S>,
|
|
|
|
MortonCopy<false, PixelFormat::RGB32F>,
|
|
|
|
MortonCopy<false, PixelFormat::RGBA8_SRGB>,
|
|
|
|
MortonCopy<false, PixelFormat::RG8U>,
|
|
|
|
MortonCopy<false, PixelFormat::RG8S>,
|
2020-06-30 09:13:46 +02:00
|
|
|
MortonCopy<false, PixelFormat::RG8I>,
|
2020-04-30 18:17:02 +02:00
|
|
|
MortonCopy<false, PixelFormat::RG8UI>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<false, PixelFormat::RG32UI>,
|
2019-09-22 01:40:46 +02:00
|
|
|
MortonCopy<false, PixelFormat::RGBX16F>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<false, PixelFormat::R32UI>,
|
2020-02-15 06:54:24 +01:00
|
|
|
MortonCopy<false, PixelFormat::R32I>,
|
2019-03-13 20:16:45 +01:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
MortonCopy<false, PixelFormat::BGRA8_SRGB>,
|
|
|
|
MortonCopy<false, PixelFormat::DXT1_SRGB>,
|
|
|
|
MortonCopy<false, PixelFormat::DXT23_SRGB>,
|
|
|
|
MortonCopy<false, PixelFormat::DXT45_SRGB>,
|
|
|
|
MortonCopy<false, PixelFormat::BC7U_SRGB>,
|
2019-10-08 18:54:59 +02:00
|
|
|
MortonCopy<false, PixelFormat::R4G4B4A4U>,
|
2019-03-13 20:16:45 +01:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2019-10-03 01:00:16 +02:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2019-10-27 21:44:09 +01:00
|
|
|
MortonCopy<false, PixelFormat::E5B9G9R9F>,
|
2019-03-13 20:16:45 +01:00
|
|
|
MortonCopy<false, PixelFormat::Z32F>,
|
|
|
|
MortonCopy<false, PixelFormat::Z16>,
|
|
|
|
MortonCopy<false, PixelFormat::Z24S8>,
|
|
|
|
MortonCopy<false, PixelFormat::S8Z24>,
|
|
|
|
MortonCopy<false, PixelFormat::Z32FS8>,
|
2018-11-25 03:53:17 +01:00
|
|
|
};
|
|
|
|
|
2018-11-27 03:21:57 +01:00
|
|
|
static MortonCopyFn GetSwizzleFunction(MortonSwizzleMode mode, Surface::PixelFormat format) {
|
2018-11-25 03:53:17 +01:00
|
|
|
switch (mode) {
|
|
|
|
case MortonSwizzleMode::MortonToLinear:
|
|
|
|
return morton_to_linear_fns[static_cast<std::size_t>(format)];
|
|
|
|
case MortonSwizzleMode::LinearToMorton:
|
|
|
|
return linear_to_morton_fns[static_cast<std::size_t>(format)];
|
|
|
|
}
|
2018-11-27 03:21:57 +01:00
|
|
|
UNREACHABLE();
|
2018-12-19 02:52:32 +01:00
|
|
|
return morton_to_linear_fns[static_cast<std::size_t>(format)];
|
2018-11-25 03:53:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MortonSwizzle(MortonSwizzleMode mode, Surface::PixelFormat format, u32 stride,
|
2018-11-16 18:01:54 +01:00
|
|
|
u32 block_height, u32 height, u32 block_depth, u32 depth, u32 tile_width_spacing,
|
2019-02-24 06:15:35 +01:00
|
|
|
u8* buffer, u8* addr) {
|
2018-11-16 18:01:54 +01:00
|
|
|
GetSwizzleFunction(mode, format)(stride, block_height, height, block_depth, depth,
|
2019-03-13 20:25:23 +01:00
|
|
|
tile_width_spacing, buffer, addr);
|
2018-11-25 03:53:17 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 18:01:54 +01:00
|
|
|
} // namespace VideoCore
|