Search
lxdream.org :: lxdream/src/pvr2/texcache.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/texcache.c
changeset 108:565de331ccec
prev107:e576dd36073a
next113:ce4eb7959d56
author nkeynes
date Wed Mar 15 13:16:50 2006 +0000 (15 years ago)
permissions -rw-r--r--
last change More video WIP - displays _something_ now, at least...
file annotate diff log raw
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.3 @@ -1,5 +1,5 @@
1.4 /**
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.7 *
1.8 * Texture cache. Responsible for maintaining a working set of OpenGL
1.9 * textures.
1.10 @@ -52,24 +52,19 @@
1.11 } *texcache_entry_t;
1.12
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.17
1.18 /**
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.22 */
1.23 void texcache_init( )
1.24 {
1.25 int i;
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.30 }
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.34 }
1.35 texcache_free_ptr = 0;
1.36 @@ -77,6 +72,21 @@
1.37 }
1.38
1.39 /**
1.40 + * Setup the initial texture ids (must be called after the GL context is
1.41 + * prepared)
1.42 + */
1.43 +void texcache_gl_init( )
1.44 +{
1.45 + int i;
1.46 + GLuint texids[MAX_TEXTURES];
1.47 +
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.51 + }
1.52 +}
1.53 +
1.54 +/**
1.55 * Flush all textures from the cache, returning them to the free list.
1.56 */
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.60 int mode ) {
1.61 uint32_t bytes = width * height;
1.62 - int bpp = 2;
1.63 + int shift = 1;
1.64 GLint intFormat, format, type;
1.65 - switch( mode & PVR2_TEX_FORMAT_MASK ) {
1.66 - case PVR2_TEX_FORMAT_ARGB1555:
1.67 - bytes <<= 1;
1.68 - intFormat = GL_RGB5_A1;
1.69 - format = GL_RGBA;
1.70 - type = GL_UNSIGNED_SHORT_5_5_5_1;
1.71 - break;
1.72 - case PVR2_TEX_FORMAT_RGB565:
1.73 - bytes <<= 1;
1.74 - intFormat = GL_RGBA;
1.75 - format = GL_RGBA;
1.76 - type = GL_UNSIGNED_SHORT_5_6_5;
1.77 - break;
1.78 - case PVR2_TEX_FORMAT_ARGB4444:
1.79 - bytes <<= 1;
1.80 - intFormat = GL_RGBA4;
1.81 - format = GL_RGBA;
1.82 - type = GL_UNSIGNED_SHORT_4_4_4_4;
1.83 - break;
1.84 - case PVR2_TEX_FORMAT_YUV422:
1.85 - ERROR( "YUV textures not supported" );
1.86 - break;
1.87 - case PVR2_TEX_FORMAT_BUMPMAP:
1.88 - ERROR( "Bumpmap not supported" );
1.89 - break;
1.90 - case PVR2_TEX_FORMAT_IDX4:
1.91 - /* Supported? */
1.92 - bytes >>= 1;
1.93 - intFormat = GL_INTENSITY4;
1.94 - format = GL_COLOR_INDEX;
1.95 - type = GL_UNSIGNED_BYTE;
1.96 - bpp = 0;
1.97 - break;
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.102 - bpp = 1;
1.103 - break;
1.104 + int tex_format = mode & PVR2_TEX_FORMAT_MASK;
1.105 +
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.113 + break;
1.114 + case 1:
1.115 + intFormat = GL_RGB;
1.116 + format = GL_RGB;
1.117 + type = GL_UNSIGNED_SHORT_5_6_5;
1.118 + break;
1.119 + case 2:
1.120 + intFormat = GL_RGBA4;
1.121 + format = GL_RGBA;
1.122 + type = GL_UNSIGNED_SHORT_4_4_4_4;
1.123 + break;
1.124 + case 3:
1.125 + intFormat = GL_RGBA8;
1.126 + format = GL_RGBA;
1.127 + type = GL_UNSIGNED_INT_8_8_8_8;
1.128 + shift = 2;
1.129 + break;
1.130 + }
1.131 +
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.136 + int i;
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.140 + if( shift == 2 )
1.141 + ((uint32_t *)data)[i] = ((uint32_t *)palette)[ch];
1.142 + else
1.143 + ((uint16_t *)data)[i] = ((uint16_t *)palette)[ch];
1.144 + }
1.145 + /* TODO: Detwiddle */
1.146 + glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type,
1.147 + data );
1.148 +
1.149 + }
1.150 + } else {
1.151 + switch( tex_format ) {
1.152 + case PVR2_TEX_FORMAT_ARGB1555:
1.153 + bytes <<= 1;
1.154 + intFormat = GL_RGB5_A1;
1.155 + format = GL_RGBA;
1.156 + type = GL_UNSIGNED_SHORT_5_5_5_1;
1.157 + break;
1.158 + case PVR2_TEX_FORMAT_RGB565:
1.159 + bytes <<= 1;
1.160 + intFormat = GL_RGBA;
1.161 + format = GL_RGBA;
1.162 + type = GL_UNSIGNED_SHORT_5_6_5;
1.163 + break;
1.164 + case PVR2_TEX_FORMAT_ARGB4444:
1.165 + bytes <<= 1;
1.166 + intFormat = GL_RGBA4;
1.167 + format = GL_RGBA;
1.168 + type = GL_UNSIGNED_SHORT_4_4_4_4;
1.169 + break;
1.170 + case PVR2_TEX_FORMAT_YUV422:
1.171 + ERROR( "YUV textures not supported" );
1.172 + break;
1.173 + case PVR2_TEX_FORMAT_BUMPMAP:
1.174 + ERROR( "Bumpmap not supported" );
1.175 + break;
1.176 + case PVR2_TEX_FORMAT_IDX4:
1.177 + /* Supported? */
1.178 + bytes >>= 1;
1.179 + intFormat = GL_INTENSITY4;
1.180 + format = GL_COLOR_INDEX;
1.181 + type = GL_UNSIGNED_BYTE;
1.182 + shift = 0;
1.183 + break;
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.188 + shift = 0;
1.189 + break;
1.190 + }
1.191 +
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.196 + } else {
1.197 + pvr2_vram64_read( &data, texture_addr, bytes );
1.198 + if( PVR2_TEX_IS_TWIDDLED(mode) ) {
1.199 + /* Untwiddle */
1.200 + }
1.201 + }
1.202 +
1.203 + /* Pass to GL */
1.204 + glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type,
1.205 + data );
1.206 }
1.207 -
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.212 - } else {
1.213 - pvr2_vram64_read( &data, texture_addr, bytes );
1.214 - if( PVR2_TEX_IS_TWIDDLED(mode) ) {
1.215 - /* Untwiddle */
1.216 - }
1.217 - }
1.218 - /* Pass to GL */
1.219 - glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type,
1.220 - data );
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.223 }
1.224
1.225 /**
1.226 @@ -283,8 +344,7 @@
1.227 texcache_page_lookup[texture_page] = slot;
1.228
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.234
1.235 return texcache_active_list[slot].texture_id;
.