Search
lxdream.org :: lxdream/src/pvr2/texcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/texcache.c
changeset 1210:79bbd30363f4
prev1165:13406c35849b
next1221:f50407acc682
author nkeynes
date Tue Feb 07 14:27:13 2012 +1000 (9 years ago)
permissions -rw-r--r--
last change Add wrangling for some of the core vs EXT/ARB versions of functions + macros
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@1140
    25
#include "pvr2/glutil.h"
nkeynes@103
    26
nkeynes@103
    27
/** Specifies the maximum number of OpenGL
nkeynes@103
    28
 * textures we're willing to have open at a time. If more are
nkeynes@103
    29
 * needed, textures will be evicted in LRU order.
nkeynes@103
    30
 */
nkeynes@349
    31
#define MAX_TEXTURES 256
nkeynes@103
    32
nkeynes@103
    33
/**
nkeynes@103
    34
 * Data structure:
nkeynes@103
    35
 *
nkeynes@103
    36
 * Main operations:
nkeynes@103
    37
 *    find entry by texture_addr
nkeynes@103
    38
 *    add new entry
nkeynes@103
    39
 *    move entry to tail of lru list
nkeynes@103
    40
 *    remove entry
nkeynes@103
    41
 */
nkeynes@103
    42
nkeynes@103
    43
typedef signed short texcache_entry_index;
nkeynes@462
    44
#define EMPTY_ENTRY -1
nkeynes@103
    45
nkeynes@107
    46
static texcache_entry_index texcache_free_ptr = 0;
nkeynes@103
    47
static GLuint texcache_free_list[MAX_TEXTURES];
nkeynes@103
    48
nkeynes@103
    49
typedef struct texcache_entry {
nkeynes@103
    50
    uint32_t texture_addr;
nkeynes@1135
    51
    uint32_t poly2_mode, tex_mode;
nkeynes@103
    52
    GLuint texture_id;
nkeynes@856
    53
    render_buffer_t buffer;
nkeynes@103
    54
    texcache_entry_index next;
nkeynes@103
    55
    uint32_t lru_count;
nkeynes@103
    56
} *texcache_entry_t;
nkeynes@103
    57
nkeynes@462
    58
static texcache_entry_index texcache_page_lookup[PVR2_RAM_PAGES];
nkeynes@103
    59
static uint32_t texcache_ref_counter;
nkeynes@103
    60
static struct texcache_entry texcache_active_list[MAX_TEXTURES];
nkeynes@886
    61
static uint32_t texcache_palette_mode;
nkeynes@886
    62
static uint32_t texcache_stride_width;
nkeynes@1140
    63
static gboolean texcache_have_palette_shader;
nkeynes@1140
    64
static gboolean texcache_palette_valid;
nkeynes@1140
    65
static GLuint texcache_palette_texid;
nkeynes@103
    66
nkeynes@103
    67
/**
nkeynes@108
    68
 * Initialize the texture cache.
nkeynes@103
    69
 */
nkeynes@103
    70
void texcache_init( )
nkeynes@103
    71
{
nkeynes@103
    72
    int i;
nkeynes@103
    73
    for( i=0; i<PVR2_RAM_PAGES; i++ ) {
nkeynes@736
    74
        texcache_page_lookup[i] = EMPTY_ENTRY;
nkeynes@103
    75
    }
nkeynes@103
    76
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
    77
        texcache_free_list[i] = i;
nkeynes@736
    78
        texcache_active_list[i].texture_addr = -1;
nkeynes@856
    79
        texcache_active_list[i].buffer = NULL;
nkeynes@736
    80
        texcache_active_list[i].next = EMPTY_ENTRY;
nkeynes@103
    81
    }
nkeynes@103
    82
    texcache_free_ptr = 0;
nkeynes@103
    83
    texcache_ref_counter = 0;
nkeynes@1140
    84
    texcache_palette_mode = -1;
nkeynes@886
    85
    texcache_stride_width = 0;
nkeynes@103
    86
}
nkeynes@103
    87
nkeynes@103
    88
/**
nkeynes@108
    89
 * Setup the initial texture ids (must be called after the GL context is
nkeynes@108
    90
 * prepared)
nkeynes@108
    91
 */
nkeynes@108
    92
void texcache_gl_init( )
nkeynes@108
    93
{
nkeynes@108
    94
    int i;
nkeynes@108
    95
    GLuint texids[MAX_TEXTURES];
nkeynes@108
    96
nkeynes@1142
    97
    if( glsl_is_supported() && isGLMultitextureSupported() ) {
nkeynes@1140
    98
        texcache_have_palette_shader = TRUE;
nkeynes@1140
    99
        texcache_palette_valid = FALSE;
nkeynes@1140
   100
        glGenTextures(1, &texcache_palette_texid );
nkeynes@1140
   101
nkeynes@1140
   102
        /* Bind the texture and set the params */
nkeynes@1140
   103
        glActiveTexture(GL_TEXTURE1);
nkeynes@1140
   104
        glBindTexture(GL_TEXTURE_1D, texcache_palette_texid);
nkeynes@1140
   105
        glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nkeynes@1140
   106
        glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nkeynes@1140
   107
        glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP );
nkeynes@1140
   108
        glActiveTexture(GL_TEXTURE0);
nkeynes@1140
   109
nkeynes@1140
   110
    } else {
nkeynes@1140
   111
        texcache_have_palette_shader = FALSE;
nkeynes@1140
   112
    }
nkeynes@1140
   113
nkeynes@108
   114
    glGenTextures( MAX_TEXTURES, texids );
nkeynes@108
   115
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   116
        texcache_active_list[i].texture_id = texids[i];
nkeynes@108
   117
    }
nkeynes@108
   118
}
nkeynes@108
   119
nkeynes@856
   120
void texcache_release_render_buffer( render_buffer_t buffer )
nkeynes@856
   121
{
nkeynes@856
   122
    if( !buffer->flushed ) 
nkeynes@856
   123
        pvr2_render_buffer_copy_to_sh4(buffer);
nkeynes@856
   124
    pvr2_destroy_render_buffer(buffer);
nkeynes@856
   125
}
nkeynes@856
   126
nkeynes@108
   127
/**
nkeynes@103
   128
 * Flush all textures from the cache, returning them to the free list.
nkeynes@103
   129
 */
nkeynes@103
   130
void texcache_flush( )
nkeynes@103
   131
{
nkeynes@103
   132
    int i;
nkeynes@103
   133
    /* clear structures */
nkeynes@103
   134
    for( i=0; i<PVR2_RAM_PAGES; i++ ) {
nkeynes@736
   135
        texcache_page_lookup[i] = EMPTY_ENTRY;
nkeynes@103
   136
    }
nkeynes@103
   137
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   138
        texcache_free_list[i] = i;
nkeynes@736
   139
        texcache_active_list[i].next = EMPTY_ENTRY;
nkeynes@886
   140
        texcache_active_list[i].texture_addr = -1;
nkeynes@856
   141
        if( texcache_active_list[i].buffer != NULL ) {
nkeynes@856
   142
            texcache_release_render_buffer(texcache_active_list[i].buffer);
nkeynes@856
   143
            texcache_active_list[i].buffer = NULL;
nkeynes@856
   144
        }
nkeynes@103
   145
    }
nkeynes@103
   146
    texcache_free_ptr = 0;
nkeynes@103
   147
    texcache_ref_counter = 0;
nkeynes@103
   148
}
nkeynes@103
   149
nkeynes@103
   150
/**
nkeynes@103
   151
 * Flush all textures and delete. The cache will be non-functional until
nkeynes@103
   152
 * the next call to texcache_init(). This would typically be done if
nkeynes@103
   153
 * switching GL targets.
nkeynes@103
   154
 */    
nkeynes@103
   155
void texcache_shutdown( )
nkeynes@103
   156
{
nkeynes@103
   157
    GLuint texids[MAX_TEXTURES];
nkeynes@103
   158
    int i;
nkeynes@103
   159
    texcache_flush();
nkeynes@736
   160
nkeynes@1140
   161
    if( texcache_have_palette_shader )
nkeynes@1140
   162
        glDeleteTextures( 1, &texcache_palette_texid );
nkeynes@1140
   163
nkeynes@103
   164
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   165
        texids[i] = texcache_active_list[i].texture_id;
nkeynes@103
   166
    }
nkeynes@103
   167
    glDeleteTextures( MAX_TEXTURES, texids );
nkeynes@103
   168
}
nkeynes@103
   169
nkeynes@337
   170
static void texcache_evict( int slot )
nkeynes@103
   171
{
nkeynes@103
   172
    /* Remove the selected slot from the lookup table */
nkeynes@462
   173
    assert( texcache_active_list[slot].texture_addr != -1 );
nkeynes@270
   174
    uint32_t evict_page = texcache_active_list[slot].texture_addr >> 12;
nkeynes@103
   175
    texcache_entry_index replace_next = texcache_active_list[slot].next;
nkeynes@337
   176
    texcache_active_list[slot].texture_addr = -1;
nkeynes@103
   177
    texcache_active_list[slot].next = EMPTY_ENTRY; /* Just for safety */
nkeynes@856
   178
    if( texcache_active_list[slot].buffer != NULL ) {
nkeynes@856
   179
        texcache_release_render_buffer(texcache_active_list[slot].buffer);
nkeynes@856
   180
        texcache_active_list[slot].buffer = NULL;
nkeynes@856
   181
    }
nkeynes@103
   182
    if( texcache_page_lookup[evict_page] == slot ) {
nkeynes@736
   183
        texcache_page_lookup[evict_page] = replace_next;
nkeynes@103
   184
    } else {
nkeynes@736
   185
        texcache_entry_index idx = texcache_page_lookup[evict_page];
nkeynes@736
   186
        texcache_entry_index next;
nkeynes@736
   187
        do {
nkeynes@736
   188
            next = texcache_active_list[idx].next;
nkeynes@736
   189
            if( next == slot ) {
nkeynes@736
   190
                assert( idx != replace_next );
nkeynes@736
   191
                texcache_active_list[idx].next = replace_next;
nkeynes@736
   192
                break;
nkeynes@736
   193
            }
nkeynes@736
   194
            idx = next;
nkeynes@736
   195
        } while( next != EMPTY_ENTRY );
nkeynes@103
   196
    }
nkeynes@337
   197
}
nkeynes@337
   198
nkeynes@337
   199
/**
nkeynes@337
   200
 * Evict a single texture from the cache.
nkeynes@337
   201
 * @return the slot of the evicted texture.
nkeynes@337
   202
 */
nkeynes@337
   203
static texcache_entry_index texcache_evict_lru( void )
nkeynes@337
   204
{
nkeynes@337
   205
    /* Full table scan - take over the entry with the lowest lru value */
nkeynes@337
   206
    texcache_entry_index slot = 0;
nkeynes@337
   207
    int lru_value = texcache_active_list[0].lru_count;
nkeynes@337
   208
    int i;
nkeynes@337
   209
    for( i=1; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   210
        /* FIXME: account for rollover */
nkeynes@736
   211
        if( texcache_active_list[i].lru_count < lru_value ) {
nkeynes@736
   212
            slot = i;
nkeynes@736
   213
            lru_value = texcache_active_list[i].lru_count;
nkeynes@736
   214
        }
nkeynes@337
   215
    }
nkeynes@337
   216
    texcache_evict(slot);
nkeynes@736
   217
nkeynes@103
   218
    return slot;
nkeynes@103
   219
}
nkeynes@103
   220
nkeynes@337
   221
/**
nkeynes@337
   222
 * Evict all textures contained in the page identified by a texture address.
nkeynes@337
   223
 */
nkeynes@337
   224
void texcache_invalidate_page( uint32_t texture_addr ) {
nkeynes@337
   225
    uint32_t texture_page = texture_addr >> 12;
nkeynes@337
   226
    texcache_entry_index idx = texcache_page_lookup[texture_page];
nkeynes@337
   227
    if( idx == EMPTY_ENTRY )
nkeynes@736
   228
        return;
nkeynes@337
   229
    assert( texcache_free_ptr >= 0 );
nkeynes@337
   230
    do {
nkeynes@736
   231
        texcache_entry_t entry = &texcache_active_list[idx];
nkeynes@736
   232
        entry->texture_addr = -1;
nkeynes@856
   233
        if( entry->buffer != NULL ) {
nkeynes@856
   234
            texcache_release_render_buffer(entry->buffer);
nkeynes@856
   235
            entry->buffer = NULL;
nkeynes@856
   236
        }
nkeynes@736
   237
        /* release entry */
nkeynes@736
   238
        texcache_free_ptr--;
nkeynes@736
   239
        texcache_free_list[texcache_free_ptr] = idx;
nkeynes@736
   240
        idx = entry->next;
nkeynes@736
   241
        entry->next = EMPTY_ENTRY;
nkeynes@337
   242
    } while( idx != EMPTY_ENTRY );
nkeynes@337
   243
    texcache_page_lookup[texture_page] = EMPTY_ENTRY;
nkeynes@337
   244
}
nkeynes@337
   245
nkeynes@337
   246
/**
nkeynes@1140
   247
 * Load the palette into 4 textures of 256 entries each. This mirrors the
nkeynes@1140
   248
 * banking done by the PVR2 for 8-bit textures, and also ensures that we
nkeynes@1140
   249
 * can use 8-bit paletted textures ourselves.
nkeynes@1140
   250
 */
nkeynes@1140
   251
static void texcache_load_palette_texture( gboolean format_changed )
nkeynes@1140
   252
{
nkeynes@1140
   253
    GLint format, type, intFormat = GL_RGBA;
nkeynes@1140
   254
    unsigned i;
nkeynes@1140
   255
    int bpp = 2;
nkeynes@1140
   256
    uint32_t *palette = (uint32_t *)mmio_region_PVR2PAL.mem;
nkeynes@1140
   257
    uint16_t packed_palette[1024];
nkeynes@1140
   258
    char *data = (char *)palette;
nkeynes@1140
   259
nkeynes@1140
   260
    switch( texcache_palette_mode ) {
nkeynes@1140
   261
    case 0: /* ARGB1555 */
nkeynes@1140
   262
        format = GL_BGRA;
nkeynes@1140
   263
        type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
nkeynes@1140
   264
        break;
nkeynes@1140
   265
    case 1:  /* RGB565 */
nkeynes@1140
   266
        intFormat = GL_RGB;
nkeynes@1140
   267
        format = GL_RGB;
nkeynes@1140
   268
        type = GL_UNSIGNED_SHORT_5_6_5;
nkeynes@1140
   269
        break;
nkeynes@1140
   270
    case 2: /* ARGB4444 */
nkeynes@1140
   271
        format = GL_BGRA;
nkeynes@1140
   272
        type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
nkeynes@1140
   273
        break;
nkeynes@1140
   274
    case 3: /* ARGB8888 */
nkeynes@1140
   275
        format = GL_BGRA;
nkeynes@1140
   276
        type = GL_UNSIGNED_BYTE;
nkeynes@1140
   277
        bpp = 4;
nkeynes@1140
   278
        break;
nkeynes@1140
   279
    default:
nkeynes@1140
   280
        break; /* Can't happen */
nkeynes@1140
   281
    }
nkeynes@1140
   282
nkeynes@1140
   283
nkeynes@1140
   284
    if( bpp == 2 ) {
nkeynes@1140
   285
        for( i=0; i<1024; i++ ) {
nkeynes@1140
   286
            packed_palette[i] = (uint16_t)palette[i];
nkeynes@1140
   287
        }
nkeynes@1140
   288
        data = (char *)packed_palette;
nkeynes@1140
   289
nkeynes@1140
   290
    }
nkeynes@1140
   291
nkeynes@1140
   292
    glActiveTexture(GL_TEXTURE1);
nkeynes@1140
   293
//    glBindTexture(GL_TEXTURE_1D, texcache_palette_texid);
nkeynes@1140
   294
    if( format_changed )
nkeynes@1140
   295
        glTexImage1D(GL_TEXTURE_1D, 0, intFormat, 1024, 0, format, type, data );
nkeynes@1140
   296
    else
nkeynes@1140
   297
        glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 1024, format, type, data);
nkeynes@1140
   298
//    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nkeynes@1140
   299
//    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nkeynes@1140
   300
//    glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP );
nkeynes@1140
   301
    glActiveTexture(GL_TEXTURE0);
nkeynes@1140
   302
    texcache_palette_valid = TRUE;
nkeynes@1140
   303
}
nkeynes@1140
   304
nkeynes@1140
   305
nkeynes@1140
   306
/**
nkeynes@1140
   307
 * Mark the palette as having changed. If we have palette support (via shaders)
nkeynes@1140
   308
 * we just flag the palette, otherwise we have to invalidate all palette
nkeynes@1140
   309
 * textures.
nkeynes@337
   310
 */
nkeynes@337
   311
void texcache_invalidate_palette( )
nkeynes@337
   312
{
nkeynes@1140
   313
    if( texcache_have_palette_shader ) {
nkeynes@1140
   314
        texcache_palette_valid = FALSE;
nkeynes@1140
   315
    } else {
nkeynes@1140
   316
        int i;
nkeynes@1140
   317
        for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@1140
   318
            if( texcache_active_list[i].texture_addr != -1 &&
nkeynes@1140
   319
                    PVR2_TEX_IS_PALETTE(texcache_active_list[i].tex_mode) ) {
nkeynes@1140
   320
                texcache_evict( i );
nkeynes@1140
   321
                texcache_free_ptr--;
nkeynes@1140
   322
                texcache_free_list[texcache_free_ptr] = i;
nkeynes@1140
   323
            }
nkeynes@736
   324
        }
nkeynes@337
   325
    }
nkeynes@337
   326
}
nkeynes@886
   327
/**
nkeynes@886
   328
 * Mark all stride textures as needing a re-read (ie when the stride width
nkeynes@886
   329
 * is changed).
nkeynes@886
   330
 */
nkeynes@886
   331
void texcache_invalidate_stride( )
nkeynes@886
   332
{
nkeynes@886
   333
    int i;
nkeynes@886
   334
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@886
   335
        if( texcache_active_list[i].texture_addr != -1 &&
nkeynes@1135
   336
                PVR2_TEX_IS_STRIDE(texcache_active_list[i].tex_mode) ) {
nkeynes@886
   337
            texcache_evict( i );
nkeynes@886
   338
            texcache_free_ptr--;
nkeynes@886
   339
            texcache_free_list[texcache_free_ptr] = i;
nkeynes@886
   340
        }
nkeynes@886
   341
    }
nkeynes@886
   342
}
nkeynes@886
   343
nkeynes@1135
   344
void texcache_begin_scene( uint32_t palette_mode, uint32_t stride )
nkeynes@886
   345
{
nkeynes@1140
   346
    gboolean format_changed = FALSE;
nkeynes@1140
   347
    if( palette_mode != texcache_palette_mode ) {
nkeynes@886
   348
        texcache_invalidate_palette();
nkeynes@1140
   349
        format_changed = TRUE;
nkeynes@1140
   350
    }
nkeynes@886
   351
    if( stride != texcache_stride_width )
nkeynes@886
   352
        texcache_invalidate_stride();
nkeynes@886
   353
    
nkeynes@886
   354
    texcache_palette_mode = palette_mode;
nkeynes@886
   355
    texcache_stride_width = stride;
nkeynes@1140
   356
nkeynes@1140
   357
    if( !texcache_palette_valid && texcache_have_palette_shader )
nkeynes@1140
   358
        texcache_load_palette_texture(format_changed);
nkeynes@886
   359
}
nkeynes@337
   360
nkeynes@311
   361
static void decode_pal8_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@311
   362
{
nkeynes@311
   363
    int i;
nkeynes@311
   364
    for( i=0; i<inbytes; i++ ) {
nkeynes@736
   365
        *out++ = pal[*in++];
nkeynes@129
   366
    }
nkeynes@129
   367
}
nkeynes@129
   368
nkeynes@321
   369
static void decode_pal8_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@311
   370
{
nkeynes@311
   371
    int i;
nkeynes@311
   372
    for( i=0; i<inbytes; i++ ) {
nkeynes@736
   373
        *out++ = (uint16_t)pal[*in++];
nkeynes@113
   374
    }
nkeynes@113
   375
}
nkeynes@113
   376
nkeynes@315
   377
static void decode_pal4_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@315
   378
{
nkeynes@315
   379
    int i;
nkeynes@315
   380
    for( i=0; i<inbytes; i++ ) {
nkeynes@736
   381
        *out++ = pal[*in & 0x0F];
nkeynes@736
   382
        *out++ = pal[(*in >> 4)];
nkeynes@736
   383
        in++;
nkeynes@315
   384
    }
nkeynes@315
   385
}
nkeynes@315
   386
nkeynes@1140
   387
static void decode_pal4_to_pal8( uint8_t *out, uint8_t *in, int inbytes )
nkeynes@1140
   388
{
nkeynes@1140
   389
    int i;
nkeynes@1140
   390
    for( i=0; i<inbytes; i++ ) {
nkeynes@1140
   391
        *out++ = (uint8_t)(*in & 0x0F);
nkeynes@1140
   392
        *out++ = (uint8_t)(*in >> 4);
nkeynes@1140
   393
        in++;
nkeynes@1140
   394
    }
nkeynes@1140
   395
}
nkeynes@1140
   396
nkeynes@1140
   397
nkeynes@315
   398
nkeynes@321
   399
static void decode_pal4_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@315
   400
{
nkeynes@315
   401
    int i;
nkeynes@315
   402
    for( i=0; i<inbytes; i++ ) {
nkeynes@736
   403
        *out++ = (uint16_t)pal[*in & 0x0F];
nkeynes@736
   404
        *out++ = (uint16_t)pal[(*in >> 4)];
nkeynes@736
   405
        in++;
nkeynes@315
   406
    }
nkeynes@315
   407
}
nkeynes@315
   408
nkeynes@224
   409
#define VQ_CODEBOOK_SIZE 2048 /* 256 entries * 4 pixels per quad * 2 byte pixels */
nkeynes@224
   410
nkeynes@224
   411
struct vq_codebook {
nkeynes@224
   412
    uint16_t quad[256][4];
nkeynes@224
   413
};
nkeynes@224
   414
nkeynes@270
   415
static void vq_get_codebook( struct vq_codebook *codebook, 
nkeynes@736
   416
                             uint16_t *input )
nkeynes@270
   417
{
nkeynes@270
   418
    /* Detwiddle the codebook, for the sake of my own sanity if nothing else */
nkeynes@270
   419
    uint16_t *p = (uint16_t *)input;
nkeynes@270
   420
    int i;
nkeynes@270
   421
    for( i=0; i<256; i++ ) {
nkeynes@736
   422
        codebook->quad[i][0] = *p++;
nkeynes@736
   423
        codebook->quad[i][2] = *p++;
nkeynes@736
   424
        codebook->quad[i][1] = *p++;
nkeynes@736
   425
        codebook->quad[i][3] = *p++;
nkeynes@270
   426
    }
nkeynes@270
   427
}    
nkeynes@270
   428
nkeynes@429
   429
static void vq_decode( uint16_t *output, unsigned char *input, int width, int height, 
nkeynes@736
   430
                       struct vq_codebook *codebook ) {
nkeynes@224
   431
    int i,j;
nkeynes@736
   432
nkeynes@270
   433
    uint8_t *c = (uint8_t *)input;
nkeynes@311
   434
    for( j=0; j<height; j+=2 ) {
nkeynes@736
   435
        for( i=0; i<width; i+=2 ) {
nkeynes@736
   436
            uint8_t code = *c++;
nkeynes@736
   437
            output[i + j*width] = codebook->quad[code][0];
nkeynes@736
   438
            output[i + 1 + j*width] = codebook->quad[code][1];
nkeynes@736
   439
            output[i + (j+1)*width] = codebook->quad[code][2];
nkeynes@736
   440
            output[i + 1 + (j+1)*width] = codebook->quad[code][3];
nkeynes@736
   441
        }
nkeynes@224
   442
    }
nkeynes@224
   443
}
nkeynes@113
   444
nkeynes@282
   445
static inline uint32_t yuv_to_rgb32( float y, float u, float v )
nkeynes@282
   446
{
nkeynes@282
   447
    u -= 128;
nkeynes@282
   448
    v -= 128;
nkeynes@282
   449
    int r = (int)(y + v*1.375);
nkeynes@282
   450
    int g = (int)(y - u*0.34375 - v*0.6875);
nkeynes@282
   451
    int b = (int)(y + u*1.71875);
nkeynes@282
   452
    if( r > 255 ) { r = 255; } else if( r < 0 ) { r = 0; }
nkeynes@282
   453
    if( g > 255 ) { g = 255; } else if( g < 0 ) { g = 0; }
nkeynes@282
   454
    if( b > 255 ) { b = 255; } else if( b < 0 ) { b = 0; }
nkeynes@289
   455
    return 0xFF000000 | (r<<16) | (g<<8) | (b);
nkeynes@282
   456
}
nkeynes@282
   457
nkeynes@282
   458
nkeynes@282
   459
/**
nkeynes@311
   460
 * Convert raster YUV texture data into RGB32 data - most GL implementations don't
nkeynes@282
   461
 * directly support this format unfortunately. The input data is formatted as
nkeynes@282
   462
 * 32 bits = 2 horizontal pixels, UYVY. This is currently done rather inefficiently
nkeynes@282
   463
 * in floating point.
nkeynes@282
   464
 */
nkeynes@311
   465
static void yuv_decode( uint32_t *output, uint32_t *input, int width, int height )
nkeynes@282
   466
{
nkeynes@282
   467
    int x, y;
nkeynes@282
   468
    uint32_t *p = input;
nkeynes@282
   469
    for( y=0; y<height; y++ ) {
nkeynes@736
   470
        for( x=0; x<width; x+=2 ) {
nkeynes@736
   471
            float u = (float)(*p & 0xFF);
nkeynes@736
   472
            float y0 = (float)( (*p>>8)&0xFF );
nkeynes@736
   473
            float v = (float)( (*p>>16)&0xFF );
nkeynes@736
   474
            float y1 = (float)( (*p>>24)&0xFF );
nkeynes@736
   475
            *output++ = yuv_to_rgb32( y0, u, v ); 
nkeynes@736
   476
            *output++ = yuv_to_rgb32( y1, u, v );
nkeynes@736
   477
            p++;
nkeynes@736
   478
        }
nkeynes@282
   479
    }
nkeynes@282
   480
}
nkeynes@282
   481
nkeynes@856
   482
static gboolean is_npot_texture( int width )
nkeynes@856
   483
{
nkeynes@856
   484
    while( width != 0 ) {
nkeynes@856
   485
        if( width & 1 ) 
nkeynes@856
   486
            return width != 1;
nkeynes@856
   487
        width >>= 1;
nkeynes@856
   488
    }
nkeynes@856
   489
    return TRUE;
nkeynes@856
   490
}
nkeynes@856
   491
nkeynes@103
   492
/**
nkeynes@103
   493
 * Load texture data from the given address and parameters into the currently
nkeynes@103
   494
 * bound OpenGL texture.
nkeynes@103
   495
 */
nkeynes@429
   496
static void texcache_load_texture( uint32_t texture_addr, int width, int height,
nkeynes@736
   497
                                   int mode ) {
nkeynes@284
   498
    int bpp_shift = 1; /* bytes per (output) pixel as a power of 2 */
nkeynes@349
   499
    GLint intFormat = GL_RGBA, format, type;
nkeynes@108
   500
    int tex_format = mode & PVR2_TEX_FORMAT_MASK;
nkeynes@270
   501
    struct vq_codebook codebook;
nkeynes@1140
   502
    GLint min_filter = GL_LINEAR;
nkeynes@1140
   503
    GLint max_filter = GL_LINEAR;
nkeynes@1140
   504
    GLint mipmapfilter = GL_LINEAR_MIPMAP_LINEAR;
nkeynes@108
   505
nkeynes@352
   506
    glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
nkeynes@654
   507
nkeynes@270
   508
    /* Decode the format parameters */
nkeynes@270
   509
    switch( tex_format ) {
nkeynes@270
   510
    case PVR2_TEX_FORMAT_IDX4:
nkeynes@270
   511
    case PVR2_TEX_FORMAT_IDX8:
nkeynes@1140
   512
        if( texcache_have_palette_shader ) {
nkeynes@1140
   513
            intFormat = GL_ALPHA8;
nkeynes@1140
   514
            format = GL_ALPHA;
nkeynes@736
   515
            type = GL_UNSIGNED_BYTE;
nkeynes@1140
   516
            bpp_shift = 0;
nkeynes@1140
   517
            min_filter = max_filter = GL_NEAREST;
nkeynes@1140
   518
            mipmapfilter = GL_NEAREST_MIPMAP_NEAREST;
nkeynes@1140
   519
        } else {
nkeynes@1140
   520
            /* For indexed-colour modes, we need to lookup the palette control
nkeynes@1140
   521
             * word to determine the de-indexed texture format.
nkeynes@1140
   522
             */
nkeynes@1140
   523
            switch( texcache_palette_mode ) {
nkeynes@1140
   524
            case 0: /* ARGB1555 */
nkeynes@1140
   525
                format = GL_BGRA;
nkeynes@1140
   526
                type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
nkeynes@1140
   527
                break;
nkeynes@1140
   528
            case 1:  /* RGB565 */
nkeynes@1140
   529
                intFormat = GL_RGB;
nkeynes@1140
   530
                format = GL_RGB;
nkeynes@1140
   531
                type = GL_UNSIGNED_SHORT_5_6_5;
nkeynes@1140
   532
                break;
nkeynes@1140
   533
            case 2: /* ARGB4444 */
nkeynes@1140
   534
                format = GL_BGRA;
nkeynes@1140
   535
                type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
nkeynes@1140
   536
                break;
nkeynes@1140
   537
            case 3: /* ARGB8888 */
nkeynes@1140
   538
                format = GL_BGRA;
nkeynes@1140
   539
                type = GL_UNSIGNED_BYTE;
nkeynes@1140
   540
                bpp_shift = 2;
nkeynes@1140
   541
                break;
nkeynes@1140
   542
            default:
nkeynes@1140
   543
                return; /* Can't happen, but it makes gcc stop complaining */
nkeynes@1140
   544
            }
nkeynes@736
   545
        }
nkeynes@736
   546
        break;
nkeynes@736
   547
nkeynes@860
   548
        default:
nkeynes@736
   549
        case PVR2_TEX_FORMAT_ARGB1555:
nkeynes@736
   550
            format = GL_BGRA;
nkeynes@736
   551
            type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
nkeynes@736
   552
            break;
nkeynes@736
   553
        case PVR2_TEX_FORMAT_RGB565:
nkeynes@736
   554
            intFormat = GL_RGB;
nkeynes@736
   555
            format = GL_RGB;
nkeynes@736
   556
            type = GL_UNSIGNED_SHORT_5_6_5;
nkeynes@736
   557
            break;
nkeynes@736
   558
        case PVR2_TEX_FORMAT_ARGB4444:
nkeynes@736
   559
            format = GL_BGRA;
nkeynes@736
   560
            type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
nkeynes@736
   561
            break;
nkeynes@736
   562
        case PVR2_TEX_FORMAT_YUV422:
nkeynes@736
   563
            /* YUV422 isn't directly supported by most implementations, so decode
nkeynes@736
   564
             * it to a (reasonably) standard ARGB32.
nkeynes@736
   565
             */
nkeynes@736
   566
            bpp_shift = 2;
nkeynes@736
   567
            format = GL_BGRA;
nkeynes@736
   568
            type = GL_UNSIGNED_BYTE;
nkeynes@736
   569
            break;
nkeynes@736
   570
        case PVR2_TEX_FORMAT_BUMPMAP:
nkeynes@860
   571
            WARN( "Bumpmap not supported" );
nkeynes@736
   572
            return;
nkeynes@270
   573
    }
nkeynes@736
   574
nkeynes@321
   575
    if( PVR2_TEX_IS_STRIDE(mode) && tex_format != PVR2_TEX_FORMAT_IDX4 &&
nkeynes@736
   576
            tex_format != PVR2_TEX_FORMAT_IDX8 ) {
nkeynes@736
   577
        /* Stride textures cannot be mip-mapped, compressed, indexed or twiddled */
nkeynes@736
   578
        unsigned char data[(width*height) << bpp_shift];
nkeynes@736
   579
        if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
nkeynes@736
   580
            unsigned char tmp[(width*height)<<1];
nkeynes@886
   581
            pvr2_vram64_read_stride( tmp, width<<1, texture_addr, texcache_stride_width<<1, height );
nkeynes@736
   582
            yuv_decode( (uint32_t *)data, (uint32_t *)tmp, width, height );
nkeynes@736
   583
        } else {
nkeynes@886
   584
            pvr2_vram64_read_stride( data, width<<bpp_shift, texture_addr, texcache_stride_width<<bpp_shift, height );
nkeynes@736
   585
        }
nkeynes@736
   586
        glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type, data );
nkeynes@1140
   587
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
nkeynes@1140
   588
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter);
nkeynes@736
   589
        return;
nkeynes@284
   590
    } 
nkeynes@284
   591
nkeynes@270
   592
    if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@736
   593
        uint16_t tmp[VQ_CODEBOOK_SIZE];
nkeynes@736
   594
        pvr2_vram64_read( (unsigned char *)tmp, texture_addr, VQ_CODEBOOK_SIZE );
nkeynes@736
   595
        texture_addr += VQ_CODEBOOK_SIZE;
nkeynes@736
   596
        vq_get_codebook( &codebook, tmp );
nkeynes@270
   597
    }
nkeynes@270
   598
nkeynes@349
   599
    int level=0, last_level = 0, mip_width = width, mip_height = height, src_bytes, dest_bytes;
nkeynes@270
   600
    if( PVR2_TEX_IS_MIPMAPPED(mode) ) {
nkeynes@736
   601
        uint32_t src_offset = 0;
nkeynes@1140
   602
        min_filter = mipmapfilter;
nkeynes@736
   603
        mip_height = height = width;
nkeynes@736
   604
        while( (1<<last_level) < width ) {
nkeynes@736
   605
            last_level++;
nkeynes@736
   606
            src_offset += ((width>>last_level)*(width>>last_level));
nkeynes@736
   607
        }
nkeynes@736
   608
        if( width != 1 ) {
nkeynes@736
   609
            src_offset += 3;
nkeynes@736
   610
        }
nkeynes@736
   611
        if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@736
   612
            src_offset >>= 2;
nkeynes@736
   613
        } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
nkeynes@736
   614
            src_offset >>= 1;
nkeynes@736
   615
        } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
nkeynes@736
   616
            src_offset <<= 1;
nkeynes@736
   617
        } else if( tex_format != PVR2_TEX_FORMAT_IDX8 ) {
nkeynes@736
   618
            src_offset <<= bpp_shift;
nkeynes@736
   619
        }
nkeynes@736
   620
        texture_addr += src_offset;
nkeynes@270
   621
    }
nkeynes@736
   622
nkeynes@654
   623
nkeynes@349
   624
    dest_bytes = (mip_width * mip_height) << bpp_shift;
nkeynes@349
   625
    src_bytes = dest_bytes; // Modes will change this (below)
nkeynes@108
   626
nkeynes@654
   627
    for( level=0; level<= last_level; level++ ) {
nkeynes@736
   628
        unsigned char data[dest_bytes];
nkeynes@736
   629
        /* load data from image, detwiddling/uncompressing as required */
nkeynes@736
   630
        if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
nkeynes@1140
   631
            if( texcache_have_palette_shader ) {
nkeynes@1140
   632
                pvr2_vram64_read_twiddled_8( data, texture_addr, mip_width, mip_height );
nkeynes@736
   633
            } else {
nkeynes@1140
   634
                src_bytes = (mip_width * mip_height);
nkeynes@1140
   635
                int bank = (mode >> 25) &0x03;
nkeynes@1140
   636
                uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<8);
nkeynes@1140
   637
                unsigned char tmp[src_bytes];
nkeynes@1140
   638
                pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width, mip_height );
nkeynes@1140
   639
                if( bpp_shift == 2 ) {
nkeynes@1140
   640
                    decode_pal8_to_32( (uint32_t *)data, tmp, src_bytes, palette );
nkeynes@1140
   641
                } else {
nkeynes@1140
   642
                    decode_pal8_to_16( (uint16_t *)data, tmp, src_bytes, palette );
nkeynes@1140
   643
                }
nkeynes@736
   644
            }
nkeynes@736
   645
        } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
nkeynes@736
   646
            src_bytes = (mip_width * mip_height) >> 1;
nkeynes@736
   647
            unsigned char tmp[src_bytes];
nkeynes@1140
   648
            if( texcache_have_palette_shader ) {
nkeynes@1140
   649
                pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
nkeynes@1140
   650
                decode_pal4_to_pal8( data, tmp, src_bytes );
nkeynes@736
   651
            } else {
nkeynes@1140
   652
                int bank = (mode >>21 ) & 0x3F;
nkeynes@1140
   653
                uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<4);
nkeynes@1140
   654
                pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
nkeynes@1140
   655
                if( bpp_shift == 2 ) {
nkeynes@1140
   656
                    decode_pal4_to_32( (uint32_t *)data, tmp, src_bytes, palette );
nkeynes@1140
   657
                } else {
nkeynes@1140
   658
                    decode_pal4_to_16( (uint16_t *)data, tmp, src_bytes, palette );
nkeynes@1140
   659
                }
nkeynes@736
   660
            }
nkeynes@736
   661
        } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
nkeynes@736
   662
            src_bytes = ((mip_width*mip_height)<<1);
nkeynes@736
   663
            unsigned char tmp[src_bytes];
nkeynes@736
   664
            if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@736
   665
                pvr2_vram64_read_twiddled_16( tmp, texture_addr, mip_width, mip_height );
nkeynes@736
   666
            } else {
nkeynes@736
   667
                pvr2_vram64_read( tmp, texture_addr, src_bytes );
nkeynes@736
   668
            }
nkeynes@736
   669
            yuv_decode( (uint32_t *)data, (uint32_t *)tmp, mip_width, mip_height );
nkeynes@736
   670
        } else if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@736
   671
            src_bytes = ((mip_width*mip_height) >> 2);
nkeynes@736
   672
            unsigned char tmp[src_bytes];
nkeynes@736
   673
            if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@736
   674
                pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width>>1, mip_height>>1 );
nkeynes@736
   675
            } else {
nkeynes@736
   676
                pvr2_vram64_read( tmp, texture_addr, src_bytes );
nkeynes@736
   677
            }
nkeynes@736
   678
            vq_decode( (uint16_t *)data, tmp, mip_width, mip_height, &codebook );
nkeynes@736
   679
        } else if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@736
   680
            pvr2_vram64_read_twiddled_16( data, texture_addr, mip_width, mip_height );
nkeynes@736
   681
        } else {
nkeynes@736
   682
            pvr2_vram64_read( data, texture_addr, src_bytes );
nkeynes@736
   683
        }
nkeynes@270
   684
nkeynes@736
   685
        /* Pass to GL */
nkeynes@736
   686
        if( level == last_level && level != 0 ) { /* 1x1 stored within a 2x2 */
nkeynes@736
   687
            glTexImage2D( GL_TEXTURE_2D, level, intFormat, 1, 1, 0, format, type,
nkeynes@736
   688
                    data + (3 << bpp_shift) );
nkeynes@736
   689
        } else {
nkeynes@736
   690
            glTexImage2D( GL_TEXTURE_2D, level, intFormat, mip_width, mip_height, 0, format, type,
nkeynes@736
   691
                    data );
nkeynes@736
   692
            if( mip_width > 2 ) {
nkeynes@736
   693
                mip_width >>= 1;
nkeynes@736
   694
                mip_height >>= 1;
nkeynes@736
   695
                dest_bytes >>= 2;
nkeynes@736
   696
                src_bytes >>= 2;
nkeynes@736
   697
            }
nkeynes@736
   698
            texture_addr -= src_bytes;
nkeynes@736
   699
        }
nkeynes@103
   700
    }
nkeynes@270
   701
nkeynes@1140
   702
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
nkeynes@1140
   703
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter);
nkeynes@103
   704
}
nkeynes@103
   705
nkeynes@1135
   706
static int texcache_find_texture_slot( uint32_t poly2_masked_word, uint32_t texture_word )
nkeynes@103
   707
{
nkeynes@653
   708
    uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
nkeynes@103
   709
    uint32_t texture_page = texture_addr >> 12;
nkeynes@462
   710
    texcache_entry_index next;
nkeynes@103
   711
    texcache_entry_index idx = texcache_page_lookup[texture_page];
nkeynes@103
   712
    while( idx != EMPTY_ENTRY ) {
nkeynes@736
   713
        texcache_entry_t entry = &texcache_active_list[idx];
nkeynes@1135
   714
        if( entry->tex_mode == texture_word &&
nkeynes@1135
   715
                entry->poly2_mode == poly2_masked_word ) {
nkeynes@736
   716
            entry->lru_count = texcache_ref_counter++;
nkeynes@856
   717
            return idx;
nkeynes@736
   718
        }
nkeynes@103
   719
        idx = entry->next;
nkeynes@103
   720
    }
nkeynes@856
   721
    return -1;
nkeynes@856
   722
}
nkeynes@103
   723
nkeynes@1135
   724
static int texcache_alloc_texture_slot( uint32_t poly2_word, uint32_t texture_word )
nkeynes@856
   725
{
nkeynes@856
   726
    uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
nkeynes@856
   727
    uint32_t texture_page = texture_addr >> 12;
nkeynes@462
   728
    texcache_entry_index slot = 0;
nkeynes@103
   729
nkeynes@103
   730
    if( texcache_free_ptr < MAX_TEXTURES ) {
nkeynes@736
   731
        slot = texcache_free_list[texcache_free_ptr++];
nkeynes@103
   732
    } else {
nkeynes@736
   733
        slot = texcache_evict_lru();
nkeynes@103
   734
    }
nkeynes@103
   735
nkeynes@103
   736
    /* Construct new entry */
nkeynes@886
   737
    assert( texcache_active_list[slot].texture_addr == -1 );
nkeynes@103
   738
    texcache_active_list[slot].texture_addr = texture_addr;
nkeynes@1135
   739
    texcache_active_list[slot].tex_mode = texture_word;
nkeynes@1135
   740
    texcache_active_list[slot].poly2_mode = poly2_word;
nkeynes@103
   741
    texcache_active_list[slot].lru_count = texcache_ref_counter++;
nkeynes@103
   742
nkeynes@103
   743
    /* Add entry to the lookup table */
nkeynes@856
   744
    int next = texcache_page_lookup[texture_page];
nkeynes@462
   745
    if( next == slot ) {
nkeynes@736
   746
        int i;
nkeynes@736
   747
        fprintf( stderr, "Active list: " );
nkeynes@736
   748
        for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   749
            fprintf( stderr, "%d, ", texcache_active_list[i].next );
nkeynes@736
   750
        }
nkeynes@736
   751
        fprintf( stderr, "\n" );
nkeynes@736
   752
        assert( next != slot );
nkeynes@736
   753
nkeynes@462
   754
    }
nkeynes@462
   755
    texcache_active_list[slot].next = next;
nkeynes@103
   756
    texcache_page_lookup[texture_page] = slot;
nkeynes@856
   757
    return slot;
nkeynes@856
   758
}
nkeynes@103
   759
nkeynes@103
   760
/**
nkeynes@103
   761
 * Return a texture ID for the texture specified at the supplied address
nkeynes@103
   762
 * and given parameters (the same sequence of bytes could in theory have
nkeynes@103
   763
 * multiple interpretations). We use the texture address as the primary
nkeynes@1135
   764
 * index, but allow for multiple instances at each address.
nkeynes@103
   765
 * 
nkeynes@103
   766
 * If the texture has already been bound, return the ID to which it was
nkeynes@103
   767
 * bound. Otherwise obtain an unused texture ID and set it up appropriately.
nkeynes@1135
   768
 * The current GL_TEXTURE_2D binding will be changed in this case.
nkeynes@103
   769
 */
nkeynes@1135
   770
GLuint texcache_get_texture( uint32_t poly2_word, uint32_t texture_word )
nkeynes@103
   771
{
nkeynes@1135
   772
    poly2_word &= 0x000F803F; /* Get just the texture-relevant bits */
nkeynes@1140
   773
    uint32_t texture_lookup = texture_word;
nkeynes@1140
   774
    if( PVR2_TEX_IS_PALETTE(texture_lookup) ) {
nkeynes@1140
   775
        texture_lookup &= 0xF81FFFFF; /* Mask out the bank bits */
nkeynes@1140
   776
    }
nkeynes@1140
   777
    int slot = texcache_find_texture_slot( poly2_word, texture_lookup );
nkeynes@856
   778
nkeynes@856
   779
    if( slot == -1 ) {
nkeynes@856
   780
        /* Not found - check the free list */
nkeynes@1140
   781
        slot = texcache_alloc_texture_slot( poly2_word, texture_lookup );
nkeynes@856
   782
        
nkeynes@856
   783
        /* Construct the GL texture */
nkeynes@856
   784
        uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
nkeynes@1135
   785
        unsigned width = POLY2_TEX_WIDTH(poly2_word);
nkeynes@1135
   786
        unsigned height = POLY2_TEX_HEIGHT(poly2_word);
nkeynes@1135
   787
nkeynes@856
   788
        glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
nkeynes@856
   789
        texcache_load_texture( texture_addr, width, height, texture_word );
nkeynes@1135
   790
nkeynes@1135
   791
        /* Set texture parameters from the poly2 word */
nkeynes@1135
   792
        if( POLY2_TEX_CLAMP_U(poly2_word) ) {
nkeynes@1210
   793
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
nkeynes@1135
   794
        } else if( POLY2_TEX_MIRROR_U(poly2_word) ) {
nkeynes@1210
   795
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT );
nkeynes@1135
   796
        } else {
nkeynes@1135
   797
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
nkeynes@1135
   798
        }
nkeynes@1135
   799
        if( POLY2_TEX_CLAMP_V(poly2_word) ) {
nkeynes@1210
   800
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
nkeynes@1135
   801
        } else if( POLY2_TEX_MIRROR_V(poly2_word) ) {
nkeynes@1210
   802
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT );
nkeynes@1135
   803
        } else {
nkeynes@1135
   804
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
nkeynes@1135
   805
        }
nkeynes@103
   806
    }
nkeynes@103
   807
nkeynes@103
   808
    return texcache_active_list[slot].texture_id;
nkeynes@103
   809
}
nkeynes@653
   810
nkeynes@1135
   811
#if 0
nkeynes@856
   812
render_buffer_t texcache_get_render_buffer( uint32_t texture_addr, int mode, int width, int height )
nkeynes@856
   813
{
nkeynes@856
   814
    uint32_t texture_word = ((texture_addr >> 3) & 0x000FFFFF) | PVR2_TEX_UNTWIDDLED;
nkeynes@856
   815
    switch( mode ) {
nkeynes@856
   816
    case COLFMT_BGRA1555: texture_word |= PVR2_TEX_FORMAT_ARGB1555; break;
nkeynes@856
   817
    case COLFMT_RGB565:   texture_word |= PVR2_TEX_FORMAT_RGB565; break;
nkeynes@856
   818
    case COLFMT_BGRA4444: texture_word |= PVR2_TEX_FORMAT_ARGB4444; break;
nkeynes@856
   819
    default:
nkeynes@856
   820
        WARN( "Rendering to non-texture colour format" );
nkeynes@856
   821
    }
nkeynes@856
   822
    if( is_npot_texture(width) )
nkeynes@856
   823
        texture_word |= PVR2_TEX_STRIDE;
nkeynes@635
   824
    
nkeynes@856
   825
    
nkeynes@856
   826
    int slot = texcache_find_texture_slot( texture_word, width, height );
nkeynes@856
   827
    if( slot == -1 ) {
nkeynes@856
   828
        slot = texcache_alloc_texture_slot( texture_word, width, height );
nkeynes@856
   829
    }
nkeynes@856
   830
    
nkeynes@856
   831
    texcache_entry_t entry = &texcache_active_list[slot];
nkeynes@103
   832
nkeynes@870
   833
    if( entry->buffer == NULL ) {
nkeynes@856
   834
        entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
nkeynes@870
   835
    } else if( entry->buffer->width != width || entry->buffer->height != height ) {        
nkeynes@870
   836
        texcache_release_render_buffer(entry->buffer);
nkeynes@870
   837
        entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
nkeynes@103
   838
    }
nkeynes@103
   839
nkeynes@856
   840
    return entry->buffer;
nkeynes@103
   841
}
nkeynes@1135
   842
#endif
nkeynes@635
   843
nkeynes@635
   844
/**
nkeynes@635
   845
 * Check the integrity of the texcache. Verifies that every cache slot
nkeynes@635
   846
 * appears exactly once on either the free list or one page list. For 
nkeynes@635
   847
 * active slots, the texture address must also match the page it appears on.
nkeynes@635
   848
 * 
nkeynes@635
   849
 */
nkeynes@635
   850
void texcache_integrity_check()
nkeynes@635
   851
{
nkeynes@635
   852
    int i;
nkeynes@635
   853
    int slot_found[MAX_TEXTURES];
nkeynes@736
   854
nkeynes@635
   855
    memset( slot_found, 0, sizeof(slot_found) );
nkeynes@635
   856
nkeynes@635
   857
    /* Check entries on the free list */
nkeynes@635
   858
    for( i= texcache_free_ptr; i< MAX_TEXTURES; i++ ) {
nkeynes@736
   859
        int slot = texcache_free_list[i];
nkeynes@736
   860
        assert( slot_found[slot] == 0 );
nkeynes@736
   861
        assert( texcache_active_list[slot].next == EMPTY_ENTRY );
nkeynes@736
   862
        slot_found[slot] = 1;
nkeynes@635
   863
    }
nkeynes@635
   864
nkeynes@635
   865
    /* Check entries on the active lists */
nkeynes@635
   866
    for( i=0; i< PVR2_RAM_PAGES; i++ ) {
nkeynes@736
   867
        int slot = texcache_page_lookup[i];
nkeynes@736
   868
        while( slot != EMPTY_ENTRY ) {
nkeynes@736
   869
            assert( slot_found[slot] == 0 );
nkeynes@736
   870
            assert( (texcache_active_list[slot].texture_addr >> 12) == i );
nkeynes@736
   871
            slot_found[slot] = 2;
nkeynes@736
   872
            slot = texcache_active_list[slot].next;
nkeynes@736
   873
        }
nkeynes@635
   874
    }
nkeynes@635
   875
nkeynes@635
   876
    /* Make sure we didn't miss any entries */
nkeynes@635
   877
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   878
        assert( slot_found[i] != 0 );
nkeynes@635
   879
    }
nkeynes@635
   880
}
nkeynes@1140
   881
nkeynes@1140
   882
/**
nkeynes@1140
   883
 * Dump the contents of the texture cache
nkeynes@1140
   884
 */
nkeynes@1140
   885
void texcache_dump()
nkeynes@1140
   886
{
nkeynes@1140
   887
    unsigned i;
nkeynes@1165
   888
    GLboolean boolresult;
nkeynes@1140
   889
    for( i=0; i< PVR2_RAM_PAGES; i++ ) {
nkeynes@1140
   890
        int slot = texcache_page_lookup[i];
nkeynes@1140
   891
        while( slot != EMPTY_ENTRY ) {
nkeynes@1165
   892
            fprintf( stderr, "%-3d: %08X %dx%d (%08X %08X) %s\n", slot,
nkeynes@1140
   893
                    texcache_active_list[slot].texture_addr,
nkeynes@1140
   894
                    POLY2_TEX_WIDTH(texcache_active_list[slot].poly2_mode),
nkeynes@1140
   895
                    POLY2_TEX_HEIGHT(texcache_active_list[slot].poly2_mode),
nkeynes@1140
   896
                    texcache_active_list[slot].poly2_mode,
nkeynes@1165
   897
                    texcache_active_list[slot].tex_mode,
nkeynes@1165
   898
                    (glAreTexturesResident(1, &texcache_active_list[slot].texture_id, &boolresult) ? "[RESIDENT]" : "[NOT RESIDENT]") );
nkeynes@1140
   899
            slot = texcache_active_list[slot].next;
nkeynes@1140
   900
        }
nkeynes@1140
   901
    }
nkeynes@1140
   902
}
nkeynes@1142
   903
nkeynes@1142
   904
void texcache_print_idx4( uint32_t texture_addr, int width )
nkeynes@1142
   905
{
nkeynes@1142
   906
    unsigned x,y;
nkeynes@1142
   907
    int src_bytes = (width*width>>1);
nkeynes@1142
   908
    char tmp[src_bytes];
nkeynes@1142
   909
    char data[width*width];
nkeynes@1142
   910
    pvr2_vram64_read_twiddled_4( tmp, texture_addr, width, width );
nkeynes@1142
   911
    decode_pal4_to_pal8( data, tmp, src_bytes );
nkeynes@1142
   912
    for( y=0; y<width; y++ ) {
nkeynes@1142
   913
        for( x=0; x<width; x++ ) {
nkeynes@1142
   914
            printf( "%1x", data[y*width+x] );
nkeynes@1142
   915
        }
nkeynes@1142
   916
        printf( "\n" );
nkeynes@1142
   917
    }
nkeynes@1142
   918
}
.