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