1.1 --- a/src/pvr2/texcache.c Tue Mar 14 13:02:06 2006 +0000
1.2 +++ b/src/pvr2/texcache.c Wed Mar 15 13:16:50 2006 +0000
1.5 - * $Id: texcache.c,v 1.2 2006-03-14 13:02:06 nkeynes Exp $
1.6 + * $Id: texcache.c,v 1.3 2006-03-15 13:16:50 nkeynes Exp $
1.8 * Texture cache. Responsible for maintaining a working set of OpenGL
1.11 } *texcache_entry_t;
1.13 static uint8_t texcache_page_lookup[PVR2_RAM_PAGES];
1.14 -static uint32_t texcache_active_ptr;
1.15 static uint32_t texcache_ref_counter;
1.16 static struct texcache_entry texcache_active_list[MAX_TEXTURES];
1.19 - * Initialize the texture cache. Note that the GL context must have been
1.20 - * initialized before calling this function.
1.21 + * Initialize the texture cache.
1.23 void texcache_init( )
1.26 - GLuint texids[MAX_TEXTURES];
1.27 - glGenTextures( MAX_TEXTURES, texids );
1.28 for( i=0; i<PVR2_RAM_PAGES; i++ ) {
1.29 texcache_page_lookup[i] = EMPTY_ENTRY;
1.31 for( i=0; i<MAX_TEXTURES; i++ ) {
1.32 - texcache_active_list[i].texture_id = texids[i];
1.33 texcache_free_list[i] = i;
1.35 texcache_free_ptr = 0;
1.40 + * Setup the initial texture ids (must be called after the GL context is
1.43 +void texcache_gl_init( )
1.46 + GLuint texids[MAX_TEXTURES];
1.48 + glGenTextures( MAX_TEXTURES, texids );
1.49 + for( i=0; i<MAX_TEXTURES; i++ ) {
1.50 + texcache_active_list[i].texture_id = texids[i];
1.55 * Flush all textures from the cache, returning them to the free list.
1.57 void texcache_flush( )
1.58 @@ -176,62 +186,113 @@
1.59 static texcache_load_texture( uint32_t texture_addr, int width, int height,
1.61 uint32_t bytes = width * height;
1.64 GLint intFormat, format, type;
1.65 - switch( mode & PVR2_TEX_FORMAT_MASK ) {
1.66 - case PVR2_TEX_FORMAT_ARGB1555:
1.68 - intFormat = GL_RGB5_A1;
1.70 - type = GL_UNSIGNED_SHORT_5_5_5_1;
1.72 - case PVR2_TEX_FORMAT_RGB565:
1.74 - intFormat = GL_RGBA;
1.76 - type = GL_UNSIGNED_SHORT_5_6_5;
1.78 - case PVR2_TEX_FORMAT_ARGB4444:
1.80 - intFormat = GL_RGBA4;
1.82 - type = GL_UNSIGNED_SHORT_4_4_4_4;
1.84 - case PVR2_TEX_FORMAT_YUV422:
1.85 - ERROR( "YUV textures not supported" );
1.87 - case PVR2_TEX_FORMAT_BUMPMAP:
1.88 - ERROR( "Bumpmap not supported" );
1.90 - case PVR2_TEX_FORMAT_IDX4:
1.93 - intFormat = GL_INTENSITY4;
1.94 - format = GL_COLOR_INDEX;
1.95 - type = GL_UNSIGNED_BYTE;
1.98 - case PVR2_TEX_FORMAT_IDX8:
1.99 - intFormat = GL_INTENSITY8;
1.100 - format = GL_COLOR_INDEX;
1.101 - type = GL_UNSIGNED_BYTE;
1.104 + int tex_format = mode & PVR2_TEX_FORMAT_MASK;
1.106 + if( tex_format == PVR2_TEX_FORMAT_IDX8 ||
1.107 + tex_format == PVR2_TEX_FORMAT_IDX4 ) {
1.108 + switch( MMIO_READ( PVR2, PALETTECFG ) & 0x03 ) {
1.109 + case 0: /* ARGB1555 */
1.110 + intFormat = GL_RGB5_A1;
1.111 + format = GL_RGBA;
1.112 + type = GL_UNSIGNED_SHORT_5_5_5_1;
1.115 + intFormat = GL_RGB;
1.117 + type = GL_UNSIGNED_SHORT_5_6_5;
1.120 + intFormat = GL_RGBA4;
1.121 + format = GL_RGBA;
1.122 + type = GL_UNSIGNED_SHORT_4_4_4_4;
1.125 + intFormat = GL_RGBA8;
1.126 + format = GL_RGBA;
1.127 + type = GL_UNSIGNED_INT_8_8_8_8;
1.132 + if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
1.133 + int bank = (mode >> 25) &0x03;
1.134 + unsigned char data[bytes<<shift];
1.135 + char *palette = mmio_region_PVR2PAL.mem + (bank * (256 << shift));
1.137 + pvr2_vram64_read( &data, texture_addr, bytes );
1.138 + for( i=bytes-1; i>=0; i-- ) {
1.139 + char ch = data[i];
1.141 + ((uint32_t *)data)[i] = ((uint32_t *)palette)[ch];
1.143 + ((uint16_t *)data)[i] = ((uint16_t *)palette)[ch];
1.145 + /* TODO: Detwiddle */
1.146 + glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type,
1.151 + switch( tex_format ) {
1.152 + case PVR2_TEX_FORMAT_ARGB1555:
1.154 + intFormat = GL_RGB5_A1;
1.155 + format = GL_RGBA;
1.156 + type = GL_UNSIGNED_SHORT_5_5_5_1;
1.158 + case PVR2_TEX_FORMAT_RGB565:
1.160 + intFormat = GL_RGBA;
1.161 + format = GL_RGBA;
1.162 + type = GL_UNSIGNED_SHORT_5_6_5;
1.164 + case PVR2_TEX_FORMAT_ARGB4444:
1.166 + intFormat = GL_RGBA4;
1.167 + format = GL_RGBA;
1.168 + type = GL_UNSIGNED_SHORT_4_4_4_4;
1.170 + case PVR2_TEX_FORMAT_YUV422:
1.171 + ERROR( "YUV textures not supported" );
1.173 + case PVR2_TEX_FORMAT_BUMPMAP:
1.174 + ERROR( "Bumpmap not supported" );
1.176 + case PVR2_TEX_FORMAT_IDX4:
1.179 + intFormat = GL_INTENSITY4;
1.180 + format = GL_COLOR_INDEX;
1.181 + type = GL_UNSIGNED_BYTE;
1.184 + case PVR2_TEX_FORMAT_IDX8:
1.185 + intFormat = GL_INTENSITY8;
1.186 + format = GL_COLOR_INDEX;
1.187 + type = GL_UNSIGNED_BYTE;
1.192 + unsigned char data[bytes];
1.193 + /* load data from image, detwiddling/uncompressing as required */
1.194 + if( PVR2_TEX_IS_COMPRESSED(mode) ) {
1.195 + ERROR( "VQ Compression not supported" );
1.197 + pvr2_vram64_read( &data, texture_addr, bytes );
1.198 + if( PVR2_TEX_IS_TWIDDLED(mode) ) {
1.204 + glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type,
1.208 - unsigned char data[bytes];
1.209 - /* load data from image, detwiddling/uncompressing as required */
1.210 - if( PVR2_TEX_IS_COMPRESSED(mode) ) {
1.211 - ERROR( "VQ Compression not supported" );
1.213 - pvr2_vram64_read( &data, texture_addr, bytes );
1.214 - if( PVR2_TEX_IS_TWIDDLED(mode) ) {
1.219 - glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type,
1.221 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1.222 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1.226 @@ -283,8 +344,7 @@
1.227 texcache_page_lookup[texture_page] = slot;
1.229 /* Construct the GL texture */
1.230 - GLuint texid = texcache_free_list[texcache_free_ptr++];
1.231 - glBindTexture( GL_TEXTURE_2D, texid );
1.232 + glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
1.233 texcache_load_texture( texture_addr, width, height, mode );
1.235 return texcache_active_list[slot].texture_id;