Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 863:a5e5310061e2
prev856:02ac5f37bfc9
next864:a90f3d5e57e1
author nkeynes
date Sun Sep 28 01:09:51 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Initial shadow volume implementation for opaque polygons (stencil isn't quite
right, but we get some kind of shadows now)
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@653
   125
/**
nkeynes@863
   126
 * Setup the basic context that's shared between normal and modified modes -
nkeynes@863
   127
 * depth, culling, shade model, and color sum
nkeynes@653
   128
 */
nkeynes@863
   129
static void render_set_base_context( uint32_t poly1 )
nkeynes@653
   130
{
nkeynes@653
   131
    glDepthFunc( POLY1_DEPTH_MODE(poly1) );
nkeynes@653
   132
    glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
nkeynes@736
   133
nkeynes@653
   134
    switch( POLY1_CULL_MODE(poly1) ) {
nkeynes@653
   135
    case CULL_NONE:
nkeynes@653
   136
    case CULL_SMALL:
nkeynes@736
   137
        glDisable( GL_CULL_FACE );
nkeynes@736
   138
        break;
nkeynes@653
   139
    case CULL_CCW:
nkeynes@736
   140
        glEnable( GL_CULL_FACE );
nkeynes@736
   141
        glFrontFace( GL_CW );
nkeynes@736
   142
        break;
nkeynes@653
   143
    case CULL_CW:
nkeynes@736
   144
        glEnable( GL_CULL_FACE );
nkeynes@736
   145
        glFrontFace( GL_CCW );
nkeynes@736
   146
        break;
nkeynes@653
   147
    }
nkeynes@653
   148
nkeynes@863
   149
    glShadeModel( POLY1_SHADE_MODEL(poly1) );
nkeynes@863
   150
nkeynes@653
   151
    if( POLY1_SPECULAR(poly1) ) {
nkeynes@736
   152
        glEnable(GL_COLOR_SUM);
nkeynes@653
   153
    } else {
nkeynes@736
   154
        glDisable(GL_COLOR_SUM);
nkeynes@653
   155
    }
nkeynes@863
   156
}
nkeynes@653
   157
nkeynes@863
   158
/**
nkeynes@863
   159
 * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
nkeynes@863
   160
 */
nkeynes@863
   161
static void render_set_tsp_context( uint32_t poly1, uint32_t poly2, uint32_t texture )
nkeynes@863
   162
{
nkeynes@863
   163
    if( POLY1_TEXTURED(poly1) ) {
nkeynes@863
   164
         glEnable(GL_TEXTURE_2D);
nkeynes@863
   165
         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
nkeynes@863
   166
         if( POLY2_TEX_CLAMP_U(poly2) ) {
nkeynes@863
   167
             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
nkeynes@863
   168
         } else if( POLY2_TEX_MIRROR_U(poly2) ) {
nkeynes@863
   169
             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB );
nkeynes@863
   170
         } else {
nkeynes@863
   171
             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
nkeynes@863
   172
         }       
nkeynes@863
   173
         if( POLY2_TEX_CLAMP_V(poly2) ) {
nkeynes@863
   174
             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
nkeynes@863
   175
         } else if( POLY2_TEX_MIRROR_V(poly2) ) {
nkeynes@863
   176
             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB );
nkeynes@863
   177
         } else {
nkeynes@863
   178
             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
nkeynes@863
   179
         }
nkeynes@863
   180
     } else {
nkeynes@863
   181
         glDisable( GL_TEXTURE_2D );
nkeynes@863
   182
     }
nkeynes@863
   183
     
nkeynes@863
   184
     switch( POLY2_FOG_MODE(poly2) ) {
nkeynes@863
   185
     case PVR2_POLY_FOG_LOOKUP:
nkeynes@863
   186
         glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
nkeynes@863
   187
         glEnable( GL_FOG );
nkeynes@863
   188
         break;
nkeynes@863
   189
     case PVR2_POLY_FOG_VERTEX:
nkeynes@863
   190
         if( POLY1_SPECULAR(poly1) ) {
nkeynes@863
   191
             glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
nkeynes@863
   192
             glEnable( GL_FOG );
nkeynes@863
   193
             break;
nkeynes@863
   194
         } /* else fallthrough */
nkeynes@863
   195
     default:
nkeynes@863
   196
         glDisable( GL_FOG );
nkeynes@863
   197
     }
nkeynes@653
   198
nkeynes@863
   199
     int srcblend = POLY2_SRC_BLEND(poly2);
nkeynes@863
   200
     int destblend = POLY2_DEST_BLEND(poly2);
nkeynes@863
   201
     glBlendFunc( srcblend, destblend );
nkeynes@863
   202
nkeynes@863
   203
     if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
nkeynes@863
   204
         WARN( "Accumulation buffer not supported" );
nkeynes@863
   205
     }   
nkeynes@863
   206
}
nkeynes@863
   207
nkeynes@863
   208
/**
nkeynes@863
   209
 * Setup the GL context for the supplied polygon context.
nkeynes@863
   210
 * @param context pointer to 3 or 5 words of polygon context
nkeynes@863
   211
 * @param modified boolean flag indicating that the modified
nkeynes@863
   212
 *  version should be used, rather than the normal version.
nkeynes@863
   213
 */
nkeynes@863
   214
void render_set_context( uint32_t *context, int render_mode )
nkeynes@863
   215
{
nkeynes@863
   216
    uint32_t poly1 = context[0], poly2, texture;
nkeynes@863
   217
    if( render_mode == RENDER_FULLMOD ) {
nkeynes@863
   218
        poly2 = context[3];
nkeynes@863
   219
        texture = context[4];
nkeynes@653
   220
    } else {
nkeynes@863
   221
        poly2 = context[1];
nkeynes@863
   222
        texture = context[2];
nkeynes@653
   223
    }
nkeynes@653
   224
nkeynes@863
   225
    render_set_base_context(poly1);
nkeynes@863
   226
    render_set_tsp_context(poly1,poly2,texture);
nkeynes@653
   227
}
nkeynes@653
   228
nkeynes@653
   229
nkeynes@653
   230
static void gl_render_poly( struct polygon_struct *poly )
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@863
   236
        render_set_context( poly->context, RENDER_NORMAL );
nkeynes@863
   237
        glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
nkeynes@863
   238
    } else {
nkeynes@863
   239
        render_set_base_context( poly->context[0] );
nkeynes@863
   240
        render_set_tsp_context( poly->context[0], poly->context[1], poly->context[2] );
nkeynes@863
   241
        glStencilFunc(GL_EQUAL, 0, 1);
nkeynes@863
   242
        glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
nkeynes@863
   243
nkeynes@863
   244
        if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
nkeynes@863
   245
            if( poly->mod_tex_id != -1 ) {
nkeynes@863
   246
                glBindTexture(GL_TEXTURE_2D, poly->mod_tex_id);
nkeynes@863
   247
            }
nkeynes@863
   248
            render_set_tsp_context( poly->context[0], poly->context[3], poly->context[4] );
nkeynes@863
   249
        }
nkeynes@863
   250
        glStencilFunc(GL_EQUAL, 1, 1);
nkeynes@863
   251
        glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count );
nkeynes@863
   252
    }
nkeynes@653
   253
}
nkeynes@653
   254
nkeynes@687
   255
static void gl_render_bkgnd( struct polygon_struct *poly )
nkeynes@687
   256
{
nkeynes@687
   257
    if( poly->tex_id != -1 ) {
nkeynes@687
   258
        glBindTexture(GL_TEXTURE_2D, poly->tex_id);
nkeynes@687
   259
    }
nkeynes@687
   260
    render_set_context( poly->context, RENDER_NORMAL );
nkeynes@687
   261
    glDisable( GL_DEPTH_TEST );
nkeynes@687
   262
    glDisable( GL_CULL_FACE );
nkeynes@687
   263
    glBlendFunc( GL_ONE, GL_ZERO );
nkeynes@687
   264
    glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
nkeynes@687
   265
    glEnable( GL_CULL_FACE );
nkeynes@687
   266
    glEnable( GL_DEPTH_TEST );
nkeynes@687
   267
}
nkeynes@687
   268
nkeynes@653
   269
void gl_render_tilelist( pvraddr_t tile_entry )
nkeynes@653
   270
{
nkeynes@653
   271
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@653
   272
    int strip_count;
nkeynes@653
   273
    struct polygon_struct *poly;
nkeynes@653
   274
nkeynes@863
   275
    if( !IS_TILE_PTR(tile_entry) )
nkeynes@863
   276
        return;
nkeynes@863
   277
nkeynes@653
   278
    while(1) {
nkeynes@736
   279
        uint32_t entry = *tile_list++;
nkeynes@736
   280
        switch( entry >> 28 ) {
nkeynes@736
   281
        case 0x0F:
nkeynes@736
   282
            return; // End-of-list
nkeynes@736
   283
        case 0x0E:
nkeynes@736
   284
            tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@736
   285
            break;
nkeynes@736
   286
        case 0x08: case 0x09: case 0x0A: case 0x0B:
nkeynes@736
   287
            strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@736
   288
            poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@736
   289
            while( strip_count > 0 ) {
nkeynes@736
   290
                assert( poly != NULL );
nkeynes@736
   291
                gl_render_poly( poly );
nkeynes@736
   292
                poly = poly->next;
nkeynes@736
   293
                strip_count--;
nkeynes@736
   294
            }
nkeynes@736
   295
            break;
nkeynes@736
   296
        default:
nkeynes@736
   297
            if( entry & 0x7E000000 ) {
nkeynes@736
   298
                poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@736
   299
                gl_render_poly( poly );
nkeynes@736
   300
            }
nkeynes@736
   301
        }
nkeynes@863
   302
    }       
nkeynes@863
   303
}
nkeynes@863
   304
nkeynes@863
   305
/**
nkeynes@863
   306
 * Render the tilelist with depthbuffer updates only. 
nkeynes@863
   307
 */
nkeynes@863
   308
void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
nkeynes@863
   309
{
nkeynes@863
   310
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@863
   311
    int strip_count;
nkeynes@863
   312
    struct polygon_struct *poly;
nkeynes@863
   313
    
nkeynes@863
   314
    if( !IS_TILE_PTR(tile_entry) )
nkeynes@863
   315
        return;
nkeynes@863
   316
nkeynes@863
   317
    glDisable( GL_TEXTURE_2D );
nkeynes@863
   318
    glDisable( GL_FOG );
nkeynes@863
   319
    glDisable( GL_COLOR_SUM );
nkeynes@863
   320
    
nkeynes@863
   321
    while(1) {
nkeynes@863
   322
        uint32_t entry = *tile_list++;
nkeynes@863
   323
        switch( entry >> 28 ) {
nkeynes@863
   324
        case 0x0F:
nkeynes@863
   325
            return; // End-of-list
nkeynes@863
   326
        case 0x0E:
nkeynes@863
   327
            tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@863
   328
            break;
nkeynes@863
   329
        case 0x08: case 0x09: case 0x0A: case 0x0B:
nkeynes@863
   330
            strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@863
   331
            poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@863
   332
            while( strip_count > 0 ) {
nkeynes@863
   333
                render_set_base_context(poly->context[0]);
nkeynes@863
   334
                glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
nkeynes@863
   335
                poly = poly->next;
nkeynes@863
   336
                strip_count--;
nkeynes@863
   337
            }
nkeynes@863
   338
            break;
nkeynes@863
   339
        default:
nkeynes@863
   340
            if( entry & 0x7E000000 ) {
nkeynes@863
   341
                poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@863
   342
                render_set_base_context(poly->context[0]);
nkeynes@863
   343
                glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
nkeynes@863
   344
            }
nkeynes@863
   345
        }
nkeynes@863
   346
    }           
nkeynes@863
   347
}
nkeynes@863
   348
nkeynes@863
   349
void gl_render_modifier_tilelist( pvraddr_t tile_entry )
nkeynes@863
   350
{
nkeynes@863
   351
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@863
   352
    int strip_count;
nkeynes@863
   353
    struct polygon_struct *poly;
nkeynes@863
   354
nkeynes@863
   355
    if( !IS_TILE_PTR(tile_entry) )
nkeynes@863
   356
        return;
nkeynes@863
   357
nkeynes@863
   358
    glDisable( GL_TEXTURE_2D );
nkeynes@863
   359
    glDisable( GL_FOG );
nkeynes@863
   360
    glDisable( GL_COLOR_SUM );
nkeynes@863
   361
    glDisable( GL_CULL_FACE );
nkeynes@863
   362
    glEnable( GL_STENCIL_TEST );
nkeynes@863
   363
    glEnable( GL_DEPTH_TEST );
nkeynes@863
   364
    glDepthFunc( GL_LEQUAL );
nkeynes@863
   365
    
nkeynes@863
   366
    glStencilFunc( GL_ALWAYS, 0, 1 );
nkeynes@863
   367
    glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
nkeynes@863
   368
nkeynes@863
   369
    glDepthMask( GL_FALSE );
nkeynes@863
   370
    
nkeynes@863
   371
    while(1) {
nkeynes@863
   372
        uint32_t entry = *tile_list++;
nkeynes@863
   373
        switch( entry >> 28 ) {
nkeynes@863
   374
        case 0x0F:
nkeynes@863
   375
            glDepthMask( GL_TRUE );
nkeynes@863
   376
            glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
nkeynes@863
   377
            return; // End-of-list
nkeynes@863
   378
        case 0x0E:
nkeynes@863
   379
            tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@863
   380
            break;
nkeynes@863
   381
        case 0x08: case 0x09: case 0x0A: case 0x0B:
nkeynes@863
   382
            strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@863
   383
            poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@863
   384
            while( strip_count > 0 ) {
nkeynes@863
   385
                glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
nkeynes@863
   386
                poly = poly->next;
nkeynes@863
   387
                strip_count--;
nkeynes@863
   388
            }
nkeynes@863
   389
            break;
nkeynes@863
   390
        default:
nkeynes@863
   391
            if( entry & 0x7E000000 ) {
nkeynes@863
   392
                poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@863
   393
                glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
nkeynes@863
   394
            }
nkeynes@863
   395
        }
nkeynes@863
   396
    }
nkeynes@863
   397
    
nkeynes@653
   398
}
nkeynes@653
   399
nkeynes@653
   400
nkeynes@653
   401
/**
nkeynes@653
   402
 * Render the currently defined scene in pvr2_scene
nkeynes@653
   403
 */
nkeynes@653
   404
void pvr2_scene_render( render_buffer_t buffer )
nkeynes@653
   405
{
nkeynes@653
   406
    /* Scene setup */
nkeynes@653
   407
    struct timeval start_tv, tex_tv, end_tv;
nkeynes@653
   408
nkeynes@653
   409
    gettimeofday(&start_tv, NULL);
nkeynes@653
   410
    display_driver->set_render_target(buffer);
nkeynes@653
   411
    pvr2_check_palette_changed();
nkeynes@653
   412
    pvr2_scene_load_textures();
nkeynes@736
   413
nkeynes@653
   414
    gettimeofday( &tex_tv, NULL );
nkeynes@653
   415
    uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
nkeynes@736
   416
    (tex_tv.tv_usec - start_tv.tv_usec)/1000;
nkeynes@653
   417
    DEBUG( "Scene setup in %dms", ms );
nkeynes@653
   418
nkeynes@653
   419
    /* Setup view projection matrix */
nkeynes@653
   420
    glMatrixMode(GL_PROJECTION);
nkeynes@653
   421
    glLoadIdentity();
nkeynes@653
   422
    float nearz = pvr2_scene.bounds[4];
nkeynes@653
   423
    float farz = pvr2_scene.bounds[5];
nkeynes@653
   424
    if( nearz == farz ) {
nkeynes@687
   425
        farz*= 4.0;
nkeynes@653
   426
    }
nkeynes@653
   427
    glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0, 
nkeynes@736
   428
             -farz, -nearz );
nkeynes@653
   429
    float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
nkeynes@653
   430
    glAlphaFunc( GL_GEQUAL, alphaRef );
nkeynes@653
   431
nkeynes@653
   432
    /* Clear the buffer (FIXME: May not want always want to do this) */
nkeynes@653
   433
    glDisable( GL_SCISSOR_TEST );
nkeynes@653
   434
    glDepthMask( GL_TRUE );
nkeynes@653
   435
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
nkeynes@653
   436
nkeynes@653
   437
    /* Setup vertex array pointers */
nkeynes@687
   438
    glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
nkeynes@687
   439
    glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
nkeynes@687
   440
    glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
nkeynes@687
   441
    glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
nkeynes@847
   442
    glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
nkeynes@847
   443
    glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
nkeynes@847
   444
    glFogi(GL_FOG_MODE, GL_LINEAR);
nkeynes@847
   445
    glFogf(GL_FOG_START, 0.0);
nkeynes@847
   446
    glFogf(GL_FOG_END, 1.0);
nkeynes@669
   447
    /* Turn on the shaders (if available) */
nkeynes@669
   448
    glsl_enable_shaders(TRUE);
nkeynes@669
   449
nkeynes@687
   450
    /* Render the background */
nkeynes@687
   451
    gl_render_bkgnd( pvr2_scene.bkgnd_poly );
nkeynes@736
   452
nkeynes@653
   453
    glEnable( GL_SCISSOR_TEST );
nkeynes@653
   454
nkeynes@653
   455
    /* Process the segment list */
nkeynes@653
   456
    struct tile_segment *segment = pvr2_scene.segment_list;
nkeynes@653
   457
    do {
nkeynes@736
   458
        int tilex = SEGMENT_X(segment->control);
nkeynes@736
   459
        int tiley = SEGMENT_Y(segment->control);
nkeynes@653
   460
nkeynes@736
   461
        uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
nkeynes@736
   462
        if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
nkeynes@736
   463
            continue; // fully clipped, skip tile
nkeynes@736
   464
        }
nkeynes@736
   465
nkeynes@736
   466
        /* Clip to the visible part of the tile */
nkeynes@736
   467
        glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], 
nkeynes@736
   468
                   tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
nkeynes@863
   469
        if( display_driver->capabilities.stencil_bits != 0 && 
nkeynes@863
   470
                IS_TILE_PTR(segment->opaquemod_ptr) &&
nkeynes@863
   471
                !IS_EMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
nkeynes@863
   472
            /* Don't do this unless there's actually some shadow polygons */
nkeynes@863
   473
nkeynes@863
   474
            /* Use colormask instead of drawbuffer for simplicity */
nkeynes@863
   475
            glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
nkeynes@863
   476
            gl_render_tilelist_depthonly(segment->opaque_ptr);
nkeynes@863
   477
            gl_render_modifier_tilelist(segment->opaquemod_ptr);
nkeynes@863
   478
            glClear( GL_DEPTH_BUFFER_BIT );
nkeynes@863
   479
            glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
nkeynes@736
   480
        }
nkeynes@863
   481
        gl_render_tilelist(segment->opaque_ptr);
nkeynes@863
   482
        if( IS_TILE_PTR(segment->punchout_ptr) ) {
nkeynes@863
   483
            glEnable(GL_ALPHA_TEST );
nkeynes@863
   484
            render_autosort_tile(segment->punchout_ptr, RENDER_NORMAL );
nkeynes@863
   485
            glDisable(GL_ALPHA_TEST );
nkeynes@863
   486
        }
nkeynes@863
   487
        glDisable( GL_STENCIL_TEST );
nkeynes@863
   488
        
nkeynes@736
   489
        if( IS_TILE_PTR(segment->trans_ptr) ) {
nkeynes@736
   490
            if( pvr2_scene.sort_mode == SORT_NEVER || 
nkeynes@736
   491
                    (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
nkeynes@736
   492
                gl_render_tilelist(segment->trans_ptr);
nkeynes@736
   493
            } else {
nkeynes@736
   494
                render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
nkeynes@736
   495
            }
nkeynes@736
   496
        }
nkeynes@653
   497
    } while( !IS_LAST_SEGMENT(segment++) );
nkeynes@653
   498
    glDisable( GL_SCISSOR_TEST );
nkeynes@653
   499
nkeynes@669
   500
    glsl_enable_shaders(FALSE);
nkeynes@669
   501
nkeynes@653
   502
    gettimeofday( &end_tv, NULL );
nkeynes@653
   503
    ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
nkeynes@736
   504
    (end_tv.tv_usec - tex_tv.tv_usec)/1000;
nkeynes@653
   505
    DEBUG( "Scene render in %dms", ms );
nkeynes@653
   506
}
.