Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 865:e10c081f4b81
prev864:a90f3d5e57e1
next886:2bc6d2329cce
author nkeynes
date Thu Oct 16 04:46:33 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Enhance triangle sort to be a little more precise
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/scene.h"
    25 #include "pvr2/glutil.h"
    27 #define IS_EMPTY_TILE_LIST(p) ((*((uint32_t *)(video_base+(p))) >> 28) == 0x0F)
    29 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
    30         GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, 
    31         GL_ALWAYS };
    32 int pvr2_poly_srcblend[8] = { 
    33         GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
    34         GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, 
    35         GL_ONE_MINUS_DST_ALPHA };
    36 int pvr2_poly_dstblend[8] = {
    37         GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
    38         GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
    39         GL_ONE_MINUS_DST_ALPHA };
    40 int pvr2_poly_texblend[4] = {
    41         GL_REPLACE, 
    42         GL_MODULATE,  
    43         GL_DECAL, 
    44         GL_MODULATE 
    45 };
    47 /**
    48  * Clip the tile bounds to the clipping plane. 
    49  * @return TRUE if the tile was not clipped completely.
    50  */
    51 static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
    52 {
    53     if( tile[0] < clip[0] ) tile[0] = clip[0];
    54     if( tile[1] > clip[1] ) tile[1] = clip[1];
    55     if( tile[2] < clip[2] ) tile[2] = clip[2];
    56     if( tile[3] > clip[3] ) tile[3] = clip[3];
    57     return tile[0] < tile[1] && tile[2] < tile[3];
    58 }
    60 void pvr2_scene_load_textures()
    61 {
    62     int i;
    63     for( i=0; i < pvr2_scene.poly_count; i++ ) {
    64         struct polygon_struct *poly = &pvr2_scene.poly_array[i];
    65         if( POLY1_TEXTURED(poly->context[0]) ) {
    66             poly->tex_id = texcache_get_texture( poly->context[2],
    67                     POLY2_TEX_WIDTH(poly->context[1]),
    68                     POLY2_TEX_HEIGHT(poly->context[1]) );
    69             if( poly->mod_vertex_index != -1 ) {
    70                 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
    71                     poly->mod_tex_id = texcache_get_texture( poly->context[4],
    72                             POLY2_TEX_WIDTH(poly->context[3]),
    73                             POLY2_TEX_HEIGHT(poly->context[3]) );
    74                 } else {
    75                     poly->mod_tex_id = poly->tex_id;
    76                 }
    77             }
    78         } else {
    79             poly->tex_id = -1;
    80             poly->mod_tex_id = -1;
    81         }
    82     }
    83 }
    87 /**
    88  * Once-off call to setup the OpenGL context.
    89  */
    90 void pvr2_setup_gl_context()
    91 {
    93     if( glsl_is_supported() ) {
    94         if( !glsl_load_shaders( glsl_vertex_shader_src, NULL ) ) {
    95             WARN( "Unable to load GL shaders" );
    96         }
    97     }
    99     texcache_gl_init(); // Allocate texture IDs
   100     glCullFace( GL_BACK );
   101     glEnable( GL_BLEND );
   102     glEnable( GL_DEPTH_TEST );
   103     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
   104     glMatrixMode(GL_MODELVIEW);
   105     glLoadIdentity();
   107 #ifdef HAVE_OPENGL_CLAMP_COLOR
   108     if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
   109         glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
   110         glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
   111     }
   112 #endif
   114     glEnableClientState( GL_COLOR_ARRAY );
   115     glEnableClientState( GL_VERTEX_ARRAY );
   116     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
   117     glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
   118     glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
   120     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   121     glClearDepth(0);
   122     glClearStencil(0);
   123 }
   125 static void render_set_cull( uint32_t poly1 )
   126 {
   127     switch( POLY1_CULL_MODE(poly1) ) {
   128     case CULL_NONE:
   129     case CULL_SMALL:
   130         glDisable( GL_CULL_FACE );
   131         break;
   132     case CULL_CCW:
   133         glEnable( GL_CULL_FACE );
   134         glFrontFace( GL_CW );
   135         break;
   136     case CULL_CW:
   137         glEnable( GL_CULL_FACE );
   138         glFrontFace( GL_CCW );
   139         break;
   140     }   
   141 }    
   143 /**
   144  * Setup the basic context that's shared between normal and modified modes -
   145  * depth, culling
   146  */
   147 static void render_set_base_context( uint32_t poly1, GLint depth_mode )
   148 {
   149     if( depth_mode == 0 ) {
   150         glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   151     } else {
   152         glDepthFunc(depth_mode);
   153     }
   155     glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
   156     render_set_cull( poly1 );
   157 }
   159 /**
   160  * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
   161  */
   162 static void render_set_tsp_context( uint32_t poly1, uint32_t poly2, uint32_t texture )
   163 {
   164     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   166     if( POLY1_SPECULAR(poly1) ) {
   167         glEnable(GL_COLOR_SUM);
   168     } else {
   169         glDisable(GL_COLOR_SUM);
   170     }
   172     if( POLY1_TEXTURED(poly1) ) {
   173          glEnable(GL_TEXTURE_2D);
   174          glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
   175          if( POLY2_TEX_CLAMP_U(poly2) ) {
   176              glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
   177          } else if( POLY2_TEX_MIRROR_U(poly2) ) {
   178              glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB );
   179          } else {
   180              glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
   181          }       
   182          if( POLY2_TEX_CLAMP_V(poly2) ) {
   183              glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
   184          } else if( POLY2_TEX_MIRROR_V(poly2) ) {
   185              glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB );
   186          } else {
   187              glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
   188          }
   189      } else {
   190          glDisable( GL_TEXTURE_2D );
   191      }
   193      switch( POLY2_FOG_MODE(poly2) ) {
   194      case PVR2_POLY_FOG_LOOKUP:
   195          glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
   196          glEnable( GL_FOG );
   197          break;
   198      case PVR2_POLY_FOG_VERTEX:
   199          if( POLY1_SPECULAR(poly1) ) {
   200              glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
   201              glEnable( GL_FOG );
   202              break;
   203          } /* else fallthrough */
   204      default:
   205          glDisable( GL_FOG );
   206      }
   208      int srcblend = POLY2_SRC_BLEND(poly2);
   209      int destblend = POLY2_DEST_BLEND(poly2);
   210      glBlendFunc( srcblend, destblend );
   212      if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
   213          WARN( "Accumulation buffer not supported" );
   214      }   
   215 }
   217 /**
   218  * Setup the GL context for the supplied polygon context.
   219  * @param context pointer to 3 or 5 words of polygon context
   220  * @param depth_mode force depth mode, or 0 to use the polygon's
   221  * depth mode.
   222  */
   223 void render_set_context( uint32_t *context, GLint depth_mode )
   224 {
   225     render_set_base_context(context[0], depth_mode);
   226     render_set_tsp_context(context[0],context[1],context[2]);
   227 }
   230 static void gl_render_poly( struct polygon_struct *poly, GLint depth_mode )
   231 {
   232     if( poly->tex_id != -1 ) {
   233         glBindTexture(GL_TEXTURE_2D, poly->tex_id);
   234     }
   235     if( poly->mod_vertex_index == -1 ) {
   236         glDisable( GL_STENCIL_TEST );
   237         render_set_context( poly->context, depth_mode );
   238         glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   239     }  else {
   240         glEnable( GL_STENCIL_TEST );
   241         render_set_base_context( poly->context[0], depth_mode );
   242         render_set_tsp_context( poly->context[0], poly->context[1], poly->context[2] );
   243         glStencilFunc(GL_EQUAL, 0, 2);
   244         glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   246         if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
   247             if( poly->mod_tex_id != -1 ) {
   248                 glBindTexture(GL_TEXTURE_2D, poly->mod_tex_id);
   249             }
   250             render_set_tsp_context( poly->context[0], poly->context[3], poly->context[4] );
   251         }
   252         glStencilFunc(GL_EQUAL, 2, 2);
   253         glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count );
   254     }
   255 }
   257 static void gl_render_bkgnd( struct polygon_struct *poly )
   258 {
   259     if( poly->tex_id != -1 ) {
   260         glBindTexture(GL_TEXTURE_2D, poly->tex_id);
   261     }
   262     render_set_context( poly->context, 0 );
   263     glDisable( GL_DEPTH_TEST );
   264     glDisable( GL_CULL_FACE );
   265     glBlendFunc( GL_ONE, GL_ZERO );
   266     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   267     glEnable( GL_CULL_FACE );
   268     glEnable( GL_DEPTH_TEST );
   269 }
   271 void gl_render_tilelist( pvraddr_t tile_entry, GLint depth_mode )
   272 {
   273     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   274     int strip_count;
   275     struct polygon_struct *poly;
   277     if( !IS_TILE_PTR(tile_entry) )
   278         return;
   280     while(1) {
   281         uint32_t entry = *tile_list++;
   282         switch( entry >> 28 ) {
   283         case 0x0F:
   284             return; // End-of-list
   285         case 0x0E:
   286             tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   287             break;
   288         case 0x08: case 0x09: case 0x0A: case 0x0B:
   289             strip_count = ((entry >> 25) & 0x0F)+1;
   290             poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   291             while( strip_count > 0 ) {
   292                 assert( poly != NULL );
   293                 gl_render_poly( poly, depth_mode );
   294                 poly = poly->next;
   295                 strip_count--;
   296             }
   297             break;
   298         default:
   299             if( entry & 0x7E000000 ) {
   300                 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   301                 gl_render_poly( poly, depth_mode );
   302             }
   303         }
   304     }       
   305 }
   307 /**
   308  * Render the tilelist with depthbuffer updates only. 
   309  */
   310 void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
   311 {
   312     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   313     int strip_count;
   314     struct polygon_struct *poly;
   316     if( !IS_TILE_PTR(tile_entry) )
   317         return;
   319     glDisable( GL_TEXTURE_2D );
   320     glDisable( GL_FOG );
   321     glDisable( GL_COLOR_SUM );
   323     while(1) {
   324         uint32_t entry = *tile_list++;
   325         switch( entry >> 28 ) {
   326         case 0x0F:
   327             return; // End-of-list
   328         case 0x0E:
   329             tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   330             break;
   331         case 0x08: case 0x09: case 0x0A: case 0x0B:
   332             strip_count = ((entry >> 25) & 0x0F)+1;
   333             poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   334             while( strip_count > 0 ) {
   335                 render_set_base_context(poly->context[0],0);
   336                 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   337                 poly = poly->next;
   338                 strip_count--;
   339             }
   340             break;
   341         default:
   342             if( entry & 0x7E000000 ) {
   343                 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   344                 render_set_base_context(poly->context[0],0);
   345                 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   346             }
   347         }
   348     }           
   349 }
   351 static void drawrect2d( uint32_t tile_bounds[], float z )
   352 {
   353     glBegin( GL_QUADS );
   354     glVertex3f( tile_bounds[0], tile_bounds[2], z );
   355     glVertex3f( tile_bounds[1], tile_bounds[2], z );
   356     glVertex3f( tile_bounds[1], tile_bounds[3], z );
   357     glVertex3f( tile_bounds[0], tile_bounds[3], z );
   358     glEnd();
   359 }
   361 void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
   362 {
   363     /* A bit of explanation:
   364      * In theory it works like this: generate a 1-bit stencil for each polygon
   365      * volume, and then AND or OR it against the overall 1-bit tile stencil at 
   366      * the end of the volume. 
   367      * 
   368      * The implementation here uses a 2-bit stencil buffer, where each volume
   369      * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
   370      * to update bit 1 accordingly and clear bit 0.
   371      * 
   372      * This could probably be more efficient, but at least it works correctly 
   373      * now :)
   374      */
   376     render_set_cull(poly->context[0]);
   377     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   379     int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
   380     if( poly_type == PVR2_VOLUME_REGION0 ) {
   381         /* 00 => 00
   382          * 01 => 00
   383          * 10 => 10
   384          * 11 => 00
   385          */
   386         glStencilMask( 0x03 );
   387         glStencilFunc(GL_EQUAL, 0x02, 0x03);
   388         glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
   389         glDisable( GL_CULL_FACE );
   390         glDisable( GL_DEPTH_TEST );
   392         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   394         glEnable( GL_DEPTH_TEST );
   395         glStencilMask( 0x01 );
   396         glStencilFunc( GL_ALWAYS, 0, 1 );
   397         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
   398     } else if( poly_type == PVR2_VOLUME_REGION1 ) {
   399         /* This is harder with the standard stencil ops - do it in two passes
   400          * 00 => 00 | 00 => 10
   401          * 01 => 10 | 01 => 10
   402          * 10 => 10 | 10 => 00
   403          * 11 => 10 | 11 => 10
   404          */
   405         glStencilMask( 0x02 );
   406         glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
   407         glDisable( GL_CULL_FACE );
   408         glDisable( GL_DEPTH_TEST );
   410         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   412         glStencilMask( 0x03 );
   413         glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
   414         glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
   416         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   418         glEnable( GL_DEPTH_TEST );
   419         glStencilMask( 0x01 );
   420         glStencilFunc( GL_ALWAYS, 0, 1 );
   421         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );         
   422     }
   423 }
   425 void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
   426 {
   427     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   428     int strip_count;
   429     struct polygon_struct *poly;
   431     if( !IS_TILE_PTR(tile_entry) )
   432         return;
   434     glDisable( GL_TEXTURE_2D );
   435     glDisable( GL_FOG );
   436     glDisable( GL_COLOR_SUM );
   437     glDisable( GL_CULL_FACE );
   438     glEnable( GL_STENCIL_TEST );
   439     glEnable( GL_DEPTH_TEST );
   440     glDepthFunc( GL_LEQUAL );
   442     glStencilFunc( GL_ALWAYS, 0, 1 );
   443     glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
   444     glStencilMask( 0x01 );
   445     glDepthMask( GL_FALSE );
   447     while(1) {
   448         uint32_t entry = *tile_list++;
   449         switch( entry >> 28 ) {
   450         case 0x0F:
   451             glDepthMask( GL_TRUE );
   452             glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
   453             return; // End-of-list
   454         case 0x0E:
   455             tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   456             break;
   457         case 0x08: case 0x09: case 0x0A: case 0x0B:
   458             strip_count = ((entry >> 25) & 0x0F)+1;
   459             poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   460             while( strip_count > 0 ) {
   461                 gl_render_modifier_polygon( poly, tile_bounds );
   462                 poly = poly->next;
   463                 strip_count--;
   464             }
   465             break;
   466         default:
   467             if( entry & 0x7E000000 ) {
   468                 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   469                 gl_render_modifier_polygon( poly, tile_bounds );
   470             }
   471         }
   472     }
   474 }
   477 /**
   478  * Render the currently defined scene in pvr2_scene
   479  */
   480 void pvr2_scene_render( render_buffer_t buffer )
   481 {
   482     /* Scene setup */
   483     struct timeval start_tv, tex_tv, end_tv;
   485     gettimeofday(&start_tv, NULL);
   486     display_driver->set_render_target(buffer);
   487     pvr2_check_palette_changed();
   488     pvr2_scene_load_textures();
   490     gettimeofday( &tex_tv, NULL );
   491     uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
   492     (tex_tv.tv_usec - start_tv.tv_usec)/1000;
   493     DEBUG( "Scene setup in %dms", ms );
   495     /* Setup view projection matrix */
   496     glMatrixMode(GL_PROJECTION);
   497     glLoadIdentity();
   498     float nearz = pvr2_scene.bounds[4];
   499     float farz = pvr2_scene.bounds[5];
   500     if( nearz == farz ) {
   501         farz*= 4.0;
   502     }
   503     glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0, 
   504              -farz, -nearz );
   505     float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
   506     glAlphaFunc( GL_GEQUAL, alphaRef );
   508     /* Clear the buffer (FIXME: May not want always want to do this) */
   509     glDisable( GL_SCISSOR_TEST );
   510     glDepthMask( GL_TRUE );
   511     glStencilMask( 0x03 );
   512     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
   514     /* Setup vertex array pointers */
   515     glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
   516     glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
   517     glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
   518     glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
   519     glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
   520     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
   521     glFogi(GL_FOG_MODE, GL_LINEAR);
   522     glFogf(GL_FOG_START, 0.0);
   523     glFogf(GL_FOG_END, 1.0);
   524     /* Turn on the shaders (if available) */
   525     glsl_enable_shaders(TRUE);
   527     /* Render the background */
   528     gl_render_bkgnd( pvr2_scene.bkgnd_poly );
   530     glEnable( GL_SCISSOR_TEST );
   532     /* Process the segment list */
   533     struct tile_segment *segment = pvr2_scene.segment_list;
   534     do {
   535         int tilex = SEGMENT_X(segment->control);
   536         int tiley = SEGMENT_Y(segment->control);
   538         uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
   539         if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
   540             continue; // fully clipped, skip tile
   541         }
   543         /* Clip to the visible part of the tile */
   544         glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], 
   545                    tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
   546         if( display_driver->capabilities.stencil_bits >= 2 && 
   547                 IS_TILE_PTR(segment->opaquemod_ptr) &&
   548                 !IS_EMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
   549             /* Don't do this unless there's actually some shadow polygons */
   551             /* Use colormask instead of drawbuffer for simplicity */
   552             glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
   553             gl_render_tilelist_depthonly(segment->opaque_ptr);
   554             gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
   555             glClear( GL_DEPTH_BUFFER_BIT );
   556             glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
   557         }
   558         gl_render_tilelist(segment->opaque_ptr,0);
   559         if( IS_TILE_PTR(segment->punchout_ptr) ) {
   560             glEnable(GL_ALPHA_TEST );
   561             gl_render_tilelist(segment->punchout_ptr, GL_GEQUAL );
   562             glDisable(GL_ALPHA_TEST );
   563         }
   564         glDisable( GL_STENCIL_TEST );
   565         glStencilMask(0x03);
   566         glClear( GL_STENCIL_BUFFER_BIT );
   568         if( IS_TILE_PTR(segment->trans_ptr) ) {
   569             if( pvr2_scene.sort_mode == SORT_NEVER || 
   570                     (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
   571                 gl_render_tilelist(segment->trans_ptr, 0);
   572             } else {
   573                 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
   574             }
   575         }
   576     } while( !IS_LAST_SEGMENT(segment++) );
   577     glDisable( GL_SCISSOR_TEST );
   579     glsl_enable_shaders(FALSE);
   581     gettimeofday( &end_tv, NULL );
   582     ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
   583     (end_tv.tv_usec - tex_tv.tv_usec)/1000;
   584     DEBUG( "Scene render in %dms", ms );
   585 }
.