Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 665:99ae9dc4cab7
prev655:31a4b664d489
next666:ea369bcf6def
author nkeynes
date Sat Apr 19 00:36:36 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Move glsl setup out of the drivers
Move pvr2 setup call up to video_gtk (probably needs to go higher really)
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 "display.h"
    21 #include "pvr2/pvr2.h"
    22 #include "pvr2/scene.h"
    23 #include "pvr2/glutil.h"
    25 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
    26 				      GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, 
    27 				      GL_ALWAYS };
    28 int pvr2_poly_srcblend[8] = { 
    29     GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
    30     GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, 
    31     GL_ONE_MINUS_DST_ALPHA };
    32 int pvr2_poly_dstblend[8] = {
    33     GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
    34     GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
    35     GL_ONE_MINUS_DST_ALPHA };
    36 int pvr2_poly_texblend[4] = {
    37     GL_REPLACE, 
    38     GL_MODULATE,  
    39     GL_DECAL, 
    40     GL_MODULATE 
    41 };
    42 int pvr2_render_colour_format[8] = {
    43     COLFMT_BGRA1555, COLFMT_RGB565, COLFMT_BGRA4444, COLFMT_BGRA1555,
    44     COLFMT_BGR888, COLFMT_BGRA8888, COLFMT_BGRA8888, COLFMT_BGRA4444 };
    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;
    63     for( i=0; i < pvr2_scene.poly_count; i++ ) {
    64 	struct polygon_struct *poly = &pvr2_scene.poly_array[i];
    65 	if( POLY1_TEXTURED(poly->context[0]) ) {
    66 	    poly->tex_id = texcache_get_texture( poly->context[2],
    67 						 POLY2_TEX_WIDTH(poly->context[1]),
    68 						 POLY2_TEX_HEIGHT(poly->context[1]) );
    69 	    if( poly->mod_vertex_index != -1 ) {
    70 		poly->mod_tex_id = texcache_get_texture( poly->context[4],
    71 							 POLY2_TEX_WIDTH(poly->context[3]),
    72 							 POLY2_TEX_HEIGHT(poly->context[3]) );
    73 	    }
    74 	} else {
    75 	    poly->tex_id = -1;
    76 	    poly->mod_tex_id = -1;
    77 	}
    78     }
    79 }
    83 /**
    84  * Once-off call to setup the OpenGL context.
    85  */
    86 void pvr2_setup_gl_context()
    87 {
    89     if( glsl_is_supported() ) {
    90     	if( !glsl_load_shaders( glsl_vertex_shader_src, NULL ) ) {
    91             WARN( "Unable to load GL shaders" );
    92         }
    93     }
    95     texcache_gl_init(); // Allocate texture IDs
    96     glCullFace( GL_BACK );
    97     glEnable( GL_BLEND );
    98     glEnable( GL_DEPTH_TEST );
    99     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
   100     glMatrixMode(GL_MODELVIEW);
   101     glLoadIdentity();
   103     glEnableClientState( GL_COLOR_ARRAY );
   104     glEnableClientState( GL_VERTEX_ARRAY );
   105     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
   106     glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
   108     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   109     glClearDepth(0);
   110     glClearStencil(0);
   111 }
   113 /**
   114  * Setup the GL context for the supplied polygon context.
   115  * @param context pointer to 3 or 5 words of polygon context
   116  * @param modified boolean flag indicating that the modified
   117  *  version should be used, rather than the normal version.
   118  */
   119 void render_set_context( uint32_t *context, int render_mode )
   120 {
   121     uint32_t poly1 = context[0], poly2, texture;
   122     if( render_mode == RENDER_FULLMOD ) {
   123 	poly2 = context[3];
   124 	texture = context[4];
   125     } else {
   126 	poly2 = context[1];
   127 	texture = context[2];
   128     }
   130     glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   131     glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
   133     switch( POLY1_CULL_MODE(poly1) ) {
   134     case CULL_NONE:
   135     case CULL_SMALL:
   136 	glDisable( GL_CULL_FACE );
   137 	break;
   138     case CULL_CCW:
   139 	glEnable( GL_CULL_FACE );
   140 	glFrontFace( GL_CW );
   141 	break;
   142     case CULL_CW:
   143 	glEnable( GL_CULL_FACE );
   144 	glFrontFace( GL_CCW );
   145 	break;
   146     }
   148     if( POLY1_SPECULAR(poly1) ) {
   149 	glEnable(GL_COLOR_SUM);
   150     } else {
   151 	glDisable(GL_COLOR_SUM);
   152     }
   155     if( POLY1_TEXTURED(poly1) ) {
   156 	int width = POLY2_TEX_WIDTH(poly2);
   157 	int height = POLY2_TEX_HEIGHT(poly2);
   158 	glEnable(GL_TEXTURE_2D);
   159 	glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
   160 	if( POLY2_TEX_CLAMP_U(poly2) ) {
   161 	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
   162 	} else if( POLY2_TEX_MIRROR_U(poly2) ) {
   163 	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB );
   164 	} else {
   165 	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
   166 	}	    
   167 	if( POLY2_TEX_CLAMP_V(poly2) ) {
   168 	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
   169 	} else if( POLY2_TEX_MIRROR_V(poly2) ) {
   170 	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB );
   171 	} else {
   172 	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
   173 	}
   174     } else {
   175 	glDisable( GL_TEXTURE_2D );
   176     }
   178     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   180     int srcblend = POLY2_SRC_BLEND(poly2);
   181     int destblend = POLY2_DEST_BLEND(poly2);
   182     glBlendFunc( srcblend, destblend );
   184     if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
   185 	ERROR( "Accumulation buffer not supported" );
   186     }
   188 }
   191 static void gl_render_poly( struct polygon_struct *poly )
   192 {
   193     if( poly->tex_id != -1 ) {
   194 	glBindTexture(GL_TEXTURE_2D, poly->tex_id);
   195     }
   196     render_set_context( poly->context, RENDER_NORMAL );
   197     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   198 }
   200 void gl_render_tilelist( pvraddr_t tile_entry )
   201 {
   202     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   203     int strip_count;
   204     struct polygon_struct *poly;
   206     while(1) {
   207 	uint32_t entry = *tile_list++;
   208 	switch( entry >> 28 ) {
   209 	case 0x0F:
   210 	    return; // End-of-list
   211 	case 0x0E:
   212 	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   213 	    break;
   214 	case 0x08: case 0x09: case 0x0A: case 0x0B:
   215 	    strip_count = ((entry >> 25) & 0x0F)+1;
   216 	    poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   217 	    while( strip_count > 0 ) {
   218 		assert( poly != NULL );
   219 		gl_render_poly( poly );
   220 		poly = poly->next;
   221 		strip_count--;
   222 	    }
   223 	    break;
   224 	default:
   225 	    if( entry & 0x7E000000 ) {
   226 		poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   227 		gl_render_poly( poly );
   228 	    }
   229 	}
   230     }	    
   231 }
   234 /**
   235  * Render the currently defined scene in pvr2_scene
   236  */
   237 void pvr2_scene_render( render_buffer_t buffer )
   238 {
   239     /* Scene setup */
   240     struct timeval start_tv, tex_tv, end_tv;
   242     gettimeofday(&start_tv, NULL);
   243     display_driver->set_render_target(buffer);
   244     pvr2_check_palette_changed();
   245     pvr2_scene_load_textures();
   247     gettimeofday( &tex_tv, NULL );
   248     uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
   249         (tex_tv.tv_usec - start_tv.tv_usec)/1000;
   250     DEBUG( "Scene setup in %dms", ms );
   252     /* Setup view projection matrix */
   253     glMatrixMode(GL_PROJECTION);
   254     glLoadIdentity();
   255     float nearz = pvr2_scene.bounds[4];
   256     float farz = pvr2_scene.bounds[5];
   257     if( nearz == farz ) {
   258 	farz*= 2.0;
   259     }
   260     glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0, 
   261     	     -farz, -nearz );
   262     float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
   263     glAlphaFunc( GL_GEQUAL, alphaRef );
   265     /* Clear the buffer (FIXME: May not want always want to do this) */
   266     glDisable( GL_SCISSOR_TEST );
   267     glDepthMask( GL_TRUE );
   268     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
   270     /* Setup vertex array pointers */
   271     glInterleavedArrays(GL_T2F_C4UB_V3F, sizeof(struct vertex_struct), pvr2_scene.vertex_array);
   272     glSecondaryColorPointerEXT(4, GL_UNSIGNED_BYTE, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba );
   274     uint32_t bgplane_mode = MMIO_READ(PVR2, RENDER_BGPLANE);
   275     uint32_t *bgplane = pvr2_scene.pvr2_pbuf + (((bgplane_mode & 0x00FFFFFF)) >> 3) ;
   276     render_backplane( bgplane, pvr2_scene.buffer_width, pvr2_scene.buffer_height, bgplane_mode );
   278     glEnable( GL_SCISSOR_TEST );
   280     /* Process the segment list */
   281     struct tile_segment *segment = pvr2_scene.segment_list;
   282     do {
   283 	int tilex = SEGMENT_X(segment->control);
   284 	int tiley = SEGMENT_Y(segment->control);
   286 	int tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
   287 	if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
   288 	    continue; // fully clipped, skip tile
   289 	}
   291 	/* Clip to the visible part of the tile */
   292 	glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], 
   293 		   tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
   294 	if( IS_TILE_PTR(segment->opaque_ptr) ) {
   295 	    gl_render_tilelist(segment->opaque_ptr);
   296 	}
   297 	if( IS_TILE_PTR(segment->trans_ptr) ) {
   298 	    if( pvr2_scene.sort_mode == SORT_NEVER || 
   299 		(pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
   300 		gl_render_tilelist(segment->trans_ptr);
   301 	    } else {
   302 		render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
   303 	    }
   304 	}
   305 	if( IS_TILE_PTR(segment->punchout_ptr) ) {
   306 	    glEnable(GL_ALPHA_TEST );
   307 	    render_autosort_tile(segment->punchout_ptr, RENDER_NORMAL );
   308 	    glDisable(GL_ALPHA_TEST );
   309 	}
   310     } while( !IS_LAST_SEGMENT(segment++) );
   311     glDisable( GL_SCISSOR_TEST );
   313     gettimeofday( &end_tv, NULL );
   314     ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
   315         (end_tv.tv_usec - tex_tv.tv_usec)/1000;
   316     DEBUG( "Scene render in %dms", ms );
   317 }
.