filename | src/pvr2/texcache.c |
changeset | 270:1e151174ef51 |
prev | 224:289ddaeeebb3 |
next | 282:01e53698ff38 |
author | nkeynes |
date | Thu Jan 11 06:51:11 2007 +0000 (16 years ago) |
permissions | -rw-r--r-- |
last change | Fix crash in texcache_evict Fix crash decoding small VQ textures Implement mip-mapped textures of all types |
view | annotate | diff | log | raw |
1 /**
2 * $Id: texcache.c,v 1.9 2007-01-11 06:51:11 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 64
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 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;
49 GLuint texture_id;
50 texcache_entry_index next;
51 uint32_t lru_count;
52 } *texcache_entry_t;
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];
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 }
70 texcache_free_ptr = 0;
71 texcache_ref_counter = 0;
72 }
74 /**
75 * Setup the initial texture ids (must be called after the GL context is
76 * prepared)
77 */
78 void texcache_gl_init( )
79 {
80 int i;
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];
86 }
87 }
89 /**
90 * Flush all textures from the cache, returning them to the free list.
91 */
92 void texcache_flush( )
93 {
94 int i;
95 /* clear structures */
96 for( i=0; i<PVR2_RAM_PAGES; i++ ) {
97 texcache_page_lookup[i] = EMPTY_ENTRY;
98 }
99 for( i=0; i<MAX_TEXTURES; i++ ) {
100 texcache_free_list[i] = i;
101 }
102 texcache_free_ptr = 0;
103 texcache_ref_counter = 0;
104 }
106 /**
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.
110 */
111 void texcache_shutdown( )
112 {
113 GLuint texids[MAX_TEXTURES];
114 int i;
115 texcache_flush();
117 for( i=0; i<MAX_TEXTURES; i++ ) {
118 texids[i] = texcache_active_list[i].texture_id;
119 }
120 glDeleteTextures( MAX_TEXTURES, texids );
121 }
123 /**
124 * Evict all textures contained in the page identified by a texture address.
125 */
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 )
130 return;
131 assert( texcache_free_ptr >= 0 );
132 do {
133 texcache_entry_t entry = &texcache_active_list[idx];
134 /* release entry */
135 texcache_free_ptr--;
136 texcache_free_list[texcache_free_ptr] = idx;
137 idx = entry->next;
138 entry->next = EMPTY_ENTRY;
139 } while( idx != EMPTY_ENTRY );
140 texcache_page_lookup[texture_page] = EMPTY_ENTRY;
141 }
143 /**
144 * Evict a single texture from the cache.
145 * @return the slot of the evicted texture.
146 */
147 static texcache_entry_index texcache_evict( void )
148 {
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;
152 int i;
153 for( i=1; i<MAX_TEXTURES; i++ ) {
154 /* FIXME: account for rollover */
155 if( texcache_active_list[i].lru_count < lru_value ) {
156 slot = i;
157 lru_value = texcache_active_list[i].lru_count;
158 }
159 }
161 /* Remove the selected slot from the lookup table */
162 uint32_t evict_page = texcache_active_list[slot].texture_addr >> 12;
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;
167 } else {
168 texcache_entry_index idx = texcache_page_lookup[evict_page];
169 texcache_entry_index next;
170 do {
171 next = texcache_active_list[idx].next;
172 if( next == slot ) {
173 texcache_active_list[idx].next = replace_next;
174 break;
175 }
176 idx = next;
177 } while( next != EMPTY_ENTRY );
178 }
179 return slot;
180 }
182 static void detwiddle_pal8_to_32(int x1, int y1, int size, int totsize,
183 char **in, uint32_t *out, uint32_t *pal) {
184 if (size == 1) {
185 out[y1 * totsize + x1] = pal[**in];
186 (*in)++;
187 } else {
188 int ns = size>>1;
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);
193 }
194 }
196 static void detwiddle_pal8_to_16(int x1, int y1, int size, int totsize,
197 char **in, uint16_t *out, uint16_t *pal) {
198 if (size == 1) {
199 out[y1 * totsize + x1] = pal[**in];
200 (*in)++;
201 } else {
202 int ns = size>>1;
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);
207 }
208 }
210 static void detwiddle_16_to_16(int x1, int y1, int size, int totsize,
211 uint16_t **in, uint16_t *out ) {
212 if (size == 1) {
213 out[y1 * totsize + x1] = **in;
214 (*in)++;
215 } else {
216 int ns = size>>1;
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);
221 }
222 }
224 #define VQ_CODEBOOK_SIZE 2048 /* 256 entries * 4 pixels per quad * 2 byte pixels */
226 struct vq_codebook {
227 uint16_t quad[256][4];
228 };
230 static void detwiddle_vq_to_16(int x1, int y1, int size, int totsize,
231 uint8_t **in, uint16_t *out, struct vq_codebook *codebook ) {
232 if( size == 2 ) {
233 uint8_t code = **in;
234 (*in)++;
235 out[y1 * totsize + x1] = codebook->quad[code][0];
236 out[y1 * totsize + x1 + 1] = codebook->quad[code][1];
237 out[(y1+1) * totsize + x1] = codebook->quad[code][2];
238 out[(y1+1) * totsize + x1 + 1] = codebook->quad[code][3];
239 } else {
240 int ns = size>>1;
241 detwiddle_vq_to_16(x1, y1, ns, totsize, in, out, codebook);
242 detwiddle_vq_to_16(x1, y1+ns, ns, totsize, in, out, codebook);
243 detwiddle_vq_to_16(x1+ns, y1, ns, totsize, in, out, codebook);
244 detwiddle_vq_to_16(x1+ns, y1+ns, ns, totsize, in, out, codebook);
245 }
246 }
248 static void vq_get_codebook( struct vq_codebook *codebook,
249 uint16_t *input )
250 {
251 /* Detwiddle the codebook, for the sake of my own sanity if nothing else */
252 uint16_t *p = (uint16_t *)input;
253 int i;
254 for( i=0; i<256; i++ ) {
255 codebook->quad[i][0] = *p++;
256 codebook->quad[i][2] = *p++;
257 codebook->quad[i][1] = *p++;
258 codebook->quad[i][3] = *p++;
259 }
260 }
263 static void vq_decode( int width, int height, char *input, uint16_t *output,
264 struct vq_codebook *codebook, int twiddled ) {
265 int i,j;
267 uint8_t *c = (uint8_t *)input;
268 if( twiddled ) {
269 detwiddle_vq_to_16( 0, 0, width, width, &c, output, codebook );
270 } else {
271 for( j=0; j<height; j+=2 ) {
272 for( i=0; i<width; i+=2 ) {
273 uint8_t code = *c;
274 output[i + j*width] = codebook->quad[code][0];
275 output[i + 1 + j*width] = codebook->quad[code][1];
276 output[i + (j+1)*width] = codebook->quad[code][2];
277 output[i + 1 + (j+1)*width] = codebook->quad[code][3];
278 }
279 }
280 }
281 }
283 /**
284 * Load texture data from the given address and parameters into the currently
285 * bound OpenGL texture.
286 */
287 static texcache_load_texture( uint32_t texture_addr, int width, int height,
288 int mode ) {
289 uint32_t bytes = width * height;
290 int shift = 1;
291 GLint intFormat, format, type;
292 int tex_format = mode & PVR2_TEX_FORMAT_MASK;
293 struct vq_codebook codebook;
294 GLint filter = GL_LINEAR;
296 /* Decode the format parameters */
297 switch( tex_format ) {
298 case PVR2_TEX_FORMAT_IDX4:
299 ERROR( "4-bit indexed textures not supported" );
300 case PVR2_TEX_FORMAT_IDX8:
301 switch( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03 ) {
302 case 0: /* ARGB1555 */
303 intFormat = GL_RGB5_A1;
304 format = GL_RGBA;
305 type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
306 break;
307 case 1:
308 intFormat = GL_RGB;
309 format = GL_RGB;
310 type = GL_UNSIGNED_SHORT_5_6_5_REV;
311 break;
312 case 2:
313 intFormat = GL_RGBA4;
314 format = GL_BGRA;
315 type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
316 break;
317 case 3:
318 intFormat = GL_RGBA8;
319 format = GL_BGRA;
320 type = GL_UNSIGNED_INT_8_8_8_8_REV;
321 shift = 2;
322 break;
323 }
324 bytes <<= shift;
325 break;
327 case PVR2_TEX_FORMAT_ARGB1555:
328 bytes <<= 1;
329 intFormat = GL_RGB5_A1;
330 format = GL_RGBA;
331 type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
332 break;
333 case PVR2_TEX_FORMAT_RGB565:
334 bytes <<= 1;
335 intFormat = GL_RGB;
336 format = GL_RGB;
337 type = GL_UNSIGNED_SHORT_5_6_5_REV;
338 break;
339 case PVR2_TEX_FORMAT_ARGB4444:
340 bytes <<= 1;
341 intFormat = GL_RGBA4;
342 format = GL_BGRA;
343 type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
344 break;
345 case PVR2_TEX_FORMAT_YUV422:
346 ERROR( "YUV textures not supported" );
347 break;
348 case PVR2_TEX_FORMAT_BUMPMAP:
349 ERROR( "Bumpmap not supported" );
350 break;
351 }
353 int level=0, last_level = 0, mip_width = width, mip_height = height, mip_bytes = bytes;
354 if( PVR2_TEX_IS_MIPMAPPED(mode) ) {
355 int i;
356 for( i=0; 1<<(i+1) < width; i++ );
357 last_level = i;
358 mip_width = width >> i;
359 mip_height= height >> i;
360 mip_bytes = bytes >> (i*2);
361 filter = GL_LINEAR_MIPMAP_LINEAR;
362 }
364 if( PVR2_TEX_IS_COMPRESSED(mode) ) {
365 uint16_t tmp[VQ_CODEBOOK_SIZE];
366 pvr2_vram64_read( (char *)tmp, texture_addr, VQ_CODEBOOK_SIZE );
367 texture_addr += VQ_CODEBOOK_SIZE;
368 vq_get_codebook( &codebook, tmp );
369 }
371 for( level=last_level; level>= 0; level-- ) {
372 char data[mip_bytes];
373 /* load data from image, detwiddling/uncompressing as required */
374 if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
375 int inputlength = mip_bytes >> shift;
376 int bank = (mode >> 25) &0x03;
377 char *palette = mmio_region_PVR2PAL.mem + (bank * (256 << shift));
378 char tmp[inputlength];
379 char *p = tmp;
380 pvr2_vram64_read( tmp, texture_addr, inputlength );
381 if( shift == 2 ) {
382 detwiddle_pal8_to_32( 0, 0, mip_width, mip_width, &p,
383 (uint32_t *)data, (uint32_t *)palette );
384 } else {
385 detwiddle_pal8_to_16( 0, 0, mip_width, mip_width, &p,
386 (uint16_t *)data, (uint16_t *)palette );
387 }
388 } else if( PVR2_TEX_IS_COMPRESSED(mode) ) {
389 int inputlength = ((mip_width*mip_height) >> 2);
390 char tmp[inputlength];
391 pvr2_vram64_read( tmp, texture_addr, inputlength );
392 vq_decode( mip_width, mip_height, tmp, (uint16_t *)&data, &codebook,
393 PVR2_TEX_IS_TWIDDLED(mode) );
394 } else if( PVR2_TEX_IS_TWIDDLED(mode) ) {
395 char tmp[mip_bytes];
396 uint16_t *p = (uint16_t *)tmp;
397 pvr2_vram64_read( tmp, texture_addr, mip_bytes );
398 /* Untwiddle */
399 detwiddle_16_to_16( 0, 0, mip_width, mip_width, &p, (uint16_t *)&data );
400 } else {
401 pvr2_vram64_read( data, texture_addr, mip_bytes );
402 }
404 if( PVR2_TEX_IS_MIPMAPPED(mode) && mip_width == 2 ) {
405 /* Opengl requires a 1x1 texture, but the PVR2 doesn't. This should
406 * strictly speaking be the average of the 2x2 texture, but we're
407 * lazy at the moment */
408 glTexImage2D( GL_TEXTURE_2D, level+1, intFormat, 1, 1, 0, format, type, data );
409 }
411 /* Pass to GL */
412 glTexImage2D( GL_TEXTURE_2D, level, intFormat, mip_width, mip_height, 0, format, type,
413 data );
414 texture_addr += mip_bytes;
415 mip_width <<= 1;
416 mip_height <<= 1;
417 mip_bytes <<= 2;
418 }
420 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
421 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
422 }
424 /**
425 * Return a texture ID for the texture specified at the supplied address
426 * and given parameters (the same sequence of bytes could in theory have
427 * multiple interpretations). We use the texture address as the primary
428 * index, but allow for multiple instances at each address. The texture
429 * will be bound to the GL_TEXTURE_2D target before being returned.
430 *
431 * If the texture has already been bound, return the ID to which it was
432 * bound. Otherwise obtain an unused texture ID and set it up appropriately.
433 */
434 GLuint texcache_get_texture( uint32_t texture_addr, int width, int height,
435 int mode )
436 {
437 uint32_t texture_page = texture_addr >> 12;
438 texcache_entry_index idx = texcache_page_lookup[texture_page];
439 while( idx != EMPTY_ENTRY ) {
440 texcache_entry_t entry = &texcache_active_list[idx];
441 if( entry->texture_addr == texture_addr &&
442 entry->mode == mode &&
443 entry->width == width &&
444 entry->height == height ) {
445 entry->lru_count = texcache_ref_counter++;
446 glBindTexture( GL_TEXTURE_2D, entry->texture_id );
447 return entry->texture_id;
448 }
449 idx = entry->next;
450 }
452 /* Not found - check the free list */
453 int slot = 0;
455 if( texcache_free_ptr < MAX_TEXTURES ) {
456 slot = texcache_free_list[texcache_free_ptr++];
457 } else {
458 slot = texcache_evict();
459 }
461 /* Construct new entry */
462 texcache_active_list[slot].texture_addr = texture_addr;
463 texcache_active_list[slot].width = width;
464 texcache_active_list[slot].height = height;
465 texcache_active_list[slot].mode = mode;
466 texcache_active_list[slot].lru_count = texcache_ref_counter++;
468 /* Add entry to the lookup table */
469 texcache_active_list[slot].next = texcache_page_lookup[texture_page];
470 texcache_page_lookup[texture_page] = slot;
472 /* Construct the GL texture */
473 glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
474 texcache_load_texture( texture_addr, width, height, mode );
476 return texcache_active_list[slot].texture_id;
477 }
.