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