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