2018-11-24 21:53:17 -05: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 00:15:35 -05:00
|
|
|
using MortonCopyFn = void (*)(u32, u32, u32, u32, u32, u32, u8*, u8*);
|
2018-11-24 21:53:17 -05: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 00:15:35 -05:00
|
|
|
u32 tile_width_spacing, u8* buffer, u8* addr) {
|
2018-11-24 21:53:17 -05: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 01:17:14 -04:00
|
|
|
constexpr u32 tile_size_x{GetDefaultBlockWidth(format)};
|
|
|
|
constexpr u32 tile_size_y{GetDefaultBlockHeight(format)};
|
2018-11-24 21:53:17 -05:00
|
|
|
|
2018-11-24 22:12:09 -05:00
|
|
|
if constexpr (morton_to_linear) {
|
2018-11-24 21:53:17 -05:00
|
|
|
Tegra::Texture::UnswizzleTexture(buffer, addr, tile_size_x, tile_size_y, bytes_per_pixel,
|
2018-11-16 12:01:54 -05:00
|
|
|
stride, height, depth, block_height, block_depth,
|
|
|
|
tile_width_spacing);
|
2018-11-24 21:53:17 -05:00
|
|
|
} else {
|
2019-02-24 00:15:35 -05: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-24 21:53:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr ConversionArray morton_to_linear_fns = {
|
2020-07-01 01:28:53 -04:00
|
|
|
MortonCopy<true, PixelFormat::A8B8G8R8_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::A8B8G8R8_SNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::A8B8G8R8_SINT>,
|
|
|
|
MortonCopy<true, PixelFormat::A8B8G8R8_UINT>,
|
|
|
|
MortonCopy<true, PixelFormat::R5G6B5_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::B5G6R5_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::A1R5G5B5_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::A2B10G10R10_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::A2B10G10R10_UINT>,
|
|
|
|
MortonCopy<true, PixelFormat::A1B5G5R5_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::R8_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::R8_SNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::R8_SINT>,
|
|
|
|
MortonCopy<true, PixelFormat::R8_UINT>,
|
|
|
|
MortonCopy<true, PixelFormat::R16G16B16A16_FLOAT>,
|
|
|
|
MortonCopy<true, PixelFormat::R16G16B16A16_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::R16G16B16A16_SNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::R16G16B16A16_SINT>,
|
|
|
|
MortonCopy<true, PixelFormat::R16G16B16A16_UINT>,
|
|
|
|
MortonCopy<true, PixelFormat::B10G11R11_FLOAT>,
|
|
|
|
MortonCopy<true, PixelFormat::R32G32B32A32_UINT>,
|
|
|
|
MortonCopy<true, PixelFormat::BC1_RGBA_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::BC2_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::BC3_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::BC4_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::BC4_SNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::BC5_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::BC5_SNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::BC7_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::BC6H_UFLOAT>,
|
|
|
|
MortonCopy<true, PixelFormat::BC6H_SFLOAT>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_4X4_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::B8G8R8A8_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::R32G32B32A32_FLOAT>,
|
|
|
|
MortonCopy<true, PixelFormat::R32G32B32A32_SINT>,
|
|
|
|
MortonCopy<true, PixelFormat::R32G32_FLOAT>,
|
|
|
|
MortonCopy<true, PixelFormat::R32G32_SINT>,
|
|
|
|
MortonCopy<true, PixelFormat::R32_FLOAT>,
|
|
|
|
MortonCopy<true, PixelFormat::R16_FLOAT>,
|
|
|
|
MortonCopy<true, PixelFormat::R16_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::R16_SNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::R16_UINT>,
|
|
|
|
MortonCopy<true, PixelFormat::R16_SINT>,
|
|
|
|
MortonCopy<true, PixelFormat::R16G16_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::R16G16_FLOAT>,
|
|
|
|
MortonCopy<true, PixelFormat::R16G16_UINT>,
|
|
|
|
MortonCopy<true, PixelFormat::R16G16_SINT>,
|
|
|
|
MortonCopy<true, PixelFormat::R16G16_SNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::R32G32B32_FLOAT>,
|
|
|
|
MortonCopy<true, PixelFormat::A8B8G8R8_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::R8G8_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::R8G8_SNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::R8G8_SINT>,
|
|
|
|
MortonCopy<true, PixelFormat::R8G8_UINT>,
|
|
|
|
MortonCopy<true, PixelFormat::R32G32_UINT>,
|
|
|
|
MortonCopy<true, PixelFormat::R16G16B16X16_FLOAT>,
|
|
|
|
MortonCopy<true, PixelFormat::R32_UINT>,
|
|
|
|
MortonCopy<true, PixelFormat::R32_SINT>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_8X8_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_8X5_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_5X4_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::B8G8R8A8_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::BC1_RGBA_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::BC2_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::BC3_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::BC7_SRGB>,
|
|
|
|
MortonCopy<true, PixelFormat::A4B4G4R4_UNORM>,
|
2019-03-13 15:16:45 -04: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>,
|
2020-07-01 01:28:53 -04:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_5X5_UNORM>,
|
2019-03-13 15:16:45 -04:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_5X5_SRGB>,
|
2020-07-01 01:28:53 -04:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_10X8_UNORM>,
|
2019-03-13 15:16:45 -04:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_10X8_SRGB>,
|
2020-07-01 01:28:53 -04:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_6X6_UNORM>,
|
2019-10-02 19:00:16 -04:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_6X6_SRGB>,
|
2020-07-01 01:28:53 -04:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_10X10_UNORM>,
|
2019-10-02 19:00:16 -04:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_10X10_SRGB>,
|
2020-07-01 01:28:53 -04:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_12X12_UNORM>,
|
2019-10-02 19:00:16 -04:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_12X12_SRGB>,
|
2020-07-01 01:28:53 -04:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_8X6_UNORM>,
|
2019-10-02 19:00:16 -04:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_8X6_SRGB>,
|
2020-07-01 01:28:53 -04:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_6X5_UNORM>,
|
2019-10-02 19:00:16 -04:00
|
|
|
MortonCopy<true, PixelFormat::ASTC_2D_6X5_SRGB>,
|
2020-07-01 01:28:53 -04:00
|
|
|
MortonCopy<true, PixelFormat::E5B9G9R9_FLOAT>,
|
|
|
|
MortonCopy<true, PixelFormat::D32_FLOAT>,
|
|
|
|
MortonCopy<true, PixelFormat::D16_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::D24_UNORM_S8_UINT>,
|
|
|
|
MortonCopy<true, PixelFormat::S8_UINT_D24_UNORM>,
|
|
|
|
MortonCopy<true, PixelFormat::D32_FLOAT_S8_UINT>,
|
2018-11-24 21:53:17 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr ConversionArray linear_to_morton_fns = {
|
2020-07-01 01:28:53 -04:00
|
|
|
MortonCopy<false, PixelFormat::A8B8G8R8_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::A8B8G8R8_SNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::A8B8G8R8_SINT>,
|
|
|
|
MortonCopy<false, PixelFormat::A8B8G8R8_UINT>,
|
|
|
|
MortonCopy<false, PixelFormat::R5G6B5_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::B5G6R5_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::A1R5G5B5_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::A2B10G10R10_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::A2B10G10R10_UINT>,
|
|
|
|
MortonCopy<false, PixelFormat::A1B5G5R5_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::R8_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::R8_SNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::R8_SINT>,
|
|
|
|
MortonCopy<false, PixelFormat::R8_UINT>,
|
|
|
|
MortonCopy<false, PixelFormat::R16G16B16A16_FLOAT>,
|
|
|
|
MortonCopy<false, PixelFormat::R16G16B16A16_SNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::R16G16B16A16_SINT>,
|
|
|
|
MortonCopy<false, PixelFormat::R16G16B16A16_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::R16G16B16A16_UINT>,
|
|
|
|
MortonCopy<false, PixelFormat::B10G11R11_FLOAT>,
|
|
|
|
MortonCopy<false, PixelFormat::R32G32B32A32_UINT>,
|
|
|
|
MortonCopy<false, PixelFormat::BC1_RGBA_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::BC2_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::BC3_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::BC4_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::BC4_SNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::BC5_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::BC5_SNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::BC7_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::BC6H_UFLOAT>,
|
|
|
|
MortonCopy<false, PixelFormat::BC6H_SFLOAT>,
|
2019-03-13 15:16:45 -04:00
|
|
|
// TODO(Subv): Swizzling ASTC formats are not supported
|
|
|
|
nullptr,
|
2020-07-01 01:28:53 -04:00
|
|
|
MortonCopy<false, PixelFormat::B8G8R8A8_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::R32G32B32A32_FLOAT>,
|
|
|
|
MortonCopy<false, PixelFormat::R32G32B32A32_SINT>,
|
|
|
|
MortonCopy<false, PixelFormat::R32G32_FLOAT>,
|
|
|
|
MortonCopy<false, PixelFormat::R32G32_SINT>,
|
|
|
|
MortonCopy<false, PixelFormat::R32_FLOAT>,
|
|
|
|
MortonCopy<false, PixelFormat::R16_FLOAT>,
|
|
|
|
MortonCopy<false, PixelFormat::R16_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::R16_SNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::R16_UINT>,
|
|
|
|
MortonCopy<false, PixelFormat::R16_SINT>,
|
|
|
|
MortonCopy<false, PixelFormat::R16G16_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::R16G16_FLOAT>,
|
|
|
|
MortonCopy<false, PixelFormat::R16G16_UINT>,
|
|
|
|
MortonCopy<false, PixelFormat::R16G16_SINT>,
|
|
|
|
MortonCopy<false, PixelFormat::R16G16_SNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::R32G32B32_FLOAT>,
|
|
|
|
MortonCopy<false, PixelFormat::A8B8G8R8_SRGB>,
|
|
|
|
MortonCopy<false, PixelFormat::R8G8_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::R8G8_SNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::R8G8_SINT>,
|
|
|
|
MortonCopy<false, PixelFormat::R8G8_UINT>,
|
|
|
|
MortonCopy<false, PixelFormat::R32G32_UINT>,
|
|
|
|
MortonCopy<false, PixelFormat::R16G16B16X16_FLOAT>,
|
|
|
|
MortonCopy<false, PixelFormat::R32_UINT>,
|
|
|
|
MortonCopy<false, PixelFormat::R32_SINT>,
|
2019-03-13 15:16:45 -04:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2020-07-01 01:28:53 -04:00
|
|
|
MortonCopy<false, PixelFormat::B8G8R8A8_SRGB>,
|
|
|
|
MortonCopy<false, PixelFormat::BC1_RGBA_SRGB>,
|
|
|
|
MortonCopy<false, PixelFormat::BC2_SRGB>,
|
|
|
|
MortonCopy<false, PixelFormat::BC3_SRGB>,
|
|
|
|
MortonCopy<false, PixelFormat::BC7_SRGB>,
|
|
|
|
MortonCopy<false, PixelFormat::A4B4G4R4_UNORM>,
|
2019-03-13 15:16:45 -04:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2019-10-02 19:00:16 -04:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2020-07-01 01:28:53 -04:00
|
|
|
MortonCopy<false, PixelFormat::E5B9G9R9_FLOAT>,
|
|
|
|
MortonCopy<false, PixelFormat::D32_FLOAT>,
|
|
|
|
MortonCopy<false, PixelFormat::D16_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::D24_UNORM_S8_UINT>,
|
|
|
|
MortonCopy<false, PixelFormat::S8_UINT_D24_UNORM>,
|
|
|
|
MortonCopy<false, PixelFormat::D32_FLOAT_S8_UINT>,
|
2018-11-24 21:53:17 -05:00
|
|
|
};
|
|
|
|
|
2018-11-26 21:21:57 -05:00
|
|
|
static MortonCopyFn GetSwizzleFunction(MortonSwizzleMode mode, Surface::PixelFormat format) {
|
2018-11-24 21:53:17 -05: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-26 21:21:57 -05:00
|
|
|
UNREACHABLE();
|
2018-12-18 20:52:32 -05:00
|
|
|
return morton_to_linear_fns[static_cast<std::size_t>(format)];
|
2018-11-24 21:53:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MortonSwizzle(MortonSwizzleMode mode, Surface::PixelFormat format, u32 stride,
|
2018-11-16 12:01:54 -05:00
|
|
|
u32 block_height, u32 height, u32 block_depth, u32 depth, u32 tile_width_spacing,
|
2019-02-24 00:15:35 -05:00
|
|
|
u8* buffer, u8* addr) {
|
2018-11-16 12:01:54 -05:00
|
|
|
GetSwizzleFunction(mode, format)(stride, block_height, height, block_depth, depth,
|
2019-03-13 15:25:23 -04:00
|
|
|
tile_width_spacing, buffer, addr);
|
2018-11-24 21:53:17 -05:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:01:54 -05:00
|
|
|
} // namespace VideoCore
|