| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <array> |
| 7 |
#include <span> |
| 8 |
#include <tuple> |
| 9 |
|
| 10 |
#include "Common/CommonTypes.h" |
| 11 |
#include "Common/EnumFormatter.h" |
| 12 |
#include "Common/SpanUtils.h" |
| 13 |
|
| 14 |
enum |
| 15 |
{ |
| 16 |
TMEM_SIZE = 1024 * 1024, |
| 17 |
TMEM_LINE_SIZE = 32, |
| 18 |
}; |
| 19 |
alignas(16) extern std::array<u8, TMEM_SIZE> s_tex_mem; |
| 20 |
|
| 21 |
enum class TextureFormat |
| 22 |
{ |
| 23 |
|
| 24 |
I4 = 0x0, |
| 25 |
I8 = 0x1, |
| 26 |
IA4 = 0x2, |
| 27 |
IA8 = 0x3, |
| 28 |
RGB565 = 0x4, |
| 29 |
RGB5A3 = 0x5, |
| 30 |
RGBA8 = 0x6, |
| 31 |
C4 = 0x8, |
| 32 |
C8 = 0x9, |
| 33 |
C14X2 = 0xA, |
| 34 |
CMPR = 0xE, |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
XFB = 0xF, |
| 41 |
}; |
| 42 |
template <> |
| 43 |
struct fmt::formatter<TextureFormat> : EnumFormatter<TextureFormat::CMPR> |
| 44 |
{ |
| 45 |
static constexpr array_type names = {"I4", "I8", "IA4", "IA8", "RGB565", |
| 46 |
"RGB5A3", "RGBA8", nullptr, "C4", "C8", |
| 47 |
"C14X2", nullptr, nullptr, nullptr, "CMPR"}; |
| 48 |
constexpr formatter() : EnumFormatter(names) {} |
| 49 |
}; |
| 50 |
|
| 51 |
static inline bool IsColorIndexed(TextureFormat format) |
| 52 |
{ |
| 53 |
return format == TextureFormat::C4 || format == TextureFormat::C8 || |
| 54 |
format == TextureFormat::C14X2; |
| 55 |
} |
| 56 |
|
| 57 |
static inline bool IsValidTextureFormat(TextureFormat format) |
| 58 |
{ |
| 59 |
return format <= TextureFormat::RGBA8 || IsColorIndexed(format) || format == TextureFormat::CMPR; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
enum class EFBCopyFormat |
| 71 |
{ |
| 72 |
|
| 73 |
|
| 74 |
R4 = 0x0, |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
R8_0x1 = 0x1, |
| 79 |
|
| 80 |
RA4 = 0x2, |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
RA8 = 0x3, |
| 89 |
|
| 90 |
RGB565 = 0x4, |
| 91 |
RGB5A3 = 0x5, |
| 92 |
RGBA8 = 0x6, |
| 93 |
A8 = 0x7, |
| 94 |
R8 = 0x8, |
| 95 |
G8 = 0x9, |
| 96 |
B8 = 0xA, |
| 97 |
RG8 = 0xB, |
| 98 |
GB8 = 0xC, |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
XFB = 0xF, |
| 105 |
}; |
| 106 |
template <> |
| 107 |
struct fmt::formatter<EFBCopyFormat> : EnumFormatter<EFBCopyFormat::GB8> |
| 108 |
{ |
| 109 |
static constexpr array_type names = { |
| 110 |
"R4/I4/Z4", "R8/I8/Z8H (?)", "RA4/IA4", "RA8/IA8 (Z16 too?)", |
| 111 |
"RGB565", "RGB5A3", "RGBA8", "A8", |
| 112 |
"R8/I8/Z8H", "G8/Z8M", "B8/Z8L", "RG8/Z16R (Note: G and R are reversed)", |
| 113 |
"GB8/Z16L", |
| 114 |
}; |
| 115 |
constexpr formatter() : EnumFormatter(names) {} |
| 116 |
}; |
| 117 |
|
| 118 |
enum class TLUTFormat |
| 119 |
{ |
| 120 |
|
| 121 |
IA8 = 0x0, |
| 122 |
RGB565 = 0x1, |
| 123 |
RGB5A3 = 0x2, |
| 124 |
}; |
| 125 |
template <> |
| 126 |
struct fmt::formatter<TLUTFormat> : EnumFormatter<TLUTFormat::RGB5A3> |
| 127 |
{ |
| 128 |
constexpr formatter() : EnumFormatter({"IA8", "RGB565", "RGB5A3"}) {} |
| 129 |
}; |
| 130 |
|
| 131 |
static inline bool IsValidTLUTFormat(TLUTFormat tlutfmt) |
| 132 |
{ |
| 133 |
return tlutfmt == TLUTFormat::IA8 || tlutfmt == TLUTFormat::RGB565 || |
| 134 |
tlutfmt == TLUTFormat::RGB5A3; |
| 135 |
} |
| 136 |
|
| 137 |
static inline bool IsCompatibleTextureFormat(TextureFormat from_format, TextureFormat to_format) |
| 138 |
{ |
| 139 |
if (from_format == to_format) |
| 140 |
return true; |
| 141 |
|
| 142 |
|
| 143 |
switch (from_format) |
| 144 |
{ |
| 145 |
case TextureFormat::I4: |
| 146 |
case TextureFormat::C4: |
| 147 |
return to_format == TextureFormat::I4 || to_format == TextureFormat::C4; |
| 148 |
|
| 149 |
case TextureFormat::I8: |
| 150 |
case TextureFormat::C8: |
| 151 |
return to_format == TextureFormat::I8 || to_format == TextureFormat::C8; |
| 152 |
|
| 153 |
default: |
| 154 |
return false; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
static inline bool CanReinterpretTextureOnGPU(TextureFormat from_format, TextureFormat to_format) |
| 159 |
{ |
| 160 |
|
| 161 |
switch (from_format) |
| 162 |
{ |
| 163 |
case TextureFormat::I8: |
| 164 |
case TextureFormat::IA4: |
| 165 |
return to_format == TextureFormat::I8 || to_format == TextureFormat::IA4; |
| 166 |
|
| 167 |
case TextureFormat::IA8: |
| 168 |
case TextureFormat::RGB565: |
| 169 |
case TextureFormat::RGB5A3: |
| 170 |
return to_format == TextureFormat::IA8 || to_format == TextureFormat::RGB565 || |
| 171 |
to_format == TextureFormat::RGB5A3; |
| 172 |
|
| 173 |
default: |
| 174 |
return false; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
inline std::span<u8> TexDecoder_GetTmemSpan(size_t offset = 0) |
| 179 |
{ |
| 180 |
return Common::SafeSubspan(std::span<u8>(s_tex_mem), offset); |
| 181 |
} |
| 182 |
|
| 183 |
int TexDecoder_GetTexelSizeInNibbles(TextureFormat format); |
| 184 |
int TexDecoder_GetTextureSizeInBytes(int width, int height, TextureFormat format); |
| 185 |
int TexDecoder_GetBlockWidthInTexels(TextureFormat format); |
| 186 |
int TexDecoder_GetBlockHeightInTexels(TextureFormat format); |
| 187 |
int TexDecoder_GetEFBCopyBlockWidthInTexels(EFBCopyFormat format); |
| 188 |
int TexDecoder_GetEFBCopyBlockHeightInTexels(EFBCopyFormat format); |
| 189 |
int TexDecoder_GetPaletteSize(TextureFormat fmt); |
| 190 |
TextureFormat TexDecoder_GetEFBCopyBaseFormat(EFBCopyFormat format); |
| 191 |
|
| 192 |
void TexDecoder_Decode(u8* dst, const u8* src, int width, int height, TextureFormat texformat, |
| 193 |
const u8* tlut, TLUTFormat tlutfmt); |
| 194 |
void TexDecoder_DecodeRGBA8FromTmem(u8* dst, const u8* src_ar, const u8* src_gb, int width, |
| 195 |
int height); |
| 196 |
void TexDecoder_DecodeTexel(u8* dst, std::span<const u8> src, int s, int t, int imageWidth, |
| 197 |
TextureFormat texformat, std::span<const u8> tlut, TLUTFormat tlutfmt); |
| 198 |
void TexDecoder_DecodeTexelRGBA8FromTmem(u8* dst, std::span<const u8> src_ar, |
| 199 |
std::span<const u8> src_gb, int s, int t, int imageWidth); |
| 200 |
void TexDecoder_DecodeTexelRGBA8FromTmem(u8* dst, const u8* src_ar, const u8* src_gb, int s, int t, |
| 201 |
int imageWidth); |
| 202 |
void TexDecoder_DecodeXFB(u8* dst, const u8* src, u32 width, u32 height, u32 stride); |
| 203 |
|
| 204 |
void TexDecoder_SetTexFmtOverlayOptions(bool enable, bool center); |
| 205 |
|
| 206 |
|
| 207 |
void _TexDecoder_DecodeImpl(u32* dst, const u8* src, int width, int height, TextureFormat texformat, |
| 208 |
const u8* tlut, TLUTFormat tlutfmt); |