Search
lxdream.org :: lxdream/src/pvr2/texcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/texcache.c
changeset 1233:06923d1020de
prev1221:f50407acc682
next1236:d93175c36387
author nkeynes
date Thu Feb 23 22:40:50 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Fix non-shader rendering
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@1233
    92
void texcache_gl_init( gboolean withShaders )
nkeynes@108
    93
{
nkeynes@108
    94
    int i;
nkeynes@108
    95
    GLuint texids[MAX_TEXTURES];
nkeynes@108
    96
nkeynes@1233
    97
    if( withShaders ) {
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@1221
   104
        glBindTexture(GL_TEXTURE_2D, texcache_palette_texid);
nkeynes@1221
   105
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nkeynes@1221
   106
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nkeynes@1221
   107
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
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
    if( format_changed )
nkeynes@1221
   294
        glTexImage2D(GL_TEXTURE_2D, 0, intFormat, 1024, 1, 0, format, type, data );
nkeynes@1140
   295
    else
nkeynes@1221
   296
        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1024, 1, format, type, data);
nkeynes@1140
   297
    glActiveTexture(GL_TEXTURE0);
nkeynes@1140
   298
    texcache_palette_valid = TRUE;
nkeynes@1140
   299
}
nkeynes@1140
   300
nkeynes@1140
   301
nkeynes@1140
   302
/**
nkeynes@1140
   303
 * Mark the palette as having changed. If we have palette support (via shaders)
nkeynes@1140
   304
 * we just flag the palette, otherwise we have to invalidate all palette
nkeynes@1140
   305
 * textures.
nkeynes@337
   306
 */
nkeynes@337
   307
void texcache_invalidate_palette( )
nkeynes@337
   308
{
nkeynes@1140
   309
    if( texcache_have_palette_shader ) {
nkeynes@1140
   310
        texcache_palette_valid = FALSE;
nkeynes@1140
   311
    } else {
nkeynes@1140
   312
        int i;
nkeynes@1140
   313
        for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@1140
   314
            if( texcache_active_list[i].texture_addr != -1 &&
nkeynes@1140
   315
                    PVR2_TEX_IS_PALETTE(texcache_active_list[i].tex_mode) ) {
nkeynes@1140
   316
                texcache_evict( i );
nkeynes@1140
   317
                texcache_free_ptr--;
nkeynes@1140
   318
                texcache_free_list[texcache_free_ptr] = i;
nkeynes@1140
   319
            }
nkeynes@736
   320
        }
nkeynes@337
   321
    }
nkeynes@337
   322
}
nkeynes@886
   323
/**
nkeynes@886
   324
 * Mark all stride textures as needing a re-read (ie when the stride width
nkeynes@886
   325
 * is changed).
nkeynes@886
   326
 */
nkeynes@886
   327
void texcache_invalidate_stride( )
nkeynes@886
   328
{
nkeynes@886
   329
    int i;
nkeynes@886
   330
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@886
   331
        if( texcache_active_list[i].texture_addr != -1 &&
nkeynes@1135
   332
                PVR2_TEX_IS_STRIDE(texcache_active_list[i].tex_mode) ) {
nkeynes@886
   333
            texcache_evict( i );
nkeynes@886
   334
            texcache_free_ptr--;
nkeynes@886
   335
            texcache_free_list[texcache_free_ptr] = i;
nkeynes@886
   336
        }
nkeynes@886
   337
    }
nkeynes@886
   338
}
nkeynes@886
   339
nkeynes@1135
   340
void texcache_begin_scene( uint32_t palette_mode, uint32_t stride )
nkeynes@886
   341
{
nkeynes@1140
   342
    gboolean format_changed = FALSE;
nkeynes@1140
   343
    if( palette_mode != texcache_palette_mode ) {
nkeynes@886
   344
        texcache_invalidate_palette();
nkeynes@1140
   345
        format_changed = TRUE;
nkeynes@1140
   346
    }
nkeynes@886
   347
    if( stride != texcache_stride_width )
nkeynes@886
   348
        texcache_invalidate_stride();
nkeynes@886
   349
    
nkeynes@886
   350
    texcache_palette_mode = palette_mode;
nkeynes@886
   351
    texcache_stride_width = stride;
nkeynes@1140
   352
nkeynes@1140
   353
    if( !texcache_palette_valid && texcache_have_palette_shader )
nkeynes@1140
   354
        texcache_load_palette_texture(format_changed);
nkeynes@886
   355
}
nkeynes@337
   356
nkeynes@311
   357
static void decode_pal8_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@311
   358
{
nkeynes@311
   359
    int i;
nkeynes@311
   360
    for( i=0; i<inbytes; i++ ) {
nkeynes@736
   361
        *out++ = pal[*in++];
nkeynes@129
   362
    }
nkeynes@129
   363
}
nkeynes@129
   364
nkeynes@321
   365
static void decode_pal8_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@311
   366
{
nkeynes@311
   367
    int i;
nkeynes@311
   368
    for( i=0; i<inbytes; i++ ) {
nkeynes@736
   369
        *out++ = (uint16_t)pal[*in++];
nkeynes@113
   370
    }
nkeynes@113
   371
}
nkeynes@113
   372
nkeynes@315
   373
static void decode_pal4_to_32( uint32_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@315
   374
{
nkeynes@315
   375
    int i;
nkeynes@315
   376
    for( i=0; i<inbytes; i++ ) {
nkeynes@736
   377
        *out++ = pal[*in & 0x0F];
nkeynes@736
   378
        *out++ = pal[(*in >> 4)];
nkeynes@736
   379
        in++;
nkeynes@315
   380
    }
nkeynes@315
   381
}
nkeynes@315
   382
nkeynes@1140
   383
static void decode_pal4_to_pal8( uint8_t *out, uint8_t *in, int inbytes )
nkeynes@1140
   384
{
nkeynes@1140
   385
    int i;
nkeynes@1140
   386
    for( i=0; i<inbytes; i++ ) {
nkeynes@1140
   387
        *out++ = (uint8_t)(*in & 0x0F);
nkeynes@1140
   388
        *out++ = (uint8_t)(*in >> 4);
nkeynes@1140
   389
        in++;
nkeynes@1140
   390
    }
nkeynes@1140
   391
}
nkeynes@1140
   392
nkeynes@1140
   393
nkeynes@315
   394
nkeynes@321
   395
static void decode_pal4_to_16( uint16_t *out, uint8_t *in, int inbytes, uint32_t *pal )
nkeynes@315
   396
{
nkeynes@315
   397
    int i;
nkeynes@315
   398
    for( i=0; i<inbytes; i++ ) {
nkeynes@736
   399
        *out++ = (uint16_t)pal[*in & 0x0F];
nkeynes@736
   400
        *out++ = (uint16_t)pal[(*in >> 4)];
nkeynes@736
   401
        in++;
nkeynes@315
   402
    }
nkeynes@315
   403
}
nkeynes@315
   404
nkeynes@224
   405
#define VQ_CODEBOOK_SIZE 2048 /* 256 entries * 4 pixels per quad * 2 byte pixels */
nkeynes@224
   406
nkeynes@224
   407
struct vq_codebook {
nkeynes@224
   408
    uint16_t quad[256][4];
nkeynes@224
   409
};
nkeynes@224
   410
nkeynes@270
   411
static void vq_get_codebook( struct vq_codebook *codebook, 
nkeynes@736
   412
                             uint16_t *input )
nkeynes@270
   413
{
nkeynes@270
   414
    /* Detwiddle the codebook, for the sake of my own sanity if nothing else */
nkeynes@270
   415
    uint16_t *p = (uint16_t *)input;
nkeynes@270
   416
    int i;
nkeynes@270
   417
    for( i=0; i<256; i++ ) {
nkeynes@736
   418
        codebook->quad[i][0] = *p++;
nkeynes@736
   419
        codebook->quad[i][2] = *p++;
nkeynes@736
   420
        codebook->quad[i][1] = *p++;
nkeynes@736
   421
        codebook->quad[i][3] = *p++;
nkeynes@270
   422
    }
nkeynes@270
   423
}    
nkeynes@270
   424
nkeynes@429
   425
static void vq_decode( uint16_t *output, unsigned char *input, int width, int height, 
nkeynes@736
   426
                       struct vq_codebook *codebook ) {
nkeynes@224
   427
    int i,j;
nkeynes@736
   428
nkeynes@270
   429
    uint8_t *c = (uint8_t *)input;
nkeynes@311
   430
    for( j=0; j<height; j+=2 ) {
nkeynes@736
   431
        for( i=0; i<width; i+=2 ) {
nkeynes@736
   432
            uint8_t code = *c++;
nkeynes@736
   433
            output[i + j*width] = codebook->quad[code][0];
nkeynes@736
   434
            output[i + 1 + j*width] = codebook->quad[code][1];
nkeynes@736
   435
            output[i + (j+1)*width] = codebook->quad[code][2];
nkeynes@736
   436
            output[i + 1 + (j+1)*width] = codebook->quad[code][3];
nkeynes@736
   437
        }
nkeynes@224
   438
    }
nkeynes@224
   439
}
nkeynes@113
   440
nkeynes@282
   441
static inline uint32_t yuv_to_rgb32( float y, float u, float v )
nkeynes@282
   442
{
nkeynes@282
   443
    u -= 128;
nkeynes@282
   444
    v -= 128;
nkeynes@282
   445
    int r = (int)(y + v*1.375);
nkeynes@282
   446
    int g = (int)(y - u*0.34375 - v*0.6875);
nkeynes@282
   447
    int b = (int)(y + u*1.71875);
nkeynes@282
   448
    if( r > 255 ) { r = 255; } else if( r < 0 ) { r = 0; }
nkeynes@282
   449
    if( g > 255 ) { g = 255; } else if( g < 0 ) { g = 0; }
nkeynes@282
   450
    if( b > 255 ) { b = 255; } else if( b < 0 ) { b = 0; }
nkeynes@289
   451
    return 0xFF000000 | (r<<16) | (g<<8) | (b);
nkeynes@282
   452
}
nkeynes@282
   453
nkeynes@282
   454
nkeynes@282
   455
/**
nkeynes@311
   456
 * Convert raster YUV texture data into RGB32 data - most GL implementations don't
nkeynes@282
   457
 * directly support this format unfortunately. The input data is formatted as
nkeynes@282
   458
 * 32 bits = 2 horizontal pixels, UYVY. This is currently done rather inefficiently
nkeynes@282
   459
 * in floating point.
nkeynes@282
   460
 */
nkeynes@311
   461
static void yuv_decode( uint32_t *output, uint32_t *input, int width, int height )
nkeynes@282
   462
{
nkeynes@282
   463
    int x, y;
nkeynes@282
   464
    uint32_t *p = input;
nkeynes@282
   465
    for( y=0; y<height; y++ ) {
nkeynes@736
   466
        for( x=0; x<width; x+=2 ) {
nkeynes@736
   467
            float u = (float)(*p & 0xFF);
nkeynes@736
   468
            float y0 = (float)( (*p>>8)&0xFF );
nkeynes@736
   469
            float v = (float)( (*p>>16)&0xFF );
nkeynes@736
   470
            float y1 = (float)( (*p>>24)&0xFF );
nkeynes@736
   471
            *output++ = yuv_to_rgb32( y0, u, v ); 
nkeynes@736
   472
            *output++ = yuv_to_rgb32( y1, u, v );
nkeynes@736
   473
            p++;
nkeynes@736
   474
        }
nkeynes@282
   475
    }
nkeynes@282
   476
}
nkeynes@282
   477
nkeynes@856
   478
static gboolean is_npot_texture( int width )
nkeynes@856
   479
{
nkeynes@856
   480
    while( width != 0 ) {
nkeynes@856
   481
        if( width & 1 ) 
nkeynes@856
   482
            return width != 1;
nkeynes@856
   483
        width >>= 1;
nkeynes@856
   484
    }
nkeynes@856
   485
    return TRUE;
nkeynes@856
   486
}
nkeynes@856
   487
nkeynes@103
   488
/**
nkeynes@103
   489
 * Load texture data from the given address and parameters into the currently
nkeynes@103
   490
 * bound OpenGL texture.
nkeynes@103
   491
 */
nkeynes@429
   492
static void texcache_load_texture( uint32_t texture_addr, int width, int height,
nkeynes@736
   493
                                   int mode ) {
nkeynes@284
   494
    int bpp_shift = 1; /* bytes per (output) pixel as a power of 2 */
nkeynes@349
   495
    GLint intFormat = GL_RGBA, format, type;
nkeynes@108
   496
    int tex_format = mode & PVR2_TEX_FORMAT_MASK;
nkeynes@270
   497
    struct vq_codebook codebook;
nkeynes@1140
   498
    GLint min_filter = GL_LINEAR;
nkeynes@1140
   499
    GLint max_filter = GL_LINEAR;
nkeynes@1140
   500
    GLint mipmapfilter = GL_LINEAR_MIPMAP_LINEAR;
nkeynes@108
   501
nkeynes@352
   502
    glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
nkeynes@654
   503
nkeynes@270
   504
    /* Decode the format parameters */
nkeynes@270
   505
    switch( tex_format ) {
nkeynes@270
   506
    case PVR2_TEX_FORMAT_IDX4:
nkeynes@270
   507
    case PVR2_TEX_FORMAT_IDX8:
nkeynes@1140
   508
        if( texcache_have_palette_shader ) {
nkeynes@1221
   509
            intFormat = GL_ALPHA;
nkeynes@1140
   510
            format = GL_ALPHA;
nkeynes@736
   511
            type = GL_UNSIGNED_BYTE;
nkeynes@1140
   512
            bpp_shift = 0;
nkeynes@1140
   513
            min_filter = max_filter = GL_NEAREST;
nkeynes@1140
   514
            mipmapfilter = GL_NEAREST_MIPMAP_NEAREST;
nkeynes@1140
   515
        } else {
nkeynes@1140
   516
            /* For indexed-colour modes, we need to lookup the palette control
nkeynes@1140
   517
             * word to determine the de-indexed texture format.
nkeynes@1140
   518
             */
nkeynes@1140
   519
            switch( texcache_palette_mode ) {
nkeynes@1140
   520
            case 0: /* ARGB1555 */
nkeynes@1140
   521
                format = GL_BGRA;
nkeynes@1140
   522
                type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
nkeynes@1140
   523
                break;
nkeynes@1140
   524
            case 1:  /* RGB565 */
nkeynes@1140
   525
                intFormat = GL_RGB;
nkeynes@1140
   526
                format = GL_RGB;
nkeynes@1140
   527
                type = GL_UNSIGNED_SHORT_5_6_5;
nkeynes@1140
   528
                break;
nkeynes@1140
   529
            case 2: /* ARGB4444 */
nkeynes@1140
   530
                format = GL_BGRA;
nkeynes@1140
   531
                type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
nkeynes@1140
   532
                break;
nkeynes@1140
   533
            case 3: /* ARGB8888 */
nkeynes@1140
   534
                format = GL_BGRA;
nkeynes@1140
   535
                type = GL_UNSIGNED_BYTE;
nkeynes@1140
   536
                bpp_shift = 2;
nkeynes@1140
   537
                break;
nkeynes@1140
   538
            default:
nkeynes@1140
   539
                return; /* Can't happen, but it makes gcc stop complaining */
nkeynes@1140
   540
            }
nkeynes@736
   541
        }
nkeynes@736
   542
        break;
nkeynes@736
   543
nkeynes@860
   544
        default:
nkeynes@736
   545
        case PVR2_TEX_FORMAT_ARGB1555:
nkeynes@736
   546
            format = GL_BGRA;
nkeynes@736
   547
            type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
nkeynes@736
   548
            break;
nkeynes@736
   549
        case PVR2_TEX_FORMAT_RGB565:
nkeynes@736
   550
            intFormat = GL_RGB;
nkeynes@736
   551
            format = GL_RGB;
nkeynes@736
   552
            type = GL_UNSIGNED_SHORT_5_6_5;
nkeynes@736
   553
            break;
nkeynes@736
   554
        case PVR2_TEX_FORMAT_ARGB4444:
nkeynes@736
   555
            format = GL_BGRA;
nkeynes@736
   556
            type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
nkeynes@736
   557
            break;
nkeynes@736
   558
        case PVR2_TEX_FORMAT_YUV422:
nkeynes@736
   559
            /* YUV422 isn't directly supported by most implementations, so decode
nkeynes@736
   560
             * it to a (reasonably) standard ARGB32.
nkeynes@736
   561
             */
nkeynes@736
   562
            bpp_shift = 2;
nkeynes@736
   563
            format = GL_BGRA;
nkeynes@736
   564
            type = GL_UNSIGNED_BYTE;
nkeynes@736
   565
            break;
nkeynes@736
   566
        case PVR2_TEX_FORMAT_BUMPMAP:
nkeynes@860
   567
            WARN( "Bumpmap not supported" );
nkeynes@736
   568
            return;
nkeynes@270
   569
    }
nkeynes@736
   570
nkeynes@321
   571
    if( PVR2_TEX_IS_STRIDE(mode) && tex_format != PVR2_TEX_FORMAT_IDX4 &&
nkeynes@736
   572
            tex_format != PVR2_TEX_FORMAT_IDX8 ) {
nkeynes@736
   573
        /* Stride textures cannot be mip-mapped, compressed, indexed or twiddled */
nkeynes@736
   574
        unsigned char data[(width*height) << bpp_shift];
nkeynes@736
   575
        if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
nkeynes@736
   576
            unsigned char tmp[(width*height)<<1];
nkeynes@886
   577
            pvr2_vram64_read_stride( tmp, width<<1, texture_addr, texcache_stride_width<<1, height );
nkeynes@736
   578
            yuv_decode( (uint32_t *)data, (uint32_t *)tmp, width, height );
nkeynes@736
   579
        } else {
nkeynes@886
   580
            pvr2_vram64_read_stride( data, width<<bpp_shift, texture_addr, texcache_stride_width<<bpp_shift, height );
nkeynes@736
   581
        }
nkeynes@736
   582
        glTexImage2D( GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type, data );
nkeynes@1140
   583
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
nkeynes@1140
   584
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter);
nkeynes@736
   585
        return;
nkeynes@284
   586
    } 
nkeynes@284
   587
nkeynes@270
   588
    if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@736
   589
        uint16_t tmp[VQ_CODEBOOK_SIZE];
nkeynes@736
   590
        pvr2_vram64_read( (unsigned char *)tmp, texture_addr, VQ_CODEBOOK_SIZE );
nkeynes@736
   591
        texture_addr += VQ_CODEBOOK_SIZE;
nkeynes@736
   592
        vq_get_codebook( &codebook, tmp );
nkeynes@270
   593
    }
nkeynes@270
   594
nkeynes@349
   595
    int level=0, last_level = 0, mip_width = width, mip_height = height, src_bytes, dest_bytes;
nkeynes@270
   596
    if( PVR2_TEX_IS_MIPMAPPED(mode) ) {
nkeynes@736
   597
        uint32_t src_offset = 0;
nkeynes@1140
   598
        min_filter = mipmapfilter;
nkeynes@736
   599
        mip_height = height = width;
nkeynes@736
   600
        while( (1<<last_level) < width ) {
nkeynes@736
   601
            last_level++;
nkeynes@736
   602
            src_offset += ((width>>last_level)*(width>>last_level));
nkeynes@736
   603
        }
nkeynes@736
   604
        if( width != 1 ) {
nkeynes@736
   605
            src_offset += 3;
nkeynes@736
   606
        }
nkeynes@736
   607
        if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@736
   608
            src_offset >>= 2;
nkeynes@736
   609
        } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
nkeynes@736
   610
            src_offset >>= 1;
nkeynes@736
   611
        } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
nkeynes@736
   612
            src_offset <<= 1;
nkeynes@736
   613
        } else if( tex_format != PVR2_TEX_FORMAT_IDX8 ) {
nkeynes@736
   614
            src_offset <<= bpp_shift;
nkeynes@736
   615
        }
nkeynes@736
   616
        texture_addr += src_offset;
nkeynes@270
   617
    }
nkeynes@736
   618
nkeynes@654
   619
nkeynes@349
   620
    dest_bytes = (mip_width * mip_height) << bpp_shift;
nkeynes@349
   621
    src_bytes = dest_bytes; // Modes will change this (below)
nkeynes@108
   622
nkeynes@654
   623
    for( level=0; level<= last_level; level++ ) {
nkeynes@736
   624
        unsigned char data[dest_bytes];
nkeynes@736
   625
        /* load data from image, detwiddling/uncompressing as required */
nkeynes@736
   626
        if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
nkeynes@1140
   627
            if( texcache_have_palette_shader ) {
nkeynes@1140
   628
                pvr2_vram64_read_twiddled_8( data, texture_addr, mip_width, mip_height );
nkeynes@736
   629
            } else {
nkeynes@1140
   630
                src_bytes = (mip_width * mip_height);
nkeynes@1140
   631
                int bank = (mode >> 25) &0x03;
nkeynes@1140
   632
                uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<8);
nkeynes@1140
   633
                unsigned char tmp[src_bytes];
nkeynes@1140
   634
                pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width, mip_height );
nkeynes@1140
   635
                if( bpp_shift == 2 ) {
nkeynes@1140
   636
                    decode_pal8_to_32( (uint32_t *)data, tmp, src_bytes, palette );
nkeynes@1140
   637
                } else {
nkeynes@1140
   638
                    decode_pal8_to_16( (uint16_t *)data, tmp, src_bytes, palette );
nkeynes@1140
   639
                }
nkeynes@736
   640
            }
nkeynes@736
   641
        } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
nkeynes@736
   642
            src_bytes = (mip_width * mip_height) >> 1;
nkeynes@736
   643
            unsigned char tmp[src_bytes];
nkeynes@1140
   644
            if( texcache_have_palette_shader ) {
nkeynes@1140
   645
                pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
nkeynes@1140
   646
                decode_pal4_to_pal8( data, tmp, src_bytes );
nkeynes@736
   647
            } else {
nkeynes@1140
   648
                int bank = (mode >>21 ) & 0x3F;
nkeynes@1140
   649
                uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<4);
nkeynes@1140
   650
                pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
nkeynes@1140
   651
                if( bpp_shift == 2 ) {
nkeynes@1140
   652
                    decode_pal4_to_32( (uint32_t *)data, tmp, src_bytes, palette );
nkeynes@1140
   653
                } else {
nkeynes@1140
   654
                    decode_pal4_to_16( (uint16_t *)data, tmp, src_bytes, palette );
nkeynes@1140
   655
                }
nkeynes@736
   656
            }
nkeynes@736
   657
        } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
nkeynes@736
   658
            src_bytes = ((mip_width*mip_height)<<1);
nkeynes@736
   659
            unsigned char tmp[src_bytes];
nkeynes@736
   660
            if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@736
   661
                pvr2_vram64_read_twiddled_16( tmp, texture_addr, mip_width, mip_height );
nkeynes@736
   662
            } else {
nkeynes@736
   663
                pvr2_vram64_read( tmp, texture_addr, src_bytes );
nkeynes@736
   664
            }
nkeynes@736
   665
            yuv_decode( (uint32_t *)data, (uint32_t *)tmp, mip_width, mip_height );
nkeynes@736
   666
        } else if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@736
   667
            src_bytes = ((mip_width*mip_height) >> 2);
nkeynes@736
   668
            unsigned char tmp[src_bytes];
nkeynes@736
   669
            if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@736
   670
                pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width>>1, mip_height>>1 );
nkeynes@736
   671
            } else {
nkeynes@736
   672
                pvr2_vram64_read( tmp, texture_addr, src_bytes );
nkeynes@736
   673
            }
nkeynes@736
   674
            vq_decode( (uint16_t *)data, tmp, mip_width, mip_height, &codebook );
nkeynes@736
   675
        } else if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@736
   676
            pvr2_vram64_read_twiddled_16( data, texture_addr, mip_width, mip_height );
nkeynes@736
   677
        } else {
nkeynes@736
   678
            pvr2_vram64_read( data, texture_addr, src_bytes );
nkeynes@736
   679
        }
nkeynes@270
   680
nkeynes@736
   681
        /* Pass to GL */
nkeynes@736
   682
        if( level == last_level && level != 0 ) { /* 1x1 stored within a 2x2 */
nkeynes@736
   683
            glTexImage2D( GL_TEXTURE_2D, level, intFormat, 1, 1, 0, format, type,
nkeynes@736
   684
                    data + (3 << bpp_shift) );
nkeynes@736
   685
        } else {
nkeynes@736
   686
            glTexImage2D( GL_TEXTURE_2D, level, intFormat, mip_width, mip_height, 0, format, type,
nkeynes@736
   687
                    data );
nkeynes@736
   688
            if( mip_width > 2 ) {
nkeynes@736
   689
                mip_width >>= 1;
nkeynes@736
   690
                mip_height >>= 1;
nkeynes@736
   691
                dest_bytes >>= 2;
nkeynes@736
   692
                src_bytes >>= 2;
nkeynes@736
   693
            }
nkeynes@736
   694
            texture_addr -= src_bytes;
nkeynes@736
   695
        }
nkeynes@103
   696
    }
nkeynes@270
   697
nkeynes@1140
   698
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
nkeynes@1140
   699
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter);
nkeynes@103
   700
}
nkeynes@103
   701
nkeynes@1135
   702
static int texcache_find_texture_slot( uint32_t poly2_masked_word, uint32_t texture_word )
nkeynes@103
   703
{
nkeynes@653
   704
    uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
nkeynes@103
   705
    uint32_t texture_page = texture_addr >> 12;
nkeynes@462
   706
    texcache_entry_index next;
nkeynes@103
   707
    texcache_entry_index idx = texcache_page_lookup[texture_page];
nkeynes@103
   708
    while( idx != EMPTY_ENTRY ) {
nkeynes@736
   709
        texcache_entry_t entry = &texcache_active_list[idx];
nkeynes@1135
   710
        if( entry->tex_mode == texture_word &&
nkeynes@1135
   711
                entry->poly2_mode == poly2_masked_word ) {
nkeynes@736
   712
            entry->lru_count = texcache_ref_counter++;
nkeynes@856
   713
            return idx;
nkeynes@736
   714
        }
nkeynes@103
   715
        idx = entry->next;
nkeynes@103
   716
    }
nkeynes@856
   717
    return -1;
nkeynes@856
   718
}
nkeynes@103
   719
nkeynes@1135
   720
static int texcache_alloc_texture_slot( uint32_t poly2_word, uint32_t texture_word )
nkeynes@856
   721
{
nkeynes@856
   722
    uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
nkeynes@856
   723
    uint32_t texture_page = texture_addr >> 12;
nkeynes@462
   724
    texcache_entry_index slot = 0;
nkeynes@103
   725
nkeynes@103
   726
    if( texcache_free_ptr < MAX_TEXTURES ) {
nkeynes@736
   727
        slot = texcache_free_list[texcache_free_ptr++];
nkeynes@103
   728
    } else {
nkeynes@736
   729
        slot = texcache_evict_lru();
nkeynes@103
   730
    }
nkeynes@103
   731
nkeynes@103
   732
    /* Construct new entry */
nkeynes@886
   733
    assert( texcache_active_list[slot].texture_addr == -1 );
nkeynes@103
   734
    texcache_active_list[slot].texture_addr = texture_addr;
nkeynes@1135
   735
    texcache_active_list[slot].tex_mode = texture_word;
nkeynes@1135
   736
    texcache_active_list[slot].poly2_mode = poly2_word;
nkeynes@103
   737
    texcache_active_list[slot].lru_count = texcache_ref_counter++;
nkeynes@103
   738
nkeynes@103
   739
    /* Add entry to the lookup table */
nkeynes@856
   740
    int next = texcache_page_lookup[texture_page];
nkeynes@462
   741
    if( next == slot ) {
nkeynes@736
   742
        int i;
nkeynes@736
   743
        fprintf( stderr, "Active list: " );
nkeynes@736
   744
        for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   745
            fprintf( stderr, "%d, ", texcache_active_list[i].next );
nkeynes@736
   746
        }
nkeynes@736
   747
        fprintf( stderr, "\n" );
nkeynes@736
   748
        assert( next != slot );
nkeynes@736
   749
nkeynes@462
   750
    }
nkeynes@462
   751
    texcache_active_list[slot].next = next;
nkeynes@103
   752
    texcache_page_lookup[texture_page] = slot;
nkeynes@856
   753
    return slot;
nkeynes@856
   754
}
nkeynes@103
   755
nkeynes@103
   756
/**
nkeynes@103
   757
 * Return a texture ID for the texture specified at the supplied address
nkeynes@103
   758
 * and given parameters (the same sequence of bytes could in theory have
nkeynes@103
   759
 * multiple interpretations). We use the texture address as the primary
nkeynes@1135
   760
 * index, but allow for multiple instances at each address.
nkeynes@103
   761
 * 
nkeynes@103
   762
 * If the texture has already been bound, return the ID to which it was
nkeynes@103
   763
 * bound. Otherwise obtain an unused texture ID and set it up appropriately.
nkeynes@1135
   764
 * The current GL_TEXTURE_2D binding will be changed in this case.
nkeynes@103
   765
 */
nkeynes@1135
   766
GLuint texcache_get_texture( uint32_t poly2_word, uint32_t texture_word )
nkeynes@103
   767
{
nkeynes@1135
   768
    poly2_word &= 0x000F803F; /* Get just the texture-relevant bits */
nkeynes@1140
   769
    uint32_t texture_lookup = texture_word;
nkeynes@1140
   770
    if( PVR2_TEX_IS_PALETTE(texture_lookup) ) {
nkeynes@1140
   771
        texture_lookup &= 0xF81FFFFF; /* Mask out the bank bits */
nkeynes@1140
   772
    }
nkeynes@1140
   773
    int slot = texcache_find_texture_slot( poly2_word, texture_lookup );
nkeynes@856
   774
nkeynes@856
   775
    if( slot == -1 ) {
nkeynes@856
   776
        /* Not found - check the free list */
nkeynes@1140
   777
        slot = texcache_alloc_texture_slot( poly2_word, texture_lookup );
nkeynes@856
   778
        
nkeynes@856
   779
        /* Construct the GL texture */
nkeynes@856
   780
        uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
nkeynes@1135
   781
        unsigned width = POLY2_TEX_WIDTH(poly2_word);
nkeynes@1135
   782
        unsigned height = POLY2_TEX_HEIGHT(poly2_word);
nkeynes@1135
   783
nkeynes@856
   784
        glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
nkeynes@856
   785
        texcache_load_texture( texture_addr, width, height, texture_word );
nkeynes@1135
   786
nkeynes@1135
   787
        /* Set texture parameters from the poly2 word */
nkeynes@1135
   788
        if( POLY2_TEX_CLAMP_U(poly2_word) ) {
nkeynes@1210
   789
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
nkeynes@1135
   790
        } else if( POLY2_TEX_MIRROR_U(poly2_word) ) {
nkeynes@1210
   791
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT );
nkeynes@1135
   792
        } else {
nkeynes@1135
   793
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
nkeynes@1135
   794
        }
nkeynes@1135
   795
        if( POLY2_TEX_CLAMP_V(poly2_word) ) {
nkeynes@1210
   796
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
nkeynes@1135
   797
        } else if( POLY2_TEX_MIRROR_V(poly2_word) ) {
nkeynes@1210
   798
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT );
nkeynes@1135
   799
        } else {
nkeynes@1135
   800
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
nkeynes@1135
   801
        }
nkeynes@103
   802
    }
nkeynes@103
   803
nkeynes@103
   804
    return texcache_active_list[slot].texture_id;
nkeynes@103
   805
}
nkeynes@653
   806
nkeynes@1135
   807
#if 0
nkeynes@856
   808
render_buffer_t texcache_get_render_buffer( uint32_t texture_addr, int mode, int width, int height )
nkeynes@856
   809
{
nkeynes@856
   810
    uint32_t texture_word = ((texture_addr >> 3) & 0x000FFFFF) | PVR2_TEX_UNTWIDDLED;
nkeynes@856
   811
    switch( mode ) {
nkeynes@856
   812
    case COLFMT_BGRA1555: texture_word |= PVR2_TEX_FORMAT_ARGB1555; break;
nkeynes@856
   813
    case COLFMT_RGB565:   texture_word |= PVR2_TEX_FORMAT_RGB565; break;
nkeynes@856
   814
    case COLFMT_BGRA4444: texture_word |= PVR2_TEX_FORMAT_ARGB4444; break;
nkeynes@856
   815
    default:
nkeynes@856
   816
        WARN( "Rendering to non-texture colour format" );
nkeynes@856
   817
    }
nkeynes@856
   818
    if( is_npot_texture(width) )
nkeynes@856
   819
        texture_word |= PVR2_TEX_STRIDE;
nkeynes@635
   820
    
nkeynes@856
   821
    
nkeynes@856
   822
    int slot = texcache_find_texture_slot( texture_word, width, height );
nkeynes@856
   823
    if( slot == -1 ) {
nkeynes@856
   824
        slot = texcache_alloc_texture_slot( texture_word, width, height );
nkeynes@856
   825
    }
nkeynes@856
   826
    
nkeynes@856
   827
    texcache_entry_t entry = &texcache_active_list[slot];
nkeynes@103
   828
nkeynes@870
   829
    if( entry->buffer == NULL ) {
nkeynes@856
   830
        entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
nkeynes@870
   831
    } else if( entry->buffer->width != width || entry->buffer->height != height ) {        
nkeynes@870
   832
        texcache_release_render_buffer(entry->buffer);
nkeynes@870
   833
        entry->buffer = pvr2_create_render_buffer( texture_addr, width, height, entry->texture_id );
nkeynes@103
   834
    }
nkeynes@103
   835
nkeynes@856
   836
    return entry->buffer;
nkeynes@103
   837
}
nkeynes@1135
   838
#endif
nkeynes@635
   839
nkeynes@635
   840
/**
nkeynes@635
   841
 * Check the integrity of the texcache. Verifies that every cache slot
nkeynes@635
   842
 * appears exactly once on either the free list or one page list. For 
nkeynes@635
   843
 * active slots, the texture address must also match the page it appears on.
nkeynes@635
   844
 * 
nkeynes@635
   845
 */
nkeynes@635
   846
void texcache_integrity_check()
nkeynes@635
   847
{
nkeynes@635
   848
    int i;
nkeynes@635
   849
    int slot_found[MAX_TEXTURES];
nkeynes@736
   850
nkeynes@635
   851
    memset( slot_found, 0, sizeof(slot_found) );
nkeynes@635
   852
nkeynes@635
   853
    /* Check entries on the free list */
nkeynes@635
   854
    for( i= texcache_free_ptr; i< MAX_TEXTURES; i++ ) {
nkeynes@736
   855
        int slot = texcache_free_list[i];
nkeynes@736
   856
        assert( slot_found[slot] == 0 );
nkeynes@736
   857
        assert( texcache_active_list[slot].next == EMPTY_ENTRY );
nkeynes@736
   858
        slot_found[slot] = 1;
nkeynes@635
   859
    }
nkeynes@635
   860
nkeynes@635
   861
    /* Check entries on the active lists */
nkeynes@635
   862
    for( i=0; i< PVR2_RAM_PAGES; i++ ) {
nkeynes@736
   863
        int slot = texcache_page_lookup[i];
nkeynes@736
   864
        while( slot != EMPTY_ENTRY ) {
nkeynes@736
   865
            assert( slot_found[slot] == 0 );
nkeynes@736
   866
            assert( (texcache_active_list[slot].texture_addr >> 12) == i );
nkeynes@736
   867
            slot_found[slot] = 2;
nkeynes@736
   868
            slot = texcache_active_list[slot].next;
nkeynes@736
   869
        }
nkeynes@635
   870
    }
nkeynes@635
   871
nkeynes@635
   872
    /* Make sure we didn't miss any entries */
nkeynes@635
   873
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   874
        assert( slot_found[i] != 0 );
nkeynes@635
   875
    }
nkeynes@635
   876
}
nkeynes@1140
   877
nkeynes@1140
   878
/**
nkeynes@1140
   879
 * Dump the contents of the texture cache
nkeynes@1140
   880
 */
nkeynes@1140
   881
void texcache_dump()
nkeynes@1140
   882
{
nkeynes@1140
   883
    unsigned i;
nkeynes@1165
   884
    GLboolean boolresult;
nkeynes@1140
   885
    for( i=0; i< PVR2_RAM_PAGES; i++ ) {
nkeynes@1140
   886
        int slot = texcache_page_lookup[i];
nkeynes@1140
   887
        while( slot != EMPTY_ENTRY ) {
nkeynes@1165
   888
            fprintf( stderr, "%-3d: %08X %dx%d (%08X %08X) %s\n", slot,
nkeynes@1140
   889
                    texcache_active_list[slot].texture_addr,
nkeynes@1140
   890
                    POLY2_TEX_WIDTH(texcache_active_list[slot].poly2_mode),
nkeynes@1140
   891
                    POLY2_TEX_HEIGHT(texcache_active_list[slot].poly2_mode),
nkeynes@1140
   892
                    texcache_active_list[slot].poly2_mode,
nkeynes@1165
   893
                    texcache_active_list[slot].tex_mode,
nkeynes@1221
   894
#ifdef HAVE_OPENGL_TEX_RESIDENT
nkeynes@1221
   895
                    (glAreTexturesResident(1, &texcache_active_list[slot].texture_id, &boolresult) ? "[RESIDENT]" : "[NOT RESIDENT]")
nkeynes@1221
   896
#else
nkeynes@1221
   897
                    ""
nkeynes@1221
   898
#endif
nkeynes@1221
   899
                    );
nkeynes@1140
   900
            slot = texcache_active_list[slot].next;
nkeynes@1140
   901
        }
nkeynes@1140
   902
    }
nkeynes@1140
   903
}
nkeynes@1142
   904
nkeynes@1142
   905
void texcache_print_idx4( uint32_t texture_addr, int width )
nkeynes@1142
   906
{
nkeynes@1142
   907
    unsigned x,y;
nkeynes@1142
   908
    int src_bytes = (width*width>>1);
nkeynes@1142
   909
    char tmp[src_bytes];
nkeynes@1142
   910
    char data[width*width];
nkeynes@1142
   911
    pvr2_vram64_read_twiddled_4( tmp, texture_addr, width, width );
nkeynes@1142
   912
    decode_pal4_to_pal8( data, tmp, src_bytes );
nkeynes@1142
   913
    for( y=0; y<width; y++ ) {
nkeynes@1142
   914
        for( x=0; x<width; x++ ) {
nkeynes@1142
   915
            printf( "%1x", data[y*width+x] );
nkeynes@1142
   916
        }
nkeynes@1142
   917
        printf( "\n" );
nkeynes@1142
   918
    }
nkeynes@1142
   919
}
.