DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
texture.cpp
1#pragma helper_mask_gpr 0x30
2#pragma helper_mask_fpr 0x1000
3#pragma name_counter 221
4
5#include "common.h"
6
7#include <eekernel.h>
8#include <libdma.h>
9#include <libgraph.h>
10#include <libpkt.h>
11
12#include <cstdio>
13#include <cstdlib>
14#include <cstring>
15
16#include "dataalloc.hpp"
17#include "dataread.hpp"
18#include "mglib.hpp"
19#include "texture.hpp"
20#include "tim2.hpp"
21
22/* TEX0 with the block address ahead of the buffer width, which is not the order the SDK's own
23 macro puts the two in. The order decides which term the compiler computes first, so the two
24 spellings are not interchangeable and this file needs its own. */
25#define TEXTURE_TEX0(tbp0, tbw, psm, tw, th, tcc, tfx, cbp, cpsm, csm, csa, cld) ((u_long) (tbp0) | ((u_long) (tbw) << 14) | ((u_long) (psm) << 20) | ((u_long) (tw) << 26) | ((u_long) (th) << 30) | ((u_long) (tcc) << 34) | ((u_long) (tfx) << 35) | ((u_long) (cbp) << 37) | ((u_long) (cpsm) << 51) | ((u_long) (csm) << 55) | ((u_long) (csa) << 56) | ((u_long) (cld) << 61))
26
27/* The IMG archive's directory as the registry walks it: a header at the start of the buffer the
28 file was read into and one fixed-size entry per picture behind it. */
29
30static u_long128 texData[100];
31
32static int BlockConv8to32(u_char *source, u_char *destination);
33static int Conv8to32(int width, int height, u_char *source, u_char *destination);
34
35CTexture::CTexture() {
36 Initialize();
37}
38
39void CTexture::Initialize() {
40 name[0] = 0;
41 block = 0;
42 image[3] = 0;
43 image[2] = 0;
44 image[1] = 0;
45 image[0] = 0;
46 clut = 0;
47 tex1 = 0;
48 tex0 = 0;
49 bpp = 0;
50 height = 0;
51 width = 0;
52 swizzled = false;
53}
54
55CTextureBlock::CTextureBlock() {
56 Initialize();
57}
58
59void CTextureBlock::Initialize() {
60 name[0] = 0;
61 vram_end = 0;
62 vram_top = 0;
63 vram_dirty = 0;
64 buffer_end = 0;
65 buffer = 0;
66 loaded = false;
67 extend = false;
68}
69
70void CTextureManager::Initialize(int size) {
71 int i;
72 int limit;
73
74 TextureData.used = 0;
75 limit = TextureData.limit;
76 SetBuffer((u_long128 *) (TextureData.base + TextureData.used * 16), limit);
77
78 for (i = 0; i < 72; i++) {
79 blocks[i].Initialize();
80 }
81
82 for (i = 0; i < 196; i++) {
83 textures[i].Initialize();
84 }
85
86 texture_max = 1;
87 vram_work = 8960;
88
89#ifdef PAL
90 for (i = 0; i < 72; i++) {
91 blocks[i].vram_top = mgTopVRAM;
92 blocks[i].vram_end = mgTopVRAM;
93 }
94
95#else
96 for (i = 0; i < 72; i++) {
97 blocks[i].vram_top = 6720;
98 blocks[i].vram_end = 6720;
99 }
100
101#endif
102 vram_size = size;
104 vram_fix = size;
105
106 strcpy(textures[0].name, "work");
107#ifdef PAL
108 textures[0].tex0 = SCE_GS_SET_TEX0(mgTopVRAM, 10, 0, 10, 8, 1, 0, 0, 0, 0, 0, 0);
109#else
110 textures[0].tex0 = SCE_GS_SET_TEX0(6720, 10, 0, 10, 8, 1, 0, 0, 0, 0, 0, 0);
111#endif
112 textures[0].block = -1;
113 last_block = -1;
114}
115
116/* The buffer is taken as it comes and walked forward to the next 128-byte boundary, because every
117 transfer out of it is a DMA read; the quadword count has to lose what the alignment ate. */
118void CTextureManager::SetBuffer(u_long128 *buffer, int size) {
119 int misalignment;
120 int skipped_quads;
121
122 this->buffer = buffer;
123 buffer_size = size;
124 misalignment = (int) this->buffer & 0x7f;
125
126 if (misalignment != 0) {
127 skipped_quads = (128 - misalignment) >> 4;
128 this->buffer = (u_long128 *) ((int) this->buffer + skipped_quads * 16);
129 buffer_size -= skipped_quads;
130 }
131
132 buffer_used = 0;
133}
134
135int CTextureManager::SearchTextureName(char *name, int block) {
136 int i = texture_max;
137 CTexture *tex = &textures[i];
138
139 for (; i >= 0; i--, tex--) {
140 char *name_cursor;
141 char *entry_cursor;
142 char name_char;
143 char entry_char;
144
145 if (tex->name[0] == 0) {
146 continue;
147 }
148
149 if (block >= 0 && block != tex->block) {
150 continue;
151 }
152
153 name_cursor = name;
154 entry_cursor = tex->name;
155
156 while ((name_char = *name_cursor) != 0 && (entry_char = *entry_cursor) != 0) {
157 if (name_char != entry_char) {
158 break;
159 }
160
161 name_cursor++;
162 entry_cursor++;
163 }
164
165 if (name_char == 0 && *entry_cursor == 0) {
166 return i;
167 }
168 }
169
170 return -1;
171}
172
173int CTextureManager::GetTextureHandle(char *name, int block) {
174 int handle = SearchTextureName(name, block);
175
176 if (handle < 0) {
177 return -1;
178 }
179
180 return handle;
181}
182
183CTexture *CTextureManager::GetTexture(int handle) {
184 if (handle < 0 || handle >= texture_max) {
185 return &textures[0];
186 }
187
188 return &textures[handle];
189}
190
191CTexture *CTextureManager::GetTexture(char *name, int block) {
192 int handle = GetTextureHandle(name, block);
193
194 if (handle < 0) {
195 return 0;
196 }
197
198 return GetTexture(handle);
199}
200
201CTexture *CTextureManager::SearchTexture(char *name) {
202 int i;
203 CTexture *tex = &textures[1];
204
205 for (i = 1; i < 196; i++, tex++) {
206 if (i == 196) {
207 printf("Texture Over!!\n");
208
209 while (1)
210 ;
211 }
212
213 if (tex->name[0] == 0) {
214 break;
215 }
216 }
217
218 SearchTextureName(name, -1);
219 return tex;
220}
221
222void CTextureManager::EnterTexture(int block, char *name, u_char *image, int width, int height, int bpp, u_char *clut, int clut_colors, int mipmap, u_char *mip1, u_char *mip2, u_char *mip3, u_long tex1, int swizzled) {
223 CTexture *tex;
224 int psm;
225 int tw;
226 int th;
227 int tbw;
228 int power;
229 int bit;
230 int tbp;
231 int cbp;
232 int image_blocks;
233 int half_width;
234 int half_height;
235 int image_size;
236 int mip_blocks;
237 int vram_top;
238 int vram_end;
239 int misalignment;
240 u_int *destination;
241
242 if (name[0] == 0) {
243 return;
244 }
245
246 tex = SearchTexture(name);
247
248 if (image == 0 || mip1 == 0) {
249 mipmap = false;
250 }
251
252 switch (bpp) {
253 case 1:
254 psm = SCE_GS_PSMT8;
255 break;
256 case 3:
257 psm = SCE_GS_PSMCT24;
258 break;
259 case 4:
260 psm = SCE_GS_PSMCT32;
261 break;
262 default:
263 return;
264 }
265
266 tw = 0;
267 th = 0;
268 power = width;
269
270 while (power >= 2) {
271 power >>= 1;
272 tw++;
273 }
274
275 for (power = 1, bit = 0; bit < tw; bit++) {
276 power <<= 1;
277 }
278
279 if (width != power) {
280 tw++;
281 }
282
283 power = height;
284
285 while (power >= 2) {
286 power >>= 1;
287 th++;
288 }
289
290 for (power = 1, bit = 0; bit < th; bit++) {
291 power <<= 1;
292 }
293
294 if (height != power) {
295 th++;
296 }
297
298 half_width = width >> 1;
299 half_height = height >> 1;
300 tbw = width >> 6;
301
302 if (tbw <= 0) {
303 tbw = 1;
304 }
305
306 tbp = 0;
307 cbp = 0;
308
309 static int clut_adr = 16000;
310
311 image_size = width * height * bpp;
312 image_blocks = image_size >> 8;
313 destination = (u_int *) (this->buffer + buffer_used);
314 vram_top = blocks[block].vram_top;
315 vram_end = blocks[block].vram_end;
316
317 switch (psm) {
318 case SCE_GS_PSMCT24:
319 case SCE_GS_PSMCT32:
320 case SCE_GS_PSMT8:
321 tbp = vram_end;
322
323 if (image == 0) {
324 vram_top += image_blocks;
325 vram_end = vram_top;
326
327 if (psm == SCE_GS_PSMT8) {
328 cbp = vram_top;
329 vram_top += 4;
330 vram_end = vram_top;
331 } else {
332 cbp = 0;
333 }
334 } else {
335 tex->image[0] = destination;
336
337 if (psm == SCE_GS_PSMT8 && swizzled == 0) {
338 Conv8to32(width, height, image, (u_char *) destination);
339 } else {
340 memcpy(destination, image, image_size);
341 }
342
343 vram_end += image_blocks;
344 buffer_used += image_blocks * 16;
345
346 if (mipmap != 0) {
347 destination = (u_int *) (this->buffer + buffer_used);
348 tex->image[1] = destination;
349
350 if (psm == SCE_GS_PSMT8 && swizzled == 0) {
351 Conv8to32(half_width, half_height, mip1, (u_char *) destination);
352 } else {
353 memcpy(destination, mip1, image_size >> 2);
354 }
355
356 mip_blocks = image_blocks >> 2;
357 vram_end += mip_blocks;
358 buffer_used += mip_blocks * 16;
359
360 destination = (u_int *) (this->buffer + buffer_used);
361 tex->image[2] = destination;
362
363 if (psm == SCE_GS_PSMT8 && swizzled == 0) {
364 Conv8to32(width >> 2, height >> 2, mip2, (u_char *) destination);
365 } else {
366 memcpy(destination, mip2, image_size >> 4);
367 }
368
369 vram_end += mip_blocks >> 2;
370 buffer_used += (mip_blocks >> 2) * 16;
371 }
372
373 if (psm == SCE_GS_PSMT8) {
374 tex->swizzled = true;
375 cbp = vram_end;
376 u_int *clut_buffer = (u_int *) (this->buffer + buffer_used);
377 tex->clut = clut_buffer;
378 memcpy(clut_buffer, clut, 1024);
379 vram_end += 4;
380 buffer_used += 64;
381 tex->tex0 = TEXTURE_TEX0(tbp, tbw, psm, tw, th, 1, 0, cbp, 0, 0, 0, 1);
382 } else {
383 tex->clut = 0;
384 tex->tex0 = TEXTURE_TEX0(tbp, tbw, psm, tw, th, 1, 0, 0, 0, 0, 0, 0);
385 }
386 }
387
388 break;
389 }
390
391 if (image == 0) {
392 if (psm != SCE_GS_PSMT8) {
393 tex->tex0 = TEXTURE_TEX0(tbp, tbw, psm, tw, th, 1, 0, 0, 0, 0, 0, 0);
394 } else {
395 tex->tex0 = TEXTURE_TEX0(tbp, tbw, psm, tw, th, 1, 0, cbp, 0, 0, 0, 1);
396 }
397 }
398
399 misalignment = vram_end % 32;
400
401 if (misalignment != 0) {
402 vram_end += 32 - misalignment;
403 buffer_used += (32 - misalignment) * 16;
404 }
405
406 blocks[block].vram_top = vram_top;
407 blocks[block].vram_end = vram_end;
408
409 if (buffer_used >= buffer_size) {
410 printf("texture buffer over!!\n");
411
412 while (1)
413 ;
414 }
415
416 if (blocks[block].vram_end > vram_max) {
417 printf("VRAM is not enough\n");
418 printf("%s %d\n", name, blocks[block].vram_end);
419
420 while (1)
421 ;
422 }
423
424 strcpy(tex->name, name);
425 tex->width = width;
426 tex->height = height;
427 tex->bpp = bpp;
428 tex->block = block;
429
430 if (bpp > 0 && mipmap != 0) {
431 tex->tex1 = SCE_GS_SET_TEX1(0, 2, 1, 5, 1, 0, -120);
432
433 if (tex1 != 0) {
434 sceGsTex1 *lod = (sceGsTex1 *) &tex1;
435 tex->tex1 = SCE_GS_SET_TEX1(0, 2, 1, 5, 1, lod->L, lod->K);
436 }
437 }
438
439 texture_max = 195;
440}
441
442/* The extended entry point keeps the caller's own pixels rather than copying them into the block
443 buffer, which is what lets a character carry textures the block was not built with. */
444void CTextureManager::EnterTextureEX(int block, char *name, u_char *image, int width, int height, int bpp, u_char *clut, int clut_colors, int mipmap, u_char *mip1, u_char *mip2, u_char *mip3, u_long tex1, int swizzled) {
445 CTexture *tex;
446 int psm;
447 int tw;
448 int th;
449 int tbw;
450 int power;
451 int bit;
452 int tbp;
453 int cbp;
454 int image_blocks;
455 int mip_blocks;
456 int vram_top;
457 int vram_end;
458 int misalignment;
459
460 if (name[0] == 0) {
461 return;
462 }
463
464 tex = SearchTexture(name);
465
466 if (image == 0 || mip1 == 0) {
467 mipmap = false;
468 }
469
470 switch (bpp) {
471 case 1:
472 psm = SCE_GS_PSMT8;
473 break;
474 case 3:
475 psm = SCE_GS_PSMCT24;
476 break;
477 case 4:
478 psm = SCE_GS_PSMCT32;
479 break;
480 default:
481 return;
482 }
483
484 tw = 0;
485 th = 0;
486 power = width;
487
488 while (power >= 2) {
489 power >>= 1;
490 tw++;
491 }
492
493 for (power = 1, bit = 0; bit < tw; bit++) {
494 power <<= 1;
495 }
496
497 if (width != power) {
498 tw++;
499 }
500
501 power = height;
502
503 while (power >= 2) {
504 power >>= 1;
505 th++;
506 }
507
508 for (power = 1, bit = 0; bit < th; bit++) {
509 power <<= 1;
510 }
511
512 if (height != power) {
513 th++;
514 }
515
516 tbw = width >> 6;
517
518 if (tbw <= 0) {
519 tbw = 1;
520 }
521
522 tbp = 0;
523 cbp = 0;
524 image_blocks = (width * height * bpp) >> 8;
525 vram_top = blocks[block].vram_top;
526 vram_end = blocks[block].vram_end;
527 blocks[block].extend = true;
528
529 switch (psm) {
530 case SCE_GS_PSMCT24:
531 case SCE_GS_PSMCT32:
532 case SCE_GS_PSMT8:
533 tbp = vram_end;
534
535 if (image == 0) {
536 vram_top += image_blocks;
537 vram_end = vram_top;
538
539 if (psm == SCE_GS_PSMT8) {
540 cbp = vram_top;
541 vram_top += 4;
542 vram_end = vram_top;
543 } else {
544 cbp = 0;
545 }
546 } else {
547 tex->image[0] = (u_int *) image;
548 vram_end += image_blocks;
549
550 if (mipmap != 0) {
551 tex->image[1] = (u_int *) mip1;
552 mip_blocks = image_blocks >> 2;
553 vram_end += mip_blocks;
554 tex->image[2] = (u_int *) mip2;
555 vram_end += mip_blocks >> 2;
556 }
557
558 if (psm == SCE_GS_PSMT8) {
559 cbp = vram_end;
560 tex->clut = (u_int *) clut;
561 vram_end += 4;
562 tex->tex0 = TEXTURE_TEX0(tbp, tbw, psm, tw, th, 1, 0, cbp, 0, 0, 0, 1);
563 } else {
564 tex->clut = 0;
565 tex->tex0 = TEXTURE_TEX0(tbp, tbw, psm, tw, th, 1, 0, 0, 0, 0, 0, 0);
566 }
567 }
568
569 break;
570 }
571
572 if (image == 0) {
573 if (psm != SCE_GS_PSMT8) {
574 tex->tex0 = TEXTURE_TEX0(tbp, tbw, psm, tw, th, 1, 0, 0, 0, 0, 0, 0);
575 } else {
576 tex->tex0 = TEXTURE_TEX0(tbp, tbw, psm, tw, th, 1, 0, cbp, 0, 0, 0, 1);
577 }
578 }
579
580 misalignment = vram_end % 32;
581
582 if (misalignment != 0) {
583 vram_end += 32 - misalignment;
584 }
585
586 blocks[block].vram_top = vram_top;
587 blocks[block].vram_end = vram_end;
588
589 if (blocks[block].vram_end > vram_max) {
590 printf("VRAM is not enough\n");
591 printf("%s %d\n", name, blocks[block].vram_end);
592
593 while (1)
594 ;
595 }
596
597 strcpy(tex->name, name);
598 tex->width = width;
599 tex->height = height;
600 tex->bpp = bpp;
601 tex->block = block;
602 tex->swizzled = swizzled;
603
604 if (bpp > 0 && mipmap != 0) {
605 tex->tex1 = SCE_GS_SET_TEX1(0, 2, 1, 5, 1, 0, -120);
606
607 if (tex1 != 0) {
608 sceGsTex1 *lod = (sceGsTex1 *) &tex1;
609 tex->tex1 = SCE_GS_SET_TEX1(0, 2, 1, 5, 1, lod->L, lod->K);
610 }
611 }
612
613 texture_max = 195;
614}
615
616/* A fixed texture is uploaded on the spot down the GIF channel rather than kept in the block
617 buffer for the frame packet to send, and its video memory is taken off the top of the region
618 the blocks grow into from below. */
619void CTextureManager::EnterFixTexture(char *name, u_char *image, int width, int height, int bpp, u_char *clut, int clut_colors, int mipmap, u_char *mip1, u_char *mip2, u_char *mip3, u_long tex1, int swizzled) {
620 CTexture *tex;
621 sceGifPacket packet;
622 int psm;
623 int tw;
624 int th;
625 int tbw;
626 int power;
627 int bit;
628 int quarter_width;
629 int quarter_height;
630 int mip1_tbw;
631 int mip2_tbw;
632 int tbp;
633 int cbp;
634 sceDmaChan *channel;
635 int half_height;
636 int half_width;
637 int image_size;
638 int swizzled_tbw;
639 int image_blocks;
640 int mip_blocks;
641 int misalignment;
642 int upload_address;
643
644 if (name[0] == 0) {
645 return;
646 }
647
648 tex = SearchTexture(name);
649
650 if (image == 0 || mip1 == 0) {
651 mipmap = false;
652 }
653
654 switch (bpp) {
655 case 1:
656 psm = SCE_GS_PSMT8;
657 break;
658 case 3:
659 psm = SCE_GS_PSMCT24;
660 break;
661 case 4:
662 psm = SCE_GS_PSMCT32;
663 break;
664 default:
665 return;
666 }
667
668 tw = 0;
669 th = 0;
670 power = width;
671
672 while (power >= 2) {
673 power >>= 1;
674 tw++;
675 }
676
677 for (power = 1, bit = 0; bit < tw; bit++) {
678 power <<= 1;
679 }
680
681 if (width != power) {
682 tw++;
683 }
684
685 power = height;
686
687 while (power >= 2) {
688 power >>= 1;
689 th++;
690 }
691
692 for (power = 1, bit = 0; bit < th; bit++) {
693 power <<= 1;
694 }
695
696 if (height != power) {
697 th++;
698 }
699
700 half_width = width >> 1;
701 half_height = height >> 1;
702 quarter_width = half_width >> 1;
703 quarter_height = half_height >> 1;
704 tbw = width >> 6;
705
706 if (tbw <= 0) {
707 tbw = 1;
708 }
709
710 swizzled_tbw = tbw >> 1;
711 mip1_tbw = swizzled_tbw;
712
713 if (mip1_tbw <= 0) {
714 mip1_tbw = 1;
715 }
716
717 mip2_tbw = mip1_tbw >> 1;
718
719 if (mip2_tbw <= 0) {
720 mip2_tbw = 1;
721 }
722
723 tbp = 0;
724 cbp = 0;
725
726 image_size = width * height * bpp;
727 image_blocks = image_size >> 8;
728
729 if (mipmap != 0) {
730 mip_blocks = image_blocks >> 2;
731 image_blocks = image_blocks + mip_blocks + (mip_blocks >> 2);
732 }
733
734 misalignment = image_blocks % 32;
735
736 if (misalignment != 0) {
737 image_blocks += 32 - misalignment;
738 }
739
740 vram_fix -= image_blocks;
741 upload_address = vram_fix;
742
743 sceGifPkInit(&packet, texData);
744 channel = sceDmaGetChan(2);
745 channel->chcr.TTE = 1;
746 sceGifPkReset(&packet);
747
748 switch (psm) {
749 case SCE_GS_PSMCT24:
750 case SCE_GS_PSMCT32:
751 case SCE_GS_PSMT8:
752 tbp = upload_address;
753
754 if (image == 0) {
755 if (psm != SCE_GS_PSMCT32) {
756 cbp = vram_fix;
757 vram_fix = cbp - 32;
758 } else {
759 cbp = 0;
760 }
761 } else {
762 if (psm == SCE_GS_PSMT8 && swizzled != 0) {
763 sceGifPkRefLoadImage(&packet, upload_address, SCE_GS_PSMCT32, swizzled_tbw, (u_long128 *) image, image_size >> 4, 0, 0, half_width, half_height);
764 } else {
765 sceGifPkRefLoadImage(&packet, upload_address, psm, tbw, (u_long128 *) image, image_size >> 4, 0, 0, width, height);
766 }
767
768 upload_address += image_blocks;
769
770 if (mipmap != 0) {
771 if (mip1 != 0) {
772 sceGifPkRefLoadImage(&packet, upload_address, psm, mip1_tbw, (u_long128 *) mip1, (half_width * half_height * bpp) >> 4, 0, 0, half_width, half_height);
773 }
774
775 upload_address += image_blocks >> 2;
776
777 if (mip2 != 0) {
778 sceGifPkRefLoadImage(&packet, upload_address, psm, mip2_tbw, (u_long128 *) mip2, (quarter_width * quarter_height * bpp) >> 4, 0, 0, quarter_width, quarter_height);
779 }
780 }
781
782 if (psm == SCE_GS_PSMT8) {
783 vram_fix -= 32;
784 cbp = vram_fix;
785 sceGifPkRefLoadImage(&packet, cbp, SCE_GS_PSMCT32, 1, (u_long128 *) clut, 64, 0, 0, 16, 16);
786 tex->tex0 = TEXTURE_TEX0(tbp, tbw, psm, tw, th, 1, 0, cbp, 0, 0, 0, 1);
787 } else {
788 tex->clut = 0;
789 tex->tex0 = TEXTURE_TEX0(tbp, tbw, psm, tw, th, 1, 0, 0, 0, 0, 0, 0);
790 }
791 }
792
793 break;
794 }
795
796 if (image == 0) {
797 if (psm != SCE_GS_PSMT8) {
798 tex->tex0 = TEXTURE_TEX0(tbp, tbw, psm, tw, th, 1, 0, 0, 0, 0, 0, 0);
799 } else {
800 tex->tex0 = TEXTURE_TEX0(tbp, tbw, psm, tw, th, 1, 0, cbp, 0, 0, 0, 1);
801 }
802 }
803
804 sceGifPkCnt(&packet, 0, 0, 0);
805 sceGifPkOpenGifTag(&packet, *(u_long128 *) &GiftagAD);
806 sceGifPkAddGsAD(&packet, 63, 0);
807 sceGifPkCloseGifTag(&packet);
808 sceGifPkEnd(&packet, 0, 0, 0);
809 sceGifPkTerminate(&packet);
810 FlushCache(0);
811 channel->chcr.TTE = 1;
812
813 if (image != 0) {
814 sceDmaSend(channel, (void *) packet.pBase);
815 }
816
817 sceGsSyncPath(0, 0);
818
819 strcpy(tex->name, name);
820 tex->width = width;
821 tex->height = height;
822 tex->bpp = bpp;
823 tex->block = -1;
824
825 if (bpp > 0 && mipmap != 0) {
826 tex->tex1 = SCE_GS_SET_TEX1(0, 2, 1, 5, 1, 0, -120);
827
828 if (tex1 != 0) {
829 sceGsTex1 *lod = (sceGsTex1 *) &tex1;
830 tex->tex1 = SCE_GS_SET_TEX1(0, 2, 1, 5, 1, lod->L, lod->K);
831 }
832 }
833
834 texture_max = 195;
835}
836
837/* The Z-buffer scratch texture, which is the one fixed texture whose size and format the loader
838 insists on rather than reads. */
839void CTextureManager::EnterFixTextureZ(u_char *buffer) {
840 char *name;
841 int width;
842 int height;
843 int bpp;
844 u_char *clut;
845 u_char *image;
846 CTexture *tex;
847 sceGifPacket packet;
848 sceDmaChan *channel;
849 TM2_head *head;
850 TM2_picture *picture;
851#ifdef PAL
852 int zbuffer;
853#endif
854
855 name = (char *) (buffer + 16);
856 head = (TM2_head *) (buffer + *(int *) (buffer + 48));
857 picture = (TM2_picture *) ((u_char *) head + 16);
858 width = head->image_width;
859 height = head->image_height;
860
861 if (width != 640) {
862 return;
863 }
864
865#ifdef PAL
866 if (height > 240) {
867 return;
868 }
869
870#else
871 if (height != 224) {
872 return;
873 }
874
875#endif
876 bpp = 1;
877
878 if (picture->image_type != TIM2_IDTEX8) {
879 return;
880 }
881
882 image = (u_char *) picture + picture->header_size;
883 clut = image + picture->image_size;
884
885 tex = SearchTexture(name);
886 strcpy(tex->name, name);
887 tex->width = width;
888 tex->height = height;
889 tex->bpp = bpp;
890 tex->block = 73;
891#ifdef PAL
892 zbuffer = mgZBufferAdr;
893 tex->tex0 = SCE_GS_SET_TEX0(zbuffer, 10, 27, 10, 8, 1, 0, 16352, 0, 0, 0, 1);
894#else
895 tex->tex0 = SCE_GS_SET_TEX0(4480, 10, 27, 10, 8, 1, 0, 16352, 0, 0, 0, 1);
896#endif
897
898 sceGifPkInit(&packet, texData);
899 channel = sceDmaGetChan(2);
900 channel->chcr.TTE = 1;
901 sceGifPkReset(&packet);
902#ifdef PAL
903 sceGifPkRefLoadImage(&packet, zbuffer, 27, 10, (u_long128 *) image, (width * height) >> 4, 0, 0, width, height);
904#else
905 sceGifPkRefLoadImage(&packet, 4480, 27, 10, (u_long128 *) image, (width * height) >> 4, 0, 0, width, height);
906#endif
907 sceGifPkRefLoadImage(&packet, 16352, 0, bpp, (u_long128 *) clut, 64, 0, 0, 16, 16);
908 sceGifPkCnt(&packet, 0, 0, 0);
909 sceGifPkOpenGifTag(&packet, *(u_long128 *) &GiftagAD);
910 sceGifPkAddGsAD(&packet, 63, 0);
911 sceGifPkCloseGifTag(&packet);
912 sceGifPkEnd(&packet, 0, 0, 0);
913 sceGifPkTerminate(&packet);
914 FlushCache(0);
915 channel->chcr.TTE = 1;
916
917 if (image != 0) {
918 sceDmaSend(channel, (void *) packet.pBase);
919 }
920
921 sceGsSyncPath(0, 0);
922}
923
924void CTextureManager::EnterIMGFile(u_char *buffer, int block, int mipmap, int extend) {
925 u_int i;
926 int swizzled;
927 u_char *entry;
928
929 if (buffer == 0) {
930 return;
931 }
932
933 swizzled = false;
934
935 if (memcmp(buffer, "IM2", 3) == 0) {
936 swizzled = true;
937 }
938
939 entry = buffer + 16;
940
941 for (i = 0; i < *(u_int *) (buffer + 4); i++) {
942 TM2_head *head;
943 TM2_picture *picture;
944 u_char *image;
945 u_char *clut;
946 int width;
947 int height;
948 int bpp;
949
950 head = (TM2_head *) (buffer + *(int *) (entry + 32));
951 picture = (TM2_picture *) ((u_char *) head + 16);
952 width = head->image_width;
953 height = head->image_height;
954
955 switch (head->image_type) {
956 case TIM2_RGB16:
957 bpp = 2;
958 break;
959 case TIM2_RGB24:
960 bpp = 3;
961 break;
962 case TIM2_RGB32:
963 bpp = 4;
964 break;
965 case TIM2_IDTEX4:
966 bpp = 0;
967 break;
968 case TIM2_IDTEX8:
969 bpp = 1;
970 break;
971 default:
972 entry += 48;
973 continue;
974 }
975
976 clut = 0;
977 image = (u_char *) picture + picture->header_size;
978
979 if (bpp < 2) {
980 clut = image + picture->image_size;
981 }
982
983 u_char *mip[4] = {0, 0, 0, 0};
984
985 if (picture->mipmap_count > 1) {
986 u_char *mip_image = image + picture->mipmap_size[0];
987
988 for (int level = 1; level < picture->mipmap_count; level++) {
989 mip[level] = mip_image;
990 mip_image += picture->mipmap_size[level];
991 }
992 }
993
994 if (extend == 0) {
995 if (block >= 0) {
996 EnterTexture(block, (char *) entry, image, width, height, bpp, clut, picture->clut_colors, mipmap, mip[1], mip[2], mip[3], picture->tex1, swizzled);
997 } else {
998 EnterFixTexture((char *) entry, image, width, height, bpp, clut, picture->clut_colors, mipmap, mip[1], mip[2], mip[3], picture->tex1, swizzled);
999 }
1000 } else if (block >= 0) {
1001 EnterTextureEX(block, (char *) entry, image, width, height, bpp, clut, picture->clut_colors, mipmap, mip[1], mip[2], mip[3], picture->tex1, swizzled);
1002 }
1003
1004 entry += 48;
1005 }
1006}
1007
1008/* The same region load the SDK writes into a GIF packet, written into a VIF1 chain instead so
1009 that a frame's own packet can carry it. */
1010int LoadImage(u_int *packet, int dbp, int dpsm, int dbw, u_long128 *source, int qwc, int dsax, int dsay, int rrw, int rrh) {
1011 u_int *start = packet;
1012 u_long bitbltbuf;
1013 u_long trxpos;
1014 u_long trxreg;
1015
1016 packet[0] = 0x10000005;
1017 packet[1] = 0;
1018 packet[2] = 0;
1019 packet[3] = 0x50000005;
1020 packet[4] = 4;
1021 packet[5] = 0x10000000;
1022 packet[6] = 14;
1023 packet[7] = 0;
1024
1025 bitbltbuf = SCE_GS_SET_BITBLTBUF(0, 0, 0, dbp, dbw, dpsm);
1026 packet[8] = (u_int) (bitbltbuf & 0xffffffff);
1027 packet[9] = (u_int) ((bitbltbuf >> 32) & 0xffffffff);
1028 packet[10] = 80;
1029 packet[11] = 0;
1030
1031 trxpos = SCE_GS_SET_TRXPOS(0, 0, dsax, dsay, 0);
1032 packet[12] = (u_int) (trxpos & 0xffffffff);
1033 packet[13] = (u_int) ((trxpos >> 32) & 0xffffffff);
1034 packet[14] = 81;
1035 packet[15] = 0;
1036
1037 trxreg = SCE_GS_SET_TRXREG(rrw, rrh);
1038 packet[16] = (u_int) (trxreg & 0xffffffff);
1039 packet[17] = (u_int) ((trxreg >> 32) & 0xffffffff);
1040 packet[18] = 82;
1041 packet[19] = 0;
1042
1043 packet[20] = 0;
1044 packet[21] = 0;
1045 packet[22] = 83;
1046 packet[23] = 0;
1047 packet += 24;
1048
1049 while (qwc > 0) {
1050 int count = 16384;
1051
1052 if (qwc < 16384) {
1053 count = qwc;
1054 }
1055
1056 packet[0] = 0x10000001;
1057 packet[1] = 0;
1058 packet[2] = 0;
1059 packet[3] = 0x50000001;
1060 packet[4] = count | 0x8000;
1061 packet[5] = 0x08000000;
1062 packet[6] = 0;
1063 packet[7] = 0;
1064 packet[8] = count | 0x30000000;
1065 packet[9] = (u_int) source;
1066 packet[10] = 0;
1067 packet[11] = count | 0x50000000;
1068 packet += 12;
1069 source += count;
1070 qwc -= 16384;
1071 }
1072
1073 return packet - start;
1074}
1075
1076void CTextureManager::ReloadTexture(sceVif1Packet *packet, int block) {
1077 u_int *cursor;
1078 u_int *start;
1079 int i;
1080 int level;
1081 CTexture *tex;
1082 sceGsTex0 tex0;
1083 int width;
1084 int height;
1085 int bpp;
1086 int reload_from;
1087 int reload_all;
1088
1089 if (block < 0 || block >= 72) {
1090 last_block = -1;
1091 return;
1092 }
1093
1094 if (last_block == block && blocks[block].loaded != 0) {
1095 return;
1096 }
1097
1098 sceVif1PkCnt(packet, 0);
1099 sceVif1PkOpenDirectCode(packet, 0);
1100 sceVif1PkOpenGifTag(packet, *(u_long128 *) &GiftagAD);
1101 sceVif1PkAddGsAD(packet, 63, 0);
1102 sceVif1PkCloseGifTag(packet);
1103 sceVif1PkCloseDirectCode(packet);
1104 sceVif1PkTerminate(packet);
1105
1106 cursor = packet->pCurrent;
1107 start = cursor;
1108 last_block = block;
1109
1110 for (i = 0; i < 72; i++) {
1111 if (i == block) {
1112 continue;
1113 }
1114
1115 if (blocks[i].vram_dirty < blocks[block].vram_end) {
1116 blocks[i].vram_dirty = blocks[block].vram_end;
1117 }
1118 }
1119
1120 reload_from = blocks[block].vram_dirty;
1121
1122 if (blocks[block].vram_end < reload_from || reload_from <= 0) {
1123 reload_from = blocks[block].vram_end;
1124 } else {
1125 reload_from = blocks[block].vram_top < reload_from ? reload_from : blocks[block].vram_end;
1126 }
1127
1128 blocks[block].vram_dirty = 0;
1129
1130 if (blocks[block].loaded == 0) {
1131 reload_all = 1;
1132 } else {
1133 reload_all = 0;
1134 }
1135
1136 tex = &textures[1];
1137
1138 for (i = 1; i < texture_max; i++, tex++) {
1139 if (tex->block != block) {
1140 continue;
1141 }
1142
1143 tex0 = *(sceGsTex0 *) &tex->tex0;
1144
1145 if (reload_from < (int) tex0.TBP0 && blocks[block].loaded != 0) {
1146 continue;
1147 }
1148
1149 width = tex->width;
1150 height = tex->height;
1151 bpp = tex->bpp;
1152
1153 if (bpp == 1) {
1154 if (tex->swizzled != 0) {
1155 width >>= 1;
1156 height >>= 1;
1157 bpp = 4;
1158 tex0.PSM = 0;
1159 tex0.TBW = tex0.TBW >> 1;
1160 }
1161
1162 if (tex->clut != 0) {
1163 cursor += LoadImage(cursor, tex0.CBP, tex0.CPSM, 1, (u_long128 *) tex->clut, 64, 0, 0, 16, 16);
1164 }
1165 }
1166
1167 for (level = 0; level < 4; level++) {
1168 if (tex->image[level] == 0) {
1169 break;
1170 }
1171
1172 if (tex0.TBW == 0) {
1173 tex0.TBW = 1;
1174 }
1175
1176 cursor += LoadImage(cursor, tex0.TBP0, tex0.PSM, tex0.TBW, (u_long128 *) tex->image[level], (bpp * (width * height)) >> 4, 0, 0, width, height);
1177 tex0.TBP0 += (width * height) >> 6;
1178 tex0.TBW = tex0.TBW >> 1;
1179 width >>= 1;
1180 height >>= 1;
1181 }
1182 }
1183
1184 sceVif1PkReserve(packet, cursor - start);
1185 blocks[block].loaded = true;
1186
1187 sceVif1PkCnt(packet, 0);
1188 sceVif1PkOpenDirectCode(packet, 0);
1189 sceVif1PkOpenGifTag(packet, *(u_long128 *) &GiftagAD);
1190 sceVif1PkAddGsAD(packet, 63, 0);
1191 sceVif1PkCloseGifTag(packet);
1192 sceVif1PkCloseDirectCode(packet);
1193}
1194
1195void CTextureManager::BeginEnterTextureBlock(int block) {
1196 if (block < 0 || block >= 72) {
1197 return;
1198 }
1199
1200#ifdef PAL
1201 if (blocks[block].vram_top == 0) {
1202 blocks[block].vram_top = mgTopVRAM;
1203 }
1204
1205 if (blocks[block].vram_end == 0) {
1206 blocks[block].vram_end = mgTopVRAM;
1207 }
1208
1209#else
1210 if (blocks[block].vram_top == 0) {
1211 blocks[block].vram_top = 6720;
1212 }
1213
1214 if (blocks[block].vram_end == 0) {
1215 blocks[block].vram_end = 6720;
1216 }
1217
1218#endif
1219 blocks[block].buffer = buffer + buffer_used;
1221 blocks[block].loaded = false;
1222}
1223
1224void CTextureManager::EndEnterTextureBlock(int block) {
1225 if (block < 0 || block >= 72) {
1226 return;
1227 }
1228
1230
1231 if (blocks[block].buffer_end == blocks[block].buffer) {
1232 blocks[block].buffer = 0;
1233 blocks[block].buffer_end = 0;
1234 }
1235
1236 if (buffer_used > buffer_size) {
1237 printf("texture buffer over!!\n");
1238
1239 while (1)
1240 ;
1241 }
1242
1243 if (vram_fix < blocks[block].vram_end) {
1244 printf("fix texture over!!\n");
1245
1246 while (1)
1247 ;
1248 }
1249}
1250
1251int CTextureManager::DeleteTextureBlock(int block) {
1252 int i;
1253
1254 if (block >= 72) {
1255 return 0;
1256 }
1257
1258 if (block >= 0 && block < 72) {
1259 blocks[block].Initialize();
1260 }
1261
1262 if (block < 0) {
1263 block = -1;
1264 }
1265
1266 for (i = 0; i < 196; i++) {
1267 if (block < 0 || textures[i].block == block) {
1268 textures[i].Initialize();
1269 }
1270 }
1271
1272 return 1;
1273}
1274
1275/* The blocks are packed back down to the front of the buffer in address order, and every texture
1276 that points into one is moved by the same distance its block was: the pointers are what the
1277 frame packet transfers from, so they cannot be left behind. */
1278int CTextureManager::CleanUpBuffer() {
1279 u_long128 *block_start[72];
1280 int block_order[72];
1281 int i;
1282 int j;
1283
1284 for (i = 0; i < 72; i++) {
1285 block_start[i] = blocks[i].buffer;
1286 block_order[i] = i;
1287 }
1288
1289 for (i = 0; i < 71; i++) {
1290 for (j = i + 1; j < 72; j++) {
1291 u_long128 **lower = &block_start[i];
1292 u_long128 **upper = &block_start[j];
1293 int *lower_block = &block_order[i];
1294 int *upper_block = &block_order[j];
1295
1296 if (*lower > *upper) {
1297 u_long128 *swap_start = *lower;
1298 int swap_block = *lower_block;
1299 *lower = *upper;
1300 *lower_block = *upper_block;
1301 *upper = swap_start;
1302 *upper_block = swap_block;
1303 }
1304 }
1305 }
1306
1307 buffer_used = 0;
1308
1309 for (i = 0; i < 72; i++) {
1310 int block_no = block_order[i];
1311 u_long128 *destination;
1312 u_long128 *source;
1313 int block_quads;
1314 int k;
1315 int misalignment;
1316 int shift;
1317
1318 if (block_no <= 0) {
1319 continue;
1320 }
1321
1322 source = block_start[i];
1323
1324 if (source == 0) {
1325 continue;
1326 }
1327
1328 block_quads = blocks[block_no].buffer_end - blocks[block_no].buffer;
1329 destination = buffer + buffer_used;
1330 shift = (source - destination) * 16;
1331 blocks[block_no].buffer = destination;
1332 blocks[block_no].buffer_end = destination + block_quads;
1333
1334 for (k = 0; k < block_quads; k++) {
1335 *destination = *source;
1336 source++;
1337 destination++;
1338 }
1339
1340 for (j = 0; j < 196 && shift != 0; j++) {
1341 CTexture *tex = &textures[j];
1342 int level;
1343
1344 if (tex->block != block_no) {
1345 continue;
1346 }
1347
1348 for (level = 0; level < 4; level++) {
1349 u_int **images = tex->image;
1350 u_int **slot = &images[level];
1351
1352 if (tex->image[level] == 0) {
1353 continue;
1354 }
1355
1356 *slot = (u_int *) ((int) *slot - shift);
1357 }
1358
1359 if (tex->clut != 0) {
1360 tex->clut = (u_int *) ((int) tex->clut - shift);
1361 }
1362 }
1363
1364 buffer_used += block_quads;
1365 misalignment = block_quads % 8;
1366
1367 if (misalignment != 0) {
1368 buffer_used += 8 - misalignment;
1369 }
1370 }
1371
1372 return 1;
1373}
1374
1375int CTextureManager::CleanUpTextureList() {
1376 int i;
1377 int j;
1378
1379 for (i = 1; i < 195; i++) {
1380 CTexture *hole = &textures[i];
1381
1382 if (hole->name[0] != 0) {
1383 continue;
1384 }
1385
1386 for (j = i + 1; j < 196; j++) {
1387 CTexture *tex = &textures[j];
1388
1389 if (tex->name[0] == 0) {
1390 continue;
1391 }
1392
1393 *hole = *tex;
1394 tex->Initialize();
1395 break;
1396 }
1397 }
1398
1399 return 1;
1400}
1401
1402/* A texture table entry may spell a placeholder rather than name a file, and the fields of one
1403 are separated by hashes. */
1404static int GetStr(char *text, char *out) {
1405 int length = 0;
1406
1407 if (*text == 0) {
1408 return 0;
1409 }
1410
1411 while (*text != '#') {
1412 out[length] = *text;
1413 length++;
1414
1415 if (*text == 0) {
1416 break;
1417 }
1418
1419 text++;
1420 }
1421
1422 out[length] = 0;
1423 return length + 1;
1424}
1425
1426static int GetDummyInfo(char *text, char *name, int *width, int *height, int *bpp) {
1427 char width_text[8];
1428 char height_text[8];
1429 char bpp_text[8];
1430 int length;
1431
1432 if (*text != '#') {
1433 return 0;
1434 }
1435
1436 text++;
1437
1438 int parsed_width = 32;
1439 int parsed_height = 32;
1440 int parsed_bpp = 4;
1441 char parsed_name[32] = "";
1442
1443 length = GetStr(text, parsed_name);
1444
1445 if (length < 2) {
1446 parsed_name[0] = 0;
1447 }
1448
1449 text += length;
1450
1451 length = GetStr(text, width_text);
1452 text += length;
1453
1454 if (length > 1) {
1455 parsed_width = atoi(width_text);
1456 }
1457
1458 length = GetStr(text, height_text);
1459 text += length;
1460
1461 if (length > 1) {
1462 parsed_height = atoi(height_text);
1463 }
1464
1465 length = GetStr(text, bpp_text);
1466
1467 if (length > 1) {
1468 parsed_bpp = atoi(bpp_text);
1469 }
1470
1471 strcpy(name, parsed_name);
1472 *width = parsed_width;
1473 *height = parsed_height;
1474 *bpp = parsed_bpp;
1475 return 1;
1476}
1477
1478int CTextureManager::LoadTextureBlock(int block, u_int *buffer) {
1479 return LoadTextureBlock(block, file, buffer);
1480}
1481
1482int CTextureManager::LoadTextureBlock(int block, LOADTEXTURE_INFO *table, u_int *buffer) {
1483 LOADTEXTURE_INFO *info;
1484 int i;
1485 int begun;
1486
1487 for (i = 0; i < 72; i++) {
1488 if (block >= 0 && block != i) {
1489 continue;
1490 }
1491
1492 info = table;
1493 begun = 0;
1494
1495 while (1) {
1496 char name[32];
1497 int width;
1498 int height;
1499 int bpp;
1500
1501 if (info->name == 0) {
1502 break;
1503 }
1504
1505 if (info->name[0] == 0) {
1506 break;
1507 }
1508
1509 if (info->block_no == i) {
1510 if (begun == 0) {
1511 BeginEnterTextureBlock(i);
1512 }
1513
1514 begun = 1;
1515
1516 width = 32;
1517 height = 32;
1518 bpp = 4;
1519
1520 if (GetDummyInfo(info->name, name, &width, &height, &bpp) != 0) {
1521 TexManager.EnterTexture(info->block_no, name, 0, width, height, bpp, 0, 0, 0, 0, 0, 0, 0, 0);
1522 } else if (LoadFile2(info->name, buffer, 0, 0) != 0) {
1523 EnterIMGFile((u_char *) buffer, info->block_no, info->mipmap, 0);
1524 }
1525 }
1526
1527 info++;
1528 }
1529
1530 if (begun != 0) {
1531 EndEnterTextureBlock(i);
1532 }
1533 }
1534
1535 return 0;
1536}
1537
1538int CTextureManager::LoadTextureBlock(int block, LOADTEXTURE_INFO2 *table) {
1539 LOADTEXTURE_INFO2 *info;
1540 int i;
1541 int begun;
1542
1543 for (i = 0; i < 72; i++) {
1544 if (block >= 0 && block != i) {
1545 continue;
1546 }
1547
1548 info = table;
1549 begun = 0;
1550
1551 while (1) {
1552 char name[32];
1553 int width;
1554 int height;
1555 int bpp;
1556
1557 if (info->name == 0) {
1558 break;
1559 }
1560
1561 if (info->name[0] == 0) {
1562 break;
1563 }
1564
1565 if (info->block_no == i) {
1566 if (begun == 0) {
1567 BeginEnterTextureBlock(i);
1568 }
1569
1570 begun = 1;
1571
1572 width = 32;
1573 height = 32;
1574 bpp = 4;
1575
1576 if (GetDummyInfo(info->name, name, &width, &height, &bpp) != 0) {
1577 TexManager.EnterTexture(info->block_no, name, 0, width, height, bpp, 0, 0, 0, 0, 0, 0, 0, 0);
1578 } else {
1579 EnterIMGFile((u_char *) info->name, info->block_no, info->mipmap, 0);
1580 }
1581 }
1582
1583 info++;
1584 }
1585
1586 if (begun != 0) {
1587 EndEnterTextureBlock(i);
1588 }
1589 }
1590
1591 return 0;
1592}
1593
1594int CTextureManager::LoadTextureBlockEX(int block, LOADTEXTURE_INFO2 *table) {
1595 LOADTEXTURE_INFO2 *info;
1596 int i;
1597 int begun;
1598
1599 for (i = 0; i < 72; i++) {
1600 if (block >= 0 && block != i) {
1601 continue;
1602 }
1603
1604 info = table;
1605 begun = 0;
1606
1607 while (1) {
1608 char name[32];
1609 int width;
1610 int height;
1611 int bpp;
1612
1613 if (info->name == 0) {
1614 break;
1615 }
1616
1617 if (info->name[0] == 0) {
1618 break;
1619 }
1620
1621 if (info->block_no == i) {
1622 if (begun == 0) {
1623 BeginEnterTextureBlock(i);
1624 }
1625
1626 begun = 1;
1627
1628 width = 32;
1629 height = 32;
1630 bpp = 4;
1631
1632 if (GetDummyInfo(info->name, name, &width, &height, &bpp) != 0) {
1633 TexManager.EnterTextureEX(info->block_no, name, 0, width, height, bpp, 0, 0, 0, 0, 0, 0, 0, 0);
1634 } else {
1635 int mipmap = info->mipmap;
1636 int entry_block = info->block_no;
1637
1638 EnterIMGFile((u_char *) info->name, entry_block, mipmap, begun);
1639 }
1640 }
1641
1642 info++;
1643 }
1644
1645 if (begun != 0) {
1646 EndEnterTextureBlock(i);
1647 }
1648 }
1649
1650 return 0;
1651}
1652
1653int CTextureManager::EnterTextureFile(LOADTEXTURE_INFO *table) {
1654 file = table;
1655 return 1;
1656}
1657
1658/* Eight-bit pixels reach the GS as a 32-bit image half as wide and half as tall, so the page the
1659 hardware would read has to be assembled here: one page of blocks, each block reordered by the
1660 table below, and the whole page written back in the order the GS stores it. */
1661static int PageConv8to32(int width, int height, u_char *source, u_char *destination) {
1662 static int block_table8[32] = {
1663 0, 1, 4, 5, 16, 17, 20, 21,
1664 2, 3, 6, 7, 18, 19, 22, 23,
1665 8, 9, 12, 13, 24, 25, 28, 29,
1666 10, 11, 14, 15, 26, 27, 30, 31};
1667 static int block_table32[32] = {
1668 0, 1, 4, 5, 16, 17, 20, 21,
1669 2, 3, 6, 7, 18, 19, 22, 23,
1670 8, 9, 12, 13, 24, 25, 28, 29,
1671 10, 11, 14, 15, 26, 27, 30, 31};
1672 int block_column[32];
1673 int block_row[32];
1674 u_char work8[256];
1675 u_char work32[256];
1676 u_char *work_cursor;
1677 u_char *source_cursor;
1678 u_char *destination_cursor;
1679 int blocks_down;
1680 int i;
1681 int j;
1682 int k;
1683 int entry;
1684 int blocks_across;
1685 int block_index;
1686
1687 entry = 0;
1688
1689 for (i = 0; i < 4; i++) {
1690 for (j = 0; j < 8; j++) {
1691 block_column[block_table32[entry]] = j;
1692 block_row[block_table32[entry]] = i;
1693 entry++;
1694 }
1695 }
1696
1697 blocks_across = width >> 4;
1698 blocks_down = height >> 4;
1699
1700 memset(work8, 0, 256);
1701 memset(work32, 0, 256);
1702
1703 for (i = 0; i < blocks_down; i++) {
1704 for (j = 0; j < blocks_across; j++) {
1705 work_cursor = work8;
1706 source_cursor = source + i * 2048 + j * 16;
1707 block_index = block_table8[i * blocks_across + j];
1708
1709 for (k = 0; k < 16; k++) {
1710 memcpy(work_cursor, source_cursor, 16);
1711 work_cursor += 16;
1712 source_cursor += 128;
1713 }
1714
1715 BlockConv8to32(work8, work32);
1716
1717 work_cursor = work32;
1718 destination_cursor = destination + block_row[block_index] * 2048 + block_column[block_index] * 32;
1719
1720 for (k = 0; k < 8; k++) {
1721 memcpy(destination_cursor, work_cursor, 32);
1722 work_cursor += 32;
1723 destination_cursor += 256;
1724 }
1725 }
1726 }
1727
1728 return 0;
1729}
1730
1731/* One block of an 8-bit image reordered into the order the GS stores its bytes in. A block is four
1732 columns of sixty-four bytes and a column's arrangement depends on whether its index is odd, so
1733 the table is two permutations rather than one: the second half is the first with bit 2 of every
1734 entry inverted, and the caller picks the half from the low bit of the column it is on. */
1735static int BlockConv8to32(u_char *source, u_char *destination) {
1736 static int lut[128] = {
1737 0, 36, 8, 44, 1, 37, 9, 45,
1738 2, 38, 10, 46, 3, 39, 11, 47,
1739 4, 32, 12, 40, 5, 33, 13, 41,
1740 6, 34, 14, 42, 7, 35, 15, 43,
1741 16, 52, 24, 60, 17, 53, 25, 61,
1742 18, 54, 26, 62, 19, 55, 27, 63,
1743 20, 48, 28, 56, 21, 49, 29, 57,
1744 22, 50, 30, 58, 23, 51, 31, 59,
1745 4, 32, 12, 40, 5, 33, 13, 41,
1746 6, 34, 14, 42, 7, 35, 15, 43,
1747 0, 36, 8, 44, 1, 37, 9, 45,
1748 2, 38, 10, 46, 3, 39, 11, 47,
1749 20, 48, 28, 56, 21, 49, 29, 57,
1750 22, 50, 30, 58, 23, 51, 31, 59,
1751 16, 52, 24, 60, 17, 53, 25, 61,
1752 18, 54, 26, 62, 19, 55, 27, 63};
1753 u_int j;
1754 u_int k;
1755 u_int i;
1756 int lut_index;
1757 int out_index;
1758 int source_index;
1759
1760 out_index = 0;
1761
1762 for (i = 0; i < 4; i++) {
1763 lut_index = (i & 1) * 64;
1764
1765 for (j = 0; j < 16; j++) {
1766 for (k = 0; k < 4; k++) {
1767 source_index = lut[lut_index];
1768 lut_index++;
1769 destination[out_index] = source[source_index];
1770 out_index++;
1771 }
1772 }
1773
1774 source += 64;
1775 }
1776
1777 return 0;
1778}
1779
1780static int Conv8to32(int width, int height, u_char *source, u_char *destination) {
1781 u_char work8[8192];
1782 u_char work32[8192];
1783 int i;
1784 int j;
1785 int k;
1786 int pages_x;
1787 int pages_y;
1788 int row_bytes;
1789 int row_count;
1790
1791 memset(work8, 0, 8192);
1792 memset(work32, 0, 8192);
1793 pages_x = ((width - 1) >> 7) + 1;
1794 pages_y = ((height - 1) >> 6) + 1;
1795
1796 if (pages_x == 1) {
1797 row_bytes = width * 2;
1798 } else {
1799 width = 128;
1800 row_bytes = 256;
1801 }
1802
1803 if (pages_y == 1) {
1804 row_count = height >> 1;
1805 } else {
1806 height = 64;
1807 row_count = 32;
1808 }
1809
1810 for (i = 0; i < pages_y; i++) {
1811 for (j = 0; j < pages_x; j++) {
1812 u_char *source_cursor = source + i * (pages_x * (width << 6)) + width * j;
1813 u_char *work_cursor = work8;
1814 u_char *destination_cursor;
1815 u_char *work_out_cursor;
1816
1817 for (k = 0; k < height; k++) {
1818 memcpy(work_cursor, source_cursor, width);
1819 source_cursor += width * pages_x;
1820 work_cursor += 128;
1821 }
1822
1823 PageConv8to32(128, 64, work8, work32);
1824
1825 destination_cursor = destination + i * (pages_x * (row_bytes * row_count)) + row_bytes * j;
1826 work_out_cursor = work32;
1827
1828 for (k = 0; k < row_count; k++) {
1829 memcpy(destination_cursor, work_out_cursor, row_bytes);
1830 destination_cursor += row_bytes * pages_x;
1831 work_out_cursor += 256;
1832 }
1833 }
1834 }
1835
1836 return 0;
1837}
1838
1839void CTextureManager::print_buff_info() {
1840}
u_long128 * buffer_end
Definition texture.hpp:80
char name[32]
Definition texture.hpp:73
u_long128 * buffer
Definition texture.hpp:79
LOADTEXTURE_INFO * file
Definition texture.hpp:104
u_long128 * buffer
Definition texture.hpp:101
CTexture textures[196]
Definition texture.hpp:100
CTextureBlock blocks[72]
Definition texture.hpp:99
short height
Definition texture.hpp:54
int swizzled
Definition texture.hpp:61
short bpp
Definition texture.hpp:55
short block
Definition texture.hpp:52
u_int * image[4]
Definition texture.hpp:59
char name[32]
Definition texture.hpp:56
u_int * clut
Definition texture.hpp:60
short width
Definition texture.hpp:53
u_long tex1
Definition texture.hpp:58
u_long tex0
Definition texture.hpp:57
u_char image_type
Definition tim2.hpp:35
u_short image_width
Definition tim2.hpp:36
u_short image_height
Definition tim2.hpp:37
u_short clut_colors
Definition tim2.hpp:47
u_short header_size
Definition tim2.hpp:46
u_char mipmap_count
Definition tim2.hpp:49
u_int mipmap_size[8]
Definition tim2.hpp:57
u_long tex1
Definition tim2.hpp:55
u_int image_size
Definition tim2.hpp:45
u_char image_type
Definition tim2.hpp:51