1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
#include "egg/gfx/eggCpuTexture.h"
#include "common.h"
#include "egg/core/eggHeap.h"
#include "egg/gfx/eggGXUtility.h"
#include "egg/gfx/eggTexture.h"
#include "egg/math/eggVector.h"
#include "rvl/GX/GXTexture.h"
#include "rvl/GX/GXTypes.h"
#include "rvl/MTX/vec.h"
#include "rvl/OS/OSCache.h"
namespace EGG {
namespace {
void blendColor(GXColor &outColor, const GXColor &c1, const GXColor &c2, f32 amount) {
if (amount <= 0.0f) {
outColor = c1;
} else if (amount >= 1.0f) {
outColor = c2;
} else {
outColor.r = c1.r + amount * (c2.r - c1.r);
outColor.g = c1.g + amount * (c2.g - c1.g);
outColor.b = c1.b + amount * (c2.b - c1.b);
outColor.a = c1.a + amount * (c2.a - c1.a);
}
}
void makeGradient(int op, GXColor *outColor, u16 size, u16 start, u16 end, const GXColor &c1, const GXColor &c2) {
for (int i = 0; i < start + 1; i++) {
outColor[i] = c1;
}
for (int i = end - 1; i < size; i++) {
outColor[i] = c2;
}
for (int i = start + 1; i < end - 1; i++) {
f32 ratio = (f32)(i - start) / (f32)(end - start);
switch (op) {
case 0: break;
case 1: ratio *= ratio; break;
case 2:
ratio *= ratio;
ratio *= ratio;
break;
case 3:
ratio *= ratio;
ratio *= ratio;
ratio *= ratio;
break;
case 4:
ratio *= ratio;
ratio *= ratio;
ratio *= ratio;
ratio *= ratio;
break;
case 5: ratio = -(ratio - 1.0f) * (ratio - 1.0f) + 1.0f; break;
case 6:
ratio -= 1.0f;
ratio = -ratio * ratio * ratio * ratio + 1.0f;
break;
}
blendColor(outColor[i], c1, c2, ratio);
}
}
} // namespace
CpuTexture::CpuTexture() {
mWidth = 0;
mHeight = 0;
mFlags = 0;
mTexFormat = GX_TF_RGBA8;
mWrapS = 0;
mWrapT = 0;
mMinFilt = 1;
mMagFilt = 1;
dataPtr = nullptr;
mpBuffer = nullptr;
}
CpuTexture::CpuTexture(u16 width, u16 height, GXTexFmt texFmt) {
mWidth = width;
mHeight = height;
mFlags = 0;
mTexFormat = texFmt;
mWrapS = 0;
mWrapT = 0;
mMinFilt = 1;
mMagFilt = 1;
dataPtr = nullptr;
mpBuffer = nullptr;
}
CpuTexture::CpuTexture(const GXTexObj *pObj) {
mWidth = GXGetTexObjWidth(pObj);
mHeight = GXGetTexObjHeight(pObj);
mTexFormat = GXGetTexObjFmt(pObj);
mWrapS = GXGetTexObjWrapS(pObj);
mWrapT = GXGetTexObjWrapT(pObj);
mMinFilt = GXGetTexObjMinFilt(pObj);
mMagFilt = GXGetTexObjMagFilt(pObj);
// Convert Physical to Virtual Address
dataPtr = (char *)GXGetTexObjData(pObj) - 0x80000000;
mpBuffer = nullptr;
}
CpuTexture::~CpuTexture() {
if ((mFlags & NEEDS_BUFFER_FREE) != 0) {
delete[] mpBuffer;
}
}
void CpuTexture::configure() {
mFlags = CONFIGURED;
}
void CpuTexture::setBuffer(void *buf) {
if (buf != nullptr) {
dataPtr = buf;
}
mFlags &= ~HAS_HEADER;
}
void CpuTexture::load(GXTexMapID id) const {
GXTexObj obj;
getTexObj(&obj);
GXLoadTexObj(&obj, id);
}
void CpuTexture::getTexObj(GXTexObj *pObj) const {
GXInitTexObj(pObj, dataPtr, mWidth, mHeight, getFormat(), (GXTexWrapMode)mWrapS, (GXTexWrapMode)mWrapT, false);
GXInitTexObjLOD(pObj, (GXTexFilter)mMinFilt, (GXTexFilter)mMagFilt, 0.0f, 0.0f, 0.0f, false, false, GX_ANISO_1);
}
void CpuTexture::flush() const {
size_t size = GXGetTexBufferSize(mWidth, mHeight, getFormat(), false, true);
DCFlushRange(dataPtr, size);
}
void CpuTexture::buildHeader() const {
ResTIMG *pHdr = getHeader();
pHdr->format = mTexFormat;
pHdr->alphaEnabled = 1;
pHdr->width = mWidth;
pHdr->height = mHeight;
pHdr->wrapS = mWrapS;
pHdr->wrapT = mWrapT;
pHdr->indexTexture = 0;
pHdr->colorFormat = 0;
pHdr->numColors = 0;
pHdr->paletteOffset = 0;
pHdr->mipmapEnabled = 0;
pHdr->doEdgeLOD = 0;
pHdr->biasClamp = 0;
pHdr->maxAnisotropy = 0;
pHdr->minFilter = mMinFilt;
pHdr->magFilter = mMagFilt;
pHdr->minLOD = 0;
pHdr->maxLOD = 0;
pHdr->mipmapCount = 1;
pHdr->LODBias = 0;
pHdr->imageOffset = sizeof(ResTIMG);
}
extern "C" double sqrt(double);
void CpuTexture::fillNormalMapSphere(f32 f1, f32 f2) {
f32 mid = static_cast<f32>(mWidth / 2);
for (u16 y = 0; y < mHeight; y++) {
for (u16 x = 0; x < mWidth; x++) {
Vector3f vec;
GXColor c;
f32 fx = f1 * ((x + f2) - mid);
f32 fy = f1 * -((y + f2) - mid);
f32 mid2 = mid * mid;
f32 fz = mid2 - (fx * fx + fy * fy);
vec(0) = fx;
vec(1) = fy;
// TODO: std::sqrtf?
vec(2) = fz < Math<f32>::zero() ? Math<f32>::zero() : (f32)sqrt(fz);
VECNormalize(vec, vec);
GXUtility::getNormalColor(c, vec);
c.r = c.a;
setColor(x, y, c);
}
}
flush();
}
void CpuTexture::fillGradient(
int op, int unk, u16 start, u16 end, const GXColor &c1, const GXColor &c2, bool b1, bool b2
) {
GXColor gradient[1024];
GXColor colors[256];
bool swapMode = unk == 0x73 || unk == 0x53;
u16 width = swapMode ? mWidth : mHeight;
u16 height = swapMode ? mHeight : mWidth;
u16 mid = width / 2;
makeGradient(op, gradient, width, start, end, c1, c2);
if (b1) {
for (int i = 0; i < width; i++) {
colors[i] = gradient[i < mid ? i + mid : i - mid];
}
for (int i = 0; i < width; i++) {
gradient[i] = colors[i];
}
}
u16 start2 = b2 ? 0 : start;
u32 end2 = b2 ? width : end;
for (u16 x = start2; x < end2; x++) {
for (u16 y = 0; y < height; y++) {
if (swapMode) {
setColor(x, y, gradient[x]);
} else {
setColor(y, x, gradient[x]);
}
}
}
flush();
}
void CpuTexture::alloc(Heap *pHeap) {
Heap *heap = !pHeap ? Heap::getCurrentHeap() : pHeap;
size_t size = GXGetTexBufferSize(mWidth, mHeight, getFormat(), false, true);
mpBuffer = new (heap, 0x20) char[size];
setBuffer(mpBuffer);
mFlags |= NEEDS_BUFFER_FREE;
}
void CpuTexture::allocWithHeaderDebug(Heap *pHeap) {
Heap *heap = !pHeap ? Heap::getCurrentHeap() : pHeap;
size_t size = GXGetTexBufferSize(mWidth, mHeight, getFormat(), false, true);
// Textures need 32-bit alignment ( ptr is represented with ptr >> 5 )
mpBuffer = new (heap, 0x20) char[size + sizeof(ResTIMG)];
// Texture has Header -> offset to actual image data
void *imageData = mpBuffer + sizeof(ResTIMG);
setBuffer(imageData);
if (imageData != nullptr) {
mFlags |= HAS_HEADER;
}
mFlags |= NEEDS_BUFFER_FREE;
buildHeader();
}
void CpuTexture::allocate(Heap *pHeap) {
alloc(pHeap);
}
void CpuTexture::setColor(u16 x, u16 y, GXColor color) {
// NONMATCHING
switch (getFormat()) {
case GX_TF_RGBA8:
case GX_TF_Z24X8: {
int pix = (x & 0x3) * 2 + (y & 0x3) * 8;
int block = (x >> 2) + (y >> 2) * (getWidth() / 4);
int offset = block * 0x40 + pix;
u8 *dat = static_cast<u8 *>(getBuffer()) + offset;
dat[0] = color.a;
dat[1] = color.r;
dat[0x20] = color.g;
dat[0x21] = color.b;
break;
}
case GX_TF_I8:
case GX_TF_Z8: {
int offset = (x & 0x7) + (y & 0x3) * 8; // pixel
offset += ((x >> 3) + ((y >> 2) * (getWidth() / 8))) * 32; // block
u8 *dat = static_cast<u8 *>(getBuffer()) + offset;
dat[0] = color.r;
break;
}
case GX_TF_IA8:
case GX_TF_Z16: {
int block = (x >> 2) + (y >> 2) * (getWidth() / 4);
int pix = (x & 0x3) * 2 + (y & 0x3) * 8;
int offset = block * 0x20 + pix;
u8 *dat = static_cast<u8 *>(getBuffer()) + offset;
dat[0] = color.a;
dat[1] = color.r;
break;
}
default: break;
}
}
GXColor CpuTexture::getColor(u16 x, u16 y) const {
GXColor c;
switch (getFormat()) {
case GX_TF_RGBA8:
case GX_TF_Z24X8: {
int offset = ((x >> 2) + (y >> 2) * (getWidth() / 4)) * 0x40 + (y & 3) * 8 + (x & 3) * 2;
u8 *dat = static_cast<u8 *>(getBuffer()) + offset;
c.r = dat[1];
c.g = dat[0x20];
c.b = dat[0x21];
c.a = dat[0];
break;
}
case GX_TF_I8:
case GX_TF_Z8: {
// TODO - probably fake
int idx = (x & 0x7);
int pixel_idx = (y & 0x3) * 8;
int block_idx = ((getWidth() / 8) * (y >> 2) + (x >> 3));
idx = idx + pixel_idx + block_idx * 0x20;
u8 *dat = static_cast<u8 *>(getBuffer()) + idx;
c.a = dat[0];
c.b = dat[0];
c.g = dat[0];
c.r = dat[0];
break;
}
case GX_TF_IA8:
case GX_TF_Z16: {
int offset = ((x >> 2) + (y >> 2) * (getWidth() / 4)) * 0x20 + (y & 3) * 8 + (x & 3) * 2;
u8 *dat = static_cast<u8 *>(getBuffer()) + offset;
c.a = dat[0];
c.b = dat[1];
c.g = c.b;
c.r = c.b;
break;
}
default: break;
}
return c;
}
} // namespace EGG
|