Search
lxdream.org :: lxdream/src/pvr2/texcache.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/texcache.c
changeset 1298:d0eb2307b847
prev1282:9f445c5e252b
author nkeynes
date Wed Feb 04 08:38:23 2015 +1000 (5 years ago)
permissions -rw-r--r--
last change Fix assorted compile warnings reported by Clang
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@108
    88
nkeynes@856
    89
void texcache_release_render_buffer( render_buffer_t buffer )
nkeynes@856
    90
{
nkeynes@1275
    91
    if( !buffer->flushed )
nkeynes@856
    92
        pvr2_render_buffer_copy_to_sh4(buffer);
nkeynes@856
    93
    pvr2_destroy_render_buffer(buffer);
nkeynes@856
    94
}
nkeynes@856
    95
nkeynes@108
    96
/**
nkeynes@103
    97
 * Flush all textures from the cache, returning them to the free list.
nkeynes@103
    98
 */
nkeynes@103
    99
void texcache_flush( )
nkeynes@103
   100
{
nkeynes@103
   101
    int i;
nkeynes@103
   102
    /* clear structures */
nkeynes@103
   103
    for( i=0; i<PVR2_RAM_PAGES; i++ ) {
nkeynes@736
   104
        texcache_page_lookup[i] = EMPTY_ENTRY;
nkeynes@103
   105
    }
nkeynes@103
   106
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   107
        texcache_free_list[i] = i;
nkeynes@736
   108
        texcache_active_list[i].next = EMPTY_ENTRY;
nkeynes@886
   109
        texcache_active_list[i].texture_addr = -1;
nkeynes@856
   110
        if( texcache_active_list[i].buffer != NULL ) {
nkeynes@856
   111
            texcache_release_render_buffer(texcache_active_list[i].buffer);
nkeynes@856
   112
            texcache_active_list[i].buffer = NULL;
nkeynes@856
   113
        }
nkeynes@103
   114
    }
nkeynes@103
   115
    texcache_free_ptr = 0;
nkeynes@103
   116
    texcache_ref_counter = 0;
nkeynes@103
   117
}
nkeynes@103
   118
nkeynes@103
   119
/**
nkeynes@1275
   120
 * Setup the initial texture ids (must be called after the GL context is
nkeynes@1275
   121
 * prepared)
nkeynes@1275
   122
 */
nkeynes@1275
   123
void texcache_gl_init( )
nkeynes@1275
   124
{
nkeynes@1275
   125
    int i;
nkeynes@1275
   126
    GLuint texids[MAX_TEXTURES];
nkeynes@1275
   127
nkeynes@1275
   128
    if( display_driver->capabilities.has_sl ) {
nkeynes@1275
   129
        texcache_have_palette_shader = TRUE;
nkeynes@1275
   130
        texcache_palette_valid = FALSE;
nkeynes@1275
   131
        glGenTextures(1, &texcache_palette_texid );
nkeynes@1275
   132
nkeynes@1275
   133
        /* Bind the texture and set the params */
nkeynes@1275
   134
        glActiveTexture(GL_TEXTURE1);
nkeynes@1275
   135
        glBindTexture(GL_TEXTURE_2D, texcache_palette_texid);
nkeynes@1275
   136
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nkeynes@1275
   137
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nkeynes@1275
   138
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
nkeynes@1275
   139
        glActiveTexture(GL_TEXTURE0);
nkeynes@1275
   140
nkeynes@1275
   141
    } else {
nkeynes@1275
   142
        texcache_have_palette_shader = FALSE;
nkeynes@1275
   143
    }
nkeynes@1275
   144
nkeynes@1275
   145
    glGenTextures( MAX_TEXTURES, texids );
nkeynes@1275
   146
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@1275
   147
        texcache_active_list[i].texture_id = texids[i];
nkeynes@1275
   148
    }
nkeynes@1275
   149
    INFO( "Texcache initialized (%s, %s)", (texcache_have_palette_shader ? "Palette shader" : "No palette support"),
nkeynes@1282
   150
            (display_driver->capabilities.has_bgra ? "BGRA" : "RGBA") );
nkeynes@1275
   151
}
nkeynes@1275
   152
nkeynes@1275
   153
/**
nkeynes@103
   154
 * Flush all textures and delete. The cache will be non-functional until
nkeynes@1275
   155
 * the next call to texcache_gl_init(). This would typically be done if
nkeynes@103
   156
 * switching GL targets.
nkeynes@103
   157
 */    
nkeynes@1275
   158
void texcache_gl_shutdown( )
nkeynes@103
   159
{
nkeynes@103
   160
    GLuint texids[MAX_TEXTURES];
nkeynes@103
   161
    int i;
nkeynes@103
   162
    texcache_flush();
nkeynes@736
   163
nkeynes@1275
   164
    if( texcache_have_palette_shader ) {
nkeynes@1140
   165
        glDeleteTextures( 1, &texcache_palette_texid );
nkeynes@1275
   166
        texcache_palette_texid = -1;
nkeynes@1275
   167
    }
nkeynes@1140
   168
nkeynes@103
   169
    for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   170
        texids[i] = texcache_active_list[i].texture_id;
nkeynes@1275
   171
        texcache_active_list[i].texture_id = -1;
nkeynes@103
   172
    }
nkeynes@103
   173
    glDeleteTextures( MAX_TEXTURES, texids );
nkeynes@103
   174
}
nkeynes@103
   175
nkeynes@337
   176
static void texcache_evict( int slot )
nkeynes@103
   177
{
nkeynes@103
   178
    /* Remove the selected slot from the lookup table */
nkeynes@462
   179
    assert( texcache_active_list[slot].texture_addr != -1 );
nkeynes@270
   180
    uint32_t evict_page = texcache_active_list[slot].texture_addr >> 12;
nkeynes@103
   181
    texcache_entry_index replace_next = texcache_active_list[slot].next;
nkeynes@337
   182
    texcache_active_list[slot].texture_addr = -1;
nkeynes@103
   183
    texcache_active_list[slot].next = EMPTY_ENTRY; /* Just for safety */
nkeynes@856
   184
    if( texcache_active_list[slot].buffer != NULL ) {
nkeynes@856
   185
        texcache_release_render_buffer(texcache_active_list[slot].buffer);
nkeynes@856
   186
        texcache_active_list[slot].buffer = NULL;
nkeynes@856
   187
    }
nkeynes@103
   188
    if( texcache_page_lookup[evict_page] == slot ) {
nkeynes@736
   189
        texcache_page_lookup[evict_page] = replace_next;
nkeynes@103
   190
    } else {
nkeynes@736
   191
        texcache_entry_index idx = texcache_page_lookup[evict_page];
nkeynes@736
   192
        texcache_entry_index next;
nkeynes@736
   193
        do {
nkeynes@736
   194
            next = texcache_active_list[idx].next;
nkeynes@736
   195
            if( next == slot ) {
nkeynes@736
   196
                assert( idx != replace_next );
nkeynes@736
   197
                texcache_active_list[idx].next = replace_next;
nkeynes@736
   198
                break;
nkeynes@736
   199
            }
nkeynes@736
   200
            idx = next;
nkeynes@736
   201
        } while( next != EMPTY_ENTRY );
nkeynes@103
   202
    }
nkeynes@337
   203
}
nkeynes@337
   204
nkeynes@337
   205
/**
nkeynes@337
   206
 * Evict a single texture from the cache.
nkeynes@337
   207
 * @return the slot of the evicted texture.
nkeynes@337
   208
 */
nkeynes@337
   209
static texcache_entry_index texcache_evict_lru( void )
nkeynes@337
   210
{
nkeynes@337
   211
    /* Full table scan - take over the entry with the lowest lru value */
nkeynes@337
   212
    texcache_entry_index slot = 0;
nkeynes@337
   213
    int lru_value = texcache_active_list[0].lru_count;
nkeynes@337
   214
    int i;
nkeynes@337
   215
    for( i=1; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   216
        /* FIXME: account for rollover */
nkeynes@736
   217
        if( texcache_active_list[i].lru_count < lru_value ) {
nkeynes@736
   218
            slot = i;
nkeynes@736
   219
            lru_value = texcache_active_list[i].lru_count;
nkeynes@736
   220
        }
nkeynes@337
   221
    }
nkeynes@337
   222
    texcache_evict(slot);
nkeynes@736
   223
nkeynes@103
   224
    return slot;
nkeynes@103
   225
}
nkeynes@103
   226
nkeynes@337
   227
/**
nkeynes@337
   228
 * Evict all textures contained in the page identified by a texture address.
nkeynes@337
   229
 */
nkeynes@337
   230
void texcache_invalidate_page( uint32_t texture_addr ) {
nkeynes@337
   231
    uint32_t texture_page = texture_addr >> 12;
nkeynes@337
   232
    texcache_entry_index idx = texcache_page_lookup[texture_page];
nkeynes@337
   233
    if( idx == EMPTY_ENTRY )
nkeynes@736
   234
        return;
nkeynes@337
   235
    assert( texcache_free_ptr >= 0 );
nkeynes@337
   236
    do {
nkeynes@736
   237
        texcache_entry_t entry = &texcache_active_list[idx];
nkeynes@736
   238
        entry->texture_addr = -1;
nkeynes@856
   239
        if( entry->buffer != NULL ) {
nkeynes@856
   240
            texcache_release_render_buffer(entry->buffer);
nkeynes@856
   241
            entry->buffer = NULL;
nkeynes@856
   242
        }
nkeynes@736
   243
        /* release entry */
nkeynes@736
   244
        texcache_free_ptr--;
nkeynes@736
   245
        texcache_free_list[texcache_free_ptr] = idx;
nkeynes@736
   246
        idx = entry->next;
nkeynes@736
   247
        entry->next = EMPTY_ENTRY;
nkeynes@337
   248
    } while( idx != EMPTY_ENTRY );
nkeynes@337
   249
    texcache_page_lookup[texture_page] = EMPTY_ENTRY;
nkeynes@337
   250
}
nkeynes@337
   251
nkeynes@337
   252
/**
nkeynes@1140
   253
 * Load the palette into 4 textures of 256 entries each. This mirrors the
nkeynes@1140
   254
 * banking done by the PVR2 for 8-bit textures, and also ensures that we
nkeynes@1140
   255
 * can use 8-bit paletted textures ourselves.
nkeynes@1140
   256
 */
nkeynes@1140
   257
static void texcache_load_palette_texture( gboolean format_changed )
nkeynes@1140
   258
{
nkeynes@1140
   259
    GLint format, type, intFormat = GL_RGBA;
nkeynes@1140
   260
    unsigned i;
nkeynes@1140
   261
    int bpp = 2;
nkeynes@1140
   262
    uint32_t *palette = (uint32_t *)mmio_region_PVR2PAL.mem;
nkeynes@1282
   263
    uint16_t packed_palette[1024];
nkeynes@1298
   264
    unsigned char *data = (unsigned char *)palette;
nkeynes@1140
   265
nkeynes@1140
   266
    switch( texcache_palette_mode ) {
nkeynes@1140
   267
    case 0: /* ARGB1555 */
nkeynes@1140
   268
        format = GL_BGRA;
nkeynes@1140
   269
        type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
nkeynes@1140
   270
        break;
nkeynes@1140
   271
    case 1:  /* RGB565 */
nkeynes@1140
   272
        intFormat = GL_RGB;
nkeynes@1140
   273
        format = GL_RGB;
nkeynes@1140
   274
        type = GL_UNSIGNED_SHORT_5_6_5;
nkeynes@1140
   275
        break;
nkeynes@1140
   276
    case 2: /* ARGB4444 */
nkeynes@1140
   277
        format = GL_BGRA;
nkeynes@1140
   278
        type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
nkeynes@1140
   279
        break;
nkeynes@1140
   280
    case 3: /* ARGB8888 */
nkeynes@1140
   281
        format = GL_BGRA;
nkeynes@1140
   282
        type = GL_UNSIGNED_BYTE;
nkeynes@1140
   283
        bpp = 4;
nkeynes@1140
   284
        break;
nkeynes@1140
   285
    default:
nkeynes@1140
   286
        break; /* Can't happen */
nkeynes@1140
   287
    }
nkeynes@1140
   288
nkeynes@1140
   289
    if( bpp == 2 ) {
nkeynes@1140
   290
        for( i=0; i<1024; i++ ) {
nkeynes@1140
   291
            packed_palette[i] = (uint16_t)palette[i];
nkeynes@1140
   292
        }
nkeynes@1298
   293
        data = (unsigned char *)packed_palette;
nkeynes@1140
   294
    }
nkeynes@1140
   295
nkeynes@1140
   296
    glActiveTexture(GL_TEXTURE1);
nkeynes@1140
   297
    if( format_changed )
nkeynes@1298
   298
        glTexImage2DBGRA(0, intFormat, 1024, 1, format, type, data, data == (unsigned char *)palette );
nkeynes@1140
   299
    else
nkeynes@1298
   300
        glTexSubImage2DBGRA(0, 0, 0, 1024, 1, format, type, data, data == (unsigned char *)palette);
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@1275
   455
    return 0xFF000000 | (b<<16) | (g<<8) | (r);
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@103
   482
/**
nkeynes@103
   483
 * Load texture data from the given address and parameters into the currently
nkeynes@103
   484
 * bound OpenGL texture.
nkeynes@103
   485
 */
nkeynes@429
   486
static void texcache_load_texture( uint32_t texture_addr, int width, int height,
nkeynes@736
   487
                                   int mode ) {
nkeynes@284
   488
    int bpp_shift = 1; /* bytes per (output) pixel as a power of 2 */
nkeynes@349
   489
    GLint intFormat = GL_RGBA, format, type;
nkeynes@108
   490
    int tex_format = mode & PVR2_TEX_FORMAT_MASK;
nkeynes@270
   491
    struct vq_codebook codebook;
nkeynes@1140
   492
    GLint min_filter = GL_LINEAR;
nkeynes@1140
   493
    GLint max_filter = GL_LINEAR;
nkeynes@1140
   494
    GLint mipmapfilter = GL_LINEAR_MIPMAP_LINEAR;
nkeynes@108
   495
nkeynes@270
   496
    /* Decode the format parameters */
nkeynes@270
   497
    switch( tex_format ) {
nkeynes@270
   498
    case PVR2_TEX_FORMAT_IDX4:
nkeynes@270
   499
    case PVR2_TEX_FORMAT_IDX8:
nkeynes@1140
   500
        if( texcache_have_palette_shader ) {
nkeynes@1221
   501
            intFormat = GL_ALPHA;
nkeynes@1140
   502
            format = GL_ALPHA;
nkeynes@736
   503
            type = GL_UNSIGNED_BYTE;
nkeynes@1140
   504
            bpp_shift = 0;
nkeynes@1140
   505
            min_filter = max_filter = GL_NEAREST;
nkeynes@1140
   506
            mipmapfilter = GL_NEAREST_MIPMAP_NEAREST;
nkeynes@1140
   507
        } else {
nkeynes@1140
   508
            /* For indexed-colour modes, we need to lookup the palette control
nkeynes@1140
   509
             * word to determine the de-indexed texture format.
nkeynes@1140
   510
             */
nkeynes@1140
   511
            switch( texcache_palette_mode ) {
nkeynes@1140
   512
            case 0: /* ARGB1555 */
nkeynes@1140
   513
                format = GL_BGRA;
nkeynes@1140
   514
                type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
nkeynes@1140
   515
                break;
nkeynes@1140
   516
            case 1:  /* RGB565 */
nkeynes@1140
   517
                intFormat = GL_RGB;
nkeynes@1140
   518
                format = GL_RGB;
nkeynes@1140
   519
                type = GL_UNSIGNED_SHORT_5_6_5;
nkeynes@1140
   520
                break;
nkeynes@1140
   521
            case 2: /* ARGB4444 */
nkeynes@1140
   522
                format = GL_BGRA;
nkeynes@1140
   523
                type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
nkeynes@1140
   524
                break;
nkeynes@1140
   525
            case 3: /* ARGB8888 */
nkeynes@1140
   526
                format = GL_BGRA;
nkeynes@1140
   527
                type = GL_UNSIGNED_BYTE;
nkeynes@1140
   528
                bpp_shift = 2;
nkeynes@1140
   529
                break;
nkeynes@1140
   530
            default:
nkeynes@1140
   531
                return; /* Can't happen, but it makes gcc stop complaining */
nkeynes@1140
   532
            }
nkeynes@736
   533
        }
nkeynes@736
   534
        break;
nkeynes@736
   535
nkeynes@860
   536
        default:
nkeynes@736
   537
        case PVR2_TEX_FORMAT_ARGB1555:
nkeynes@736
   538
            format = GL_BGRA;
nkeynes@736
   539
            type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
nkeynes@736
   540
            break;
nkeynes@736
   541
        case PVR2_TEX_FORMAT_RGB565:
nkeynes@736
   542
            intFormat = GL_RGB;
nkeynes@736
   543
            format = GL_RGB;
nkeynes@736
   544
            type = GL_UNSIGNED_SHORT_5_6_5;
nkeynes@736
   545
            break;
nkeynes@736
   546
        case PVR2_TEX_FORMAT_ARGB4444:
nkeynes@736
   547
            format = GL_BGRA;
nkeynes@736
   548
            type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
nkeynes@736
   549
            break;
nkeynes@736
   550
        case PVR2_TEX_FORMAT_YUV422:
nkeynes@736
   551
            /* YUV422 isn't directly supported by most implementations, so decode
nkeynes@1275
   552
             * it to a (reasonably) standard RGBA8.
nkeynes@736
   553
             */
nkeynes@736
   554
            bpp_shift = 2;
nkeynes@1275
   555
            format = GL_RGBA;
nkeynes@736
   556
            type = GL_UNSIGNED_BYTE;
nkeynes@736
   557
            break;
nkeynes@736
   558
        case PVR2_TEX_FORMAT_BUMPMAP:
nkeynes@860
   559
            WARN( "Bumpmap not supported" );
nkeynes@736
   560
            return;
nkeynes@270
   561
    }
nkeynes@736
   562
nkeynes@321
   563
    if( PVR2_TEX_IS_STRIDE(mode) && tex_format != PVR2_TEX_FORMAT_IDX4 &&
nkeynes@736
   564
            tex_format != PVR2_TEX_FORMAT_IDX8 ) {
nkeynes@736
   565
        /* Stride textures cannot be mip-mapped, compressed, indexed or twiddled */
nkeynes@736
   566
        unsigned char data[(width*height) << bpp_shift];
nkeynes@736
   567
        if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
nkeynes@736
   568
            unsigned char tmp[(width*height)<<1];
nkeynes@886
   569
            pvr2_vram64_read_stride( tmp, width<<1, texture_addr, texcache_stride_width<<1, height );
nkeynes@736
   570
            yuv_decode( (uint32_t *)data, (uint32_t *)tmp, width, height );
nkeynes@736
   571
        } else {
nkeynes@886
   572
            pvr2_vram64_read_stride( data, width<<bpp_shift, texture_addr, texcache_stride_width<<bpp_shift, height );
nkeynes@736
   573
        }
nkeynes@1282
   574
        glTexImage2DBGRA( 0, intFormat, width, height, format, type, data, FALSE );
nkeynes@1140
   575
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
nkeynes@1140
   576
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter);
nkeynes@736
   577
        return;
nkeynes@284
   578
    } 
nkeynes@284
   579
nkeynes@270
   580
    if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@736
   581
        uint16_t tmp[VQ_CODEBOOK_SIZE];
nkeynes@736
   582
        pvr2_vram64_read( (unsigned char *)tmp, texture_addr, VQ_CODEBOOK_SIZE );
nkeynes@736
   583
        texture_addr += VQ_CODEBOOK_SIZE;
nkeynes@736
   584
        vq_get_codebook( &codebook, tmp );
nkeynes@270
   585
    }
nkeynes@270
   586
nkeynes@349
   587
    int level=0, last_level = 0, mip_width = width, mip_height = height, src_bytes, dest_bytes;
nkeynes@270
   588
    if( PVR2_TEX_IS_MIPMAPPED(mode) ) {
nkeynes@736
   589
        uint32_t src_offset = 0;
nkeynes@1140
   590
        min_filter = mipmapfilter;
nkeynes@736
   591
        mip_height = height = width;
nkeynes@736
   592
        while( (1<<last_level) < width ) {
nkeynes@736
   593
            last_level++;
nkeynes@736
   594
            src_offset += ((width>>last_level)*(width>>last_level));
nkeynes@736
   595
        }
nkeynes@736
   596
        if( width != 1 ) {
nkeynes@736
   597
            src_offset += 3;
nkeynes@736
   598
        }
nkeynes@736
   599
        if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@736
   600
            src_offset >>= 2;
nkeynes@736
   601
        } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
nkeynes@736
   602
            src_offset >>= 1;
nkeynes@736
   603
        } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
nkeynes@736
   604
            src_offset <<= 1;
nkeynes@736
   605
        } else if( tex_format != PVR2_TEX_FORMAT_IDX8 ) {
nkeynes@736
   606
            src_offset <<= bpp_shift;
nkeynes@736
   607
        }
nkeynes@736
   608
        texture_addr += src_offset;
nkeynes@270
   609
    }
nkeynes@736
   610
nkeynes@654
   611
nkeynes@349
   612
    dest_bytes = (mip_width * mip_height) << bpp_shift;
nkeynes@349
   613
    src_bytes = dest_bytes; // Modes will change this (below)
nkeynes@108
   614
nkeynes@654
   615
    for( level=0; level<= last_level; level++ ) {
nkeynes@736
   616
        unsigned char data[dest_bytes];
nkeynes@736
   617
        /* load data from image, detwiddling/uncompressing as required */
nkeynes@736
   618
        if( tex_format == PVR2_TEX_FORMAT_IDX8 ) {
nkeynes@1140
   619
            if( texcache_have_palette_shader ) {
nkeynes@1140
   620
                pvr2_vram64_read_twiddled_8( data, texture_addr, mip_width, mip_height );
nkeynes@736
   621
            } else {
nkeynes@1140
   622
                src_bytes = (mip_width * mip_height);
nkeynes@1140
   623
                int bank = (mode >> 25) &0x03;
nkeynes@1140
   624
                uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<8);
nkeynes@1140
   625
                unsigned char tmp[src_bytes];
nkeynes@1140
   626
                pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width, mip_height );
nkeynes@1140
   627
                if( bpp_shift == 2 ) {
nkeynes@1140
   628
                    decode_pal8_to_32( (uint32_t *)data, tmp, src_bytes, palette );
nkeynes@1140
   629
                } else {
nkeynes@1140
   630
                    decode_pal8_to_16( (uint16_t *)data, tmp, src_bytes, palette );
nkeynes@1140
   631
                }
nkeynes@736
   632
            }
nkeynes@736
   633
        } else if( tex_format == PVR2_TEX_FORMAT_IDX4 ) {
nkeynes@736
   634
            src_bytes = (mip_width * mip_height) >> 1;
nkeynes@736
   635
            unsigned char tmp[src_bytes];
nkeynes@1140
   636
            if( texcache_have_palette_shader ) {
nkeynes@1140
   637
                pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
nkeynes@1140
   638
                decode_pal4_to_pal8( data, tmp, src_bytes );
nkeynes@736
   639
            } else {
nkeynes@1140
   640
                int bank = (mode >>21 ) & 0x3F;
nkeynes@1140
   641
                uint32_t *palette = ((uint32_t *)mmio_region_PVR2PAL.mem) + (bank<<4);
nkeynes@1140
   642
                pvr2_vram64_read_twiddled_4( tmp, texture_addr, mip_width, mip_height );
nkeynes@1140
   643
                if( bpp_shift == 2 ) {
nkeynes@1140
   644
                    decode_pal4_to_32( (uint32_t *)data, tmp, src_bytes, palette );
nkeynes@1140
   645
                } else {
nkeynes@1140
   646
                    decode_pal4_to_16( (uint16_t *)data, tmp, src_bytes, palette );
nkeynes@1140
   647
                }
nkeynes@736
   648
            }
nkeynes@736
   649
        } else if( tex_format == PVR2_TEX_FORMAT_YUV422 ) {
nkeynes@736
   650
            src_bytes = ((mip_width*mip_height)<<1);
nkeynes@736
   651
            unsigned char tmp[src_bytes];
nkeynes@736
   652
            if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@736
   653
                pvr2_vram64_read_twiddled_16( tmp, texture_addr, mip_width, mip_height );
nkeynes@736
   654
            } else {
nkeynes@736
   655
                pvr2_vram64_read( tmp, texture_addr, src_bytes );
nkeynes@736
   656
            }
nkeynes@736
   657
            yuv_decode( (uint32_t *)data, (uint32_t *)tmp, mip_width, mip_height );
nkeynes@736
   658
        } else if( PVR2_TEX_IS_COMPRESSED(mode) ) {
nkeynes@736
   659
            src_bytes = ((mip_width*mip_height) >> 2);
nkeynes@736
   660
            unsigned char tmp[src_bytes];
nkeynes@736
   661
            if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@736
   662
                pvr2_vram64_read_twiddled_8( tmp, texture_addr, mip_width>>1, mip_height>>1 );
nkeynes@736
   663
            } else {
nkeynes@736
   664
                pvr2_vram64_read( tmp, texture_addr, src_bytes );
nkeynes@736
   665
            }
nkeynes@736
   666
            vq_decode( (uint16_t *)data, tmp, mip_width, mip_height, &codebook );
nkeynes@736
   667
        } else if( PVR2_TEX_IS_TWIDDLED(mode) ) {
nkeynes@736
   668
            pvr2_vram64_read_twiddled_16( data, texture_addr, mip_width, mip_height );
nkeynes@736
   669
        } else {
nkeynes@736
   670
            pvr2_vram64_read( data, texture_addr, src_bytes );
nkeynes@736
   671
        }
nkeynes@270
   672
nkeynes@736
   673
        /* Pass to GL */
nkeynes@736
   674
        if( level == last_level && level != 0 ) { /* 1x1 stored within a 2x2 */
nkeynes@1282
   675
            glTexImage2DBGRA( level, intFormat, 1, 1, format, type,
nkeynes@1282
   676
                    data + (3 << bpp_shift), FALSE );
nkeynes@736
   677
        } else {
nkeynes@1282
   678
            glTexImage2DBGRA( level, intFormat, mip_width, mip_height, format, type, data, FALSE );
nkeynes@736
   679
            if( mip_width > 2 ) {
nkeynes@736
   680
                mip_width >>= 1;
nkeynes@736
   681
                mip_height >>= 1;
nkeynes@736
   682
                dest_bytes >>= 2;
nkeynes@736
   683
                src_bytes >>= 2;
nkeynes@736
   684
            }
nkeynes@736
   685
            texture_addr -= src_bytes;
nkeynes@736
   686
        }
nkeynes@103
   687
    }
nkeynes@270
   688
nkeynes@1140
   689
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
nkeynes@1140
   690
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter);
nkeynes@103
   691
}
nkeynes@103
   692
nkeynes@1135
   693
static int texcache_find_texture_slot( uint32_t poly2_masked_word, uint32_t texture_word )
nkeynes@103
   694
{
nkeynes@653
   695
    uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
nkeynes@103
   696
    uint32_t texture_page = texture_addr >> 12;
nkeynes@103
   697
    texcache_entry_index idx = texcache_page_lookup[texture_page];
nkeynes@103
   698
    while( idx != EMPTY_ENTRY ) {
nkeynes@736
   699
        texcache_entry_t entry = &texcache_active_list[idx];
nkeynes@1135
   700
        if( entry->tex_mode == texture_word &&
nkeynes@1135
   701
                entry->poly2_mode == poly2_masked_word ) {
nkeynes@736
   702
            entry->lru_count = texcache_ref_counter++;
nkeynes@856
   703
            return idx;
nkeynes@736
   704
        }
nkeynes@103
   705
        idx = entry->next;
nkeynes@103
   706
    }
nkeynes@856
   707
    return -1;
nkeynes@856
   708
}
nkeynes@103
   709
nkeynes@1135
   710
static int texcache_alloc_texture_slot( uint32_t poly2_word, uint32_t texture_word )
nkeynes@856
   711
{
nkeynes@856
   712
    uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
nkeynes@856
   713
    uint32_t texture_page = texture_addr >> 12;
nkeynes@462
   714
    texcache_entry_index slot = 0;
nkeynes@103
   715
nkeynes@103
   716
    if( texcache_free_ptr < MAX_TEXTURES ) {
nkeynes@736
   717
        slot = texcache_free_list[texcache_free_ptr++];
nkeynes@103
   718
    } else {
nkeynes@736
   719
        slot = texcache_evict_lru();
nkeynes@103
   720
    }
nkeynes@103
   721
nkeynes@103
   722
    /* Construct new entry */
nkeynes@886
   723
    assert( texcache_active_list[slot].texture_addr == -1 );
nkeynes@103
   724
    texcache_active_list[slot].texture_addr = texture_addr;
nkeynes@1135
   725
    texcache_active_list[slot].tex_mode = texture_word;
nkeynes@1135
   726
    texcache_active_list[slot].poly2_mode = poly2_word;
nkeynes@103
   727
    texcache_active_list[slot].lru_count = texcache_ref_counter++;
nkeynes@103
   728
nkeynes@103
   729
    /* Add entry to the lookup table */
nkeynes@856
   730
    int next = texcache_page_lookup[texture_page];
nkeynes@462
   731
    if( next == slot ) {
nkeynes@736
   732
        int i;
nkeynes@736
   733
        fprintf( stderr, "Active list: " );
nkeynes@736
   734
        for( i=0; i<MAX_TEXTURES; i++ ) {
nkeynes@736
   735
            fprintf( stderr, "%d, ", texcache_active_list[i].next );
nkeynes@736
   736
        }
nkeynes@736
   737
        fprintf( stderr, "\n" );
nkeynes@736
   738
        assert( next != slot );
nkeynes@736
   739
nkeynes@462
   740
    }
nkeynes@462
   741
    texcache_active_list[slot].next = next;
nkeynes@103
   742
    texcache_page_lookup[texture_page] = slot;
nkeynes@856
   743
    return slot;
nkeynes@856
   744
}
nkeynes@103
   745
nkeynes@103
   746
/**
nkeynes@103
   747
 * Return a texture ID for the texture specified at the supplied address
nkeynes@103
   748
 * and given parameters (the same sequence of bytes could in theory have
nkeynes@103
   749
 * multiple interpretations). We use the texture address as the primary
nkeynes@1135
   750
 * index, but allow for multiple instances at each address.
nkeynes@103
   751
 * 
nkeynes@103
   752
 * If the texture has already been bound, return the ID to which it was
nkeynes@103
   753
 * bound. Otherwise obtain an unused texture ID and set it up appropriately.
nkeynes@1135
   754
 * The current GL_TEXTURE_2D binding will be changed in this case.
nkeynes@103
   755
 */
nkeynes@1135
   756
GLuint texcache_get_texture( uint32_t poly2_word, uint32_t texture_word )
nkeynes@103
   757
{
nkeynes@1135
   758
    poly2_word &= 0x000F803F; /* Get just the texture-relevant bits */
nkeynes@1140
   759
    uint32_t texture_lookup = texture_word;
nkeynes@1140
   760
    if( PVR2_TEX_IS_PALETTE(texture_lookup) ) {
nkeynes@1140
   761
        texture_lookup &= 0xF81FFFFF; /* Mask out the bank bits */
nkeynes@1140
   762
    }
nkeynes@1140
   763
    int slot = texcache_find_texture_slot( poly2_word, texture_lookup );
nkeynes@856
   764
nkeynes@856
   765
    if( slot == -1 ) {
nkeynes@856
   766
        /* Not found - check the free list */
nkeynes@1140
   767
        slot = texcache_alloc_texture_slot( poly2_word, texture_lookup );
nkeynes@856
   768
        
nkeynes@856
   769
        /* Construct the GL texture */
nkeynes@856
   770
        uint32_t texture_addr = (texture_word & 0x000FFFFF)<<3;
nkeynes@1135
   771
        unsigned width = POLY2_TEX_WIDTH(poly2_word);
nkeynes@1135
   772
        unsigned height = POLY2_TEX_HEIGHT(poly2_word);
nkeynes@1135
   773
nkeynes@856
   774
        glBindTexture( GL_TEXTURE_2D, texcache_active_list[slot].texture_id );
nkeynes@1275
   775
        glGetError();
nkeynes@856
   776
        texcache_load_texture( texture_addr, width, height, texture_word );
nkeynes@1275
   777
        INFO( "Loaded texture %d: %x %dx%d %x (%x)", texcache_active_list[slot].texture_id, texture_addr, width, height, texture_word,
nkeynes@1275
   778
                glGetError() );
nkeynes@1135
   779
nkeynes@1135
   780
        /* Set texture parameters from the poly2 word */
nkeynes@1135
   781
        if( POLY2_TEX_CLAMP_U(poly2_word) ) {
nkeynes@1210
   782
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
nkeynes@1135
   783
        } else if( POLY2_TEX_MIRROR_U(poly2_word) ) {
nkeynes@1210
   784
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT );
nkeynes@1135
   785
        } else {
nkeynes@1135
   786
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
nkeynes@1135
   787
        }
nkeynes@1135
   788
        if( POLY2_TEX_CLAMP_V(poly2_word) ) {
nkeynes@1210
   789
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
nkeynes@1135
   790
        } else if( POLY2_TEX_MIRROR_V(poly2_word) ) {
nkeynes@1210
   791
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT );
nkeynes@1135
   792
        } else {
nkeynes@1135
   793
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
nkeynes@1135
   794
        }
nkeynes@103
   795
    }
nkeynes@103
   796
nkeynes@103
   797
    return texcache_active_list[slot].texture_id;
nkeynes@103
   798
}
nkeynes@653
   799
nkeynes@1135
   800
#if 0
nkeynes@1298
   801
nkeynes@1298
   802
static gboolean is_npot_texture( int width )
nkeynes@1298
   803
{
nkeynes@1298
   804
    while( width != 0 ) {
nkeynes@1298
   805
        if( width & 1 )
nkeynes@1298
   806
            return width != 1;
nkeynes@1298
   807
        width >>= 1;
nkeynes@1298
   808
    }
nkeynes@1298
   809
    return TRUE;
nkeynes@1298
   810
}
nkeynes@1298
   811
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@1221
   898
#ifdef HAVE_OPENGL_TEX_RESIDENT
nkeynes@1221
   899
                    (glAreTexturesResident(1, &texcache_active_list[slot].texture_id, &boolresult) ? "[RESIDENT]" : "[NOT RESIDENT]")
nkeynes@1221
   900
#else
nkeynes@1221
   901
                    ""
nkeynes@1221
   902
#endif
nkeynes@1221
   903
                    );
nkeynes@1140
   904
            slot = texcache_active_list[slot].next;
nkeynes@1140
   905
        }
nkeynes@1140
   906
    }
nkeynes@1140
   907
}
nkeynes@1142
   908
nkeynes@1142
   909
void texcache_print_idx4( uint32_t texture_addr, int width )
nkeynes@1142
   910
{
nkeynes@1142
   911
    unsigned x,y;
nkeynes@1142
   912
    int src_bytes = (width*width>>1);
nkeynes@1298
   913
    unsigned char tmp[src_bytes];
nkeynes@1298
   914
    unsigned char data[width*width];
nkeynes@1142
   915
    pvr2_vram64_read_twiddled_4( tmp, texture_addr, width, width );
nkeynes@1142
   916
    decode_pal4_to_pal8( data, tmp, src_bytes );
nkeynes@1142
   917
    for( y=0; y<width; y++ ) {
nkeynes@1142
   918
        for( x=0; x<width; x++ ) {
nkeynes@1142
   919
            printf( "%1x", data[y*width+x] );
nkeynes@1142
   920
        }
nkeynes@1142
   921
        printf( "\n" );
nkeynes@1142
   922
    }
nkeynes@1142
   923
}
.