Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 1136:078a2202958a
prev1135:68daed8f38af
next1137:4799d64b3478
author nkeynes
date Sun Oct 24 13:10:53 2010 +1000 (11 years ago)
permissions -rw-r--r--
last change Ensure stencil is disabled after use.
file annotate diff log raw
nkeynes@639
     1
/**
nkeynes@639
     2
 * $Id$
nkeynes@639
     3
 *
nkeynes@639
     4
 * Standard OpenGL rendering engine. 
nkeynes@639
     5
 *
nkeynes@639
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@639
     7
 *
nkeynes@639
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@639
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@639
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@639
    11
 * (at your option) any later version.
nkeynes@639
    12
 *
nkeynes@639
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@639
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@639
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@639
    16
 * GNU General Public License for more details.
nkeynes@639
    17
 */
nkeynes@639
    18
nkeynes@645
    19
#include <assert.h>
nkeynes@669
    20
#include <sys/time.h>
nkeynes@639
    21
#include "display.h"
nkeynes@639
    22
#include "pvr2/pvr2.h"
nkeynes@677
    23
#include "pvr2/pvr2mmio.h"
nkeynes@639
    24
#include "pvr2/scene.h"
nkeynes@665
    25
#include "pvr2/glutil.h"
nkeynes@653
    26
nkeynes@934
    27
#define IS_EMPTY_TILE_LIST(p) ((*((uint32_t *)(pvr2_main_ram+(p))) >> 28) == 0x0F)
nkeynes@639
    28
nkeynes@645
    29
int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
nkeynes@736
    30
        GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, 
nkeynes@736
    31
        GL_ALWAYS };
nkeynes@645
    32
int pvr2_poly_srcblend[8] = { 
nkeynes@736
    33
        GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
nkeynes@736
    34
        GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, 
nkeynes@736
    35
        GL_ONE_MINUS_DST_ALPHA };
nkeynes@645
    36
int pvr2_poly_dstblend[8] = {
nkeynes@736
    37
        GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
nkeynes@736
    38
        GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
nkeynes@736
    39
        GL_ONE_MINUS_DST_ALPHA };
nkeynes@645
    40
int pvr2_poly_texblend[4] = {
nkeynes@736
    41
        GL_REPLACE, 
nkeynes@736
    42
        GL_MODULATE,  
nkeynes@736
    43
        GL_DECAL, 
nkeynes@736
    44
        GL_MODULATE 
nkeynes@645
    45
};
nkeynes@645
    46
nkeynes@639
    47
/**
nkeynes@639
    48
 * Clip the tile bounds to the clipping plane. 
nkeynes@639
    49
 * @return TRUE if the tile was not clipped completely.
nkeynes@639
    50
 */
nkeynes@639
    51
static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
nkeynes@639
    52
{
nkeynes@639
    53
    if( tile[0] < clip[0] ) tile[0] = clip[0];
nkeynes@639
    54
    if( tile[1] > clip[1] ) tile[1] = clip[1];
nkeynes@639
    55
    if( tile[2] < clip[2] ) tile[2] = clip[2];
nkeynes@639
    56
    if( tile[3] > clip[3] ) tile[3] = clip[3];
nkeynes@639
    57
    return tile[0] < tile[1] && tile[2] < tile[3];
nkeynes@639
    58
}
nkeynes@639
    59
nkeynes@645
    60
void pvr2_scene_load_textures()
nkeynes@645
    61
{
nkeynes@645
    62
    int i;
nkeynes@886
    63
    
nkeynes@1135
    64
    texcache_begin_scene( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03,
nkeynes@886
    65
                         (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5 );
nkeynes@886
    66
    
nkeynes@645
    67
    for( i=0; i < pvr2_scene.poly_count; i++ ) {
nkeynes@736
    68
        struct polygon_struct *poly = &pvr2_scene.poly_array[i];
nkeynes@736
    69
        if( POLY1_TEXTURED(poly->context[0]) ) {
nkeynes@1135
    70
            poly->tex_id = texcache_get_texture( poly->context[1], poly->context[2] );
nkeynes@736
    71
            if( poly->mod_vertex_index != -1 ) {
nkeynes@863
    72
                if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
nkeynes@1135
    73
                    poly->mod_tex_id = texcache_get_texture( poly->context[3], poly->context[4] );
nkeynes@863
    74
                } else {
nkeynes@863
    75
                    poly->mod_tex_id = poly->tex_id;
nkeynes@863
    76
                }
nkeynes@736
    77
            }
nkeynes@736
    78
        } else {
nkeynes@736
    79
            poly->tex_id = -1;
nkeynes@736
    80
            poly->mod_tex_id = -1;
nkeynes@736
    81
        }
nkeynes@645
    82
    }
nkeynes@645
    83
}
nkeynes@645
    84
nkeynes@645
    85
nkeynes@645
    86
nkeynes@639
    87
/**
nkeynes@639
    88
 * Once-off call to setup the OpenGL context.
nkeynes@639
    89
 */
nkeynes@639
    90
void pvr2_setup_gl_context()
nkeynes@639
    91
{
nkeynes@665
    92
nkeynes@665
    93
    if( glsl_is_supported() ) {
nkeynes@1130
    94
        if( !glsl_load_shaders( ) ) {
nkeynes@665
    95
            WARN( "Unable to load GL shaders" );
nkeynes@665
    96
        }
nkeynes@665
    97
    }
nkeynes@736
    98
nkeynes@639
    99
    texcache_gl_init(); // Allocate texture IDs
nkeynes@1133
   100
    glDisable( GL_CULL_FACE );
nkeynes@639
   101
    glEnable( GL_BLEND );
nkeynes@649
   102
    glEnable( GL_DEPTH_TEST );
nkeynes@639
   103
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
nkeynes@639
   104
    glMatrixMode(GL_MODELVIEW);
nkeynes@639
   105
    glLoadIdentity();
nkeynes@639
   106
nkeynes@687
   107
#ifdef HAVE_OPENGL_CLAMP_COLOR
nkeynes@687
   108
    if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
nkeynes@687
   109
        glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
nkeynes@687
   110
        glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
nkeynes@687
   111
    }
nkeynes@687
   112
#endif
nkeynes@653
   113
nkeynes@639
   114
    glEnableClientState( GL_COLOR_ARRAY );
nkeynes@639
   115
    glEnableClientState( GL_VERTEX_ARRAY );
nkeynes@639
   116
    glEnableClientState( GL_TEXTURE_COORD_ARRAY );
nkeynes@639
   117
    glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
nkeynes@847
   118
    glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
nkeynes@639
   119
nkeynes@639
   120
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
nkeynes@639
   121
    glClearDepth(0);
nkeynes@639
   122
    glClearStencil(0);
nkeynes@1132
   123
nkeynes@1132
   124
    glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
nkeynes@1132
   125
    glFogi(GL_FOG_MODE, GL_LINEAR);
nkeynes@1132
   126
    glFogf(GL_FOG_START, 0.0);
nkeynes@1132
   127
    glFogf(GL_FOG_END, 1.0);
nkeynes@639
   128
}
nkeynes@639
   129
nkeynes@864
   130
/**
nkeynes@864
   131
 * Setup the basic context that's shared between normal and modified modes -
nkeynes@864
   132
 * depth, culling
nkeynes@864
   133
 */
nkeynes@865
   134
static void render_set_base_context( uint32_t poly1, GLint depth_mode )
nkeynes@864
   135
{
nkeynes@865
   136
    if( depth_mode == 0 ) {
nkeynes@865
   137
        glDepthFunc( POLY1_DEPTH_MODE(poly1) );
nkeynes@865
   138
    } else {
nkeynes@865
   139
        glDepthFunc(depth_mode);
nkeynes@865
   140
    }
nkeynes@865
   141
nkeynes@864
   142
    glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
nkeynes@865
   143
}
nkeynes@864
   144
nkeynes@865
   145
/**
nkeynes@865
   146
 * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
nkeynes@865
   147
 */
nkeynes@865
   148
static void render_set_tsp_context( uint32_t poly1, uint32_t poly2, uint32_t texture )
nkeynes@865
   149
{
nkeynes@863
   150
    glShadeModel( POLY1_SHADE_MODEL(poly1) );
nkeynes@863
   151
    if( POLY1_TEXTURED(poly1) ) {
nkeynes@863
   152
         glEnable(GL_TEXTURE_2D);
nkeynes@863
   153
         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
nkeynes@863
   154
     } else {
nkeynes@863
   155
         glDisable( GL_TEXTURE_2D );
nkeynes@863
   156
     }
nkeynes@1132
   157
nkeynes@863
   158
     switch( POLY2_FOG_MODE(poly2) ) {
nkeynes@863
   159
     case PVR2_POLY_FOG_LOOKUP:
nkeynes@863
   160
         glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
nkeynes@863
   161
         break;
nkeynes@863
   162
     case PVR2_POLY_FOG_VERTEX:
nkeynes@1132
   163
         glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
nkeynes@1132
   164
         break;
nkeynes@863
   165
     }
nkeynes@653
   166
nkeynes@863
   167
     int srcblend = POLY2_SRC_BLEND(poly2);
nkeynes@863
   168
     int destblend = POLY2_DEST_BLEND(poly2);
nkeynes@863
   169
     glBlendFunc( srcblend, destblend );
nkeynes@863
   170
nkeynes@863
   171
     if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
nkeynes@863
   172
         WARN( "Accumulation buffer not supported" );
nkeynes@863
   173
     }   
nkeynes@863
   174
}
nkeynes@863
   175
nkeynes@645
   176
/**
nkeynes@645
   177
 * Setup the GL context for the supplied polygon context.
nkeynes@645
   178
 * @param context pointer to 3 or 5 words of polygon context
nkeynes@865
   179
 * @param depth_mode force depth mode, or 0 to use the polygon's
nkeynes@865
   180
 * depth mode.
nkeynes@645
   181
 */
nkeynes@865
   182
void render_set_context( uint32_t *context, GLint depth_mode )
nkeynes@645
   183
{
nkeynes@865
   184
    render_set_base_context(context[0], depth_mode);
nkeynes@865
   185
    render_set_tsp_context(context[0],context[1],context[2]);
nkeynes@645
   186
}
nkeynes@645
   187
nkeynes@1133
   188
static inline void gl_draw_vertexes( struct polygon_struct *poly )
nkeynes@1133
   189
{
nkeynes@1133
   190
    do {
nkeynes@1133
   191
        glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
nkeynes@1133
   192
        poly = poly->sub_next;
nkeynes@1133
   193
    } while( poly != NULL );
nkeynes@1133
   194
}
nkeynes@1133
   195
nkeynes@1133
   196
static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
nkeynes@1133
   197
{
nkeynes@1133
   198
    do {
nkeynes@1133
   199
        glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
nkeynes@1133
   200
        poly = poly->sub_next;
nkeynes@1133
   201
    } while( poly != NULL );
nkeynes@1133
   202
}
nkeynes@645
   203
nkeynes@865
   204
static void gl_render_poly( struct polygon_struct *poly, GLint depth_mode )
nkeynes@639
   205
{
nkeynes@1133
   206
    if( poly->vertex_count == 0 )
nkeynes@1133
   207
        return; /* Culled */
nkeynes@1133
   208
nkeynes@645
   209
    if( poly->tex_id != -1 ) {
nkeynes@736
   210
        glBindTexture(GL_TEXTURE_2D, poly->tex_id);
nkeynes@645
   211
    }
nkeynes@863
   212
    if( poly->mod_vertex_index == -1 ) {
nkeynes@865
   213
        render_set_context( poly->context, depth_mode );
nkeynes@1133
   214
        gl_draw_vertexes(poly);
nkeynes@864
   215
    }  else {
nkeynes@864
   216
        glEnable( GL_STENCIL_TEST );
nkeynes@865
   217
        render_set_base_context( poly->context[0], depth_mode );
nkeynes@863
   218
        render_set_tsp_context( poly->context[0], poly->context[1], poly->context[2] );
nkeynes@864
   219
        glStencilFunc(GL_EQUAL, 0, 2);
nkeynes@1133
   220
        gl_draw_vertexes(poly);
nkeynes@863
   221
nkeynes@863
   222
        if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
nkeynes@863
   223
            if( poly->mod_tex_id != -1 ) {
nkeynes@863
   224
                glBindTexture(GL_TEXTURE_2D, poly->mod_tex_id);
nkeynes@863
   225
            }
nkeynes@863
   226
            render_set_tsp_context( poly->context[0], poly->context[3], poly->context[4] );
nkeynes@863
   227
        }
nkeynes@864
   228
        glStencilFunc(GL_EQUAL, 2, 2);
nkeynes@1133
   229
        gl_draw_mod_vertexes(poly);
nkeynes@1136
   230
        glDisable( GL_STENCIL_TEST );
nkeynes@863
   231
    }
nkeynes@639
   232
}
nkeynes@639
   233
nkeynes@687
   234
static void gl_render_bkgnd( struct polygon_struct *poly )
nkeynes@639
   235
{
nkeynes@687
   236
    if( poly->tex_id != -1 ) {
nkeynes@687
   237
        glBindTexture(GL_TEXTURE_2D, poly->tex_id);
nkeynes@687
   238
    }
nkeynes@865
   239
    render_set_context( poly->context, 0 );
nkeynes@687
   240
    glDisable( GL_DEPTH_TEST );
nkeynes@687
   241
    glBlendFunc( GL_ONE, GL_ZERO );
nkeynes@1133
   242
    gl_draw_vertexes(poly);
nkeynes@687
   243
    glEnable( GL_DEPTH_TEST );
nkeynes@687
   244
}
nkeynes@687
   245
nkeynes@865
   246
void gl_render_tilelist( pvraddr_t tile_entry, GLint depth_mode )
nkeynes@653
   247
{
nkeynes@934
   248
    uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
nkeynes@639
   249
    int strip_count;
nkeynes@639
   250
    struct polygon_struct *poly;
nkeynes@639
   251
nkeynes@863
   252
    if( !IS_TILE_PTR(tile_entry) )
nkeynes@863
   253
        return;
nkeynes@863
   254
nkeynes@639
   255
    while(1) {
nkeynes@736
   256
        uint32_t entry = *tile_list++;
nkeynes@736
   257
        switch( entry >> 28 ) {
nkeynes@736
   258
        case 0x0F:
nkeynes@736
   259
            return; // End-of-list
nkeynes@736
   260
        case 0x0E:
nkeynes@934
   261
            tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
nkeynes@736
   262
            break;
nkeynes@736
   263
        case 0x08: case 0x09: case 0x0A: case 0x0B:
nkeynes@736
   264
            strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@736
   265
            poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@736
   266
            while( strip_count > 0 ) {
nkeynes@736
   267
                assert( poly != NULL );
nkeynes@865
   268
                gl_render_poly( poly, depth_mode );
nkeynes@736
   269
                poly = poly->next;
nkeynes@736
   270
                strip_count--;
nkeynes@736
   271
            }
nkeynes@736
   272
            break;
nkeynes@736
   273
        default:
nkeynes@736
   274
            if( entry & 0x7E000000 ) {
nkeynes@736
   275
                poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@865
   276
                gl_render_poly( poly, depth_mode );
nkeynes@736
   277
            }
nkeynes@736
   278
        }
nkeynes@863
   279
    }       
nkeynes@863
   280
}
nkeynes@863
   281
nkeynes@863
   282
/**
nkeynes@863
   283
 * Render the tilelist with depthbuffer updates only. 
nkeynes@863
   284
 */
nkeynes@863
   285
void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
nkeynes@863
   286
{
nkeynes@934
   287
    uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
nkeynes@863
   288
    int strip_count;
nkeynes@863
   289
    struct polygon_struct *poly;
nkeynes@863
   290
    
nkeynes@863
   291
    if( !IS_TILE_PTR(tile_entry) )
nkeynes@863
   292
        return;
nkeynes@863
   293
nkeynes@863
   294
    glDisable( GL_TEXTURE_2D );
nkeynes@863
   295
    
nkeynes@863
   296
    while(1) {
nkeynes@863
   297
        uint32_t entry = *tile_list++;
nkeynes@863
   298
        switch( entry >> 28 ) {
nkeynes@863
   299
        case 0x0F:
nkeynes@863
   300
            return; // End-of-list
nkeynes@863
   301
        case 0x0E:
nkeynes@934
   302
            tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
nkeynes@863
   303
            break;
nkeynes@863
   304
        case 0x08: case 0x09: case 0x0A: case 0x0B:
nkeynes@863
   305
            strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@863
   306
            poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@863
   307
            while( strip_count > 0 ) {
nkeynes@1133
   308
                if( poly->vertex_count != 0 ) {
nkeynes@1133
   309
                    render_set_base_context(poly->context[0],0);
nkeynes@1133
   310
                    gl_draw_vertexes(poly);
nkeynes@1133
   311
                }
nkeynes@863
   312
                poly = poly->next;
nkeynes@863
   313
                strip_count--;
nkeynes@863
   314
            }
nkeynes@863
   315
            break;
nkeynes@863
   316
        default:
nkeynes@863
   317
            if( entry & 0x7E000000 ) {
nkeynes@863
   318
                poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@1133
   319
                if( poly->vertex_count != 0 ) {
nkeynes@1133
   320
                    render_set_base_context(poly->context[0],0);
nkeynes@1133
   321
                    gl_draw_vertexes(poly);
nkeynes@1133
   322
                }
nkeynes@863
   323
            }
nkeynes@863
   324
        }
nkeynes@863
   325
    }           
nkeynes@863
   326
}
nkeynes@863
   327
nkeynes@864
   328
static void drawrect2d( uint32_t tile_bounds[], float z )
nkeynes@864
   329
{
nkeynes@864
   330
    glBegin( GL_QUADS );
nkeynes@864
   331
    glVertex3f( tile_bounds[0], tile_bounds[2], z );
nkeynes@864
   332
    glVertex3f( tile_bounds[1], tile_bounds[2], z );
nkeynes@864
   333
    glVertex3f( tile_bounds[1], tile_bounds[3], z );
nkeynes@864
   334
    glVertex3f( tile_bounds[0], tile_bounds[3], z );
nkeynes@864
   335
    glEnd();
nkeynes@864
   336
}
nkeynes@864
   337
nkeynes@864
   338
void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
nkeynes@864
   339
{
nkeynes@864
   340
    /* A bit of explanation:
nkeynes@864
   341
     * In theory it works like this: generate a 1-bit stencil for each polygon
nkeynes@864
   342
     * volume, and then AND or OR it against the overall 1-bit tile stencil at 
nkeynes@864
   343
     * the end of the volume. 
nkeynes@864
   344
     * 
nkeynes@864
   345
     * The implementation here uses a 2-bit stencil buffer, where each volume
nkeynes@864
   346
     * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
nkeynes@864
   347
     * to update bit 1 accordingly and clear bit 0.
nkeynes@864
   348
     * 
nkeynes@864
   349
     * This could probably be more efficient, but at least it works correctly 
nkeynes@864
   350
     * now :)
nkeynes@864
   351
     */
nkeynes@864
   352
    
nkeynes@1133
   353
    if( poly->vertex_count == 0 )
nkeynes@1133
   354
        return; /* Culled */
nkeynes@1133
   355
nkeynes@1133
   356
    gl_draw_vertexes(poly);
nkeynes@1133
   357
nkeynes@1133
   358
nkeynes@864
   359
    
nkeynes@864
   360
    int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
nkeynes@864
   361
    if( poly_type == PVR2_VOLUME_REGION0 ) {
nkeynes@864
   362
        /* 00 => 00
nkeynes@864
   363
         * 01 => 00
nkeynes@864
   364
         * 10 => 10
nkeynes@864
   365
         * 11 => 00
nkeynes@864
   366
         */
nkeynes@864
   367
        glStencilMask( 0x03 );
nkeynes@864
   368
        glStencilFunc(GL_EQUAL, 0x02, 0x03);
nkeynes@864
   369
        glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
nkeynes@864
   370
        glDisable( GL_DEPTH_TEST );
nkeynes@864
   371
nkeynes@864
   372
        drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
nkeynes@864
   373
        
nkeynes@864
   374
        glEnable( GL_DEPTH_TEST );
nkeynes@864
   375
        glStencilMask( 0x01 );
nkeynes@864
   376
        glStencilFunc( GL_ALWAYS, 0, 1 );
nkeynes@864
   377
        glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
nkeynes@864
   378
    } else if( poly_type == PVR2_VOLUME_REGION1 ) {
nkeynes@864
   379
        /* This is harder with the standard stencil ops - do it in two passes
nkeynes@864
   380
         * 00 => 00 | 00 => 10
nkeynes@864
   381
         * 01 => 10 | 01 => 10
nkeynes@864
   382
         * 10 => 10 | 10 => 00
nkeynes@864
   383
         * 11 => 10 | 11 => 10
nkeynes@864
   384
         */
nkeynes@864
   385
        glStencilMask( 0x02 );
nkeynes@864
   386
        glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
nkeynes@864
   387
        glDisable( GL_DEPTH_TEST );
nkeynes@864
   388
        
nkeynes@864
   389
        drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
nkeynes@864
   390
        
nkeynes@864
   391
        glStencilMask( 0x03 );
nkeynes@864
   392
        glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
nkeynes@864
   393
        glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
nkeynes@864
   394
        
nkeynes@864
   395
        drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
nkeynes@864
   396
        
nkeynes@864
   397
        glEnable( GL_DEPTH_TEST );
nkeynes@864
   398
        glStencilMask( 0x01 );
nkeynes@864
   399
        glStencilFunc( GL_ALWAYS, 0, 1 );
nkeynes@864
   400
        glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );         
nkeynes@864
   401
    }
nkeynes@864
   402
}
nkeynes@864
   403
nkeynes@864
   404
void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
nkeynes@863
   405
{
nkeynes@934
   406
    uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
nkeynes@863
   407
    int strip_count;
nkeynes@863
   408
    struct polygon_struct *poly;
nkeynes@863
   409
nkeynes@863
   410
    if( !IS_TILE_PTR(tile_entry) )
nkeynes@863
   411
        return;
nkeynes@863
   412
nkeynes@863
   413
    glDisable( GL_TEXTURE_2D );
nkeynes@863
   414
    glEnable( GL_STENCIL_TEST );
nkeynes@863
   415
    glEnable( GL_DEPTH_TEST );
nkeynes@863
   416
    glDepthFunc( GL_LEQUAL );
nkeynes@863
   417
    
nkeynes@863
   418
    glStencilFunc( GL_ALWAYS, 0, 1 );
nkeynes@863
   419
    glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
nkeynes@864
   420
    glStencilMask( 0x01 );
nkeynes@863
   421
    glDepthMask( GL_FALSE );
nkeynes@863
   422
    
nkeynes@863
   423
    while(1) {
nkeynes@863
   424
        uint32_t entry = *tile_list++;
nkeynes@863
   425
        switch( entry >> 28 ) {
nkeynes@863
   426
        case 0x0F:
nkeynes@863
   427
            glDepthMask( GL_TRUE );
nkeynes@863
   428
            glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
nkeynes@1136
   429
            glDisable( GL_STENCIL_TEST );
nkeynes@863
   430
            return; // End-of-list
nkeynes@863
   431
        case 0x0E:
nkeynes@934
   432
            tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
nkeynes@863
   433
            break;
nkeynes@863
   434
        case 0x08: case 0x09: case 0x0A: case 0x0B:
nkeynes@863
   435
            strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@863
   436
            poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@863
   437
            while( strip_count > 0 ) {
nkeynes@864
   438
                gl_render_modifier_polygon( poly, tile_bounds );
nkeynes@863
   439
                poly = poly->next;
nkeynes@863
   440
                strip_count--;
nkeynes@863
   441
            }
nkeynes@863
   442
            break;
nkeynes@863
   443
        default:
nkeynes@863
   444
            if( entry & 0x7E000000 ) {
nkeynes@863
   445
                poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@864
   446
                gl_render_modifier_polygon( poly, tile_bounds );
nkeynes@863
   447
            }
nkeynes@863
   448
        }
nkeynes@863
   449
    }
nkeynes@863
   450
    
nkeynes@639
   451
}
nkeynes@639
   452
nkeynes@639
   453
nkeynes@639
   454
/**
nkeynes@639
   455
 * Render the currently defined scene in pvr2_scene
nkeynes@639
   456
 */
nkeynes@639
   457
void pvr2_scene_render( render_buffer_t buffer )
nkeynes@639
   458
{
nkeynes@639
   459
    /* Scene setup */
nkeynes@645
   460
    struct timeval start_tv, tex_tv, end_tv;
nkeynes@645
   461
nkeynes@645
   462
    gettimeofday(&start_tv, NULL);
nkeynes@639
   463
    display_driver->set_render_target(buffer);
nkeynes@639
   464
    pvr2_check_palette_changed();
nkeynes@645
   465
    pvr2_scene_load_textures();
nkeynes@736
   466
nkeynes@645
   467
    gettimeofday( &tex_tv, NULL );
nkeynes@645
   468
    uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
nkeynes@736
   469
    (tex_tv.tv_usec - start_tv.tv_usec)/1000;
nkeynes@645
   470
    DEBUG( "Scene setup in %dms", ms );
nkeynes@639
   471
nkeynes@639
   472
    /* Setup view projection matrix */
nkeynes@639
   473
    glMatrixMode(GL_PROJECTION);
nkeynes@639
   474
    glLoadIdentity();
nkeynes@639
   475
    float nearz = pvr2_scene.bounds[4];
nkeynes@639
   476
    float farz = pvr2_scene.bounds[5];
nkeynes@639
   477
    if( nearz == farz ) {
nkeynes@687
   478
        farz*= 4.0;
nkeynes@639
   479
    }
nkeynes@639
   480
    glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0, 
nkeynes@736
   481
             -farz, -nearz );
nkeynes@649
   482
    float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
nkeynes@649
   483
    glAlphaFunc( GL_GEQUAL, alphaRef );
nkeynes@639
   484
nkeynes@639
   485
    /* Clear the buffer (FIXME: May not want always want to do this) */
nkeynes@639
   486
    glDisable( GL_SCISSOR_TEST );
nkeynes@651
   487
    glDepthMask( GL_TRUE );
nkeynes@864
   488
    glStencilMask( 0x03 );
nkeynes@639
   489
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
nkeynes@639
   490
nkeynes@639
   491
    /* Setup vertex array pointers */
nkeynes@687
   492
    glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
nkeynes@687
   493
    glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
nkeynes@687
   494
    glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
nkeynes@687
   495
    glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
nkeynes@847
   496
    glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
nkeynes@669
   497
    /* Turn on the shaders (if available) */
nkeynes@1130
   498
    glsl_set_shader(DEFAULT_PROGRAM);
nkeynes@639
   499
nkeynes@687
   500
    /* Render the background */
nkeynes@687
   501
    gl_render_bkgnd( pvr2_scene.bkgnd_poly );
nkeynes@736
   502
nkeynes@639
   503
    glEnable( GL_SCISSOR_TEST );
nkeynes@1132
   504
    glEnable( GL_COLOR_SUM );
nkeynes@1132
   505
    glEnable( GL_FOG );
nkeynes@639
   506
nkeynes@639
   507
    /* Process the segment list */
nkeynes@639
   508
    struct tile_segment *segment = pvr2_scene.segment_list;
nkeynes@639
   509
    do {
nkeynes@736
   510
        int tilex = SEGMENT_X(segment->control);
nkeynes@736
   511
        int tiley = SEGMENT_Y(segment->control);
nkeynes@639
   512
nkeynes@736
   513
        uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
nkeynes@736
   514
        if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
nkeynes@736
   515
            continue; // fully clipped, skip tile
nkeynes@736
   516
        }
nkeynes@736
   517
nkeynes@736
   518
        /* Clip to the visible part of the tile */
nkeynes@736
   519
        glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], 
nkeynes@736
   520
                   tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
nkeynes@864
   521
        if( display_driver->capabilities.stencil_bits >= 2 && 
nkeynes@863
   522
                IS_TILE_PTR(segment->opaquemod_ptr) &&
nkeynes@863
   523
                !IS_EMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
nkeynes@863
   524
            /* Don't do this unless there's actually some shadow polygons */
nkeynes@863
   525
nkeynes@863
   526
            /* Use colormask instead of drawbuffer for simplicity */
nkeynes@863
   527
            glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
nkeynes@863
   528
            gl_render_tilelist_depthonly(segment->opaque_ptr);
nkeynes@864
   529
            gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
nkeynes@863
   530
            glClear( GL_DEPTH_BUFFER_BIT );
nkeynes@863
   531
            glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
nkeynes@736
   532
        }
nkeynes@865
   533
        gl_render_tilelist(segment->opaque_ptr,0);
nkeynes@863
   534
        if( IS_TILE_PTR(segment->punchout_ptr) ) {
nkeynes@863
   535
            glEnable(GL_ALPHA_TEST );
nkeynes@865
   536
            gl_render_tilelist(segment->punchout_ptr, GL_GEQUAL );
nkeynes@863
   537
            glDisable(GL_ALPHA_TEST );
nkeynes@863
   538
        }
nkeynes@1133
   539
nkeynes@736
   540
        if( IS_TILE_PTR(segment->trans_ptr) ) {
nkeynes@736
   541
            if( pvr2_scene.sort_mode == SORT_NEVER || 
nkeynes@736
   542
                    (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
nkeynes@865
   543
                gl_render_tilelist(segment->trans_ptr, 0);
nkeynes@736
   544
            } else {
nkeynes@736
   545
                render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
nkeynes@736
   546
            }
nkeynes@736
   547
        }
nkeynes@639
   548
    } while( !IS_LAST_SEGMENT(segment++) );
nkeynes@639
   549
    glDisable( GL_SCISSOR_TEST );
nkeynes@1132
   550
    glDisable( GL_COLOR_SUM );
nkeynes@1132
   551
    glDisable( GL_FOG );
nkeynes@1130
   552
    glsl_clear_shader();
nkeynes@669
   553
nkeynes@645
   554
    gettimeofday( &end_tv, NULL );
nkeynes@645
   555
    ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
nkeynes@736
   556
    (end_tv.tv_usec - tex_tv.tv_usec)/1000;
nkeynes@645
   557
    DEBUG( "Scene render in %dms", ms );
nkeynes@639
   558
}
.