Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 856:02ac5f37bfc9
prev847:2089244671d2
next863:a5e5310061e2
author nkeynes
date Sun Sep 28 00:30:45 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Add missing declaration for pvr2_finish_render_buffer
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 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
    28         GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, 
    29         GL_ALWAYS };
    30 int pvr2_poly_srcblend[8] = { 
    31         GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
    32         GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, 
    33         GL_ONE_MINUS_DST_ALPHA };
    34 int pvr2_poly_dstblend[8] = {
    35         GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
    36         GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
    37         GL_ONE_MINUS_DST_ALPHA };
    38 int pvr2_poly_texblend[4] = {
    39         GL_REPLACE, 
    40         GL_MODULATE,  
    41         GL_DECAL, 
    42         GL_MODULATE 
    43 };
    45 /**
    46  * Clip the tile bounds to the clipping plane. 
    47  * @return TRUE if the tile was not clipped completely.
    48  */
    49 static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
    50 {
    51     if( tile[0] < clip[0] ) tile[0] = clip[0];
    52     if( tile[1] > clip[1] ) tile[1] = clip[1];
    53     if( tile[2] < clip[2] ) tile[2] = clip[2];
    54     if( tile[3] > clip[3] ) tile[3] = clip[3];
    55     return tile[0] < tile[1] && tile[2] < tile[3];
    56 }
    58 void pvr2_scene_load_textures()
    59 {
    60     int i;
    61     for( i=0; i < pvr2_scene.poly_count; i++ ) {
    62         struct polygon_struct *poly = &pvr2_scene.poly_array[i];
    63         if( POLY1_TEXTURED(poly->context[0]) ) {
    64             poly->tex_id = texcache_get_texture( poly->context[2],
    65                     POLY2_TEX_WIDTH(poly->context[1]),
    66                     POLY2_TEX_HEIGHT(poly->context[1]) );
    67             if( poly->mod_vertex_index != -1 ) {
    68                 poly->mod_tex_id = texcache_get_texture( poly->context[4],
    69                         POLY2_TEX_WIDTH(poly->context[3]),
    70                         POLY2_TEX_HEIGHT(poly->context[3]) );
    71             }
    72         } else {
    73             poly->tex_id = -1;
    74             poly->mod_tex_id = -1;
    75         }
    76     }
    77 }
    81 /**
    82  * Once-off call to setup the OpenGL context.
    83  */
    84 void pvr2_setup_gl_context()
    85 {
    87     if( glsl_is_supported() ) {
    88         if( !glsl_load_shaders( glsl_vertex_shader_src, NULL ) ) {
    89             WARN( "Unable to load GL shaders" );
    90         }
    91     }
    93     texcache_gl_init(); // Allocate texture IDs
    94     glCullFace( GL_BACK );
    95     glEnable( GL_BLEND );
    96     glEnable( GL_DEPTH_TEST );
    97     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    98     glMatrixMode(GL_MODELVIEW);
    99     glLoadIdentity();
   101 #ifdef HAVE_OPENGL_CLAMP_COLOR
   102     if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
   103         glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
   104         glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
   105     }
   106 #endif
   108     glEnableClientState( GL_COLOR_ARRAY );
   109     glEnableClientState( GL_VERTEX_ARRAY );
   110     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
   111     glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
   112     glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
   114     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   115     glClearDepth(0);
   116     glClearStencil(0);
   117 }
   119 /**
   120  * Setup the GL context for the supplied polygon context.
   121  * @param context pointer to 3 or 5 words of polygon context
   122  * @param modified boolean flag indicating that the modified
   123  *  version should be used, rather than the normal version.
   124  */
   125 void render_set_context( uint32_t *context, int render_mode )
   126 {
   127     uint32_t poly1 = context[0], poly2, texture;
   128     if( render_mode == RENDER_FULLMOD ) {
   129         poly2 = context[3];
   130         texture = context[4];
   131     } else {
   132         poly2 = context[1];
   133         texture = context[2];
   134     }
   136     glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   137     glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
   139     switch( POLY1_CULL_MODE(poly1) ) {
   140     case CULL_NONE:
   141     case CULL_SMALL:
   142         glDisable( GL_CULL_FACE );
   143         break;
   144     case CULL_CCW:
   145         glEnable( GL_CULL_FACE );
   146         glFrontFace( GL_CW );
   147         break;
   148     case CULL_CW:
   149         glEnable( GL_CULL_FACE );
   150         glFrontFace( GL_CCW );
   151         break;
   152     }
   154     if( POLY1_SPECULAR(poly1) ) {
   155         glEnable(GL_COLOR_SUM);
   156     } else {
   157         glDisable(GL_COLOR_SUM);
   158     }
   161     if( POLY1_TEXTURED(poly1) ) {
   162         glEnable(GL_TEXTURE_2D);
   163         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
   164         if( POLY2_TEX_CLAMP_U(poly2) ) {
   165             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
   166         } else if( POLY2_TEX_MIRROR_U(poly2) ) {
   167             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB );
   168         } else {
   169             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
   170         }	    
   171         if( POLY2_TEX_CLAMP_V(poly2) ) {
   172             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
   173         } else if( POLY2_TEX_MIRROR_V(poly2) ) {
   174             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB );
   175         } else {
   176             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
   177         }
   178     } else {
   179         glDisable( GL_TEXTURE_2D );
   180     }
   182     switch( POLY2_FOG_MODE(poly2) ) {
   183     case PVR2_POLY_FOG_LOOKUP:
   184         glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
   185         glEnable( GL_FOG );
   186         break;
   187     case PVR2_POLY_FOG_VERTEX:
   188         if( POLY1_SPECULAR(poly1) ) {
   189             glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
   190             glEnable( GL_FOG );
   191             break;
   192         } /* else fallthrough */
   193     default:
   194         glDisable( GL_FOG );
   195     }
   198     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   200     int srcblend = POLY2_SRC_BLEND(poly2);
   201     int destblend = POLY2_DEST_BLEND(poly2);
   202     glBlendFunc( srcblend, destblend );
   204     if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
   205         ERROR( "Accumulation buffer not supported" );
   206     }
   208 }
   211 static void gl_render_poly( struct polygon_struct *poly )
   212 {
   213     if( poly->tex_id != -1 ) {
   214         glBindTexture(GL_TEXTURE_2D, poly->tex_id);
   215     }
   216     render_set_context( poly->context, RENDER_NORMAL );
   217     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   218 }
   221 static void gl_render_bkgnd( struct polygon_struct *poly )
   222 {
   223     if( poly->tex_id != -1 ) {
   224         glBindTexture(GL_TEXTURE_2D, poly->tex_id);
   225     }
   226     render_set_context( poly->context, RENDER_NORMAL );
   227     glDisable( GL_DEPTH_TEST );
   228     glDisable( GL_CULL_FACE );
   229     glBlendFunc( GL_ONE, GL_ZERO );
   230     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   231     glEnable( GL_CULL_FACE );
   232     glEnable( GL_DEPTH_TEST );
   233 }
   237 void gl_render_tilelist( pvraddr_t tile_entry )
   238 {
   239     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   240     int strip_count;
   241     struct polygon_struct *poly;
   243     while(1) {
   244         uint32_t entry = *tile_list++;
   245         switch( entry >> 28 ) {
   246         case 0x0F:
   247             return; // End-of-list
   248         case 0x0E:
   249             tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   250             break;
   251         case 0x08: case 0x09: case 0x0A: case 0x0B:
   252             strip_count = ((entry >> 25) & 0x0F)+1;
   253             poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   254             while( strip_count > 0 ) {
   255                 assert( poly != NULL );
   256                 gl_render_poly( poly );
   257                 poly = poly->next;
   258                 strip_count--;
   259             }
   260             break;
   261         default:
   262             if( entry & 0x7E000000 ) {
   263                 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   264                 gl_render_poly( poly );
   265             }
   266         }
   267     }	    
   268 }
   271 /**
   272  * Render the currently defined scene in pvr2_scene
   273  */
   274 void pvr2_scene_render( render_buffer_t buffer )
   275 {
   276     /* Scene setup */
   277     struct timeval start_tv, tex_tv, end_tv;
   279     gettimeofday(&start_tv, NULL);
   280     display_driver->set_render_target(buffer);
   281     pvr2_check_palette_changed();
   282     pvr2_scene_load_textures();
   284     gettimeofday( &tex_tv, NULL );
   285     uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
   286     (tex_tv.tv_usec - start_tv.tv_usec)/1000;
   287     DEBUG( "Scene setup in %dms", ms );
   289     /* Setup view projection matrix */
   290     glMatrixMode(GL_PROJECTION);
   291     glLoadIdentity();
   292     float nearz = pvr2_scene.bounds[4];
   293     float farz = pvr2_scene.bounds[5];
   294     if( nearz == farz ) {
   295         farz*= 4.0;
   296     }
   297     glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0, 
   298              -farz, -nearz );
   299     float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
   300     glAlphaFunc( GL_GEQUAL, alphaRef );
   302     /* Clear the buffer (FIXME: May not want always want to do this) */
   303     glDisable( GL_SCISSOR_TEST );
   304     glDepthMask( GL_TRUE );
   305     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
   307     /* Setup vertex array pointers */
   308     glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
   309     glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
   310     glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
   311     glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
   312     glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
   313     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
   314     glFogi(GL_FOG_MODE, GL_LINEAR);
   315     glFogf(GL_FOG_START, 0.0);
   316     glFogf(GL_FOG_END, 1.0);
   317     /* Turn on the shaders (if available) */
   318     glsl_enable_shaders(TRUE);
   320     /* Render the background */
   321     gl_render_bkgnd( pvr2_scene.bkgnd_poly );
   323     glEnable( GL_SCISSOR_TEST );
   325     /* Process the segment list */
   326     struct tile_segment *segment = pvr2_scene.segment_list;
   327     do {
   328         int tilex = SEGMENT_X(segment->control);
   329         int tiley = SEGMENT_Y(segment->control);
   331         uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
   332         if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
   333             continue; // fully clipped, skip tile
   334         }
   336         /* Clip to the visible part of the tile */
   337         glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], 
   338                    tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
   339         if( IS_TILE_PTR(segment->opaque_ptr) ) {
   340             gl_render_tilelist(segment->opaque_ptr);
   341         }
   342         if( IS_TILE_PTR(segment->trans_ptr) ) {
   343             if( pvr2_scene.sort_mode == SORT_NEVER || 
   344                     (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
   345                 gl_render_tilelist(segment->trans_ptr);
   346             } else {
   347                 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
   348             }
   349         }
   350         if( IS_TILE_PTR(segment->punchout_ptr) ) {
   351             glEnable(GL_ALPHA_TEST );
   352             render_autosort_tile(segment->punchout_ptr, RENDER_NORMAL );
   353             glDisable(GL_ALPHA_TEST );
   354         }
   355     } while( !IS_LAST_SEGMENT(segment++) );
   356     glDisable( GL_SCISSOR_TEST );
   358     glsl_enable_shaders(FALSE);
   360     gettimeofday( &end_tv, NULL );
   361     ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
   362     (end_tv.tv_usec - tex_tv.tv_usec)/1000;
   363     DEBUG( "Scene render in %dms", ms );
   364 }
.