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