Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 1236:d93175c36387
prev1233:06923d1020de
next1238:91bcb2dec4ef
author nkeynes
date Fri Feb 24 21:17:47 2012 +1000 (9 years ago)
permissions -rw-r--r--
last change Factor video_width/video_height out into video_gl.c
Convert immediate-mode bits in video_gl.c into a structure for glDrawArray
Move setOrtho into defineOrthoMatrix in glutil.c
Rearrange various GL settings to keep a consistent state
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
   128     /* Global settings */
   129     glDisable( GL_CULL_FACE );
   130     glEnable( GL_BLEND );
   131     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   133 #ifdef HAVE_OPENGL_CLAMP_COLOR
   134     if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
   135         glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
   136         glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
   137     }
   138 #endif
   140 #ifdef HAVE_OPENGL_FIXEDFUNC
   141     /* Setup defaults for perspective correction + matrices */
   142     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
   143     glMatrixMode(GL_MODELVIEW);
   144     glLoadIdentity();
   145     glMatrixMode(GL_PROJECTION);
   146 #endif
   149 #ifdef HAVE_OPENGL_CLEAR_DEPTHF
   150     glClearDepthf(0);
   151 #else
   152     glClearDepth(0);
   153 #endif
   154     glClearStencil(0);
   155 }
   157 /**
   158  * Setup the basic context that's shared between normal and modified modes -
   159  * depth, culling
   160  */
   161 static void render_set_base_context( uint32_t poly1, gboolean set_depth )
   162 {
   163     if( set_depth ) {
   164         glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   165     }
   167     glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
   168 }
   170 /**
   171  * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
   172  */
   173 static void render_set_tsp_context( uint32_t poly1, uint32_t poly2 )
   174 {
   175     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   177 #ifdef HAVE_OPENGL_FIXEDFUNC
   178     if( !have_shaders ) {
   179         if( POLY1_TEXTURED(poly1) ) {
   180             if( POLY2_TEX_BLEND(poly2) == 2 )
   181                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
   182             else
   183                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
   185          }
   187          switch( POLY2_FOG_MODE(poly2) ) {
   188          case PVR2_POLY_FOG_LOOKUP:
   189              glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
   190              break;
   191          case PVR2_POLY_FOG_VERTEX:
   192              glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
   193              break;
   194          }
   195      }
   196 #endif
   198      int srcblend = POLY2_SRC_BLEND(poly2);
   199      int destblend = POLY2_DEST_BLEND(poly2);
   200      glBlendFunc( srcblend, destblend );
   202      if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
   203          WARN( "Accumulation buffer not supported" );
   204      }   
   205 }
   207 /**
   208  * Setup the GL context for the supplied polygon context.
   209  * @param context pointer to 3 or 5 words of polygon context
   210  * @param depth_mode force depth mode, or 0 to use the polygon's
   211  * depth mode.
   212  */
   213 static void render_set_context( uint32_t *context, gboolean set_depth )
   214 {
   215     render_set_base_context(context[0], set_depth);
   216     render_set_tsp_context(context[0],context[1]);
   217 }
   219 static inline void gl_draw_vertexes( struct polygon_struct *poly )
   220 {
   221     do {
   222         glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
   223         poly = poly->sub_next;
   224     } while( poly != NULL );
   225 }
   227 static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
   228 {
   229     do {
   230         glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
   231         poly = poly->sub_next;
   232     } while( poly != NULL );
   233 }
   235 static void gl_render_poly( struct polygon_struct *poly, gboolean set_depth)
   236 {
   237     if( poly->vertex_count == 0 )
   238         return; /* Culled */
   240     bind_texture(poly->tex_id);
   241     if( poly->mod_vertex_index == -1 ) {
   242         render_set_context( poly->context, set_depth );
   243         gl_draw_vertexes(poly);
   244     }  else {
   245         glEnable( GL_STENCIL_TEST );
   246         render_set_base_context( poly->context[0], set_depth );
   247         render_set_tsp_context( poly->context[0], poly->context[1] );
   248         glStencilFunc(GL_EQUAL, 0, 2);
   249         gl_draw_vertexes(poly);
   251         if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
   252             bind_texture(poly->mod_tex_id);
   253             render_set_tsp_context( poly->context[0], poly->context[3] );
   254         }
   255         glStencilFunc(GL_EQUAL, 2, 2);
   256         gl_draw_mod_vertexes(poly);
   257         glDisable( GL_STENCIL_TEST );
   258     }
   259 }
   262 static void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
   263 {
   264     /* A bit of explanation:
   265      * In theory it works like this: generate a 1-bit stencil for each polygon
   266      * volume, and then AND or OR it against the overall 1-bit tile stencil at 
   267      * the end of the volume. 
   268      * 
   269      * The implementation here uses a 2-bit stencil buffer, where each volume
   270      * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
   271      * to update bit 1 accordingly and clear bit 0.
   272      * 
   273      * This could probably be more efficient, but at least it works correctly 
   274      * now :)
   275      */
   277     if( poly->vertex_count == 0 )
   278         return; /* Culled */
   280     gl_draw_vertexes(poly);
   284     int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
   285     if( poly_type == PVR2_VOLUME_REGION0 ) {
   286         /* 00 => 00
   287          * 01 => 00
   288          * 10 => 10
   289          * 11 => 00
   290          */
   291         glStencilMask( 0x03 );
   292         glStencilFunc(GL_EQUAL, 0x02, 0x03);
   293         glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
   294         glDisable( GL_DEPTH_TEST );
   296         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   298         glEnable( GL_DEPTH_TEST );
   299         glStencilMask( 0x01 );
   300         glStencilFunc( GL_ALWAYS, 0, 1 );
   301         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
   302     } else if( poly_type == PVR2_VOLUME_REGION1 ) {
   303         /* This is harder with the standard stencil ops - do it in two passes
   304          * 00 => 00 | 00 => 10
   305          * 01 => 10 | 01 => 10
   306          * 10 => 10 | 10 => 00
   307          * 11 => 10 | 11 => 10
   308          */
   309         glStencilMask( 0x02 );
   310         glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
   311         glDisable( GL_DEPTH_TEST );
   313         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   315         glStencilMask( 0x03 );
   316         glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
   317         glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
   319         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   321         glEnable( GL_DEPTH_TEST );
   322         glStencilMask( 0x01 );
   323         glStencilFunc( GL_ALWAYS, 0, 1 );
   324         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );         
   325     }
   326 }
   328 static void gl_render_bkgnd( struct polygon_struct *poly )
   329 {
   330     bind_texture(poly->tex_id);
   331     render_set_tsp_context( poly->context[0], poly->context[1] );
   332     glDisable( GL_DEPTH_TEST );
   333     glBlendFunc( GL_ONE, GL_ZERO );
   334     gl_draw_vertexes(poly);
   335     glEnable( GL_DEPTH_TEST );
   336 }
   338 void gl_render_triangle( struct polygon_struct *poly, int index )
   339 {
   340     bind_texture(poly->tex_id);
   341     render_set_tsp_context( poly->context[0], poly->context[1] );
   342     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index + index, 3 );
   344 }
   346 void gl_render_tilelist( pvraddr_t tile_entry, gboolean set_depth )
   347 {
   348     tileentryiter list;
   350     FOREACH_TILEENTRY(list, tile_entry) {
   351         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   352         if( poly != NULL ) {
   353             do {
   354                 gl_render_poly(poly, set_depth);
   355                 poly = poly->next;
   356             } while( list.strip_count-- > 0 );
   357         }
   358     }
   359 }
   361 /**
   362  * Render the tilelist with depthbuffer updates only.
   363  */
   364 static void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
   365 {
   366     tileentryiter list;
   368     FOREACH_TILEENTRY(list, tile_entry) {
   369         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   370         if( poly != NULL ) {
   371             do {
   372                 render_set_base_context(poly->context[0],TRUE);
   373                 gl_draw_vertexes(poly);
   374                 poly = poly->next;
   375             } while( list.strip_count-- > 0 );
   376         }
   377     }
   378 }
   380 static void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
   381 {
   382     tileentryiter list;
   384     FOREACH_TILEENTRY(list, tile_entry ) {
   385         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   386         if( poly != NULL ) {
   387             do {
   388                 gl_render_modifier_polygon( poly, tile_bounds );
   389                 poly = poly->next;
   390             } while( list.strip_count-- > 0 );
   391         }
   392     }
   393 }
   396 #ifdef HAVE_OPENGL_FIXEDFUNC
   397 void pvr2_scene_setup_fixed( GLfloat *viewMatrix )
   398 {
   399     glLoadMatrixf(viewMatrix);
   400     glEnable( GL_DEPTH_TEST );
   402     glEnable( GL_FOG );
   403     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
   404     glFogi(GL_FOG_MODE, GL_LINEAR);
   405     glFogf(GL_FOG_START, 0.0);
   406     glFogf(GL_FOG_END, 1.0);
   408     glEnable( GL_ALPHA_TEST );
   409     glAlphaFunc( GL_GEQUAL, 0 );
   411     glEnable( GL_COLOR_SUM );
   413     glEnableClientState( GL_VERTEX_ARRAY );
   414     glEnableClientState( GL_COLOR_ARRAY );
   415     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
   416     glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
   417     glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
   419     /* Vertex array pointers */
   420     glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
   421     glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
   422     glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
   423     glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
   424     glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
   425 }
   427 void pvr2_scene_set_alpha_fixed( float alphaRef )
   428 {
   429     glAlphaFunc( GL_GEQUAL, alphaRef );
   430 }
   432 void pvr2_scene_cleanup_fixed()
   433 {
   434     glDisable( GL_COLOR_SUM );
   435     glDisable( GL_FOG );
   436     glDisable( GL_ALPHA_TEST );
   437     glDisable( GL_DEPTH_TEST );
   439     glDisableClientState( GL_VERTEX_ARRAY );
   440     glDisableClientState( GL_COLOR_ARRAY );
   441     glDisableClientState( GL_TEXTURE_COORD_ARRAY );
   442     glDisableClientState( GL_SECONDARY_COLOR_ARRAY );
   443     glDisableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
   445 }
   446 #else
   447 void pvr2_scene_setup_fixed( GLfloat *viewMatrix, float alphaRef )
   448 {
   449 }
   450 void pvr2_scene_set_alpha_fixed( float alphaRef )
   451 {
   452 }
   453 void pvr2_scene_cleanup_fixed()
   454 {
   455 }
   456 #endif
   458 void pvr2_scene_setup_shader( GLfloat *viewMatrix )
   459 {
   460     glEnable( GL_DEPTH_TEST );
   462     glsl_use_pvr2_shader();
   463     glsl_set_pvr2_shader_view_matrix(viewMatrix);
   464     glsl_set_pvr2_shader_fog_colour1(pvr2_scene.fog_vert_colour);
   465     glsl_set_pvr2_shader_fog_colour2(pvr2_scene.fog_lut_colour);
   466     glsl_set_pvr2_shader_in_vertex_vec3_pointer(&pvr2_scene.vertex_array[0].x, sizeof(struct vertex_struct));
   467     glsl_set_pvr2_shader_in_colour_pointer(&pvr2_scene.vertex_array[0].rgba[0], sizeof(struct vertex_struct));
   468     glsl_set_pvr2_shader_in_colour2_pointer(&pvr2_scene.vertex_array[0].offset_rgba[0], sizeof(struct vertex_struct));
   469     glsl_set_pvr2_shader_in_texcoord_pointer(&pvr2_scene.vertex_array[0].u, sizeof(struct vertex_struct));
   470     glsl_set_pvr2_shader_alpha_ref(0.0);
   471     glsl_set_pvr2_shader_primary_texture(0);
   472     glsl_set_pvr2_shader_palette_texture(1);
   473 }
   475 void pvr2_scene_cleanup_shader( )
   476 {
   477     glsl_clear_shader();
   479     glDisable( GL_DEPTH_TEST );
   480 }
   482 void pvr2_scene_set_alpha_shader( float alphaRef )
   483 {
   484     glsl_set_pvr2_shader_alpha_ref(alphaRef);
   485 }
   487 /**
   488  * Render the currently defined scene in pvr2_scene
   489  */
   490 void pvr2_scene_render( render_buffer_t buffer )
   491 {
   492     /* Scene setup */
   493     struct timeval start_tv, tex_tv, end_tv;
   494     int i;
   495     GLfloat viewMatrix[16];
   496     uint32_t clip_bounds[4];
   499     gettimeofday(&start_tv, NULL);
   500     display_driver->set_render_target(buffer);
   501     pvr2_check_palette_changed();
   502     pvr2_scene_load_textures();
   503     currentTexId = -1;
   505     gettimeofday( &tex_tv, NULL );
   506     uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
   507     (tex_tv.tv_usec - start_tv.tv_usec)/1000;
   508     DEBUG( "Texture load in %dms", ms );
   510     float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
   511     float nearz = pvr2_scene.bounds[4];
   512     float farz = pvr2_scene.bounds[5];
   513     if( nearz == farz ) {
   514         farz*= 4.0;
   515     }
   517     /* Generate integer clip boundaries */
   518     for( i=0; i<4; i++ ) {
   519         clip_bounds[i] = (uint32_t)pvr2_scene.bounds[i];
   520     }
   522     defineOrthoMatrix(viewMatrix, pvr2_scene.buffer_width, pvr2_scene.buffer_height, -farz, -nearz);
   524     if( have_shaders ) {
   525         pvr2_scene_setup_shader(viewMatrix);
   526     } else {
   527         pvr2_scene_setup_fixed(viewMatrix);
   528     }
   531     /* Clear the buffer (FIXME: May not want always want to do this) */
   532     glDisable( GL_SCISSOR_TEST );
   533     glDepthMask( GL_TRUE );
   534     glStencilMask( 0x03 );
   535     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
   537     /* Render the background */
   538     gl_render_bkgnd( pvr2_scene.bkgnd_poly );
   540     glEnable( GL_SCISSOR_TEST );
   541     glEnable( GL_TEXTURE_2D );
   543     struct tile_segment *segment;
   545 #define FOREACH_SEGMENT(segment) \
   546     segment = pvr2_scene.segment_list; \
   547     do { \
   548         int tilex = SEGMENT_X(segment->control); \
   549         int tiley = SEGMENT_Y(segment->control); \
   550         \
   551         uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 }; \
   552         if( !clip_tile_bounds(tile_bounds, clip_bounds) ) { \
   553             continue; \
   554         }
   555 #define END_FOREACH_SEGMENT() \
   556     } while( !IS_LAST_SEGMENT(segment++) );
   557 #define CLIP_TO_SEGMENT() \
   558     glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] )
   560     /* Build up the opaque stencil map */
   561     if( display_driver->capabilities.stencil_bits >= 2 ) {
   562         glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
   563         FOREACH_SEGMENT(segment)
   564             if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
   565                 CLIP_TO_SEGMENT();
   566                 gl_render_tilelist_depthonly(segment->opaque_ptr);
   567             }
   568         END_FOREACH_SEGMENT()
   570         glEnable( GL_STENCIL_TEST );
   571         glStencilFunc( GL_ALWAYS, 0, 1 );
   572         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
   573         glStencilMask( 0x01 );
   574         glDepthFunc( GL_LEQUAL );
   575         glDepthMask( GL_FALSE );
   576         FOREACH_SEGMENT(segment)
   577             if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
   578                 CLIP_TO_SEGMENT();
   579                 gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
   580             }
   581         END_FOREACH_SEGMENT()
   582         glDepthMask( GL_TRUE );
   583         glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
   584         glDisable( GL_SCISSOR_TEST );
   585         glClear( GL_DEPTH_BUFFER_BIT );
   586         glEnable( GL_SCISSOR_TEST );
   587         glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
   588     }
   590     /* Render the opaque polygons */
   591     FOREACH_SEGMENT(segment)
   592         CLIP_TO_SEGMENT();
   593         gl_render_tilelist(segment->opaque_ptr,TRUE);
   594     END_FOREACH_SEGMENT()
   595     glDisable( GL_STENCIL_TEST );
   597     /* Render the punch-out polygons */
   598     if( have_shaders )
   599         pvr2_scene_set_alpha_shader(alphaRef);
   600     else
   601         pvr2_scene_set_alpha_fixed(alphaRef);
   602     glDepthFunc(GL_GEQUAL);
   603     FOREACH_SEGMENT(segment)
   604         CLIP_TO_SEGMENT();
   605         gl_render_tilelist(segment->punchout_ptr, FALSE );
   606     END_FOREACH_SEGMENT()
   607     if( have_shaders )
   608         pvr2_scene_set_alpha_shader(0.0);
   609     else
   610         pvr2_scene_set_alpha_fixed(0.0);
   612     /* Render the translucent polygons */
   613     FOREACH_SEGMENT(segment)
   614         if( IS_NONEMPTY_TILE_LIST(segment->trans_ptr) ) {
   615             CLIP_TO_SEGMENT();
   616             if( pvr2_scene.sort_mode == SORT_NEVER || 
   617                     (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
   618                 gl_render_tilelist(segment->trans_ptr, TRUE);
   619             } else {
   620                 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
   621             }
   622         }
   623     END_FOREACH_SEGMENT()
   625     glDisable( GL_SCISSOR_TEST );
   627     if( have_shaders ) {
   628         pvr2_scene_cleanup_shader();
   629     } else {
   630         pvr2_scene_cleanup_fixed();
   631     }
   633     pvr2_scene_finished();
   635     gettimeofday( &end_tv, NULL );
   636     ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
   637     (end_tv.tv_usec - tex_tv.tv_usec)/1000;
   638     DEBUG( "Scene render in %dms", ms );
   639 }
.