filename | src/pvr2/texcache.c |
changeset | 477:9a373f2ff009 |
prev | 462:9add12452876 |
next | 561:533f6b478071 |
author | nkeynes |
date | Wed Oct 31 09:10:23 2007 +0000 (15 years ago) |
permissions | -rw-r--r-- |
last change | Add save/restore of render buffers in save states Gzip memory blocks in save states Move front-buffer management back to pvr2 Add screenshot preview when loading save states Various minor fixes and cleanups |
view | annotate | diff | log | raw |
1 /**
2 * $Id: texcache.c,v 1.29 2007-10-31 09:10:23 nkeynes Exp $
3 *
4 * Texture cache. Responsible for maintaining a working set of OpenGL
5 * textures.
6 *
7 *
8 * Copyright (c) 2005 Nathan Keynes.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 */
21 #include <assert.h>
22 #include "pvr2/pvr2.h"
24 /** Specifies the maximum number of OpenGL
25 * textures we're willing to have open at a time. If more are
26 * needed, textures will be evicted in LRU order.
27 */
28 #define MAX_TEXTURES 256
30 /**
31 * Data structure:
32 *
33 * Main operations:
34 * find entry by texture_addr
35 * add new entry
36 * move entry to tail of lru list
37 * remove entry
38 */
40 typedef signed short texcache_entry_index;
41 #define EMPTY_ENTRY -1
43 static texcache_entry_index texcache_free_ptr = 0;
44 static GLuint texcache_free_list[MAX_TEXTURES];
46 typedef struct texcache_entry {
47 uint32_t texture_addr;
48 int width, height, mode;
49 GLuint texture_id;
50 texcache_entry_index next;
51 uint32_t lru_count;
52 } *texcache_entry_t;
54 static texcache_entry_index texcache_page_lookup[PVR2_RAM_PAGES];
55 static uint32_t texcache_ref_counter;
56 static struct texcache_entry texcache_active_list[MAX_TEXTURES];
58 /**
59 * Initialize the texture cache.
60 */
61 void texcache_init( )
62 {
63 int i;
64 for( i=0; i<PVR2_RAM_PAGES; i++ ) {
65 texcache_page_lookup[i] = EMPTY_ENTRY;
66 }
67 for( i=0; i<MAX_TEXTURES; i++ ) {
68 texcache_free_list[i] = i;
69 texcache_active_list[i].texture_addr = -1;
70 }
71 texcache_free_ptr = 0;
72 texcache_ref_counter = 0;
73 }
75 /**
76 * Setup the initial texture ids (must be called after the GL context is
77 * prepared)
78 */
79 void texcache_gl_init( )
80 {
81 int i;
82 GLuint texids[MAX_TEXTURES];
84 glGenTextures( MAX_TEXTURES, texids );
85 for( i=0; i<MAX_TEXTURES; i++ ) {
86 texcache_active_list[i].texture_id = texids[i];
87 }
88 }
90 /**
91 * Flush all textures from the cache, returning them to the free list.
92 */
93 void texcache_flush( )
94 {
95 int i;
96 /* clear structures */
97 for( i=0; i<PVR2_RAM_PAGES; i++ ) {
98 texcache_page_lookup[i] = EMPTY_ENTRY;
99 }
100 for( i=0; i<MAX_TEXTURES; i++ ) {
101 texcache_free_list[i] = i;
102 }
103 texcache_free_ptr = 0;
104 texcache_ref_counter = 0;
105 }
107 /**
108 * Flush all textures and delete. The cache will be non-functional until
109 * the next call to texcache_init(). This would typically be done if
110 * switching GL targets.
111 */
112 void texcache_shutdown( )
113 {
114 GLuint texids[MAX_TEXTURES];
115 int i;
116 texcache_flush();
118 for( i=0; i<MAX_TEXTURES; i++ ) {
119 texids[i] = texcache_active_list[i].texture_id;
120 }
121 glDeleteTextures( MAX_TEXTURES, texids );
122 }
124 static void texcache_evict( int slot )
125 {
126 /* Remove the selected slot from the lookup table */
127 assert( texcache_active_list[slot].texture_addr != -1 );
128 uint32_t evict_page = texcache_active_list[slot].texture_addr >> 12;
129 texcache_entry_index replace_next = texcache_active_list[slot].next;
130 texcache_active_list[slot].texture_addr = -1;
131 texcache_active_list[slot].next = EMPTY_ENTRY; /* Just for safety */
132 if( texcache_page_lookup[evict_page] == slot ) {
133 texcache_page_lookup[evict_page] = replace_next;
134 } else {
135 texcache_entry_index idx = texcache_page_lookup[evict_page];
136 texcache_entry_index next;
137 do {
138 next = texcache_active_list[idx].next;
139 if( next == slot ) {
140 assert( idx != replace_next );
141 texcache_active_list[idx].next = replace_next;
142 break;
143 }
144 idx = next;
145 } while( next != EMPTY_ENTRY );
146 }
147 }
149 /**
150 * Evict a single texture from the cache.
151 * @return the slot of the evicted texture.
152 */
153 static texcache_entry_index texcache_evict_lru( void )
154 {
155 /* Full table scan - take over the entry with the lowest lru value */
156 texcache_entry_index slot = 0;
157 int lru_value = texcache_active_list[0].lru_count;
158 int i;
159 for( i=1; i<MAX_TEXTURES; i++ ) {
160 /* FIXME: account for rollover */
161 if( texcache_active_list[i].lru_count < lru_value ) {
162 slot = i;
163 lru_value = texcache_active_list[i].lru_count;
164 }
165 }
166 texcache_evict(slot);
168 return slot;
169 }
171 /**
172 * Evict all textures contained in the page identified by a texture address.
173 */
174 void texcache_invalidate_page( uint32_t texture_addr ) {
175 uint32_t texture_page = texture_addr >> 12;
176 texcache_entry_index idx = texcache_page_lookup[texture_page];
177 if( idx == EMPTY_ENTRY )
178 return;
179 assert( texcache_free_ptr >= 0 );
180 do {
181 texcache_entry_t entry = &texcache_active_list[idx];
182 entry->texture_addr = -1;
183 /* release entry */
184 texcache_free_ptr--;
185 texcache_free_list[texcache_free_ptr] = idx;
186 idx = entry->next;
187 entry->next = EMPTY_ENTRY;
188 } while( idx != EMPTY_ENTRY );
189 texcache_page_lookup[texture_page] = EMPTY_ENTRY;
190 }
192 /**
193 * Mark all textures that use the palette table as needing a re-read (ie
194 * for when the palette is changed. We could track exactly which ones are
195 * affected, but it's not clear that the extra maintanence overhead is
196 * worthwhile.
197 */
198 void texcache_invalidate_palette( )
199 {
200 int i;
201 for( i=0; i<MAX_TEXTURES; i++ ) {
202 if( texcache_active_list[i].texture_addr != -1 &&
203 PVR2_TEX_IS_PALETTE(texcache_active_list[i].mode) ) {
204 texcache_evict( i );
205 texcache_free_ptr--;
206 texcache_free_list[texcache_free_ptr] = i;
207 }
208 }
209 }
211 static void decode_pal8_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
212 {
213 int i;
214 for( i=0; i<inbytes; i++ ) {
215 *out++ = pal[*in++];
216 }
217 }
219 static void decode_pal8_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
220 {
221 int i;
222 for( i=0; i<inbytes; i++ ) {
223 *out++ = (uint16_t)pal[*in++];
224 }
225 }
227 static void decode_pal4_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
228 {
229 int i;
230 for( i=0; i<inbytes; i++ ) {
231 *out++ = pal[*in & 0x0F];
232 *out++ = pal[(*in >> 4)];
233 in++;
234 }
235 }
238 static void decode_pal4_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
239 {
240 int i;
241 for( i=0; i<inbytes; i++ ) {
242 *out++ = (uint16_t)pal[*in & 0x0F];
243 *out++ = (uint16_t)pal[(*in >> 4)];
244 in++;
245 }
246 }
248 #define VQ_CODEBOOK_SIZE 2048 /* 256 entries * 4 pixels per quad * 2 byte pixels */
250 struct vq_codebook {
251 uint16_t quad[256][4];
252 };
254 static void vq_get_codebook( struct vq_codebook *codebook,
255 uint16_t *input )
256 {
257 /* Detwiddle the codebook, for the sake of my own sanity if nothing else */
258 uint16_t *p = (uint16_t *)input;
259 int i;
260 for( i=0; i<256; i++ ) {
261 codebook->quad[i][0] = *p++;
262 codebook->quad[i][2] = *p++;
263 codebook->quad[i][1] = *p++;
264 codebook->quad[i][3] = *p++;
265 }
266 }
268 static void vq_decode( uint16_t *output, unsigned char *input, int width, int height,
269 struct vq_codebook *codebook ) {
270 int i,j;
272 uint8_t *c = (uint8_t *)input;
273 for( j=0; j<height; j+=2 ) {
274 for( i=0; i<width; i+=2 ) {
275 uint8_t code = *c++;
276 output[i + j*width] = codebook->quad[code][0];
277 output[i + 1 + j*width] = codebook->quad[code][1];
278 output[i + (j+1)*width] = codebook->quad[code][2];
279 output[i + 1 + (j+1)*width] = codebook->quad[code][3];
280 }
281 }
282 }
284 static inline uint32_t yuv_to_rgb32( float y, float u, float v )
285 {
286 u -= 128;
287 v -= 128;
288 int r = (int)(y + v*1.375);
289 int g = (int)(y - u*0.34375 - v*0.6875);
290 int b = (int)(y + u*1.71875);
291 if( r > 255 ) { r = 255; } else if( r < 0 ) { r = 0; }
292 if( g > 255 ) { g = 255; } else if( g < 0 ) { g = 0; }
293 if( b > 255 ) { b = 255; } else if( b < 0 ) { b = 0; }
294 return 0xFF000000 | (r<<16) | (g<<8) | (b);
295 }
298 /**
299 * Convert raster YUV texture data into RGB32 data - most GL implementations don't
300 * directly support this format unfortunately. The input data is formatted as
301 * 32 bits = 2 horizontal pixels, UYVY. This is currently done rather inefficiently
302 * in floating point.
303 */
304 static void yuv_decode( uint32_t *output, uint32_t *input, int width, int height )
305 {
306 int x, y;
307 uint32_t *p = input;
308 for( y=0; y<height; y++ ) {
309 for( x=0; x<width; x+=2 ) {
310 float u = (float)(*p & 0xFF);
311 float y0 = (float)( (*p>>8)&0xFF );
312 float v = (float)( (*p>>16)&0xFF );
313 float y1 = (float)( (*p>>24)&0xFF );
314 *output++ = yuv_to_rgb32( y0, u, v );
315 *output++ = yuv_to_rgb32( y1, u, v );
316 p++;
317 }
318 }
319 }
321 /**
322 * Load texture data from the given address and parameters into the currently
323 * bound OpenGL texture.
324 */
325 static void texcache_load_texture( uint32_t texture_addr, int width, int height,
326 int mode ) {
327 int bpp_shift = 1; /* bytes per (output) pixel as a power of 2 */
328 GLint intFormat = GL_RGBA, format, type;
329 int tex_format = mode & PVR2_TEX_FORMAT_MASK;
330 struct vq_codebook codebook;
331 GLint filter = GL_LINEAR;
333 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
334 /* Decode the format parameters */
335 switch( tex_format ) {
336 case PVR2_TEX_FORMAT_IDX4:
337 case PVR2_TEX_FORMAT_IDX8:
338 /* For indexed-colour modes, we need to lookup the palette control
339 * word to determine the de-indexed texture format.
340 */
341 switch( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03 ) {
342 case 0: /* ARGB1555 */
343 format = GL_BGRA;
344 type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
345 break;
346 case 1: /* RGB565 */
347 intFormat = GL_RGB;
348 format = GL_RGB;
349 type = GL_UNSIGNED_SHORT_5_6_5;
350 break;
351 case 2: /* ARGB4444 */
352 format = GL_BGRA;
353 type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
354 break;
355 case 3: /* ARGB8888 */
356 format = GL_BGRA;
357 type = GL_UNSIGNED_BYTE;
358 bpp_shift = 2;
359 break;
360 default:
361 return; /* Can't happen, but it makes gcc stop complaining */
362 }
363 break;
365 case PVR2_TEX_FORMAT_ARGB1555:
366 format = GL_BGRA;
367 type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
368 break;
369 case PVR2_TEX_FORMAT_RGB565:
370 intFormat = GL_RGB;
371 format = GL_RGB;
372 type = GL_UNSIGNED_SHORT_5_6_5;
373 break;
374 case PVR2_TEX_FORMAT_ARGB4444:
375 format = GL_BGRA;
376 type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
377 break;
378 case PVR2_TEX_FORMAT_YUV422:
379 /* YUV422 isn't directly supported by most implementations, so decode
380 * it to a (reasonably) standard ARGB32.
381 */
382 bpp_shift = 2;
383 format = GL_BGRA;
384 type = GL_UNSIGNED_BYTE;
385 break;
386 case PVR2_TEX_FORMAT_BUMPMAP:
387 ERROR( "Bumpmap not supported" );
388 return;
389 default:
390 ERROR( "Undefined texture format" );
391 return;
392 }
394 if( PVR2_TEX_IS_STRIDE(mode) && tex_format != PVR2_TEX_FORMAT_IDX4 &&
395 tex_format != PVR2_TEX_FORMAT_IDX8 ) {
396 /* Stride textures cannot be mip-mapped, compressed, indexed or twiddled */
397 uint32_t stride = (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5;
398 unsigned char data[(width*height) << bpp_shift];
399 if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
400 unsigned char tmp[(width*height)<<1];
401 pvr2_vram64_read_stride( tmp, width<<1, texture_addr, stride<<1, height );
402 yuv_decode( (uint32_t *)data, (uint32_t *)tmp, width, height );
403 } else {
404 pvr2_vram64_read_stride( data, width<<bpp_shift, texture_addr, stride<<bpp_shift, height );
405 }
406 glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type, data );
407 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
408 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
409 return;
410 }
412 int level=0, last_level = 0, mip_width = width, mip_height = height, src_bytes, dest_bytes;
413 if( PVR2_TEX_IS_MIPMAPPED(mode) ) {
414 int i;
415 for( i=0; 1<<i < width; i++ );
416 last_level = i;
417 mip_width = 2;
418 mip_height= 2;
419 filter = GL_LINEAR_MIPMAP_LINEAR;
420 }
421 dest_bytes = (mip_width * mip_height) << bpp_shift;
422 src_bytes = dest_bytes; // Modes will change this (below)
424 if( PVR2_TEX_IS_COMPRESSED(mode) ) {
425 uint16_t tmp[VQ_CODEBOOK_SIZE];
426 pvr2_vram64_read( (unsigned char *)tmp, texture_addr, VQ_CODEBOOK_SIZE );
427 texture_addr += VQ_CODEBOOK_SIZE;
428 vq_get_codebook( &codebook, tmp );
429 }
431 for( level=last_level; level>= 0; level-- ) {
432 unsigned char data[dest_bytes];
433 /* load data from image, detwiddling/uncompressing as required */
434 if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
435 src_bytes = (mip_width * mip_height);
436 int bank = (mode >> 25) &0x03;
437 uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<8);
438 unsigned char tmp[src_bytes];
439 pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width, mip_height );
440 if( bpp_shift == 2 ) {
441 decode_pal8_to_32( (uint32_t *)data, tmp, src_bytes, palette );
442 } else {
443 decode_pal8_to_16( (uint16_t *)data, tmp, src_bytes, palette );
444 }
445 } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
446 src_bytes = (mip_width * mip_height) >> 1;
447 int bank = (mode >>21 ) & 0x3F;
448 uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<4);
449 unsigned char tmp[src_bytes];
450 pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
451 if( bpp_shift == 2 ) {
452 decode_pal4_to_32( (uint32_t *)data, tmp, src_bytes, palette );
453 } else {
454 decode_pal4_to_16( (uint16_t *)data, tmp, src_bytes, palette );
455 }
456 } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
457 src_bytes = ((mip_width*mip_height)<<1);
458 unsigned char tmp[src_bytes];
459 if( PVR2_TEX_IS_TWIDDLED(mode) ) {
460 pvr2_vram64_read_twiddled_16( tmp, texture_addr, mip_width, mip_height );
461 } else {
462 pvr2_vram64_read( tmp, texture_addr, src_bytes );
463 }
464 yuv_decode( (uint32_t *)data, (uint32_t *)tmp, mip_width, mip_height );
465 } else if( PVR2_TEX_IS_COMPRESSED(mode) ) {
466 src_bytes = ((mip_width*mip_height) >> 2);
467 unsigned char tmp[src_bytes];
468 if( PVR2_TEX_IS_TWIDDLED(mode) ) {
469 pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width>>1, mip_height>>1 );
470 } else {
471 pvr2_vram64_read( tmp, texture_addr, src_bytes );
472 }
473 vq_decode( (uint16_t *)data, tmp, mip_width, mip_height, &codebook );
474 } else if( PVR2_TEX_IS_TWIDDLED(mode) ) {
475 pvr2_vram64_read_twiddled_16( data, texture_addr, mip_width, mip_height );
476 } else {
477 pvr2_vram64_read( data, texture_addr, src_bytes );
478 }
480 /* Pass to GL */
481 if( level == last_level && level != 0 ) { /* 1x1 stored within a 2x2 */
482 glTexImage2D( GL_TEXTURE_2D, level, intFormat, 1, 1, 0, format, type,
483 data + (3 << bpp_shift) );
484 texture_addr += src_bytes;
485 } else {
486 glTexImage2D( GL_TEXTURE_2D, level, intFormat, mip_width, mip_height, 0, format, type,
487 data );
488 texture_addr += src_bytes;
489 mip_width <<= 1;
490 mip_height <<= 1;
491 dest_bytes <<= 2;
492 src_bytes <<= 2;
493 }
494 }
496 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
497 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
498 }
500 /**
501 * Return a texture ID for the texture specified at the supplied address
502 * and given parameters (the same sequence of bytes could in theory have
503 * multiple interpretations). We use the texture address as the primary
504 * index, but allow for multiple instances at each address. The texture
505 * will be bound to the GL_TEXTURE_2D target before being returned.
506 *
507 * If the texture has already been bound, return the ID to which it was
508 * bound. Otherwise obtain an unused texture ID and set it up appropriately.
509 */
510 GLuint texcache_get_texture( uint32_t texture_addr, int width, int height,
511 int mode )
512 {
513 uint32_t texture_page = texture_addr >> 12;
514 texcache_entry_index next;
515 texcache_entry_index idx = texcache_page_lookup[texture_page];
516 while( idx != EMPTY_ENTRY ) {
517 texcache_entry_t entry = &texcache_active_list[idx];
518 if( entry->texture_addr == texture_addr &&
519 entry->mode == mode &&
520 entry->width == width &&
521 entry->height == height ) {
522 entry->lru_count = texcache_ref_counter++;
523 glBindTexture( GL_TEXTURE_2D, entry->texture_id );
524 return entry->texture_id;
525 }
526 idx = entry->next;
527 }
529 /* Not found - check the free list */
530 texcache_entry_index slot = 0;
532 if( texcache_free_ptr < MAX_TEXTURES ) {
533 slot = texcache_free_list[texcache_free_ptr++];
534 } else {
535 slot = texcache_evict_lru();
536 }
538 /* Construct new entry */
539 texcache_active_list[slot].texture_addr = texture_addr;
540 texcache_active_list[slot].width = width;
541 texcache_active_list[slot].height = height;
542 texcache_active_list[slot].mode = mode;
543 texcache_active_list[slot].lru_count = texcache_ref_counter++;
545 /* Add entry to the lookup table */
546 next = texcache_page_lookup[texture_page];
547 if( next == slot ) {
548 int i;
549 fprintf( stderr, "Active list: " );
550 for( i=0; i<MAX_TEXTURES; i++ ) {
551 fprintf( stderr, "%d, ", texcache_active_list[i].next );
552 }
553 fprintf( stderr, "\n" );
554 assert( next != slot );
556 }
557 assert( next != slot );
558 texcache_active_list[slot].next = next;
559 texcache_page_lookup[texture_page] = slot;
561 /* Construct the GL texture */
562 glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
563 texcache_load_texture( texture_addr, width, height, mode );
565 return texcache_active_list[slot].texture_id;
566 }
.