Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 847:2089244671d2
prev736:a02d1475ccfd
next856:02ac5f37bfc9
author nkeynes
date Sun Sep 07 04:23:49 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Fog work-in-progress
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 };
    44 int pvr2_render_colour_format[8] = {
    45         COLFMT_BGRA1555, COLFMT_RGB565, COLFMT_BGRA4444, COLFMT_BGRA1555,
    46         COLFMT_BGR888, COLFMT_BGRA8888, COLFMT_BGRA8888, COLFMT_BGRA4444 };
    49 /**
    50  * Clip the tile bounds to the clipping plane. 
    51  * @return TRUE if the tile was not clipped completely.
    52  */
    53 static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
    54 {
    55     if( tile[0] < clip[0] ) tile[0] = clip[0];
    56     if( tile[1] > clip[1] ) tile[1] = clip[1];
    57     if( tile[2] < clip[2] ) tile[2] = clip[2];
    58     if( tile[3] > clip[3] ) tile[3] = clip[3];
    59     return tile[0] < tile[1] && tile[2] < tile[3];
    60 }
    62 void pvr2_scene_load_textures()
    63 {
    64     int i;
    65     for( i=0; i < pvr2_scene.poly_count; i++ ) {
    66         struct polygon_struct *poly = &pvr2_scene.poly_array[i];
    67         if( POLY1_TEXTURED(poly->context[0]) ) {
    68             poly->tex_id = texcache_get_texture( poly->context[2],
    69                     POLY2_TEX_WIDTH(poly->context[1]),
    70                     POLY2_TEX_HEIGHT(poly->context[1]) );
    71             if( poly->mod_vertex_index != -1 ) {
    72                 poly->mod_tex_id = texcache_get_texture( poly->context[4],
    73                         POLY2_TEX_WIDTH(poly->context[3]),
    74                         POLY2_TEX_HEIGHT(poly->context[3]) );
    75             }
    76         } else {
    77             poly->tex_id = -1;
    78             poly->mod_tex_id = -1;
    79         }
    80     }
    81 }
    85 /**
    86  * Once-off call to setup the OpenGL context.
    87  */
    88 void pvr2_setup_gl_context()
    89 {
    91     if( glsl_is_supported() ) {
    92         if( !glsl_load_shaders( glsl_vertex_shader_src, NULL ) ) {
    93             WARN( "Unable to load GL shaders" );
    94         }
    95     }
    97     texcache_gl_init(); // Allocate texture IDs
    98     glCullFace( GL_BACK );
    99     glEnable( GL_BLEND );
   100     glEnable( GL_DEPTH_TEST );
   101     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
   102     glMatrixMode(GL_MODELVIEW);
   103     glLoadIdentity();
   105 #ifdef HAVE_OPENGL_CLAMP_COLOR
   106     if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
   107         glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
   108         glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
   109     }
   110 #endif
   112     glEnableClientState( GL_COLOR_ARRAY );
   113     glEnableClientState( GL_VERTEX_ARRAY );
   114     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
   115     glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
   116     glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
   118     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   119     glClearDepth(0);
   120     glClearStencil(0);
   121 }
   123 /**
   124  * Setup the GL context for the supplied polygon context.
   125  * @param context pointer to 3 or 5 words of polygon context
   126  * @param modified boolean flag indicating that the modified
   127  *  version should be used, rather than the normal version.
   128  */
   129 void render_set_context( uint32_t *context, int render_mode )
   130 {
   131     uint32_t poly1 = context[0], poly2, texture;
   132     if( render_mode == RENDER_FULLMOD ) {
   133         poly2 = context[3];
   134         texture = context[4];
   135     } else {
   136         poly2 = context[1];
   137         texture = context[2];
   138     }
   140     glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   141     glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
   143     switch( POLY1_CULL_MODE(poly1) ) {
   144     case CULL_NONE:
   145     case CULL_SMALL:
   146         glDisable( GL_CULL_FACE );
   147         break;
   148     case CULL_CCW:
   149         glEnable( GL_CULL_FACE );
   150         glFrontFace( GL_CW );
   151         break;
   152     case CULL_CW:
   153         glEnable( GL_CULL_FACE );
   154         glFrontFace( GL_CCW );
   155         break;
   156     }
   158     if( POLY1_SPECULAR(poly1) ) {
   159         glEnable(GL_COLOR_SUM);
   160     } else {
   161         glDisable(GL_COLOR_SUM);
   162     }
   165     if( POLY1_TEXTURED(poly1) ) {
   166         glEnable(GL_TEXTURE_2D);
   167         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
   168         if( POLY2_TEX_CLAMP_U(poly2) ) {
   169             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
   170         } else if( POLY2_TEX_MIRROR_U(poly2) ) {
   171             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB );
   172         } else {
   173             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
   174         }	    
   175         if( POLY2_TEX_CLAMP_V(poly2) ) {
   176             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
   177         } else if( POLY2_TEX_MIRROR_V(poly2) ) {
   178             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB );
   179         } else {
   180             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
   181         }
   182     } else {
   183         glDisable( GL_TEXTURE_2D );
   184     }
   186     switch( POLY2_FOG_MODE(poly2) ) {
   187     case PVR2_POLY_FOG_LOOKUP:
   188         glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
   189         glEnable( GL_FOG );
   190         break;
   191     case PVR2_POLY_FOG_VERTEX:
   192         if( POLY1_SPECULAR(poly1) ) {
   193             glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
   194             glEnable( GL_FOG );
   195             break;
   196         } /* else fallthrough */
   197     default:
   198         glDisable( GL_FOG );
   199     }
   202     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   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         ERROR( "Accumulation buffer not supported" );
   210     }
   212 }
   215 static void gl_render_poly( struct polygon_struct *poly )
   216 {
   217     if( poly->tex_id != -1 ) {
   218         glBindTexture(GL_TEXTURE_2D, poly->tex_id);
   219     }
   220     render_set_context( poly->context, RENDER_NORMAL );
   221     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   222 }
   225 static void gl_render_bkgnd( struct polygon_struct *poly )
   226 {
   227     if( poly->tex_id != -1 ) {
   228         glBindTexture(GL_TEXTURE_2D, poly->tex_id);
   229     }
   230     render_set_context( poly->context, RENDER_NORMAL );
   231     glDisable( GL_DEPTH_TEST );
   232     glDisable( GL_CULL_FACE );
   233     glBlendFunc( GL_ONE, GL_ZERO );
   234     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   235     glEnable( GL_CULL_FACE );
   236     glEnable( GL_DEPTH_TEST );
   237 }
   241 void gl_render_tilelist( pvraddr_t tile_entry )
   242 {
   243     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   244     int strip_count;
   245     struct polygon_struct *poly;
   247     while(1) {
   248         uint32_t entry = *tile_list++;
   249         switch( entry >> 28 ) {
   250         case 0x0F:
   251             return; // End-of-list
   252         case 0x0E:
   253             tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   254             break;
   255         case 0x08: case 0x09: case 0x0A: case 0x0B:
   256             strip_count = ((entry >> 25) & 0x0F)+1;
   257             poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   258             while( strip_count > 0 ) {
   259                 assert( poly != NULL );
   260                 gl_render_poly( poly );
   261                 poly = poly->next;
   262                 strip_count--;
   263             }
   264             break;
   265         default:
   266             if( entry & 0x7E000000 ) {
   267                 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   268                 gl_render_poly( poly );
   269             }
   270         }
   271     }	    
   272 }
   275 /**
   276  * Render the currently defined scene in pvr2_scene
   277  */
   278 void pvr2_scene_render( render_buffer_t buffer )
   279 {
   280     /* Scene setup */
   281     struct timeval start_tv, tex_tv, end_tv;
   283     gettimeofday(&start_tv, NULL);
   284     display_driver->set_render_target(buffer);
   285     pvr2_check_palette_changed();
   286     pvr2_scene_load_textures();
   288     gettimeofday( &tex_tv, NULL );
   289     uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
   290     (tex_tv.tv_usec - start_tv.tv_usec)/1000;
   291     DEBUG( "Scene setup in %dms", ms );
   293     /* Setup view projection matrix */
   294     glMatrixMode(GL_PROJECTION);
   295     glLoadIdentity();
   296     float nearz = pvr2_scene.bounds[4];
   297     float farz = pvr2_scene.bounds[5];
   298     if( nearz == farz ) {
   299         farz*= 4.0;
   300     }
   301     glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0, 
   302              -farz, -nearz );
   303     float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
   304     glAlphaFunc( GL_GEQUAL, alphaRef );
   306     /* Clear the buffer (FIXME: May not want always want to do this) */
   307     glDisable( GL_SCISSOR_TEST );
   308     glDepthMask( GL_TRUE );
   309     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
   311     /* Setup vertex array pointers */
   312     glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
   313     glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
   314     glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
   315     glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
   316     glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
   317     glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
   318     glFogi(GL_FOG_MODE, GL_LINEAR);
   319     glFogf(GL_FOG_START, 0.0);
   320     glFogf(GL_FOG_END, 1.0);
   321     /* Turn on the shaders (if available) */
   322     glsl_enable_shaders(TRUE);
   324     /* Render the background */
   325     gl_render_bkgnd( pvr2_scene.bkgnd_poly );
   327     glEnable( GL_SCISSOR_TEST );
   329     /* Process the segment list */
   330     struct tile_segment *segment = pvr2_scene.segment_list;
   331     do {
   332         int tilex = SEGMENT_X(segment->control);
   333         int tiley = SEGMENT_Y(segment->control);
   335         uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
   336         if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
   337             continue; // fully clipped, skip tile
   338         }
   340         /* Clip to the visible part of the tile */
   341         glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], 
   342                    tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
   343         if( IS_TILE_PTR(segment->opaque_ptr) ) {
   344             gl_render_tilelist(segment->opaque_ptr);
   345         }
   346         if( IS_TILE_PTR(segment->trans_ptr) ) {
   347             if( pvr2_scene.sort_mode == SORT_NEVER || 
   348                     (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
   349                 gl_render_tilelist(segment->trans_ptr);
   350             } else {
   351                 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
   352             }
   353         }
   354         if( IS_TILE_PTR(segment->punchout_ptr) ) {
   355             glEnable(GL_ALPHA_TEST );
   356             render_autosort_tile(segment->punchout_ptr, RENDER_NORMAL );
   357             glDisable(GL_ALPHA_TEST );
   358         }
   359     } while( !IS_LAST_SEGMENT(segment++) );
   360     glDisable( GL_SCISSOR_TEST );
   362     glsl_enable_shaders(FALSE);
   364     gettimeofday( &end_tv, NULL );
   365     ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
   366     (end_tv.tv_usec - tex_tv.tv_usec)/1000;
   367     DEBUG( "Scene render in %dms", ms );
   368 }
.