Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 645:a7392098299c
prev639:162ee7614b60
next648:ef9aa5cba86f
author nkeynes
date Thu Mar 06 08:22:00 2008 +0000 (12 years ago)
branchlxdream-render
permissions -rw-r--r--
last change More refactor work in progress - nearly done now
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"
    24 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
    25 				      GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, 
    26 				      GL_ALWAYS };
    27 int pvr2_poly_srcblend[8] = { 
    28     GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
    29     GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, 
    30     GL_ONE_MINUS_DST_ALPHA };
    31 int pvr2_poly_dstblend[8] = {
    32     GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
    33     GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
    34     GL_ONE_MINUS_DST_ALPHA };
    35 int pvr2_poly_texblend[4] = {
    36     GL_REPLACE, 
    37     GL_MODULATE,  
    38     GL_DECAL, 
    39     GL_MODULATE 
    40 };
    41 int pvr2_render_colour_format[8] = {
    42     COLFMT_BGRA1555, COLFMT_RGB565, COLFMT_BGRA4444, COLFMT_BGRA1555,
    43     COLFMT_BGR888, COLFMT_BGRA8888, COLFMT_BGRA8888, COLFMT_BGRA4444 };
    46 /**
    47  * Clip the tile bounds to the clipping plane. 
    48  * @return TRUE if the tile was not clipped completely.
    49  */
    50 static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
    51 {
    52     if( tile[0] < clip[0] ) tile[0] = clip[0];
    53     if( tile[1] > clip[1] ) tile[1] = clip[1];
    54     if( tile[2] < clip[2] ) tile[2] = clip[2];
    55     if( tile[3] > clip[3] ) tile[3] = clip[3];
    56     return tile[0] < tile[1] && tile[2] < tile[3];
    57 }
    59 void pvr2_scene_load_textures()
    60 {
    61     int i;
    62     for( i=0; i < pvr2_scene.poly_count; i++ ) {
    63 	struct polygon_struct *poly = &pvr2_scene.poly_array[i];
    64 	if( POLY1_TEXTURED(poly->context[0]) ) {
    65 	    poly->tex_id = texcache_get_texture( poly->context[2],
    66 						 POLY2_TEX_WIDTH(poly->context[1]),
    67 						 POLY2_TEX_HEIGHT(poly->context[1]) );
    68 	    if( poly->mod_vertex_index != -1 ) {
    69 		poly->mod_tex_id = texcache_get_texture( poly->context[4],
    70 							 POLY2_TEX_WIDTH(poly->context[3]),
    71 							 POLY2_TEX_HEIGHT(poly->context[3]) );
    72 	    }
    73 	} else {
    74 	    poly->tex_id = -1;
    75 	    poly->mod_tex_id = -1;
    76 	}
    77     }
    78 }
    82 /**
    83  * Once-off call to setup the OpenGL context.
    84  */
    85 void pvr2_setup_gl_context()
    86 {
    87     texcache_gl_init(); // Allocate texture IDs
    88     glCullFace( GL_BACK );
    89     glEnable( GL_BLEND );
    90     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    91     glMatrixMode(GL_MODELVIEW);
    92     glLoadIdentity();
    94     glEnableClientState( GL_COLOR_ARRAY );
    95     glEnableClientState( GL_VERTEX_ARRAY );
    96     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
    97     glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
    99     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   100     glClearDepth(0);
   101     glClearStencil(0);
   102 }
   104 /**
   105  * Setup the GL context for the supplied polygon context.
   106  * @param context pointer to 3 or 5 words of polygon context
   107  * @param modified boolean flag indicating that the modified
   108  *  version should be used, rather than the normal version.
   109  */
   110 void render_set_context( uint32_t *context, int render_mode )
   111 {
   112     uint32_t poly1 = context[0], poly2, texture;
   113     if( render_mode == RENDER_FULLMOD ) {
   114 	poly2 = context[3];
   115 	texture = context[4];
   116     } else {
   117 	poly2 = context[1];
   118 	texture = context[2];
   119     }
   121     if( POLY1_DEPTH_ENABLE(poly1) ) {
   122 	glEnable( GL_DEPTH_TEST );
   123 	glDepthFunc( POLY1_DEPTH_MODE(poly1) );
   124     } else {
   125 	glDisable( GL_DEPTH_TEST );
   126     }
   128     switch( POLY1_CULL_MODE(poly1) ) {
   129     case CULL_NONE:
   130     case CULL_SMALL:
   131 	glDisable( GL_CULL_FACE );
   132 	break;
   133     case CULL_CCW:
   134 	glEnable( GL_CULL_FACE );
   135 	glFrontFace( GL_CW );
   136 	break;
   137     case CULL_CW:
   138 	glEnable( GL_CULL_FACE );
   139 	glFrontFace( GL_CCW );
   140 	break;
   141     }
   143     if( POLY1_SPECULAR(poly1) ) {
   144 	glEnable(GL_COLOR_SUM);
   145     } else {
   146 	glDisable(GL_COLOR_SUM);
   147     }
   150     if( POLY1_TEXTURED(poly1) ) {
   151 	int width = POLY2_TEX_WIDTH(poly2);
   152 	int height = POLY2_TEX_HEIGHT(poly2);
   153 	glEnable(GL_TEXTURE_2D);
   154 	switch( POLY2_TEX_BLEND(poly2) ) {
   155 	case 0: /* Replace */
   156 	    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
   157 	    break;
   158 	case 2:/* Decal */
   159 	    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
   160 	    break;
   161 	case 1: /* Modulate RGB */
   162 	    /* This is not directly supported by opengl (other than by mucking
   163 	     * with the texture format), but we get the same effect by forcing
   164 	     * the fragment alpha to 1.0 and using GL_MODULATE.
   165 	     */
   166 	case 3: /* Modulate RGBA */
   167 	    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
   168 	    break;
   169 	}
   171 	if( POLY2_TEX_CLAMP_U(poly2) ) {
   172 	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
   173 	} else {
   174 	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
   175 	}	    
   176 	if( POLY2_TEX_CLAMP_V(poly2) ) {
   177 	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
   178 	} else {
   179 	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
   180 	}
   181     } else {
   182 	glDisable( GL_TEXTURE_2D );
   183     }
   185     glShadeModel( POLY1_SHADE_MODEL(poly1) );
   187     int srcblend = POLY2_SRC_BLEND(poly2);
   188     int destblend = POLY2_DEST_BLEND(poly2);
   189     glBlendFunc( srcblend, destblend );
   191     if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
   192 	ERROR( "Accumulation buffer not supported" );
   193     }
   195 }
   198 static void gl_render_poly( struct polygon_struct *poly )
   199 {
   200     if( poly->tex_id != -1 ) {
   201 	glBindTexture(GL_TEXTURE_2D, poly->tex_id);
   202     }
   203     render_set_context( poly->context, RENDER_NORMAL );
   204     glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
   206 }
   208 void gl_render_tilelist( pvraddr_t tile_entry )
   209 {
   210     uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
   211     int strip_count;
   212     struct polygon_struct *poly;
   214     while(1) {
   215 	uint32_t entry = *tile_list++;
   216 	switch( entry >> 28 ) {
   217 	case 0x0F:
   218 	    return; // End-of-list
   219 	case 0x0E:
   220 	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
   221 	    break;
   222 	case 0x08: case 0x09: case 0x0A: case 0x0B:
   223 	    strip_count = ((entry >> 25) & 0x0F)+1;
   224 	    poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   225 	    while( strip_count > 0 ) {
   226 		assert( poly != NULL );
   227 		gl_render_poly( poly );
   228 		poly = poly->next;
   229 		strip_count--;
   230 	    }
   231 	    break;
   232 	default:
   233 	    if( entry & 0x7E000000 ) {
   234 		poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
   235 		gl_render_poly( poly );
   236 	    }
   237 	}
   238     }	    
   239 }
   242 /**
   243  * Render the currently defined scene in pvr2_scene
   244  */
   245 void pvr2_scene_render( render_buffer_t buffer )
   246 {
   247     /* Scene setup */
   248     struct timeval start_tv, tex_tv, end_tv;
   250     gettimeofday(&start_tv, NULL);
   251     display_driver->set_render_target(buffer);
   252     pvr2_check_palette_changed();
   253     pvr2_scene_load_textures();
   255     gettimeofday( &tex_tv, NULL );
   256     uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
   257         (tex_tv.tv_usec - start_tv.tv_usec)/1000;
   258     DEBUG( "Scene setup in %dms", ms );
   260     /* Setup view projection matrix */
   261     glMatrixMode(GL_PROJECTION);
   262     glLoadIdentity();
   263     float nearz = pvr2_scene.bounds[4];
   264     float farz = pvr2_scene.bounds[5];
   265     if( nearz == farz ) {
   266 	farz*= 2.0;
   267     }
   268     glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0, 
   269     	     -nearz, -farz );
   271     /* Clear the buffer (FIXME: May not want always want to do this) */
   272     glDisable( GL_SCISSOR_TEST );
   273     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
   275     /* Setup vertex array pointers */
   276     glInterleavedArrays(GL_T2F_C4UB_V3F, sizeof(struct vertex_struct), pvr2_scene.vertex_array);
   277     glSecondaryColorPointerEXT(4, GL_UNSIGNED_BYTE, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba );
   279     uint32_t bgplane_mode = MMIO_READ(PVR2, RENDER_BGPLANE);
   280     uint32_t *bgplane = pvr2_scene.pvr2_pbuf + (((bgplane_mode & 0x00FFFFFF)) >> 3) ;
   281     render_backplane( bgplane, pvr2_scene.buffer_width, pvr2_scene.buffer_height, bgplane_mode );
   283     glEnable( GL_SCISSOR_TEST );
   285     /* Process the segment list */
   286     struct tile_segment *segment = pvr2_scene.segment_list;
   287     do {
   288 	int tilex = SEGMENT_X(segment->control);
   289 	int tiley = SEGMENT_Y(segment->control);
   291 	int tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
   292 	if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
   293 	    continue; // fully clipped, skip tile
   294 	}
   296 	/* Clip to the visible part of the tile */
   297 	glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], 
   298 		   tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
   299 	if( IS_TILE_PTR(segment->opaque_ptr) ) {
   300 	    gl_render_tilelist(segment->opaque_ptr);
   301 	}
   302 	if( IS_TILE_PTR(segment->trans_ptr) ) {
   303 	    if( pvr2_scene.sort_mode == SORT_NEVER || 
   304 		(pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
   305 		gl_render_tilelist(segment->trans_ptr);
   306 	    } else {
   307 		render_autosort_tile(segment->trans_ptr, RENDER_NORMAL, !pvr2_scene.full_shadow);
   308 	    }
   309 	}
   310 	if( IS_TILE_PTR(segment->punchout_ptr) ) {
   311 	    gl_render_tilelist(segment->punchout_ptr);
   312 	}
   313     } while( !IS_LAST_SEGMENT(segment++) );
   314     glDisable( GL_SCISSOR_TEST );
   316     gettimeofday( &end_tv, NULL );
   317     ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
   318         (end_tv.tv_usec - tex_tv.tv_usec)/1000;
   319     DEBUG( "Scene render in %dms", ms );
   320 }
.