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