2017-01-05 23:11:23 +01:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/color.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/math_util.h"
|
2017-01-06 04:59:34 +01:00
|
|
|
#include "common/swap.h"
|
2017-01-05 23:11:23 +01:00
|
|
|
#include "common/vector_math.h"
|
2017-01-28 05:51:59 +01:00
|
|
|
#include "video_core/regs_texturing.h"
|
2017-01-06 04:59:34 +01:00
|
|
|
#include "video_core/texture/etc1.h"
|
2017-01-05 23:11:23 +01:00
|
|
|
#include "video_core/texture/texture_decode.h"
|
|
|
|
#include "video_core/utils.h"
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
using TextureFormat = Pica::TexturingRegs::TextureFormat;
|
2017-01-06 04:19:06 +01:00
|
|
|
|
2019-02-19 09:09:57 +01:00
|
|
|
namespace Pica::Texture {
|
2017-01-05 23:11:23 +01:00
|
|
|
|
2018-09-06 22:03:28 +02:00
|
|
|
constexpr std::size_t TILE_SIZE = 8 * 8;
|
|
|
|
constexpr std::size_t ETC1_SUBTILES = 2 * 2;
|
2017-01-06 04:19:06 +01:00
|
|
|
|
|
|
|
size_t CalculateTileSize(TextureFormat format) {
|
|
|
|
switch (format) {
|
|
|
|
case TextureFormat::RGBA8:
|
|
|
|
return 4 * TILE_SIZE;
|
|
|
|
|
|
|
|
case TextureFormat::RGB8:
|
|
|
|
return 3 * TILE_SIZE;
|
|
|
|
|
|
|
|
case TextureFormat::RGB5A1:
|
|
|
|
case TextureFormat::RGB565:
|
|
|
|
case TextureFormat::RGBA4:
|
|
|
|
case TextureFormat::IA8:
|
|
|
|
case TextureFormat::RG8:
|
|
|
|
return 2 * TILE_SIZE;
|
|
|
|
|
|
|
|
case TextureFormat::I8:
|
|
|
|
case TextureFormat::A8:
|
|
|
|
case TextureFormat::IA4:
|
|
|
|
return 1 * TILE_SIZE;
|
|
|
|
|
|
|
|
case TextureFormat::I4:
|
|
|
|
case TextureFormat::A4:
|
|
|
|
return TILE_SIZE / 2;
|
|
|
|
|
|
|
|
case TextureFormat::ETC1:
|
|
|
|
return ETC1_SUBTILES * 8;
|
|
|
|
|
|
|
|
case TextureFormat::ETC1A4:
|
|
|
|
return ETC1_SUBTILES * 16;
|
2017-01-05 23:11:23 +01:00
|
|
|
|
2017-01-06 04:19:06 +01:00
|
|
|
default: // placeholder for yet unknown formats
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
return 0;
|
2017-01-05 23:11:23 +01:00
|
|
|
}
|
2017-01-06 04:19:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Math::Vec4<u8> LookupTexture(const u8* source, unsigned int x, unsigned int y,
|
|
|
|
const TextureInfo& info, bool disable_alpha) {
|
|
|
|
// Coordinate in tiles
|
|
|
|
const unsigned int coarse_x = x / 8;
|
|
|
|
const unsigned int coarse_y = y / 8;
|
2017-01-05 23:11:23 +01:00
|
|
|
|
2017-01-06 04:19:06 +01:00
|
|
|
// Coordinate inside the tile
|
|
|
|
const unsigned int fine_x = x % 8;
|
|
|
|
const unsigned int fine_y = y % 8;
|
|
|
|
|
|
|
|
const u8* line = source + coarse_y * info.stride;
|
|
|
|
const u8* tile = line + coarse_x * CalculateTileSize(info.format);
|
|
|
|
return LookupTexelInTile(tile, fine_x, fine_y, info, disable_alpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
Math::Vec4<u8> LookupTexelInTile(const u8* source, unsigned int x, unsigned int y,
|
|
|
|
const TextureInfo& info, bool disable_alpha) {
|
|
|
|
DEBUG_ASSERT(x < 8);
|
|
|
|
DEBUG_ASSERT(y < 8);
|
|
|
|
|
|
|
|
using VideoCore::MortonInterleave;
|
2017-01-05 23:11:23 +01:00
|
|
|
|
|
|
|
switch (info.format) {
|
2017-01-28 05:51:59 +01:00
|
|
|
case TextureFormat::RGBA8: {
|
2017-01-06 04:19:06 +01:00
|
|
|
auto res = Color::DecodeRGBA8(source + MortonInterleave(x, y) * 4);
|
2017-01-05 23:11:23 +01:00
|
|
|
return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
|
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
case TextureFormat::RGB8: {
|
2017-01-06 04:19:06 +01:00
|
|
|
auto res = Color::DecodeRGB8(source + MortonInterleave(x, y) * 3);
|
2017-01-05 23:11:23 +01:00
|
|
|
return {res.r(), res.g(), res.b(), 255};
|
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
case TextureFormat::RGB5A1: {
|
2017-01-06 04:19:06 +01:00
|
|
|
auto res = Color::DecodeRGB5A1(source + MortonInterleave(x, y) * 2);
|
2017-01-05 23:11:23 +01:00
|
|
|
return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
|
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
case TextureFormat::RGB565: {
|
2017-01-06 04:19:06 +01:00
|
|
|
auto res = Color::DecodeRGB565(source + MortonInterleave(x, y) * 2);
|
2017-01-05 23:11:23 +01:00
|
|
|
return {res.r(), res.g(), res.b(), 255};
|
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
case TextureFormat::RGBA4: {
|
2017-01-06 04:19:06 +01:00
|
|
|
auto res = Color::DecodeRGBA4(source + MortonInterleave(x, y) * 2);
|
2017-01-05 23:11:23 +01:00
|
|
|
return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
|
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
case TextureFormat::IA8: {
|
2017-01-06 04:19:06 +01:00
|
|
|
const u8* source_ptr = source + MortonInterleave(x, y) * 2;
|
2017-01-05 23:11:23 +01:00
|
|
|
|
|
|
|
if (disable_alpha) {
|
|
|
|
// Show intensity as red, alpha as green
|
|
|
|
return {source_ptr[1], source_ptr[0], 0, 255};
|
|
|
|
} else {
|
|
|
|
return {source_ptr[1], source_ptr[1], source_ptr[1], source_ptr[0]};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
case TextureFormat::RG8: {
|
2017-01-06 04:19:06 +01:00
|
|
|
auto res = Color::DecodeRG8(source + MortonInterleave(x, y) * 2);
|
2017-01-05 23:11:23 +01:00
|
|
|
return {res.r(), res.g(), 0, 255};
|
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
case TextureFormat::I8: {
|
2017-01-06 04:19:06 +01:00
|
|
|
const u8* source_ptr = source + MortonInterleave(x, y);
|
2017-01-05 23:11:23 +01:00
|
|
|
return {*source_ptr, *source_ptr, *source_ptr, 255};
|
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
case TextureFormat::A8: {
|
2017-01-06 04:19:06 +01:00
|
|
|
const u8* source_ptr = source + MortonInterleave(x, y);
|
2017-01-05 23:11:23 +01:00
|
|
|
|
|
|
|
if (disable_alpha) {
|
|
|
|
return {*source_ptr, *source_ptr, *source_ptr, 255};
|
|
|
|
} else {
|
|
|
|
return {0, 0, 0, *source_ptr};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
case TextureFormat::IA4: {
|
2017-01-06 04:19:06 +01:00
|
|
|
const u8* source_ptr = source + MortonInterleave(x, y);
|
2017-01-05 23:11:23 +01:00
|
|
|
|
|
|
|
u8 i = Color::Convert4To8(((*source_ptr) & 0xF0) >> 4);
|
|
|
|
u8 a = Color::Convert4To8((*source_ptr) & 0xF);
|
|
|
|
|
|
|
|
if (disable_alpha) {
|
|
|
|
// Show intensity as red, alpha as green
|
|
|
|
return {i, a, 0, 255};
|
|
|
|
} else {
|
|
|
|
return {i, i, i, a};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
case TextureFormat::I4: {
|
2017-01-06 04:19:06 +01:00
|
|
|
u32 morton_offset = MortonInterleave(x, y);
|
2017-01-05 23:11:23 +01:00
|
|
|
const u8* source_ptr = source + morton_offset / 2;
|
|
|
|
|
|
|
|
u8 i = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
|
|
|
|
i = Color::Convert4To8(i);
|
|
|
|
|
|
|
|
return {i, i, i, 255};
|
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
case TextureFormat::A4: {
|
2017-01-06 04:19:06 +01:00
|
|
|
u32 morton_offset = MortonInterleave(x, y);
|
2017-01-05 23:11:23 +01:00
|
|
|
const u8* source_ptr = source + morton_offset / 2;
|
|
|
|
|
|
|
|
u8 a = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
|
|
|
|
a = Color::Convert4To8(a);
|
|
|
|
|
|
|
|
if (disable_alpha) {
|
|
|
|
return {a, a, a, 255};
|
|
|
|
} else {
|
|
|
|
return {0, 0, 0, a};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
case TextureFormat::ETC1:
|
|
|
|
case TextureFormat::ETC1A4: {
|
|
|
|
bool has_alpha = (info.format == TextureFormat::ETC1A4);
|
2018-09-06 22:03:28 +02:00
|
|
|
std::size_t subtile_size = has_alpha ? 16 : 8;
|
2017-01-05 23:11:23 +01:00
|
|
|
|
|
|
|
// ETC1 further subdivides each 8x8 tile into four 4x4 subtiles
|
2017-01-06 04:19:06 +01:00
|
|
|
constexpr unsigned int subtile_width = 4;
|
|
|
|
constexpr unsigned int subtile_height = 4;
|
|
|
|
|
|
|
|
unsigned int subtile_index = (x / subtile_width) + 2 * (y / subtile_height);
|
2017-01-06 04:59:34 +01:00
|
|
|
x %= subtile_width;
|
|
|
|
y %= subtile_height;
|
2017-01-05 23:11:23 +01:00
|
|
|
|
2017-01-06 04:59:34 +01:00
|
|
|
const u8* subtile_ptr = source + subtile_index * subtile_size;
|
2017-01-05 23:11:23 +01:00
|
|
|
|
2017-01-06 04:59:34 +01:00
|
|
|
u8 alpha = 255;
|
2017-01-05 23:11:23 +01:00
|
|
|
if (has_alpha) {
|
2017-01-06 04:59:34 +01:00
|
|
|
u64_le packed_alpha;
|
|
|
|
memcpy(&packed_alpha, subtile_ptr, sizeof(u64));
|
|
|
|
subtile_ptr += sizeof(u64);
|
|
|
|
|
|
|
|
alpha = Color::Convert4To8((packed_alpha >> (4 * (x * subtile_width + y))) & 0xF);
|
2017-01-05 23:11:23 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 04:59:34 +01:00
|
|
|
u64_le subtile_data;
|
|
|
|
memcpy(&subtile_data, subtile_ptr, sizeof(u64));
|
|
|
|
|
|
|
|
return Math::MakeVec(SampleETC1Subtile(subtile_data, x, y),
|
|
|
|
disable_alpha ? (u8)255 : alpha);
|
2017-01-05 23:11:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_ERROR(HW_GPU, "Unknown texture format: {:x}", (u32)info.format);
|
2017-01-05 23:11:23 +01:00
|
|
|
DEBUG_ASSERT(false);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 05:51:59 +01:00
|
|
|
TextureInfo TextureInfo::FromPicaRegister(const TexturingRegs::TextureConfig& config,
|
|
|
|
const TexturingRegs::TextureFormat& format) {
|
2017-01-05 23:11:23 +01:00
|
|
|
TextureInfo info;
|
|
|
|
info.physical_address = config.GetPhysicalAddress();
|
|
|
|
info.width = config.width;
|
|
|
|
info.height = config.height;
|
|
|
|
info.format = format;
|
2017-01-06 04:19:06 +01:00
|
|
|
info.SetDefaultStride();
|
2017-01-05 23:11:23 +01:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2019-02-19 09:09:57 +01:00
|
|
|
} // namespace Pica::Texture
|