Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 1298:d0eb2307b847
prev1280:38f2b0e60261
author nkeynes
date Wed Feb 04 08:38:23 2015 +1000 (5 years ago)
permissions -rw-r--r--
last change Fix assorted compile warnings reported by Clang
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     /* FIXME: Find a non-fixed-func way to do this */
    77 #ifdef HAVE_OPENGL_FIXEDFUNC
    78     glBegin( GL_TRIANGLE_STRIP );
    79     glVertex3f( tile_bounds[0], tile_bounds[2], z );
    80     glVertex3f( tile_bounds[1], tile_bounds[2], z );
    81     glVertex3f( tile_bounds[0], tile_bounds[3], z );
    82     glVertex3f( tile_bounds[1], tile_bounds[3], z );
    83     glEnd();
    84 #endif
    85 }
    87 static void pvr2_scene_load_textures()
    88 {
    89     int i;
    91     texcache_begin_scene( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03,
    92                          (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5 );
    94     for( i=0; i < pvr2_scene.poly_count; i++ ) {
    95         struct polygon_struct *poly = &pvr2_scene.poly_array[i];
    96         if( POLY1_TEXTURED(poly->context[0]) ) {
    97             poly->tex_id = texcache_get_texture( poly->context[1], poly->context[2] );
    98             if( poly->mod_vertex_index != -1 ) {
    99                 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
   100                     poly->mod_tex_id = texcache_get_texture( poly->context[3], poly->context[4] );
   101                 } else {
   102                     poly->mod_tex_id = poly->tex_id;
   103                 }
   104             }
   105         } else {
   106             poly->tex_id = 0;
   107             poly->mod_tex_id = 0;
   108         }
   109     }
   110 }
   113 /**
   114  * Once-off call to setup the OpenGL context.
   115  */
   116 void pvr2_setup_gl_context()
   117 {
   118     have_shaders = display_driver->capabilities.has_sl;
   119 #ifdef APPLE_BUILD
   120     CGL_MACRO_CONTEXT = CGLGetCurrentContext();
   121 #endif
   122     texcache_gl_init(); // Allocate texture IDs
   124     /* Global settings */
   125     glDisable( GL_CULL_FACE );
   126     glEnable( GL_BLEND );
   127     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   129 #ifdef HAVE_OPENGL_CLAMP_COLOR
   130     if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
   131         glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
   132         glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
   133     }
   134 #endif
   136 #ifdef HAVE_OPENGL_FIXEDFUNC
   137     /* Setup defaults for perspective correction + matrices */
   138     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
   139     glMatrixMode(GL_MODELVIEW);
   140     glLoadIdentity();
   141     glMatrixMode(GL_PROJECTION);
   142 #endif
   145 #ifdef HAVE_GLES2
   146     glClearDepthf(0);
   147 #else
   148     glClearDepth(0);
   149 #endif
   150     glClearStencil(0);
   151 }
   153 void pvr2_shutdown_gl_context()
   154 {
   155     texcache_gl_shutdown();
   156     pvr2_destroy_render_buffers();
   157 }
   159 /**
   160  * Setup the basic context that's shared between normal and modified modes -
   161  * depth, culling
   162  */
   163 static void render_set_base_context( uint32_t poly1, gboolean set_depth )
   164 {
   165     if( set_depth ) {
   166         glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   167     }
   169     glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
   170 }
   172 /**
   173  * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
   174  */
   175 static void render_set_tsp_context( uint32_t poly1, uint32_t poly2 )
   176 {
   177 #ifdef HAVE_OPENGL_FIXEDFUNC
   178     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   180     if( !have_shaders ) {
   181         if( POLY1_TEXTURED(poly1) ) {
   182             if( POLY2_TEX_BLEND(poly2) == 2 )
   183                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
   184             else
   185                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
   187          }
   189          switch( POLY2_FOG_MODE(poly2) ) {
   190          case PVR2_POLY_FOG_LOOKUP:
   191              glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
   192              break;
   193          case PVR2_POLY_FOG_VERTEX:
   194              glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
   195              break;
   196          }
   197      }
   198 #endif
   200      int srcblend = POLY2_SRC_BLEND(poly2);
   201      int destblend = POLY2_DEST_BLEND(poly2);
   202      glBlendFunc( srcblend, destblend );
   204      if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
   205          WARN( "Accumulation buffer not supported" );
   206      }   
   207 }
   209 /**
   210  * Setup the GL context for the supplied polygon context.
   211  * @param context pointer to 3 or 5 words of polygon context
   212  * @param depth_mode force depth mode, or 0 to use the polygon's
   213  * depth mode.
   214  */
   215 static void render_set_context( uint32_t *context, gboolean set_depth )
   216 {
   217     render_set_base_context(context[0], set_depth);
   218     render_set_tsp_context(context[0],context[1]);
   219 }
   221 static inline void gl_draw_vertexes( struct polygon_struct *poly )
   222 {
   223     do {
   224         glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
   225         poly = poly->sub_next;
   226     } while( poly != NULL );
   227 }
   229 static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
   230 {
   231     do {
   232         glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
   233         poly = poly->sub_next;
   234     } while( poly != NULL );
   235 }
   237 static void gl_render_poly( struct polygon_struct *poly, gboolean set_depth)
   238 {
   239     if( poly->vertex_count == 0 )
   240         return; /* Culled */
   242     bind_texture(poly->tex_id);
   243     if( poly->mod_vertex_index == -1 ) {
   244         render_set_context( poly->context, set_depth );
   245         gl_draw_vertexes(poly);
   246     }  else {
   247         glEnable( GL_STENCIL_TEST );
   248         render_set_base_context( poly->context[0], set_depth );
   249         render_set_tsp_context( poly->context[0], poly->context[1] );
   250         glStencilFunc(GL_EQUAL, 0, 2);
   251         gl_draw_vertexes(poly);
   253         if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
   254             bind_texture(poly->mod_tex_id);
   255             render_set_tsp_context( poly->context[0], poly->context[3] );
   256         }
   257         glStencilFunc(GL_EQUAL, 2, 2);
   258         gl_draw_mod_vertexes(poly);
   259         glDisable( GL_STENCIL_TEST );
   260     }
   261 }
   264 static void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
   265 {
   266     /* A bit of explanation:
   267      * In theory it works like this: generate a 1-bit stencil for each polygon
   268      * volume, and then AND or OR it against the overall 1-bit tile stencil at 
   269      * the end of the volume. 
   270      * 
   271      * The implementation here uses a 2-bit stencil buffer, where each volume
   272      * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
   273      * to update bit 1 accordingly and clear bit 0.
   274      * 
   275      * This could probably be more efficient, but at least it works correctly 
   276      * now :)
   277      */
   279     if( poly->vertex_count == 0 )
   280         return; /* Culled */
   282     gl_draw_vertexes(poly);
   286     int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
   287     if( poly_type == PVR2_VOLUME_REGION0 ) {
   288         /* 00 => 00
   289          * 01 => 00
   290          * 10 => 10
   291          * 11 => 00
   292          */
   293         glStencilMask( 0x03 );
   294         glStencilFunc(GL_EQUAL, 0x02, 0x03);
   295         glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
   296         glDisable( GL_DEPTH_TEST );
   298         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   300         glEnable( GL_DEPTH_TEST );
   301         glStencilMask( 0x01 );
   302         glStencilFunc( GL_ALWAYS, 0, 1 );
   303         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
   304     } else if( poly_type == PVR2_VOLUME_REGION1 ) {
   305         /* This is harder with the standard stencil ops - do it in two passes
   306          * 00 => 00 | 00 => 10
   307          * 01 => 10 | 01 => 10
   308          * 10 => 10 | 10 => 00
   309          * 11 => 10 | 11 => 10
   310          */
   311         glStencilMask( 0x02 );
   312         glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
   313         glDisable( GL_DEPTH_TEST );
   315         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   317         glStencilMask( 0x03 );
   318         glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
   319         glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
   321         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   323         glEnable( GL_DEPTH_TEST );
   324         glStencilMask( 0x01 );
   325         glStencilFunc( GL_ALWAYS, 0, 1 );
   326         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );         
   327     }
   328 }
   330 static void gl_render_bkgnd( struct polygon_struct *poly )
   331 {
   332     bind_texture(poly->tex_id);
   333     render_set_tsp_context( poly->context[0], poly->context[1] );
   334     glDisable( GL_DEPTH_TEST );
   335     glBlendFunc( GL_ONE, GL_ZERO );
   336     gl_draw_vertexes(poly);
   337     glEnable( GL_DEPTH_TEST );
   338 }
   340 void gl_render_triangle( struct polygon_struct *poly, int index )
   341 {
   342     bind_texture(poly->tex_id);
   343     render_set_tsp_context( poly->context[0], poly->context[1] );
   344     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index + index, 3 );
   346 }
   348 void gl_render_tilelist( pvraddr_t tile_entry, gboolean set_depth )
   349 {
   350     tileentryiter list;
   352     FOREACH_TILEENTRY(list, tile_entry) {
   353         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   354         if( poly != NULL ) {
   355             do {
   356                 gl_render_poly(poly, set_depth);
   357                 poly = poly->next;
   358             } while( list.strip_count-- > 0 );
   359         }
   360     }
   361 }
   363 /**
   364  * Render the tilelist with depthbuffer updates only.
   365  */
   366 static void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
   367 {
   368     tileentryiter list;
   370     FOREACH_TILEENTRY(list, tile_entry) {
   371         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   372         if( poly != NULL ) {
   373             do {
   374                 render_set_base_context(poly->context[0],TRUE);
   375                 gl_draw_vertexes(poly);
   376                 poly = poly->next;
   377             } while( list.strip_count-- > 0 );
   378         }
   379     }
   380 }
   382 static void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
   383 {
   384     tileentryiter list;
   386     FOREACH_TILEENTRY(list, tile_entry ) {
   387         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   388         if( poly != NULL ) {
   389             do {
   390                 gl_render_modifier_polygon( poly, tile_bounds );
   391                 poly = poly->next;
   392             } while( list.strip_count-- > 0 );
   393         }
   394     }
   395 }
   398 #ifdef HAVE_OPENGL_FIXEDFUNC
   399 void pvr2_scene_setup_fixed( GLfloat *viewMatrix )
   400 {
   401     glLoadMatrixf(viewMatrix);
   402     glEnable( GL_DEPTH_TEST );
   404     glEnable( GL_FOG );
   405     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
   406     glFogi(GL_FOG_MODE, GL_LINEAR);
   407     glFogf(GL_FOG_START, 0.0);
   408     glFogf(GL_FOG_END, 1.0);
   410     glEnable( GL_ALPHA_TEST );
   411     glAlphaFunc( GL_GEQUAL, 0 );
   413     glEnable( GL_COLOR_SUM );
   415     glEnableClientState( GL_VERTEX_ARRAY );
   416     glEnableClientState( GL_COLOR_ARRAY );
   417     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
   418     glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
   419     glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
   421     /* Vertex array pointers */
   422     glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
   423     glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
   424     glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
   425     glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
   426     glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
   427 }
   429 void pvr2_scene_set_alpha_fixed( float alphaRef )
   430 {
   431     glAlphaFunc( GL_GEQUAL, alphaRef );
   432 }
   434 void pvr2_scene_cleanup_fixed()
   435 {
   436     glDisable( GL_COLOR_SUM );
   437     glDisable( GL_FOG );
   438     glDisable( GL_ALPHA_TEST );
   439     glDisable( GL_DEPTH_TEST );
   441     glDisableClientState( GL_VERTEX_ARRAY );
   442     glDisableClientState( GL_COLOR_ARRAY );
   443     glDisableClientState( GL_TEXTURE_COORD_ARRAY );
   444     glDisableClientState( GL_SECONDARY_COLOR_ARRAY );
   445     glDisableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
   447 }
   448 #else
   449 void pvr2_scene_setup_fixed( GLfloat *viewMatrix )
   450 {
   451 }
   452 void pvr2_scene_set_alpha_fixed( float alphaRef )
   453 {
   454 }
   455 void pvr2_scene_cleanup_fixed()
   456 {
   457 }
   458 #endif
   460 void pvr2_scene_setup_shader( GLfloat *viewMatrix )
   461 {
   462     glEnable( GL_DEPTH_TEST );
   464     glsl_use_pvr2_shader();
   465     glsl_set_pvr2_shader_view_matrix(viewMatrix);
   466     glsl_set_pvr2_shader_fog_colour1(pvr2_scene.fog_vert_colour);
   467     glsl_set_pvr2_shader_fog_colour2(pvr2_scene.fog_lut_colour);
   468     glsl_set_pvr2_shader_in_vertex_vec3_pointer(&pvr2_scene.vertex_array[0].x, sizeof(struct vertex_struct));
   469     glsl_set_pvr2_shader_in_colour_pointer(&pvr2_scene.vertex_array[0].rgba[0], sizeof(struct vertex_struct));
   470     glsl_set_pvr2_shader_in_colour2_pointer(&pvr2_scene.vertex_array[0].offset_rgba[0], sizeof(struct vertex_struct));
   471     glsl_set_pvr2_shader_in_texcoord_pointer(&pvr2_scene.vertex_array[0].u, sizeof(struct vertex_struct));
   472     glsl_set_pvr2_shader_alpha_ref(0.0);
   473     glsl_set_pvr2_shader_primary_texture(0);
   474     glsl_set_pvr2_shader_palette_texture(1);
   475 }
   477 void pvr2_scene_cleanup_shader( )
   478 {
   479     glsl_clear_shader();
   481     glDisable( GL_DEPTH_TEST );
   482 }
   484 void pvr2_scene_set_alpha_shader( float alphaRef )
   485 {
   486     glsl_set_pvr2_shader_alpha_ref(alphaRef);
   487 }
   489 /**
   490  * Render the currently defined scene in pvr2_scene
   491  */
   492 void pvr2_scene_render( render_buffer_t buffer )
   493 {
   494     /* Scene setup */
   495     struct timeval start_tv, tex_tv, end_tv;
   496     int i;
   497     GLfloat viewMatrix[16];
   498     uint32_t clip_bounds[4];
   501     gettimeofday(&start_tv, NULL);
   502     display_driver->set_render_target(buffer);
   503     pvr2_check_palette_changed();
   504     pvr2_scene_load_textures();
   505     currentTexId = -1;
   507     gettimeofday( &tex_tv, NULL );
   508     uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
   509     (tex_tv.tv_usec - start_tv.tv_usec)/1000;
   510     DEBUG( "Texture load in %dms", ms );
   512     float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
   513     float nearz = pvr2_scene.bounds[4];
   514     float farz = pvr2_scene.bounds[5];
   515     if( nearz == farz ) {
   516         farz*= 4.0;
   517     }
   519     /* Generate integer clip boundaries */
   520     for( i=0; i<4; i++ ) {
   521         clip_bounds[i] = (uint32_t)pvr2_scene.bounds[i];
   522     }
   524     defineOrthoMatrix(viewMatrix, pvr2_scene.buffer_width, pvr2_scene.buffer_height, -farz, -nearz);
   526     if( have_shaders ) {
   527         pvr2_scene_setup_shader(viewMatrix);
   528     } else {
   529         pvr2_scene_setup_fixed(viewMatrix);
   530     }
   533     /* Clear the buffer (FIXME: May not want always want to do this) */
   534     glDisable( GL_SCISSOR_TEST );
   535     glDepthMask( GL_TRUE );
   536     glStencilMask( 0x03 );
   537     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
   539     /* Render the background */
   540     gl_render_bkgnd( pvr2_scene.bkgnd_poly );
   542     glEnable( GL_SCISSOR_TEST );
   543     glEnable( GL_TEXTURE_2D );
   545     struct tile_segment *segment;
   547 #define FOREACH_SEGMENT(segment) \
   548     segment = pvr2_scene.segment_list; \
   549     do { \
   550         int tilex = SEGMENT_X(segment->control); \
   551         int tiley = SEGMENT_Y(segment->control); \
   552         \
   553         uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 }; \
   554         if( !clip_tile_bounds(tile_bounds, clip_bounds) ) { \
   555             continue; \
   556         }
   557 #define END_FOREACH_SEGMENT() \
   558     } while( !IS_LAST_SEGMENT(segment++) );
   559 #define CLIP_TO_SEGMENT() \
   560     glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] )
   562     /* Build up the opaque stencil map */
   563     if( display_driver->capabilities.stencil_bits >= 2 ) {
   564         glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
   565         FOREACH_SEGMENT(segment)
   566             if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
   567                 CLIP_TO_SEGMENT();
   568                 gl_render_tilelist_depthonly(segment->opaque_ptr);
   569             }
   570         END_FOREACH_SEGMENT()
   572         glEnable( GL_STENCIL_TEST );
   573         glStencilFunc( GL_ALWAYS, 0, 1 );
   574         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
   575         glStencilMask( 0x01 );
   576         glDepthFunc( GL_LEQUAL );
   577         glDepthMask( GL_FALSE );
   578         FOREACH_SEGMENT(segment)
   579             if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
   580                 CLIP_TO_SEGMENT();
   581                 gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
   582             }
   583         END_FOREACH_SEGMENT()
   584         glDepthMask( GL_TRUE );
   585         glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
   586         glDisable( GL_SCISSOR_TEST );
   587         glClear( GL_DEPTH_BUFFER_BIT );
   588         glEnable( GL_SCISSOR_TEST );
   589         glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
   590     }
   592     /* Render the opaque polygons */
   593     FOREACH_SEGMENT(segment)
   594         CLIP_TO_SEGMENT();
   595         gl_render_tilelist(segment->opaque_ptr,TRUE);
   596     END_FOREACH_SEGMENT()
   597     glDisable( GL_STENCIL_TEST );
   599     /* Render the punch-out polygons */
   600     if( have_shaders )
   601         pvr2_scene_set_alpha_shader(alphaRef);
   602     else
   603         pvr2_scene_set_alpha_fixed(alphaRef);
   604     glDepthFunc(GL_GEQUAL);
   605     FOREACH_SEGMENT(segment)
   606         CLIP_TO_SEGMENT();
   607         gl_render_tilelist(segment->punchout_ptr, FALSE );
   608     END_FOREACH_SEGMENT()
   609     if( have_shaders )
   610         pvr2_scene_set_alpha_shader(0.0);
   611     else
   612         pvr2_scene_set_alpha_fixed(0.0);
   614     /* Render the translucent polygons */
   615     FOREACH_SEGMENT(segment)
   616         if( IS_NONEMPTY_TILE_LIST(segment->trans_ptr) ) {
   617             CLIP_TO_SEGMENT();
   618             if( pvr2_scene.sort_mode == SORT_NEVER || 
   619                     (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
   620                 gl_render_tilelist(segment->trans_ptr, TRUE);
   621             } else {
   622                 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
   623             }
   624         }
   625     END_FOREACH_SEGMENT()
   627     glDisable( GL_SCISSOR_TEST );
   629     if( have_shaders ) {
   630         pvr2_scene_cleanup_shader();
   631     } else {
   632         pvr2_scene_cleanup_fixed();
   633     }
   635     pvr2_scene_finished();
   637     gettimeofday( &end_tv, NULL );
   638     ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
   639     (end_tv.tv_usec - tex_tv.tv_usec)/1000;
   640     DEBUG( "Scene render in %dms", ms );
   641 }
.