Search
lxdream.org :: lxdream/src/pvr2/texcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/texcache.c
changeset 107:e576dd36073a
prev103:9b9cfc5855e0
next108:565de331ccec
author nkeynes
date Tue Mar 14 13:02:06 2006 +0000 (13 years ago)
permissions -rw-r--r--
last change Make sure subfunctions are initted
Fix stupid sign bug in texcache
file annotate diff log raw
nkeynes@103
     1
/**
nkeynes@107
     2
 * $Id: texcache.c,v 1.2 2006-03-14 13:02:06 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_active_ptr;
nkeynes@103
    56
static uint32_t texcache_ref_counter;
nkeynes@103
    57
static struct texcache_entry texcache_active_list[MAX_TEXTURES];
nkeynes@103
    58
nkeynes@103
    59
/**
nkeynes@103
    60
 * Initialize the texture cache. Note that the GL context must have been
nkeynes@103
    61
 * initialized before calling this function.
nkeynes@103
    62
 */
nkeynes@103
    63
void texcache_init( )
nkeynes@103
    64
{
nkeynes@103
    65
    int i;
nkeynes@103
    66
    GLuint texids[MAX_TEXTURES];
nkeynes@103
    67
    glGenTextures( MAX_TEXTURES, texids );
nkeynes@103
    68
    for( i=0; i<PVR2_RAM_PAGES; i++ ) {
nkeynes@103
    69
	texcache_page_lookup[i] = EMPTY_ENTRY;
nkeynes@103
    70
    }
nkeynes@103
    71
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@103
    72
	texcache_active_list[i].texture_id = texids[i];
nkeynes@103
    73
	texcache_free_list[i] = i;
nkeynes@103
    74
    }
nkeynes@103
    75
    texcache_free_ptr = 0;
nkeynes@103
    76
    texcache_ref_counter = 0;
nkeynes@103
    77
}
nkeynes@103
    78
nkeynes@103
    79
/**
nkeynes@103
    80
 * Flush all textures from the cache, returning them to the free list.
nkeynes@103
    81
 */
nkeynes@103
    82
void texcache_flush( )
nkeynes@103
    83
{
nkeynes@103
    84
    int i;
nkeynes@103
    85
    /* clear structures */
nkeynes@103
    86
    for( i=0; i<PVR2_RAM_PAGES; i++ ) {
nkeynes@103
    87
	texcache_page_lookup[i] = EMPTY_ENTRY;
nkeynes@103
    88
    }
nkeynes@103
    89
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@103
    90
	texcache_free_list[i] = i;
nkeynes@103
    91
    }
nkeynes@103
    92
    texcache_free_ptr = 0;
nkeynes@103
    93
    texcache_ref_counter = 0;
nkeynes@103
    94
}
nkeynes@103
    95
nkeynes@103
    96
/**
nkeynes@103
    97
 * Flush all textures and delete. The cache will be non-functional until
nkeynes@103
    98
 * the next call to texcache_init(). This would typically be done if
nkeynes@103
    99
 * switching GL targets.
nkeynes@103
   100
 */    
nkeynes@103
   101
void texcache_shutdown( )
nkeynes@103
   102
{
nkeynes@103
   103
    GLuint texids[MAX_TEXTURES];
nkeynes@103
   104
    int i;
nkeynes@103
   105
    texcache_flush();
nkeynes@103
   106
    
nkeynes@103
   107
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@103
   108
	texids[i] = texcache_active_list[i].texture_id;
nkeynes@103
   109
    }
nkeynes@103
   110
    glDeleteTextures( MAX_TEXTURES, texids );
nkeynes@103
   111
}
nkeynes@103
   112
nkeynes@103
   113
/**
nkeynes@103
   114
 * Evict all textures contained in the page identified by a texture address.
nkeynes@103
   115
 */
nkeynes@103
   116
void texcache_invalidate_page( uint32_t texture_addr ) {
nkeynes@103
   117
    uint32_t texture_page = texture_addr >> 12;
nkeynes@103
   118
    texcache_entry_index idx = texcache_page_lookup[texture_page];
nkeynes@103
   119
    if( idx == EMPTY_ENTRY )
nkeynes@103
   120
	return;
nkeynes@107
   121
    assert( texcache_free_ptr >= 0 );
nkeynes@103
   122
    do {
nkeynes@103
   123
	texcache_entry_t entry = &texcache_active_list[idx];	
nkeynes@103
   124
	/* release entry */
nkeynes@103
   125
	texcache_free_ptr--;
nkeynes@103
   126
	texcache_free_list[texcache_free_ptr] = idx;
nkeynes@103
   127
	idx = entry->next;
nkeynes@103
   128
	entry->next = EMPTY_ENTRY;
nkeynes@103
   129
    } while( idx != EMPTY_ENTRY );
nkeynes@103
   130
    texcache_page_lookup[texture_page] = EMPTY_ENTRY;
nkeynes@103
   131
}
nkeynes@103
   132
nkeynes@103
   133
/**
nkeynes@103
   134
 * Evict a single texture from the cache.
nkeynes@103
   135
 * @return the slot of the evicted texture.
nkeynes@103
   136
 */
nkeynes@103
   137
static texcache_entry_index texcache_evict( void )
nkeynes@103
   138
{
nkeynes@103
   139
    /* Full table scan - take over the entry with the lowest lru value */
nkeynes@103
   140
    texcache_entry_index slot = 0;
nkeynes@103
   141
    int lru_value = texcache_active_list[0].lru_count;
nkeynes@103
   142
    int i;
nkeynes@103
   143
    for( i=1; i<MAX_TEXTURES; i++ ) {
nkeynes@103
   144
	/* FIXME: account for rollover */
nkeynes@103
   145
	if( texcache_active_list[i].lru_count < lru_value ) {
nkeynes@103
   146
	    slot = i;
nkeynes@103
   147
	    lru_value = texcache_active_list[i].lru_count;
nkeynes@103
   148
	}
nkeynes@103
   149
    }
nkeynes@103
   150
    
nkeynes@103
   151
    /* Remove the selected slot from the lookup table */
nkeynes@103
   152
    uint32_t evict_page = texcache_active_list[slot].texture_addr;
nkeynes@103
   153
    texcache_entry_index replace_next = texcache_active_list[slot].next;
nkeynes@103
   154
    texcache_active_list[slot].next = EMPTY_ENTRY; /* Just for safety */
nkeynes@103
   155
    if( texcache_page_lookup[evict_page] == slot ) {
nkeynes@103
   156
	texcache_page_lookup[evict_page] = replace_next;
nkeynes@103
   157
    } else {
nkeynes@103
   158
	texcache_entry_index idx = texcache_page_lookup[evict_page];
nkeynes@103
   159
	texcache_entry_index next;
nkeynes@103
   160
	do {
nkeynes@103
   161
	    next = texcache_active_list[idx].next;
nkeynes@103
   162
	    if( next == slot ) {
nkeynes@103
   163
		texcache_active_list[idx].next = replace_next;
nkeynes@103
   164
		break;
nkeynes@103
   165
	    }
nkeynes@103
   166
	    idx = next;
nkeynes@103
   167
	} while( next != EMPTY_ENTRY );
nkeynes@103
   168
    }
nkeynes@103
   169
    return slot;
nkeynes@103
   170
}
nkeynes@103
   171
nkeynes@103
   172
/**
nkeynes@103
   173
 * Load texture data from the given address and parameters into the currently
nkeynes@103
   174
 * bound OpenGL texture.
nkeynes@103
   175
 */
nkeynes@103
   176
static texcache_load_texture( uint32_t texture_addr, int width, int height,
nkeynes@103
   177
			      int mode ) {
nkeynes@103
   178
    uint32_t bytes = width * height;
nkeynes@103
   179
    int bpp = 2;
nkeynes@103
   180
    GLint intFormat, format, type;
nkeynes@103
   181
    switch( mode & PVR2_TEX_FORMAT_MASK ) {
nkeynes@103
   182
    case PVR2_TEX_FORMAT_ARGB1555:
nkeynes@103
   183
	bytes <<= 1;
nkeynes@103
   184
	intFormat = GL_RGB5_A1;
nkeynes@103
   185
	format = GL_RGBA;
nkeynes@103
   186
	type = GL_UNSIGNED_SHORT_5_5_5_1;
nkeynes@103
   187
	break;
nkeynes@103
   188
    case PVR2_TEX_FORMAT_RGB565:
nkeynes@103
   189
	bytes <<= 1;
nkeynes@103
   190
	intFormat = GL_RGBA;
nkeynes@103
   191
	format = GL_RGBA;
nkeynes@103
   192
	type = GL_UNSIGNED_SHORT_5_6_5;
nkeynes@103
   193
	break;
nkeynes@103
   194
    case PVR2_TEX_FORMAT_ARGB4444:
nkeynes@103
   195
	bytes <<= 1;
nkeynes@103
   196
	intFormat = GL_RGBA4;
nkeynes@103
   197
	format = GL_RGBA;
nkeynes@103
   198
	type = GL_UNSIGNED_SHORT_4_4_4_4;
nkeynes@103
   199
	break;
nkeynes@103
   200
    case PVR2_TEX_FORMAT_YUV422:
nkeynes@103
   201
	ERROR( "YUV textures not supported" );
nkeynes@103
   202
	break;
nkeynes@103
   203
    case PVR2_TEX_FORMAT_BUMPMAP:
nkeynes@103
   204
	ERROR( "Bumpmap not supported" );
nkeynes@103
   205
	break;
nkeynes@103
   206
    case PVR2_TEX_FORMAT_IDX4:
nkeynes@103
   207
	/* Supported? */
nkeynes@103
   208
	bytes >>= 1;
nkeynes@103
   209
	intFormat = GL_INTENSITY4;
nkeynes@103
   210
	format = GL_COLOR_INDEX;
nkeynes@103
   211
	type = GL_UNSIGNED_BYTE;
nkeynes@103
   212
	bpp = 0;
nkeynes@103
   213
	break;
nkeynes@103
   214
    case PVR2_TEX_FORMAT_IDX8:
nkeynes@103
   215
	intFormat = GL_INTENSITY8;
nkeynes@103
   216
	format = GL_COLOR_INDEX;
nkeynes@103
   217
	type = GL_UNSIGNED_BYTE;
nkeynes@103
   218
	bpp = 1;
nkeynes@103
   219
	break;
nkeynes@103
   220
    }
nkeynes@103
   221
nkeynes@103
   222
    unsigned char data[bytes];
nkeynes@103
   223
    /* load data from image, detwiddling/uncompressing as required */
nkeynes@103
   224
    if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@103
   225
	ERROR( "VQ Compression not supported" );
nkeynes@103
   226
    } else {
nkeynes@103
   227
	pvr2_vram64_read( &data, texture_addr, bytes );
nkeynes@103
   228
	if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@103
   229
	    /* Untwiddle */
nkeynes@103
   230
	}
nkeynes@103
   231
    }
nkeynes@103
   232
    /* Pass to GL */
nkeynes@103
   233
    glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type,
nkeynes@103
   234
		  data );
nkeynes@103
   235
}
nkeynes@103
   236
nkeynes@103
   237
/**
nkeynes@103
   238
 * Return a texture ID for the texture specified at the supplied address
nkeynes@103
   239
 * and given parameters (the same sequence of bytes could in theory have
nkeynes@103
   240
 * multiple interpretations). We use the texture address as the primary
nkeynes@103
   241
 * index, but allow for multiple instances at each address. The texture
nkeynes@103
   242
 * will be bound to the GL_TEXTURE_2D target before being returned.
nkeynes@103
   243
 * 
nkeynes@103
   244
 * If the texture has already been bound, return the ID to which it was
nkeynes@103
   245
 * bound. Otherwise obtain an unused texture ID and set it up appropriately.
nkeynes@103
   246
 */
nkeynes@103
   247
GLuint texcache_get_texture( uint32_t texture_addr, int width, int height,
nkeynes@103
   248
			     int mode )
nkeynes@103
   249
{
nkeynes@103
   250
    uint32_t texture_page = texture_addr >> 12;
nkeynes@103
   251
    texcache_entry_index idx = texcache_page_lookup[texture_page];
nkeynes@103
   252
    while( idx != EMPTY_ENTRY ) {
nkeynes@103
   253
	texcache_entry_t entry = &texcache_active_list[idx];
nkeynes@103
   254
	if( entry->texture_addr == texture_addr &&
nkeynes@103
   255
	    entry->mode == mode &&
nkeynes@103
   256
	    entry->width == width &&
nkeynes@103
   257
	    entry->height == height ) {
nkeynes@103
   258
	    entry->lru_count = texcache_ref_counter++;
nkeynes@103
   259
	    glBindTexture( GL_TEXTURE_2D, entry->texture_id );
nkeynes@103
   260
	    return entry->texture_id;
nkeynes@103
   261
	}
nkeynes@103
   262
        idx = entry->next;
nkeynes@103
   263
    }
nkeynes@103
   264
nkeynes@103
   265
    /* Not found - check the free list */
nkeynes@103
   266
    int slot = 0;
nkeynes@103
   267
nkeynes@103
   268
    if( texcache_free_ptr < MAX_TEXTURES ) {
nkeynes@103
   269
	slot = texcache_free_list[texcache_free_ptr++];
nkeynes@103
   270
    } else {
nkeynes@103
   271
	slot = texcache_evict();
nkeynes@103
   272
    }
nkeynes@103
   273
nkeynes@103
   274
    /* Construct new entry */
nkeynes@103
   275
    texcache_active_list[slot].texture_addr = texture_addr;
nkeynes@103
   276
    texcache_active_list[slot].width = width;
nkeynes@103
   277
    texcache_active_list[slot].height = height;
nkeynes@103
   278
    texcache_active_list[slot].mode = mode;
nkeynes@103
   279
    texcache_active_list[slot].lru_count = texcache_ref_counter++;
nkeynes@103
   280
nkeynes@103
   281
    /* Add entry to the lookup table */
nkeynes@103
   282
    texcache_active_list[slot].next = texcache_page_lookup[texture_page];
nkeynes@103
   283
    texcache_page_lookup[texture_page] = slot;
nkeynes@103
   284
nkeynes@103
   285
    /* Construct the GL texture */
nkeynes@103
   286
    GLuint texid = texcache_free_list[texcache_free_ptr++];
nkeynes@103
   287
    glBindTexture( GL_TEXTURE_2D, texid );
nkeynes@103
   288
    texcache_load_texture( texture_addr, width, height, mode );
nkeynes@103
   289
    
nkeynes@103
   290
    return texcache_active_list[slot].texture_id;
nkeynes@103
   291
}
.