4 * Texture cache. Responsible for maintaining a working set of OpenGL
8 * Copyright (c) 2005 Nathan Keynes.
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.
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.
23 #include "pvr2/pvr2.h"
24 #include "pvr2/pvr2mmio.h"
26 /** Specifies the maximum number of OpenGL
27 * textures we're willing to have open at a time. If more are
28 * needed, textures will be evicted in LRU order.
30 #define MAX_TEXTURES 256
36 * find entry by texture_addr
38 * move entry to tail of lru list
42 typedef signed short texcache_entry_index;
43 #define EMPTY_ENTRY -1
45 static texcache_entry_index texcache_free_ptr = 0;
46 static GLuint texcache_free_list[MAX_TEXTURES];
48 typedef struct texcache_entry {
49 uint32_t texture_addr;
50 int width, height, mode;
52 render_buffer_t buffer;
53 texcache_entry_index next;
57 static texcache_entry_index texcache_page_lookup[PVR2_RAM_PAGES];
58 static uint32_t texcache_ref_counter;
59 static struct texcache_entry texcache_active_list[MAX_TEXTURES];
62 * Initialize the texture cache.
67 for( i=0; i<PVR2_RAM_PAGES; i++ ) {
68 texcache_page_lookup[i] = EMPTY_ENTRY;
70 for( i=0; i<MAX_TEXTURES; i++ ) {
71 texcache_free_list[i] = i;
72 texcache_active_list[i].texture_addr = -1;
73 texcache_active_list[i].buffer = NULL;
74 texcache_active_list[i].next = EMPTY_ENTRY;
76 texcache_free_ptr = 0;
77 texcache_ref_counter = 0;
81 * Setup the initial texture ids (must be called after the GL context is
84 void texcache_gl_init( )
87 GLuint texids[MAX_TEXTURES];
89 glGenTextures( MAX_TEXTURES, texids );
90 for( i=0; i<MAX_TEXTURES; i++ ) {
91 texcache_active_list[i].texture_id = texids[i];
95 void texcache_release_render_buffer( render_buffer_t buffer )
97 if( !buffer->flushed )
98 pvr2_render_buffer_copy_to_sh4(buffer);
99 pvr2_destroy_render_buffer(buffer);
103 * Flush all textures from the cache, returning them to the free list.
105 void texcache_flush( )
108 /* clear structures */
109 for( i=0; i<PVR2_RAM_PAGES; i++ ) {
110 texcache_page_lookup[i] = EMPTY_ENTRY;
112 for( i=0; i<MAX_TEXTURES; i++ ) {
113 texcache_free_list[i] = i;
114 texcache_active_list[i].next = EMPTY_ENTRY;
115 if( texcache_active_list[i].buffer != NULL ) {
116 texcache_release_render_buffer(texcache_active_list[i].buffer);
117 texcache_active_list[i].buffer = NULL;
120 texcache_free_ptr = 0;
121 texcache_ref_counter = 0;
125 * Flush all textures and delete. The cache will be non-functional until
126 * the next call to texcache_init(). This would typically be done if
127 * switching GL targets.
129 void texcache_shutdown( )
131 GLuint texids[MAX_TEXTURES];
135 for( i=0; i<MAX_TEXTURES; i++ ) {
136 texids[i] = texcache_active_list[i].texture_id;
138 glDeleteTextures( MAX_TEXTURES, texids );
141 static void texcache_evict( int slot )
143 /* Remove the selected slot from the lookup table */
144 assert( texcache_active_list[slot].texture_addr != -1 );
145 uint32_t evict_page = texcache_active_list[slot].texture_addr >> 12;
146 texcache_entry_index replace_next = texcache_active_list[slot].next;
147 texcache_active_list[slot].texture_addr = -1;
148 texcache_active_list[slot].next = EMPTY_ENTRY; /* Just for safety */
149 if( texcache_active_list[slot].buffer != NULL ) {
150 texcache_release_render_buffer(texcache_active_list[slot].buffer);
151 texcache_active_list[slot].buffer = NULL;
153 if( texcache_page_lookup[evict_page] == slot ) {
154 texcache_page_lookup[evict_page] = replace_next;
156 texcache_entry_index idx = texcache_page_lookup[evict_page];
157 texcache_entry_index next;
159 next = texcache_active_list[idx].next;
161 assert( idx != replace_next );
162 texcache_active_list[idx].next = replace_next;
166 } while( next != EMPTY_ENTRY );
171 * Evict a single texture from the cache.
172 * @return the slot of the evicted texture.
174 static texcache_entry_index texcache_evict_lru( void )
176 /* Full table scan - take over the entry with the lowest lru value */
177 texcache_entry_index slot = 0;
178 int lru_value = texcache_active_list[0].lru_count;
180 for( i=1; i<MAX_TEXTURES; i++ ) {
181 /* FIXME: account for rollover */
182 if( texcache_active_list[i].lru_count < lru_value ) {
184 lru_value = texcache_active_list[i].lru_count;
187 texcache_evict(slot);
193 * Evict all textures contained in the page identified by a texture address.
195 void texcache_invalidate_page( uint32_t texture_addr ) {
196 uint32_t texture_page = texture_addr >> 12;
197 texcache_entry_index idx = texcache_page_lookup[texture_page];
198 if( idx == EMPTY_ENTRY )
200 assert( texcache_free_ptr >= 0 );
202 texcache_entry_t entry = &texcache_active_list[idx];
203 entry->texture_addr = -1;
204 if( entry->buffer != NULL ) {
205 texcache_release_render_buffer(entry->buffer);
206 entry->buffer = NULL;
210 texcache_free_list[texcache_free_ptr] = idx;
212 entry->next = EMPTY_ENTRY;
213 } while( idx != EMPTY_ENTRY );
214 texcache_page_lookup[texture_page] = EMPTY_ENTRY;
218 * Mark all textures that use the palette table as needing a re-read (ie
219 * for when the palette is changed. We could track exactly which ones are
220 * affected, but it's not clear that the extra maintanence overhead is
223 void texcache_invalidate_palette( )
226 for( i=0; i<MAX_TEXTURES; i++ ) {
227 if( texcache_active_list[i].texture_addr != -1 &&
228 PVR2_TEX_IS_PALETTE(texcache_active_list[i].mode) ) {
231 texcache_free_list[texcache_free_ptr] = i;
236 static void decode_pal8_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
239 for( i=0; i<inbytes; i++ ) {
244 static void decode_pal8_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
247 for( i=0; i<inbytes; i++ ) {
248 *out++ = (uint16_t)pal[*in++];
252 static void decode_pal4_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
255 for( i=0; i<inbytes; i++ ) {
256 *out++ = pal[*in & 0x0F];
257 *out++ = pal[(*in >> 4)];
263 static void decode_pal4_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
266 for( i=0; i<inbytes; i++ ) {
267 *out++ = (uint16_t)pal[*in & 0x0F];
268 *out++ = (uint16_t)pal[(*in >> 4)];
273 #define VQ_CODEBOOK_SIZE 2048 /* 256 entries * 4 pixels per quad * 2 byte pixels */
276 uint16_t quad[256][4];
279 static void vq_get_codebook( struct vq_codebook *codebook,
282 /* Detwiddle the codebook, for the sake of my own sanity if nothing else */
283 uint16_t *p = (uint16_t *)input;
285 for( i=0; i<256; i++ ) {
286 codebook->quad[i][0] = *p++;
287 codebook->quad[i][2] = *p++;
288 codebook->quad[i][1] = *p++;
289 codebook->quad[i][3] = *p++;
293 static void vq_decode( uint16_t *output, unsigned char *input, int width, int height,
294 struct vq_codebook *codebook ) {
297 uint8_t *c = (uint8_t *)input;
298 for( j=0; j<height; j+=2 ) {
299 for( i=0; i<width; i+=2 ) {
301 output[i + j*width] = codebook->quad[code][0];
302 output[i + 1 + j*width] = codebook->quad[code][1];
303 output[i + (j+1)*width] = codebook->quad[code][2];
304 output[i + 1 + (j+1)*width] = codebook->quad[code][3];
309 static inline uint32_t yuv_to_rgb32( float y, float u, float v )
313 int r = (int)(y + v*1.375);
314 int g = (int)(y - u*0.34375 - v*0.6875);
315 int b = (int)(y + u*1.71875);
316 if( r > 255 ) { r = 255; } else if( r < 0 ) { r = 0; }
317 if( g > 255 ) { g = 255; } else if( g < 0 ) { g = 0; }
318 if( b > 255 ) { b = 255; } else if( b < 0 ) { b = 0; }
319 return 0xFF000000 | (r<<16) | (g<<8) | (b);
324 * Convert raster YUV texture data into RGB32 data - most GL implementations don't
325 * directly support this format unfortunately. The input data is formatted as
326 * 32 bits = 2 horizontal pixels, UYVY. This is currently done rather inefficiently
329 static void yuv_decode( uint32_t *output, uint32_t *input, int width, int height )
333 for( y=0; y<height; y++ ) {
334 for( x=0; x<width; x+=2 ) {
335 float u = (float)(*p & 0xFF);
336 float y0 = (float)( (*p>>8)&0xFF );
337 float v = (float)( (*p>>16)&0xFF );
338 float y1 = (float)( (*p>>24)&0xFF );
339 *output++ = yuv_to_rgb32( y0, u, v );
340 *output++ = yuv_to_rgb32( y1, u, v );
346 static gboolean is_npot_texture( int width )
348 while( width != 0 ) {
357 * Load texture data from the given address and parameters into the currently
358 * bound OpenGL texture.
360 static void texcache_load_texture( uint32_t texture_addr, int width, int height,
362 int bpp_shift = 1; /* bytes per (output) pixel as a power of 2 */
363 GLint intFormat = GL_RGBA, format, type;
364 int tex_format = mode & PVR2_TEX_FORMAT_MASK;
365 struct vq_codebook codebook;
366 GLint filter = GL_LINEAR;
368 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
370 /* Decode the format parameters */
371 switch( tex_format ) {
372 case PVR2_TEX_FORMAT_IDX4:
373 case PVR2_TEX_FORMAT_IDX8:
374 /* For indexed-colour modes, we need to lookup the palette control
375 * word to determine the de-indexed texture format.
377 switch( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03 ) {
378 case 0: /* ARGB1555 */
380 type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
385 type = GL_UNSIGNED_SHORT_5_6_5;
387 case 2: /* ARGB4444 */
389 type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
391 case 3: /* ARGB8888 */
393 type = GL_UNSIGNED_BYTE;
397 return; /* Can't happen, but it makes gcc stop complaining */
402 case PVR2_TEX_FORMAT_ARGB1555:
404 type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
406 case PVR2_TEX_FORMAT_RGB565:
409 type = GL_UNSIGNED_SHORT_5_6_5;
411 case PVR2_TEX_FORMAT_ARGB4444:
413 type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
415 case PVR2_TEX_FORMAT_YUV422:
416 /* YUV422 isn't directly supported by most implementations, so decode
417 * it to a (reasonably) standard ARGB32.
421 type = GL_UNSIGNED_BYTE;
423 case PVR2_TEX_FORMAT_BUMPMAP:
424 WARN( "Bumpmap not supported" );
428 if( PVR2_TEX_IS_STRIDE(mode) && tex_format != PVR2_TEX_FORMAT_IDX4 &&
429 tex_format != PVR2_TEX_FORMAT_IDX8 ) {
430 /* Stride textures cannot be mip-mapped, compressed, indexed or twiddled */
431 uint32_t stride = (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5;
432 unsigned char data[(width*height) << bpp_shift];
433 if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
434 unsigned char tmp[(width*height)<<1];
435 pvr2_vram64_read_stride( tmp, width<<1, texture_addr, stride<<1, height );
436 yuv_decode( (uint32_t *)data, (uint32_t *)tmp, width, height );
438 pvr2_vram64_read_stride( data, width<<bpp_shift, texture_addr, stride<<bpp_shift, height );
440 glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type, data );
441 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
442 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
446 if( PVR2_TEX_IS_COMPRESSED(mode) ) {
447 uint16_t tmp[VQ_CODEBOOK_SIZE];
448 pvr2_vram64_read( (unsigned char *)tmp, texture_addr, VQ_CODEBOOK_SIZE );
449 texture_addr += VQ_CODEBOOK_SIZE;
450 vq_get_codebook( &codebook, tmp );
453 int level=0, last_level = 0, mip_width = width, mip_height = height, src_bytes, dest_bytes;
454 if( PVR2_TEX_IS_MIPMAPPED(mode) ) {
455 uint32_t src_offset = 0;
456 filter = GL_LINEAR_MIPMAP_LINEAR;
457 mip_height = height = width;
458 while( (1<<last_level) < width ) {
460 src_offset += ((width>>last_level)*(width>>last_level));
465 if( PVR2_TEX_IS_COMPRESSED(mode) ) {
467 } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
469 } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
471 } else if( tex_format != PVR2_TEX_FORMAT_IDX8 ) {
472 src_offset <<= bpp_shift;
474 texture_addr += src_offset;
478 dest_bytes = (mip_width * mip_height) << bpp_shift;
479 src_bytes = dest_bytes; // Modes will change this (below)
481 for( level=0; level<= last_level; level++ ) {
482 unsigned char data[dest_bytes];
483 /* load data from image, detwiddling/uncompressing as required */
484 if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
485 src_bytes = (mip_width * mip_height);
486 int bank = (mode >> 25) &0x03;
487 uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<8);
488 unsigned char tmp[src_bytes];
489 pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width, mip_height );
490 if( bpp_shift == 2 ) {
491 decode_pal8_to_32( (uint32_t *)data, tmp, src_bytes, palette );
493 decode_pal8_to_16( (uint16_t *)data, tmp, src_bytes, palette );
495 } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
496 src_bytes = (mip_width * mip_height) >> 1;
497 int bank = (mode >>21 ) & 0x3F;
498 uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<4);
499 unsigned char tmp[src_bytes];
500 pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
501 if( bpp_shift == 2 ) {
502 decode_pal4_to_32( (uint32_t *)data, tmp, src_bytes, palette );
504 decode_pal4_to_16( (uint16_t *)data, tmp, src_bytes, palette );
506 } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
507 src_bytes = ((mip_width*mip_height)<<1);
508 unsigned char tmp[src_bytes];
509 if( PVR2_TEX_IS_TWIDDLED(mode) ) {
510 pvr2_vram64_read_twiddled_16( tmp, texture_addr, mip_width, mip_height );
512 pvr2_vram64_read( tmp, texture_addr, src_bytes );
514 yuv_decode( (uint32_t *)data, (uint32_t *)tmp, mip_width, mip_height );
515 } else if( PVR2_TEX_IS_COMPRESSED(mode) ) {
516 src_bytes = ((mip_width*mip_height) >> 2);
517 unsigned char tmp[src_bytes];
518 if( PVR2_TEX_IS_TWIDDLED(mode) ) {
519 pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width>>1, mip_height>>1 );
521 pvr2_vram64_read( tmp, texture_addr, src_bytes );
523 vq_decode( (uint16_t *)data, tmp, mip_width, mip_height, &codebook );
524 } else if( PVR2_TEX_IS_TWIDDLED(mode) ) {
525 pvr2_vram64_read_twiddled_16( data, texture_addr, mip_width, mip_height );
527 pvr2_vram64_read( data, texture_addr, src_bytes );
531 if( level == last_level && level != 0 ) { /* 1x1 stored within a 2x2 */
532 glTexImage2D( GL_TEXTURE_2D, level, intFormat, 1, 1, 0, format, type,
533 data + (3 << bpp_shift) );
535 glTexImage2D( GL_TEXTURE_2D, level, intFormat, mip_width, mip_height, 0, format, type,
537 if( mip_width > 2 ) {
543 texture_addr -= src_bytes;
547 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
548 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
551 static int texcache_find_texture_slot( uint32_t texture_word, int width, int height )
553 uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
554 uint32_t texture_page = texture_addr >> 12;
555 texcache_entry_index next;
556 texcache_entry_index idx = texcache_page_lookup[texture_page];
557 while( idx != EMPTY_ENTRY ) {
558 texcache_entry_t entry = &texcache_active_list[idx];
559 if( entry->texture_addr == texture_addr &&
560 entry->mode == texture_word &&
561 entry->width == width &&
562 entry->height == height ) {
563 entry->lru_count = texcache_ref_counter++;
571 static int texcache_alloc_texture_slot( uint32_t texture_word, int width, int height )
573 uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
574 uint32_t texture_page = texture_addr >> 12;
575 texcache_entry_index slot = 0;
577 if( texcache_free_ptr < MAX_TEXTURES ) {
578 slot = texcache_free_list[texcache_free_ptr++];
580 slot = texcache_evict_lru();
583 /* Construct new entry */
584 texcache_active_list[slot].texture_addr = texture_addr;
585 texcache_active_list[slot].width = width;
586 texcache_active_list[slot].height = height;
587 texcache_active_list[slot].mode = texture_word;
588 texcache_active_list[slot].lru_count = texcache_ref_counter++;
590 /* Add entry to the lookup table */
591 int next = texcache_page_lookup[texture_page];
594 fprintf( stderr, "Active list: " );
595 for( i=0; i<MAX_TEXTURES; i++ ) {
596 fprintf( stderr, "%d, ", texcache_active_list[i].next );
598 fprintf( stderr, "\n" );
599 assert( next != slot );
602 assert( next != slot );
603 texcache_active_list[slot].next = next;
604 texcache_page_lookup[texture_page] = slot;
609 * Return a texture ID for the texture specified at the supplied address
610 * and given parameters (the same sequence of bytes could in theory have
611 * multiple interpretations). We use the texture address as the primary
612 * index, but allow for multiple instances at each address. The texture
613 * will be bound to the GL_TEXTURE_2D target before being returned.
615 * If the texture has already been bound, return the ID to which it was
616 * bound. Otherwise obtain an unused texture ID and set it up appropriately.
618 GLuint texcache_get_texture( uint32_t texture_word, int width, int height )
620 int slot = texcache_find_texture_slot( texture_word, width, height );
623 /* Not found - check the free list */
624 slot = texcache_alloc_texture_slot( texture_word, width, height );
626 /* Construct the GL texture */
627 uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
628 glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
629 texcache_load_texture( texture_addr, width, height, texture_word );
632 return texcache_active_list[slot].texture_id;
635 render_buffer_t texcache_get_render_buffer( uint32_t texture_addr, int mode, int width, int height )
637 INFO( "Rendering to texture!" );
638 uint32_t texture_word = ((texture_addr >> 3) & 0x000FFFFF) | PVR2_TEX_UNTWIDDLED;
640 case COLFMT_BGRA1555: texture_word |= PVR2_TEX_FORMAT_ARGB1555; break;
641 case COLFMT_RGB565: texture_word |= PVR2_TEX_FORMAT_RGB565; break;
642 case COLFMT_BGRA4444: texture_word |= PVR2_TEX_FORMAT_ARGB4444; break;
644 WARN( "Rendering to non-texture colour format" );
646 if( is_npot_texture(width) )
647 texture_word |= PVR2_TEX_STRIDE;
650 int slot = texcache_find_texture_slot( texture_word, width, height );
652 slot = texcache_alloc_texture_slot( texture_word, width, height );
655 texcache_entry_t entry = &texcache_active_list[slot];
657 if( entry->buffer == NULL ) {
658 entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
659 } else if( entry->buffer->width != width || entry->buffer->height != height ) {
660 texcache_release_render_buffer(entry->buffer);
661 entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
664 return entry->buffer;
668 * Check the integrity of the texcache. Verifies that every cache slot
669 * appears exactly once on either the free list or one page list. For
670 * active slots, the texture address must also match the page it appears on.
673 void texcache_integrity_check()
676 int slot_found[MAX_TEXTURES];
678 memset( slot_found, 0, sizeof(slot_found) );
680 /* Check entries on the free list */
681 for( i= texcache_free_ptr; i< MAX_TEXTURES; i++ ) {
682 int slot = texcache_free_list[i];
683 assert( slot_found[slot] == 0 );
684 assert( texcache_active_list[slot].next == EMPTY_ENTRY );
685 slot_found[slot] = 1;
688 /* Check entries on the active lists */
689 for( i=0; i< PVR2_RAM_PAGES; i++ ) {
690 int slot = texcache_page_lookup[i];
691 while( slot != EMPTY_ENTRY ) {
692 assert( slot_found[slot] == 0 );
693 assert( (texcache_active_list[slot].texture_addr >> 12) == i );
694 slot_found[slot] = 2;
695 slot = texcache_active_list[slot].next;
699 /* Make sure we didn't miss any entries */
700 for( i=0; i<MAX_TEXTURES; i++ ) {
701 assert( slot_found[i] != 0 );
.