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