Search
lxdream.org :: lxdream/src/pvr2/texcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/texcache.c
changeset 1075:1a21750d300c
prev1066:ddffe9d2b332
next1135:68daed8f38af
author nkeynes
date Fri Jul 31 13:45:32 2009 +1000 (11 years ago)
permissions -rw-r--r--
last change Remove or change the level of a bunch of INFO messages that shouldn't really
be INFO level
file annotate diff log raw
nkeynes@103
     1
/**
nkeynes@561
     2
 * $Id$
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@647
    22
#include <string.h>
nkeynes@103
    23
#include "pvr2/pvr2.h"
nkeynes@677
    24
#include "pvr2/pvr2mmio.h"
nkeynes@103
    25
nkeynes@103
    26
/** Specifies the maximum number of OpenGL
nkeynes@103
    27
 * textures we're willing to have open at a time. If more are
nkeynes@103
    28
 * needed, textures will be evicted in LRU order.
nkeynes@103
    29
 */
nkeynes@349
    30
#define MAX_TEXTURES 256
nkeynes@103
    31
nkeynes@103
    32
/**
nkeynes@103
    33
 * Data structure:
nkeynes@103
    34
 *
nkeynes@103
    35
 * Main operations:
nkeynes@103
    36
 *    find entry by texture_addr
nkeynes@103
    37
 *    add new entry
nkeynes@103
    38
 *    move entry to tail of lru list
nkeynes@103
    39
 *    remove entry
nkeynes@103
    40
 */
nkeynes@103
    41
nkeynes@103
    42
typedef signed short texcache_entry_index;
nkeynes@462
    43
#define EMPTY_ENTRY -1
nkeynes@103
    44
nkeynes@107
    45
static texcache_entry_index texcache_free_ptr = 0;
nkeynes@103
    46
static GLuint texcache_free_list[MAX_TEXTURES];
nkeynes@103
    47
nkeynes@103
    48
typedef struct texcache_entry {
nkeynes@103
    49
    uint32_t texture_addr;
nkeynes@103
    50
    int width, height, mode;
nkeynes@103
    51
    GLuint texture_id;
nkeynes@856
    52
    render_buffer_t buffer;
nkeynes@103
    53
    texcache_entry_index next;
nkeynes@103
    54
    uint32_t lru_count;
nkeynes@103
    55
} *texcache_entry_t;
nkeynes@103
    56
nkeynes@462
    57
static texcache_entry_index texcache_page_lookup[PVR2_RAM_PAGES];
nkeynes@103
    58
static uint32_t texcache_ref_counter;
nkeynes@103
    59
static struct texcache_entry texcache_active_list[MAX_TEXTURES];
nkeynes@886
    60
static uint32_t texcache_palette_mode;
nkeynes@886
    61
static uint32_t texcache_stride_width;
nkeynes@103
    62
nkeynes@103
    63
/**
nkeynes@108
    64
 * Initialize the texture cache.
nkeynes@103
    65
 */
nkeynes@103
    66
void texcache_init( )
nkeynes@103
    67
{
nkeynes@103
    68
    int i;
nkeynes@103
    69
    for( i=0; i<PVR2_RAM_PAGES; i++ ) {
nkeynes@736
    70
        texcache_page_lookup[i] = EMPTY_ENTRY;
nkeynes@103
    71
    }
nkeynes@103
    72
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
    73
        texcache_free_list[i] = i;
nkeynes@736
    74
        texcache_active_list[i].texture_addr = -1;
nkeynes@856
    75
        texcache_active_list[i].buffer = NULL;
nkeynes@736
    76
        texcache_active_list[i].next = EMPTY_ENTRY;
nkeynes@103
    77
    }
nkeynes@103
    78
    texcache_free_ptr = 0;
nkeynes@103
    79
    texcache_ref_counter = 0;
nkeynes@886
    80
    texcache_palette_mode = 0;
nkeynes@886
    81
    texcache_stride_width = 0;
nkeynes@103
    82
}
nkeynes@103
    83
nkeynes@103
    84
/**
nkeynes@108
    85
 * Setup the initial texture ids (must be called after the GL context is
nkeynes@108
    86
 * prepared)
nkeynes@108
    87
 */
nkeynes@108
    88
void texcache_gl_init( )
nkeynes@108
    89
{
nkeynes@108
    90
    int i;
nkeynes@108
    91
    GLuint texids[MAX_TEXTURES];
nkeynes@108
    92
nkeynes@108
    93
    glGenTextures( MAX_TEXTURES, texids );
nkeynes@108
    94
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
    95
        texcache_active_list[i].texture_id = texids[i];
nkeynes@108
    96
    }
nkeynes@108
    97
}
nkeynes@108
    98
nkeynes@856
    99
void texcache_release_render_buffer( render_buffer_t buffer )
nkeynes@856
   100
{
nkeynes@856
   101
    if( !buffer->flushed ) 
nkeynes@856
   102
        pvr2_render_buffer_copy_to_sh4(buffer);
nkeynes@856
   103
    pvr2_destroy_render_buffer(buffer);
nkeynes@856
   104
}
nkeynes@856
   105
nkeynes@108
   106
/**
nkeynes@103
   107
 * Flush all textures from the cache, returning them to the free list.
nkeynes@103
   108
 */
nkeynes@103
   109
void texcache_flush( )
nkeynes@103
   110
{
nkeynes@103
   111
    int i;
nkeynes@103
   112
    /* clear structures */
nkeynes@103
   113
    for( i=0; i<PVR2_RAM_PAGES; i++ ) {
nkeynes@736
   114
        texcache_page_lookup[i] = EMPTY_ENTRY;
nkeynes@103
   115
    }
nkeynes@103
   116
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   117
        texcache_free_list[i] = i;
nkeynes@736
   118
        texcache_active_list[i].next = EMPTY_ENTRY;
nkeynes@886
   119
        texcache_active_list[i].texture_addr = -1;
nkeynes@856
   120
        if( texcache_active_list[i].buffer != NULL ) {
nkeynes@856
   121
            texcache_release_render_buffer(texcache_active_list[i].buffer);
nkeynes@856
   122
            texcache_active_list[i].buffer = NULL;
nkeynes@856
   123
        }
nkeynes@103
   124
    }
nkeynes@103
   125
    texcache_free_ptr = 0;
nkeynes@103
   126
    texcache_ref_counter = 0;
nkeynes@103
   127
}
nkeynes@103
   128
nkeynes@103
   129
/**
nkeynes@103
   130
 * Flush all textures and delete. The cache will be non-functional until
nkeynes@103
   131
 * the next call to texcache_init(). This would typically be done if
nkeynes@103
   132
 * switching GL targets.
nkeynes@103
   133
 */    
nkeynes@103
   134
void texcache_shutdown( )
nkeynes@103
   135
{
nkeynes@103
   136
    GLuint texids[MAX_TEXTURES];
nkeynes@103
   137
    int i;
nkeynes@103
   138
    texcache_flush();
nkeynes@736
   139
nkeynes@103
   140
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   141
        texids[i] = texcache_active_list[i].texture_id;
nkeynes@103
   142
    }
nkeynes@103
   143
    glDeleteTextures( MAX_TEXTURES, texids );
nkeynes@103
   144
}
nkeynes@103
   145
nkeynes@337
   146
static void texcache_evict( int slot )
nkeynes@103
   147
{
nkeynes@103
   148
    /* Remove the selected slot from the lookup table */
nkeynes@462
   149
    assert( texcache_active_list[slot].texture_addr != -1 );
nkeynes@270
   150
    uint32_t evict_page = texcache_active_list[slot].texture_addr >> 12;
nkeynes@103
   151
    texcache_entry_index replace_next = texcache_active_list[slot].next;
nkeynes@337
   152
    texcache_active_list[slot].texture_addr = -1;
nkeynes@103
   153
    texcache_active_list[slot].next = EMPTY_ENTRY; /* Just for safety */
nkeynes@856
   154
    if( texcache_active_list[slot].buffer != NULL ) {
nkeynes@856
   155
        texcache_release_render_buffer(texcache_active_list[slot].buffer);
nkeynes@856
   156
        texcache_active_list[slot].buffer = NULL;
nkeynes@856
   157
    }
nkeynes@103
   158
    if( texcache_page_lookup[evict_page] == slot ) {
nkeynes@736
   159
        texcache_page_lookup[evict_page] = replace_next;
nkeynes@103
   160
    } else {
nkeynes@736
   161
        texcache_entry_index idx = texcache_page_lookup[evict_page];
nkeynes@736
   162
        texcache_entry_index next;
nkeynes@736
   163
        do {
nkeynes@736
   164
            next = texcache_active_list[idx].next;
nkeynes@736
   165
            if( next == slot ) {
nkeynes@736
   166
                assert( idx != replace_next );
nkeynes@736
   167
                texcache_active_list[idx].next = replace_next;
nkeynes@736
   168
                break;
nkeynes@736
   169
            }
nkeynes@736
   170
            idx = next;
nkeynes@736
   171
        } while( next != EMPTY_ENTRY );
nkeynes@103
   172
    }
nkeynes@337
   173
}
nkeynes@337
   174
nkeynes@337
   175
/**
nkeynes@337
   176
 * Evict a single texture from the cache.
nkeynes@337
   177
 * @return the slot of the evicted texture.
nkeynes@337
   178
 */
nkeynes@337
   179
static texcache_entry_index texcache_evict_lru( void )
nkeynes@337
   180
{
nkeynes@337
   181
    /* Full table scan - take over the entry with the lowest lru value */
nkeynes@337
   182
    texcache_entry_index slot = 0;
nkeynes@337
   183
    int lru_value = texcache_active_list[0].lru_count;
nkeynes@337
   184
    int i;
nkeynes@337
   185
    for( i=1; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   186
        /* FIXME: account for rollover */
nkeynes@736
   187
        if( texcache_active_list[i].lru_count < lru_value ) {
nkeynes@736
   188
            slot = i;
nkeynes@736
   189
            lru_value = texcache_active_list[i].lru_count;
nkeynes@736
   190
        }
nkeynes@337
   191
    }
nkeynes@337
   192
    texcache_evict(slot);
nkeynes@736
   193
nkeynes@103
   194
    return slot;
nkeynes@103
   195
}
nkeynes@103
   196
nkeynes@337
   197
/**
nkeynes@337
   198
 * Evict all textures contained in the page identified by a texture address.
nkeynes@337
   199
 */
nkeynes@337
   200
void texcache_invalidate_page( uint32_t texture_addr ) {
nkeynes@337
   201
    uint32_t texture_page = texture_addr >> 12;
nkeynes@337
   202
    texcache_entry_index idx = texcache_page_lookup[texture_page];
nkeynes@337
   203
    if( idx == EMPTY_ENTRY )
nkeynes@736
   204
        return;
nkeynes@337
   205
    assert( texcache_free_ptr >= 0 );
nkeynes@337
   206
    do {
nkeynes@736
   207
        texcache_entry_t entry = &texcache_active_list[idx];
nkeynes@736
   208
        entry->texture_addr = -1;
nkeynes@856
   209
        if( entry->buffer != NULL ) {
nkeynes@856
   210
            texcache_release_render_buffer(entry->buffer);
nkeynes@856
   211
            entry->buffer = NULL;
nkeynes@856
   212
        }
nkeynes@736
   213
        /* release entry */
nkeynes@736
   214
        texcache_free_ptr--;
nkeynes@736
   215
        texcache_free_list[texcache_free_ptr] = idx;
nkeynes@736
   216
        idx = entry->next;
nkeynes@736
   217
        entry->next = EMPTY_ENTRY;
nkeynes@337
   218
    } while( idx != EMPTY_ENTRY );
nkeynes@337
   219
    texcache_page_lookup[texture_page] = EMPTY_ENTRY;
nkeynes@337
   220
}
nkeynes@337
   221
nkeynes@337
   222
/**
nkeynes@337
   223
 * Mark all textures that use the palette table as needing a re-read (ie 
nkeynes@337
   224
 * for when the palette is changed. We could track exactly which ones are 
nkeynes@337
   225
 * affected, but it's not clear that the extra maintanence overhead is 
nkeynes@337
   226
 * worthwhile.
nkeynes@337
   227
 */
nkeynes@337
   228
void texcache_invalidate_palette( )
nkeynes@337
   229
{
nkeynes@337
   230
    int i;
nkeynes@337
   231
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   232
        if( texcache_active_list[i].texture_addr != -1 &&
nkeynes@736
   233
                PVR2_TEX_IS_PALETTE(texcache_active_list[i].mode) ) {
nkeynes@736
   234
            texcache_evict( i );
nkeynes@736
   235
            texcache_free_ptr--;
nkeynes@736
   236
            texcache_free_list[texcache_free_ptr] = i;
nkeynes@736
   237
        }
nkeynes@337
   238
    }
nkeynes@337
   239
}
nkeynes@886
   240
/**
nkeynes@886
   241
 * Mark all stride textures as needing a re-read (ie when the stride width
nkeynes@886
   242
 * is changed).
nkeynes@886
   243
 */
nkeynes@886
   244
void texcache_invalidate_stride( )
nkeynes@886
   245
{
nkeynes@886
   246
    int i;
nkeynes@886
   247
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@886
   248
        if( texcache_active_list[i].texture_addr != -1 &&
nkeynes@886
   249
                PVR2_TEX_IS_STRIDE(texcache_active_list[i].mode) ) {
nkeynes@886
   250
            texcache_evict( i );
nkeynes@886
   251
            texcache_free_ptr--;
nkeynes@886
   252
            texcache_free_list[texcache_free_ptr] = i;
nkeynes@886
   253
        }
nkeynes@886
   254
    }
nkeynes@886
   255
}
nkeynes@886
   256
nkeynes@886
   257
void texcache_set_config( uint32_t palette_mode, uint32_t stride )
nkeynes@886
   258
{
nkeynes@886
   259
    if( palette_mode != texcache_palette_mode )
nkeynes@886
   260
        texcache_invalidate_palette();
nkeynes@886
   261
    if( stride != texcache_stride_width )
nkeynes@886
   262
        texcache_invalidate_stride();
nkeynes@886
   263
    
nkeynes@886
   264
    texcache_palette_mode = palette_mode;
nkeynes@886
   265
    texcache_stride_width = stride;
nkeynes@886
   266
}
nkeynes@337
   267
nkeynes@311
   268
static void decode_pal8_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@311
   269
{
nkeynes@311
   270
    int i;
nkeynes@311
   271
    for( i=0; i<inbytes; i++ ) {
nkeynes@736
   272
        *out++ = pal[*in++];
nkeynes@129
   273
    }
nkeynes@129
   274
}
nkeynes@129
   275
nkeynes@321
   276
static void decode_pal8_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@311
   277
{
nkeynes@311
   278
    int i;
nkeynes@311
   279
    for( i=0; i<inbytes; i++ ) {
nkeynes@736
   280
        *out++ = (uint16_t)pal[*in++];
nkeynes@113
   281
    }
nkeynes@113
   282
}
nkeynes@113
   283
nkeynes@315
   284
static void decode_pal4_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@315
   285
{
nkeynes@315
   286
    int i;
nkeynes@315
   287
    for( i=0; i<inbytes; i++ ) {
nkeynes@736
   288
        *out++ = pal[*in & 0x0F];
nkeynes@736
   289
        *out++ = pal[(*in >> 4)];
nkeynes@736
   290
        in++;
nkeynes@315
   291
    }
nkeynes@315
   292
}
nkeynes@315
   293
nkeynes@315
   294
nkeynes@321
   295
static void decode_pal4_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@315
   296
{
nkeynes@315
   297
    int i;
nkeynes@315
   298
    for( i=0; i<inbytes; i++ ) {
nkeynes@736
   299
        *out++ = (uint16_t)pal[*in & 0x0F];
nkeynes@736
   300
        *out++ = (uint16_t)pal[(*in >> 4)];
nkeynes@736
   301
        in++;
nkeynes@315
   302
    }
nkeynes@315
   303
}
nkeynes@315
   304
nkeynes@224
   305
#define VQ_CODEBOOK_SIZE 2048 /* 256 entries * 4 pixels per quad * 2 byte pixels */
nkeynes@224
   306
nkeynes@224
   307
struct vq_codebook {
nkeynes@224
   308
    uint16_t quad[256][4];
nkeynes@224
   309
};
nkeynes@224
   310
nkeynes@270
   311
static void vq_get_codebook( struct vq_codebook *codebook, 
nkeynes@736
   312
                             uint16_t *input )
nkeynes@270
   313
{
nkeynes@270
   314
    /* Detwiddle the codebook, for the sake of my own sanity if nothing else */
nkeynes@270
   315
    uint16_t *p = (uint16_t *)input;
nkeynes@270
   316
    int i;
nkeynes@270
   317
    for( i=0; i<256; i++ ) {
nkeynes@736
   318
        codebook->quad[i][0] = *p++;
nkeynes@736
   319
        codebook->quad[i][2] = *p++;
nkeynes@736
   320
        codebook->quad[i][1] = *p++;
nkeynes@736
   321
        codebook->quad[i][3] = *p++;
nkeynes@270
   322
    }
nkeynes@270
   323
}    
nkeynes@270
   324
nkeynes@429
   325
static void vq_decode( uint16_t *output, unsigned char *input, int width, int height, 
nkeynes@736
   326
                       struct vq_codebook *codebook ) {
nkeynes@224
   327
    int i,j;
nkeynes@736
   328
nkeynes@270
   329
    uint8_t *c = (uint8_t *)input;
nkeynes@311
   330
    for( j=0; j<height; j+=2 ) {
nkeynes@736
   331
        for( i=0; i<width; i+=2 ) {
nkeynes@736
   332
            uint8_t code = *c++;
nkeynes@736
   333
            output[i + j*width] = codebook->quad[code][0];
nkeynes@736
   334
            output[i + 1 + j*width] = codebook->quad[code][1];
nkeynes@736
   335
            output[i + (j+1)*width] = codebook->quad[code][2];
nkeynes@736
   336
            output[i + 1 + (j+1)*width] = codebook->quad[code][3];
nkeynes@736
   337
        }
nkeynes@224
   338
    }
nkeynes@224
   339
}
nkeynes@113
   340
nkeynes@282
   341
static inline uint32_t yuv_to_rgb32( float y, float u, float v )
nkeynes@282
   342
{
nkeynes@282
   343
    u -= 128;
nkeynes@282
   344
    v -= 128;
nkeynes@282
   345
    int r = (int)(y + v*1.375);
nkeynes@282
   346
    int g = (int)(y - u*0.34375 - v*0.6875);
nkeynes@282
   347
    int b = (int)(y + u*1.71875);
nkeynes@282
   348
    if( r > 255 ) { r = 255; } else if( r < 0 ) { r = 0; }
nkeynes@282
   349
    if( g > 255 ) { g = 255; } else if( g < 0 ) { g = 0; }
nkeynes@282
   350
    if( b > 255 ) { b = 255; } else if( b < 0 ) { b = 0; }
nkeynes@289
   351
    return 0xFF000000 | (r<<16) | (g<<8) | (b);
nkeynes@282
   352
}
nkeynes@282
   353
nkeynes@282
   354
nkeynes@282
   355
/**
nkeynes@311
   356
 * Convert raster YUV texture data into RGB32 data - most GL implementations don't
nkeynes@282
   357
 * directly support this format unfortunately. The input data is formatted as
nkeynes@282
   358
 * 32 bits = 2 horizontal pixels, UYVY. This is currently done rather inefficiently
nkeynes@282
   359
 * in floating point.
nkeynes@282
   360
 */
nkeynes@311
   361
static void yuv_decode( uint32_t *output, uint32_t *input, int width, int height )
nkeynes@282
   362
{
nkeynes@282
   363
    int x, y;
nkeynes@282
   364
    uint32_t *p = input;
nkeynes@282
   365
    for( y=0; y<height; y++ ) {
nkeynes@736
   366
        for( x=0; x<width; x+=2 ) {
nkeynes@736
   367
            float u = (float)(*p & 0xFF);
nkeynes@736
   368
            float y0 = (float)( (*p>>8)&0xFF );
nkeynes@736
   369
            float v = (float)( (*p>>16)&0xFF );
nkeynes@736
   370
            float y1 = (float)( (*p>>24)&0xFF );
nkeynes@736
   371
            *output++ = yuv_to_rgb32( y0, u, v ); 
nkeynes@736
   372
            *output++ = yuv_to_rgb32( y1, u, v );
nkeynes@736
   373
            p++;
nkeynes@736
   374
        }
nkeynes@282
   375
    }
nkeynes@282
   376
}
nkeynes@282
   377
nkeynes@856
   378
static gboolean is_npot_texture( int width )
nkeynes@856
   379
{
nkeynes@856
   380
    while( width != 0 ) {
nkeynes@856
   381
        if( width & 1 ) 
nkeynes@856
   382
            return width != 1;
nkeynes@856
   383
        width >>= 1;
nkeynes@856
   384
    }
nkeynes@856
   385
    return TRUE;
nkeynes@856
   386
}
nkeynes@856
   387
nkeynes@103
   388
/**
nkeynes@103
   389
 * Load texture data from the given address and parameters into the currently
nkeynes@103
   390
 * bound OpenGL texture.
nkeynes@103
   391
 */
nkeynes@429
   392
static void texcache_load_texture( uint32_t texture_addr, int width, int height,
nkeynes@736
   393
                                   int mode ) {
nkeynes@284
   394
    int bpp_shift = 1; /* bytes per (output) pixel as a power of 2 */
nkeynes@349
   395
    GLint intFormat = GL_RGBA, format, type;
nkeynes@108
   396
    int tex_format = mode & PVR2_TEX_FORMAT_MASK;
nkeynes@270
   397
    struct vq_codebook codebook;
nkeynes@270
   398
    GLint filter = GL_LINEAR;
nkeynes@108
   399
nkeynes@352
   400
    glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
nkeynes@654
   401
nkeynes@270
   402
    /* Decode the format parameters */
nkeynes@270
   403
    switch( tex_format ) {
nkeynes@270
   404
    case PVR2_TEX_FORMAT_IDX4:
nkeynes@270
   405
    case PVR2_TEX_FORMAT_IDX8:
nkeynes@736
   406
        /* For indexed-colour modes, we need to lookup the palette control
nkeynes@736
   407
         * word to determine the de-indexed texture format.
nkeynes@736
   408
         */
nkeynes@886
   409
        switch( texcache_palette_mode ) {
nkeynes@736
   410
        case 0: /* ARGB1555 */
nkeynes@736
   411
            format = GL_BGRA;
nkeynes@736
   412
            type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
nkeynes@736
   413
            break;
nkeynes@736
   414
        case 1:  /* RGB565 */
nkeynes@736
   415
            intFormat = GL_RGB;
nkeynes@736
   416
            format = GL_RGB;
nkeynes@736
   417
            type = GL_UNSIGNED_SHORT_5_6_5;
nkeynes@736
   418
            break;
nkeynes@736
   419
        case 2: /* ARGB4444 */
nkeynes@736
   420
            format = GL_BGRA;
nkeynes@736
   421
            type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
nkeynes@736
   422
            break;
nkeynes@736
   423
        case 3: /* ARGB8888 */
nkeynes@736
   424
            format = GL_BGRA;
nkeynes@736
   425
            type = GL_UNSIGNED_BYTE;
nkeynes@736
   426
            bpp_shift = 2;
nkeynes@736
   427
            break;
nkeynes@736
   428
        default:
nkeynes@736
   429
            return; /* Can't happen, but it makes gcc stop complaining */
nkeynes@736
   430
        }
nkeynes@736
   431
        break;
nkeynes@736
   432
nkeynes@860
   433
        default:
nkeynes@736
   434
        case PVR2_TEX_FORMAT_ARGB1555:
nkeynes@736
   435
            format = GL_BGRA;
nkeynes@736
   436
            type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
nkeynes@736
   437
            break;
nkeynes@736
   438
        case PVR2_TEX_FORMAT_RGB565:
nkeynes@736
   439
            intFormat = GL_RGB;
nkeynes@736
   440
            format = GL_RGB;
nkeynes@736
   441
            type = GL_UNSIGNED_SHORT_5_6_5;
nkeynes@736
   442
            break;
nkeynes@736
   443
        case PVR2_TEX_FORMAT_ARGB4444:
nkeynes@736
   444
            format = GL_BGRA;
nkeynes@736
   445
            type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
nkeynes@736
   446
            break;
nkeynes@736
   447
        case PVR2_TEX_FORMAT_YUV422:
nkeynes@736
   448
            /* YUV422 isn't directly supported by most implementations, so decode
nkeynes@736
   449
             * it to a (reasonably) standard ARGB32.
nkeynes@736
   450
             */
nkeynes@736
   451
            bpp_shift = 2;
nkeynes@736
   452
            format = GL_BGRA;
nkeynes@736
   453
            type = GL_UNSIGNED_BYTE;
nkeynes@736
   454
            break;
nkeynes@736
   455
        case PVR2_TEX_FORMAT_BUMPMAP:
nkeynes@860
   456
            WARN( "Bumpmap not supported" );
nkeynes@736
   457
            return;
nkeynes@270
   458
    }
nkeynes@736
   459
nkeynes@321
   460
    if( PVR2_TEX_IS_STRIDE(mode) && tex_format != PVR2_TEX_FORMAT_IDX4 &&
nkeynes@736
   461
            tex_format != PVR2_TEX_FORMAT_IDX8 ) {
nkeynes@736
   462
        /* Stride textures cannot be mip-mapped, compressed, indexed or twiddled */
nkeynes@736
   463
        unsigned char data[(width*height) << bpp_shift];
nkeynes@736
   464
        if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
nkeynes@736
   465
            unsigned char tmp[(width*height)<<1];
nkeynes@886
   466
            pvr2_vram64_read_stride( tmp, width<<1, texture_addr, texcache_stride_width<<1, height );
nkeynes@736
   467
            yuv_decode( (uint32_t *)data, (uint32_t *)tmp, width, height );
nkeynes@736
   468
        } else {
nkeynes@886
   469
            pvr2_vram64_read_stride( data, width<<bpp_shift, texture_addr, texcache_stride_width<<bpp_shift, height );
nkeynes@736
   470
        }
nkeynes@736
   471
        glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type, data );
nkeynes@736
   472
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
nkeynes@736
   473
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nkeynes@736
   474
        return;
nkeynes@284
   475
    } 
nkeynes@284
   476
nkeynes@270
   477
    if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@736
   478
        uint16_t tmp[VQ_CODEBOOK_SIZE];
nkeynes@736
   479
        pvr2_vram64_read( (unsigned char *)tmp, texture_addr, VQ_CODEBOOK_SIZE );
nkeynes@736
   480
        texture_addr += VQ_CODEBOOK_SIZE;
nkeynes@736
   481
        vq_get_codebook( &codebook, tmp );
nkeynes@270
   482
    }
nkeynes@270
   483
nkeynes@349
   484
    int level=0, last_level = 0, mip_width = width, mip_height = height, src_bytes, dest_bytes;
nkeynes@270
   485
    if( PVR2_TEX_IS_MIPMAPPED(mode) ) {
nkeynes@736
   486
        uint32_t src_offset = 0;
nkeynes@736
   487
        filter = GL_LINEAR_MIPMAP_LINEAR;
nkeynes@736
   488
        mip_height = height = width;
nkeynes@736
   489
        while( (1<<last_level) < width ) {
nkeynes@736
   490
            last_level++;
nkeynes@736
   491
            src_offset += ((width>>last_level)*(width>>last_level));
nkeynes@736
   492
        }
nkeynes@736
   493
        if( width != 1 ) {
nkeynes@736
   494
            src_offset += 3;
nkeynes@736
   495
        }
nkeynes@736
   496
        if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@736
   497
            src_offset >>= 2;
nkeynes@736
   498
        } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
nkeynes@736
   499
            src_offset >>= 1;
nkeynes@736
   500
        } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
nkeynes@736
   501
            src_offset <<= 1;
nkeynes@736
   502
        } else if( tex_format != PVR2_TEX_FORMAT_IDX8 ) {
nkeynes@736
   503
            src_offset <<= bpp_shift;
nkeynes@736
   504
        }
nkeynes@736
   505
        texture_addr += src_offset;
nkeynes@270
   506
    }
nkeynes@736
   507
nkeynes@654
   508
nkeynes@349
   509
    dest_bytes = (mip_width * mip_height) << bpp_shift;
nkeynes@349
   510
    src_bytes = dest_bytes; // Modes will change this (below)
nkeynes@108
   511
nkeynes@654
   512
    for( level=0; level<= last_level; level++ ) {
nkeynes@736
   513
        unsigned char data[dest_bytes];
nkeynes@736
   514
        /* load data from image, detwiddling/uncompressing as required */
nkeynes@736
   515
        if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
nkeynes@736
   516
            src_bytes = (mip_width * mip_height);
nkeynes@736
   517
            int bank = (mode >> 25) &0x03;
nkeynes@736
   518
            uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<8);
nkeynes@736
   519
            unsigned char tmp[src_bytes];
nkeynes@736
   520
            pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width, mip_height );
nkeynes@736
   521
            if( bpp_shift == 2 ) {
nkeynes@736
   522
                decode_pal8_to_32( (uint32_t *)data, tmp, src_bytes, palette );
nkeynes@736
   523
            } else {
nkeynes@736
   524
                decode_pal8_to_16( (uint16_t *)data, tmp, src_bytes, palette );
nkeynes@736
   525
            }
nkeynes@736
   526
        } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
nkeynes@736
   527
            src_bytes = (mip_width * mip_height) >> 1;
nkeynes@736
   528
            int bank = (mode >>21 ) & 0x3F;
nkeynes@736
   529
            uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<4);
nkeynes@736
   530
            unsigned char tmp[src_bytes];
nkeynes@736
   531
            pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
nkeynes@736
   532
            if( bpp_shift == 2 ) {
nkeynes@736
   533
                decode_pal4_to_32( (uint32_t *)data, tmp, src_bytes, palette );
nkeynes@736
   534
            } else {
nkeynes@736
   535
                decode_pal4_to_16( (uint16_t *)data, tmp, src_bytes, palette );
nkeynes@736
   536
            }
nkeynes@736
   537
        } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
nkeynes@736
   538
            src_bytes = ((mip_width*mip_height)<<1);
nkeynes@736
   539
            unsigned char tmp[src_bytes];
nkeynes@736
   540
            if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@736
   541
                pvr2_vram64_read_twiddled_16( tmp, texture_addr, mip_width, mip_height );
nkeynes@736
   542
            } else {
nkeynes@736
   543
                pvr2_vram64_read( tmp, texture_addr, src_bytes );
nkeynes@736
   544
            }
nkeynes@736
   545
            yuv_decode( (uint32_t *)data, (uint32_t *)tmp, mip_width, mip_height );
nkeynes@736
   546
        } else if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@736
   547
            src_bytes = ((mip_width*mip_height) >> 2);
nkeynes@736
   548
            unsigned char tmp[src_bytes];
nkeynes@736
   549
            if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@736
   550
                pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width>>1, mip_height>>1 );
nkeynes@736
   551
            } else {
nkeynes@736
   552
                pvr2_vram64_read( tmp, texture_addr, src_bytes );
nkeynes@736
   553
            }
nkeynes@736
   554
            vq_decode( (uint16_t *)data, tmp, mip_width, mip_height, &codebook );
nkeynes@736
   555
        } else if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@736
   556
            pvr2_vram64_read_twiddled_16( data, texture_addr, mip_width, mip_height );
nkeynes@736
   557
        } else {
nkeynes@736
   558
            pvr2_vram64_read( data, texture_addr, src_bytes );
nkeynes@736
   559
        }
nkeynes@270
   560
nkeynes@736
   561
        /* Pass to GL */
nkeynes@736
   562
        if( level == last_level && level != 0 ) { /* 1x1 stored within a 2x2 */
nkeynes@736
   563
            glTexImage2D( GL_TEXTURE_2D, level, intFormat, 1, 1, 0, format, type,
nkeynes@736
   564
                    data + (3 << bpp_shift) );
nkeynes@736
   565
        } else {
nkeynes@736
   566
            glTexImage2D( GL_TEXTURE_2D, level, intFormat, mip_width, mip_height, 0, format, type,
nkeynes@736
   567
                    data );
nkeynes@736
   568
            if( mip_width > 2 ) {
nkeynes@736
   569
                mip_width >>= 1;
nkeynes@736
   570
                mip_height >>= 1;
nkeynes@736
   571
                dest_bytes >>= 2;
nkeynes@736
   572
                src_bytes >>= 2;
nkeynes@736
   573
            }
nkeynes@736
   574
            texture_addr -= src_bytes;
nkeynes@736
   575
        }
nkeynes@103
   576
    }
nkeynes@270
   577
nkeynes@270
   578
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
nkeynes@108
   579
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nkeynes@103
   580
}
nkeynes@103
   581
nkeynes@856
   582
static int texcache_find_texture_slot( uint32_t texture_word, int width, int height )
nkeynes@103
   583
{
nkeynes@653
   584
    uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
nkeynes@103
   585
    uint32_t texture_page = texture_addr >> 12;
nkeynes@462
   586
    texcache_entry_index next;
nkeynes@103
   587
    texcache_entry_index idx = texcache_page_lookup[texture_page];
nkeynes@103
   588
    while( idx != EMPTY_ENTRY ) {
nkeynes@736
   589
        texcache_entry_t entry = &texcache_active_list[idx];
nkeynes@736
   590
        if( entry->texture_addr == texture_addr &&
nkeynes@736
   591
                entry->mode == texture_word &&
nkeynes@736
   592
                entry->width == width &&
nkeynes@736
   593
                entry->height == height ) {
nkeynes@736
   594
            entry->lru_count = texcache_ref_counter++;
nkeynes@856
   595
            return idx;
nkeynes@736
   596
        }
nkeynes@103
   597
        idx = entry->next;
nkeynes@103
   598
    }
nkeynes@856
   599
    return -1;
nkeynes@856
   600
}
nkeynes@103
   601
nkeynes@856
   602
static int texcache_alloc_texture_slot( uint32_t texture_word, int width, int height )
nkeynes@856
   603
{
nkeynes@856
   604
    uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
nkeynes@856
   605
    uint32_t texture_page = texture_addr >> 12;
nkeynes@462
   606
    texcache_entry_index slot = 0;
nkeynes@103
   607
nkeynes@103
   608
    if( texcache_free_ptr < MAX_TEXTURES ) {
nkeynes@736
   609
        slot = texcache_free_list[texcache_free_ptr++];
nkeynes@103
   610
    } else {
nkeynes@736
   611
        slot = texcache_evict_lru();
nkeynes@103
   612
    }
nkeynes@103
   613
nkeynes@103
   614
    /* Construct new entry */
nkeynes@886
   615
    assert( texcache_active_list[slot].texture_addr == -1 );
nkeynes@103
   616
    texcache_active_list[slot].texture_addr = texture_addr;
nkeynes@103
   617
    texcache_active_list[slot].width = width;
nkeynes@103
   618
    texcache_active_list[slot].height = height;
nkeynes@653
   619
    texcache_active_list[slot].mode = texture_word;
nkeynes@103
   620
    texcache_active_list[slot].lru_count = texcache_ref_counter++;
nkeynes@103
   621
nkeynes@103
   622
    /* Add entry to the lookup table */
nkeynes@856
   623
    int next = texcache_page_lookup[texture_page];
nkeynes@462
   624
    if( next == slot ) {
nkeynes@736
   625
        int i;
nkeynes@736
   626
        fprintf( stderr, "Active list: " );
nkeynes@736
   627
        for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   628
            fprintf( stderr, "%d, ", texcache_active_list[i].next );
nkeynes@736
   629
        }
nkeynes@736
   630
        fprintf( stderr, "\n" );
nkeynes@736
   631
        assert( next != slot );
nkeynes@736
   632
nkeynes@462
   633
    }
nkeynes@462
   634
    texcache_active_list[slot].next = next;
nkeynes@103
   635
    texcache_page_lookup[texture_page] = slot;
nkeynes@856
   636
    return slot;
nkeynes@856
   637
}
nkeynes@103
   638
nkeynes@103
   639
/**
nkeynes@103
   640
 * Return a texture ID for the texture specified at the supplied address
nkeynes@103
   641
 * and given parameters (the same sequence of bytes could in theory have
nkeynes@103
   642
 * multiple interpretations). We use the texture address as the primary
nkeynes@103
   643
 * index, but allow for multiple instances at each address. The texture
nkeynes@103
   644
 * will be bound to the GL_TEXTURE_2D target before being returned.
nkeynes@103
   645
 * 
nkeynes@103
   646
 * If the texture has already been bound, return the ID to which it was
nkeynes@103
   647
 * bound. Otherwise obtain an unused texture ID and set it up appropriately.
nkeynes@103
   648
 */
nkeynes@645
   649
GLuint texcache_get_texture( uint32_t texture_word, int width, int height )
nkeynes@103
   650
{
nkeynes@856
   651
    int slot = texcache_find_texture_slot( texture_word, width, height );
nkeynes@856
   652
nkeynes@856
   653
    if( slot == -1 ) {
nkeynes@856
   654
        /* Not found - check the free list */
nkeynes@856
   655
        slot = texcache_alloc_texture_slot( texture_word, width, height );
nkeynes@856
   656
        
nkeynes@856
   657
        /* Construct the GL texture */
nkeynes@856
   658
        uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
nkeynes@856
   659
        glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
nkeynes@856
   660
        texcache_load_texture( texture_addr, width, height, texture_word );
nkeynes@103
   661
    }
nkeynes@103
   662
nkeynes@103
   663
    return texcache_active_list[slot].texture_id;
nkeynes@103
   664
}
nkeynes@653
   665
nkeynes@856
   666
render_buffer_t texcache_get_render_buffer( uint32_t texture_addr, int mode, int width, int height )
nkeynes@856
   667
{
nkeynes@856
   668
    uint32_t texture_word = ((texture_addr >> 3) & 0x000FFFFF) | PVR2_TEX_UNTWIDDLED;
nkeynes@856
   669
    switch( mode ) {
nkeynes@856
   670
    case COLFMT_BGRA1555: texture_word |= PVR2_TEX_FORMAT_ARGB1555; break;
nkeynes@856
   671
    case COLFMT_RGB565:   texture_word |= PVR2_TEX_FORMAT_RGB565; break;
nkeynes@856
   672
    case COLFMT_BGRA4444: texture_word |= PVR2_TEX_FORMAT_ARGB4444; break;
nkeynes@856
   673
    default:
nkeynes@856
   674
        WARN( "Rendering to non-texture colour format" );
nkeynes@856
   675
    }
nkeynes@856
   676
    if( is_npot_texture(width) )
nkeynes@856
   677
        texture_word |= PVR2_TEX_STRIDE;
nkeynes@635
   678
    
nkeynes@856
   679
    
nkeynes@856
   680
    int slot = texcache_find_texture_slot( texture_word, width, height );
nkeynes@856
   681
    if( slot == -1 ) {
nkeynes@856
   682
        slot = texcache_alloc_texture_slot( texture_word, width, height );
nkeynes@856
   683
    }
nkeynes@856
   684
    
nkeynes@856
   685
    texcache_entry_t entry = &texcache_active_list[slot];
nkeynes@103
   686
nkeynes@870
   687
    if( entry->buffer == NULL ) {
nkeynes@856
   688
        entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
nkeynes@870
   689
    } else if( entry->buffer->width != width || entry->buffer->height != height ) {        
nkeynes@870
   690
        texcache_release_render_buffer(entry->buffer);
nkeynes@870
   691
        entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
nkeynes@103
   692
    }
nkeynes@103
   693
nkeynes@856
   694
    return entry->buffer;
nkeynes@103
   695
}
nkeynes@635
   696
nkeynes@635
   697
/**
nkeynes@635
   698
 * Check the integrity of the texcache. Verifies that every cache slot
nkeynes@635
   699
 * appears exactly once on either the free list or one page list. For 
nkeynes@635
   700
 * active slots, the texture address must also match the page it appears on.
nkeynes@635
   701
 * 
nkeynes@635
   702
 */
nkeynes@635
   703
void texcache_integrity_check()
nkeynes@635
   704
{
nkeynes@635
   705
    int i;
nkeynes@635
   706
    int slot_found[MAX_TEXTURES];
nkeynes@736
   707
nkeynes@635
   708
    memset( slot_found, 0, sizeof(slot_found) );
nkeynes@635
   709
nkeynes@635
   710
    /* Check entries on the free list */
nkeynes@635
   711
    for( i= texcache_free_ptr; i< MAX_TEXTURES; i++ ) {
nkeynes@736
   712
        int slot = texcache_free_list[i];
nkeynes@736
   713
        assert( slot_found[slot] == 0 );
nkeynes@736
   714
        assert( texcache_active_list[slot].next == EMPTY_ENTRY );
nkeynes@736
   715
        slot_found[slot] = 1;
nkeynes@635
   716
    }
nkeynes@635
   717
nkeynes@635
   718
    /* Check entries on the active lists */
nkeynes@635
   719
    for( i=0; i< PVR2_RAM_PAGES; i++ ) {
nkeynes@736
   720
        int slot = texcache_page_lookup[i];
nkeynes@736
   721
        while( slot != EMPTY_ENTRY ) {
nkeynes@736
   722
            assert( slot_found[slot] == 0 );
nkeynes@736
   723
            assert( (texcache_active_list[slot].texture_addr >> 12) == i );
nkeynes@736
   724
            slot_found[slot] = 2;
nkeynes@736
   725
            slot = texcache_active_list[slot].next;
nkeynes@736
   726
        }
nkeynes@635
   727
    }
nkeynes@635
   728
nkeynes@635
   729
    /* Make sure we didn't miss any entries */
nkeynes@635
   730
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   731
        assert( slot_found[i] != 0 );
nkeynes@635
   732
    }
nkeynes@635
   733
}
.