Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 1233:06923d1020de
prev1232:e5b12e2fe6ba
next1236:d93175c36387
author nkeynes
date Thu Feb 23 22:40:50 2012 +1000 (7 years ago)
permissions -rw-r--r--
last change Fix non-shader rendering
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Standard OpenGL rendering engine. 
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <assert.h>
    20 #include <sys/time.h>
    21 #include "display.h"
    22 #include "pvr2/pvr2.h"
    23 #include "pvr2/pvr2mmio.h"
    24 #include "pvr2/glutil.h"
    25 #include "pvr2/scene.h"
    26 #include "pvr2/tileiter.h"
    27 #include "pvr2/shaders.h"
    29 #ifdef APPLE_BUILD
    30 #include "OpenGL/CGLCurrent.h"
    31 #include "OpenGL/CGLMacro.h"
    33 static CGLContextObj CGL_MACRO_CONTEXT;
    34 #endif
    36 #define IS_NONEMPTY_TILE_LIST(p) (IS_TILE_PTR(p) && ((*((uint32_t *)(pvr2_main_ram+(p))) >> 28) != 0x0F))
    38 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
    39         GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, 
    40         GL_ALWAYS };
    41 int pvr2_poly_srcblend[8] = { 
    42         GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
    43         GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, 
    44         GL_ONE_MINUS_DST_ALPHA };
    45 int pvr2_poly_dstblend[8] = {
    46         GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
    47         GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
    48         GL_ONE_MINUS_DST_ALPHA };
    50 static gboolean have_shaders = FALSE;
    51 static int currentTexId = -1;
    53 static inline void bind_texture(int texid)
    54 {
    55     if( currentTexId != texid ) {
    56         currentTexId = texid;
    57         glBindTexture(GL_TEXTURE_2D, texid);
    58     }
    59 }
    61 /**
    62  * Clip the tile bounds to the clipping plane. 
    63  * @return TRUE if the tile was not clipped completely.
    64  */
    65 static gboolean clip_tile_bounds( uint32_t *tile, uint32_t *clip )
    66 {
    67     if( tile[0] < clip[0] ) tile[0] = clip[0];
    68     if( tile[1] > clip[1] ) tile[1] = clip[1];
    69     if( tile[2] < clip[2] ) tile[2] = clip[2];
    70     if( tile[3] > clip[3] ) tile[3] = clip[3];
    71     return tile[0] < tile[1] && tile[2] < tile[3];
    72 }
    74 static void drawrect2d( uint32_t tile_bounds[], float z )
    75 {
    76     glBegin( GL_TRIANGLE_STRIP );
    77     glVertex3f( tile_bounds[0], tile_bounds[2], z );
    78     glVertex3f( tile_bounds[1], tile_bounds[2], z );
    79     glVertex3f( tile_bounds[0], tile_bounds[3], z );
    80     glVertex3f( tile_bounds[1], tile_bounds[3], z );
    81     glEnd();
    82 }
    84 static void pvr2_scene_load_textures()
    85 {
    86     int i;
    88     texcache_begin_scene( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03,
    89                          (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5 );
    91     for( i=0; i < pvr2_scene.poly_count; i++ ) {
    92         struct polygon_struct *poly = &pvr2_scene.poly_array[i];
    93         if( POLY1_TEXTURED(poly->context[0]) ) {
    94             poly->tex_id = texcache_get_texture( poly->context[1], poly->context[2] );
    95             if( poly->mod_vertex_index != -1 ) {
    96                 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
    97                     poly->mod_tex_id = texcache_get_texture( poly->context[3], poly->context[4] );
    98                 } else {
    99                     poly->mod_tex_id = poly->tex_id;
   100                 }
   101             }
   102         } else {
   103             poly->tex_id = 0;
   104             poly->mod_tex_id = 0;
   105         }
   106     }
   107 }
   110 /**
   111  * Once-off call to setup the OpenGL context.
   112  */
   113 void pvr2_setup_gl_context()
   114 {
   115     if( glsl_is_supported() && isGLMultitextureSupported() ) {
   116         if( !glsl_load_shaders( ) ) {
   117             WARN( "Unable to load GL shaders" );
   118         } else {
   119             have_shaders = TRUE;
   120         }
   121     }
   123 #ifdef APPLE_BUILD
   124     CGL_MACRO_CONTEXT = CGLGetCurrentContext();
   125 #endif
   126     texcache_gl_init(have_shaders); // Allocate texture IDs
   127     glDisable( GL_CULL_FACE );
   128     glEnable( GL_BLEND );
   129     glEnable( GL_DEPTH_TEST );
   131 #ifdef HAVE_OPENGL_CLAMP_COLOR
   132     if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
   133         glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
   134         glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
   135     }
   136 #endif
   138     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   140 #ifdef HAVE_OPENGL_CLEAR_DEPTHF
   141     glClearDepthf(0);
   142 #else
   143     glClearDepth(0);
   144 #endif
   145     glClearStencil(0);
   146 }
   148 /**
   149  * Setup the basic context that's shared between normal and modified modes -
   150  * depth, culling
   151  */
   152 static void render_set_base_context( uint32_t poly1, gboolean set_depth )
   153 {
   154     if( set_depth ) {
   155         glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   156     }
   158     glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
   159 }
   161 /**
   162  * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
   163  */
   164 static void render_set_tsp_context( uint32_t poly1, uint32_t poly2 )
   165 {
   166     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   168 #ifdef HAVE_OPENGL_FIXEDFUNC
   169     if( !have_shaders ) {
   170         if( POLY1_TEXTURED(poly1) ) {
   171             if( POLY2_TEX_BLEND(poly2) == 2 )
   172                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
   173             else
   174                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
   176          }
   178          switch( POLY2_FOG_MODE(poly2) ) {
   179          case PVR2_POLY_FOG_LOOKUP:
   180              glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
   181              break;
   182          case PVR2_POLY_FOG_VERTEX:
   183              glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
   184              break;
   185          }
   186      }
   187 #endif
   189      int srcblend = POLY2_SRC_BLEND(poly2);
   190      int destblend = POLY2_DEST_BLEND(poly2);
   191      glBlendFunc( srcblend, destblend );
   193      if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
   194          WARN( "Accumulation buffer not supported" );
   195      }   
   196 }
   198 /**
   199  * Setup the GL context for the supplied polygon context.
   200  * @param context pointer to 3 or 5 words of polygon context
   201  * @param depth_mode force depth mode, or 0 to use the polygon's
   202  * depth mode.
   203  */
   204 static void render_set_context( uint32_t *context, gboolean set_depth )
   205 {
   206     render_set_base_context(context[0], set_depth);
   207     render_set_tsp_context(context[0],context[1]);
   208 }
   210 static inline void gl_draw_vertexes( struct polygon_struct *poly )
   211 {
   212     do {
   213         glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
   214         poly = poly->sub_next;
   215     } while( poly != NULL );
   216 }
   218 static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
   219 {
   220     do {
   221         glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
   222         poly = poly->sub_next;
   223     } while( poly != NULL );
   224 }
   226 static void gl_render_poly( struct polygon_struct *poly, gboolean set_depth)
   227 {
   228     if( poly->vertex_count == 0 )
   229         return; /* Culled */
   231     bind_texture(poly->tex_id);
   232     if( poly->mod_vertex_index == -1 ) {
   233         render_set_context( poly->context, set_depth );
   234         gl_draw_vertexes(poly);
   235     }  else {
   236         glEnable( GL_STENCIL_TEST );
   237         render_set_base_context( poly->context[0], set_depth );
   238         render_set_tsp_context( poly->context[0], poly->context[1] );
   239         glStencilFunc(GL_EQUAL, 0, 2);
   240         gl_draw_vertexes(poly);
   242         if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
   243             bind_texture(poly->mod_tex_id);
   244             render_set_tsp_context( poly->context[0], poly->context[3] );
   245         }
   246         glStencilFunc(GL_EQUAL, 2, 2);
   247         gl_draw_mod_vertexes(poly);
   248         glDisable( GL_STENCIL_TEST );
   249     }
   250 }
   253 static void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
   254 {
   255     /* A bit of explanation:
   256      * In theory it works like this: generate a 1-bit stencil for each polygon
   257      * volume, and then AND or OR it against the overall 1-bit tile stencil at 
   258      * the end of the volume. 
   259      * 
   260      * The implementation here uses a 2-bit stencil buffer, where each volume
   261      * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
   262      * to update bit 1 accordingly and clear bit 0.
   263      * 
   264      * This could probably be more efficient, but at least it works correctly 
   265      * now :)
   266      */
   268     if( poly->vertex_count == 0 )
   269         return; /* Culled */
   271     gl_draw_vertexes(poly);
   275     int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
   276     if( poly_type == PVR2_VOLUME_REGION0 ) {
   277         /* 00 => 00
   278          * 01 => 00
   279          * 10 => 10
   280          * 11 => 00
   281          */
   282         glStencilMask( 0x03 );
   283         glStencilFunc(GL_EQUAL, 0x02, 0x03);
   284         glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
   285         glDisable( GL_DEPTH_TEST );
   287         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   289         glEnable( GL_DEPTH_TEST );
   290         glStencilMask( 0x01 );
   291         glStencilFunc( GL_ALWAYS, 0, 1 );
   292         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
   293     } else if( poly_type == PVR2_VOLUME_REGION1 ) {
   294         /* This is harder with the standard stencil ops - do it in two passes
   295          * 00 => 00 | 00 => 10
   296          * 01 => 10 | 01 => 10
   297          * 10 => 10 | 10 => 00
   298          * 11 => 10 | 11 => 10
   299          */
   300         glStencilMask( 0x02 );
   301         glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
   302         glDisable( GL_DEPTH_TEST );
   304         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   306         glStencilMask( 0x03 );
   307         glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
   308         glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
   310         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   312         glEnable( GL_DEPTH_TEST );
   313         glStencilMask( 0x01 );
   314         glStencilFunc( GL_ALWAYS, 0, 1 );
   315         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );         
   316     }
   317 }
   319 static void gl_render_bkgnd( struct polygon_struct *poly )
   320 {
   321     bind_texture(poly->tex_id);
   322     render_set_tsp_context( poly->context[0], poly->context[1] );
   323     glDisable( GL_DEPTH_TEST );
   324     glBlendFunc( GL_ONE, GL_ZERO );
   325     gl_draw_vertexes(poly);
   326     glEnable( GL_DEPTH_TEST );
   327 }
   329 void gl_render_triangle( struct polygon_struct *poly, int index )
   330 {
   331     bind_texture(poly->tex_id);
   332     render_set_tsp_context( poly->context[0], poly->context[1] );
   333     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index + index, 3 );
   335 }
   337 void gl_render_tilelist( pvraddr_t tile_entry, gboolean set_depth )
   338 {
   339     tileentryiter list;
   341     FOREACH_TILEENTRY(list, tile_entry) {
   342         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   343         if( poly != NULL ) {
   344             do {
   345                 gl_render_poly(poly, set_depth);
   346                 poly = poly->next;
   347             } while( list.strip_count-- > 0 );
   348         }
   349     }
   350 }
   352 /**
   353  * Render the tilelist with depthbuffer updates only.
   354  */
   355 static void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
   356 {
   357     tileentryiter list;
   359     FOREACH_TILEENTRY(list, tile_entry) {
   360         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   361         if( poly != NULL ) {
   362             do {
   363                 render_set_base_context(poly->context[0],TRUE);
   364                 gl_draw_vertexes(poly);
   365                 poly = poly->next;
   366             } while( list.strip_count-- > 0 );
   367         }
   368     }
   369 }
   371 static void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
   372 {
   373     tileentryiter list;
   375     FOREACH_TILEENTRY(list, tile_entry ) {
   376         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   377         if( poly != NULL ) {
   378             do {
   379                 gl_render_modifier_polygon( poly, tile_bounds );
   380                 poly = poly->next;
   381             } while( list.strip_count-- > 0 );
   382         }
   383     }
   384 }
   386 /**
   387  * Define an orthographic projection matrix
   388  * Note: row-major order
   389  */
   390 static void setOrtho( GLfloat *matrix, GLfloat width, GLfloat height, GLfloat znear, GLfloat zfar )
   391 {
   392     matrix[0] =  2/width;
   393     matrix[1] =  0;
   394     matrix[2] =  0;
   395     matrix[3] =  0;
   397     matrix[4] =  0;
   398     matrix[5] = -2/height;
   399     matrix[6] =  0;
   400     matrix[7] =  0;
   402     matrix[8] =  0;
   403     matrix[9] =  0;
   404     matrix[10]= -2/(zfar-znear);
   405     matrix[11]=  0;
   407     matrix[12]= -1;
   408     matrix[13]=  1;
   409     matrix[14]= -(zfar+znear)/(zfar-znear);
   410     matrix[15]=  1;
   411 }
   413 #ifdef HAVE_OPENGL_FIXEDFUNC
   414 void pvr2_scene_setup_fixed( GLfloat *viewMatrix )
   415 {
   416     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
   418     glMatrixMode(GL_MODELVIEW);
   419     glLoadIdentity();
   420     glMatrixMode(GL_PROJECTION);
   421     glLoadIdentity();
   422     glLoadMatrixf(viewMatrix);
   424     glEnable( GL_FOG );
   425     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
   426     glFogi(GL_FOG_MODE, GL_LINEAR);
   427     glFogf(GL_FOG_START, 0.0);
   428     glFogf(GL_FOG_END, 1.0);
   430     glEnable( GL_ALPHA_TEST );
   431     glAlphaFunc( GL_GEQUAL, 0 );
   433     glEnable( GL_COLOR_SUM );
   435     glEnableClientState( GL_VERTEX_ARRAY );
   436     glEnableClientState( GL_COLOR_ARRAY );
   437     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
   438     glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
   439     glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
   441     /* Vertex array pointers */
   442     glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
   443     glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
   444     glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
   445     glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
   446     glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
   447 }
   449 void pvr2_scene_set_alpha_fixed( float alphaRef )
   450 {
   451     glAlphaFunc( GL_GEQUAL, alphaRef );
   452 }
   454 void pvr2_scene_cleanup_fixed()
   455 {
   456     glDisable( GL_COLOR_SUM );
   457     glDisable( GL_FOG );
   458     glDisable( GL_ALPHA_TEST );
   460     glDisableClientState( GL_VERTEX_ARRAY );
   461     glDisableClientState( GL_COLOR_ARRAY );
   462     glDisableClientState( GL_TEXTURE_COORD_ARRAY );
   463     glDisableClientState( GL_SECONDARY_COLOR_ARRAY );
   464     glDisableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
   465 }
   466 #else
   467 void pvr2_scene_setup_fixed( GLfloat *viewMatrix, float alphaRef )
   468 {
   469 }
   470 void pvr2_scene_set_alpha_fixed( float alphaRef )
   471 {
   472 }
   473 void pvr2_scene_cleanup_fixed()
   474 {
   475 }
   476 #endif
   478 void pvr2_scene_setup_shader( GLfloat *viewMatrix )
   479 {
   480     glsl_use_pvr2_shader();
   481     glsl_set_pvr2_shader_view_matrix(viewMatrix);
   482     glsl_set_pvr2_shader_fog_colour1(pvr2_scene.fog_vert_colour);
   483     glsl_set_pvr2_shader_fog_colour2(pvr2_scene.fog_lut_colour);
   484     glsl_set_pvr2_shader_in_vertex_vec3_pointer(&pvr2_scene.vertex_array[0].x, sizeof(struct vertex_struct));
   485     glsl_set_pvr2_shader_in_colour_pointer(&pvr2_scene.vertex_array[0].rgba[0], sizeof(struct vertex_struct));
   486     glsl_set_pvr2_shader_in_colour2_pointer(&pvr2_scene.vertex_array[0].offset_rgba[0], sizeof(struct vertex_struct));
   487     glsl_set_pvr2_shader_in_texcoord_pointer(&pvr2_scene.vertex_array[0].u, sizeof(struct vertex_struct));
   488     glsl_set_pvr2_shader_alpha_ref(0.0);
   489     glsl_set_pvr2_shader_primary_texture(0);
   490     glsl_set_pvr2_shader_palette_texture(1);
   491 }
   493 void pvr2_scene_cleanup_shader( )
   494 {
   495     glsl_clear_shader();
   496 }
   498 void pvr2_scene_set_alpha_shader( float alphaRef )
   499 {
   500     glsl_set_pvr2_shader_alpha_ref(alphaRef);
   501 }
   503 /**
   504  * Render the currently defined scene in pvr2_scene
   505  */
   506 void pvr2_scene_render( render_buffer_t buffer )
   507 {
   508     /* Scene setup */
   509     struct timeval start_tv, tex_tv, end_tv;
   510     int i;
   511     GLfloat viewMatrix[16];
   512     uint32_t clip_bounds[4];
   515     gettimeofday(&start_tv, NULL);
   516     display_driver->set_render_target(buffer);
   517     pvr2_check_palette_changed();
   518     pvr2_scene_load_textures();
   519     currentTexId = -1;
   521     gettimeofday( &tex_tv, NULL );
   522     uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
   523     (tex_tv.tv_usec - start_tv.tv_usec)/1000;
   524     DEBUG( "Texture load in %dms", ms );
   526     float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
   527     float nearz = pvr2_scene.bounds[4];
   528     float farz = pvr2_scene.bounds[5];
   529     if( nearz == farz ) {
   530         farz*= 4.0;
   531     }
   533     /* Generate integer clip boundaries */
   534     for( i=0; i<4; i++ ) {
   535         clip_bounds[i] = (uint32_t)pvr2_scene.bounds[i];
   536     }
   538     setOrtho(viewMatrix, pvr2_scene.buffer_width, pvr2_scene.buffer_height, -farz, -nearz);
   540     if( have_shaders ) {
   541         pvr2_scene_setup_shader(viewMatrix);
   542     } else {
   543         pvr2_scene_setup_fixed(viewMatrix);
   544     }
   547     /* Clear the buffer (FIXME: May not want always want to do this) */
   548     glDisable( GL_SCISSOR_TEST );
   549     glDepthMask( GL_TRUE );
   550     glStencilMask( 0x03 );
   551     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
   553     /* Render the background */
   554     gl_render_bkgnd( pvr2_scene.bkgnd_poly );
   556     glEnable( GL_SCISSOR_TEST );
   557     glEnable( GL_TEXTURE_2D );
   559     struct tile_segment *segment;
   561 #define FOREACH_SEGMENT(segment) \
   562     segment = pvr2_scene.segment_list; \
   563     do { \
   564         int tilex = SEGMENT_X(segment->control); \
   565         int tiley = SEGMENT_Y(segment->control); \
   566         \
   567         uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 }; \
   568         if( !clip_tile_bounds(tile_bounds, clip_bounds) ) { \
   569             continue; \
   570         }
   571 #define END_FOREACH_SEGMENT() \
   572     } while( !IS_LAST_SEGMENT(segment++) );
   573 #define CLIP_TO_SEGMENT() \
   574     glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] )
   576     /* Build up the opaque stencil map */
   577     if( display_driver->capabilities.stencil_bits >= 2 ) {
   578         glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
   579         FOREACH_SEGMENT(segment)
   580             if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
   581                 CLIP_TO_SEGMENT();
   582                 gl_render_tilelist_depthonly(segment->opaque_ptr);
   583             }
   584         END_FOREACH_SEGMENT()
   586         glEnable( GL_STENCIL_TEST );
   587         glStencilFunc( GL_ALWAYS, 0, 1 );
   588         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
   589         glStencilMask( 0x01 );
   590         glDepthFunc( GL_LEQUAL );
   591         glDepthMask( GL_FALSE );
   592         glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
   593         FOREACH_SEGMENT(segment)
   594             if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
   595                 CLIP_TO_SEGMENT();
   596                 gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
   597             }
   598         END_FOREACH_SEGMENT()
   599         glDepthMask( GL_TRUE );
   600         glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
   601         glDisable( GL_SCISSOR_TEST );
   602         glClear( GL_DEPTH_BUFFER_BIT );
   603         glEnable( GL_SCISSOR_TEST );
   604         glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
   605     }
   607     /* Render the opaque polygons */
   608     FOREACH_SEGMENT(segment)
   609         CLIP_TO_SEGMENT();
   610         gl_render_tilelist(segment->opaque_ptr,TRUE);
   611     END_FOREACH_SEGMENT()
   613     /* Render the punch-out polygons */
   614     if( have_shaders )
   615         pvr2_scene_set_alpha_shader(alphaRef);
   616     else
   617         pvr2_scene_set_alpha_fixed(alphaRef);
   618     glDepthFunc(GL_GEQUAL);
   619     FOREACH_SEGMENT(segment)
   620         CLIP_TO_SEGMENT();
   621         gl_render_tilelist(segment->punchout_ptr, FALSE );
   622     END_FOREACH_SEGMENT()
   623     if( have_shaders )
   624         pvr2_scene_set_alpha_shader(0.0);
   625     else
   626         pvr2_scene_set_alpha_fixed(0.0);
   628     /* Render the translucent polygons */
   629     FOREACH_SEGMENT(segment)
   630         if( IS_NONEMPTY_TILE_LIST(segment->trans_ptr) ) {
   631             CLIP_TO_SEGMENT();
   632             if( pvr2_scene.sort_mode == SORT_NEVER || 
   633                     (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
   634                 gl_render_tilelist(segment->trans_ptr, TRUE);
   635             } else {
   636                 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
   637             }
   638         }
   639     END_FOREACH_SEGMENT()
   641     glDisable( GL_SCISSOR_TEST );
   643     if( have_shaders ) {
   644         pvr2_scene_cleanup_shader();
   645     } else {
   646         pvr2_scene_cleanup_fixed();
   647     }
   649     pvr2_scene_finished();
   651     gettimeofday( &end_tv, NULL );
   652     ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
   653     (end_tv.tv_usec - tex_tv.tv_usec)/1000;
   654     DEBUG( "Scene render in %dms", ms );
   655 }
.