Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 1219:3966d3e55351
prev1207:f7ca985659c6
next1232:e5b12e2fe6ba
author nkeynes
date Mon Feb 13 21:43:22 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Remove unused pvr2_poly_texblend array
Support GL_MAX_TEXTURE_UNITS and GL_MAX_IMAGE_TEXTURE_UNITS
Protected against undefined GL_STACK_OVERFLOW
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@934
    36
#define IS_EMPTY_TILE_LIST(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@639
    65
static gboolean clip_tile_bounds( uint32_t *tile, float *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@1145
    76
    glBegin( GL_QUADS );
nkeynes@1145
    77
    glVertex3f( tile_bounds[0], tile_bounds[2], z );
nkeynes@1145
    78
    glVertex3f( tile_bounds[1], tile_bounds[2], z );
nkeynes@1145
    79
    glVertex3f( tile_bounds[1], tile_bounds[3], z );
nkeynes@1145
    80
    glVertex3f( tile_bounds[0], 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@665
   115
nkeynes@1140
   116
    if( glsl_is_supported() && isGLMultitextureSupported() ) {
nkeynes@1130
   117
        if( !glsl_load_shaders( ) ) {
nkeynes@665
   118
            WARN( "Unable to load GL shaders" );
nkeynes@1140
   119
        } else {
nkeynes@1140
   120
            have_shaders = TRUE;
nkeynes@665
   121
        }
nkeynes@665
   122
    }
nkeynes@736
   123
nkeynes@1156
   124
#ifdef APPLE_BUILD
nkeynes@1156
   125
    CGL_MACRO_CONTEXT = CGLGetCurrentContext();
nkeynes@1156
   126
#endif
nkeynes@639
   127
    texcache_gl_init(); // Allocate texture IDs
nkeynes@1133
   128
    glDisable( GL_CULL_FACE );
nkeynes@639
   129
    glEnable( GL_BLEND );
nkeynes@649
   130
    glEnable( GL_DEPTH_TEST );
nkeynes@639
   131
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
nkeynes@639
   132
    glMatrixMode(GL_MODELVIEW);
nkeynes@639
   133
    glLoadIdentity();
nkeynes@639
   134
nkeynes@687
   135
#ifdef HAVE_OPENGL_CLAMP_COLOR
nkeynes@687
   136
    if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
nkeynes@687
   137
        glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
nkeynes@687
   138
        glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
nkeynes@687
   139
    }
nkeynes@687
   140
#endif
nkeynes@653
   141
nkeynes@639
   142
    glEnableClientState( GL_COLOR_ARRAY );
nkeynes@639
   143
    glEnableClientState( GL_VERTEX_ARRAY );
nkeynes@639
   144
    glEnableClientState( GL_TEXTURE_COORD_ARRAY );
nkeynes@639
   145
    glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
nkeynes@847
   146
    glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
nkeynes@639
   147
nkeynes@639
   148
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
nkeynes@639
   149
    glClearDepth(0);
nkeynes@639
   150
    glClearStencil(0);
nkeynes@1132
   151
nkeynes@1132
   152
    glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
nkeynes@1132
   153
    glFogi(GL_FOG_MODE, GL_LINEAR);
nkeynes@1132
   154
    glFogf(GL_FOG_START, 0.0);
nkeynes@1132
   155
    glFogf(GL_FOG_END, 1.0);
nkeynes@1140
   156
nkeynes@1140
   157
    if( have_shaders ) {
nkeynes@1207
   158
        glsl_use_pvr2_shader();
nkeynes@1207
   159
        glsl_set_pvr2_shader_primary_texture(0);
nkeynes@1207
   160
        glsl_set_pvr2_shader_palette_texture(1);
nkeynes@1140
   161
        glsl_clear_shader();
nkeynes@1140
   162
    }
nkeynes@639
   163
}
nkeynes@639
   164
nkeynes@864
   165
/**
nkeynes@864
   166
 * Setup the basic context that's shared between normal and modified modes -
nkeynes@864
   167
 * depth, culling
nkeynes@864
   168
 */
nkeynes@1137
   169
static void render_set_base_context( uint32_t poly1, gboolean set_depth )
nkeynes@864
   170
{
nkeynes@1137
   171
    if( set_depth ) {
nkeynes@865
   172
        glDepthFunc( POLY1_DEPTH_MODE(poly1) );
nkeynes@865
   173
    }
nkeynes@865
   174
nkeynes@864
   175
    glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
nkeynes@865
   176
}
nkeynes@864
   177
nkeynes@865
   178
/**
nkeynes@865
   179
 * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
nkeynes@865
   180
 */
nkeynes@1154
   181
static void render_set_tsp_context( uint32_t poly1, uint32_t poly2 )
nkeynes@865
   182
{
nkeynes@863
   183
    glShadeModel( POLY1_SHADE_MODEL(poly1) );
nkeynes@1140
   184
nkeynes@1219
   185
    if( !have_shaders ) {
nkeynes@1219
   186
        if( POLY1_TEXTURED(poly1) ) {
nkeynes@1219
   187
            if( POLY2_TEX_BLEND(poly2) == 2 )
nkeynes@1219
   188
                glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
nkeynes@1219
   189
            else
nkeynes@1219
   190
                glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
nkeynes@1140
   191
nkeynes@1219
   192
        }
nkeynes@1219
   193
    }
nkeynes@1132
   194
nkeynes@863
   195
     switch( POLY2_FOG_MODE(poly2) ) {
nkeynes@863
   196
     case PVR2_POLY_FOG_LOOKUP:
nkeynes@863
   197
         glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
nkeynes@863
   198
         break;
nkeynes@863
   199
     case PVR2_POLY_FOG_VERTEX:
nkeynes@1132
   200
         glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
nkeynes@1132
   201
         break;
nkeynes@863
   202
     }
nkeynes@653
   203
nkeynes@863
   204
     int srcblend = POLY2_SRC_BLEND(poly2);
nkeynes@863
   205
     int destblend = POLY2_DEST_BLEND(poly2);
nkeynes@863
   206
     glBlendFunc( srcblend, destblend );
nkeynes@863
   207
nkeynes@863
   208
     if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
nkeynes@863
   209
         WARN( "Accumulation buffer not supported" );
nkeynes@863
   210
     }   
nkeynes@863
   211
}
nkeynes@863
   212
nkeynes@645
   213
/**
nkeynes@645
   214
 * Setup the GL context for the supplied polygon context.
nkeynes@645
   215
 * @param context pointer to 3 or 5 words of polygon context
nkeynes@865
   216
 * @param depth_mode force depth mode, or 0 to use the polygon's
nkeynes@865
   217
 * depth mode.
nkeynes@645
   218
 */
nkeynes@1154
   219
static void render_set_context( uint32_t *context, gboolean set_depth )
nkeynes@645
   220
{
nkeynes@1137
   221
    render_set_base_context(context[0], set_depth);
nkeynes@1137
   222
    render_set_tsp_context(context[0],context[1]);
nkeynes@645
   223
}
nkeynes@645
   224
nkeynes@1133
   225
static inline void gl_draw_vertexes( struct polygon_struct *poly )
nkeynes@1133
   226
{
nkeynes@1133
   227
    do {
nkeynes@1133
   228
        glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
nkeynes@1133
   229
        poly = poly->sub_next;
nkeynes@1133
   230
    } while( poly != NULL );
nkeynes@1133
   231
}
nkeynes@1133
   232
nkeynes@1133
   233
static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
nkeynes@1133
   234
{
nkeynes@1133
   235
    do {
nkeynes@1133
   236
        glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
nkeynes@1133
   237
        poly = poly->sub_next;
nkeynes@1133
   238
    } while( poly != NULL );
nkeynes@1133
   239
}
nkeynes@645
   240
nkeynes@1153
   241
static void gl_render_poly( struct polygon_struct *poly, gboolean set_depth)
nkeynes@639
   242
{
nkeynes@1133
   243
    if( poly->vertex_count == 0 )
nkeynes@1133
   244
        return; /* Culled */
nkeynes@1133
   245
nkeynes@1154
   246
    bind_texture(poly->tex_id);
nkeynes@863
   247
    if( poly->mod_vertex_index == -1 ) {
nkeynes@1137
   248
        render_set_context( poly->context, set_depth );
nkeynes@1133
   249
        gl_draw_vertexes(poly);
nkeynes@864
   250
    }  else {
nkeynes@864
   251
        glEnable( GL_STENCIL_TEST );
nkeynes@1137
   252
        render_set_base_context( poly->context[0], set_depth );
nkeynes@1137
   253
        render_set_tsp_context( poly->context[0], poly->context[1] );
nkeynes@864
   254
        glStencilFunc(GL_EQUAL, 0, 2);
nkeynes@1133
   255
        gl_draw_vertexes(poly);
nkeynes@863
   256
nkeynes@863
   257
        if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
nkeynes@1154
   258
            bind_texture(poly->mod_tex_id);
nkeynes@1137
   259
            render_set_tsp_context( poly->context[0], poly->context[3] );
nkeynes@863
   260
        }
nkeynes@864
   261
        glStencilFunc(GL_EQUAL, 2, 2);
nkeynes@1133
   262
        gl_draw_mod_vertexes(poly);
nkeynes@1136
   263
        glDisable( GL_STENCIL_TEST );
nkeynes@863
   264
    }
nkeynes@639
   265
}
nkeynes@639
   266
nkeynes@687
   267
nkeynes@1145
   268
static void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
nkeynes@864
   269
{
nkeynes@864
   270
    /* A bit of explanation:
nkeynes@864
   271
     * In theory it works like this: generate a 1-bit stencil for each polygon
nkeynes@864
   272
     * volume, and then AND or OR it against the overall 1-bit tile stencil at 
nkeynes@864
   273
     * the end of the volume. 
nkeynes@864
   274
     * 
nkeynes@864
   275
     * The implementation here uses a 2-bit stencil buffer, where each volume
nkeynes@864
   276
     * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
nkeynes@864
   277
     * to update bit 1 accordingly and clear bit 0.
nkeynes@864
   278
     * 
nkeynes@864
   279
     * This could probably be more efficient, but at least it works correctly 
nkeynes@864
   280
     * now :)
nkeynes@864
   281
     */
nkeynes@864
   282
    
nkeynes@1133
   283
    if( poly->vertex_count == 0 )
nkeynes@1133
   284
        return; /* Culled */
nkeynes@1133
   285
nkeynes@1133
   286
    gl_draw_vertexes(poly);
nkeynes@1133
   287
nkeynes@1133
   288
nkeynes@864
   289
    
nkeynes@864
   290
    int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
nkeynes@864
   291
    if( poly_type == PVR2_VOLUME_REGION0 ) {
nkeynes@864
   292
        /* 00 => 00
nkeynes@864
   293
         * 01 => 00
nkeynes@864
   294
         * 10 => 10
nkeynes@864
   295
         * 11 => 00
nkeynes@864
   296
         */
nkeynes@864
   297
        glStencilMask( 0x03 );
nkeynes@864
   298
        glStencilFunc(GL_EQUAL, 0x02, 0x03);
nkeynes@864
   299
        glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
nkeynes@864
   300
        glDisable( GL_DEPTH_TEST );
nkeynes@864
   301
nkeynes@864
   302
        drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
nkeynes@864
   303
        
nkeynes@864
   304
        glEnable( GL_DEPTH_TEST );
nkeynes@864
   305
        glStencilMask( 0x01 );
nkeynes@864
   306
        glStencilFunc( GL_ALWAYS, 0, 1 );
nkeynes@864
   307
        glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
nkeynes@864
   308
    } else if( poly_type == PVR2_VOLUME_REGION1 ) {
nkeynes@864
   309
        /* This is harder with the standard stencil ops - do it in two passes
nkeynes@864
   310
         * 00 => 00 | 00 => 10
nkeynes@864
   311
         * 01 => 10 | 01 => 10
nkeynes@864
   312
         * 10 => 10 | 10 => 00
nkeynes@864
   313
         * 11 => 10 | 11 => 10
nkeynes@864
   314
         */
nkeynes@864
   315
        glStencilMask( 0x02 );
nkeynes@864
   316
        glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
nkeynes@864
   317
        glDisable( GL_DEPTH_TEST );
nkeynes@864
   318
        
nkeynes@864
   319
        drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
nkeynes@864
   320
        
nkeynes@864
   321
        glStencilMask( 0x03 );
nkeynes@864
   322
        glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
nkeynes@864
   323
        glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
nkeynes@864
   324
        
nkeynes@864
   325
        drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
nkeynes@864
   326
        
nkeynes@864
   327
        glEnable( GL_DEPTH_TEST );
nkeynes@864
   328
        glStencilMask( 0x01 );
nkeynes@864
   329
        glStencilFunc( GL_ALWAYS, 0, 1 );
nkeynes@864
   330
        glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );         
nkeynes@864
   331
    }
nkeynes@864
   332
}
nkeynes@864
   333
nkeynes@1145
   334
static void gl_render_bkgnd( struct polygon_struct *poly )
nkeynes@863
   335
{
nkeynes@1154
   336
    bind_texture(poly->tex_id);
nkeynes@1145
   337
    render_set_tsp_context( poly->context[0], poly->context[1] );
nkeynes@1145
   338
    glDisable( GL_DEPTH_TEST );
nkeynes@1145
   339
    glBlendFunc( GL_ONE, GL_ZERO );
nkeynes@1145
   340
    gl_draw_vertexes(poly);
nkeynes@1145
   341
    glEnable( GL_DEPTH_TEST );
nkeynes@1145
   342
}
nkeynes@1145
   343
nkeynes@1154
   344
void gl_render_triangle( struct polygon_struct *poly, int index )
nkeynes@1154
   345
{
nkeynes@1154
   346
    bind_texture(poly->tex_id);
nkeynes@1154
   347
    render_set_tsp_context( poly->context[0], poly->context[1] );
nkeynes@1154
   348
    glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index + index, 3 );
nkeynes@1154
   349
nkeynes@1154
   350
}
nkeynes@1154
   351
nkeynes@1145
   352
void gl_render_tilelist( pvraddr_t tile_entry, gboolean set_depth )
nkeynes@1145
   353
{
nkeynes@1145
   354
    tileentryiter list;
nkeynes@1145
   355
nkeynes@1145
   356
    FOREACH_TILEENTRY(list, tile_entry) {
nkeynes@1145
   357
        struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
nkeynes@1145
   358
        if( poly != NULL ) {
nkeynes@1145
   359
            do {
nkeynes@1145
   360
                gl_render_poly(poly, set_depth);
nkeynes@1145
   361
                poly = poly->next;
nkeynes@1145
   362
            } while( list.strip_count-- > 0 );
nkeynes@1145
   363
        }
nkeynes@1145
   364
    }
nkeynes@1145
   365
}
nkeynes@1145
   366
nkeynes@1145
   367
/**
nkeynes@1145
   368
 * Render the tilelist with depthbuffer updates only.
nkeynes@1145
   369
 */
nkeynes@1145
   370
static void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
nkeynes@1145
   371
{
nkeynes@1145
   372
    tileentryiter list;
nkeynes@1145
   373
nkeynes@1145
   374
    FOREACH_TILEENTRY(list, tile_entry) {
nkeynes@1145
   375
        struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
nkeynes@1145
   376
        if( poly != NULL ) {
nkeynes@1145
   377
            do {
nkeynes@1145
   378
                render_set_base_context(poly->context[0],TRUE);
nkeynes@1145
   379
                gl_draw_vertexes(poly);
nkeynes@1145
   380
                poly = poly->next;
nkeynes@1145
   381
            } while( list.strip_count-- > 0 );
nkeynes@1145
   382
        }
nkeynes@1145
   383
    }
nkeynes@1145
   384
}
nkeynes@1145
   385
nkeynes@1145
   386
static void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
nkeynes@1145
   387
{
nkeynes@1145
   388
    tileentryiter list;
nkeynes@863
   389
nkeynes@863
   390
    if( !IS_TILE_PTR(tile_entry) )
nkeynes@863
   391
        return;
nkeynes@863
   392
nkeynes@863
   393
    glEnable( GL_STENCIL_TEST );
nkeynes@863
   394
    glEnable( GL_DEPTH_TEST );
nkeynes@863
   395
    glStencilFunc( GL_ALWAYS, 0, 1 );
nkeynes@863
   396
    glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
nkeynes@864
   397
    glStencilMask( 0x01 );
nkeynes@1145
   398
    glDepthFunc( GL_LEQUAL );
nkeynes@863
   399
    glDepthMask( GL_FALSE );
nkeynes@863
   400
    
nkeynes@1145
   401
    FOREACH_TILEENTRY(list, tile_entry ) {
nkeynes@1145
   402
        struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
nkeynes@1145
   403
        if( poly != NULL ) {
nkeynes@1145
   404
            do {
nkeynes@864
   405
                gl_render_modifier_polygon( poly, tile_bounds );
nkeynes@863
   406
                poly = poly->next;
nkeynes@1145
   407
            } while( list.strip_count-- > 0 );
nkeynes@863
   408
        }
nkeynes@863
   409
    }
nkeynes@1145
   410
    glDepthMask( GL_TRUE );
nkeynes@1145
   411
    glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
nkeynes@1145
   412
    glDisable( GL_STENCIL_TEST );
nkeynes@639
   413
}
nkeynes@639
   414
nkeynes@639
   415
/**
nkeynes@639
   416
 * Render the currently defined scene in pvr2_scene
nkeynes@639
   417
 */
nkeynes@639
   418
void pvr2_scene_render( render_buffer_t buffer )
nkeynes@639
   419
{
nkeynes@639
   420
    /* Scene setup */
nkeynes@645
   421
    struct timeval start_tv, tex_tv, end_tv;
nkeynes@645
   422
nkeynes@645
   423
    gettimeofday(&start_tv, NULL);
nkeynes@639
   424
    display_driver->set_render_target(buffer);
nkeynes@639
   425
    pvr2_check_palette_changed();
nkeynes@645
   426
    pvr2_scene_load_textures();
nkeynes@1154
   427
    currentTexId = -1;
nkeynes@736
   428
nkeynes@645
   429
    gettimeofday( &tex_tv, NULL );
nkeynes@645
   430
    uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
nkeynes@736
   431
    (tex_tv.tv_usec - start_tv.tv_usec)/1000;
nkeynes@645
   432
    DEBUG( "Scene setup in %dms", ms );
nkeynes@639
   433
nkeynes@639
   434
    /* Setup view projection matrix */
nkeynes@639
   435
    glMatrixMode(GL_PROJECTION);
nkeynes@639
   436
    glLoadIdentity();
nkeynes@639
   437
    float nearz = pvr2_scene.bounds[4];
nkeynes@639
   438
    float farz = pvr2_scene.bounds[5];
nkeynes@639
   439
    if( nearz == farz ) {
nkeynes@687
   440
        farz*= 4.0;
nkeynes@639
   441
    }
nkeynes@639
   442
    glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0, 
nkeynes@736
   443
             -farz, -nearz );
nkeynes@649
   444
    float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
nkeynes@649
   445
    glAlphaFunc( GL_GEQUAL, alphaRef );
nkeynes@639
   446
nkeynes@639
   447
    /* Clear the buffer (FIXME: May not want always want to do this) */
nkeynes@639
   448
    glDisable( GL_SCISSOR_TEST );
nkeynes@651
   449
    glDepthMask( GL_TRUE );
nkeynes@864
   450
    glStencilMask( 0x03 );
nkeynes@639
   451
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
nkeynes@639
   452
nkeynes@639
   453
    /* Setup vertex array pointers */
nkeynes@687
   454
    glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
nkeynes@687
   455
    glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
nkeynes@1140
   456
    glTexCoordPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
nkeynes@687
   457
    glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
nkeynes@847
   458
    glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
nkeynes@669
   459
    /* Turn on the shaders (if available) */
nkeynes@1207
   460
    glsl_use_pvr2_shader();
nkeynes@639
   461
nkeynes@687
   462
    /* Render the background */
nkeynes@687
   463
    gl_render_bkgnd( pvr2_scene.bkgnd_poly );
nkeynes@736
   464
nkeynes@639
   465
    glEnable( GL_SCISSOR_TEST );
nkeynes@1132
   466
    glEnable( GL_COLOR_SUM );
nkeynes@1132
   467
    glEnable( GL_FOG );
nkeynes@1138
   468
    glEnable( GL_TEXTURE_2D );
nkeynes@639
   469
nkeynes@639
   470
    /* Process the segment list */
nkeynes@639
   471
    struct tile_segment *segment = pvr2_scene.segment_list;
nkeynes@639
   472
    do {
nkeynes@736
   473
        int tilex = SEGMENT_X(segment->control);
nkeynes@736
   474
        int tiley = SEGMENT_Y(segment->control);
nkeynes@639
   475
nkeynes@736
   476
        uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
nkeynes@736
   477
        if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
nkeynes@736
   478
            continue; // fully clipped, skip tile
nkeynes@736
   479
        }
nkeynes@736
   480
nkeynes@736
   481
        /* Clip to the visible part of the tile */
nkeynes@736
   482
        glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], 
nkeynes@736
   483
                   tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
nkeynes@1153
   484
        if( display_driver->capabilities.stencil_bits >= 2 && 
nkeynes@1153
   485
                IS_TILE_PTR(segment->opaquemod_ptr) &&
nkeynes@1153
   486
                !IS_EMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
nkeynes@1153
   487
            /* Don't do this unless there's actually some shadow polygons */
nkeynes@1153
   488
nkeynes@1153
   489
            /* Use colormask instead of drawbuffer for simplicity */
nkeynes@1153
   490
            glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
nkeynes@1153
   491
            gl_render_tilelist_depthonly(segment->opaque_ptr);
nkeynes@1153
   492
            gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
nkeynes@1153
   493
            glClear( GL_DEPTH_BUFFER_BIT );
nkeynes@1153
   494
            glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
nkeynes@1153
   495
        }
nkeynes@1153
   496
        gl_render_tilelist(segment->opaque_ptr,TRUE);
nkeynes@863
   497
        if( IS_TILE_PTR(segment->punchout_ptr) ) {
nkeynes@863
   498
            glEnable(GL_ALPHA_TEST );
nkeynes@1137
   499
            glDepthFunc(GL_GEQUAL);
nkeynes@1137
   500
            gl_render_tilelist(segment->punchout_ptr, FALSE );
nkeynes@863
   501
            glDisable(GL_ALPHA_TEST );
nkeynes@863
   502
        }
nkeynes@1133
   503
nkeynes@736
   504
        if( IS_TILE_PTR(segment->trans_ptr) ) {
nkeynes@736
   505
            if( pvr2_scene.sort_mode == SORT_NEVER || 
nkeynes@736
   506
                    (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
nkeynes@1137
   507
                gl_render_tilelist(segment->trans_ptr, TRUE);
nkeynes@736
   508
            } else {
nkeynes@736
   509
                render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
nkeynes@736
   510
            }
nkeynes@736
   511
        }
nkeynes@639
   512
    } while( !IS_LAST_SEGMENT(segment++) );
nkeynes@639
   513
    glDisable( GL_SCISSOR_TEST );
nkeynes@1132
   514
    glDisable( GL_COLOR_SUM );
nkeynes@1132
   515
    glDisable( GL_FOG );
nkeynes@1130
   516
    glsl_clear_shader();
nkeynes@669
   517
nkeynes@1159
   518
    pvr2_scene_finished();
nkeynes@1159
   519
nkeynes@645
   520
    gettimeofday( &end_tv, NULL );
nkeynes@645
   521
    ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
nkeynes@736
   522
    (end_tv.tv_usec - tex_tv.tv_usec)/1000;
nkeynes@645
   523
    DEBUG( "Scene render in %dms", ms );
nkeynes@639
   524
}
.