Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 1156:d124a1c833cb
prev1154:5225c7c059ce
next1159:580436b01b6c
author nkeynes
date Wed Jan 19 10:45:06 2011 +1000 (9 years ago)
permissions -rw-r--r--
last change Add apple-specific GL context optimization
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"
    28 #ifdef APPLE_BUILD
    29 #include "OpenGL/CGLCurrent.h"
    30 #include "OpenGL/CGLMacro.h"
    32 static CGLContextObj CGL_MACRO_CONTEXT;
    33 #endif
    35 #define IS_EMPTY_TILE_LIST(p) ((*((uint32_t *)(pvr2_main_ram+(p))) >> 28) == 0x0F)
    37 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
    38         GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, 
    39         GL_ALWAYS };
    40 int pvr2_poly_srcblend[8] = { 
    41         GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
    42         GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, 
    43         GL_ONE_MINUS_DST_ALPHA };
    44 int pvr2_poly_dstblend[8] = {
    45         GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
    46         GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
    47         GL_ONE_MINUS_DST_ALPHA };
    48 int pvr2_poly_texblend[4] = {
    49         GL_REPLACE, 
    50         GL_MODULATE,  
    51         GL_DECAL, 
    52         GL_MODULATE 
    53 };
    55 static gboolean have_shaders = FALSE;
    56 static int currentTexId = -1;
    58 static inline void bind_texture(int texid)
    59 {
    60     if( currentTexId != texid ) {
    61         currentTexId = texid;
    62         glBindTexture(GL_TEXTURE_2D, texid);
    63     }
    64 }
    66 /**
    67  * Clip the tile bounds to the clipping plane. 
    68  * @return TRUE if the tile was not clipped completely.
    69  */
    70 static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
    71 {
    72     if( tile[0] < clip[0] ) tile[0] = clip[0];
    73     if( tile[1] > clip[1] ) tile[1] = clip[1];
    74     if( tile[2] < clip[2] ) tile[2] = clip[2];
    75     if( tile[3] > clip[3] ) tile[3] = clip[3];
    76     return tile[0] < tile[1] && tile[2] < tile[3];
    77 }
    79 static void drawrect2d( uint32_t tile_bounds[], float z )
    80 {
    81     glBegin( GL_QUADS );
    82     glVertex3f( tile_bounds[0], tile_bounds[2], z );
    83     glVertex3f( tile_bounds[1], tile_bounds[2], z );
    84     glVertex3f( tile_bounds[1], tile_bounds[3], z );
    85     glVertex3f( tile_bounds[0], tile_bounds[3], z );
    86     glEnd();
    87 }
    89 static void pvr2_scene_load_textures()
    90 {
    91     int i;
    93     texcache_begin_scene( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03,
    94                          (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5 );
    96     for( i=0; i < pvr2_scene.poly_count; i++ ) {
    97         struct polygon_struct *poly = &pvr2_scene.poly_array[i];
    98         if( POLY1_TEXTURED(poly->context[0]) ) {
    99             poly->tex_id = texcache_get_texture( poly->context[1], poly->context[2] );
   100             if( poly->mod_vertex_index != -1 ) {
   101                 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
   102                     poly->mod_tex_id = texcache_get_texture( poly->context[3], poly->context[4] );
   103                 } else {
   104                     poly->mod_tex_id = poly->tex_id;
   105                 }
   106             }
   107         } else {
   108             poly->tex_id = 0;
   109             poly->mod_tex_id = 0;
   110         }
   111     }
   112 }
   115 /**
   116  * Once-off call to setup the OpenGL context.
   117  */
   118 void pvr2_setup_gl_context()
   119 {
   121     if( glsl_is_supported() && isGLMultitextureSupported() ) {
   122         if( !glsl_load_shaders( ) ) {
   123             WARN( "Unable to load GL shaders" );
   124         } else {
   125             have_shaders = TRUE;
   126         }
   127     }
   129 #ifdef APPLE_BUILD
   130     CGL_MACRO_CONTEXT = CGLGetCurrentContext();
   131 #endif
   132     texcache_gl_init(); // Allocate texture IDs
   133     glDisable( GL_CULL_FACE );
   134     glEnable( GL_BLEND );
   135     glEnable( GL_DEPTH_TEST );
   136     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
   137     glMatrixMode(GL_MODELVIEW);
   138     glLoadIdentity();
   140 #ifdef HAVE_OPENGL_CLAMP_COLOR
   141     if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
   142         glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
   143         glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
   144     }
   145 #endif
   147     glEnableClientState( GL_COLOR_ARRAY );
   148     glEnableClientState( GL_VERTEX_ARRAY );
   149     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
   150     glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
   151     glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
   153     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   154     glClearDepth(0);
   155     glClearStencil(0);
   157     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
   158     glFogi(GL_FOG_MODE, GL_LINEAR);
   159     glFogf(GL_FOG_START, 0.0);
   160     glFogf(GL_FOG_END, 1.0);
   162     if( have_shaders ) {
   163         glsl_set_shader(DEFAULT_PROGRAM);
   164         glsl_set_uniform_int(DEFAULT_PROGRAM, "primary_texture", 0);
   165         glsl_set_uniform_int(DEFAULT_PROGRAM, "palette_texture", 1);
   166         glsl_clear_shader();
   167     }
   168 }
   170 /**
   171  * Setup the basic context that's shared between normal and modified modes -
   172  * depth, culling
   173  */
   174 static void render_set_base_context( uint32_t poly1, gboolean set_depth )
   175 {
   176     if( set_depth ) {
   177         glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   178     }
   180     glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
   181 }
   183 /**
   184  * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
   185  */
   186 static void render_set_tsp_context( uint32_t poly1, uint32_t poly2 )
   187 {
   188     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   190     if( POLY1_TEXTURED(poly1) && !have_shaders ) {
   191         if( POLY2_TEX_BLEND(poly2) == 2 )
   192             glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
   193         else
   194             glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
   196      }
   198      switch( POLY2_FOG_MODE(poly2) ) {
   199      case PVR2_POLY_FOG_LOOKUP:
   200          glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
   201          break;
   202      case PVR2_POLY_FOG_VERTEX:
   203          glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
   204          break;
   205      }
   207      int srcblend = POLY2_SRC_BLEND(poly2);
   208      int destblend = POLY2_DEST_BLEND(poly2);
   209      glBlendFunc( srcblend, destblend );
   211      if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
   212          WARN( "Accumulation buffer not supported" );
   213      }   
   214 }
   216 /**
   217  * Setup the GL context for the supplied polygon context.
   218  * @param context pointer to 3 or 5 words of polygon context
   219  * @param depth_mode force depth mode, or 0 to use the polygon's
   220  * depth mode.
   221  */
   222 static void render_set_context( uint32_t *context, gboolean set_depth )
   223 {
   224     render_set_base_context(context[0], set_depth);
   225     render_set_tsp_context(context[0],context[1]);
   226 }
   228 static inline void gl_draw_vertexes( struct polygon_struct *poly )
   229 {
   230     do {
   231         glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
   232         poly = poly->sub_next;
   233     } while( poly != NULL );
   234 }
   236 static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
   237 {
   238     do {
   239         glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
   240         poly = poly->sub_next;
   241     } while( poly != NULL );
   242 }
   244 static void gl_render_poly( struct polygon_struct *poly, gboolean set_depth)
   245 {
   246     if( poly->vertex_count == 0 )
   247         return; /* Culled */
   249     bind_texture(poly->tex_id);
   250     if( poly->mod_vertex_index == -1 ) {
   251         render_set_context( poly->context, set_depth );
   252         gl_draw_vertexes(poly);
   253     }  else {
   254         glEnable( GL_STENCIL_TEST );
   255         render_set_base_context( poly->context[0], set_depth );
   256         render_set_tsp_context( poly->context[0], poly->context[1] );
   257         glStencilFunc(GL_EQUAL, 0, 2);
   258         gl_draw_vertexes(poly);
   260         if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
   261             bind_texture(poly->mod_tex_id);
   262             render_set_tsp_context( poly->context[0], poly->context[3] );
   263         }
   264         glStencilFunc(GL_EQUAL, 2, 2);
   265         gl_draw_mod_vertexes(poly);
   266         glDisable( GL_STENCIL_TEST );
   267     }
   268 }
   271 static void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
   272 {
   273     /* A bit of explanation:
   274      * In theory it works like this: generate a 1-bit stencil for each polygon
   275      * volume, and then AND or OR it against the overall 1-bit tile stencil at 
   276      * the end of the volume. 
   277      * 
   278      * The implementation here uses a 2-bit stencil buffer, where each volume
   279      * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
   280      * to update bit 1 accordingly and clear bit 0.
   281      * 
   282      * This could probably be more efficient, but at least it works correctly 
   283      * now :)
   284      */
   286     if( poly->vertex_count == 0 )
   287         return; /* Culled */
   289     gl_draw_vertexes(poly);
   293     int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
   294     if( poly_type == PVR2_VOLUME_REGION0 ) {
   295         /* 00 => 00
   296          * 01 => 00
   297          * 10 => 10
   298          * 11 => 00
   299          */
   300         glStencilMask( 0x03 );
   301         glStencilFunc(GL_EQUAL, 0x02, 0x03);
   302         glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
   303         glDisable( GL_DEPTH_TEST );
   305         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   307         glEnable( GL_DEPTH_TEST );
   308         glStencilMask( 0x01 );
   309         glStencilFunc( GL_ALWAYS, 0, 1 );
   310         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
   311     } else if( poly_type == PVR2_VOLUME_REGION1 ) {
   312         /* This is harder with the standard stencil ops - do it in two passes
   313          * 00 => 00 | 00 => 10
   314          * 01 => 10 | 01 => 10
   315          * 10 => 10 | 10 => 00
   316          * 11 => 10 | 11 => 10
   317          */
   318         glStencilMask( 0x02 );
   319         glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
   320         glDisable( GL_DEPTH_TEST );
   322         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   324         glStencilMask( 0x03 );
   325         glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
   326         glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
   328         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   330         glEnable( GL_DEPTH_TEST );
   331         glStencilMask( 0x01 );
   332         glStencilFunc( GL_ALWAYS, 0, 1 );
   333         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );         
   334     }
   335 }
   337 static void gl_render_bkgnd( struct polygon_struct *poly )
   338 {
   339     bind_texture(poly->tex_id);
   340     render_set_tsp_context( poly->context[0], poly->context[1] );
   341     glDisable( GL_DEPTH_TEST );
   342     glBlendFunc( GL_ONE, GL_ZERO );
   343     gl_draw_vertexes(poly);
   344     glEnable( GL_DEPTH_TEST );
   345 }
   347 void gl_render_triangle( struct polygon_struct *poly, int index )
   348 {
   349     bind_texture(poly->tex_id);
   350     render_set_tsp_context( poly->context[0], poly->context[1] );
   351     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index + index, 3 );
   353 }
   355 void gl_render_tilelist( pvraddr_t tile_entry, gboolean set_depth )
   356 {
   357     tileentryiter list;
   359     FOREACH_TILEENTRY(list, tile_entry) {
   360         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   361         if( poly != NULL ) {
   362             do {
   363                 gl_render_poly(poly, set_depth);
   364                 poly = poly->next;
   365             } while( list.strip_count-- > 0 );
   366         }
   367     }
   368 }
   370 /**
   371  * Render the tilelist with depthbuffer updates only.
   372  */
   373 static void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
   374 {
   375     tileentryiter list;
   377     FOREACH_TILEENTRY(list, tile_entry) {
   378         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   379         if( poly != NULL ) {
   380             do {
   381                 render_set_base_context(poly->context[0],TRUE);
   382                 gl_draw_vertexes(poly);
   383                 poly = poly->next;
   384             } while( list.strip_count-- > 0 );
   385         }
   386     }
   387 }
   389 static void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
   390 {
   391     tileentryiter list;
   393     if( !IS_TILE_PTR(tile_entry) )
   394         return;
   396     glEnable( GL_STENCIL_TEST );
   397     glEnable( GL_DEPTH_TEST );
   398     glStencilFunc( GL_ALWAYS, 0, 1 );
   399     glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
   400     glStencilMask( 0x01 );
   401     glDepthFunc( GL_LEQUAL );
   402     glDepthMask( GL_FALSE );
   404     FOREACH_TILEENTRY(list, tile_entry ) {
   405         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   406         if( poly != NULL ) {
   407             do {
   408                 gl_render_modifier_polygon( poly, tile_bounds );
   409                 poly = poly->next;
   410             } while( list.strip_count-- > 0 );
   411         }
   412     }
   413     glDepthMask( GL_TRUE );
   414     glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
   415     glDisable( GL_STENCIL_TEST );
   416 }
   418 /**
   419  * Render the currently defined scene in pvr2_scene
   420  */
   421 void pvr2_scene_render( render_buffer_t buffer )
   422 {
   423     /* Scene setup */
   424     struct timeval start_tv, tex_tv, end_tv;
   426     gettimeofday(&start_tv, NULL);
   427     display_driver->set_render_target(buffer);
   428     pvr2_check_palette_changed();
   429     pvr2_scene_load_textures();
   430     currentTexId = -1;
   432     gettimeofday( &tex_tv, NULL );
   433     uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
   434     (tex_tv.tv_usec - start_tv.tv_usec)/1000;
   435     DEBUG( "Scene setup in %dms", ms );
   437     /* Setup view projection matrix */
   438     glMatrixMode(GL_PROJECTION);
   439     glLoadIdentity();
   440     float nearz = pvr2_scene.bounds[4];
   441     float farz = pvr2_scene.bounds[5];
   442     if( nearz == farz ) {
   443         farz*= 4.0;
   444     }
   445     glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0, 
   446              -farz, -nearz );
   447     float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
   448     glAlphaFunc( GL_GEQUAL, alphaRef );
   450     /* Clear the buffer (FIXME: May not want always want to do this) */
   451     glDisable( GL_SCISSOR_TEST );
   452     glDepthMask( GL_TRUE );
   453     glStencilMask( 0x03 );
   454     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
   456     /* Setup vertex array pointers */
   457     glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
   458     glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
   459     glTexCoordPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
   460     glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
   461     glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
   462     /* Turn on the shaders (if available) */
   463     glsl_set_shader(DEFAULT_PROGRAM);
   465     /* Render the background */
   466     gl_render_bkgnd( pvr2_scene.bkgnd_poly );
   468     glEnable( GL_SCISSOR_TEST );
   469     glEnable( GL_COLOR_SUM );
   470     glEnable( GL_FOG );
   471     glEnable( GL_TEXTURE_2D );
   473     /* Process the segment list */
   474     struct tile_segment *segment = pvr2_scene.segment_list;
   475     do {
   476         int tilex = SEGMENT_X(segment->control);
   477         int tiley = SEGMENT_Y(segment->control);
   479         uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
   480         if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
   481             continue; // fully clipped, skip tile
   482         }
   484         /* Clip to the visible part of the tile */
   485         glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], 
   486                    tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
   487         if( display_driver->capabilities.stencil_bits >= 2 && 
   488                 IS_TILE_PTR(segment->opaquemod_ptr) &&
   489                 !IS_EMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
   490             /* Don't do this unless there's actually some shadow polygons */
   492             /* Use colormask instead of drawbuffer for simplicity */
   493             glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
   494             gl_render_tilelist_depthonly(segment->opaque_ptr);
   495             gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
   496             glClear( GL_DEPTH_BUFFER_BIT );
   497             glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
   498         }
   499         gl_render_tilelist(segment->opaque_ptr,TRUE);
   500         if( IS_TILE_PTR(segment->punchout_ptr) ) {
   501             glEnable(GL_ALPHA_TEST );
   502             glDepthFunc(GL_GEQUAL);
   503             gl_render_tilelist(segment->punchout_ptr, FALSE );
   504             glDisable(GL_ALPHA_TEST );
   505         }
   507         if( IS_TILE_PTR(segment->trans_ptr) ) {
   508             if( pvr2_scene.sort_mode == SORT_NEVER || 
   509                     (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
   510                 gl_render_tilelist(segment->trans_ptr, TRUE);
   511             } else {
   512                 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
   513             }
   514         }
   515     } while( !IS_LAST_SEGMENT(segment++) );
   516     glDisable( GL_SCISSOR_TEST );
   517     glDisable( GL_COLOR_SUM );
   518     glDisable( GL_FOG );
   519     glsl_clear_shader();
   521     gettimeofday( &end_tv, NULL );
   522     ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
   523     (end_tv.tv_usec - tex_tv.tv_usec)/1000;
   524     DEBUG( "Scene render in %dms", ms );
   525 }
.