Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 1257:e1314ad3e7cc
prev1256:a9d29fe74bf3
next1275:83b15705cdde
author nkeynes
date Sun Mar 04 21:28:48 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Fix have_shaders in glrender.c
Set negative fog for lut when using shaders
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_OPENGL_CLEAR_DEPTHF
   146     glClearDepthf(0);
   147 #else
   148     glClearDepth(0);
   149 #endif
   150     glClearStencil(0);
   151 }
   153 /**
   154  * Setup the basic context that's shared between normal and modified modes -
   155  * depth, culling
   156  */
   157 static void render_set_base_context( uint32_t poly1, gboolean set_depth )
   158 {
   159     if( set_depth ) {
   160         glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   161     }
   163     glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
   164 }
   166 /**
   167  * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
   168  */
   169 static void render_set_tsp_context( uint32_t poly1, uint32_t poly2 )
   170 {
   171 #ifdef HAVE_OPENGL_FIXEDFUNC
   172     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   174     if( !have_shaders ) {
   175         if( POLY1_TEXTURED(poly1) ) {
   176             if( POLY2_TEX_BLEND(poly2) == 2 )
   177                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
   178             else
   179                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
   181          }
   183          switch( POLY2_FOG_MODE(poly2) ) {
   184          case PVR2_POLY_FOG_LOOKUP:
   185              glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
   186              break;
   187          case PVR2_POLY_FOG_VERTEX:
   188              glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
   189              break;
   190          }
   191      }
   192 #endif
   194      int srcblend = POLY2_SRC_BLEND(poly2);
   195      int destblend = POLY2_DEST_BLEND(poly2);
   196      glBlendFunc( srcblend, destblend );
   198      if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
   199          WARN( "Accumulation buffer not supported" );
   200      }   
   201 }
   203 /**
   204  * Setup the GL context for the supplied polygon context.
   205  * @param context pointer to 3 or 5 words of polygon context
   206  * @param depth_mode force depth mode, or 0 to use the polygon's
   207  * depth mode.
   208  */
   209 static void render_set_context( uint32_t *context, gboolean set_depth )
   210 {
   211     render_set_base_context(context[0], set_depth);
   212     render_set_tsp_context(context[0],context[1]);
   213 }
   215 static inline void gl_draw_vertexes( struct polygon_struct *poly )
   216 {
   217     do {
   218         glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
   219         poly = poly->sub_next;
   220     } while( poly != NULL );
   221 }
   223 static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
   224 {
   225     do {
   226         glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
   227         poly = poly->sub_next;
   228     } while( poly != NULL );
   229 }
   231 static void gl_render_poly( struct polygon_struct *poly, gboolean set_depth)
   232 {
   233     if( poly->vertex_count == 0 )
   234         return; /* Culled */
   236     bind_texture(poly->tex_id);
   237     if( poly->mod_vertex_index == -1 ) {
   238         render_set_context( poly->context, set_depth );
   239         gl_draw_vertexes(poly);
   240     }  else {
   241         glEnable( GL_STENCIL_TEST );
   242         render_set_base_context( poly->context[0], set_depth );
   243         render_set_tsp_context( poly->context[0], poly->context[1] );
   244         glStencilFunc(GL_EQUAL, 0, 2);
   245         gl_draw_vertexes(poly);
   247         if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
   248             bind_texture(poly->mod_tex_id);
   249             render_set_tsp_context( poly->context[0], poly->context[3] );
   250         }
   251         glStencilFunc(GL_EQUAL, 2, 2);
   252         gl_draw_mod_vertexes(poly);
   253         glDisable( GL_STENCIL_TEST );
   254     }
   255 }
   258 static void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
   259 {
   260     /* A bit of explanation:
   261      * In theory it works like this: generate a 1-bit stencil for each polygon
   262      * volume, and then AND or OR it against the overall 1-bit tile stencil at 
   263      * the end of the volume. 
   264      * 
   265      * The implementation here uses a 2-bit stencil buffer, where each volume
   266      * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
   267      * to update bit 1 accordingly and clear bit 0.
   268      * 
   269      * This could probably be more efficient, but at least it works correctly 
   270      * now :)
   271      */
   273     if( poly->vertex_count == 0 )
   274         return; /* Culled */
   276     gl_draw_vertexes(poly);
   280     int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
   281     if( poly_type == PVR2_VOLUME_REGION0 ) {
   282         /* 00 => 00
   283          * 01 => 00
   284          * 10 => 10
   285          * 11 => 00
   286          */
   287         glStencilMask( 0x03 );
   288         glStencilFunc(GL_EQUAL, 0x02, 0x03);
   289         glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
   290         glDisable( GL_DEPTH_TEST );
   292         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   294         glEnable( GL_DEPTH_TEST );
   295         glStencilMask( 0x01 );
   296         glStencilFunc( GL_ALWAYS, 0, 1 );
   297         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
   298     } else if( poly_type == PVR2_VOLUME_REGION1 ) {
   299         /* This is harder with the standard stencil ops - do it in two passes
   300          * 00 => 00 | 00 => 10
   301          * 01 => 10 | 01 => 10
   302          * 10 => 10 | 10 => 00
   303          * 11 => 10 | 11 => 10
   304          */
   305         glStencilMask( 0x02 );
   306         glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
   307         glDisable( GL_DEPTH_TEST );
   309         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   311         glStencilMask( 0x03 );
   312         glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
   313         glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
   315         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   317         glEnable( GL_DEPTH_TEST );
   318         glStencilMask( 0x01 );
   319         glStencilFunc( GL_ALWAYS, 0, 1 );
   320         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );         
   321     }
   322 }
   324 static void gl_render_bkgnd( struct polygon_struct *poly )
   325 {
   326     bind_texture(poly->tex_id);
   327     render_set_tsp_context( poly->context[0], poly->context[1] );
   328     glDisable( GL_DEPTH_TEST );
   329     glBlendFunc( GL_ONE, GL_ZERO );
   330     gl_draw_vertexes(poly);
   331     glEnable( GL_DEPTH_TEST );
   332 }
   334 void gl_render_triangle( struct polygon_struct *poly, int index )
   335 {
   336     bind_texture(poly->tex_id);
   337     render_set_tsp_context( poly->context[0], poly->context[1] );
   338     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index + index, 3 );
   340 }
   342 void gl_render_tilelist( pvraddr_t tile_entry, gboolean set_depth )
   343 {
   344     tileentryiter list;
   346     FOREACH_TILEENTRY(list, tile_entry) {
   347         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   348         if( poly != NULL ) {
   349             do {
   350                 gl_render_poly(poly, set_depth);
   351                 poly = poly->next;
   352             } while( list.strip_count-- > 0 );
   353         }
   354     }
   355 }
   357 /**
   358  * Render the tilelist with depthbuffer updates only.
   359  */
   360 static void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
   361 {
   362     tileentryiter list;
   364     FOREACH_TILEENTRY(list, tile_entry) {
   365         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   366         if( poly != NULL ) {
   367             do {
   368                 render_set_base_context(poly->context[0],TRUE);
   369                 gl_draw_vertexes(poly);
   370                 poly = poly->next;
   371             } while( list.strip_count-- > 0 );
   372         }
   373     }
   374 }
   376 static void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
   377 {
   378     tileentryiter list;
   380     FOREACH_TILEENTRY(list, tile_entry ) {
   381         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   382         if( poly != NULL ) {
   383             do {
   384                 gl_render_modifier_polygon( poly, tile_bounds );
   385                 poly = poly->next;
   386             } while( list.strip_count-- > 0 );
   387         }
   388     }
   389 }
   392 #ifdef HAVE_OPENGL_FIXEDFUNC
   393 void pvr2_scene_setup_fixed( GLfloat *viewMatrix )
   394 {
   395     glLoadMatrixf(viewMatrix);
   396     glEnable( GL_DEPTH_TEST );
   398     glEnable( GL_FOG );
   399     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
   400     glFogi(GL_FOG_MODE, GL_LINEAR);
   401     glFogf(GL_FOG_START, 0.0);
   402     glFogf(GL_FOG_END, 1.0);
   404     glEnable( GL_ALPHA_TEST );
   405     glAlphaFunc( GL_GEQUAL, 0 );
   407     glEnable( GL_COLOR_SUM );
   409     glEnableClientState( GL_VERTEX_ARRAY );
   410     glEnableClientState( GL_COLOR_ARRAY );
   411     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
   412     glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
   413     glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
   415     /* Vertex array pointers */
   416     glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
   417     glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
   418     glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
   419     glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
   420     glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
   421 }
   423 void pvr2_scene_set_alpha_fixed( float alphaRef )
   424 {
   425     glAlphaFunc( GL_GEQUAL, alphaRef );
   426 }
   428 void pvr2_scene_cleanup_fixed()
   429 {
   430     glDisable( GL_COLOR_SUM );
   431     glDisable( GL_FOG );
   432     glDisable( GL_ALPHA_TEST );
   433     glDisable( GL_DEPTH_TEST );
   435     glDisableClientState( GL_VERTEX_ARRAY );
   436     glDisableClientState( GL_COLOR_ARRAY );
   437     glDisableClientState( GL_TEXTURE_COORD_ARRAY );
   438     glDisableClientState( GL_SECONDARY_COLOR_ARRAY );
   439     glDisableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
   441 }
   442 #else
   443 void pvr2_scene_setup_fixed( GLfloat *viewMatrix )
   444 {
   445 }
   446 void pvr2_scene_set_alpha_fixed( float alphaRef )
   447 {
   448 }
   449 void pvr2_scene_cleanup_fixed()
   450 {
   451 }
   452 #endif
   454 void pvr2_scene_setup_shader( GLfloat *viewMatrix )
   455 {
   456     glEnable( GL_DEPTH_TEST );
   458     glsl_use_pvr2_shader();
   459     glsl_set_pvr2_shader_view_matrix(viewMatrix);
   460     glsl_set_pvr2_shader_fog_colour1(pvr2_scene.fog_vert_colour);
   461     glsl_set_pvr2_shader_fog_colour2(pvr2_scene.fog_lut_colour);
   462     glsl_set_pvr2_shader_in_vertex_vec3_pointer(&pvr2_scene.vertex_array[0].x, sizeof(struct vertex_struct));
   463     glsl_set_pvr2_shader_in_colour_pointer(&pvr2_scene.vertex_array[0].rgba[0], sizeof(struct vertex_struct));
   464     glsl_set_pvr2_shader_in_colour2_pointer(&pvr2_scene.vertex_array[0].offset_rgba[0], sizeof(struct vertex_struct));
   465     glsl_set_pvr2_shader_in_texcoord_pointer(&pvr2_scene.vertex_array[0].u, sizeof(struct vertex_struct));
   466     glsl_set_pvr2_shader_alpha_ref(0.0);
   467     glsl_set_pvr2_shader_primary_texture(0);
   468     glsl_set_pvr2_shader_palette_texture(1);
   469 }
   471 void pvr2_scene_cleanup_shader( )
   472 {
   473     glsl_clear_shader();
   475     glDisable( GL_DEPTH_TEST );
   476 }
   478 void pvr2_scene_set_alpha_shader( float alphaRef )
   479 {
   480     glsl_set_pvr2_shader_alpha_ref(alphaRef);
   481 }
   483 /**
   484  * Render the currently defined scene in pvr2_scene
   485  */
   486 void pvr2_scene_render( render_buffer_t buffer )
   487 {
   488     /* Scene setup */
   489     struct timeval start_tv, tex_tv, end_tv;
   490     int i;
   491     GLfloat viewMatrix[16];
   492     uint32_t clip_bounds[4];
   495     gettimeofday(&start_tv, NULL);
   496     display_driver->set_render_target(buffer);
   497     pvr2_check_palette_changed();
   498     pvr2_scene_load_textures();
   499     currentTexId = -1;
   501     gettimeofday( &tex_tv, NULL );
   502     uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
   503     (tex_tv.tv_usec - start_tv.tv_usec)/1000;
   504     DEBUG( "Texture load in %dms", ms );
   506     float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
   507     float nearz = pvr2_scene.bounds[4];
   508     float farz = pvr2_scene.bounds[5];
   509     if( nearz == farz ) {
   510         farz*= 4.0;
   511     }
   513     /* Generate integer clip boundaries */
   514     for( i=0; i<4; i++ ) {
   515         clip_bounds[i] = (uint32_t)pvr2_scene.bounds[i];
   516     }
   518     defineOrthoMatrix(viewMatrix, pvr2_scene.buffer_width, pvr2_scene.buffer_height, -farz, -nearz);
   520     if( have_shaders ) {
   521         pvr2_scene_setup_shader(viewMatrix);
   522     } else {
   523         pvr2_scene_setup_fixed(viewMatrix);
   524     }
   527     /* Clear the buffer (FIXME: May not want always want to do this) */
   528     glDisable( GL_SCISSOR_TEST );
   529     glDepthMask( GL_TRUE );
   530     glStencilMask( 0x03 );
   531     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
   533     /* Render the background */
   534     gl_render_bkgnd( pvr2_scene.bkgnd_poly );
   536     glEnable( GL_SCISSOR_TEST );
   537     glEnable( GL_TEXTURE_2D );
   539     struct tile_segment *segment;
   541 #define FOREACH_SEGMENT(segment) \
   542     segment = pvr2_scene.segment_list; \
   543     do { \
   544         int tilex = SEGMENT_X(segment->control); \
   545         int tiley = SEGMENT_Y(segment->control); \
   546         \
   547         uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 }; \
   548         if( !clip_tile_bounds(tile_bounds, clip_bounds) ) { \
   549             continue; \
   550         }
   551 #define END_FOREACH_SEGMENT() \
   552     } while( !IS_LAST_SEGMENT(segment++) );
   553 #define CLIP_TO_SEGMENT() \
   554     glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] )
   556     /* Build up the opaque stencil map */
   557     if( display_driver->capabilities.stencil_bits >= 2 ) {
   558         glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
   559         FOREACH_SEGMENT(segment)
   560             if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
   561                 CLIP_TO_SEGMENT();
   562                 gl_render_tilelist_depthonly(segment->opaque_ptr);
   563             }
   564         END_FOREACH_SEGMENT()
   566         glEnable( GL_STENCIL_TEST );
   567         glStencilFunc( GL_ALWAYS, 0, 1 );
   568         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
   569         glStencilMask( 0x01 );
   570         glDepthFunc( GL_LEQUAL );
   571         glDepthMask( GL_FALSE );
   572         FOREACH_SEGMENT(segment)
   573             if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
   574                 CLIP_TO_SEGMENT();
   575                 gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
   576             }
   577         END_FOREACH_SEGMENT()
   578         glDepthMask( GL_TRUE );
   579         glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
   580         glDisable( GL_SCISSOR_TEST );
   581         glClear( GL_DEPTH_BUFFER_BIT );
   582         glEnable( GL_SCISSOR_TEST );
   583         glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
   584     }
   586     /* Render the opaque polygons */
   587     FOREACH_SEGMENT(segment)
   588         CLIP_TO_SEGMENT();
   589         gl_render_tilelist(segment->opaque_ptr,TRUE);
   590     END_FOREACH_SEGMENT()
   591     glDisable( GL_STENCIL_TEST );
   593     /* Render the punch-out polygons */
   594     if( have_shaders )
   595         pvr2_scene_set_alpha_shader(alphaRef);
   596     else
   597         pvr2_scene_set_alpha_fixed(alphaRef);
   598     glDepthFunc(GL_GEQUAL);
   599     FOREACH_SEGMENT(segment)
   600         CLIP_TO_SEGMENT();
   601         gl_render_tilelist(segment->punchout_ptr, FALSE );
   602     END_FOREACH_SEGMENT()
   603     if( have_shaders )
   604         pvr2_scene_set_alpha_shader(0.0);
   605     else
   606         pvr2_scene_set_alpha_fixed(0.0);
   608     /* Render the translucent polygons */
   609     FOREACH_SEGMENT(segment)
   610         if( IS_NONEMPTY_TILE_LIST(segment->trans_ptr) ) {
   611             CLIP_TO_SEGMENT();
   612             if( pvr2_scene.sort_mode == SORT_NEVER || 
   613                     (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
   614                 gl_render_tilelist(segment->trans_ptr, TRUE);
   615             } else {
   616                 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
   617             }
   618         }
   619     END_FOREACH_SEGMENT()
   621     glDisable( GL_SCISSOR_TEST );
   623     if( have_shaders ) {
   624         pvr2_scene_cleanup_shader();
   625     } else {
   626         pvr2_scene_cleanup_fixed();
   627     }
   629     pvr2_scene_finished();
   631     gettimeofday( &end_tv, NULL );
   632     ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
   633     (end_tv.tv_usec - tex_tv.tv_usec)/1000;
   634     DEBUG( "Scene render in %dms", ms );
   635 }
.