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