Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 1242:67909902ede9
prev1240:190df8a791ca
next1251:b8ab59d39756
author nkeynes
date Tue Feb 28 17:31:26 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Fix accidental change of sense for HAVE_OPENGL_FIXEDFUNC
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@1145
    24
#include "pvr2/glutil.h"
nkeynes@639
    25
#include "pvr2/scene.h"
nkeynes@1145
    26
#include "pvr2/tileiter.h"
nkeynes@1207
    27
#include "pvr2/shaders.h"
nkeynes@653
    28
nkeynes@1156
    29
#ifdef APPLE_BUILD
nkeynes@1156
    30
#include "OpenGL/CGLCurrent.h"
nkeynes@1156
    31
#include "OpenGL/CGLMacro.h"
nkeynes@1156
    32
nkeynes@1156
    33
static CGLContextObj CGL_MACRO_CONTEXT;
nkeynes@1156
    34
#endif
nkeynes@1156
    35
nkeynes@1232
    36
#define IS_NONEMPTY_TILE_LIST(p) (IS_TILE_PTR(p) && ((*((uint32_t *)(pvr2_main_ram+(p))) >> 28) != 0x0F))
nkeynes@639
    37
nkeynes@645
    38
int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
nkeynes@736
    39
        GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, 
nkeynes@736
    40
        GL_ALWAYS };
nkeynes@645
    41
int pvr2_poly_srcblend[8] = { 
nkeynes@736
    42
        GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
nkeynes@736
    43
        GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, 
nkeynes@736
    44
        GL_ONE_MINUS_DST_ALPHA };
nkeynes@645
    45
int pvr2_poly_dstblend[8] = {
nkeynes@736
    46
        GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
nkeynes@736
    47
        GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
nkeynes@736
    48
        GL_ONE_MINUS_DST_ALPHA };
nkeynes@645
    49
nkeynes@1140
    50
static gboolean have_shaders = FALSE;
nkeynes@1154
    51
static int currentTexId = -1;
nkeynes@1154
    52
nkeynes@1154
    53
static inline void bind_texture(int texid)
nkeynes@1154
    54
{
nkeynes@1154
    55
    if( currentTexId != texid ) {
nkeynes@1154
    56
        currentTexId = texid;
nkeynes@1154
    57
        glBindTexture(GL_TEXTURE_2D, texid);
nkeynes@1154
    58
    }
nkeynes@1154
    59
}
nkeynes@1140
    60
nkeynes@639
    61
/**
nkeynes@639
    62
 * Clip the tile bounds to the clipping plane. 
nkeynes@639
    63
 * @return TRUE if the tile was not clipped completely.
nkeynes@639
    64
 */
nkeynes@1232
    65
static gboolean clip_tile_bounds( uint32_t *tile, uint32_t *clip )
nkeynes@639
    66
{
nkeynes@639
    67
    if( tile[0] < clip[0] ) tile[0] = clip[0];
nkeynes@639
    68
    if( tile[1] > clip[1] ) tile[1] = clip[1];
nkeynes@639
    69
    if( tile[2] < clip[2] ) tile[2] = clip[2];
nkeynes@639
    70
    if( tile[3] > clip[3] ) tile[3] = clip[3];
nkeynes@639
    71
    return tile[0] < tile[1] && tile[2] < tile[3];
nkeynes@639
    72
}
nkeynes@639
    73
nkeynes@1145
    74
static void drawrect2d( uint32_t tile_bounds[], float z )
nkeynes@1145
    75
{
nkeynes@1232
    76
    glBegin( GL_TRIANGLE_STRIP );
nkeynes@1145
    77
    glVertex3f( tile_bounds[0], tile_bounds[2], z );
nkeynes@1145
    78
    glVertex3f( tile_bounds[1], tile_bounds[2], z );
nkeynes@1232
    79
    glVertex3f( tile_bounds[0], tile_bounds[3], z );
nkeynes@1145
    80
    glVertex3f( tile_bounds[1], tile_bounds[3], z );
nkeynes@1145
    81
    glEnd();
nkeynes@1145
    82
}
nkeynes@1145
    83
nkeynes@1154
    84
static void pvr2_scene_load_textures()
nkeynes@645
    85
{
nkeynes@645
    86
    int i;
nkeynes@886
    87
    
nkeynes@1135
    88
    texcache_begin_scene( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03,
nkeynes@886
    89
                         (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5 );
nkeynes@886
    90
    
nkeynes@645
    91
    for( i=0; i < pvr2_scene.poly_count; i++ ) {
nkeynes@736
    92
        struct polygon_struct *poly = &pvr2_scene.poly_array[i];
nkeynes@736
    93
        if( POLY1_TEXTURED(poly->context[0]) ) {
nkeynes@1135
    94
            poly->tex_id = texcache_get_texture( poly->context[1], poly->context[2] );
nkeynes@736
    95
            if( poly->mod_vertex_index != -1 ) {
nkeynes@863
    96
                if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
nkeynes@1135
    97
                    poly->mod_tex_id = texcache_get_texture( poly->context[3], poly->context[4] );
nkeynes@863
    98
                } else {
nkeynes@863
    99
                    poly->mod_tex_id = poly->tex_id;
nkeynes@863
   100
                }
nkeynes@736
   101
            }
nkeynes@736
   102
        } else {
nkeynes@1138
   103
            poly->tex_id = 0;
nkeynes@1138
   104
            poly->mod_tex_id = 0;
nkeynes@736
   105
        }
nkeynes@645
   106
    }
nkeynes@645
   107
}
nkeynes@645
   108
nkeynes@645
   109
nkeynes@639
   110
/**
nkeynes@639
   111
 * Once-off call to setup the OpenGL context.
nkeynes@639
   112
 */
nkeynes@639
   113
void pvr2_setup_gl_context()
nkeynes@639
   114
{
nkeynes@1140
   115
    if( glsl_is_supported() && isGLMultitextureSupported() ) {
nkeynes@1130
   116
        if( !glsl_load_shaders( ) ) {
nkeynes@665
   117
            WARN( "Unable to load GL shaders" );
nkeynes@1140
   118
        } else {
nkeynes@1140
   119
            have_shaders = TRUE;
nkeynes@665
   120
        }
nkeynes@665
   121
    }
nkeynes@736
   122
nkeynes@1156
   123
#ifdef APPLE_BUILD
nkeynes@1156
   124
    CGL_MACRO_CONTEXT = CGLGetCurrentContext();
nkeynes@1156
   125
#endif
nkeynes@1233
   126
    texcache_gl_init(have_shaders); // Allocate texture IDs
nkeynes@1236
   127
nkeynes@1236
   128
    /* Global settings */
nkeynes@1133
   129
    glDisable( GL_CULL_FACE );
nkeynes@639
   130
    glEnable( GL_BLEND );
nkeynes@1236
   131
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
nkeynes@639
   132
nkeynes@687
   133
#ifdef HAVE_OPENGL_CLAMP_COLOR
nkeynes@687
   134
    if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
nkeynes@687
   135
        glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
nkeynes@687
   136
        glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
nkeynes@687
   137
    }
nkeynes@687
   138
#endif
nkeynes@653
   139
nkeynes@1242
   140
#ifdef HAVE_OPENGL_FIXEDFUNC
nkeynes@1236
   141
    /* Setup defaults for perspective correction + matrices */
nkeynes@1236
   142
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
nkeynes@1236
   143
    glMatrixMode(GL_MODELVIEW);
nkeynes@1236
   144
    glLoadIdentity();
nkeynes@1236
   145
    glMatrixMode(GL_PROJECTION);
nkeynes@1236
   146
#endif
nkeynes@1236
   147
nkeynes@639
   148
nkeynes@1232
   149
#ifdef HAVE_OPENGL_CLEAR_DEPTHF
nkeynes@1232
   150
    glClearDepthf(0);
nkeynes@1232
   151
#else
nkeynes@639
   152
    glClearDepth(0);
nkeynes@1232
   153
#endif
nkeynes@639
   154
    glClearStencil(0);
nkeynes@639
   155
}
nkeynes@639
   156
nkeynes@864
   157
/**
nkeynes@864
   158
 * Setup the basic context that's shared between normal and modified modes -
nkeynes@864
   159
 * depth, culling
nkeynes@864
   160
 */
nkeynes@1137
   161
static void render_set_base_context( uint32_t poly1, gboolean set_depth )
nkeynes@864
   162
{
nkeynes@1137
   163
    if( set_depth ) {
nkeynes@865
   164
        glDepthFunc( POLY1_DEPTH_MODE(poly1) );
nkeynes@865
   165
    }
nkeynes@865
   166
nkeynes@864
   167
    glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
nkeynes@865
   168
}
nkeynes@864
   169
nkeynes@865
   170
/**
nkeynes@865
   171
 * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
nkeynes@865
   172
 */
nkeynes@1154
   173
static void render_set_tsp_context( uint32_t poly1, uint32_t poly2 )
nkeynes@865
   174
{
nkeynes@863
   175
    glShadeModel( POLY1_SHADE_MODEL(poly1) );
nkeynes@1140
   176
nkeynes@1232
   177
#ifdef HAVE_OPENGL_FIXEDFUNC
nkeynes@1219
   178
    if( !have_shaders ) {
nkeynes@1219
   179
        if( POLY1_TEXTURED(poly1) ) {
nkeynes@1219
   180
            if( POLY2_TEX_BLEND(poly2) == 2 )
nkeynes@1219
   181
                glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
nkeynes@1219
   182
            else
nkeynes@1219
   183
                glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
nkeynes@1232
   184
   
nkeynes@1232
   185
         }
nkeynes@1140
   186
nkeynes@1232
   187
         switch( POLY2_FOG_MODE(poly2) ) {
nkeynes@1232
   188
         case PVR2_POLY_FOG_LOOKUP:
nkeynes@1232
   189
             glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
nkeynes@1232
   190
             break;
nkeynes@1232
   191
         case PVR2_POLY_FOG_VERTEX:
nkeynes@1232
   192
             glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
nkeynes@1232
   193
             break;
nkeynes@1232
   194
         }
nkeynes@863
   195
     }
nkeynes@1232
   196
#endif
nkeynes@653
   197
nkeynes@863
   198
     int srcblend = POLY2_SRC_BLEND(poly2);
nkeynes@863
   199
     int destblend = POLY2_DEST_BLEND(poly2);
nkeynes@863
   200
     glBlendFunc( srcblend, destblend );
nkeynes@863
   201
nkeynes@863
   202
     if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
nkeynes@863
   203
         WARN( "Accumulation buffer not supported" );
nkeynes@863
   204
     }   
nkeynes@863
   205
}
nkeynes@863
   206
nkeynes@645
   207
/**
nkeynes@645
   208
 * Setup the GL context for the supplied polygon context.
nkeynes@645
   209
 * @param context pointer to 3 or 5 words of polygon context
nkeynes@865
   210
 * @param depth_mode force depth mode, or 0 to use the polygon's
nkeynes@865
   211
 * depth mode.
nkeynes@645
   212
 */
nkeynes@1154
   213
static void render_set_context( uint32_t *context, gboolean set_depth )
nkeynes@645
   214
{
nkeynes@1137
   215
    render_set_base_context(context[0], set_depth);
nkeynes@1137
   216
    render_set_tsp_context(context[0],context[1]);
nkeynes@645
   217
}
nkeynes@645
   218
nkeynes@1133
   219
static inline void gl_draw_vertexes( struct polygon_struct *poly )
nkeynes@1133
   220
{
nkeynes@1133
   221
    do {
nkeynes@1133
   222
        glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
nkeynes@1133
   223
        poly = poly->sub_next;
nkeynes@1133
   224
    } while( poly != NULL );
nkeynes@1133
   225
}
nkeynes@1133
   226
nkeynes@1133
   227
static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
nkeynes@1133
   228
{
nkeynes@1133
   229
    do {
nkeynes@1133
   230
        glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
nkeynes@1133
   231
        poly = poly->sub_next;
nkeynes@1133
   232
    } while( poly != NULL );
nkeynes@1133
   233
}
nkeynes@645
   234
nkeynes@1153
   235
static void gl_render_poly( struct polygon_struct *poly, gboolean set_depth)
nkeynes@639
   236
{
nkeynes@1133
   237
    if( poly->vertex_count == 0 )
nkeynes@1133
   238
        return; /* Culled */
nkeynes@1133
   239
nkeynes@1154
   240
    bind_texture(poly->tex_id);
nkeynes@863
   241
    if( poly->mod_vertex_index == -1 ) {
nkeynes@1137
   242
        render_set_context( poly->context, set_depth );
nkeynes@1133
   243
        gl_draw_vertexes(poly);
nkeynes@864
   244
    }  else {
nkeynes@864
   245
        glEnable( GL_STENCIL_TEST );
nkeynes@1137
   246
        render_set_base_context( poly->context[0], set_depth );
nkeynes@1137
   247
        render_set_tsp_context( poly->context[0], poly->context[1] );
nkeynes@864
   248
        glStencilFunc(GL_EQUAL, 0, 2);
nkeynes@1133
   249
        gl_draw_vertexes(poly);
nkeynes@863
   250
nkeynes@863
   251
        if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
nkeynes@1154
   252
            bind_texture(poly->mod_tex_id);
nkeynes@1137
   253
            render_set_tsp_context( poly->context[0], poly->context[3] );
nkeynes@863
   254
        }
nkeynes@864
   255
        glStencilFunc(GL_EQUAL, 2, 2);
nkeynes@1133
   256
        gl_draw_mod_vertexes(poly);
nkeynes@1136
   257
        glDisable( GL_STENCIL_TEST );
nkeynes@863
   258
    }
nkeynes@639
   259
}
nkeynes@639
   260
nkeynes@687
   261
nkeynes@1145
   262
static void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
nkeynes@864
   263
{
nkeynes@864
   264
    /* A bit of explanation:
nkeynes@864
   265
     * In theory it works like this: generate a 1-bit stencil for each polygon
nkeynes@864
   266
     * volume, and then AND or OR it against the overall 1-bit tile stencil at 
nkeynes@864
   267
     * the end of the volume. 
nkeynes@864
   268
     * 
nkeynes@864
   269
     * The implementation here uses a 2-bit stencil buffer, where each volume
nkeynes@864
   270
     * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
nkeynes@864
   271
     * to update bit 1 accordingly and clear bit 0.
nkeynes@864
   272
     * 
nkeynes@864
   273
     * This could probably be more efficient, but at least it works correctly 
nkeynes@864
   274
     * now :)
nkeynes@864
   275
     */
nkeynes@864
   276
    
nkeynes@1133
   277
    if( poly->vertex_count == 0 )
nkeynes@1133
   278
        return; /* Culled */
nkeynes@1133
   279
nkeynes@1133
   280
    gl_draw_vertexes(poly);
nkeynes@1133
   281
nkeynes@1133
   282
nkeynes@864
   283
    
nkeynes@864
   284
    int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
nkeynes@864
   285
    if( poly_type == PVR2_VOLUME_REGION0 ) {
nkeynes@864
   286
        /* 00 => 00
nkeynes@864
   287
         * 01 => 00
nkeynes@864
   288
         * 10 => 10
nkeynes@864
   289
         * 11 => 00
nkeynes@864
   290
         */
nkeynes@864
   291
        glStencilMask( 0x03 );
nkeynes@864
   292
        glStencilFunc(GL_EQUAL, 0x02, 0x03);
nkeynes@864
   293
        glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
nkeynes@864
   294
        glDisable( GL_DEPTH_TEST );
nkeynes@864
   295
nkeynes@864
   296
        drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
nkeynes@864
   297
        
nkeynes@864
   298
        glEnable( GL_DEPTH_TEST );
nkeynes@864
   299
        glStencilMask( 0x01 );
nkeynes@864
   300
        glStencilFunc( GL_ALWAYS, 0, 1 );
nkeynes@864
   301
        glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
nkeynes@864
   302
    } else if( poly_type == PVR2_VOLUME_REGION1 ) {
nkeynes@864
   303
        /* This is harder with the standard stencil ops - do it in two passes
nkeynes@864
   304
         * 00 => 00 | 00 => 10
nkeynes@864
   305
         * 01 => 10 | 01 => 10
nkeynes@864
   306
         * 10 => 10 | 10 => 00
nkeynes@864
   307
         * 11 => 10 | 11 => 10
nkeynes@864
   308
         */
nkeynes@864
   309
        glStencilMask( 0x02 );
nkeynes@864
   310
        glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
nkeynes@864
   311
        glDisable( GL_DEPTH_TEST );
nkeynes@864
   312
        
nkeynes@864
   313
        drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
nkeynes@864
   314
        
nkeynes@864
   315
        glStencilMask( 0x03 );
nkeynes@864
   316
        glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
nkeynes@864
   317
        glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
nkeynes@864
   318
        
nkeynes@864
   319
        drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
nkeynes@864
   320
        
nkeynes@864
   321
        glEnable( GL_DEPTH_TEST );
nkeynes@864
   322
        glStencilMask( 0x01 );
nkeynes@864
   323
        glStencilFunc( GL_ALWAYS, 0, 1 );
nkeynes@864
   324
        glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );         
nkeynes@864
   325
    }
nkeynes@864
   326
}
nkeynes@864
   327
nkeynes@1145
   328
static void gl_render_bkgnd( struct polygon_struct *poly )
nkeynes@863
   329
{
nkeynes@1154
   330
    bind_texture(poly->tex_id);
nkeynes@1145
   331
    render_set_tsp_context( poly->context[0], poly->context[1] );
nkeynes@1145
   332
    glDisable( GL_DEPTH_TEST );
nkeynes@1145
   333
    glBlendFunc( GL_ONE, GL_ZERO );
nkeynes@1145
   334
    gl_draw_vertexes(poly);
nkeynes@1145
   335
    glEnable( GL_DEPTH_TEST );
nkeynes@1145
   336
}
nkeynes@1145
   337
nkeynes@1154
   338
void gl_render_triangle( struct polygon_struct *poly, int index )
nkeynes@1154
   339
{
nkeynes@1154
   340
    bind_texture(poly->tex_id);
nkeynes@1154
   341
    render_set_tsp_context( poly->context[0], poly->context[1] );
nkeynes@1154
   342
    glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index + index, 3 );
nkeynes@1154
   343
nkeynes@1154
   344
}
nkeynes@1154
   345
nkeynes@1145
   346
void gl_render_tilelist( pvraddr_t tile_entry, gboolean set_depth )
nkeynes@1145
   347
{
nkeynes@1145
   348
    tileentryiter list;
nkeynes@1145
   349
nkeynes@1145
   350
    FOREACH_TILEENTRY(list, tile_entry) {
nkeynes@1145
   351
        struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
nkeynes@1145
   352
        if( poly != NULL ) {
nkeynes@1145
   353
            do {
nkeynes@1145
   354
                gl_render_poly(poly, set_depth);
nkeynes@1145
   355
                poly = poly->next;
nkeynes@1145
   356
            } while( list.strip_count-- > 0 );
nkeynes@1145
   357
        }
nkeynes@1145
   358
    }
nkeynes@1145
   359
}
nkeynes@1145
   360
nkeynes@1145
   361
/**
nkeynes@1145
   362
 * Render the tilelist with depthbuffer updates only.
nkeynes@1145
   363
 */
nkeynes@1145
   364
static void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
nkeynes@1145
   365
{
nkeynes@1145
   366
    tileentryiter list;
nkeynes@1145
   367
nkeynes@1145
   368
    FOREACH_TILEENTRY(list, tile_entry) {
nkeynes@1145
   369
        struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
nkeynes@1145
   370
        if( poly != NULL ) {
nkeynes@1145
   371
            do {
nkeynes@1145
   372
                render_set_base_context(poly->context[0],TRUE);
nkeynes@1145
   373
                gl_draw_vertexes(poly);
nkeynes@1145
   374
                poly = poly->next;
nkeynes@1145
   375
            } while( list.strip_count-- > 0 );
nkeynes@1145
   376
        }
nkeynes@1145
   377
    }
nkeynes@1145
   378
}
nkeynes@1145
   379
nkeynes@1145
   380
static void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
nkeynes@1145
   381
{
nkeynes@1145
   382
    tileentryiter list;
nkeynes@863
   383
nkeynes@1145
   384
    FOREACH_TILEENTRY(list, tile_entry ) {
nkeynes@1145
   385
        struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
nkeynes@1145
   386
        if( poly != NULL ) {
nkeynes@1145
   387
            do {
nkeynes@864
   388
                gl_render_modifier_polygon( poly, tile_bounds );
nkeynes@863
   389
                poly = poly->next;
nkeynes@1145
   390
            } while( list.strip_count-- > 0 );
nkeynes@863
   391
        }
nkeynes@863
   392
    }
nkeynes@1232
   393
}
nkeynes@1232
   394
nkeynes@1232
   395
nkeynes@1232
   396
#ifdef HAVE_OPENGL_FIXEDFUNC
nkeynes@1232
   397
void pvr2_scene_setup_fixed( GLfloat *viewMatrix )
nkeynes@1232
   398
{
nkeynes@1232
   399
    glLoadMatrixf(viewMatrix);
nkeynes@1236
   400
    glEnable( GL_DEPTH_TEST );
nkeynes@1232
   401
    
nkeynes@1232
   402
    glEnable( GL_FOG );
nkeynes@1232
   403
    glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
nkeynes@1232
   404
    glFogi(GL_FOG_MODE, GL_LINEAR);
nkeynes@1232
   405
    glFogf(GL_FOG_START, 0.0);
nkeynes@1232
   406
    glFogf(GL_FOG_END, 1.0);
nkeynes@1232
   407
nkeynes@1232
   408
    glEnable( GL_ALPHA_TEST );
nkeynes@1232
   409
    glAlphaFunc( GL_GEQUAL, 0 );
nkeynes@1232
   410
nkeynes@1232
   411
    glEnable( GL_COLOR_SUM );
nkeynes@1232
   412
nkeynes@1232
   413
    glEnableClientState( GL_VERTEX_ARRAY );
nkeynes@1232
   414
    glEnableClientState( GL_COLOR_ARRAY );
nkeynes@1232
   415
    glEnableClientState( GL_TEXTURE_COORD_ARRAY );
nkeynes@1232
   416
    glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
nkeynes@1232
   417
    glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
nkeynes@1232
   418
nkeynes@1232
   419
    /* Vertex array pointers */
nkeynes@1232
   420
    glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
nkeynes@1232
   421
    glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
nkeynes@1233
   422
    glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
nkeynes@1232
   423
    glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
nkeynes@1232
   424
    glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
nkeynes@1232
   425
}
nkeynes@1232
   426
nkeynes@1232
   427
void pvr2_scene_set_alpha_fixed( float alphaRef )
nkeynes@1232
   428
{
nkeynes@1232
   429
    glAlphaFunc( GL_GEQUAL, alphaRef );
nkeynes@1232
   430
}
nkeynes@1232
   431
nkeynes@1232
   432
void pvr2_scene_cleanup_fixed()
nkeynes@1232
   433
{
nkeynes@1232
   434
    glDisable( GL_COLOR_SUM );
nkeynes@1232
   435
    glDisable( GL_FOG );
nkeynes@1232
   436
    glDisable( GL_ALPHA_TEST );
nkeynes@1236
   437
    glDisable( GL_DEPTH_TEST );
nkeynes@1232
   438
nkeynes@1232
   439
    glDisableClientState( GL_VERTEX_ARRAY );
nkeynes@1232
   440
    glDisableClientState( GL_COLOR_ARRAY );
nkeynes@1232
   441
    glDisableClientState( GL_TEXTURE_COORD_ARRAY );
nkeynes@1232
   442
    glDisableClientState( GL_SECONDARY_COLOR_ARRAY );
nkeynes@1232
   443
    glDisableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
nkeynes@1236
   444
nkeynes@1232
   445
}
nkeynes@1232
   446
#else
nkeynes@1238
   447
void pvr2_scene_setup_fixed( GLfloat *viewMatrix )
nkeynes@1232
   448
{
nkeynes@1232
   449
}
nkeynes@1232
   450
void pvr2_scene_set_alpha_fixed( float alphaRef )
nkeynes@1232
   451
{
nkeynes@1232
   452
}
nkeynes@1232
   453
void pvr2_scene_cleanup_fixed()
nkeynes@1232
   454
{
nkeynes@1232
   455
}
nkeynes@1232
   456
#endif
nkeynes@1232
   457
nkeynes@1232
   458
void pvr2_scene_setup_shader( GLfloat *viewMatrix )
nkeynes@1232
   459
{
nkeynes@1236
   460
    glEnable( GL_DEPTH_TEST );
nkeynes@1236
   461
nkeynes@1232
   462
    glsl_use_pvr2_shader();
nkeynes@1232
   463
    glsl_set_pvr2_shader_view_matrix(viewMatrix);
nkeynes@1232
   464
    glsl_set_pvr2_shader_fog_colour1(pvr2_scene.fog_vert_colour);
nkeynes@1232
   465
    glsl_set_pvr2_shader_fog_colour2(pvr2_scene.fog_lut_colour);
nkeynes@1232
   466
    glsl_set_pvr2_shader_in_vertex_vec3_pointer(&pvr2_scene.vertex_array[0].x, sizeof(struct vertex_struct));
nkeynes@1232
   467
    glsl_set_pvr2_shader_in_colour_pointer(&pvr2_scene.vertex_array[0].rgba[0], sizeof(struct vertex_struct));
nkeynes@1232
   468
    glsl_set_pvr2_shader_in_colour2_pointer(&pvr2_scene.vertex_array[0].offset_rgba[0], sizeof(struct vertex_struct));
nkeynes@1232
   469
    glsl_set_pvr2_shader_in_texcoord_pointer(&pvr2_scene.vertex_array[0].u, sizeof(struct vertex_struct));
nkeynes@1232
   470
    glsl_set_pvr2_shader_alpha_ref(0.0);
nkeynes@1232
   471
    glsl_set_pvr2_shader_primary_texture(0);
nkeynes@1232
   472
    glsl_set_pvr2_shader_palette_texture(1);
nkeynes@1232
   473
}
nkeynes@1232
   474
nkeynes@1232
   475
void pvr2_scene_cleanup_shader( )
nkeynes@1232
   476
{
nkeynes@1232
   477
    glsl_clear_shader();
nkeynes@1236
   478
nkeynes@1236
   479
    glDisable( GL_DEPTH_TEST );
nkeynes@1232
   480
}
nkeynes@1232
   481
nkeynes@1232
   482
void pvr2_scene_set_alpha_shader( float alphaRef )
nkeynes@1232
   483
{
nkeynes@1232
   484
    glsl_set_pvr2_shader_alpha_ref(alphaRef);
nkeynes@639
   485
}
nkeynes@639
   486
nkeynes@639
   487
/**
nkeynes@639
   488
 * Render the currently defined scene in pvr2_scene
nkeynes@639
   489
 */
nkeynes@639
   490
void pvr2_scene_render( render_buffer_t buffer )
nkeynes@639
   491
{
nkeynes@639
   492
    /* Scene setup */
nkeynes@645
   493
    struct timeval start_tv, tex_tv, end_tv;
nkeynes@1232
   494
    int i;
nkeynes@1232
   495
    GLfloat viewMatrix[16];
nkeynes@1232
   496
    uint32_t clip_bounds[4];
nkeynes@1232
   497
nkeynes@645
   498
nkeynes@645
   499
    gettimeofday(&start_tv, NULL);
nkeynes@639
   500
    display_driver->set_render_target(buffer);
nkeynes@639
   501
    pvr2_check_palette_changed();
nkeynes@645
   502
    pvr2_scene_load_textures();
nkeynes@1154
   503
    currentTexId = -1;
nkeynes@736
   504
nkeynes@645
   505
    gettimeofday( &tex_tv, NULL );
nkeynes@645
   506
    uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
nkeynes@736
   507
    (tex_tv.tv_usec - start_tv.tv_usec)/1000;
nkeynes@1232
   508
    DEBUG( "Texture load in %dms", ms );
nkeynes@639
   509
nkeynes@1232
   510
    float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
nkeynes@639
   511
    float nearz = pvr2_scene.bounds[4];
nkeynes@639
   512
    float farz = pvr2_scene.bounds[5];
nkeynes@639
   513
    if( nearz == farz ) {
nkeynes@687
   514
        farz*= 4.0;
nkeynes@639
   515
    }
nkeynes@1232
   516
nkeynes@1232
   517
    /* Generate integer clip boundaries */
nkeynes@1232
   518
    for( i=0; i<4; i++ ) {
nkeynes@1232
   519
        clip_bounds[i] = (uint32_t)pvr2_scene.bounds[i];
nkeynes@1232
   520
    }
nkeynes@1232
   521
nkeynes@1236
   522
    defineOrthoMatrix(viewMatrix, pvr2_scene.buffer_width, pvr2_scene.buffer_height, -farz, -nearz);
nkeynes@1232
   523
nkeynes@1232
   524
    if( have_shaders ) {
nkeynes@1232
   525
        pvr2_scene_setup_shader(viewMatrix);
nkeynes@1232
   526
    } else {
nkeynes@1232
   527
        pvr2_scene_setup_fixed(viewMatrix);
nkeynes@1232
   528
    }
nkeynes@1232
   529
nkeynes@639
   530
nkeynes@639
   531
    /* Clear the buffer (FIXME: May not want always want to do this) */
nkeynes@639
   532
    glDisable( GL_SCISSOR_TEST );
nkeynes@651
   533
    glDepthMask( GL_TRUE );
nkeynes@864
   534
    glStencilMask( 0x03 );
nkeynes@639
   535
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
nkeynes@639
   536
nkeynes@687
   537
    /* Render the background */
nkeynes@687
   538
    gl_render_bkgnd( pvr2_scene.bkgnd_poly );
nkeynes@736
   539
nkeynes@639
   540
    glEnable( GL_SCISSOR_TEST );
nkeynes@1138
   541
    glEnable( GL_TEXTURE_2D );
nkeynes@639
   542
nkeynes@1232
   543
    struct tile_segment *segment;
nkeynes@639
   544
nkeynes@1232
   545
#define FOREACH_SEGMENT(segment) \
nkeynes@1232
   546
    segment = pvr2_scene.segment_list; \
nkeynes@1232
   547
    do { \
nkeynes@1232
   548
        int tilex = SEGMENT_X(segment->control); \
nkeynes@1232
   549
        int tiley = SEGMENT_Y(segment->control); \
nkeynes@1232
   550
        \
nkeynes@1232
   551
        uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 }; \
nkeynes@1232
   552
        if( !clip_tile_bounds(tile_bounds, clip_bounds) ) { \
nkeynes@1232
   553
            continue; \
nkeynes@736
   554
        }
nkeynes@1232
   555
#define END_FOREACH_SEGMENT() \
nkeynes@1232
   556
    } while( !IS_LAST_SEGMENT(segment++) );
nkeynes@1232
   557
#define CLIP_TO_SEGMENT() \
nkeynes@1232
   558
    glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] )
nkeynes@736
   559
nkeynes@1232
   560
    /* Build up the opaque stencil map */
nkeynes@1232
   561
    if( display_driver->capabilities.stencil_bits >= 2 ) {
nkeynes@1232
   562
        glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
nkeynes@1232
   563
        FOREACH_SEGMENT(segment)
nkeynes@1232
   564
            if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
nkeynes@1232
   565
                CLIP_TO_SEGMENT();
nkeynes@1232
   566
                gl_render_tilelist_depthonly(segment->opaque_ptr);
nkeynes@1232
   567
            }
nkeynes@1232
   568
        END_FOREACH_SEGMENT()
nkeynes@1153
   569
nkeynes@1232
   570
        glEnable( GL_STENCIL_TEST );
nkeynes@1232
   571
        glStencilFunc( GL_ALWAYS, 0, 1 );
nkeynes@1232
   572
        glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
nkeynes@1232
   573
        glStencilMask( 0x01 );
nkeynes@1232
   574
        glDepthFunc( GL_LEQUAL );
nkeynes@1232
   575
        glDepthMask( GL_FALSE );
nkeynes@1232
   576
        FOREACH_SEGMENT(segment)
nkeynes@1232
   577
            if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
nkeynes@1232
   578
                CLIP_TO_SEGMENT();
nkeynes@1232
   579
                gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
nkeynes@1232
   580
            }
nkeynes@1232
   581
        END_FOREACH_SEGMENT()
nkeynes@1232
   582
        glDepthMask( GL_TRUE );
nkeynes@1232
   583
        glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
nkeynes@1232
   584
        glDisable( GL_SCISSOR_TEST );
nkeynes@1232
   585
        glClear( GL_DEPTH_BUFFER_BIT );
nkeynes@1232
   586
        glEnable( GL_SCISSOR_TEST );
nkeynes@1232
   587
        glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
nkeynes@1232
   588
    }
nkeynes@1232
   589
nkeynes@1232
   590
    /* Render the opaque polygons */
nkeynes@1232
   591
    FOREACH_SEGMENT(segment)
nkeynes@1232
   592
        CLIP_TO_SEGMENT();
nkeynes@1153
   593
        gl_render_tilelist(segment->opaque_ptr,TRUE);
nkeynes@1232
   594
    END_FOREACH_SEGMENT()
nkeynes@1236
   595
    glDisable( GL_STENCIL_TEST );
nkeynes@1133
   596
nkeynes@1232
   597
    /* Render the punch-out polygons */
nkeynes@1232
   598
    if( have_shaders )
nkeynes@1232
   599
        pvr2_scene_set_alpha_shader(alphaRef);
nkeynes@1232
   600
    else
nkeynes@1232
   601
        pvr2_scene_set_alpha_fixed(alphaRef);
nkeynes@1232
   602
    glDepthFunc(GL_GEQUAL);
nkeynes@1232
   603
    FOREACH_SEGMENT(segment)
nkeynes@1232
   604
        CLIP_TO_SEGMENT();
nkeynes@1232
   605
        gl_render_tilelist(segment->punchout_ptr, FALSE );
nkeynes@1232
   606
    END_FOREACH_SEGMENT()
nkeynes@1232
   607
    if( have_shaders )
nkeynes@1232
   608
        pvr2_scene_set_alpha_shader(0.0);
nkeynes@1232
   609
    else
nkeynes@1232
   610
        pvr2_scene_set_alpha_fixed(0.0);
nkeynes@1232
   611
nkeynes@1232
   612
    /* Render the translucent polygons */
nkeynes@1232
   613
    FOREACH_SEGMENT(segment)
nkeynes@1232
   614
        if( IS_NONEMPTY_TILE_LIST(segment->trans_ptr) ) {
nkeynes@1232
   615
            CLIP_TO_SEGMENT();
nkeynes@736
   616
            if( pvr2_scene.sort_mode == SORT_NEVER || 
nkeynes@736
   617
                    (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
nkeynes@1137
   618
                gl_render_tilelist(segment->trans_ptr, TRUE);
nkeynes@736
   619
            } else {
nkeynes@736
   620
                render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
nkeynes@736
   621
            }
nkeynes@736
   622
        }
nkeynes@1232
   623
    END_FOREACH_SEGMENT()
nkeynes@1232
   624
nkeynes@639
   625
    glDisable( GL_SCISSOR_TEST );
nkeynes@1232
   626
nkeynes@1232
   627
    if( have_shaders ) {
nkeynes@1232
   628
        pvr2_scene_cleanup_shader();
nkeynes@1232
   629
    } else {
nkeynes@1232
   630
        pvr2_scene_cleanup_fixed();
nkeynes@1232
   631
    }
nkeynes@669
   632
nkeynes@1159
   633
    pvr2_scene_finished();
nkeynes@1159
   634
nkeynes@645
   635
    gettimeofday( &end_tv, NULL );
nkeynes@645
   636
    ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
nkeynes@736
   637
    (end_tv.tv_usec - tex_tv.tv_usec)/1000;
nkeynes@645
   638
    DEBUG( "Scene render in %dms", ms );
nkeynes@639
   639
}
.