metaforce/Runtime/Graphics/CTextureBoo.cpp

904 lines
29 KiB
C++
Raw Normal View History

2019-12-11 08:37:30 +00:00
#include "Runtime/Graphics/CTexture.hpp"
#include <array>
#include "Runtime/CBasics.hpp"
2019-12-11 08:37:30 +00:00
#include "Runtime/CSimplePool.hpp"
#include "Runtime/CTextureCache.hpp"
#include "Runtime/CToken.hpp"
2019-12-11 08:37:30 +00:00
#include "Runtime/GameGlobalObjects.hpp"
#include "Runtime/Graphics/CGraphics.hpp"
2016-02-16 05:50:41 +00:00
2021-04-10 08:42:06 +00:00
namespace metaforce {
namespace {
2021-04-10 08:42:06 +00:00
logvisor::Module Log("metaforce::CTextureBoo");
struct RGBA8 {
u8 r;
u8 g;
u8 b;
u8 a;
};
struct DXT1Block {
u16 color1;
u16 color2;
std::array<u8, 4> lines;
};
2016-02-16 05:50:41 +00:00
/* GX uses this upsampling technique to extract full 8-bit range */
constexpr u8 Convert3To8(u8 v) {
2018-12-08 05:30:43 +00:00
/* Swizzle bits: 00000123 -> 12312312 */
return static_cast<u8>((u32{v} << 5) | (u32{v} << 2) | (u32{v} >> 1));
2016-02-16 05:50:41 +00:00
}
constexpr u8 Convert4To8(u8 v) {
2018-12-08 05:30:43 +00:00
/* Swizzle bits: 00001234 -> 12341234 */
return static_cast<u8>((u32{v} << 4) | u32{v});
2016-02-16 05:50:41 +00:00
}
constexpr u8 Convert5To8(u8 v) {
2018-12-08 05:30:43 +00:00
/* Swizzle bits: 00012345 -> 12345123 */
return static_cast<u8>((u32{v} << 3) | (u32{v} >> 2));
2016-02-16 05:50:41 +00:00
}
constexpr u8 Convert6To8(u8 v) {
2018-12-08 05:30:43 +00:00
/* Swizzle bits: 00123456 -> 12345612 */
return static_cast<u8>((u32{v} << 2) | (u32{v} >> 4));
2016-02-16 05:50:41 +00:00
}
} // Anonymous namespace
2016-02-16 05:50:41 +00:00
size_t CTexture::ComputeMippedTexelCount() const {
2018-12-08 05:30:43 +00:00
size_t w = x4_w;
size_t h = x6_h;
size_t ret = w * h;
for (u32 i = x8_mips; i > 1; --i) {
if (w > 1)
w /= 2;
if (h > 1)
h /= 2;
ret += w * h;
}
return ret;
2016-02-16 05:50:41 +00:00
}
size_t CTexture::ComputeMippedBlockCountDXT1() const {
2018-12-08 05:30:43 +00:00
size_t w = x4_w / 4;
size_t h = x6_h / 4;
size_t ret = w * h;
for (u32 i = x8_mips; i > 1; --i) {
if (w > 1)
w /= 2;
if (h > 1)
h /= 2;
ret += w * h;
}
return ret;
2016-02-16 05:50:41 +00:00
}
2022-02-18 00:38:31 +00:00
void CTexture::BuildI4FromGCN(CInputStream& in, aurora::zstring_view label) {
const size_t texelCount = ComputeMippedTexelCount();
2018-12-08 05:30:43 +00:00
std::unique_ptr<RGBA8[]> buf(new RGBA8[texelCount]);
int w = x4_w;
int h = x6_h;
RGBA8* targetMip = buf.get();
for (u32 mip = 0; mip < x8_mips; ++mip) {
const int bwidth = (w + 7) / 8;
const int bheight = (h + 7) / 8;
2018-12-08 05:30:43 +00:00
for (int by = 0; by < bheight; ++by) {
const int baseY = by * 8;
2018-12-08 05:30:43 +00:00
for (int bx = 0; bx < bwidth; ++bx) {
const int baseX = bx * 8;
2022-02-01 00:06:54 +00:00
for (int y = 0; y < std::min(h, 8); ++y) {
2018-12-08 05:30:43 +00:00
RGBA8* target = targetMip + (baseY + y) * w + baseX;
std::array<u8, 4> source;
in.Get(source.data(), std::min(size_t(w) / 4, source.size()));
2022-02-01 00:06:54 +00:00
for (size_t x = 0; x < std::min(w, 8); ++x) {
2018-12-08 05:30:43 +00:00
target[x].r = Convert4To8(source[x / 2] >> ((x & 1) ? 0 : 4) & 0xf);
target[x].g = target[x].r;
target[x].b = target[x].r;
target[x].a = target[x].r;
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
targetMip += w * h;
if (w > 1) {
2018-12-08 05:30:43 +00:00
w /= 2;
}
if (h > 1) {
2018-12-08 05:30:43 +00:00
h /= 2;
}
2018-12-08 05:30:43 +00:00
}
2022-02-16 05:21:24 +00:00
m_tex = aurora::gfx::new_static_texture_2d(x4_w, x6_h, x8_mips, aurora::gfx::TextureFormat::RGBA8,
{reinterpret_cast<const uint8_t*>(buf.get()), texelCount * 4}, label);
2016-02-16 05:50:41 +00:00
}
2022-02-18 00:38:31 +00:00
void CTexture::BuildI8FromGCN(CInputStream& in, aurora::zstring_view label) {
const size_t texelCount = ComputeMippedTexelCount();
2018-12-08 05:30:43 +00:00
std::unique_ptr<RGBA8[]> buf(new RGBA8[texelCount]);
int w = x4_w;
int h = x6_h;
RGBA8* targetMip = buf.get();
for (u32 mip = 0; mip < x8_mips; ++mip) {
const int bwidth = (w + 7) / 8;
const int bheight = (h + 3) / 4;
2018-12-08 05:30:43 +00:00
for (int by = 0; by < bheight; ++by) {
const int baseY = by * 4;
2018-12-08 05:30:43 +00:00
for (int bx = 0; bx < bwidth; ++bx) {
const int baseX = bx * 8;
2018-12-08 05:30:43 +00:00
for (int y = 0; y < 4; ++y) {
RGBA8* target = targetMip + (baseY + y) * w + baseX;
std::array<u8, 8> source;
in.Get(source.data(), source.size());
for (size_t x = 0; x < source.size(); ++x) {
2018-12-08 05:30:43 +00:00
target[x].r = source[x];
target[x].g = source[x];
target[x].b = source[x];
target[x].a = source[x];
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
targetMip += w * h;
if (w > 1) {
2018-12-08 05:30:43 +00:00
w /= 2;
}
if (h > 1) {
2018-12-08 05:30:43 +00:00
h /= 2;
}
2018-12-08 05:30:43 +00:00
}
2022-02-16 05:21:24 +00:00
m_tex = aurora::gfx::new_static_texture_2d(x4_w, x6_h, x8_mips, aurora::gfx::TextureFormat::RGBA8,
{reinterpret_cast<const uint8_t*>(buf.get()), texelCount * 4}, label);
2016-02-16 05:50:41 +00:00
}
2022-02-18 00:38:31 +00:00
void CTexture::BuildIA4FromGCN(CInputStream& in, aurora::zstring_view label) {
const size_t texelCount = ComputeMippedTexelCount();
2018-12-08 05:30:43 +00:00
std::unique_ptr<RGBA8[]> buf(new RGBA8[texelCount]);
int w = x4_w;
int h = x6_h;
RGBA8* targetMip = buf.get();
for (u32 mip = 0; mip < x8_mips; ++mip) {
const int bwidth = (w + 7) / 8;
const int bheight = (h + 3) / 4;
2018-12-08 05:30:43 +00:00
for (int by = 0; by < bheight; ++by) {
const int baseY = by * 4;
2018-12-08 05:30:43 +00:00
for (int bx = 0; bx < bwidth; ++bx) {
const int baseX = bx * 8;
2018-12-08 05:30:43 +00:00
for (int y = 0; y < 4; ++y) {
RGBA8* target = targetMip + (baseY + y) * w + baseX;
std::array<u8, 8> source;
in.Get(source.data(), source.size());
for (size_t x = 0; x < source.size(); ++x) {
const u8 intensity = Convert4To8(source[x] >> 4 & 0xf);
2018-12-08 05:30:43 +00:00
target[x].r = intensity;
target[x].g = intensity;
target[x].b = intensity;
target[x].a = Convert4To8(source[x] & 0xf);
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
targetMip += w * h;
if (w > 1) {
2018-12-08 05:30:43 +00:00
w /= 2;
}
if (h > 1) {
2018-12-08 05:30:43 +00:00
h /= 2;
}
2018-12-08 05:30:43 +00:00
}
2022-02-16 05:21:24 +00:00
m_tex = aurora::gfx::new_static_texture_2d(x4_w, x6_h, x8_mips, aurora::gfx::TextureFormat::RGBA8,
{reinterpret_cast<const uint8_t*>(buf.get()), texelCount * 4}, label);
2016-02-16 05:50:41 +00:00
}
2022-02-18 00:38:31 +00:00
void CTexture::BuildIA8FromGCN(CInputStream& in, aurora::zstring_view label) {
const size_t texelCount = ComputeMippedTexelCount();
2018-12-08 05:30:43 +00:00
std::unique_ptr<RGBA8[]> buf(new RGBA8[texelCount]);
int w = x4_w;
int h = x6_h;
RGBA8* targetMip = buf.get();
for (u32 mip = 0; mip < x8_mips; ++mip) {
const int bwidth = (w + 3) / 4;
const int bheight = (h + 3) / 4;
2018-12-08 05:30:43 +00:00
for (int by = 0; by < bheight; ++by) {
const int baseY = by * 4;
2018-12-08 05:30:43 +00:00
for (int bx = 0; bx < bwidth; ++bx) {
const int baseX = bx * 4;
2018-12-08 05:30:43 +00:00
for (int y = 0; y < 4; ++y) {
RGBA8* target = targetMip + (baseY + y) * w + baseX;
std::array<u16, 4> source;
in.Get(reinterpret_cast<u8*>(source.data()), sizeof(source));
for (size_t x = 0; x < source.size(); ++x) {
const u8 intensity = source[x] >> 8;
2018-12-08 05:30:43 +00:00
target[x].r = intensity;
target[x].g = intensity;
target[x].b = intensity;
target[x].a = source[x] & 0xff;
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
targetMip += w * h;
if (w > 1) {
2018-12-08 05:30:43 +00:00
w /= 2;
}
if (h > 1) {
2018-12-08 05:30:43 +00:00
h /= 2;
}
2018-12-08 05:30:43 +00:00
}
2022-02-16 05:21:24 +00:00
m_tex = aurora::gfx::new_static_texture_2d(x4_w, x6_h, x8_mips, aurora::gfx::TextureFormat::RGBA8,
{reinterpret_cast<const uint8_t*>(buf.get()), texelCount * 4}, label);
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
static std::vector<RGBA8> DecodePalette(int numEntries, CInputStream& in) {
std::vector<RGBA8> ret;
ret.reserve(numEntries);
enum class EPaletteType { IA8, RGB565, RGB5A3 };
EPaletteType format = EPaletteType(in.ReadLong());
in.ReadLong();
2018-12-08 05:30:43 +00:00
switch (format) {
case EPaletteType::IA8: {
for (int e = 0; e < numEntries; ++e) {
u8 intensity = in.ReadUint8();
u8 alpha = in.ReadUint8();
2018-12-08 05:30:43 +00:00
ret.push_back({intensity, intensity, intensity, alpha});
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
break;
}
case EPaletteType::RGB565: {
for (int e = 0; e < numEntries; ++e) {
u16 texel = in.ReadShort();
2018-12-08 05:30:43 +00:00
ret.push_back({Convert5To8(texel >> 11 & 0x1f), Convert6To8(texel >> 5 & 0x3f), Convert5To8(texel & 0x1f), 0xff});
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
break;
}
case EPaletteType::RGB5A3: {
for (int e = 0; e < numEntries; ++e) {
u16 texel = in.ReadShort();
2018-12-08 05:30:43 +00:00
if (texel & 0x8000) {
ret.push_back(
{Convert5To8(texel >> 10 & 0x1f), Convert5To8(texel >> 5 & 0x1f), Convert5To8(texel & 0x1f), 0xff});
} else {
ret.push_back({Convert4To8(texel >> 8 & 0xf), Convert4To8(texel >> 4 & 0xf), Convert4To8(texel & 0xf),
Convert3To8(texel >> 12 & 0x7)});
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
break;
}
}
return ret;
2016-02-16 05:50:41 +00:00
}
2022-02-18 00:38:31 +00:00
void CTexture::BuildC4FromGCN(CInputStream& in, aurora::zstring_view label) {
const size_t texelCount = ComputeMippedTexelCount();
2018-12-08 05:30:43 +00:00
std::unique_ptr<RGBA8[]> buf(new RGBA8[texelCount]);
std::vector<RGBA8> palette = DecodePalette(16, in);
int w = x4_w;
int h = x6_h;
RGBA8* targetMip = buf.get();
for (u32 mip = 0; mip < x8_mips; ++mip) {
const int bwidth = (w + 7) / 8;
const int bheight = (h + 7) / 8;
2018-12-08 05:30:43 +00:00
for (int by = 0; by < bheight; ++by) {
const int baseY = by * 8;
2018-12-08 05:30:43 +00:00
for (int bx = 0; bx < bwidth; ++bx) {
const int baseX = bx * 8;
2018-12-08 05:30:43 +00:00
for (int y = 0; y < 8; ++y) {
RGBA8* target = targetMip + (baseY + y) * w + baseX;
std::array<u8, 4> source;
in.Get(source.data(), source.size());
for (size_t x = 0; x < 8; ++x) {
2018-12-08 05:30:43 +00:00
target[x] = palette[source[x / 2] >> ((x & 1) ? 0 : 4) & 0xf];
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
targetMip += w * h;
if (w > 1) {
2018-12-08 05:30:43 +00:00
w /= 2;
}
if (h > 1) {
2018-12-08 05:30:43 +00:00
h /= 2;
}
2018-12-08 05:30:43 +00:00
}
2022-02-16 05:21:24 +00:00
m_tex = aurora::gfx::new_static_texture_2d(x4_w, x6_h, x8_mips, aurora::gfx::TextureFormat::RGBA8,
{reinterpret_cast<const uint8_t*>(buf.get()), texelCount * 4}, label);
2016-02-16 05:50:41 +00:00
}
2022-02-18 00:38:31 +00:00
void CTexture::BuildC8FromGCN(CInputStream& in, aurora::zstring_view label) {
const size_t texelCount = ComputeMippedTexelCount();
2018-12-08 05:30:43 +00:00
std::unique_ptr<RGBA8[]> buf(new RGBA8[texelCount]);
std::vector<RGBA8> palette = DecodePalette(256, in);
int w = x4_w;
int h = x6_h;
RGBA8* targetMip = buf.get();
for (u32 mip = 0; mip < x8_mips; ++mip) {
const int bwidth = (w + 7) / 8;
const int bheight = (h + 3) / 4;
2018-12-08 05:30:43 +00:00
for (int by = 0; by < bheight; ++by) {
const int baseY = by * 4;
2018-12-08 05:30:43 +00:00
for (int bx = 0; bx < bwidth; ++bx) {
const int baseX = bx * 8;
2018-12-08 05:30:43 +00:00
for (int y = 0; y < 4; ++y) {
RGBA8* target = targetMip + (baseY + y) * w + baseX;
std::array<u8, 8> source;
in.Get(source.data(), source.size());
for (size_t x = 0; x < source.size(); ++x) {
2018-12-08 05:30:43 +00:00
target[x] = palette[source[x]];
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
targetMip += w * h;
if (w > 1) {
2018-12-08 05:30:43 +00:00
w /= 2;
}
if (h > 1) {
2018-12-08 05:30:43 +00:00
h /= 2;
}
2018-12-08 05:30:43 +00:00
}
2022-02-16 05:21:24 +00:00
m_tex = aurora::gfx::new_static_texture_2d(x4_w, x6_h, x8_mips, aurora::gfx::TextureFormat::RGBA8,
{reinterpret_cast<const uint8_t*>(buf.get()), texelCount * 4}, label);
2016-02-16 05:50:41 +00:00
}
2022-02-18 00:38:31 +00:00
void CTexture::BuildC14X2FromGCN(CInputStream& in, aurora::zstring_view label) {
2022-02-01 00:06:54 +00:00
Log.report(logvisor::Fatal, FMT_STRING("C14X2 not implemented"));
}
2018-12-08 05:30:43 +00:00
2022-02-18 00:38:31 +00:00
void CTexture::BuildRGB565FromGCN(CInputStream& in, aurora::zstring_view label) {
const size_t texelCount = ComputeMippedTexelCount();
2018-12-08 05:30:43 +00:00
std::unique_ptr<RGBA8[]> buf(new RGBA8[texelCount]);
int w = x4_w;
int h = x6_h;
RGBA8* targetMip = buf.get();
for (u32 mip = 0; mip < x8_mips; ++mip) {
const int bwidth = (w + 3) / 4;
const int bheight = (h + 3) / 4;
2018-12-08 05:30:43 +00:00
for (int by = 0; by < bheight; ++by) {
const int baseY = by * 4;
2018-12-08 05:30:43 +00:00
for (int bx = 0; bx < bwidth; ++bx) {
const int baseX = bx * 4;
2018-12-08 05:30:43 +00:00
for (int y = 0; y < 4; ++y) {
RGBA8* target = targetMip + (baseY + y) * w + baseX;
for (size_t x = 0; x < 4; ++x) {
const u16 texel = in.ReadShort();
2018-12-08 05:30:43 +00:00
target[x].r = Convert5To8(texel >> 11 & 0x1f);
target[x].g = Convert6To8(texel >> 5 & 0x3f);
target[x].b = Convert5To8(texel & 0x1f);
target[x].a = 0xff;
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
targetMip += w * h;
if (w > 1) {
2018-12-08 05:30:43 +00:00
w /= 2;
}
if (h > 1) {
2018-12-08 05:30:43 +00:00
h /= 2;
}
2018-12-08 05:30:43 +00:00
}
2022-02-16 05:21:24 +00:00
m_tex = aurora::gfx::new_static_texture_2d(x4_w, x6_h, x8_mips, aurora::gfx::TextureFormat::RGBA8,
{reinterpret_cast<const uint8_t*>(buf.get()), texelCount * 4}, label);
2016-02-16 05:50:41 +00:00
}
2022-02-18 00:38:31 +00:00
void CTexture::BuildRGB5A3FromGCN(CInputStream& in, aurora::zstring_view label) {
2018-12-08 05:30:43 +00:00
size_t texelCount = ComputeMippedTexelCount();
std::unique_ptr<RGBA8[]> buf(new RGBA8[texelCount]);
int w = x4_w;
int h = x6_h;
RGBA8* targetMip = buf.get();
for (u32 mip = 0; mip < x8_mips; ++mip) {
const int bwidth = (w + 3) / 4;
const int bheight = (h + 3) / 4;
2018-12-08 05:30:43 +00:00
for (int by = 0; by < bheight; ++by) {
const int baseY = by * 4;
2018-12-08 05:30:43 +00:00
for (int bx = 0; bx < bwidth; ++bx) {
const int baseX = bx * 4;
2018-12-08 05:30:43 +00:00
for (int y = 0; y < 4; ++y) {
RGBA8* target = targetMip + (baseY + y) * w + baseX;
for (size_t x = 0; x < 4; ++x) {
const u16 texel = in.ReadShort();
if ((texel & 0x8000) != 0) {
2018-12-08 05:30:43 +00:00
target[x].r = Convert5To8(texel >> 10 & 0x1f);
target[x].g = Convert5To8(texel >> 5 & 0x1f);
target[x].b = Convert5To8(texel & 0x1f);
target[x].a = 0xff;
} else {
target[x].r = Convert4To8(texel >> 8 & 0xf);
target[x].g = Convert4To8(texel >> 4 & 0xf);
target[x].b = Convert4To8(texel & 0xf);
target[x].a = Convert3To8(texel >> 12 & 0x7);
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
targetMip += w * h;
if (w > 1) {
2018-12-08 05:30:43 +00:00
w /= 2;
}
if (h > 1) {
2018-12-08 05:30:43 +00:00
h /= 2;
}
2018-12-08 05:30:43 +00:00
}
2022-02-16 05:21:24 +00:00
m_tex = aurora::gfx::new_static_texture_2d(x4_w, x6_h, x8_mips, aurora::gfx::TextureFormat::RGBA8,
{reinterpret_cast<const uint8_t*>(buf.get()), texelCount * 4}, label);
2016-02-16 05:50:41 +00:00
}
2022-02-18 00:38:31 +00:00
void CTexture::BuildRGBA8FromGCN(CInputStream& in, aurora::zstring_view label) {
const size_t texelCount = ComputeMippedTexelCount();
2018-12-08 05:30:43 +00:00
std::unique_ptr<RGBA8[]> buf(new RGBA8[texelCount]);
int w = x4_w;
int h = x6_h;
RGBA8* targetMip = buf.get();
for (u32 mip = 0; mip < x8_mips; ++mip) {
const int bwidth = (w + 3) / 4;
const int bheight = (h + 3) / 4;
2018-12-08 05:30:43 +00:00
for (int by = 0; by < bheight; ++by) {
const int baseY = by * 4;
2018-12-08 05:30:43 +00:00
for (int bx = 0; bx < bwidth; ++bx) {
const int baseX = bx * 4;
2018-12-08 05:30:43 +00:00
for (int c = 0; c < 2; ++c) {
for (int y = 0; y < 4; ++y) {
RGBA8* target = targetMip + (baseY + y) * w + baseX;
std::array<u8, 8> source;
in.Get(source.data(), source.size());
for (size_t x = 0; x < 4; ++x) {
if (c != 0) {
2018-12-08 05:30:43 +00:00
target[x].g = source[x * 2];
target[x].b = source[x * 2 + 1];
} else {
target[x].a = source[x * 2];
target[x].r = source[x * 2 + 1];
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
targetMip += w * h;
if (w > 1) {
2018-12-08 05:30:43 +00:00
w /= 2;
}
if (h > 1) {
2018-12-08 05:30:43 +00:00
h /= 2;
}
2018-12-08 05:30:43 +00:00
}
2022-02-16 05:21:24 +00:00
m_tex = aurora::gfx::new_static_texture_2d(x4_w, x6_h, x8_mips, aurora::gfx::TextureFormat::RGBA8,
{reinterpret_cast<const uint8_t*>(buf.get()), texelCount * 4}, label);
2016-02-16 05:50:41 +00:00
}
2022-02-18 00:38:31 +00:00
void CTexture::BuildDXT1FromGCN(CInputStream& in, aurora::zstring_view label) {
const size_t blockCount = ComputeMippedBlockCountDXT1();
2018-12-08 05:30:43 +00:00
std::unique_ptr<DXT1Block[]> buf(new DXT1Block[blockCount]);
int w = x4_w / 4;
int h = x6_h / 4;
DXT1Block* targetMip = buf.get();
for (u32 mip = 0; mip < x8_mips; ++mip) {
const int bwidth = (w + 1) / 2;
const int bheight = (h + 1) / 2;
2018-12-08 05:30:43 +00:00
for (int by = 0; by < bheight; ++by) {
const int baseY = by * 2;
2018-12-08 05:30:43 +00:00
for (int bx = 0; bx < bwidth; ++bx) {
const int baseX = bx * 2;
2018-12-08 05:30:43 +00:00
for (int y = 0; y < 2; ++y) {
DXT1Block* target = targetMip + (baseY + y) * w + baseX;
std::array<DXT1Block, 2> source;
in.Get(reinterpret_cast<u8*>(source.data()), sizeof(source));
for (size_t x = 0; x < source.size(); ++x) {
target[x].color1 = CBasics::SwapBytes(source[x].color1);
target[x].color2 = CBasics::SwapBytes(source[x].color2);
for (size_t i = 0; i < 4; ++i) {
std::array<u8, 4> ind;
const u8 packed = source[x].lines[i];
2018-12-08 05:30:43 +00:00
ind[3] = packed & 0x3;
ind[2] = (packed >> 2) & 0x3;
ind[1] = (packed >> 4) & 0x3;
ind[0] = (packed >> 6) & 0x3;
target[x].lines[i] = ind[0] | (ind[1] << 2) | (ind[2] << 4) | (ind[3] << 6);
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
targetMip += w * h;
if (w > 1) {
2018-12-08 05:30:43 +00:00
w /= 2;
}
if (h > 1) {
2018-12-08 05:30:43 +00:00
h /= 2;
}
2018-12-08 05:30:43 +00:00
}
2022-02-16 05:21:24 +00:00
m_tex = aurora::gfx::new_static_texture_2d(x4_w, x6_h, x8_mips, aurora::gfx::TextureFormat::DXT1,
{reinterpret_cast<const uint8_t*>(buf.get()), blockCount * 8}, label);
2016-03-31 21:06:41 +00:00
}
2022-02-18 00:38:31 +00:00
void CTexture::BuildRGBA8(const void* data, size_t length, aurora::zstring_view label) {
2018-12-08 05:30:43 +00:00
size_t texelCount = ComputeMippedTexelCount();
size_t expectedSize = texelCount * 4;
if (expectedSize > length)
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Fatal, FMT_STRING("insufficient TXTR length ({}/{})"), length, expectedSize);
2018-12-08 05:30:43 +00:00
2022-02-16 05:21:24 +00:00
m_tex = aurora::gfx::new_static_texture_2d(x4_w, x6_h, x8_mips, aurora::gfx::TextureFormat::RGBA8,
{reinterpret_cast<const uint8_t*>(data), expectedSize}, label);
}
2022-02-18 00:38:31 +00:00
void CTexture::BuildC8(const void* data, size_t length, aurora::zstring_view label) {
2018-12-08 05:30:43 +00:00
size_t texelCount = ComputeMippedTexelCount();
if (texelCount > length)
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Fatal, FMT_STRING("insufficient TXTR length ({}/{})"), length, texelCount);
2017-01-29 03:58:16 +00:00
uint32_t nentries = CBasics::SwapBytes(*reinterpret_cast<const uint32_t*>(data));
2022-02-01 00:06:54 +00:00
const u8* paletteTexels = reinterpret_cast<const u8*>(data) + 4;
const u8* texels = reinterpret_cast<const u8*>(data) + 4 + nentries * 4;
2022-02-16 05:21:24 +00:00
m_paletteTex = aurora::gfx::new_static_texture_2d(nentries, 1, 1, aurora::gfx::TextureFormat::RGBA8,
{paletteTexels, nentries * 4}, label);
m_tex = aurora::gfx::new_static_texture_2d(x4_w, x6_h, x8_mips, aurora::gfx::TextureFormat::R8, {texels, texelCount},
label);
2017-01-29 03:58:16 +00:00
}
2022-02-18 00:38:31 +00:00
void CTexture::BuildC8Font(const void* data, EFontType ftype, aurora::zstring_view label) {
2018-12-08 05:30:43 +00:00
size_t texelCount = ComputeMippedTexelCount();
size_t layerCount = 1;
switch (ftype) {
case EFontType::OneLayer:
case EFontType::OneLayerOutline:
layerCount = 1;
break;
case EFontType::FourLayers:
layerCount = 4;
break;
case EFontType::TwoLayersOutlines:
case EFontType::TwoLayers:
case EFontType::TwoLayersOutlines2:
layerCount = 2;
break;
default:
break;
}
const uint32_t nentries = CBasics::SwapBytes(*reinterpret_cast<const uint32_t*>(data));
2018-12-08 05:30:43 +00:00
const u8* texels = reinterpret_cast<const u8*>(data) + 4 + nentries * 4;
auto buf = std::make_unique<RGBA8[]>(texelCount * layerCount);
2018-12-08 05:30:43 +00:00
size_t w = x4_w;
size_t h = x6_h;
RGBA8* bufCur = buf.get();
for (size_t i = 0; i < x8_mips; ++i) {
size_t tCount = w * h;
RGBA8* l0 = bufCur;
RGBA8* l1 = bufCur + tCount;
RGBA8* l2 = bufCur + tCount * 2;
RGBA8* l3 = bufCur + tCount * 3;
for (size_t j = 0; j < tCount; ++j) {
u8 texel = texels[j];
switch (ftype) {
case EFontType::OneLayer:
l0[j].r = (texel & 0x1) ? 0xff : 0;
l0[j].a = 0xff;
2016-02-16 05:50:41 +00:00
break;
2018-12-08 05:30:43 +00:00
case EFontType::OneLayerOutline:
l0[j].r = (texel & 0x1) ? 0xff : 0;
l0[j].g = (texel & 0x2) ? 0xff : 0;
l0[j].a = 0xff;
2016-02-16 05:50:41 +00:00
break;
2018-12-08 05:30:43 +00:00
case EFontType::FourLayers:
l0[j].r = (texel & 0x1) ? 0xff : 0;
l0[j].a = 0xff;
l1[j].r = (texel & 0x2) ? 0xff : 0;
l1[j].a = 0xff;
l2[j].r = (texel & 0x4) ? 0xff : 0;
l2[j].a = 0xff;
l3[j].r = (texel & 0x8) ? 0xff : 0;
l3[j].a = 0xff;
2016-02-16 05:50:41 +00:00
break;
2018-12-08 05:30:43 +00:00
case EFontType::TwoLayersOutlines:
l0[j].r = (texel & 0x1) ? 0xff : 0;
l0[j].g = (texel & 0x2) ? 0xff : 0;
l0[j].a = 0xff;
l1[j].r = (texel & 0x4) ? 0xff : 0;
l1[j].g = (texel & 0x8) ? 0xff : 0;
l1[j].a = 0xff;
2016-02-16 05:50:41 +00:00
break;
2018-12-08 05:30:43 +00:00
case EFontType::TwoLayers:
l0[j].r = (texel & 0x1) ? 0xff : 0;
l0[j].a = 0xff;
l1[j].r = (texel & 0x2) ? 0xff : 0;
l1[j].a = 0xff;
2016-02-16 05:50:41 +00:00
break;
2018-12-08 05:30:43 +00:00
case EFontType::TwoLayersOutlines2:
l0[j].r = (texel & 0x4) ? 0xff : 0;
l0[j].g = (texel & 0x1) ? 0xff : 0;
l0[j].a = 0xff;
l1[j].r = (texel & 0x8) ? 0xff : 0;
l1[j].g = (texel & 0x2) ? 0xff : 0;
l1[j].a = 0xff;
2016-02-16 05:50:41 +00:00
break;
2018-12-08 05:30:43 +00:00
default:
2016-02-16 05:50:41 +00:00
break;
2018-12-08 05:30:43 +00:00
}
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
texels += tCount;
bufCur += tCount * layerCount;
if (w > 1)
w /= 2;
if (h > 1)
h /= 2;
}
2022-02-01 00:06:54 +00:00
// TODO array tex
// CGraphics::CommitResources([&](boo::IGraphicsDataFactory::Context& ctx) {
// m_booTex = ctx.newStaticArrayTexture(x4_w, x6_h, layerCount, x8_mips, boo::TextureFormat::RGBA8,
// boo::TextureClampMode::Repeat, buf.get(), texelCount * layerCount * 4)
// .get();
// return true;
// } BooTrace);
}
2022-02-18 00:38:31 +00:00
void CTexture::BuildDXT1(const void* data, size_t length, aurora::zstring_view label) {
2022-02-16 05:21:24 +00:00
m_tex = aurora::gfx::new_static_texture_2d(x4_w, x6_h, x8_mips, aurora::gfx::TextureFormat::DXT1,
{reinterpret_cast<const uint8_t*>(data), length}, label);
2018-12-08 05:30:43 +00:00
}
2016-12-23 06:41:39 +00:00
2022-02-18 00:38:31 +00:00
void CTexture::BuildDXT3(const void* data, size_t length, aurora::zstring_view label) {
2022-02-16 05:21:24 +00:00
m_tex = aurora::gfx::new_static_texture_2d(x4_w, x6_h, x8_mips, aurora::gfx::TextureFormat::DXT3,
{reinterpret_cast<const uint8_t*>(data), length}, label);
2016-02-16 05:50:41 +00:00
}
2022-02-01 00:06:54 +00:00
static std::string_view TextureFormatString(ETexelFormat format) {
switch (format) {
case ETexelFormat::I4:
return "I4"sv;
case ETexelFormat::I8:
return "I8"sv;
case ETexelFormat::IA4:
return "IA4"sv;
case ETexelFormat::IA8:
return "IA8"sv;
case ETexelFormat::C4:
return "C4"sv;
case ETexelFormat::C8:
return "C8"sv;
case ETexelFormat::C14X2:
return "C14X2"sv;
case ETexelFormat::RGB565:
return "RGB565"sv;
case ETexelFormat::RGB5A3:
return "RGB5A3"sv;
case ETexelFormat::RGBA8:
return "RGBA8"sv;
case ETexelFormat::CMPR:
return "CMPR"sv;
case ETexelFormat::RGBA8PC:
return "RGBA8PC"sv;
case ETexelFormat::C8PC:
return "C8PC"sv;
case ETexelFormat::CMPRPC:
return "CMPRPC"sv;
case ETexelFormat::CMPRPCA:
return "CMPRPCA"sv;
}
2019-03-03 06:19:42 +00:00
}
2018-12-08 05:30:43 +00:00
CTexture::CTexture(ETexelFormat fmt, s16 w, s16 h, s32 mips) : x0_fmt(fmt), x4_w(w), x6_h(h), x8_mips(mips) {
/*
x64_ = sMangleMipmaps;
InitBitmapBuffers(fmt, w, h, mips);
InitTextureObjs();
*/
}
2016-02-16 05:50:41 +00:00
2022-02-01 00:06:54 +00:00
CTexture::CTexture(std::unique_ptr<u8[]>&& in, u32 length, bool otex, const CTextureInfo* inf, CAssetId id)
: m_textureInfo(inf) {
2018-12-08 05:30:43 +00:00
std::unique_ptr<u8[]> owned = std::move(in);
CMemoryInStream r(owned.get(), length, CMemoryInStream::EOwnerShip::NotOwned);
x0_fmt = ETexelFormat(r.ReadLong());
x4_w = r.ReadShort();
x6_h = r.ReadShort();
x8_mips = r.ReadLong();
2018-12-08 05:30:43 +00:00
2022-02-01 00:06:54 +00:00
auto label = fmt::format(FMT_STRING("TXTR {:08X} ({})"), id.Value(), TextureFormatString(x0_fmt));
2018-12-08 05:30:43 +00:00
switch (x0_fmt) {
case ETexelFormat::I4:
2022-02-01 00:06:54 +00:00
BuildI4FromGCN(r, label);
2018-12-08 05:30:43 +00:00
break;
case ETexelFormat::I8:
2022-02-01 00:06:54 +00:00
BuildI8FromGCN(r, label);
2018-12-08 05:30:43 +00:00
break;
case ETexelFormat::IA4:
2022-02-01 00:06:54 +00:00
BuildIA4FromGCN(r, label);
2018-12-08 05:30:43 +00:00
break;
case ETexelFormat::IA8:
2022-02-01 00:06:54 +00:00
BuildIA8FromGCN(r, label);
2018-12-08 05:30:43 +00:00
break;
case ETexelFormat::C4:
2022-02-01 00:06:54 +00:00
BuildC4FromGCN(r, label);
2018-12-08 05:30:43 +00:00
break;
case ETexelFormat::C8:
2022-02-01 00:06:54 +00:00
BuildC8FromGCN(r, label);
2018-12-08 05:30:43 +00:00
break;
case ETexelFormat::C14X2:
2022-02-01 00:06:54 +00:00
BuildC14X2FromGCN(r, label);
2018-12-08 05:30:43 +00:00
break;
case ETexelFormat::RGB565:
2022-02-01 00:06:54 +00:00
BuildRGB565FromGCN(r, label);
2018-12-08 05:30:43 +00:00
break;
case ETexelFormat::RGB5A3:
2022-02-01 00:06:54 +00:00
BuildRGB5A3FromGCN(r, label);
2018-12-08 05:30:43 +00:00
break;
case ETexelFormat::RGBA8:
2022-02-01 00:06:54 +00:00
BuildRGBA8FromGCN(r, label);
2018-12-08 05:30:43 +00:00
break;
case ETexelFormat::CMPR:
2022-02-16 05:21:24 +00:00
if (aurora::gfx::get_dxt_compression_supported()) {
BuildDXT1FromGCN(r, label);
} else {
Log.report(logvisor::Error, FMT_STRING("BC/DXT1 compression is not supported on your GPU, unable to load {}"),
label);
x0_fmt = ETexelFormat::RGBA8PC;
x8_mips = 1;
2022-02-16 05:21:24 +00:00
std::unique_ptr<u8[]> data = std::make_unique<u8[]>(x4_w * x6_h * 4);
/* Build a fake texture to use */
for (u32 i = 0; i < (x4_w * x6_h) * 4; i += 4) {
data[i + 0] = 0xFF;
data[i + 1] = 0x00;
data[i + 2] = 0xFF;
data[i + 3] = 0xFF;
}
2022-02-16 05:21:24 +00:00
m_tex = aurora::gfx::new_static_texture_2d(x4_w, x6_h, x8_mips, aurora::gfx::TextureFormat::RGBA8,
{reinterpret_cast<const uint8_t*>(data.get()), (x4_w * x6_h * 4ul)},
label);
}
2018-12-08 05:30:43 +00:00
break;
case ETexelFormat::RGBA8PC:
2022-02-01 00:06:54 +00:00
BuildRGBA8(owned.get() + 12, length - 12, label);
2018-12-08 05:30:43 +00:00
break;
case ETexelFormat::C8PC:
2022-02-01 00:06:54 +00:00
BuildC8(owned.get() + 12, length - 12, label);
2018-12-08 05:30:43 +00:00
otex = true;
break;
case ETexelFormat::CMPRPC:
2022-02-01 00:06:54 +00:00
BuildDXT1(owned.get() + 12, length - 12, label);
2018-12-08 05:30:43 +00:00
break;
2019-03-03 06:19:42 +00:00
case ETexelFormat::CMPRPCA:
2022-02-01 00:06:54 +00:00
BuildDXT3(owned.get() + 12, length - 12, label);
2019-03-03 06:19:42 +00:00
break;
2018-12-08 05:30:43 +00:00
default:
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Fatal, FMT_STRING("invalid texture type {} for boo"), int(x0_fmt));
2018-12-08 05:30:43 +00:00
}
if (otex)
m_otex = std::move(owned);
2016-02-16 05:50:41 +00:00
}
2018-12-08 05:30:43 +00:00
void CTexture::Load(int slot, EClampMode clamp) const {}
2016-12-23 06:41:39 +00:00
std::unique_ptr<u8[]> CTexture::BuildMemoryCardTex(u32& sizeOut, ETexelFormat& fmtOut,
2018-12-08 05:30:43 +00:00
std::unique_ptr<u8[]>& paletteOut) const {
if (!m_otex)
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Fatal, FMT_STRING("MemoryCard TXTR not loaded with 'otex'"));
2018-12-08 05:30:43 +00:00
size_t texelCount = x4_w * x6_h;
std::unique_ptr<u8[]> ret;
if (x0_fmt == ETexelFormat::RGBA8PC) {
sizeOut = texelCount * 2;
fmtOut = ETexelFormat::RGB5A3;
ret.reset(new u8[sizeOut]);
u16* texel = reinterpret_cast<u16*>(ret.get());
int w = x4_w;
int h = x6_h;
const RGBA8* sourceMip = reinterpret_cast<const RGBA8*>(m_otex.get() + 12);
int bwidth = (w + 3) / 4;
int bheight = (h + 3) / 4;
for (int by = 0; by < bheight; ++by) {
int baseY = by * 4;
for (int bx = 0; bx < bwidth; ++bx) {
int baseX = bx * 4;
for (int y = 0; y < 4; ++y) {
const RGBA8* source = sourceMip + (x6_h - (baseY + y) - 1) * w + baseX;
for (int x = 0; x < 4; ++x) {
if (source[x].a == 0xff) {
*texel++ = CBasics::SwapBytes(u16((source[x].r >> 3 << 10) | (source[x].g >> 3 << 5) | (source[x].b >> 3)));
2018-12-08 05:30:43 +00:00
} else {
*texel++ = CBasics::SwapBytes(u16((source[x].r >> 4 << 8) | (source[x].g >> 4 << 4) | (source[x].b >> 4) |
2018-12-08 05:30:43 +00:00
(source[x].a >> 5 << 12)));
2016-12-23 06:41:39 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-12-23 06:41:39 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-12-23 06:41:39 +00:00
}
2018-12-08 05:30:43 +00:00
} else if (x0_fmt == ETexelFormat::C8PC) {
sizeOut = texelCount;
fmtOut = ETexelFormat::C8;
ret.reset(new u8[sizeOut]);
paletteOut.reset(new u8[512]);
u8* texel = ret.get();
u16* paletteColors = reinterpret_cast<u16*>(paletteOut.get());
int w = x4_w;
int h = x6_h;
const u8* data = m_otex.get() + 12;
u32 nentries = CBasics::SwapBytes(*reinterpret_cast<const u32*>(data));
2018-12-08 05:30:43 +00:00
const RGBA8* paletteTexels = reinterpret_cast<const RGBA8*>(data + 4);
const u8* sourceMip = data + 4 + nentries * 4;
for (u32 i = 0; i < 256; ++i) {
2018-12-08 05:30:43 +00:00
u16& color = paletteColors[i];
if (i >= nentries) {
color = 0;
} else {
const RGBA8& colorIn = paletteTexels[i];
if (colorIn.a == 0xff) {
color = CBasics::SwapBytes(u16((colorIn.r >> 3 << 10) | (colorIn.g >> 3 << 5) | (colorIn.b >> 3) | 0x8000));
2018-12-08 05:30:43 +00:00
} else {
color = CBasics::SwapBytes(
2018-12-08 05:30:43 +00:00
u16((colorIn.r >> 4 << 8) | (colorIn.g >> 4 << 4) | (colorIn.b >> 4) | (colorIn.a >> 5 << 12)));
2016-12-23 06:41:39 +00:00
}
2018-12-08 05:30:43 +00:00
}
}
2016-12-23 06:41:39 +00:00
2018-12-08 05:30:43 +00:00
int bwidth = (w + 7) / 8;
int bheight = (h + 3) / 4;
for (int by = 0; by < bheight; ++by) {
int baseY = by * 4;
for (int bx = 0; bx < bwidth; ++bx) {
int baseX = bx * 8;
for (int y = 0; y < 4; ++y) {
const u8* source = sourceMip + (x6_h - (baseY + y) - 1) * w + baseX;
for (int x = 0; x < 8; ++x)
*texel++ = source[x];
2016-12-23 06:41:39 +00:00
}
2018-12-08 05:30:43 +00:00
}
2016-12-23 06:41:39 +00:00
}
2018-12-08 05:30:43 +00:00
} else
2020-04-11 22:51:39 +00:00
Log.report(logvisor::Fatal, FMT_STRING("MemoryCard texture may only use RGBA8PC or C8PC format"));
2016-12-23 06:41:39 +00:00
2018-12-08 05:30:43 +00:00
return ret;
2016-12-23 06:41:39 +00:00
}
2022-02-18 00:38:31 +00:00
const aurora::gfx::TextureHandle& CTexture::GetFontTexture(EFontType tp) {
2018-12-08 05:30:43 +00:00
if (m_ftype != tp && x0_fmt == ETexelFormat::C8PC) {
m_ftype = tp;
2022-02-18 00:38:31 +00:00
BuildC8Font(m_otex.get() + 12, m_ftype, "Font (TODO)");
2018-12-08 05:30:43 +00:00
}
2022-02-01 00:06:54 +00:00
return m_tex;
2017-01-29 03:58:16 +00:00
}
2021-04-10 08:42:06 +00:00
CFactoryFnReturn FTextureFactory(const metaforce::SObjectTag& tag, std::unique_ptr<u8[]>&& in, u32 len,
const metaforce::CVParamTransfer& vparms, CObjectReference* selfRef) {
2018-12-08 05:30:43 +00:00
u32 u32Owned = vparms.GetOwnedObj<u32>();
const CTextureInfo* inf = nullptr;
if (g_TextureCache)
2021-06-07 19:29:18 +00:00
inf = g_TextureCache->GetTextureInfo(tag.id);
2019-12-11 08:37:30 +00:00
return TToken<CTexture>::GetIObjObjectFor(
2022-02-01 00:06:54 +00:00
std::make_unique<CTexture>(std::move(in), len, u32Owned == SBIG('OTEX'), inf, tag.id));
2016-02-16 05:50:41 +00:00
}
2021-04-10 08:42:06 +00:00
} // namespace metaforce