-
Notifications
You must be signed in to change notification settings - Fork 0
/
tex.c
626 lines (446 loc) · 12.4 KB
/
tex.c
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//
// Tex
// Texture manager for PSP
//
// Licensed under the BSD license. See LICENSE for details.
// Copyright (c) 2006 Daniel S. Neumeyer <[email protected]>
//
#include <tex.h>
#include <malloc.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <pspgu.h>
#include <pspdebug.h>
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//
// --- Constants ----------------------------------------------------------- //
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//
#define TEX_TEXTURE_TABLE_SIZE (256)
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//
// --- Types --------------------------------------------------------------- //
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//
typedef struct _TexMemoryBlockDescriptor
{
void *base;
u32 size;
struct _TexMemoryBlockDescriptor *next;
struct _TexMemoryBlockDescriptor *prev;
}
TexMemoryBlockDescriptor;
typedef struct _TexTextureTableEntry
{
char *file;
u16 referenceCount;
TexTexture *texture;
struct _TexTextureTableEntry *next;
struct _TexTextureTableEntry *prev;
}
TexTextureTableEntry;
#pragma pack(1)
typedef struct
{
u16 bufferWidth;
u16 bufferHeight;
u16 imageWidth;
u16 imageHeight;
u8 pixelFormat; // uses GU_PSM_* constants in pspgu.h
u8 reserved;
u32 pixelBytes;
}
TexFileHeader;
#pragma pack()
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//
// --- Variables ----------------------------------------------------------- //
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//
TexMemoryBlockDescriptor *gTexFreeBlocks = NULL;
TexMemoryBlockDescriptor *gTexUsedBlocks = NULL;
TexTextureTableEntry *gTexTextureTable[TEX_TEXTURE_TABLE_SIZE] = { NULL };
u32 gTexBytesPerPixel[4] = { 2, 2, 2, 4 };
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//
// --- Prototypes ---------------------------------------------------------- //
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//
void *texAllocateVRAM(u32 size);
void texFreeVRAM(void *pointer);
void texAddMemoryBlock(TexMemoryBlockDescriptor *block, TexMemoryBlockDescriptor **list);
void texRemoveMemoryBlock(TexMemoryBlockDescriptor *block, TexMemoryBlockDescriptor **list);
void texMergeFreeBlocks();
int texPowerOfTwo(int i);
int texMultipleOfEight(int i);
u32 texHash(const char *key);
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//
// --- Functions ----------------------------------------------------------- //
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//
void texInit(void *vramBlockBase, u32 vramBlockSize)
{
// Calculate vramBlockSize if necessary.
if (vramBlockSize == TEX_MAX_BLOCK_SIZE)
{
u32 base = ((u32) vramBlockBase) & 0x00ffffff;
vramBlockSize = 0x00200000 - base;
}
// Align buffer on a quadword boundary.
u32 boundaryOffset = 16 - (((u32) vramBlockBase) % 16);
if (boundaryOffset == 16) boundaryOffset = 0;
vramBlockBase += boundaryOffset;
vramBlockSize -= boundaryOffset;
// Create block lists.
gTexFreeBlocks = malloc(sizeof(TexMemoryBlockDescriptor));
gTexUsedBlocks = NULL;
if (! gTexFreeBlocks) return;
gTexFreeBlocks->base = vramBlockBase;
gTexFreeBlocks->size = vramBlockSize;
gTexFreeBlocks->next = NULL;
gTexFreeBlocks->prev = NULL;
}
TexTexture *texLoad(const char *file)
{
// Find texture in texture table.
u32 hash = texHash(file);
TexTextureTableEntry *entry = gTexTextureTable[hash];
while (entry)
{
if (strcmp(entry->file, file) == 0)
{
entry->referenceCount++;
return entry->texture;
}
entry = entry->next;
}
// Open file.
int fd = open(file, O_RDONLY, 0);
if (fd < 0) return NULL;
// Create texture data structure.
TexTexture *texture = malloc(sizeof(TexTexture));
if (! texture) goto TEX_LOAD_EXIT;
// Read file header.
TexFileHeader header;
int bytesRead = read(fd, &header, sizeof(header));
if (bytesRead != sizeof(header))
{
free(texture);
texture = NULL;
goto TEX_LOAD_EXIT;
}
texture->pixelFormat = header.pixelFormat;
texture->bufferWidth = header.bufferWidth;
texture->bufferHeight = header.bufferHeight;
texture->imageWidth = header.imageWidth;
texture->imageHeight = header.imageHeight;
// Create pixel buffer.
texture->pixels = texAllocateVRAM(header.pixelBytes);
if (! texture->pixels)
{
free(texture);
texture = NULL;
goto TEX_LOAD_EXIT;
}
// Read pixels.
bytesRead = read(fd, texture->pixels, header.pixelBytes);
if (header.pixelBytes != bytesRead)
{
texFreeVRAM(texture->pixels);
free(texture);
texture = NULL;
goto TEX_LOAD_EXIT;
}
// Add to texture table.
TexTextureTableEntry *newEntry = malloc(sizeof(TexTextureTableEntry));
if (newEntry) newEntry->file = malloc(strlen(file) + 1);
if (! (newEntry && newEntry->file))
{
if (newEntry) free(newEntry);
texFreeVRAM(texture->pixels);
free(texture);
texture = NULL;
goto TEX_LOAD_EXIT;
}
strcpy(newEntry->file, file);
newEntry->referenceCount = 1;
newEntry->texture = texture;
newEntry->prev = NULL;
newEntry->next = NULL;
entry = gTexTextureTable[hash];
if (! entry)
gTexTextureTable[hash] = newEntry;
else
{
while (entry->next)
entry = entry->next;
entry->next = newEntry;
}
newEntry->prev = entry;
texture->tableEntry = newEntry;
// Clean up.
TEX_LOAD_EXIT:
close(fd);
return texture;
}
TexTexture *texNew(u16 width, u16 height, u16 pixelFormat)
{
// Create texture structure.
TexTexture *texture = malloc(sizeof(TexTexture));
texture->pixelFormat = pixelFormat;
texture->bufferWidth = texPowerOfTwo(width);
texture->bufferHeight = texMultipleOfEight(height);
texture->imageWidth = width;
texture->imageHeight = height;
texture->pixels = texAllocateVRAM(texture->bufferWidth * texture->bufferHeight * (pixelFormat == GU_PSM_8888 ? 4 : 2));
if (! texture->pixels)
{
free(texture);
return NULL;
}
// Add to texture table.
TexTextureTableEntry *newEntry = malloc(sizeof(TexTextureTableEntry));
if (newEntry) newEntry->file = malloc(1);
if (! (newEntry && newEntry->file))
{
if (newEntry) free(newEntry);
texFreeVRAM(texture->pixels);
free(texture);
return NULL;
}
newEntry->file[0] = 0;
newEntry->referenceCount = 1;
newEntry->texture = texture;
newEntry->prev = NULL;
newEntry->next = NULL;
u32 hash = texHash("");
TexTextureTableEntry *entry = gTexTextureTable[hash];
if (! entry)
gTexTextureTable[hash] = newEntry;
else
{
while (entry->next)
entry = entry->next;
entry->next = newEntry;
}
newEntry->prev = entry;
texture->tableEntry = newEntry;
return texture;
}
void texSwizzle(TexTexture *texture)
{
u8* out;
u8* in;
unsigned width;
unsigned height;
out = malloc(texture->bufferWidth * texture->bufferHeight * (texture->pixelFormat == GU_PSM_8888 ? 4 : 2));
if (! out) return;
in = texture->pixels;
width = texture->bufferWidth * (texture->pixelFormat == GU_PSM_8888 ? 4 : 2);
height = texture->bufferHeight;
unsigned int blockx, blocky;
unsigned int j;
unsigned int width_blocks = (width / 16);
unsigned int height_blocks = (height / 8);
unsigned int src_pitch = (width-16)/4;
unsigned int src_row = width * 8;
const u8* ysrc = in;
u32* dst = (u32*) out;
for (blocky = 0; blocky < height_blocks; ++blocky)
{
const u8* xsrc = ysrc;
for (blockx = 0; blockx < width_blocks; ++blockx)
{
const u32* src = (u32*) xsrc;
for (j = 0; j < 8; ++j)
{
*(dst++) = *(src++);
*(dst++) = *(src++);
*(dst++) = *(src++);
*(dst++) = *(src++);
src += src_pitch;
}
xsrc += 16;
}
ysrc += src_row;
}
}
void texFree(TexTexture *texture)
{
TexTextureTableEntry *entry = (TexTextureTableEntry *) texture->tableEntry;
entry->referenceCount--;
if (entry->referenceCount == 0)
{
if (entry->next) entry->next->prev = entry->prev;
if (entry->prev)
entry->prev->next = entry->next;
else
{
u32 hash = texHash(entry->file);
gTexTextureTable[hash] = entry->next;
}
free(entry->file);
free(entry);
texFreeVRAM(texture->pixels);
free(texture);
}
}
TexTexture **texLoadList(const char **files, unsigned count)
{
if (count == 0) return NULL;
TexTexture **textures = malloc(count * sizeof(TexTexture*));
if (! textures) return NULL;
while (count--)
textures[count] = texLoad(files[count]);
return textures;
}
void texFreeList(TexTexture **textures, unsigned count)
{
if (count == 0) return;
while (count--) texFree(textures[count]);
free(textures);
}
void texSet(const TexTexture *texture)
{
sceGuTexMode(texture->pixelFormat, 0, 0, 1);
sceGuTexImage(0, texture->bufferWidth, texture->bufferHeight, texture->bufferWidth, texture->pixels);
}
void *texAllocateVRAM(u32 size)
{
if (size == 0) return NULL;
TexMemoryBlockDescriptor *block;
TexMemoryBlockDescriptor *bestBlock = gTexFreeBlocks;
// Find best-fit block.
block = gTexFreeBlocks;
while (block)
{
if (block->size >= size && block->size < bestBlock->size)
bestBlock = block;
block = block->next;
}
if ((! bestBlock) || bestBlock->size < size)
return NULL;
// Move free block to used block list.
if (bestBlock->size == size)
{
texRemoveMemoryBlock(bestBlock, &gTexFreeBlocks);
texAddMemoryBlock(bestBlock, &gTexUsedBlocks);
}
else
{
TexMemoryBlockDescriptor *newFreeBlock = malloc(sizeof(TexMemoryBlockDescriptor));
if (! newFreeBlock) return NULL;
newFreeBlock->base = (void *) (((u32) bestBlock->base) + size);
newFreeBlock->size = bestBlock->size - size;
newFreeBlock->next = NULL;
newFreeBlock->prev = NULL;
bestBlock->size = size;
texRemoveMemoryBlock(bestBlock, &gTexFreeBlocks);
texAddMemoryBlock(bestBlock, &gTexUsedBlocks);
texAddMemoryBlock(newFreeBlock, &gTexFreeBlocks);
}
texMergeFreeBlocks();
return bestBlock->base;
}
void texFreeVRAM(void *pointer)
{
TexMemoryBlockDescriptor *block;
// Find pointer in used block list.
block = gTexUsedBlocks;
while (block)
{
if (block->base == pointer) break;
block = block->next;
}
if (! block) return;
// Move block from used list to free list.
texRemoveMemoryBlock(block, &gTexUsedBlocks);
texAddMemoryBlock(block, &gTexFreeBlocks);
texMergeFreeBlocks();
}
void texAddMemoryBlock(TexMemoryBlockDescriptor *block, TexMemoryBlockDescriptor **list)
{
if (! *list)
{
*list = block;
return;
}
// Insert block in sort order by base address.
TexMemoryBlockDescriptor *listBlock = *list;
while (listBlock)
{
if (listBlock->base > block->base)
{
if (listBlock == *list)
{
*list = block;
block->next = listBlock;
listBlock->prev = block;
}
else
{
block->prev = listBlock->prev;
block->next = listBlock;
listBlock->prev->next = block;
listBlock->prev = block;
}
break;
}
else if (listBlock->next == NULL)
{
block->prev = listBlock;
listBlock->next = block;
break;
}
listBlock = listBlock->next;
}
}
void texRemoveMemoryBlock(TexMemoryBlockDescriptor *block, TexMemoryBlockDescriptor **list)
{
if (*list == block)
*list = block->next;
if (block->prev != NULL)
block->prev->next = block->next;
if (block->next != NULL)
block->next->prev = block->prev;
block->prev = NULL;
block->next = NULL;
}
void texMergeFreeBlocks()
{
TexMemoryBlockDescriptor *block;
// Find adjacent free blocks.
block = gTexFreeBlocks;
while (block && block->next)
{
TexMemoryBlockDescriptor *nextBlock = block->next;
if (((u32) block->base) + block->size == (u32) nextBlock->base)
{
block->size += nextBlock->size;
block->next = nextBlock->next;
if (block->next)
block->next->prev = block;
free(nextBlock);
continue;
}
block = block->next;
}
}
int texPowerOfTwo(int i)
{
if (i <= 2) return 2;
if (i <= 4) return 4;
if (i <= 8) return 8;
if (i <= 16) return 16;
if (i <= 32) return 32;
if (i <= 64) return 64;
if (i <= 128) return 128;
if (i <= 256) return 256;
return 512;
}
int texMultipleOfEight(int i)
{
int multiple = i / 8;
if (i % 8) multiple++;
return multiple;
}
u32 texHash(const char *key)
{
unsigned length = strlen(key);
u32 hash = length;
while (length--)
hash = ((hash << 5) ^ (hash >> 27)) ^ *key++;
return hash % TEX_TEXTURE_TABLE_SIZE;
}