2 * $Id: texcache.c,v 1.7 2006-08-02 06:24:08 nkeynes Exp $
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.
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.
28 #define MAX_TEXTURES 64
34 * find entry by texture_addr
36 * move entry to tail of lru list
40 typedef signed short texcache_entry_index;
41 #define EMPTY_ENTRY 0xFF
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;
50 texcache_entry_index next;
54 static uint8_t texcache_page_lookup[PVR2_RAM_PAGES];
55 static uint32_t texcache_ref_counter;
56 static struct texcache_entry texcache_active_list[MAX_TEXTURES];
59 * Initialize the texture cache.
64 for( i=0; i<PVR2_RAM_PAGES; i++ ) {
65 texcache_page_lookup[i] = EMPTY_ENTRY;
67 for( i=0; i<MAX_TEXTURES; i++ ) {
68 texcache_free_list[i] = i;
70 texcache_free_ptr = 0;
71 texcache_ref_counter = 0;
75 * Setup the initial texture ids (must be called after the GL context is
78 void texcache_gl_init( )
81 GLuint texids[MAX_TEXTURES];
83 glGenTextures( MAX_TEXTURES, texids );
84 for( i=0; i<MAX_TEXTURES; i++ ) {
85 texcache_active_list[i].texture_id = texids[i];
90 * Flush all textures from the cache, returning them to the free list.
92 void texcache_flush( )
95 /* clear structures */
96 for( i=0; i<PVR2_RAM_PAGES; i++ ) {
97 texcache_page_lookup[i] = EMPTY_ENTRY;
99 for( i=0; i<MAX_TEXTURES; i++ ) {
100 texcache_free_list[i] = i;
102 texcache_free_ptr = 0;
103 texcache_ref_counter = 0;
107 * Flush all textures and delete. The cache will be non-functional until
108 * the next call to texcache_init(). This would typically be done if
109 * switching GL targets.
111 void texcache_shutdown( )
113 GLuint texids[MAX_TEXTURES];
117 for( i=0; i<MAX_TEXTURES; i++ ) {
118 texids[i] = texcache_active_list[i].texture_id;
120 glDeleteTextures( MAX_TEXTURES, texids );
124 * Evict all textures contained in the page identified by a texture address.
126 void texcache_invalidate_page( uint32_t texture_addr ) {
127 uint32_t texture_page = texture_addr >> 12;
128 texcache_entry_index idx = texcache_page_lookup[texture_page];
129 if( idx == EMPTY_ENTRY )
131 assert( texcache_free_ptr >= 0 );
133 texcache_entry_t entry = &texcache_active_list[idx];
136 texcache_free_list[texcache_free_ptr] = idx;
138 entry->next = EMPTY_ENTRY;
139 } while( idx != EMPTY_ENTRY );
140 texcache_page_lookup[texture_page] = EMPTY_ENTRY;
144 * Evict a single texture from the cache.
145 * @return the slot of the evicted texture.
147 static texcache_entry_index texcache_evict( void )
149 /* Full table scan - take over the entry with the lowest lru value */
150 texcache_entry_index slot = 0;
151 int lru_value = texcache_active_list[0].lru_count;
153 for( i=1; i<MAX_TEXTURES; i++ ) {
154 /* FIXME: account for rollover */
155 if( texcache_active_list[i].lru_count < lru_value ) {
157 lru_value = texcache_active_list[i].lru_count;
161 /* Remove the selected slot from the lookup table */
162 uint32_t evict_page = texcache_active_list[slot].texture_addr;
163 texcache_entry_index replace_next = texcache_active_list[slot].next;
164 texcache_active_list[slot].next = EMPTY_ENTRY; /* Just for safety */
165 if( texcache_page_lookup[evict_page] == slot ) {
166 texcache_page_lookup[evict_page] = replace_next;
168 texcache_entry_index idx = texcache_page_lookup[evict_page];
169 texcache_entry_index next;
171 next = texcache_active_list[idx].next;
173 texcache_active_list[idx].next = replace_next;
177 } while( next != EMPTY_ENTRY );
182 static void detwiddle_pal8_to_32(int x1, int y1, int size, int totsize,
183 char **in, uint32_t *out, uint32_t *pal) {
185 out[y1 * totsize + x1] = pal[**in];
189 detwiddle_pal8_to_32(x1, y1, ns, totsize, in, out, pal);
190 detwiddle_pal8_to_32(x1, y1+ns, ns, totsize, in, out, pal);
191 detwiddle_pal8_to_32(x1+ns, y1, ns, totsize, in, out, pal);
192 detwiddle_pal8_to_32(x1+ns, y1+ns, ns, totsize, in, out, pal);
196 static void detwiddle_pal8_to_16(int x1, int y1, int size, int totsize,
197 char **in, uint16_t *out, uint16_t *pal) {
199 out[y1 * totsize + x1] = pal[**in];
203 detwiddle_pal8_to_16(x1, y1, ns, totsize, in, out, pal);
204 detwiddle_pal8_to_16(x1, y1+ns, ns, totsize, in, out, pal);
205 detwiddle_pal8_to_16(x1+ns, y1, ns, totsize, in, out, pal);
206 detwiddle_pal8_to_16(x1+ns, y1+ns, ns, totsize, in, out, pal);
210 static void detwiddle_16_to_16(int x1, int y1, int size, int totsize,
211 uint16_t **in, uint16_t *out ) {
213 out[y1 * totsize + x1] = **in;
217 detwiddle_16_to_16(x1, y1, ns, totsize, in, out);
218 detwiddle_16_to_16(x1, y1+ns, ns, totsize, in, out);
219 detwiddle_16_to_16(x1+ns, y1, ns, totsize, in, out);
220 detwiddle_16_to_16(x1+ns, y1+ns, ns, totsize, in, out);
226 * Load texture data from the given address and parameters into the currently
227 * bound OpenGL texture.
229 static texcache_load_texture( uint32_t texture_addr, int width, int height,
231 uint32_t bytes = width * height;
233 GLint intFormat, format, type;
234 int tex_format = mode & PVR2_TEX_FORMAT_MASK;
236 if( tex_format == PVR2_TEX_FORMAT_IDX8 ||
237 tex_format == PVR2_TEX_FORMAT_IDX4 ) {
238 switch( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03 ) {
239 case 0: /* ARGB1555 */
240 intFormat = GL_RGB5_A1;
242 type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
247 type = GL_UNSIGNED_SHORT_5_6_5_REV;
250 intFormat = GL_RGBA4;
252 type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
255 intFormat = GL_RGBA8;
257 type = GL_UNSIGNED_INT_8_8_8_8_REV;
262 if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
263 unsigned char data[bytes<<shift];
264 int bank = (mode >> 25) &0x03;
265 char *palette = mmio_region_PVR2PAL.mem + (bank * (256 << shift));
270 pvr2_vram64_read( tmp, texture_addr, bytes );
271 detwiddle_pal8_to_32( 0, 0, width, width, &p,
272 (uint32_t *)data, (uint32_t *)palette );
276 pvr2_vram64_read( tmp, texture_addr, bytes );
277 detwiddle_pal8_to_16( 0, 0, width, width, &p,
278 (uint16_t *)data, (uint16_t *)palette );
280 glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type,
285 switch( tex_format ) {
286 case PVR2_TEX_FORMAT_ARGB1555:
288 intFormat = GL_RGB5_A1;
290 type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
292 case PVR2_TEX_FORMAT_RGB565:
296 type = GL_UNSIGNED_SHORT_5_6_5_REV;
298 case PVR2_TEX_FORMAT_ARGB4444:
300 intFormat = GL_RGBA4;
302 type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
304 case PVR2_TEX_FORMAT_YUV422:
305 ERROR( "YUV textures not supported" );
307 case PVR2_TEX_FORMAT_BUMPMAP:
308 ERROR( "Bumpmap not supported" );
310 case PVR2_TEX_FORMAT_IDX4:
313 intFormat = GL_INTENSITY4;
314 format = GL_COLOR_INDEX;
315 type = GL_UNSIGNED_BYTE;
318 case PVR2_TEX_FORMAT_IDX8:
319 intFormat = GL_INTENSITY8;
320 format = GL_COLOR_INDEX;
321 type = GL_UNSIGNED_BYTE;
327 /* load data from image, detwiddling/uncompressing as required */
328 if( PVR2_TEX_IS_COMPRESSED(mode) ) {
329 ERROR( "VQ Compression not supported" );
331 if( PVR2_TEX_IS_TWIDDLED(mode) ) {
333 uint16_t *p = (uint16_t *)tmp;
334 pvr2_vram64_read( tmp, texture_addr, bytes );
336 detwiddle_16_to_16( 0, 0, width, width, &p, (uint16_t *)&data );
338 pvr2_vram64_read( data, texture_addr, bytes );
343 glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type,
346 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
347 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
351 * Return a texture ID for the texture specified at the supplied address
352 * and given parameters (the same sequence of bytes could in theory have
353 * multiple interpretations). We use the texture address as the primary
354 * index, but allow for multiple instances at each address. The texture
355 * will be bound to the GL_TEXTURE_2D target before being returned.
357 * If the texture has already been bound, return the ID to which it was
358 * bound. Otherwise obtain an unused texture ID and set it up appropriately.
360 GLuint texcache_get_texture( uint32_t texture_addr, int width, int height,
363 uint32_t texture_page = texture_addr >> 12;
364 texcache_entry_index idx = texcache_page_lookup[texture_page];
365 while( idx != EMPTY_ENTRY ) {
366 texcache_entry_t entry = &texcache_active_list[idx];
367 if( entry->texture_addr == texture_addr &&
368 entry->mode == mode &&
369 entry->width == width &&
370 entry->height == height ) {
371 entry->lru_count = texcache_ref_counter++;
372 glBindTexture( GL_TEXTURE_2D, entry->texture_id );
373 return entry->texture_id;
378 /* Not found - check the free list */
381 if( texcache_free_ptr < MAX_TEXTURES ) {
382 slot = texcache_free_list[texcache_free_ptr++];
384 slot = texcache_evict();
387 /* Construct new entry */
388 texcache_active_list[slot].texture_addr = texture_addr;
389 texcache_active_list[slot].width = width;
390 texcache_active_list[slot].height = height;
391 texcache_active_list[slot].mode = mode;
392 texcache_active_list[slot].lru_count = texcache_ref_counter++;
394 /* Add entry to the lookup table */
395 texcache_active_list[slot].next = texcache_page_lookup[texture_page];
396 texcache_page_lookup[texture_page] = slot;
398 /* Construct the GL texture */
399 glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
400 texcache_load_texture( texture_addr, width, height, mode );
402 return texcache_active_list[slot].texture_id;
.