Search
lxdream.org :: lxdream/src/pvr2/texcache.c
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 (14 years ago)
permissions -rw-r--r--
last change More video WIP - displays _something_ now, at least...
file annotate diff log raw
nkeynes@103
     1
/**
nkeynes@108
     2
 * $Id: texcache.c,v 1.3 2006-03-15 13:16:50 nkeynes Exp $
nkeynes@103
     3
 *
nkeynes@103
     4
 * Texture cache. Responsible for maintaining a working set of OpenGL 
nkeynes@103
     5
 * textures. 
nkeynes@103
     6
 *
nkeynes@103
     7
 *
nkeynes@103
     8
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@103
     9
 *
nkeynes@103
    10
 * This program is free software; you can redistribute it and/or modify
nkeynes@103
    11
 * it under the terms of the GNU General Public License as published by
nkeynes@103
    12
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@103
    13
 * (at your option) any later version.
nkeynes@103
    14
 *
nkeynes@103
    15
 * This program is distributed in the hope that it will be useful,
nkeynes@103
    16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@103
    17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@103
    18
 * GNU General Public License for more details.
nkeynes@103
    19
 */
nkeynes@103
    20
nkeynes@103
    21
#include <assert.h>
nkeynes@103
    22
#include "pvr2/pvr2.h"
nkeynes@103
    23
nkeynes@103
    24
/** Specifies the maximum number of OpenGL
nkeynes@103
    25
 * textures we're willing to have open at a time. If more are
nkeynes@103
    26
 * needed, textures will be evicted in LRU order.
nkeynes@103
    27
 */
nkeynes@103
    28
#define MAX_TEXTURES 64
nkeynes@103
    29
nkeynes@103
    30
/**
nkeynes@103
    31
 * Data structure:
nkeynes@103
    32
 *
nkeynes@103
    33
 * Main operations:
nkeynes@103
    34
 *    find entry by texture_addr
nkeynes@103
    35
 *    add new entry
nkeynes@103
    36
 *    move entry to tail of lru list
nkeynes@103
    37
 *    remove entry
nkeynes@103
    38
 */
nkeynes@103
    39
nkeynes@103
    40
typedef signed short texcache_entry_index;
nkeynes@107
    41
#define EMPTY_ENTRY 0xFF
nkeynes@103
    42
nkeynes@107
    43
static texcache_entry_index texcache_free_ptr = 0;
nkeynes@103
    44
static GLuint texcache_free_list[MAX_TEXTURES];
nkeynes@103
    45
nkeynes@103
    46
typedef struct texcache_entry {
nkeynes@103
    47
    uint32_t texture_addr;
nkeynes@103
    48
    int width, height, mode;
nkeynes@103
    49
    GLuint texture_id;
nkeynes@103
    50
    texcache_entry_index next;
nkeynes@103
    51
    uint32_t lru_count;
nkeynes@103
    52
} *texcache_entry_t;
nkeynes@103
    53
nkeynes@103
    54
static uint8_t texcache_page_lookup[PVR2_RAM_PAGES];
nkeynes@103
    55
static uint32_t texcache_ref_counter;
nkeynes@103
    56
static struct texcache_entry texcache_active_list[MAX_TEXTURES];
nkeynes@103
    57
nkeynes@103
    58
/**
nkeynes@108
    59
 * Initialize the texture cache.
nkeynes@103
    60
 */
nkeynes@103
    61
void texcache_init( )
nkeynes@103
    62
{
nkeynes@103
    63
    int i;
nkeynes@103
    64
    for( i=0; i<PVR2_RAM_PAGES; i++ ) {
nkeynes@103
    65
	texcache_page_lookup[i] = EMPTY_ENTRY;
nkeynes@103
    66
    }
nkeynes@103
    67
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@103
    68
	texcache_free_list[i] = i;
nkeynes@103
    69
    }
nkeynes@103
    70
    texcache_free_ptr = 0;
nkeynes@103
    71
    texcache_ref_counter = 0;
nkeynes@103
    72
}
nkeynes@103
    73
nkeynes@103
    74
/**
nkeynes@108
    75
 * Setup the initial texture ids (must be called after the GL context is
nkeynes@108
    76
 * prepared)
nkeynes@108
    77
 */
nkeynes@108
    78
void texcache_gl_init( )
nkeynes@108
    79
{
nkeynes@108
    80
    int i;
nkeynes@108
    81
    GLuint texids[MAX_TEXTURES];
nkeynes@108
    82
nkeynes@108
    83
    glGenTextures( MAX_TEXTURES, texids );
nkeynes@108
    84
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@108
    85
	texcache_active_list[i].texture_id = texids[i];
nkeynes@108
    86
    }
nkeynes@108
    87
}
nkeynes@108
    88
nkeynes@108
    89
/**
nkeynes@103
    90
 * Flush all textures from the cache, returning them to the free list.
nkeynes@103
    91
 */
nkeynes@103
    92
void texcache_flush( )
nkeynes@103
    93
{
nkeynes@103
    94
    int i;
nkeynes@103
    95
    /* clear structures */
nkeynes@103
    96
    for( i=0; i<PVR2_RAM_PAGES; i++ ) {
nkeynes@103
    97
	texcache_page_lookup[i] = EMPTY_ENTRY;
nkeynes@103
    98
    }
nkeynes@103
    99
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@103
   100
	texcache_free_list[i] = i;
nkeynes@103
   101
    }
nkeynes@103
   102
    texcache_free_ptr = 0;
nkeynes@103
   103
    texcache_ref_counter = 0;
nkeynes@103
   104
}
nkeynes@103
   105
nkeynes@103
   106
/**
nkeynes@103
   107
 * Flush all textures and delete. The cache will be non-functional until
nkeynes@103
   108
 * the next call to texcache_init(). This would typically be done if
nkeynes@103
   109
 * switching GL targets.
nkeynes@103
   110
 */    
nkeynes@103
   111
void texcache_shutdown( )
nkeynes@103
   112
{
nkeynes@103
   113
    GLuint texids[MAX_TEXTURES];
nkeynes@103
   114
    int i;
nkeynes@103
   115
    texcache_flush();
nkeynes@103
   116
    
nkeynes@103
   117
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@103
   118
	texids[i] = texcache_active_list[i].texture_id;
nkeynes@103
   119
    }
nkeynes@103
   120
    glDeleteTextures( MAX_TEXTURES, texids );
nkeynes@103
   121
}
nkeynes@103
   122
nkeynes@103
   123
/**
nkeynes@103
   124
 * Evict all textures contained in the page identified by a texture address.
nkeynes@103
   125
 */
nkeynes@103
   126
void texcache_invalidate_page( uint32_t texture_addr ) {
nkeynes@103
   127
    uint32_t texture_page = texture_addr >> 12;
nkeynes@103
   128
    texcache_entry_index idx = texcache_page_lookup[texture_page];
nkeynes@103
   129
    if( idx == EMPTY_ENTRY )
nkeynes@103
   130
	return;
nkeynes@107
   131
    assert( texcache_free_ptr >= 0 );
nkeynes@103
   132
    do {
nkeynes@103
   133
	texcache_entry_t entry = &texcache_active_list[idx];	
nkeynes@103
   134
	/* release entry */
nkeynes@103
   135
	texcache_free_ptr--;
nkeynes@103
   136
	texcache_free_list[texcache_free_ptr] = idx;
nkeynes@103
   137
	idx = entry->next;
nkeynes@103
   138
	entry->next = EMPTY_ENTRY;
nkeynes@103
   139
    } while( idx != EMPTY_ENTRY );
nkeynes@103
   140
    texcache_page_lookup[texture_page] = EMPTY_ENTRY;
nkeynes@103
   141
}
nkeynes@103
   142
nkeynes@103
   143
/**
nkeynes@103
   144
 * Evict a single texture from the cache.
nkeynes@103
   145
 * @return the slot of the evicted texture.
nkeynes@103
   146
 */
nkeynes@103
   147
static texcache_entry_index texcache_evict( void )
nkeynes@103
   148
{
nkeynes@103
   149
    /* Full table scan - take over the entry with the lowest lru value */
nkeynes@103
   150
    texcache_entry_index slot = 0;
nkeynes@103
   151
    int lru_value = texcache_active_list[0].lru_count;
nkeynes@103
   152
    int i;
nkeynes@103
   153
    for( i=1; i<MAX_TEXTURES; i++ ) {
nkeynes@103
   154
	/* FIXME: account for rollover */
nkeynes@103
   155
	if( texcache_active_list[i].lru_count < lru_value ) {
nkeynes@103
   156
	    slot = i;
nkeynes@103
   157
	    lru_value = texcache_active_list[i].lru_count;
nkeynes@103
   158
	}
nkeynes@103
   159
    }
nkeynes@103
   160
    
nkeynes@103
   161
    /* Remove the selected slot from the lookup table */
nkeynes@103
   162
    uint32_t evict_page = texcache_active_list[slot].texture_addr;
nkeynes@103
   163
    texcache_entry_index replace_next = texcache_active_list[slot].next;
nkeynes@103
   164
    texcache_active_list[slot].next = EMPTY_ENTRY; /* Just for safety */
nkeynes@103
   165
    if( texcache_page_lookup[evict_page] == slot ) {
nkeynes@103
   166
	texcache_page_lookup[evict_page] = replace_next;
nkeynes@103
   167
    } else {
nkeynes@103
   168
	texcache_entry_index idx = texcache_page_lookup[evict_page];
nkeynes@103
   169
	texcache_entry_index next;
nkeynes@103
   170
	do {
nkeynes@103
   171
	    next = texcache_active_list[idx].next;
nkeynes@103
   172
	    if( next == slot ) {
nkeynes@103
   173
		texcache_active_list[idx].next = replace_next;
nkeynes@103
   174
		break;
nkeynes@103
   175
	    }
nkeynes@103
   176
	    idx = next;
nkeynes@103
   177
	} while( next != EMPTY_ENTRY );
nkeynes@103
   178
    }
nkeynes@103
   179
    return slot;
nkeynes@103
   180
}
nkeynes@103
   181
nkeynes@103
   182
/**
nkeynes@103
   183
 * Load texture data from the given address and parameters into the currently
nkeynes@103
   184
 * bound OpenGL texture.
nkeynes@103
   185
 */
nkeynes@103
   186
static texcache_load_texture( uint32_t texture_addr, int width, int height,
nkeynes@103
   187
			      int mode ) {
nkeynes@103
   188
    uint32_t bytes = width * height;
nkeynes@108
   189
    int shift = 1;
nkeynes@103
   190
    GLint intFormat, format, type;
nkeynes@108
   191
    int tex_format = mode & PVR2_TEX_FORMAT_MASK;
nkeynes@108
   192
nkeynes@108
   193
    if( tex_format == PVR2_TEX_FORMAT_IDX8 ||
nkeynes@108
   194
	tex_format == PVR2_TEX_FORMAT_IDX4 ) {
nkeynes@108
   195
	switch( MMIO_READ( PVR2, PALETTECFG ) & 0x03 ) {
nkeynes@108
   196
	case 0: /* ARGB1555 */
nkeynes@108
   197
	    intFormat = GL_RGB5_A1;
nkeynes@108
   198
	    format = GL_RGBA;
nkeynes@108
   199
	    type = GL_UNSIGNED_SHORT_5_5_5_1;
nkeynes@108
   200
	    break;
nkeynes@108
   201
	case 1: 
nkeynes@108
   202
	    intFormat = GL_RGB;
nkeynes@108
   203
	    format = GL_RGB;
nkeynes@108
   204
	    type = GL_UNSIGNED_SHORT_5_6_5;
nkeynes@108
   205
	    break;
nkeynes@108
   206
	case 2:
nkeynes@108
   207
	    intFormat = GL_RGBA4;
nkeynes@108
   208
	    format = GL_RGBA;
nkeynes@108
   209
	    type = GL_UNSIGNED_SHORT_4_4_4_4;
nkeynes@108
   210
	    break;
nkeynes@108
   211
	case 3:
nkeynes@108
   212
	    intFormat = GL_RGBA8;
nkeynes@108
   213
	    format = GL_RGBA;
nkeynes@108
   214
	    type = GL_UNSIGNED_INT_8_8_8_8;
nkeynes@108
   215
	    shift = 2;
nkeynes@108
   216
	    break;
nkeynes@108
   217
	}
nkeynes@108
   218
nkeynes@108
   219
	if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
nkeynes@108
   220
	    int bank = (mode >> 25) &0x03;
nkeynes@108
   221
	    unsigned char data[bytes<<shift];
nkeynes@108
   222
	    char *palette = mmio_region_PVR2PAL.mem + (bank * (256 << shift));
nkeynes@108
   223
	    int i;
nkeynes@108
   224
	    pvr2_vram64_read( &data, texture_addr, bytes );
nkeynes@108
   225
	    for( i=bytes-1; i>=0; i-- ) {
nkeynes@108
   226
		char ch = data[i];
nkeynes@108
   227
		if( shift == 2 )
nkeynes@108
   228
		    ((uint32_t *)data)[i] = ((uint32_t *)palette)[ch];
nkeynes@108
   229
		else
nkeynes@108
   230
		    ((uint16_t *)data)[i] = ((uint16_t *)palette)[ch];
nkeynes@108
   231
	    }
nkeynes@108
   232
	    /* TODO: Detwiddle */
nkeynes@108
   233
	    glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type,
nkeynes@108
   234
			  data );
nkeynes@108
   235
nkeynes@108
   236
	}
nkeynes@108
   237
    } else {
nkeynes@108
   238
	switch( tex_format ) {
nkeynes@108
   239
	case PVR2_TEX_FORMAT_ARGB1555:
nkeynes@108
   240
	    bytes <<= 1;
nkeynes@108
   241
	    intFormat = GL_RGB5_A1;
nkeynes@108
   242
	    format = GL_RGBA;
nkeynes@108
   243
	    type = GL_UNSIGNED_SHORT_5_5_5_1;
nkeynes@108
   244
	    break;
nkeynes@108
   245
	case PVR2_TEX_FORMAT_RGB565:
nkeynes@108
   246
	    bytes <<= 1;
nkeynes@108
   247
	    intFormat = GL_RGBA;
nkeynes@108
   248
	    format = GL_RGBA;
nkeynes@108
   249
	    type = GL_UNSIGNED_SHORT_5_6_5;
nkeynes@108
   250
	    break;
nkeynes@108
   251
	case PVR2_TEX_FORMAT_ARGB4444:
nkeynes@108
   252
	    bytes <<= 1;
nkeynes@108
   253
	    intFormat = GL_RGBA4;
nkeynes@108
   254
	    format = GL_RGBA;
nkeynes@108
   255
	    type = GL_UNSIGNED_SHORT_4_4_4_4;
nkeynes@108
   256
	    break;
nkeynes@108
   257
	case PVR2_TEX_FORMAT_YUV422:
nkeynes@108
   258
	    ERROR( "YUV textures not supported" );
nkeynes@108
   259
	    break;
nkeynes@108
   260
	case PVR2_TEX_FORMAT_BUMPMAP:
nkeynes@108
   261
	    ERROR( "Bumpmap not supported" );
nkeynes@108
   262
	    break;
nkeynes@108
   263
	case PVR2_TEX_FORMAT_IDX4:
nkeynes@108
   264
	    /* Supported? */
nkeynes@108
   265
	    bytes >>= 1;
nkeynes@108
   266
	    intFormat = GL_INTENSITY4;
nkeynes@108
   267
	    format = GL_COLOR_INDEX;
nkeynes@108
   268
	    type = GL_UNSIGNED_BYTE;
nkeynes@108
   269
	    shift = 0;
nkeynes@108
   270
	    break;
nkeynes@108
   271
	case PVR2_TEX_FORMAT_IDX8:
nkeynes@108
   272
	    intFormat = GL_INTENSITY8;
nkeynes@108
   273
	    format = GL_COLOR_INDEX;
nkeynes@108
   274
	    type = GL_UNSIGNED_BYTE;
nkeynes@108
   275
	    shift = 0;
nkeynes@108
   276
	    break;
nkeynes@108
   277
	}
nkeynes@108
   278
	
nkeynes@108
   279
	unsigned char data[bytes];
nkeynes@108
   280
	/* load data from image, detwiddling/uncompressing as required */
nkeynes@108
   281
	if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@108
   282
	    ERROR( "VQ Compression not supported" );
nkeynes@108
   283
	} else {
nkeynes@108
   284
	    pvr2_vram64_read( &data, texture_addr, bytes );
nkeynes@108
   285
	    if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@108
   286
		/* Untwiddle */
nkeynes@108
   287
	    }
nkeynes@108
   288
	}
nkeynes@108
   289
nkeynes@108
   290
	/* Pass to GL */
nkeynes@108
   291
	glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type,
nkeynes@108
   292
		      data );
nkeynes@103
   293
    }
nkeynes@108
   294
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nkeynes@108
   295
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nkeynes@103
   296
}
nkeynes@103
   297
nkeynes@103
   298
/**
nkeynes@103
   299
 * Return a texture ID for the texture specified at the supplied address
nkeynes@103
   300
 * and given parameters (the same sequence of bytes could in theory have
nkeynes@103
   301
 * multiple interpretations). We use the texture address as the primary
nkeynes@103
   302
 * index, but allow for multiple instances at each address. The texture
nkeynes@103
   303
 * will be bound to the GL_TEXTURE_2D target before being returned.
nkeynes@103
   304
 * 
nkeynes@103
   305
 * If the texture has already been bound, return the ID to which it was
nkeynes@103
   306
 * bound. Otherwise obtain an unused texture ID and set it up appropriately.
nkeynes@103
   307
 */
nkeynes@103
   308
GLuint texcache_get_texture( uint32_t texture_addr, int width, int height,
nkeynes@103
   309
			     int mode )
nkeynes@103
   310
{
nkeynes@103
   311
    uint32_t texture_page = texture_addr >> 12;
nkeynes@103
   312
    texcache_entry_index idx = texcache_page_lookup[texture_page];
nkeynes@103
   313
    while( idx != EMPTY_ENTRY ) {
nkeynes@103
   314
	texcache_entry_t entry = &texcache_active_list[idx];
nkeynes@103
   315
	if( entry->texture_addr == texture_addr &&
nkeynes@103
   316
	    entry->mode == mode &&
nkeynes@103
   317
	    entry->width == width &&
nkeynes@103
   318
	    entry->height == height ) {
nkeynes@103
   319
	    entry->lru_count = texcache_ref_counter++;
nkeynes@103
   320
	    glBindTexture( GL_TEXTURE_2D, entry->texture_id );
nkeynes@103
   321
	    return entry->texture_id;
nkeynes@103
   322
	}
nkeynes@103
   323
        idx = entry->next;
nkeynes@103
   324
    }
nkeynes@103
   325
nkeynes@103
   326
    /* Not found - check the free list */
nkeynes@103
   327
    int slot = 0;
nkeynes@103
   328
nkeynes@103
   329
    if( texcache_free_ptr < MAX_TEXTURES ) {
nkeynes@103
   330
	slot = texcache_free_list[texcache_free_ptr++];
nkeynes@103
   331
    } else {
nkeynes@103
   332
	slot = texcache_evict();
nkeynes@103
   333
    }
nkeynes@103
   334
nkeynes@103
   335
    /* Construct new entry */
nkeynes@103
   336
    texcache_active_list[slot].texture_addr = texture_addr;
nkeynes@103
   337
    texcache_active_list[slot].width = width;
nkeynes@103
   338
    texcache_active_list[slot].height = height;
nkeynes@103
   339
    texcache_active_list[slot].mode = mode;
nkeynes@103
   340
    texcache_active_list[slot].lru_count = texcache_ref_counter++;
nkeynes@103
   341
nkeynes@103
   342
    /* Add entry to the lookup table */
nkeynes@103
   343
    texcache_active_list[slot].next = texcache_page_lookup[texture_page];
nkeynes@103
   344
    texcache_page_lookup[texture_page] = slot;
nkeynes@103
   345
nkeynes@103
   346
    /* Construct the GL texture */
nkeynes@108
   347
    glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
nkeynes@103
   348
    texcache_load_texture( texture_addr, width, height, mode );
nkeynes@103
   349
    
nkeynes@103
   350
    return texcache_active_list[slot].texture_id;
nkeynes@103
   351
}
.