CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Common/TextureDecoder.h
Views: 1401
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
#include "ppsspp_config.h"
21
22
#include "Common/Common.h"
23
#include "Common/Swap.h"
24
#include "Core/MemMap.h"
25
#include "GPU/ge_constants.h"
26
#include "GPU/GPUState.h"
27
28
enum CheckAlphaResult {
29
// These are intended to line up with TexCacheEntry::STATUS_ALPHA_UNKNOWN, etc.
30
CHECKALPHA_FULL = 0,
31
CHECKALPHA_ANY = 4,
32
};
33
34
// For both of these, pitch must be aligned to 16 bits (as is the case on a PSP).
35
void DoSwizzleTex16(const u32 *ysrcp, u8 *texptr, int bxc, int byc, u32 pitch);
36
void DoUnswizzleTex16(const u8 *texptr, u32 *ydestp, int bxc, int byc, u32 pitch);
37
38
u32 StableQuickTexHash(const void *checkp, u32 size);
39
40
// outMask is an in/out parameter.
41
void CopyAndSumMask16(u16 *dst, const u16 *src, int width, u32 *outMask);
42
void CopyAndSumMask32(u32 *dst, const u32 *src, int width, u32 *outMask);
43
void CheckMask16(const u16 *src, int width, u32 *outMask);
44
void CheckMask32(const u32 *src, int width, u32 *outMask);
45
46
// All these DXT structs are in the reverse order, as compared to PC.
47
// On PC, alpha comes before color, and interpolants are before the tile data.
48
49
struct DXT1Block {
50
u8 lines[4];
51
u16_le color1;
52
u16_le color2;
53
};
54
55
struct DXT3Block {
56
DXT1Block color;
57
u16_le alphaLines[4];
58
};
59
60
struct DXT5Block {
61
DXT1Block color;
62
u32_le alphadata2;
63
u16_le alphadata1;
64
u8 alpha1; u8 alpha2;
65
};
66
67
void DecodeDXT1Block(u32 *dst, const DXT1Block *src, int pitch, int width, int height, u32 *alpha);
68
void DecodeDXT3Block(u32 *dst, const DXT3Block *src, int pitch, int width, int height);
69
void DecodeDXT5Block(u32 *dst, const DXT5Block *src, int pitch, int width, int height);
70
71
uint32_t GetDXT1Texel(const DXT1Block *src, int x, int y);
72
uint32_t GetDXT3Texel(const DXT3Block *src, int x, int y);
73
uint32_t GetDXT5Texel(const DXT5Block *src, int x, int y);
74
75
extern const u8 textureBitsPerPixel[16];
76
77
u32 GetTextureBufw(int level, u32 texaddr, GETextureFormat format);
78
79
// WARNING: Bits not bytes, this is needed due to the presence of 4 - bit formats.
80
inline u32 TextureFormatBitsPerPixel(GETextureFormat format) {
81
u32 bits = textureBitsPerPixel[(int)format];
82
return bits != 0 ? bits : 1; // Best to return 1 here to survive divisions in case of invalid data.
83
}
84
85
inline bool AlphaSumIsFull(u32 alphaSum, u32 fullAlphaMask) {
86
return fullAlphaMask != 0 && (alphaSum & fullAlphaMask) == fullAlphaMask;
87
}
88
89
inline CheckAlphaResult CheckAlpha16(const u16 *pixelData, int width, u32 fullAlphaMask) {
90
u32 alphaSum = 0xFFFFFFFF;
91
CheckMask16(pixelData, width, &alphaSum);
92
return AlphaSumIsFull(alphaSum, fullAlphaMask) ? CHECKALPHA_FULL : CHECKALPHA_ANY;
93
}
94
95
inline CheckAlphaResult CheckAlpha32(const u32 *pixelData, int width, u32 fullAlphaMask) {
96
u32 alphaSum = 0xFFFFFFFF;
97
CheckMask32(pixelData, width, &alphaSum);
98
return AlphaSumIsFull(alphaSum, fullAlphaMask) ? CHECKALPHA_FULL : CHECKALPHA_ANY;
99
}
100
101
inline CheckAlphaResult CheckAlpha32Rect(const u32 *pixelData, int stride, int width, int height, u32 fullAlphaMask) {
102
u32 alphaSum = 0xFFFFFFFF;
103
for (int y = 0; y < height; y++) {
104
CheckMask32(pixelData + stride * y, width, &alphaSum);
105
}
106
return AlphaSumIsFull(alphaSum, fullAlphaMask) ? CHECKALPHA_FULL : CHECKALPHA_ANY;
107
}
108
109
template <typename IndexT, typename ClutT>
110
inline void DeIndexTexture(/*WRITEONLY*/ ClutT *dest, const IndexT *indexed, int length, const ClutT *clut, u32 *outAlphaSum) {
111
// Usually, there is no special offset, mask, or shift.
112
const bool nakedIndex = gstate.isClutIndexSimple();
113
114
ClutT alphaSum = (ClutT)(-1);
115
116
if (nakedIndex) {
117
if (sizeof(IndexT) == 1) {
118
for (int i = 0; i < length; ++i) {
119
ClutT color = clut[*indexed++];
120
alphaSum &= color;
121
*dest++ = color;
122
}
123
} else {
124
for (int i = 0; i < length; ++i) {
125
ClutT color = clut[(*indexed++) & 0xFF];
126
alphaSum &= color;
127
*dest++ = color;
128
}
129
}
130
} else {
131
for (int i = 0; i < length; ++i) {
132
ClutT color = clut[gstate.transformClutIndex(*indexed++)];
133
alphaSum &= color;
134
*dest++ = color;
135
}
136
}
137
138
*outAlphaSum &= (u32)alphaSum;
139
}
140
141
template <typename IndexT, typename ClutT>
142
inline void DeIndexTexture(/*WRITEONLY*/ ClutT *dest, const u32 texaddr, int length, const ClutT *clut, u32 *outAlphaSum) {
143
const IndexT *indexed = (const IndexT *) Memory::GetPointer(texaddr);
144
DeIndexTexture(dest, indexed, length, clut, outAlphaSum);
145
}
146
147
template <typename ClutT>
148
inline void DeIndexTexture4(/*WRITEONLY*/ ClutT *dest, const u8 *indexed, int length, const ClutT *clut, u32 *outAlphaSum) {
149
// Usually, there is no special offset, mask, or shift.
150
const bool nakedIndex = gstate.isClutIndexSimple();
151
152
ClutT alphaSum = (ClutT)(-1);
153
if (nakedIndex) {
154
while (length >= 2) {
155
u8 index = *indexed++;
156
ClutT color0 = clut[index & 0xf];
157
ClutT color1 = clut[index >> 4];
158
*dest++ = color0;
159
*dest++ = color1;
160
alphaSum &= color0 & color1;
161
length -= 2;
162
}
163
if (length) { // Last pixel. Can really only happen in 1xY textures, but making this work generically.
164
u8 index = *indexed++;
165
ClutT color0 = clut[index & 0xf];
166
*dest = color0;
167
alphaSum &= color0;
168
}
169
} else {
170
while (length >= 2) {
171
u8 index = *indexed++;
172
ClutT color0 = clut[gstate.transformClutIndex((index >> 0) & 0xf)];
173
ClutT color1 = clut[gstate.transformClutIndex((index >> 4) & 0xf)];
174
*dest++ = color0;
175
*dest++ = color1;
176
alphaSum &= color0 & color1;
177
length -= 2;
178
}
179
if (length) {
180
u8 index = *indexed++;
181
ClutT color0 = clut[gstate.transformClutIndex((index >> 0) & 0xf)];
182
*dest = color0;
183
alphaSum &= color0;
184
}
185
}
186
187
*outAlphaSum &= (u32)alphaSum;
188
}
189
190
template <typename ClutT>
191
inline void DeIndexTexture4Optimal(ClutT *dest, const u8 *indexed, int length, ClutT color) {
192
while (length >= 2) {
193
u8 index = *indexed++;
194
*dest++ = color | ((index >> 0) & 0xf);
195
*dest++ = color | ((index >> 4) & 0xf);
196
length -= 2;
197
}
198
if (length) {
199
u8 index = *indexed++;
200
*dest++ = color | ((index >> 0) & 0xf);
201
}
202
}
203
204
template <>
205
inline void DeIndexTexture4Optimal<u16>(u16 *dest, const u8 *indexed, int length, u16 color) {
206
const u16_le *indexed16 = (const u16_le *)indexed;
207
const u32 color32 = (color << 16) | color;
208
u32 *dest32 = (u32 *)dest;
209
for (int i = 0; i < length / 2; i += 2) {
210
u16 index = *indexed16++;
211
dest32[i + 0] = color32 | ((index & 0x00f0) << 12) | ((index & 0x000f) >> 0);
212
dest32[i + 1] = color32 | ((index & 0xf000) << 4) | ((index & 0x0f00) >> 8);
213
}
214
}
215
216
inline void DeIndexTexture4OptimalRev(u16 *dest, const u8 *indexed, int length, u16 color) {
217
const u16_le *indexed16 = (const u16_le *)indexed;
218
const u32 color32 = (color << 16) | color;
219
u32 *dest32 = (u32 *)dest;
220
for (int i = 0; i < length / 2; i += 2) {
221
u16 index = *indexed16++;
222
dest32[i + 0] = color32 | ((index & 0x00f0) << 24) | ((index & 0x000f) << 12);
223
dest32[i + 1] = color32 | ((index & 0xf000) << 16) | ((index & 0x0f00) << 4);
224
}
225
}
226
227
template <typename ClutT>
228
inline void DeIndexTexture4(ClutT *dest, const u32 texaddr, int length, const ClutT *clut) {
229
const u8 *indexed = (const u8 *) Memory::GetPointer(texaddr);
230
DeIndexTexture4(dest, indexed, length, clut);
231
}
232
233
template <typename ClutT>
234
inline void DeIndexTexture4Optimal(ClutT *dest, const u32 texaddr, int length, ClutT color) {
235
const u8 *indexed = (const u8 *) Memory::GetPointer(texaddr);
236
DeIndexTexture4Optimal(dest, indexed, length, color);
237
}
238
239