Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 864:a90f3d5e57e1
prev863:a5e5310061e2
next865:e10c081f4b81
author nkeynes
date Mon Sep 29 05:55:33 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Compute accurate shadow volume stencils
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@864
   147
static void render_set_base_context( uint32_t poly1 )
nkeynes@864
   148
{
nkeynes@864
   149
    glDepthFunc( POLY1_DEPTH_MODE(poly1) );
nkeynes@864
   150
    glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
nkeynes@864
   151
nkeynes@864
   152
    render_set_cull( poly1 );
nkeynes@864
   153
    
nkeynes@863
   154
    glShadeModel( POLY1_SHADE_MODEL(poly1) );
nkeynes@863
   155
nkeynes@653
   156
    if( POLY1_SPECULAR(poly1) ) {
nkeynes@736
   157
        glEnable(GL_COLOR_SUM);
nkeynes@653
   158
    } else {
nkeynes@736
   159
        glDisable(GL_COLOR_SUM);
nkeynes@653
   160
    }
nkeynes@863
   161
}
nkeynes@653
   162
nkeynes@863
   163
/**
nkeynes@863
   164
 * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
nkeynes@863
   165
 */
nkeynes@863
   166
static void render_set_tsp_context( uint32_t poly1, uint32_t poly2, uint32_t texture )
nkeynes@863
   167
{
nkeynes@863
   168
    if( POLY1_TEXTURED(poly1) ) {
nkeynes@863
   169
         glEnable(GL_TEXTURE_2D);
nkeynes@863
   170
         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
nkeynes@863
   171
         if( POLY2_TEX_CLAMP_U(poly2) ) {
nkeynes@863
   172
             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
nkeynes@863
   173
         } else if( POLY2_TEX_MIRROR_U(poly2) ) {
nkeynes@863
   174
             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB );
nkeynes@863
   175
         } else {
nkeynes@863
   176
             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
nkeynes@863
   177
         }       
nkeynes@863
   178
         if( POLY2_TEX_CLAMP_V(poly2) ) {
nkeynes@863
   179
             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
nkeynes@863
   180
         } else if( POLY2_TEX_MIRROR_V(poly2) ) {
nkeynes@863
   181
             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB );
nkeynes@863
   182
         } else {
nkeynes@863
   183
             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
nkeynes@863
   184
         }
nkeynes@863
   185
     } else {
nkeynes@863
   186
         glDisable( GL_TEXTURE_2D );
nkeynes@863
   187
     }
nkeynes@863
   188
     
nkeynes@863
   189
     switch( POLY2_FOG_MODE(poly2) ) {
nkeynes@863
   190
     case PVR2_POLY_FOG_LOOKUP:
nkeynes@863
   191
         glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
nkeynes@863
   192
         glEnable( GL_FOG );
nkeynes@863
   193
         break;
nkeynes@863
   194
     case PVR2_POLY_FOG_VERTEX:
nkeynes@863
   195
         if( POLY1_SPECULAR(poly1) ) {
nkeynes@863
   196
             glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
nkeynes@863
   197
             glEnable( GL_FOG );
nkeynes@863
   198
             break;
nkeynes@863
   199
         } /* else fallthrough */
nkeynes@863
   200
     default:
nkeynes@863
   201
         glDisable( GL_FOG );
nkeynes@863
   202
     }
nkeynes@653
   203
nkeynes@863
   204
     int srcblend = POLY2_SRC_BLEND(poly2);
nkeynes@863
   205
     int destblend = POLY2_DEST_BLEND(poly2);
nkeynes@863
   206
     glBlendFunc( srcblend, destblend );
nkeynes@863
   207
nkeynes@863
   208
     if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
nkeynes@863
   209
         WARN( "Accumulation buffer not supported" );
nkeynes@863
   210
     }   
nkeynes@863
   211
}
nkeynes@863
   212
nkeynes@863
   213
/**
nkeynes@863
   214
 * Setup the GL context for the supplied polygon context.
nkeynes@863
   215
 * @param context pointer to 3 or 5 words of polygon context
nkeynes@863
   216
 * @param modified boolean flag indicating that the modified
nkeynes@863
   217
 *  version should be used, rather than the normal version.
nkeynes@863
   218
 */
nkeynes@863
   219
void render_set_context( uint32_t *context, int render_mode )
nkeynes@863
   220
{
nkeynes@863
   221
    uint32_t poly1 = context[0], poly2, texture;
nkeynes@863
   222
    if( render_mode == RENDER_FULLMOD ) {
nkeynes@863
   223
        poly2 = context[3];
nkeynes@863
   224
        texture = context[4];
nkeynes@653
   225
    } else {
nkeynes@863
   226
        poly2 = context[1];
nkeynes@863
   227
        texture = context[2];
nkeynes@653
   228
    }
nkeynes@653
   229
nkeynes@863
   230
    render_set_base_context(poly1);
nkeynes@863
   231
    render_set_tsp_context(poly1,poly2,texture);
nkeynes@653
   232
}
nkeynes@653
   233
nkeynes@653
   234
nkeynes@653
   235
static void gl_render_poly( struct polygon_struct *poly )
nkeynes@653
   236
{
nkeynes@653
   237
    if( poly->tex_id != -1 ) {
nkeynes@736
   238
        glBindTexture(GL_TEXTURE_2D, poly->tex_id);
nkeynes@653
   239
    }
nkeynes@863
   240
    if( poly->mod_vertex_index == -1 ) {
nkeynes@864
   241
        glDisable( GL_STENCIL_TEST );
nkeynes@863
   242
        render_set_context( poly->context, RENDER_NORMAL );
nkeynes@863
   243
        glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
nkeynes@864
   244
    }  else {
nkeynes@864
   245
        glEnable( GL_STENCIL_TEST );
nkeynes@863
   246
        render_set_base_context( poly->context[0] );
nkeynes@863
   247
        render_set_tsp_context( poly->context[0], poly->context[1], poly->context[2] );
nkeynes@864
   248
        glStencilFunc(GL_EQUAL, 0, 2);
nkeynes@863
   249
        glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
nkeynes@863
   250
nkeynes@863
   251
        if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
nkeynes@863
   252
            if( poly->mod_tex_id != -1 ) {
nkeynes@863
   253
                glBindTexture(GL_TEXTURE_2D, poly->mod_tex_id);
nkeynes@863
   254
            }
nkeynes@863
   255
            render_set_tsp_context( poly->context[0], poly->context[3], poly->context[4] );
nkeynes@863
   256
        }
nkeynes@864
   257
        glStencilFunc(GL_EQUAL, 2, 2);
nkeynes@863
   258
        glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count );
nkeynes@863
   259
    }
nkeynes@653
   260
}
nkeynes@653
   261
nkeynes@687
   262
static void gl_render_bkgnd( struct polygon_struct *poly )
nkeynes@687
   263
{
nkeynes@687
   264
    if( poly->tex_id != -1 ) {
nkeynes@687
   265
        glBindTexture(GL_TEXTURE_2D, poly->tex_id);
nkeynes@687
   266
    }
nkeynes@687
   267
    render_set_context( poly->context, RENDER_NORMAL );
nkeynes@687
   268
    glDisable( GL_DEPTH_TEST );
nkeynes@687
   269
    glDisable( GL_CULL_FACE );
nkeynes@687
   270
    glBlendFunc( GL_ONE, GL_ZERO );
nkeynes@687
   271
    glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
nkeynes@687
   272
    glEnable( GL_CULL_FACE );
nkeynes@687
   273
    glEnable( GL_DEPTH_TEST );
nkeynes@687
   274
}
nkeynes@687
   275
nkeynes@653
   276
void gl_render_tilelist( pvraddr_t tile_entry )
nkeynes@653
   277
{
nkeynes@653
   278
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@653
   279
    int strip_count;
nkeynes@653
   280
    struct polygon_struct *poly;
nkeynes@653
   281
nkeynes@863
   282
    if( !IS_TILE_PTR(tile_entry) )
nkeynes@863
   283
        return;
nkeynes@863
   284
nkeynes@653
   285
    while(1) {
nkeynes@736
   286
        uint32_t entry = *tile_list++;
nkeynes@736
   287
        switch( entry >> 28 ) {
nkeynes@736
   288
        case 0x0F:
nkeynes@736
   289
            return; // End-of-list
nkeynes@736
   290
        case 0x0E:
nkeynes@736
   291
            tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@736
   292
            break;
nkeynes@736
   293
        case 0x08: case 0x09: case 0x0A: case 0x0B:
nkeynes@736
   294
            strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@736
   295
            poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@736
   296
            while( strip_count > 0 ) {
nkeynes@736
   297
                assert( poly != NULL );
nkeynes@736
   298
                gl_render_poly( poly );
nkeynes@736
   299
                poly = poly->next;
nkeynes@736
   300
                strip_count--;
nkeynes@736
   301
            }
nkeynes@736
   302
            break;
nkeynes@736
   303
        default:
nkeynes@736
   304
            if( entry & 0x7E000000 ) {
nkeynes@736
   305
                poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@736
   306
                gl_render_poly( poly );
nkeynes@736
   307
            }
nkeynes@736
   308
        }
nkeynes@863
   309
    }       
nkeynes@863
   310
}
nkeynes@863
   311
nkeynes@863
   312
/**
nkeynes@863
   313
 * Render the tilelist with depthbuffer updates only. 
nkeynes@863
   314
 */
nkeynes@863
   315
void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
nkeynes@863
   316
{
nkeynes@863
   317
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@863
   318
    int strip_count;
nkeynes@863
   319
    struct polygon_struct *poly;
nkeynes@863
   320
    
nkeynes@863
   321
    if( !IS_TILE_PTR(tile_entry) )
nkeynes@863
   322
        return;
nkeynes@863
   323
nkeynes@863
   324
    glDisable( GL_TEXTURE_2D );
nkeynes@863
   325
    glDisable( GL_FOG );
nkeynes@863
   326
    glDisable( GL_COLOR_SUM );
nkeynes@863
   327
    
nkeynes@863
   328
    while(1) {
nkeynes@863
   329
        uint32_t entry = *tile_list++;
nkeynes@863
   330
        switch( entry >> 28 ) {
nkeynes@863
   331
        case 0x0F:
nkeynes@863
   332
            return; // End-of-list
nkeynes@863
   333
        case 0x0E:
nkeynes@863
   334
            tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@863
   335
            break;
nkeynes@863
   336
        case 0x08: case 0x09: case 0x0A: case 0x0B:
nkeynes@863
   337
            strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@863
   338
            poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@863
   339
            while( strip_count > 0 ) {
nkeynes@863
   340
                render_set_base_context(poly->context[0]);
nkeynes@863
   341
                glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
nkeynes@863
   342
                poly = poly->next;
nkeynes@863
   343
                strip_count--;
nkeynes@863
   344
            }
nkeynes@863
   345
            break;
nkeynes@863
   346
        default:
nkeynes@863
   347
            if( entry & 0x7E000000 ) {
nkeynes@863
   348
                poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@863
   349
                render_set_base_context(poly->context[0]);
nkeynes@863
   350
                glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
nkeynes@863
   351
            }
nkeynes@863
   352
        }
nkeynes@863
   353
    }           
nkeynes@863
   354
}
nkeynes@863
   355
nkeynes@864
   356
static void drawrect2d( uint32_t tile_bounds[], float z )
nkeynes@864
   357
{
nkeynes@864
   358
    glBegin( GL_QUADS );
nkeynes@864
   359
    glVertex3f( tile_bounds[0], tile_bounds[2], z );
nkeynes@864
   360
    glVertex3f( tile_bounds[1], tile_bounds[2], z );
nkeynes@864
   361
    glVertex3f( tile_bounds[1], tile_bounds[3], z );
nkeynes@864
   362
    glVertex3f( tile_bounds[0], tile_bounds[3], z );
nkeynes@864
   363
    glEnd();
nkeynes@864
   364
}
nkeynes@864
   365
nkeynes@864
   366
void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
nkeynes@864
   367
{
nkeynes@864
   368
    /* A bit of explanation:
nkeynes@864
   369
     * In theory it works like this: generate a 1-bit stencil for each polygon
nkeynes@864
   370
     * volume, and then AND or OR it against the overall 1-bit tile stencil at 
nkeynes@864
   371
     * the end of the volume. 
nkeynes@864
   372
     * 
nkeynes@864
   373
     * The implementation here uses a 2-bit stencil buffer, where each volume
nkeynes@864
   374
     * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
nkeynes@864
   375
     * to update bit 1 accordingly and clear bit 0.
nkeynes@864
   376
     * 
nkeynes@864
   377
     * This could probably be more efficient, but at least it works correctly 
nkeynes@864
   378
     * now :)
nkeynes@864
   379
     */
nkeynes@864
   380
    
nkeynes@864
   381
    render_set_cull(poly->context[0]);
nkeynes@864
   382
    glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
nkeynes@864
   383
    
nkeynes@864
   384
    int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
nkeynes@864
   385
    if( poly_type == PVR2_VOLUME_REGION0 ) {
nkeynes@864
   386
        /* 00 => 00
nkeynes@864
   387
         * 01 => 00
nkeynes@864
   388
         * 10 => 10
nkeynes@864
   389
         * 11 => 00
nkeynes@864
   390
         */
nkeynes@864
   391
        glStencilMask( 0x03 );
nkeynes@864
   392
        glStencilFunc(GL_EQUAL, 0x02, 0x03);
nkeynes@864
   393
        glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
nkeynes@864
   394
        glDisable( GL_CULL_FACE );
nkeynes@864
   395
        glDisable( GL_DEPTH_TEST );
nkeynes@864
   396
nkeynes@864
   397
        drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
nkeynes@864
   398
        
nkeynes@864
   399
        glEnable( GL_DEPTH_TEST );
nkeynes@864
   400
        glStencilMask( 0x01 );
nkeynes@864
   401
        glStencilFunc( GL_ALWAYS, 0, 1 );
nkeynes@864
   402
        glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
nkeynes@864
   403
    } else if( poly_type == PVR2_VOLUME_REGION1 ) {
nkeynes@864
   404
        /* This is harder with the standard stencil ops - do it in two passes
nkeynes@864
   405
         * 00 => 00 | 00 => 10
nkeynes@864
   406
         * 01 => 10 | 01 => 10
nkeynes@864
   407
         * 10 => 10 | 10 => 00
nkeynes@864
   408
         * 11 => 10 | 11 => 10
nkeynes@864
   409
         */
nkeynes@864
   410
        glStencilMask( 0x02 );
nkeynes@864
   411
        glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
nkeynes@864
   412
        glDisable( GL_CULL_FACE );
nkeynes@864
   413
        glDisable( GL_DEPTH_TEST );
nkeynes@864
   414
        
nkeynes@864
   415
        drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
nkeynes@864
   416
        
nkeynes@864
   417
        glStencilMask( 0x03 );
nkeynes@864
   418
        glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
nkeynes@864
   419
        glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
nkeynes@864
   420
        
nkeynes@864
   421
        drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
nkeynes@864
   422
        
nkeynes@864
   423
        glEnable( GL_DEPTH_TEST );
nkeynes@864
   424
        glStencilMask( 0x01 );
nkeynes@864
   425
        glStencilFunc( GL_ALWAYS, 0, 1 );
nkeynes@864
   426
        glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );         
nkeynes@864
   427
    }
nkeynes@864
   428
}
nkeynes@864
   429
nkeynes@864
   430
void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
nkeynes@863
   431
{
nkeynes@863
   432
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@863
   433
    int strip_count;
nkeynes@863
   434
    struct polygon_struct *poly;
nkeynes@863
   435
nkeynes@863
   436
    if( !IS_TILE_PTR(tile_entry) )
nkeynes@863
   437
        return;
nkeynes@863
   438
nkeynes@863
   439
    glDisable( GL_TEXTURE_2D );
nkeynes@863
   440
    glDisable( GL_FOG );
nkeynes@863
   441
    glDisable( GL_COLOR_SUM );
nkeynes@863
   442
    glDisable( GL_CULL_FACE );
nkeynes@863
   443
    glEnable( GL_STENCIL_TEST );
nkeynes@863
   444
    glEnable( GL_DEPTH_TEST );
nkeynes@863
   445
    glDepthFunc( GL_LEQUAL );
nkeynes@863
   446
    
nkeynes@863
   447
    glStencilFunc( GL_ALWAYS, 0, 1 );
nkeynes@863
   448
    glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
nkeynes@864
   449
    glStencilMask( 0x01 );
nkeynes@863
   450
    glDepthMask( GL_FALSE );
nkeynes@863
   451
    
nkeynes@863
   452
    while(1) {
nkeynes@863
   453
        uint32_t entry = *tile_list++;
nkeynes@863
   454
        switch( entry >> 28 ) {
nkeynes@863
   455
        case 0x0F:
nkeynes@863
   456
            glDepthMask( GL_TRUE );
nkeynes@863
   457
            glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
nkeynes@863
   458
            return; // End-of-list
nkeynes@863
   459
        case 0x0E:
nkeynes@863
   460
            tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@863
   461
            break;
nkeynes@863
   462
        case 0x08: case 0x09: case 0x0A: case 0x0B:
nkeynes@863
   463
            strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@863
   464
            poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@863
   465
            while( strip_count > 0 ) {
nkeynes@864
   466
                gl_render_modifier_polygon( poly, tile_bounds );
nkeynes@863
   467
                poly = poly->next;
nkeynes@863
   468
                strip_count--;
nkeynes@863
   469
            }
nkeynes@863
   470
            break;
nkeynes@863
   471
        default:
nkeynes@863
   472
            if( entry & 0x7E000000 ) {
nkeynes@863
   473
                poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@864
   474
                gl_render_modifier_polygon( poly, tile_bounds );
nkeynes@863
   475
            }
nkeynes@863
   476
        }
nkeynes@863
   477
    }
nkeynes@863
   478
    
nkeynes@653
   479
}
nkeynes@653
   480
nkeynes@653
   481
nkeynes@653
   482
/**
nkeynes@653
   483
 * Render the currently defined scene in pvr2_scene
nkeynes@653
   484
 */
nkeynes@653
   485
void pvr2_scene_render( render_buffer_t buffer )
nkeynes@653
   486
{
nkeynes@653
   487
    /* Scene setup */
nkeynes@653
   488
    struct timeval start_tv, tex_tv, end_tv;
nkeynes@653
   489
nkeynes@653
   490
    gettimeofday(&start_tv, NULL);
nkeynes@653
   491
    display_driver->set_render_target(buffer);
nkeynes@653
   492
    pvr2_check_palette_changed();
nkeynes@653
   493
    pvr2_scene_load_textures();
nkeynes@736
   494
nkeynes@653
   495
    gettimeofday( &tex_tv, NULL );
nkeynes@653
   496
    uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
nkeynes@736
   497
    (tex_tv.tv_usec - start_tv.tv_usec)/1000;
nkeynes@653
   498
    DEBUG( "Scene setup in %dms", ms );
nkeynes@653
   499
nkeynes@653
   500
    /* Setup view projection matrix */
nkeynes@653
   501
    glMatrixMode(GL_PROJECTION);
nkeynes@653
   502
    glLoadIdentity();
nkeynes@653
   503
    float nearz = pvr2_scene.bounds[4];
nkeynes@653
   504
    float farz = pvr2_scene.bounds[5];
nkeynes@653
   505
    if( nearz == farz ) {
nkeynes@687
   506
        farz*= 4.0;
nkeynes@653
   507
    }
nkeynes@653
   508
    glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0, 
nkeynes@736
   509
             -farz, -nearz );
nkeynes@653
   510
    float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
nkeynes@653
   511
    glAlphaFunc( GL_GEQUAL, alphaRef );
nkeynes@653
   512
nkeynes@653
   513
    /* Clear the buffer (FIXME: May not want always want to do this) */
nkeynes@653
   514
    glDisable( GL_SCISSOR_TEST );
nkeynes@653
   515
    glDepthMask( GL_TRUE );
nkeynes@864
   516
    glStencilMask( 0x03 );
nkeynes@653
   517
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
nkeynes@653
   518
nkeynes@653
   519
    /* Setup vertex array pointers */
nkeynes@687
   520
    glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
nkeynes@687
   521
    glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
nkeynes@687
   522
    glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
nkeynes@687
   523
    glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
nkeynes@847
   524
    glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
nkeynes@847
   525
    glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
nkeynes@847
   526
    glFogi(GL_FOG_MODE, GL_LINEAR);
nkeynes@847
   527
    glFogf(GL_FOG_START, 0.0);
nkeynes@847
   528
    glFogf(GL_FOG_END, 1.0);
nkeynes@669
   529
    /* Turn on the shaders (if available) */
nkeynes@669
   530
    glsl_enable_shaders(TRUE);
nkeynes@669
   531
nkeynes@687
   532
    /* Render the background */
nkeynes@687
   533
    gl_render_bkgnd( pvr2_scene.bkgnd_poly );
nkeynes@736
   534
nkeynes@653
   535
    glEnable( GL_SCISSOR_TEST );
nkeynes@653
   536
nkeynes@653
   537
    /* Process the segment list */
nkeynes@653
   538
    struct tile_segment *segment = pvr2_scene.segment_list;
nkeynes@653
   539
    do {
nkeynes@736
   540
        int tilex = SEGMENT_X(segment->control);
nkeynes@736
   541
        int tiley = SEGMENT_Y(segment->control);
nkeynes@653
   542
nkeynes@736
   543
        uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
nkeynes@736
   544
        if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
nkeynes@736
   545
            continue; // fully clipped, skip tile
nkeynes@736
   546
        }
nkeynes@736
   547
nkeynes@736
   548
        /* Clip to the visible part of the tile */
nkeynes@736
   549
        glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], 
nkeynes@736
   550
                   tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
nkeynes@864
   551
        if( display_driver->capabilities.stencil_bits >= 2 && 
nkeynes@863
   552
                IS_TILE_PTR(segment->opaquemod_ptr) &&
nkeynes@863
   553
                !IS_EMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
nkeynes@863
   554
            /* Don't do this unless there's actually some shadow polygons */
nkeynes@863
   555
nkeynes@863
   556
            /* Use colormask instead of drawbuffer for simplicity */
nkeynes@863
   557
            glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
nkeynes@863
   558
            gl_render_tilelist_depthonly(segment->opaque_ptr);
nkeynes@864
   559
            gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
nkeynes@863
   560
            glClear( GL_DEPTH_BUFFER_BIT );
nkeynes@863
   561
            glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
nkeynes@736
   562
        }
nkeynes@863
   563
        gl_render_tilelist(segment->opaque_ptr);
nkeynes@863
   564
        if( IS_TILE_PTR(segment->punchout_ptr) ) {
nkeynes@863
   565
            glEnable(GL_ALPHA_TEST );
nkeynes@863
   566
            render_autosort_tile(segment->punchout_ptr, RENDER_NORMAL );
nkeynes@863
   567
            glDisable(GL_ALPHA_TEST );
nkeynes@863
   568
        }
nkeynes@863
   569
        glDisable( GL_STENCIL_TEST );
nkeynes@863
   570
        
nkeynes@736
   571
        if( IS_TILE_PTR(segment->trans_ptr) ) {
nkeynes@736
   572
            if( pvr2_scene.sort_mode == SORT_NEVER || 
nkeynes@736
   573
                    (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
nkeynes@736
   574
                gl_render_tilelist(segment->trans_ptr);
nkeynes@736
   575
            } else {
nkeynes@736
   576
                render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
nkeynes@736
   577
            }
nkeynes@736
   578
        }
nkeynes@653
   579
    } while( !IS_LAST_SEGMENT(segment++) );
nkeynes@653
   580
    glDisable( GL_SCISSOR_TEST );
nkeynes@653
   581
nkeynes@669
   582
    glsl_enable_shaders(FALSE);
nkeynes@669
   583
nkeynes@653
   584
    gettimeofday( &end_tv, NULL );
nkeynes@653
   585
    ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
nkeynes@736
   586
    (end_tv.tv_usec - tex_tv.tv_usec)/1000;
nkeynes@653
   587
    DEBUG( "Scene render in %dms", ms );
nkeynes@653
   588
}
.