1 // Copyright 2008 Dolphin Emulator Project
2 // Licensed under GPLv2+
3 // Refer to the license.txt file included.
4 
5 #pragma once
6 
7 #include <tuple>
8 #include "Common/CommonTypes.h"
9 
10 enum
11 {
12   TMEM_SIZE = 1024 * 1024,
13   TMEM_LINE_SIZE = 32,
14 };
15 alignas(16) extern u8 texMem[TMEM_SIZE];
16 
17 enum class TextureFormat
18 {
19   // These values represent texture format in GX registers.
20   I4 = 0x0,
21   I8 = 0x1,
22   IA4 = 0x2,
23   IA8 = 0x3,
24   RGB565 = 0x4,
25   RGB5A3 = 0x5,
26   RGBA8 = 0x6,
27   C4 = 0x8,
28   C8 = 0x9,
29   C14X2 = 0xA,
30   CMPR = 0xE,
31 
32   // Special texture format used to represent YUVY xfb copies.
33   // They aren't really textures, but they share so much hardware and usecases that it makes sense
34   // to emulate them as part of texture cache.
35   XFB = 0xF,
36 };
37 
IsColorIndexed(TextureFormat format)38 static inline bool IsColorIndexed(TextureFormat format)
39 {
40   return format == TextureFormat::C4 || format == TextureFormat::C8 ||
41          format == TextureFormat::C14X2;
42 }
43 
44 // The EFB Copy pipeline looks like:
45 //
46 //   1. Read EFB -> 2. Select color/depth -> 3. Downscale (optional)
47 //   -> 4. YUV conversion (optional) -> 5. Encode Tiles -> 6. Write RAM
48 //
49 // The "Encode Tiles" stage receives RGBA8 texels from previous stages and encodes them to various
50 // formats. EFBCopyFormat is the tile encoder mode. Note that the tile encoder does not care about
51 // color vs. depth or intensity formats - it only sees RGBA8 texels.
52 enum class EFBCopyFormat
53 {
54   // These values represent EFB copy format in GX registers.
55   // Most (but not all) of these values correspond to values of TextureFormat.
56   R4 = 0x0,  // R4, I4, Z4
57 
58   // FIXME: Does 0x1 (Z8) have identical results to 0x8 (Z8H)?
59   //        Is either or both of 0x1 and 0x8 used in games?
60   R8_0x1 = 0x1,  // R8, I8, Z8H (?)
61 
62   RA4 = 0x2,  // RA4, IA4
63 
64   // FIXME: Earlier versions of this file named the value 0x3 "GX_TF_Z16", which does not reflect
65   //        the results one would expect when copying from the depth buffer with this format.
66   //        For reference: When copying from the depth buffer, R should receive the top 8 bits of
67   //                       the Z value, and A should be either 0xFF or 0 (please investigate).
68   //        Please test original hardware and make sure dolphin-emu implements this format
69   //        correctly.
70   RA8 = 0x3,  // RA8, IA8, (FIXME: Z16 too?)
71 
72   RGB565 = 0x4,
73   RGB5A3 = 0x5,
74   RGBA8 = 0x6,  // RGBA8, Z24
75   A8 = 0x7,
76   R8 = 0x8,   // R8, I8, Z8H
77   G8 = 0x9,   // G8, Z8M
78   B8 = 0xA,   // B8, Z8L
79   RG8 = 0xB,  // RG8, Z16R (Note: G and R are reversed)
80   GB8 = 0xC,  // GB8, Z16L
81 
82   // Special texture format used to represent YUVY xfb copies.
83   // They aren't really textures, but they share so much hardware and usecases that it makes sense
84   // to emulate them as part of texture cache.
85   XFB = 0xF,
86 };
87 
88 enum class TLUTFormat
89 {
90   // These values represent TLUT format in GX registers.
91   IA8 = 0x0,
92   RGB565 = 0x1,
93   RGB5A3 = 0x2,
94 };
95 
IsValidTLUTFormat(TLUTFormat tlutfmt)96 static inline bool IsValidTLUTFormat(TLUTFormat tlutfmt)
97 {
98   return tlutfmt == TLUTFormat::IA8 || tlutfmt == TLUTFormat::RGB565 ||
99          tlutfmt == TLUTFormat::RGB5A3;
100 }
101 
IsCompatibleTextureFormat(TextureFormat from_format,TextureFormat to_format)102 static inline bool IsCompatibleTextureFormat(TextureFormat from_format, TextureFormat to_format)
103 {
104   if (from_format == to_format)
105     return true;
106 
107   // Indexed and paletted formats are "compatible", that is do not require conversion.
108   switch (from_format)
109   {
110   case TextureFormat::I4:
111   case TextureFormat::C4:
112     return to_format == TextureFormat::I4 || to_format == TextureFormat::C4;
113 
114   case TextureFormat::I8:
115   case TextureFormat::C8:
116     return to_format == TextureFormat::I8 || to_format == TextureFormat::C8;
117 
118   default:
119     return false;
120   }
121 }
122 
CanReinterpretTextureOnGPU(TextureFormat from_format,TextureFormat to_format)123 static inline bool CanReinterpretTextureOnGPU(TextureFormat from_format, TextureFormat to_format)
124 {
125   // Currently, we can only reinterpret textures of the same width.
126   switch (from_format)
127   {
128   case TextureFormat::I8:
129   case TextureFormat::IA4:
130     return to_format == TextureFormat::I8 || to_format == TextureFormat::IA4;
131 
132   case TextureFormat::IA8:
133   case TextureFormat::RGB565:
134   case TextureFormat::RGB5A3:
135     return to_format == TextureFormat::IA8 || to_format == TextureFormat::RGB565 ||
136            to_format == TextureFormat::RGB5A3;
137 
138   default:
139     return false;
140   }
141 }
142 
143 int TexDecoder_GetTexelSizeInNibbles(TextureFormat format);
144 int TexDecoder_GetTextureSizeInBytes(int width, int height, TextureFormat format);
145 int TexDecoder_GetBlockWidthInTexels(TextureFormat format);
146 int TexDecoder_GetBlockHeightInTexels(TextureFormat format);
147 int TexDecoder_GetEFBCopyBlockWidthInTexels(EFBCopyFormat format);
148 int TexDecoder_GetEFBCopyBlockHeightInTexels(EFBCopyFormat format);
149 int TexDecoder_GetPaletteSize(TextureFormat fmt);
150 TextureFormat TexDecoder_GetEFBCopyBaseFormat(EFBCopyFormat format);
151 
152 void TexDecoder_Decode(u8* dst, const u8* src, int width, int height, TextureFormat texformat,
153                        const u8* tlut, TLUTFormat tlutfmt);
154 void TexDecoder_DecodeRGBA8FromTmem(u8* dst, const u8* src_ar, const u8* src_gb, int width,
155                                     int height);
156 void TexDecoder_DecodeTexel(u8* dst, const u8* src, int s, int t, int imageWidth,
157                             TextureFormat texformat, const u8* tlut, TLUTFormat tlutfmt);
158 void TexDecoder_DecodeTexelRGBA8FromTmem(u8* dst, const u8* src_ar, const u8* src_gb, int s, int t,
159                                          int imageWidth);
160 void TexDecoder_DecodeXFB(u8* dst, const u8* src, u32 width, u32 height, u32 stride);
161 
162 void TexDecoder_SetTexFmtOverlayOptions(bool enable, bool center);
163 
164 /* Internal method, implemented by TextureDecoder_Generic and TextureDecoder_x64. */
165 void _TexDecoder_DecodeImpl(u32* dst, const u8* src, int width, int height, TextureFormat texformat,
166                             const u8* tlut, TLUTFormat tlutfmt);
167