Debian Codegraph @

Source/Core/VideoCommon/TextureDecoder.h

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
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 // These values represent texture format in GX registers.
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 // Special texture format used to represent YUVY xfb copies.
37 // They aren't really textures, but they share so much hardware and usecases that it makes sense
38 // to emulate them as part of texture cache.
39 // This isn't a real value that can be used on console; it only exists for ease of implementation.
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 // The EFB Copy pipeline looks like:
63 //
64 // 1. Read EFB -> 2. Select color/depth -> 3. Downscale (optional)
65 // -> 4. YUV conversion (optional) -> 5. Encode Tiles -> 6. Write RAM
66 //
67 // The "Encode Tiles" stage receives RGBA8 texels from previous stages and encodes them to various
68 // formats. EFBCopyFormat is the tile encoder mode. Note that the tile encoder does not care about
69 // color vs. depth or intensity formats - it only sees RGBA8 texels.
70 enum class EFBCopyFormat
71 {
72 // These values represent EFB copy format in GX registers.
73 // Most (but not all) of these values correspond to values of TextureFormat.
74 R4 = 0x0, // R4, I4, Z4
75
76 // FIXME: Does 0x1 (Z8) have identical results to 0x8 (Z8H)?
77 // Is either or both of 0x1 and 0x8 used in games?
78 R8_0x1 = 0x1, // R8, I8, Z8H (?)
79
80 RA4 = 0x2, // RA4, IA4
81
82 // FIXME: Earlier versions of this file named the value 0x3 "GX_TF_Z16", which does not reflect
83 // the results one would expect when copying from the depth buffer with this format.
84 // For reference: When copying from the depth buffer, R should receive the top 8 bits of
85 // the Z value, and A should be either 0xFF or 0 (please investigate).
86 // Please test original hardware and make sure dolphin-emu implements this format
87 // correctly.
88 RA8 = 0x3, // RA8, IA8, (FIXME: Z16 too?)
89
90 RGB565 = 0x4,
91 RGB5A3 = 0x5,
92 RGBA8 = 0x6, // RGBA8, Z24
93 A8 = 0x7,
94 R8 = 0x8, // R8, I8, Z8H
95 G8 = 0x9, // G8, Z8M
96 B8 = 0xA, // B8, Z8L
97 RG8 = 0xB, // RG8, Z16R (Note: G and R are reversed)
98 GB8 = 0xC, // GB8, Z16L
99
100 // Special texture format used to represent YUVY xfb copies.
101 // They aren't really textures, but they share so much hardware and usecases that it makes sense
102 // to emulate them as part of texture cache.
103 // This isn't a real value that can be used on console; it only exists for ease of implementation.
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 // These values represent TLUT format in GX registers.
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 // Indexed and paletted formats are "compatible", that is do not require conversion.
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 // Currently, we can only reinterpret textures of the same width.
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 /* Internal method, implemented by TextureDecoder_Generic and TextureDecoder_x64. */
207 void _TexDecoder_DecodeImpl(u32* dst, const u8* src, int width, int height, TextureFormat texformat,
208 const u8* tlut, TLUTFormat tlutfmt);