Search
lxdream.org :: lxdream/src/pvr2/glrender.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/glrender.c
changeset 649:620c5c6496b5
prev648:ef9aa5cba86f
next651:95a9cf98785b
author nkeynes
date Tue Mar 11 08:50:16 2008 +0000 (12 years ago)
branchlxdream-render
permissions -rw-r--r--
last change Use maxz rather than minz for tri sorting (better results atm)
Change depth-test-disable to depth-mask (more correct)
Implement alpha test for punchthru polys
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@649
    90
    glEnable( GL_DEPTH_TEST );
nkeynes@639
    91
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
nkeynes@639
    92
    glMatrixMode(GL_MODELVIEW);
nkeynes@639
    93
    glLoadIdentity();
nkeynes@639
    94
nkeynes@639
    95
    glEnableClientState( GL_COLOR_ARRAY );
nkeynes@639
    96
    glEnableClientState( GL_VERTEX_ARRAY );
nkeynes@639
    97
    glEnableClientState( GL_TEXTURE_COORD_ARRAY );
nkeynes@639
    98
    glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
nkeynes@639
    99
nkeynes@639
   100
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
nkeynes@639
   101
    glClearDepth(0);
nkeynes@639
   102
    glClearStencil(0);
nkeynes@639
   103
}
nkeynes@639
   104
nkeynes@645
   105
/**
nkeynes@645
   106
 * Setup the GL context for the supplied polygon context.
nkeynes@645
   107
 * @param context pointer to 3 or 5 words of polygon context
nkeynes@645
   108
 * @param modified boolean flag indicating that the modified
nkeynes@645
   109
 *  version should be used, rather than the normal version.
nkeynes@645
   110
 */
nkeynes@645
   111
void render_set_context( uint32_t *context, int render_mode )
nkeynes@645
   112
{
nkeynes@645
   113
    uint32_t poly1 = context[0], poly2, texture;
nkeynes@645
   114
    if( render_mode == RENDER_FULLMOD ) {
nkeynes@645
   115
	poly2 = context[3];
nkeynes@645
   116
	texture = context[4];
nkeynes@645
   117
    } else {
nkeynes@645
   118
	poly2 = context[1];
nkeynes@645
   119
	texture = context[2];
nkeynes@645
   120
    }
nkeynes@645
   121
    
nkeynes@649
   122
    glDepthFunc( POLY1_DEPTH_MODE(poly1) );
nkeynes@649
   123
    glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
nkeynes@645
   124
    
nkeynes@645
   125
    switch( POLY1_CULL_MODE(poly1) ) {
nkeynes@645
   126
    case CULL_NONE:
nkeynes@645
   127
    case CULL_SMALL:
nkeynes@645
   128
	glDisable( GL_CULL_FACE );
nkeynes@645
   129
	break;
nkeynes@645
   130
    case CULL_CCW:
nkeynes@645
   131
	glEnable( GL_CULL_FACE );
nkeynes@645
   132
	glFrontFace( GL_CW );
nkeynes@645
   133
	break;
nkeynes@645
   134
    case CULL_CW:
nkeynes@645
   135
	glEnable( GL_CULL_FACE );
nkeynes@645
   136
	glFrontFace( GL_CCW );
nkeynes@645
   137
	break;
nkeynes@645
   138
    }
nkeynes@645
   139
nkeynes@645
   140
    if( POLY1_SPECULAR(poly1) ) {
nkeynes@645
   141
	glEnable(GL_COLOR_SUM);
nkeynes@645
   142
    } else {
nkeynes@645
   143
	glDisable(GL_COLOR_SUM);
nkeynes@645
   144
    }
nkeynes@645
   145
nkeynes@645
   146
nkeynes@645
   147
    if( POLY1_TEXTURED(poly1) ) {
nkeynes@645
   148
	int width = POLY2_TEX_WIDTH(poly2);
nkeynes@645
   149
	int height = POLY2_TEX_HEIGHT(poly2);
nkeynes@645
   150
	glEnable(GL_TEXTURE_2D);
nkeynes@648
   151
	glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
nkeynes@645
   152
	if( POLY2_TEX_CLAMP_U(poly2) ) {
nkeynes@645
   153
	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
nkeynes@645
   154
	} else {
nkeynes@645
   155
	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
nkeynes@645
   156
	}	    
nkeynes@645
   157
	if( POLY2_TEX_CLAMP_V(poly2) ) {
nkeynes@645
   158
	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
nkeynes@645
   159
	} else {
nkeynes@645
   160
	    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
nkeynes@645
   161
	}
nkeynes@645
   162
    } else {
nkeynes@645
   163
	glDisable( GL_TEXTURE_2D );
nkeynes@645
   164
    }
nkeynes@645
   165
nkeynes@645
   166
    glShadeModel( POLY1_SHADE_MODEL(poly1) );
nkeynes@645
   167
nkeynes@645
   168
    int srcblend = POLY2_SRC_BLEND(poly2);
nkeynes@645
   169
    int destblend = POLY2_DEST_BLEND(poly2);
nkeynes@645
   170
    glBlendFunc( srcblend, destblend );
nkeynes@645
   171
nkeynes@645
   172
    if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
nkeynes@645
   173
	ERROR( "Accumulation buffer not supported" );
nkeynes@645
   174
    }
nkeynes@645
   175
nkeynes@645
   176
}
nkeynes@645
   177
nkeynes@645
   178
nkeynes@639
   179
static void gl_render_poly( struct polygon_struct *poly )
nkeynes@639
   180
{
nkeynes@645
   181
    if( poly->tex_id != -1 ) {
nkeynes@645
   182
	glBindTexture(GL_TEXTURE_2D, poly->tex_id);
nkeynes@645
   183
    }
nkeynes@639
   184
    render_set_context( poly->context, RENDER_NORMAL );
nkeynes@639
   185
    glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
nkeynes@639
   186
}
nkeynes@639
   187
nkeynes@645
   188
void gl_render_tilelist( pvraddr_t tile_entry )
nkeynes@639
   189
{
nkeynes@639
   190
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@639
   191
    int strip_count;
nkeynes@639
   192
    struct polygon_struct *poly;
nkeynes@639
   193
nkeynes@639
   194
    while(1) {
nkeynes@639
   195
	uint32_t entry = *tile_list++;
nkeynes@639
   196
	switch( entry >> 28 ) {
nkeynes@639
   197
	case 0x0F:
nkeynes@639
   198
	    return; // End-of-list
nkeynes@639
   199
	case 0x0E:
nkeynes@639
   200
	    tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@639
   201
	    break;
nkeynes@645
   202
	case 0x08: case 0x09: case 0x0A: case 0x0B:
nkeynes@639
   203
	    strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@639
   204
	    poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@639
   205
	    while( strip_count > 0 ) {
nkeynes@645
   206
		assert( poly != NULL );
nkeynes@639
   207
		gl_render_poly( poly );
nkeynes@639
   208
		poly = poly->next;
nkeynes@639
   209
		strip_count--;
nkeynes@639
   210
	    }
nkeynes@639
   211
	    break;
nkeynes@639
   212
	default:
nkeynes@645
   213
	    if( entry & 0x7E000000 ) {
nkeynes@645
   214
		poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@645
   215
		gl_render_poly( poly );
nkeynes@645
   216
	    }
nkeynes@639
   217
	}
nkeynes@639
   218
    }	    
nkeynes@639
   219
}
nkeynes@639
   220
nkeynes@639
   221
nkeynes@639
   222
/**
nkeynes@639
   223
 * Render the currently defined scene in pvr2_scene
nkeynes@639
   224
 */
nkeynes@639
   225
void pvr2_scene_render( render_buffer_t buffer )
nkeynes@639
   226
{
nkeynes@639
   227
    /* Scene setup */
nkeynes@645
   228
    struct timeval start_tv, tex_tv, end_tv;
nkeynes@645
   229
nkeynes@645
   230
    gettimeofday(&start_tv, NULL);
nkeynes@639
   231
    display_driver->set_render_target(buffer);
nkeynes@639
   232
    pvr2_check_palette_changed();
nkeynes@645
   233
    pvr2_scene_load_textures();
nkeynes@645
   234
    
nkeynes@645
   235
    gettimeofday( &tex_tv, NULL );
nkeynes@645
   236
    uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
nkeynes@645
   237
        (tex_tv.tv_usec - start_tv.tv_usec)/1000;
nkeynes@645
   238
    DEBUG( "Scene setup in %dms", ms );
nkeynes@639
   239
nkeynes@639
   240
    /* Setup view projection matrix */
nkeynes@639
   241
    glMatrixMode(GL_PROJECTION);
nkeynes@639
   242
    glLoadIdentity();
nkeynes@639
   243
    float nearz = pvr2_scene.bounds[4];
nkeynes@639
   244
    float farz = pvr2_scene.bounds[5];
nkeynes@639
   245
    if( nearz == farz ) {
nkeynes@639
   246
	farz*= 2.0;
nkeynes@639
   247
    }
nkeynes@639
   248
    glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0, 
nkeynes@648
   249
    	     -farz, -nearz );
nkeynes@649
   250
    float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
nkeynes@649
   251
    glAlphaFunc( GL_GEQUAL, alphaRef );
nkeynes@639
   252
nkeynes@639
   253
    /* Clear the buffer (FIXME: May not want always want to do this) */
nkeynes@639
   254
    glDisable( GL_SCISSOR_TEST );
nkeynes@639
   255
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
nkeynes@639
   256
nkeynes@639
   257
    /* Setup vertex array pointers */
nkeynes@639
   258
    glInterleavedArrays(GL_T2F_C4UB_V3F, sizeof(struct vertex_struct), pvr2_scene.vertex_array);
nkeynes@639
   259
    glSecondaryColorPointerEXT(4, GL_UNSIGNED_BYTE, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba );
nkeynes@639
   260
nkeynes@639
   261
    uint32_t bgplane_mode = MMIO_READ(PVR2, RENDER_BGPLANE);
nkeynes@639
   262
    uint32_t *bgplane = pvr2_scene.pvr2_pbuf + (((bgplane_mode & 0x00FFFFFF)) >> 3) ;
nkeynes@639
   263
    render_backplane( bgplane, pvr2_scene.buffer_width, pvr2_scene.buffer_height, bgplane_mode );
nkeynes@639
   264
    
nkeynes@639
   265
    glEnable( GL_SCISSOR_TEST );
nkeynes@639
   266
nkeynes@639
   267
    /* Process the segment list */
nkeynes@639
   268
    struct tile_segment *segment = pvr2_scene.segment_list;
nkeynes@639
   269
    do {
nkeynes@639
   270
	int tilex = SEGMENT_X(segment->control);
nkeynes@639
   271
	int tiley = SEGMENT_Y(segment->control);
nkeynes@639
   272
	
nkeynes@639
   273
	int tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
nkeynes@639
   274
	if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
nkeynes@639
   275
	    continue; // fully clipped, skip tile
nkeynes@639
   276
	}
nkeynes@639
   277
nkeynes@639
   278
	/* Clip to the visible part of the tile */
nkeynes@639
   279
	glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], 
nkeynes@639
   280
		   tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
nkeynes@639
   281
	if( IS_TILE_PTR(segment->opaque_ptr) ) {
nkeynes@639
   282
	    gl_render_tilelist(segment->opaque_ptr);
nkeynes@639
   283
	}
nkeynes@639
   284
	if( IS_TILE_PTR(segment->trans_ptr) ) {
nkeynes@645
   285
	    if( pvr2_scene.sort_mode == SORT_NEVER || 
nkeynes@645
   286
		(pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
nkeynes@645
   287
		gl_render_tilelist(segment->trans_ptr);
nkeynes@645
   288
	    } else {
nkeynes@648
   289
		render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
nkeynes@645
   290
	    }
nkeynes@639
   291
	}
nkeynes@639
   292
	if( IS_TILE_PTR(segment->punchout_ptr) ) {
nkeynes@649
   293
	    glEnable(GL_ALPHA_TEST );
nkeynes@649
   294
	    render_autosort_tile(segment->punchout_ptr, RENDER_NORMAL );
nkeynes@649
   295
	    glDisable(GL_ALPHA_TEST );
nkeynes@639
   296
	}
nkeynes@639
   297
    } while( !IS_LAST_SEGMENT(segment++) );
nkeynes@639
   298
    glDisable( GL_SCISSOR_TEST );
nkeynes@639
   299
nkeynes@645
   300
    gettimeofday( &end_tv, NULL );
nkeynes@645
   301
    ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
nkeynes@645
   302
        (end_tv.tv_usec - tex_tv.tv_usec)/1000;
nkeynes@645
   303
    DEBUG( "Scene render in %dms", ms );
nkeynes@639
   304
}
.