1 // Copyright (c) 2012- PPSSPP Project.
2
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 2.0 for more details.
11
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18 #pragma once
19
20 enum CheckAlphaResult {
21 // These are intended to line up with TexCacheEntry::STATUS_ALPHA_UNKNOWN, etc.
22 CHECKALPHA_FULL = 0,
23 CHECKALPHA_ANY = 4,
24 };
25
26 #include "ppsspp_config.h"
27 #include "Common/Common.h"
28 #include "Common/Swap.h"
29 #include "Core/MemMap.h"
30 #include "Core/ConfigValues.h"
31 #include "GPU/ge_constants.h"
32 #include "GPU/Common/TextureDecoderNEON.h"
33 #include "GPU/GPUState.h"
34
35 void SetupTextureDecoder();
36
37 // Pitch must be aligned to 16 bits (as is the case on a PSP)
38 void DoSwizzleTex16(const u32 *ysrcp, u8 *texptr, int bxc, int byc, u32 pitch);
39
40 // For SSE, we statically link the SSE2 algorithms.
41 #if defined(_M_SSE)
42 u32 QuickTexHashSSE2(const void *checkp, u32 size);
43 #define DoQuickTexHash QuickTexHashSSE2
44 #define StableQuickTexHash QuickTexHashSSE2
45
46 // Pitch must be aligned to 16 bytes (as is the case on a PSP)
47 void DoUnswizzleTex16Basic(const u8 *texptr, u32 *ydestp, int bxc, int byc, u32 pitch);
48 #define DoUnswizzleTex16 DoUnswizzleTex16Basic
49
50 // For ARM64, NEON is mandatory, so we also statically link.
51 #elif PPSSPP_ARCH(ARM64)
52 #define DoQuickTexHash QuickTexHashNEON
53 #define StableQuickTexHash QuickTexHashNEON
54 #define DoUnswizzleTex16 DoUnswizzleTex16NEON
55 #else
56 typedef u32 (*QuickTexHashFunc)(const void *checkp, u32 size);
57 extern QuickTexHashFunc DoQuickTexHash;
58 extern QuickTexHashFunc StableQuickTexHash;
59
60 typedef void (*UnswizzleTex16Func)(const u8 *texptr, u32 *ydestp, int bxc, int byc, u32 pitch);
61 extern UnswizzleTex16Func DoUnswizzleTex16;
62 #endif
63
64 CheckAlphaResult CheckAlphaRGBA8888Basic(const u32 *pixelData, int stride, int w, int h);
65 CheckAlphaResult CheckAlphaABGR4444Basic(const u32 *pixelData, int stride, int w, int h);
66 CheckAlphaResult CheckAlphaRGBA4444Basic(const u32 *pixelData, int stride, int w, int h);
67 CheckAlphaResult CheckAlphaABGR1555Basic(const u32 *pixelData, int stride, int w, int h);
68 CheckAlphaResult CheckAlphaRGBA5551Basic(const u32 *pixelData, int stride, int w, int h);
69
70 // All these DXT structs are in the reverse order, as compared to PC.
71 // On PC, alpha comes before color, and interpolants are before the tile data.
72
73 struct DXT1Block {
74 u8 lines[4];
75 u16_le color1;
76 u16_le color2;
77 };
78
79 struct DXT3Block {
80 DXT1Block color;
81 u16_le alphaLines[4];
82 };
83
84 struct DXT5Block {
85 DXT1Block color;
86 u32_le alphadata2;
87 u16_le alphadata1;
88 u8 alpha1; u8 alpha2;
89 };
90
91 void DecodeDXT1Block(u32 *dst, const DXT1Block *src, int pitch, int height, bool ignore1bitAlpha);
92 void DecodeDXT3Block(u32 *dst, const DXT3Block *src, int pitch, int height);
93 void DecodeDXT5Block(u32 *dst, const DXT5Block *src, int pitch, int height);
94
95 uint32_t GetDXT1Texel(const DXT1Block *src, int x, int y);
96 uint32_t GetDXT3Texel(const DXT3Block *src, int x, int y);
97 uint32_t GetDXT5Texel(const DXT5Block *src, int x, int y);
98
99 static const u8 textureBitsPerPixel[16] = {
100 16, //GE_TFMT_5650,
101 16, //GE_TFMT_5551,
102 16, //GE_TFMT_4444,
103 32, //GE_TFMT_8888,
104 4, //GE_TFMT_CLUT4,
105 8, //GE_TFMT_CLUT8,
106 16, //GE_TFMT_CLUT16,
107 32, //GE_TFMT_CLUT32,
108 4, //GE_TFMT_DXT1,
109 8, //GE_TFMT_DXT3,
110 8, //GE_TFMT_DXT5,
111 0, // INVALID,
112 0, // INVALID,
113 0, // INVALID,
114 0, // INVALID,
115 0, // INVALID,
116 };
117
118 u32 GetTextureBufw(int level, u32 texaddr, GETextureFormat format);
119
120 template <typename IndexT, typename ClutT>
DeIndexTexture(ClutT * dest,const IndexT * indexed,int length,const ClutT * clut)121 inline void DeIndexTexture(ClutT *dest, const IndexT *indexed, int length, const ClutT *clut) {
122 // Usually, there is no special offset, mask, or shift.
123 const bool nakedIndex = gstate.isClutIndexSimple();
124
125 if (nakedIndex) {
126 if (sizeof(IndexT) == 1) {
127 for (int i = 0; i < length; ++i) {
128 *dest++ = clut[*indexed++];
129 }
130 } else {
131 for (int i = 0; i < length; ++i) {
132 *dest++ = clut[(*indexed++) & 0xFF];
133 }
134 }
135 } else {
136 for (int i = 0; i < length; ++i) {
137 *dest++ = clut[gstate.transformClutIndex(*indexed++)];
138 }
139 }
140 }
141
142 template <typename IndexT, typename ClutT>
DeIndexTexture(ClutT * dest,const u32 texaddr,int length,const ClutT * clut)143 inline void DeIndexTexture(ClutT *dest, const u32 texaddr, int length, const ClutT *clut) {
144 const IndexT *indexed = (const IndexT *) Memory::GetPointer(texaddr);
145 DeIndexTexture(dest, indexed, length, clut);
146 }
147
148 template <typename ClutT>
DeIndexTexture4(ClutT * dest,const u8 * indexed,int length,const ClutT * clut)149 inline void DeIndexTexture4(ClutT *dest, const u8 *indexed, int length, const ClutT *clut) {
150 // Usually, there is no special offset, mask, or shift.
151 const bool nakedIndex = gstate.isClutIndexSimple();
152
153 if (nakedIndex) {
154 for (int i = 0; i < length; i += 2) {
155 u8 index = *indexed++;
156 dest[i + 0] = clut[(index >> 0) & 0xf];
157 dest[i + 1] = clut[(index >> 4) & 0xf];
158 }
159 } else {
160 for (int i = 0; i < length; i += 2) {
161 u8 index = *indexed++;
162 dest[i + 0] = clut[gstate.transformClutIndex((index >> 0) & 0xf)];
163 dest[i + 1] = clut[gstate.transformClutIndex((index >> 4) & 0xf)];
164 }
165 }
166 }
167
168 template <typename ClutT>
DeIndexTexture4Optimal(ClutT * dest,const u8 * indexed,int length,ClutT color)169 inline void DeIndexTexture4Optimal(ClutT *dest, const u8 *indexed, int length, ClutT color) {
170 for (int i = 0; i < length; i += 2) {
171 u8 index = *indexed++;
172 dest[i + 0] = color | ((index >> 0) & 0xf);
173 dest[i + 1] = color | ((index >> 4) & 0xf);
174 }
175 }
176
177 template <>
178 inline void DeIndexTexture4Optimal<u16>(u16 *dest, const u8 *indexed, int length, u16 color) {
179 const u16_le *indexed16 = (const u16_le *)indexed;
180 const u32 color32 = (color << 16) | color;
181 u32 *dest32 = (u32 *)dest;
182 for (int i = 0; i < length / 2; i += 2) {
183 u16 index = *indexed16++;
184 dest32[i + 0] = color32 | ((index & 0x00f0) << 12) | ((index & 0x000f) >> 0);
185 dest32[i + 1] = color32 | ((index & 0xf000) << 4) | ((index & 0x0f00) >> 8);
186 }
187 }
188
DeIndexTexture4OptimalRev(u16 * dest,const u8 * indexed,int length,u16 color)189 inline void DeIndexTexture4OptimalRev(u16 *dest, const u8 *indexed, int length, u16 color) {
190 const u16_le *indexed16 = (const u16_le *)indexed;
191 const u32 color32 = (color << 16) | color;
192 u32 *dest32 = (u32 *)dest;
193 for (int i = 0; i < length / 2; i += 2) {
194 u16 index = *indexed16++;
195 dest32[i + 0] = color32 | ((index & 0x00f0) << 24) | ((index & 0x000f) << 12);
196 dest32[i + 1] = color32 | ((index & 0xf000) << 16) | ((index & 0x0f00) << 4);
197 }
198 }
199
200 template <typename ClutT>
DeIndexTexture4(ClutT * dest,const u32 texaddr,int length,const ClutT * clut)201 inline void DeIndexTexture4(ClutT *dest, const u32 texaddr, int length, const ClutT *clut) {
202 const u8 *indexed = (const u8 *) Memory::GetPointer(texaddr);
203 DeIndexTexture4(dest, indexed, length, clut);
204 }
205
206 template <typename ClutT>
DeIndexTexture4Optimal(ClutT * dest,const u32 texaddr,int length,ClutT color)207 inline void DeIndexTexture4Optimal(ClutT *dest, const u32 texaddr, int length, ClutT color) {
208 const u8 *indexed = (const u8 *) Memory::GetPointer(texaddr);
209 DeIndexTexture4Optimal(dest, indexed, length, color);
210 }
211