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"
25 #include "pvr2/glutil.h"
27 /** Specifies the maximum number of OpenGL
28 * textures we're willing to have open at a time. If more are
29 * needed, textures will be evicted in LRU order.
31 #define MAX_TEXTURES 256
37 * find entry by texture_addr
39 * move entry to tail of lru list
43 typedef signed short texcache_entry_index;
44 #define EMPTY_ENTRY -1
46 static texcache_entry_index texcache_free_ptr = 0;
47 static GLuint texcache_free_list[MAX_TEXTURES];
49 typedef struct texcache_entry {
50 uint32_t texture_addr;
51 uint32_t poly2_mode, tex_mode;
53 render_buffer_t buffer;
54 texcache_entry_index next;
58 static texcache_entry_index texcache_page_lookup[PVR2_RAM_PAGES];
59 static uint32_t texcache_ref_counter;
60 static struct texcache_entry texcache_active_list[MAX_TEXTURES];
61 static uint32_t texcache_palette_mode;
62 static uint32_t texcache_stride_width;
63 static gboolean texcache_have_palette_shader;
64 static gboolean texcache_palette_valid;
65 static GLuint texcache_palette_texid;
68 * Initialize the texture cache.
73 for( i=0; i<PVR2_RAM_PAGES; i++ ) {
74 texcache_page_lookup[i] = EMPTY_ENTRY;
76 for( i=0; i<MAX_TEXTURES; i++ ) {
77 texcache_free_list[i] = i;
78 texcache_active_list[i].texture_addr = -1;
79 texcache_active_list[i].buffer = NULL;
80 texcache_active_list[i].next = EMPTY_ENTRY;
82 texcache_free_ptr = 0;
83 texcache_ref_counter = 0;
84 texcache_palette_mode = -1;
85 texcache_stride_width = 0;
89 * Setup the initial texture ids (must be called after the GL context is
92 void texcache_gl_init( )
95 GLuint texids[MAX_TEXTURES];
97 if( glsl_is_supported() && isGLMultitextureSupported() ) {
98 texcache_have_palette_shader = TRUE;
99 texcache_palette_valid = FALSE;
100 glGenTextures(1, &texcache_palette_texid );
102 /* Bind the texture and set the params */
103 glActiveTexture(GL_TEXTURE1);
104 glBindTexture(GL_TEXTURE_1D, texcache_palette_texid);
105 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
106 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
107 glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP );
108 glActiveTexture(GL_TEXTURE0);
111 texcache_have_palette_shader = FALSE;
114 glGenTextures( MAX_TEXTURES, texids );
115 for( i=0; i<MAX_TEXTURES; i++ ) {
116 texcache_active_list[i].texture_id = texids[i];
120 void texcache_release_render_buffer( render_buffer_t buffer )
122 if( !buffer->flushed )
123 pvr2_render_buffer_copy_to_sh4(buffer);
124 pvr2_destroy_render_buffer(buffer);
128 * Flush all textures from the cache, returning them to the free list.
130 void texcache_flush( )
133 /* clear structures */
134 for( i=0; i<PVR2_RAM_PAGES; i++ ) {
135 texcache_page_lookup[i] = EMPTY_ENTRY;
137 for( i=0; i<MAX_TEXTURES; i++ ) {
138 texcache_free_list[i] = i;
139 texcache_active_list[i].next = EMPTY_ENTRY;
140 texcache_active_list[i].texture_addr = -1;
141 if( texcache_active_list[i].buffer != NULL ) {
142 texcache_release_render_buffer(texcache_active_list[i].buffer);
143 texcache_active_list[i].buffer = NULL;
146 texcache_free_ptr = 0;
147 texcache_ref_counter = 0;
151 * Flush all textures and delete. The cache will be non-functional until
152 * the next call to texcache_init(). This would typically be done if
153 * switching GL targets.
155 void texcache_shutdown( )
157 GLuint texids[MAX_TEXTURES];
161 if( texcache_have_palette_shader )
162 glDeleteTextures( 1, &texcache_palette_texid );
164 for( i=0; i<MAX_TEXTURES; i++ ) {
165 texids[i] = texcache_active_list[i].texture_id;
167 glDeleteTextures( MAX_TEXTURES, texids );
170 static void texcache_evict( int slot )
172 /* Remove the selected slot from the lookup table */
173 assert( texcache_active_list[slot].texture_addr != -1 );
174 uint32_t evict_page = texcache_active_list[slot].texture_addr >> 12;
175 texcache_entry_index replace_next = texcache_active_list[slot].next;
176 texcache_active_list[slot].texture_addr = -1;
177 texcache_active_list[slot].next = EMPTY_ENTRY; /* Just for safety */
178 if( texcache_active_list[slot].buffer != NULL ) {
179 texcache_release_render_buffer(texcache_active_list[slot].buffer);
180 texcache_active_list[slot].buffer = NULL;
182 if( texcache_page_lookup[evict_page] == slot ) {
183 texcache_page_lookup[evict_page] = replace_next;
185 texcache_entry_index idx = texcache_page_lookup[evict_page];
186 texcache_entry_index next;
188 next = texcache_active_list[idx].next;
190 assert( idx != replace_next );
191 texcache_active_list[idx].next = replace_next;
195 } while( next != EMPTY_ENTRY );
200 * Evict a single texture from the cache.
201 * @return the slot of the evicted texture.
203 static texcache_entry_index texcache_evict_lru( void )
205 /* Full table scan - take over the entry with the lowest lru value */
206 texcache_entry_index slot = 0;
207 int lru_value = texcache_active_list[0].lru_count;
209 for( i=1; i<MAX_TEXTURES; i++ ) {
210 /* FIXME: account for rollover */
211 if( texcache_active_list[i].lru_count < lru_value ) {
213 lru_value = texcache_active_list[i].lru_count;
216 texcache_evict(slot);
222 * Evict all textures contained in the page identified by a texture address.
224 void texcache_invalidate_page( uint32_t texture_addr ) {
225 uint32_t texture_page = texture_addr >> 12;
226 texcache_entry_index idx = texcache_page_lookup[texture_page];
227 if( idx == EMPTY_ENTRY )
229 assert( texcache_free_ptr >= 0 );
231 texcache_entry_t entry = &texcache_active_list[idx];
232 entry->texture_addr = -1;
233 if( entry->buffer != NULL ) {
234 texcache_release_render_buffer(entry->buffer);
235 entry->buffer = NULL;
239 texcache_free_list[texcache_free_ptr] = idx;
241 entry->next = EMPTY_ENTRY;
242 } while( idx != EMPTY_ENTRY );
243 texcache_page_lookup[texture_page] = EMPTY_ENTRY;
247 * Load the palette into 4 textures of 256 entries each. This mirrors the
248 * banking done by the PVR2 for 8-bit textures, and also ensures that we
249 * can use 8-bit paletted textures ourselves.
251 static void texcache_load_palette_texture( gboolean format_changed )
253 GLint format, type, intFormat = GL_RGBA;
256 uint32_t *palette = (uint32_t *)mmio_region_PVR2PAL.mem;
257 uint16_t packed_palette[1024];
258 char *data = (char *)palette;
260 switch( texcache_palette_mode ) {
261 case 0: /* ARGB1555 */
263 type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
268 type = GL_UNSIGNED_SHORT_5_6_5;
270 case 2: /* ARGB4444 */
272 type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
274 case 3: /* ARGB8888 */
276 type = GL_UNSIGNED_BYTE;
280 break; /* Can't happen */
285 for( i=0; i<1024; i++ ) {
286 packed_palette[i] = (uint16_t)palette[i];
288 data = (char *)packed_palette;
292 glActiveTexture(GL_TEXTURE1);
293 // glBindTexture(GL_TEXTURE_1D, texcache_palette_texid);
295 glTexImage1D(GL_TEXTURE_1D, 0, intFormat, 1024, 0, format, type, data );
297 glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 1024, format, type, data);
298 // glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
299 // glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
300 // glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP );
301 glActiveTexture(GL_TEXTURE0);
302 texcache_palette_valid = TRUE;
307 * Mark the palette as having changed. If we have palette support (via shaders)
308 * we just flag the palette, otherwise we have to invalidate all palette
311 void texcache_invalidate_palette( )
313 if( texcache_have_palette_shader ) {
314 texcache_palette_valid = FALSE;
317 for( i=0; i<MAX_TEXTURES; i++ ) {
318 if( texcache_active_list[i].texture_addr != -1 &&
319 PVR2_TEX_IS_PALETTE(texcache_active_list[i].tex_mode) ) {
322 texcache_free_list[texcache_free_ptr] = i;
328 * Mark all stride textures as needing a re-read (ie when the stride width
331 void texcache_invalidate_stride( )
334 for( i=0; i<MAX_TEXTURES; i++ ) {
335 if( texcache_active_list[i].texture_addr != -1 &&
336 PVR2_TEX_IS_STRIDE(texcache_active_list[i].tex_mode) ) {
339 texcache_free_list[texcache_free_ptr] = i;
344 void texcache_begin_scene( uint32_t palette_mode, uint32_t stride )
346 gboolean format_changed = FALSE;
347 if( palette_mode != texcache_palette_mode ) {
348 texcache_invalidate_palette();
349 format_changed = TRUE;
351 if( stride != texcache_stride_width )
352 texcache_invalidate_stride();
354 texcache_palette_mode = palette_mode;
355 texcache_stride_width = stride;
357 if( !texcache_palette_valid && texcache_have_palette_shader )
358 texcache_load_palette_texture(format_changed);
361 static void decode_pal8_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
364 for( i=0; i<inbytes; i++ ) {
369 static void decode_pal8_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
372 for( i=0; i<inbytes; i++ ) {
373 *out++ = (uint16_t)pal[*in++];
377 static void decode_pal4_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
380 for( i=0; i<inbytes; i++ ) {
381 *out++ = pal[*in & 0x0F];
382 *out++ = pal[(*in >> 4)];
387 static void decode_pal4_to_pal8( uint8_t *out, uint8_t *in, int inbytes )
390 for( i=0; i<inbytes; i++ ) {
391 *out++ = (uint8_t)(*in & 0x0F);
392 *out++ = (uint8_t)(*in >> 4);
399 static void decode_pal4_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
402 for( i=0; i<inbytes; i++ ) {
403 *out++ = (uint16_t)pal[*in & 0x0F];
404 *out++ = (uint16_t)pal[(*in >> 4)];
409 #define VQ_CODEBOOK_SIZE 2048 /* 256 entries * 4 pixels per quad * 2 byte pixels */
412 uint16_t quad[256][4];
415 static void vq_get_codebook( struct vq_codebook *codebook,
418 /* Detwiddle the codebook, for the sake of my own sanity if nothing else */
419 uint16_t *p = (uint16_t *)input;
421 for( i=0; i<256; i++ ) {
422 codebook->quad[i][0] = *p++;
423 codebook->quad[i][2] = *p++;
424 codebook->quad[i][1] = *p++;
425 codebook->quad[i][3] = *p++;
429 static void vq_decode( uint16_t *output, unsigned char *input, int width, int height,
430 struct vq_codebook *codebook ) {
433 uint8_t *c = (uint8_t *)input;
434 for( j=0; j<height; j+=2 ) {
435 for( i=0; i<width; i+=2 ) {
437 output[i + j*width] = codebook->quad[code][0];
438 output[i + 1 + j*width] = codebook->quad[code][1];
439 output[i + (j+1)*width] = codebook->quad[code][2];
440 output[i + 1 + (j+1)*width] = codebook->quad[code][3];
445 static inline uint32_t yuv_to_rgb32( float y, float u, float v )
449 int r = (int)(y + v*1.375);
450 int g = (int)(y - u*0.34375 - v*0.6875);
451 int b = (int)(y + u*1.71875);
452 if( r > 255 ) { r = 255; } else if( r < 0 ) { r = 0; }
453 if( g > 255 ) { g = 255; } else if( g < 0 ) { g = 0; }
454 if( b > 255 ) { b = 255; } else if( b < 0 ) { b = 0; }
455 return 0xFF000000 | (r<<16) | (g<<8) | (b);
460 * Convert raster YUV texture data into RGB32 data - most GL implementations don't
461 * directly support this format unfortunately. The input data is formatted as
462 * 32 bits = 2 horizontal pixels, UYVY. This is currently done rather inefficiently
465 static void yuv_decode( uint32_t *output, uint32_t *input, int width, int height )
469 for( y=0; y<height; y++ ) {
470 for( x=0; x<width; x+=2 ) {
471 float u = (float)(*p & 0xFF);
472 float y0 = (float)( (*p>>8)&0xFF );
473 float v = (float)( (*p>>16)&0xFF );
474 float y1 = (float)( (*p>>24)&0xFF );
475 *output++ = yuv_to_rgb32( y0, u, v );
476 *output++ = yuv_to_rgb32( y1, u, v );
482 static gboolean is_npot_texture( int width )
484 while( width != 0 ) {
493 * Load texture data from the given address and parameters into the currently
494 * bound OpenGL texture.
496 static void texcache_load_texture( uint32_t texture_addr, int width, int height,
498 int bpp_shift = 1; /* bytes per (output) pixel as a power of 2 */
499 GLint intFormat = GL_RGBA, format, type;
500 int tex_format = mode & PVR2_TEX_FORMAT_MASK;
501 struct vq_codebook codebook;
502 GLint min_filter = GL_LINEAR;
503 GLint max_filter = GL_LINEAR;
504 GLint mipmapfilter = GL_LINEAR_MIPMAP_LINEAR;
506 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
508 /* Decode the format parameters */
509 switch( tex_format ) {
510 case PVR2_TEX_FORMAT_IDX4:
511 case PVR2_TEX_FORMAT_IDX8:
512 if( texcache_have_palette_shader ) {
513 intFormat = GL_ALPHA8;
515 type = GL_UNSIGNED_BYTE;
517 min_filter = max_filter = GL_NEAREST;
518 mipmapfilter = GL_NEAREST_MIPMAP_NEAREST;
520 /* For indexed-colour modes, we need to lookup the palette control
521 * word to determine the de-indexed texture format.
523 switch( texcache_palette_mode ) {
524 case 0: /* ARGB1555 */
526 type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
531 type = GL_UNSIGNED_SHORT_5_6_5;
533 case 2: /* ARGB4444 */
535 type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
537 case 3: /* ARGB8888 */
539 type = GL_UNSIGNED_BYTE;
543 return; /* Can't happen, but it makes gcc stop complaining */
549 case PVR2_TEX_FORMAT_ARGB1555:
551 type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
553 case PVR2_TEX_FORMAT_RGB565:
556 type = GL_UNSIGNED_SHORT_5_6_5;
558 case PVR2_TEX_FORMAT_ARGB4444:
560 type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
562 case PVR2_TEX_FORMAT_YUV422:
563 /* YUV422 isn't directly supported by most implementations, so decode
564 * it to a (reasonably) standard ARGB32.
568 type = GL_UNSIGNED_BYTE;
570 case PVR2_TEX_FORMAT_BUMPMAP:
571 WARN( "Bumpmap not supported" );
575 if( PVR2_TEX_IS_STRIDE(mode) && tex_format != PVR2_TEX_FORMAT_IDX4 &&
576 tex_format != PVR2_TEX_FORMAT_IDX8 ) {
577 /* Stride textures cannot be mip-mapped, compressed, indexed or twiddled */
578 unsigned char data[(width*height) << bpp_shift];
579 if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
580 unsigned char tmp[(width*height)<<1];
581 pvr2_vram64_read_stride( tmp, width<<1, texture_addr, texcache_stride_width<<1, height );
582 yuv_decode( (uint32_t *)data, (uint32_t *)tmp, width, height );
584 pvr2_vram64_read_stride( data, width<<bpp_shift, texture_addr, texcache_stride_width<<bpp_shift, height );
586 glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type, data );
587 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
588 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter);
592 if( PVR2_TEX_IS_COMPRESSED(mode) ) {
593 uint16_t tmp[VQ_CODEBOOK_SIZE];
594 pvr2_vram64_read( (unsigned char *)tmp, texture_addr, VQ_CODEBOOK_SIZE );
595 texture_addr += VQ_CODEBOOK_SIZE;
596 vq_get_codebook( &codebook, tmp );
599 int level=0, last_level = 0, mip_width = width, mip_height = height, src_bytes, dest_bytes;
600 if( PVR2_TEX_IS_MIPMAPPED(mode) ) {
601 uint32_t src_offset = 0;
602 min_filter = mipmapfilter;
603 mip_height = height = width;
604 while( (1<<last_level) < width ) {
606 src_offset += ((width>>last_level)*(width>>last_level));
611 if( PVR2_TEX_IS_COMPRESSED(mode) ) {
613 } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
615 } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
617 } else if( tex_format != PVR2_TEX_FORMAT_IDX8 ) {
618 src_offset <<= bpp_shift;
620 texture_addr += src_offset;
624 dest_bytes = (mip_width * mip_height) << bpp_shift;
625 src_bytes = dest_bytes; // Modes will change this (below)
627 for( level=0; level<= last_level; level++ ) {
628 unsigned char data[dest_bytes];
629 /* load data from image, detwiddling/uncompressing as required */
630 if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
631 if( texcache_have_palette_shader ) {
632 pvr2_vram64_read_twiddled_8( data, texture_addr, mip_width, mip_height );
634 src_bytes = (mip_width * mip_height);
635 int bank = (mode >> 25) &0x03;
636 uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<8);
637 unsigned char tmp[src_bytes];
638 pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width, mip_height );
639 if( bpp_shift == 2 ) {
640 decode_pal8_to_32( (uint32_t *)data, tmp, src_bytes, palette );
642 decode_pal8_to_16( (uint16_t *)data, tmp, src_bytes, palette );
645 } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
646 src_bytes = (mip_width * mip_height) >> 1;
647 unsigned char tmp[src_bytes];
648 if( texcache_have_palette_shader ) {
649 pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
650 decode_pal4_to_pal8( data, tmp, src_bytes );
652 int bank = (mode >>21 ) & 0x3F;
653 uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<4);
654 pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
655 if( bpp_shift == 2 ) {
656 decode_pal4_to_32( (uint32_t *)data, tmp, src_bytes, palette );
658 decode_pal4_to_16( (uint16_t *)data, tmp, src_bytes, palette );
661 } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
662 src_bytes = ((mip_width*mip_height)<<1);
663 unsigned char tmp[src_bytes];
664 if( PVR2_TEX_IS_TWIDDLED(mode) ) {
665 pvr2_vram64_read_twiddled_16( tmp, texture_addr, mip_width, mip_height );
667 pvr2_vram64_read( tmp, texture_addr, src_bytes );
669 yuv_decode( (uint32_t *)data, (uint32_t *)tmp, mip_width, mip_height );
670 } else if( PVR2_TEX_IS_COMPRESSED(mode) ) {
671 src_bytes = ((mip_width*mip_height) >> 2);
672 unsigned char tmp[src_bytes];
673 if( PVR2_TEX_IS_TWIDDLED(mode) ) {
674 pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width>>1, mip_height>>1 );
676 pvr2_vram64_read( tmp, texture_addr, src_bytes );
678 vq_decode( (uint16_t *)data, tmp, mip_width, mip_height, &codebook );
679 } else if( PVR2_TEX_IS_TWIDDLED(mode) ) {
680 pvr2_vram64_read_twiddled_16( data, texture_addr, mip_width, mip_height );
682 pvr2_vram64_read( data, texture_addr, src_bytes );
686 if( level == last_level && level != 0 ) { /* 1x1 stored within a 2x2 */
687 glTexImage2D( GL_TEXTURE_2D, level, intFormat, 1, 1, 0, format, type,
688 data + (3 << bpp_shift) );
690 glTexImage2D( GL_TEXTURE_2D, level, intFormat, mip_width, mip_height, 0, format, type,
692 if( mip_width > 2 ) {
698 texture_addr -= src_bytes;
702 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
703 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter);
706 static int texcache_find_texture_slot( uint32_t poly2_masked_word, uint32_t texture_word )
708 uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
709 uint32_t texture_page = texture_addr >> 12;
710 texcache_entry_index next;
711 texcache_entry_index idx = texcache_page_lookup[texture_page];
712 while( idx != EMPTY_ENTRY ) {
713 texcache_entry_t entry = &texcache_active_list[idx];
714 if( entry->tex_mode == texture_word &&
715 entry->poly2_mode == poly2_masked_word ) {
716 entry->lru_count = texcache_ref_counter++;
724 static int texcache_alloc_texture_slot( uint32_t poly2_word, uint32_t texture_word )
726 uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
727 uint32_t texture_page = texture_addr >> 12;
728 texcache_entry_index slot = 0;
730 if( texcache_free_ptr < MAX_TEXTURES ) {
731 slot = texcache_free_list[texcache_free_ptr++];
733 slot = texcache_evict_lru();
736 /* Construct new entry */
737 assert( texcache_active_list[slot].texture_addr == -1 );
738 texcache_active_list[slot].texture_addr = texture_addr;
739 texcache_active_list[slot].tex_mode = texture_word;
740 texcache_active_list[slot].poly2_mode = poly2_word;
741 texcache_active_list[slot].lru_count = texcache_ref_counter++;
743 /* Add entry to the lookup table */
744 int next = texcache_page_lookup[texture_page];
747 fprintf( stderr, "Active list: " );
748 for( i=0; i<MAX_TEXTURES; i++ ) {
749 fprintf( stderr, "%d, ", texcache_active_list[i].next );
751 fprintf( stderr, "\n" );
752 assert( next != slot );
755 texcache_active_list[slot].next = next;
756 texcache_page_lookup[texture_page] = slot;
761 * Return a texture ID for the texture specified at the supplied address
762 * and given parameters (the same sequence of bytes could in theory have
763 * multiple interpretations). We use the texture address as the primary
764 * index, but allow for multiple instances at each address.
766 * If the texture has already been bound, return the ID to which it was
767 * bound. Otherwise obtain an unused texture ID and set it up appropriately.
768 * The current GL_TEXTURE_2D binding will be changed in this case.
770 GLuint texcache_get_texture( uint32_t poly2_word, uint32_t texture_word )
772 poly2_word &= 0x000F803F; /* Get just the texture-relevant bits */
773 uint32_t texture_lookup = texture_word;
774 if( PVR2_TEX_IS_PALETTE(texture_lookup) ) {
775 texture_lookup &= 0xF81FFFFF; /* Mask out the bank bits */
777 int slot = texcache_find_texture_slot( poly2_word, texture_lookup );
780 /* Not found - check the free list */
781 slot = texcache_alloc_texture_slot( poly2_word, texture_lookup );
783 /* Construct the GL texture */
784 uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
785 unsigned width = POLY2_TEX_WIDTH(poly2_word);
786 unsigned height = POLY2_TEX_HEIGHT(poly2_word);
788 glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
789 texcache_load_texture( texture_addr, width, height, texture_word );
791 /* Set texture parameters from the poly2 word */
792 if( POLY2_TEX_CLAMP_U(poly2_word) ) {
793 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
794 } else if( POLY2_TEX_MIRROR_U(poly2_word) ) {
795 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB );
797 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
799 if( POLY2_TEX_CLAMP_V(poly2_word) ) {
800 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
801 } else if( POLY2_TEX_MIRROR_V(poly2_word) ) {
802 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB );
804 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
808 return texcache_active_list[slot].texture_id;
812 render_buffer_t texcache_get_render_buffer( uint32_t texture_addr, int mode, int width, int height )
814 uint32_t texture_word = ((texture_addr >> 3) & 0x000FFFFF) | PVR2_TEX_UNTWIDDLED;
816 case COLFMT_BGRA1555: texture_word |= PVR2_TEX_FORMAT_ARGB1555; break;
817 case COLFMT_RGB565: texture_word |= PVR2_TEX_FORMAT_RGB565; break;
818 case COLFMT_BGRA4444: texture_word |= PVR2_TEX_FORMAT_ARGB4444; break;
820 WARN( "Rendering to non-texture colour format" );
822 if( is_npot_texture(width) )
823 texture_word |= PVR2_TEX_STRIDE;
826 int slot = texcache_find_texture_slot( texture_word, width, height );
828 slot = texcache_alloc_texture_slot( texture_word, width, height );
831 texcache_entry_t entry = &texcache_active_list[slot];
833 if( entry->buffer == NULL ) {
834 entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
835 } else if( entry->buffer->width != width || entry->buffer->height != height ) {
836 texcache_release_render_buffer(entry->buffer);
837 entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
840 return entry->buffer;
845 * Check the integrity of the texcache. Verifies that every cache slot
846 * appears exactly once on either the free list or one page list. For
847 * active slots, the texture address must also match the page it appears on.
850 void texcache_integrity_check()
853 int slot_found[MAX_TEXTURES];
855 memset( slot_found, 0, sizeof(slot_found) );
857 /* Check entries on the free list */
858 for( i= texcache_free_ptr; i< MAX_TEXTURES; i++ ) {
859 int slot = texcache_free_list[i];
860 assert( slot_found[slot] == 0 );
861 assert( texcache_active_list[slot].next == EMPTY_ENTRY );
862 slot_found[slot] = 1;
865 /* Check entries on the active lists */
866 for( i=0; i< PVR2_RAM_PAGES; i++ ) {
867 int slot = texcache_page_lookup[i];
868 while( slot != EMPTY_ENTRY ) {
869 assert( slot_found[slot] == 0 );
870 assert( (texcache_active_list[slot].texture_addr >> 12) == i );
871 slot_found[slot] = 2;
872 slot = texcache_active_list[slot].next;
876 /* Make sure we didn't miss any entries */
877 for( i=0; i<MAX_TEXTURES; i++ ) {
878 assert( slot_found[i] != 0 );
883 * Dump the contents of the texture cache
888 for( i=0; i< PVR2_RAM_PAGES; i++ ) {
889 int slot = texcache_page_lookup[i];
890 while( slot != EMPTY_ENTRY ) {
891 fprintf( stderr, "%-3d: %08X %dx%d (%08X %08X)\n", slot,
892 texcache_active_list[slot].texture_addr,
893 POLY2_TEX_WIDTH(texcache_active_list[slot].poly2_mode),
894 POLY2_TEX_HEIGHT(texcache_active_list[slot].poly2_mode),
895 texcache_active_list[slot].poly2_mode,
896 texcache_active_list[slot].tex_mode );
897 slot = texcache_active_list[slot].next;
902 void texcache_print_idx4( uint32_t texture_addr, int width )
905 int src_bytes = (width*width>>1);
907 char data[width*width];
908 pvr2_vram64_read_twiddled_4( tmp, texture_addr, width, width );
909 decode_pal4_to_pal8( data, tmp, src_bytes );
910 for( y=0; y<width; y++ ) {
911 for( x=0; x<width; x++ ) {
912 printf( "%1x", data[y*width+x] );
.