Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 677:3ee62740ff8f
prev669:ab344e42bca9
next684:95f2068235ef
author nkeynes
date Thu May 29 10:50:25 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Remove pvr2mmio.h include from pvr2.h (it's supposed to be moore or less private)
Move redraw function from driver into pvr2_redraw_display()
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     glEnableClientState( GL_COLOR_ARRAY );
   106     glEnableClientState( GL_VERTEX_ARRAY );
   107     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
   108     glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
   110     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   111     glClearDepth(0);
   112     glClearStencil(0);
   113 }
   115 /**
   116  * Setup the GL context for the supplied polygon context.
   117  * @param context pointer to 3 or 5 words of polygon context
   118  * @param modified boolean flag indicating that the modified
   119  *  version should be used, rather than the normal version.
   120  */
   121 void render_set_context( uint32_t *context, int render_mode )
   122 {
   123     uint32_t poly1 = context[0], poly2, texture;
   124     if( render_mode == RENDER_FULLMOD ) {
   125 	poly2 = context[3];
   126 	texture = context[4];
   127     } else {
   128 	poly2 = context[1];
   129 	texture = context[2];
   130     }
   132     glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   133     glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
   135     switch( POLY1_CULL_MODE(poly1) ) {
   136     case CULL_NONE:
   137     case CULL_SMALL:
   138 	glDisable( GL_CULL_FACE );
   139 	break;
   140     case CULL_CCW:
   141 	glEnable( GL_CULL_FACE );
   142 	glFrontFace( GL_CW );
   143 	break;
   144     case CULL_CW:
   145 	glEnable( GL_CULL_FACE );
   146 	glFrontFace( GL_CCW );
   147 	break;
   148     }
   150     if( POLY1_SPECULAR(poly1) ) {
   151 	glEnable(GL_COLOR_SUM);
   152     } else {
   153 	glDisable(GL_COLOR_SUM);
   154     }
   157     if( POLY1_TEXTURED(poly1) ) {
   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(3, GL_UNSIGNED_BYTE, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba );
   274     /* Turn on the shaders (if available) */
   275     glsl_enable_shaders(TRUE);
   277     uint32_t bgplane_mode = MMIO_READ(PVR2, RENDER_BGPLANE);
   278     uint32_t *bgplane = pvr2_scene.pvr2_pbuf + (((bgplane_mode & 0x00FFFFFF)) >> 3) ;
   279     render_backplane( bgplane, pvr2_scene.buffer_width, pvr2_scene.buffer_height, bgplane_mode );
   281     glEnable( GL_SCISSOR_TEST );
   283     /* Process the segment list */
   284     struct tile_segment *segment = pvr2_scene.segment_list;
   285     do {
   286 	int tilex = SEGMENT_X(segment->control);
   287 	int tiley = SEGMENT_Y(segment->control);
   289 	uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
   290 	if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
   291 	    continue; // fully clipped, skip tile
   292 	}
   294 	/* Clip to the visible part of the tile */
   295 	glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], 
   296 		   tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
   297 	if( IS_TILE_PTR(segment->opaque_ptr) ) {
   298 	    gl_render_tilelist(segment->opaque_ptr);
   299 	}
   300 	if( IS_TILE_PTR(segment->trans_ptr) ) {
   301 	    if( pvr2_scene.sort_mode == SORT_NEVER || 
   302 		(pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
   303 		gl_render_tilelist(segment->trans_ptr);
   304 	    } else {
   305 		render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
   306 	    }
   307 	}
   308 	if( IS_TILE_PTR(segment->punchout_ptr) ) {
   309 	    glEnable(GL_ALPHA_TEST );
   310 	    render_autosort_tile(segment->punchout_ptr, RENDER_NORMAL );
   311 	    glDisable(GL_ALPHA_TEST );
   312 	}
   313     } while( !IS_LAST_SEGMENT(segment++) );
   314     glDisable( GL_SCISSOR_TEST );
   316     glsl_enable_shaders(FALSE);
   318     gettimeofday( &end_tv, NULL );
   319     ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
   320         (end_tv.tv_usec - tex_tv.tv_usec)/1000;
   321     DEBUG( "Scene render in %dms", ms );
   322 }
.