Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 1219:3966d3e55351
prev1207:f7ca985659c6
next1232:e5b12e2fe6ba
author nkeynes
date Mon Feb 13 21:43:22 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Remove unused pvr2_poly_texblend array
Support GL_MAX_TEXTURE_UNITS and GL_MAX_IMAGE_TEXTURE_UNITS
Protected against undefined GL_STACK_OVERFLOW
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_EMPTY_TILE_LIST(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, float *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     glBegin( GL_QUADS );
    77     glVertex3f( tile_bounds[0], tile_bounds[2], z );
    78     glVertex3f( tile_bounds[1], tile_bounds[2], z );
    79     glVertex3f( tile_bounds[1], tile_bounds[3], z );
    80     glVertex3f( tile_bounds[0], tile_bounds[3], z );
    81     glEnd();
    82 }
    84 static void pvr2_scene_load_textures()
    85 {
    86     int i;
    88     texcache_begin_scene( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03,
    89                          (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5 );
    91     for( i=0; i < pvr2_scene.poly_count; i++ ) {
    92         struct polygon_struct *poly = &pvr2_scene.poly_array[i];
    93         if( POLY1_TEXTURED(poly->context[0]) ) {
    94             poly->tex_id = texcache_get_texture( poly->context[1], poly->context[2] );
    95             if( poly->mod_vertex_index != -1 ) {
    96                 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
    97                     poly->mod_tex_id = texcache_get_texture( poly->context[3], poly->context[4] );
    98                 } else {
    99                     poly->mod_tex_id = poly->tex_id;
   100                 }
   101             }
   102         } else {
   103             poly->tex_id = 0;
   104             poly->mod_tex_id = 0;
   105         }
   106     }
   107 }
   110 /**
   111  * Once-off call to setup the OpenGL context.
   112  */
   113 void pvr2_setup_gl_context()
   114 {
   116     if( glsl_is_supported() && isGLMultitextureSupported() ) {
   117         if( !glsl_load_shaders( ) ) {
   118             WARN( "Unable to load GL shaders" );
   119         } else {
   120             have_shaders = TRUE;
   121         }
   122     }
   124 #ifdef APPLE_BUILD
   125     CGL_MACRO_CONTEXT = CGLGetCurrentContext();
   126 #endif
   127     texcache_gl_init(); // Allocate texture IDs
   128     glDisable( GL_CULL_FACE );
   129     glEnable( GL_BLEND );
   130     glEnable( GL_DEPTH_TEST );
   131     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
   132     glMatrixMode(GL_MODELVIEW);
   133     glLoadIdentity();
   135 #ifdef HAVE_OPENGL_CLAMP_COLOR
   136     if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
   137         glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
   138         glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
   139     }
   140 #endif
   142     glEnableClientState( GL_COLOR_ARRAY );
   143     glEnableClientState( GL_VERTEX_ARRAY );
   144     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
   145     glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
   146     glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
   148     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   149     glClearDepth(0);
   150     glClearStencil(0);
   152     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
   153     glFogi(GL_FOG_MODE, GL_LINEAR);
   154     glFogf(GL_FOG_START, 0.0);
   155     glFogf(GL_FOG_END, 1.0);
   157     if( have_shaders ) {
   158         glsl_use_pvr2_shader();
   159         glsl_set_pvr2_shader_primary_texture(0);
   160         glsl_set_pvr2_shader_palette_texture(1);
   161         glsl_clear_shader();
   162     }
   163 }
   165 /**
   166  * Setup the basic context that's shared between normal and modified modes -
   167  * depth, culling
   168  */
   169 static void render_set_base_context( uint32_t poly1, gboolean set_depth )
   170 {
   171     if( set_depth ) {
   172         glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   173     }
   175     glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
   176 }
   178 /**
   179  * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
   180  */
   181 static void render_set_tsp_context( uint32_t poly1, uint32_t poly2 )
   182 {
   183     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   185     if( !have_shaders ) {
   186         if( POLY1_TEXTURED(poly1) ) {
   187             if( POLY2_TEX_BLEND(poly2) == 2 )
   188                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
   189             else
   190                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
   192         }
   193     }
   195      switch( POLY2_FOG_MODE(poly2) ) {
   196      case PVR2_POLY_FOG_LOOKUP:
   197          glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
   198          break;
   199      case PVR2_POLY_FOG_VERTEX:
   200          glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
   201          break;
   202      }
   204      int srcblend = POLY2_SRC_BLEND(poly2);
   205      int destblend = POLY2_DEST_BLEND(poly2);
   206      glBlendFunc( srcblend, destblend );
   208      if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
   209          WARN( "Accumulation buffer not supported" );
   210      }   
   211 }
   213 /**
   214  * Setup the GL context for the supplied polygon context.
   215  * @param context pointer to 3 or 5 words of polygon context
   216  * @param depth_mode force depth mode, or 0 to use the polygon's
   217  * depth mode.
   218  */
   219 static void render_set_context( uint32_t *context, gboolean set_depth )
   220 {
   221     render_set_base_context(context[0], set_depth);
   222     render_set_tsp_context(context[0],context[1]);
   223 }
   225 static inline void gl_draw_vertexes( struct polygon_struct *poly )
   226 {
   227     do {
   228         glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
   229         poly = poly->sub_next;
   230     } while( poly != NULL );
   231 }
   233 static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
   234 {
   235     do {
   236         glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
   237         poly = poly->sub_next;
   238     } while( poly != NULL );
   239 }
   241 static void gl_render_poly( struct polygon_struct *poly, gboolean set_depth)
   242 {
   243     if( poly->vertex_count == 0 )
   244         return; /* Culled */
   246     bind_texture(poly->tex_id);
   247     if( poly->mod_vertex_index == -1 ) {
   248         render_set_context( poly->context, set_depth );
   249         gl_draw_vertexes(poly);
   250     }  else {
   251         glEnable( GL_STENCIL_TEST );
   252         render_set_base_context( poly->context[0], set_depth );
   253         render_set_tsp_context( poly->context[0], poly->context[1] );
   254         glStencilFunc(GL_EQUAL, 0, 2);
   255         gl_draw_vertexes(poly);
   257         if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
   258             bind_texture(poly->mod_tex_id);
   259             render_set_tsp_context( poly->context[0], poly->context[3] );
   260         }
   261         glStencilFunc(GL_EQUAL, 2, 2);
   262         gl_draw_mod_vertexes(poly);
   263         glDisable( GL_STENCIL_TEST );
   264     }
   265 }
   268 static void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
   269 {
   270     /* A bit of explanation:
   271      * In theory it works like this: generate a 1-bit stencil for each polygon
   272      * volume, and then AND or OR it against the overall 1-bit tile stencil at 
   273      * the end of the volume. 
   274      * 
   275      * The implementation here uses a 2-bit stencil buffer, where each volume
   276      * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
   277      * to update bit 1 accordingly and clear bit 0.
   278      * 
   279      * This could probably be more efficient, but at least it works correctly 
   280      * now :)
   281      */
   283     if( poly->vertex_count == 0 )
   284         return; /* Culled */
   286     gl_draw_vertexes(poly);
   290     int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
   291     if( poly_type == PVR2_VOLUME_REGION0 ) {
   292         /* 00 => 00
   293          * 01 => 00
   294          * 10 => 10
   295          * 11 => 00
   296          */
   297         glStencilMask( 0x03 );
   298         glStencilFunc(GL_EQUAL, 0x02, 0x03);
   299         glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
   300         glDisable( GL_DEPTH_TEST );
   302         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   304         glEnable( GL_DEPTH_TEST );
   305         glStencilMask( 0x01 );
   306         glStencilFunc( GL_ALWAYS, 0, 1 );
   307         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
   308     } else if( poly_type == PVR2_VOLUME_REGION1 ) {
   309         /* This is harder with the standard stencil ops - do it in two passes
   310          * 00 => 00 | 00 => 10
   311          * 01 => 10 | 01 => 10
   312          * 10 => 10 | 10 => 00
   313          * 11 => 10 | 11 => 10
   314          */
   315         glStencilMask( 0x02 );
   316         glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
   317         glDisable( GL_DEPTH_TEST );
   319         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   321         glStencilMask( 0x03 );
   322         glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
   323         glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
   325         drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
   327         glEnable( GL_DEPTH_TEST );
   328         glStencilMask( 0x01 );
   329         glStencilFunc( GL_ALWAYS, 0, 1 );
   330         glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );         
   331     }
   332 }
   334 static void gl_render_bkgnd( struct polygon_struct *poly )
   335 {
   336     bind_texture(poly->tex_id);
   337     render_set_tsp_context( poly->context[0], poly->context[1] );
   338     glDisable( GL_DEPTH_TEST );
   339     glBlendFunc( GL_ONE, GL_ZERO );
   340     gl_draw_vertexes(poly);
   341     glEnable( GL_DEPTH_TEST );
   342 }
   344 void gl_render_triangle( struct polygon_struct *poly, int index )
   345 {
   346     bind_texture(poly->tex_id);
   347     render_set_tsp_context( poly->context[0], poly->context[1] );
   348     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index + index, 3 );
   350 }
   352 void gl_render_tilelist( pvraddr_t tile_entry, gboolean set_depth )
   353 {
   354     tileentryiter list;
   356     FOREACH_TILEENTRY(list, tile_entry) {
   357         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   358         if( poly != NULL ) {
   359             do {
   360                 gl_render_poly(poly, set_depth);
   361                 poly = poly->next;
   362             } while( list.strip_count-- > 0 );
   363         }
   364     }
   365 }
   367 /**
   368  * Render the tilelist with depthbuffer updates only.
   369  */
   370 static void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
   371 {
   372     tileentryiter list;
   374     FOREACH_TILEENTRY(list, tile_entry) {
   375         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   376         if( poly != NULL ) {
   377             do {
   378                 render_set_base_context(poly->context[0],TRUE);
   379                 gl_draw_vertexes(poly);
   380                 poly = poly->next;
   381             } while( list.strip_count-- > 0 );
   382         }
   383     }
   384 }
   386 static void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
   387 {
   388     tileentryiter list;
   390     if( !IS_TILE_PTR(tile_entry) )
   391         return;
   393     glEnable( GL_STENCIL_TEST );
   394     glEnable( GL_DEPTH_TEST );
   395     glStencilFunc( GL_ALWAYS, 0, 1 );
   396     glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP ); 
   397     glStencilMask( 0x01 );
   398     glDepthFunc( GL_LEQUAL );
   399     glDepthMask( GL_FALSE );
   401     FOREACH_TILEENTRY(list, tile_entry ) {
   402         struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
   403         if( poly != NULL ) {
   404             do {
   405                 gl_render_modifier_polygon( poly, tile_bounds );
   406                 poly = poly->next;
   407             } while( list.strip_count-- > 0 );
   408         }
   409     }
   410     glDepthMask( GL_TRUE );
   411     glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
   412     glDisable( GL_STENCIL_TEST );
   413 }
   415 /**
   416  * Render the currently defined scene in pvr2_scene
   417  */
   418 void pvr2_scene_render( render_buffer_t buffer )
   419 {
   420     /* Scene setup */
   421     struct timeval start_tv, tex_tv, end_tv;
   423     gettimeofday(&start_tv, NULL);
   424     display_driver->set_render_target(buffer);
   425     pvr2_check_palette_changed();
   426     pvr2_scene_load_textures();
   427     currentTexId = -1;
   429     gettimeofday( &tex_tv, NULL );
   430     uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
   431     (tex_tv.tv_usec - start_tv.tv_usec)/1000;
   432     DEBUG( "Scene setup in %dms", ms );
   434     /* Setup view projection matrix */
   435     glMatrixMode(GL_PROJECTION);
   436     glLoadIdentity();
   437     float nearz = pvr2_scene.bounds[4];
   438     float farz = pvr2_scene.bounds[5];
   439     if( nearz == farz ) {
   440         farz*= 4.0;
   441     }
   442     glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0, 
   443              -farz, -nearz );
   444     float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
   445     glAlphaFunc( GL_GEQUAL, alphaRef );
   447     /* Clear the buffer (FIXME: May not want always want to do this) */
   448     glDisable( GL_SCISSOR_TEST );
   449     glDepthMask( GL_TRUE );
   450     glStencilMask( 0x03 );
   451     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
   453     /* Setup vertex array pointers */
   454     glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
   455     glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
   456     glTexCoordPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
   457     glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
   458     glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
   459     /* Turn on the shaders (if available) */
   460     glsl_use_pvr2_shader();
   462     /* Render the background */
   463     gl_render_bkgnd( pvr2_scene.bkgnd_poly );
   465     glEnable( GL_SCISSOR_TEST );
   466     glEnable( GL_COLOR_SUM );
   467     glEnable( GL_FOG );
   468     glEnable( GL_TEXTURE_2D );
   470     /* Process the segment list */
   471     struct tile_segment *segment = pvr2_scene.segment_list;
   472     do {
   473         int tilex = SEGMENT_X(segment->control);
   474         int tiley = SEGMENT_Y(segment->control);
   476         uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
   477         if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
   478             continue; // fully clipped, skip tile
   479         }
   481         /* Clip to the visible part of the tile */
   482         glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], 
   483                    tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
   484         if( display_driver->capabilities.stencil_bits >= 2 && 
   485                 IS_TILE_PTR(segment->opaquemod_ptr) &&
   486                 !IS_EMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
   487             /* Don't do this unless there's actually some shadow polygons */
   489             /* Use colormask instead of drawbuffer for simplicity */
   490             glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
   491             gl_render_tilelist_depthonly(segment->opaque_ptr);
   492             gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
   493             glClear( GL_DEPTH_BUFFER_BIT );
   494             glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
   495         }
   496         gl_render_tilelist(segment->opaque_ptr,TRUE);
   497         if( IS_TILE_PTR(segment->punchout_ptr) ) {
   498             glEnable(GL_ALPHA_TEST );
   499             glDepthFunc(GL_GEQUAL);
   500             gl_render_tilelist(segment->punchout_ptr, FALSE );
   501             glDisable(GL_ALPHA_TEST );
   502         }
   504         if( IS_TILE_PTR(segment->trans_ptr) ) {
   505             if( pvr2_scene.sort_mode == SORT_NEVER || 
   506                     (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
   507                 gl_render_tilelist(segment->trans_ptr, TRUE);
   508             } else {
   509                 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
   510             }
   511         }
   512     } while( !IS_LAST_SEGMENT(segment++) );
   513     glDisable( GL_SCISSOR_TEST );
   514     glDisable( GL_COLOR_SUM );
   515     glDisable( GL_FOG );
   516     glsl_clear_shader();
   518     pvr2_scene_finished();
   520     gettimeofday( &end_tv, NULL );
   521     ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
   522     (end_tv.tv_usec - tex_tv.tv_usec)/1000;
   523     DEBUG( "Scene render in %dms", ms );
   524 }
.