Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 934:3acd3b3ee6d1
prev886:2bc6d2329cce
next1066:ddffe9d2b332
author nkeynes
date Fri Dec 26 14:25:23 2008 +0000 (15 years ago)
branchlxdream-mem
permissions -rw-r--r--
last change Change RAM regions to use static arrays rather than mmap regions, for a 2-3% performance gain.
General mem cleanups, including some save state fixes that break states again.
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 *)(pvr2_main_ram+(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;
    64     texcache_set_config( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03,
    65                          (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5 );
    67     for( i=0; i < pvr2_scene.poly_count; i++ ) {
    68         struct polygon_struct *poly = &pvr2_scene.poly_array[i];
    69         if( POLY1_TEXTURED(poly->context[0]) ) {
    70             poly->tex_id = texcache_get_texture( poly->context[2],
    71                     POLY2_TEX_WIDTH(poly->context[1]),
    72                     POLY2_TEX_HEIGHT(poly->context[1]) );
    73             if( poly->mod_vertex_index != -1 ) {
    74                 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
    75                     poly->mod_tex_id = texcache_get_texture( poly->context[4],
    76                             POLY2_TEX_WIDTH(poly->context[3]),
    77                             POLY2_TEX_HEIGHT(poly->context[3]) );
    78                 } else {
    79                     poly->mod_tex_id = poly->tex_id;
    80                 }
    81             }
    82         } else {
    83             poly->tex_id = -1;
    84             poly->mod_tex_id = -1;
    85         }
    86     }
    87 }
    91 /**
    92  * Once-off call to setup the OpenGL context.
    93  */
    94 void pvr2_setup_gl_context()
    95 {
    97     if( glsl_is_supported() ) {
    98         if( !glsl_load_shaders( glsl_vertex_shader_src, NULL ) ) {
    99             WARN( "Unable to load GL shaders" );
   100         }
   101     }
   103     texcache_gl_init(); // Allocate texture IDs
   104     glCullFace( GL_BACK );
   105     glEnable( GL_BLEND );
   106     glEnable( GL_DEPTH_TEST );
   107     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
   108     glMatrixMode(GL_MODELVIEW);
   109     glLoadIdentity();
   111 #ifdef HAVE_OPENGL_CLAMP_COLOR
   112     if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
   113         glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
   114         glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
   115     }
   116 #endif
   118     glEnableClientState( GL_COLOR_ARRAY );
   119     glEnableClientState( GL_VERTEX_ARRAY );
   120     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
   121     glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
   122     glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
   124     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   125     glClearDepth(0);
   126     glClearStencil(0);
   127 }
   129 static void render_set_cull( uint32_t poly1 )
   130 {
   131     switch( POLY1_CULL_MODE(poly1) ) {
   132     case CULL_NONE:
   133     case CULL_SMALL:
   134         glDisable( GL_CULL_FACE );
   135         break;
   136     case CULL_CCW:
   137         glEnable( GL_CULL_FACE );
   138         glFrontFace( GL_CW );
   139         break;
   140     case CULL_CW:
   141         glEnable( GL_CULL_FACE );
   142         glFrontFace( GL_CCW );
   143         break;
   144     }   
   145 }    
   147 /**
   148  * Setup the basic context that's shared between normal and modified modes -
   149  * depth, culling
   150  */
   151 static void render_set_base_context( uint32_t poly1, GLint depth_mode )
   152 {
   153     if( depth_mode == 0 ) {
   154         glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   155     } else {
   156         glDepthFunc(depth_mode);
   157     }
   159     glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
   160     render_set_cull( poly1 );
   161 }
   163 /**
   164  * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
   165  */
   166 static void render_set_tsp_context( uint32_t poly1, uint32_t poly2, uint32_t texture )
   167 {
   168     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   170     if( POLY1_SPECULAR(poly1) ) {
   171         glEnable(GL_COLOR_SUM);
   172     } else {
   173         glDisable(GL_COLOR_SUM);
   174     }
   176     if( POLY1_TEXTURED(poly1) ) {
   177          glEnable(GL_TEXTURE_2D);
   178          glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
   179          if( POLY2_TEX_CLAMP_U(poly2) ) {
   180              glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
   181          } else if( POLY2_TEX_MIRROR_U(poly2) ) {
   182              glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB );
   183          } else {
   184              glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
   185          }       
   186          if( POLY2_TEX_CLAMP_V(poly2) ) {
   187              glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
   188          } else if( POLY2_TEX_MIRROR_V(poly2) ) {
   189              glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB );
   190          } else {
   191              glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
   192          }
   193      } else {
   194          glDisable( GL_TEXTURE_2D );
   195      }
   197      switch( POLY2_FOG_MODE(poly2) ) {
   198      case PVR2_POLY_FOG_LOOKUP:
   199          glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
   200          glEnable( GL_FOG );
   201          break;
   202      case PVR2_POLY_FOG_VERTEX:
   203          if( POLY1_SPECULAR(poly1) ) {
   204              glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
   205              glEnable( GL_FOG );
   206              break;
   207          } /* else fallthrough */
   208      default:
   209          glDisable( GL_FOG );
   210      }
   212      int srcblend = POLY2_SRC_BLEND(poly2);
   213      int destblend = POLY2_DEST_BLEND(poly2);
   214      glBlendFunc( srcblend, destblend );
   216      if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
   217          WARN( "Accumulation buffer not supported" );
   218      }   
   219 }
   221 /**
   222  * Setup the GL context for the supplied polygon context.
   223  * @param context pointer to 3 or 5 words of polygon context
   224  * @param depth_mode force depth mode, or 0 to use the polygon's
   225  * depth mode.
   226  */
   227 void render_set_context( uint32_t *context, GLint depth_mode )
   228 {
   229     render_set_base_context(context[0], depth_mode);
   230     render_set_tsp_context(context[0],context[1],context[2]);
   231 }
   234 static void gl_render_poly( struct polygon_struct *poly, GLint depth_mode )
   235 {
   236     if( poly->tex_id != -1 ) {
   237         glBindTexture(GL_TEXTURE_2D, poly->tex_id);
   238     }
   239     if( poly->mod_vertex_index == -1 ) {
   240         glDisable( GL_STENCIL_TEST );
   241         render_set_context( poly->context, depth_mode );
   242         glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   243     }  else {
   244         glEnable( GL_STENCIL_TEST );
   245         render_set_base_context( poly->context[0], depth_mode );
   246         render_set_tsp_context( poly->context[0], poly->context[1], poly->context[2] );
   247         glStencilFunc(GL_EQUAL, 0, 2);
   248         glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   250         if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
   251             if( poly->mod_tex_id != -1 ) {
   252                 glBindTexture(GL_TEXTURE_2D, poly->mod_tex_id);
   253             }
   254             render_set_tsp_context( poly->context[0], poly->context[3], poly->context[4] );
   255         }
   256         glStencilFunc(GL_EQUAL, 2, 2);
   257         glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count );
   258     }
   259 }
   261 static void gl_render_bkgnd( struct polygon_struct *poly )
   262 {
   263     if( poly->tex_id != -1 ) {
   264         glBindTexture(GL_TEXTURE_2D, poly->tex_id);
   265     }
   266     render_set_context( poly->context, 0 );
   267     glDisable( GL_DEPTH_TEST );
   268     glDisable( GL_CULL_FACE );
   269     glBlendFunc( GL_ONE, GL_ZERO );
   270     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   271     glEnable( GL_CULL_FACE );
   272     glEnable( GL_DEPTH_TEST );
   273 }
   275 void gl_render_tilelist( pvraddr_t tile_entry, GLint depth_mode )
   276 {
   277     uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
   278     int strip_count;
   279     struct polygon_struct *poly;
   281     if( !IS_TILE_PTR(tile_entry) )
   282         return;
   284     while(1) {
   285         uint32_t entry = *tile_list++;
   286         switch( entry >> 28 ) {
   287         case 0x0F:
   288             return; // End-of-list
   289         case 0x0E:
   290             tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
   291             break;
   292         case 0x08: case 0x09: case 0x0A: case 0x0B:
   293             strip_count = ((entry >> 25) & 0x0F)+1;
   294             poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   295             while( strip_count > 0 ) {
   296                 assert( poly != NULL );
   297                 gl_render_poly( poly, depth_mode );
   298                 poly = poly->next;
   299                 strip_count--;
   300             }
   301             break;
   302         default:
   303             if( entry & 0x7E000000 ) {
   304                 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   305                 gl_render_poly( poly, depth_mode );
   306             }
   307         }
   308     }       
   309 }
   311 /**
   312  * Render the tilelist with depthbuffer updates only. 
   313  */
   314 void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
   315 {
   316     uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
   317     int strip_count;
   318     struct polygon_struct *poly;
   320     if( !IS_TILE_PTR(tile_entry) )
   321         return;
   323     glDisable( GL_TEXTURE_2D );
   324     glDisable( GL_FOG );
   325     glDisable( GL_COLOR_SUM );
   327     while(1) {
   328         uint32_t entry = *tile_list++;
   329         switch( entry >> 28 ) {
   330         case 0x0F:
   331             return; // End-of-list
   332         case 0x0E:
   333             tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
   334             break;
   335         case 0x08: case 0x09: case 0x0A: case 0x0B:
   336             strip_count = ((entry >> 25) & 0x0F)+1;
   337             poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   338             while( strip_count > 0 ) {
   339                 render_set_base_context(poly->context[0],0);
   340                 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   341                 poly = poly->next;
   342                 strip_count--;
   343             }
   344             break;
   345         default:
   346             if( entry & 0x7E000000 ) {
   347                 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   348                 render_set_base_context(poly->context[0],0);
   349                 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   350             }
   351         }
   352     }           
   353 }
   355 static void drawrect2d( uint32_t tile_bounds[], float z )
   356 {
   357     glBegin( GL_QUADS );
   358     glVertex3f( tile_bounds[0], tile_bounds[2], z );
   359     glVertex3f( tile_bounds[1], tile_bounds[2], z );
   360     glVertex3f( tile_bounds[1], tile_bounds[3], z );
   361     glVertex3f( tile_bounds[0], tile_bounds[3], z );
   362     glEnd();
   363 }
   365 void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
   366 {
   367     /* A bit of explanation:
   368      * In theory it works like this: generate a 1-bit stencil for each polygon
   369      * volume, and then AND or OR it against the overall 1-bit tile stencil at 
   370      * the end of the volume. 
   371      * 
   372      * The implementation here uses a 2-bit stencil buffer, where each volume
   373      * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
   374      * to update bit 1 accordingly and clear bit 0.
   375      * 
   376      * This could probably be more efficient, but at least it works correctly 
   377      * now :)
   378      */
   380     render_set_cull(poly->context[0]);
   381     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   383     int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
   384     if( poly_type == PVR2_VOLUME_REGION0 ) {
   385         /* 00 => 00
   386          * 01 => 00
   387          * 10 => 10
   388          * 11 => 00
   389          */
   390         glStencilMask( 0x03 );
   391         glStencilFunc(GL_EQUAL, 0x02, 0x03);
   392         glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
   393         glDisable( GL_CULL_FACE );
   394         glDisable( GL_DEPTH_TEST );
   396         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   398         glEnable( GL_DEPTH_TEST );
   399         glStencilMask( 0x01 );
   400         glStencilFunc( GL_ALWAYS, 0, 1 );
   401         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
   402     } else if( poly_type == PVR2_VOLUME_REGION1 ) {
   403         /* This is harder with the standard stencil ops - do it in two passes
   404          * 00 => 00 | 00 => 10
   405          * 01 => 10 | 01 => 10
   406          * 10 => 10 | 10 => 00
   407          * 11 => 10 | 11 => 10
   408          */
   409         glStencilMask( 0x02 );
   410         glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
   411         glDisable( GL_CULL_FACE );
   412         glDisable( GL_DEPTH_TEST );
   414         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   416         glStencilMask( 0x03 );
   417         glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
   418         glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
   420         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   422         glEnable( GL_DEPTH_TEST );
   423         glStencilMask( 0x01 );
   424         glStencilFunc( GL_ALWAYS, 0, 1 );
   425         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );         
   426     }
   427 }
   429 void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
   430 {
   431     uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
   432     int strip_count;
   433     struct polygon_struct *poly;
   435     if( !IS_TILE_PTR(tile_entry) )
   436         return;
   438     glDisable( GL_TEXTURE_2D );
   439     glDisable( GL_FOG );
   440     glDisable( GL_COLOR_SUM );
   441     glDisable( GL_CULL_FACE );
   442     glEnable( GL_STENCIL_TEST );
   443     glEnable( GL_DEPTH_TEST );
   444     glDepthFunc( GL_LEQUAL );
   446     glStencilFunc( GL_ALWAYS, 0, 1 );
   447     glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
   448     glStencilMask( 0x01 );
   449     glDepthMask( GL_FALSE );
   451     while(1) {
   452         uint32_t entry = *tile_list++;
   453         switch( entry >> 28 ) {
   454         case 0x0F:
   455             glDepthMask( GL_TRUE );
   456             glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
   457             return; // End-of-list
   458         case 0x0E:
   459             tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
   460             break;
   461         case 0x08: case 0x09: case 0x0A: case 0x0B:
   462             strip_count = ((entry >> 25) & 0x0F)+1;
   463             poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   464             while( strip_count > 0 ) {
   465                 gl_render_modifier_polygon( poly, tile_bounds );
   466                 poly = poly->next;
   467                 strip_count--;
   468             }
   469             break;
   470         default:
   471             if( entry & 0x7E000000 ) {
   472                 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   473                 gl_render_modifier_polygon( poly, tile_bounds );
   474             }
   475         }
   476     }
   478 }
   481 /**
   482  * Render the currently defined scene in pvr2_scene
   483  */
   484 void pvr2_scene_render( render_buffer_t buffer )
   485 {
   486     /* Scene setup */
   487     struct timeval start_tv, tex_tv, end_tv;
   489     gettimeofday(&start_tv, NULL);
   490     display_driver->set_render_target(buffer);
   491     pvr2_check_palette_changed();
   492     pvr2_scene_load_textures();
   494     gettimeofday( &tex_tv, NULL );
   495     uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
   496     (tex_tv.tv_usec - start_tv.tv_usec)/1000;
   497     DEBUG( "Scene setup in %dms", ms );
   499     /* Setup view projection matrix */
   500     glMatrixMode(GL_PROJECTION);
   501     glLoadIdentity();
   502     float nearz = pvr2_scene.bounds[4];
   503     float farz = pvr2_scene.bounds[5];
   504     if( nearz == farz ) {
   505         farz*= 4.0;
   506     }
   507     glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0, 
   508              -farz, -nearz );
   509     float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
   510     glAlphaFunc( GL_GEQUAL, alphaRef );
   512     /* Clear the buffer (FIXME: May not want always want to do this) */
   513     glDisable( GL_SCISSOR_TEST );
   514     glDepthMask( GL_TRUE );
   515     glStencilMask( 0x03 );
   516     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
   518     /* Setup vertex array pointers */
   519     glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
   520     glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
   521     glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
   522     glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
   523     glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
   524     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
   525     glFogi(GL_FOG_MODE, GL_LINEAR);
   526     glFogf(GL_FOG_START, 0.0);
   527     glFogf(GL_FOG_END, 1.0);
   528     /* Turn on the shaders (if available) */
   529     glsl_enable_shaders(TRUE);
   531     /* Render the background */
   532     gl_render_bkgnd( pvr2_scene.bkgnd_poly );
   534     glEnable( GL_SCISSOR_TEST );
   536     /* Process the segment list */
   537     struct tile_segment *segment = pvr2_scene.segment_list;
   538     do {
   539         int tilex = SEGMENT_X(segment->control);
   540         int tiley = SEGMENT_Y(segment->control);
   542         uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
   543         if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
   544             continue; // fully clipped, skip tile
   545         }
   547         /* Clip to the visible part of the tile */
   548         glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], 
   549                    tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
   550         if( display_driver->capabilities.stencil_bits >= 2 && 
   551                 IS_TILE_PTR(segment->opaquemod_ptr) &&
   552                 !IS_EMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
   553             /* Don't do this unless there's actually some shadow polygons */
   555             /* Use colormask instead of drawbuffer for simplicity */
   556             glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
   557             gl_render_tilelist_depthonly(segment->opaque_ptr);
   558             gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
   559             glClear( GL_DEPTH_BUFFER_BIT );
   560             glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
   561         }
   562         gl_render_tilelist(segment->opaque_ptr,0);
   563         if( IS_TILE_PTR(segment->punchout_ptr) ) {
   564             glEnable(GL_ALPHA_TEST );
   565             gl_render_tilelist(segment->punchout_ptr, GL_GEQUAL );
   566             glDisable(GL_ALPHA_TEST );
   567         }
   568         glDisable( GL_STENCIL_TEST );
   569         glStencilMask(0x03);
   570         glClear( GL_STENCIL_BUFFER_BIT );
   572         if( IS_TILE_PTR(segment->trans_ptr) ) {
   573             if( pvr2_scene.sort_mode == SORT_NEVER || 
   574                     (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
   575                 gl_render_tilelist(segment->trans_ptr, 0);
   576             } else {
   577                 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
   578             }
   579         }
   580     } while( !IS_LAST_SEGMENT(segment++) );
   581     glDisable( GL_SCISSOR_TEST );
   583     glsl_enable_shaders(FALSE);
   585     gettimeofday( &end_tv, NULL );
   586     ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
   587     (end_tv.tv_usec - tex_tv.tv_usec)/1000;
   588     DEBUG( "Scene render in %dms", ms );
   589 }
.