2 * $Id: rendcore.c,v 1.3 2006-08-18 12:43:24 nkeynes Exp $
6 * Copyright (c) 2005 Nathan Keynes.
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.
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.
19 #include "pvr2/pvr2.h"
22 static int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
23 GL_GREATER, GL_NOTEQUAL, GL_GEQUAL,
25 static int pvr2_poly_srcblend[8] = {
26 GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
27 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
28 GL_ONE_MINUS_DST_ALPHA };
29 static int pvr2_poly_dstblend[8] = {
30 GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
31 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
32 GL_ONE_MINUS_DST_ALPHA };
33 static int pvr2_poly_texblend[4] = {
34 GL_REPLACE, GL_BLEND, GL_DECAL, GL_MODULATE };
35 static int pvr2_render_colour_format[8] = {
36 COLFMT_ARGB1555, COLFMT_RGB565, COLFMT_ARGB4444, COLFMT_ARGB1555,
37 COLFMT_RGB888, COLFMT_ARGB8888, COLFMT_ARGB8888, COLFMT_ARGB4444 };
38 #define POLY1_DEPTH_MODE(poly1) ( pvr2_poly_depthmode[(poly1)>>29] )
39 #define POLY1_DEPTH_ENABLE(poly1) (((poly1)&0x04000000) == 0 )
40 #define POLY1_CULL_MODE(poly1) (((poly1)>>27)&0x03)
41 #define POLY1_TEXTURED(poly1) (((poly1)&0x02000000))
42 #define POLY1_SPECULAR(poly1) (((poly1)&0x01000000))
43 #define POLY1_SHADE_MODEL(poly1) (((poly1)&0x00800000) ? GL_SMOOTH : GL_FLAT)
44 #define POLY1_UV16(poly1) (((poly1)&0x00400000))
45 #define POLY1_SINGLE_TILE(poly1) (((poly1)&0x00200000))
47 #define POLY2_SRC_BLEND(poly2) ( pvr2_poly_srcblend[(poly2) >> 29] )
48 #define POLY2_DEST_BLEND(poly2) ( pvr2_poly_dstblend[((poly2)>>26)&0x07] )
49 #define POLY2_SRC_BLEND_ENABLE(poly2) ((poly2)&0x02000000)
50 #define POLY2_DEST_BLEND_ENABLE(poly2) ((poly2)&0x01000000)
51 #define POLY2_COLOUR_CLAMP_ENABLE(poly2) ((poly2)&0x00200000)
52 #define POLY2_ALPHA_ENABLE(poly2) ((poly2)&0x001000000)
53 #define POLY2_TEX_ALPHA_ENABLE(poly2) (((poly2)&0x00080000) == 0 )
54 #define POLY2_TEX_WIDTH(poly2) ( 1<< ((((poly2) >> 3) & 0x07 ) + 3) )
55 #define POLY2_TEX_HEIGHT(poly2) ( 1<< (((poly2) & 0x07 ) + 3) )
56 #define POLY2_TEX_BLEND(poly2) ( pvr2_poly_texblend[((poly2) >> 6)&0x03] )
58 #define RENDER_ZONLY 0
59 #define RENDER_NORMAL 1 /* Render non-modified polygons */
60 #define RENDER_CHEAPMOD 2 /* Render cheap-modified polygons */
61 #define RENDER_FULLMOD 3 /* Render the fully-modified version of the polygons */
68 #define SEGMENT_END 0x80000000
69 #define SEGMENT_SORT_TRANS 0x20000000
70 #define SEGMENT_START 0x10000000
71 #define SEGMENT_X(c) (((c) >> 2) & 0x3F)
72 #define SEGMENT_Y(c) (((c) >> 8) & 0x3F)
73 #define NO_POINTER 0x80000000
75 extern char *video_base;
80 pvraddr_t opaquemod_ptr;
82 pvraddr_t transmod_ptr;
83 pvraddr_t punchout_ptr;
87 * Convert a half-float (16-bit) FP number to a regular 32-bit float.
88 * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
89 * TODO: Check the correctness of this.
91 float halftofloat( uint16_t half )
97 int e = ((half & 0x7C00) >> 10) - 15 + 127;
99 temp.i = ((half & 0x8000) << 16) | (e << 23) |
100 ((half & 0x03FF) << 13);
106 * Setup the GL context for the supplied polygon context.
107 * @param context pointer to 3 or 5 words of polygon context
108 * @param modified boolean flag indicating that the modified
109 * version should be used, rather than the normal version.
111 void render_set_context( uint32_t *context, int render_mode )
113 uint32_t poly1 = context[0], poly2, texture;
114 if( render_mode == RENDER_FULLMOD ) {
116 texture = context[4];
119 texture = context[2];
122 if( POLY1_DEPTH_ENABLE(poly1) ) {
123 glEnable( GL_DEPTH_TEST );
124 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
126 glDisable( GL_DEPTH_TEST );
129 switch( POLY1_CULL_MODE(poly1) ) {
132 glDisable( GL_CULL_FACE );
135 glEnable( GL_CULL_FACE );
136 glFrontFace( GL_CW );
139 glEnable( GL_CULL_FACE );
140 glFrontFace( GL_CCW );
144 if( POLY1_TEXTURED(poly1) ) {
145 int width = POLY2_TEX_WIDTH(poly2);
146 int height = POLY2_TEX_HEIGHT(poly2);
147 glEnable(GL_TEXTURE_2D);
148 texcache_get_texture( (texture&0x001FFFFF)<<3, width, height, texture );
149 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, POLY2_TEX_BLEND(poly2) );
151 glDisable( GL_TEXTURE_2D );
154 glShadeModel( POLY1_SHADE_MODEL(poly1) );
156 glBlendFunc( POLY2_SRC_BLEND(poly2), POLY2_DEST_BLEND(poly2) );
157 if( POLY2_TEX_ALPHA_ENABLE(poly2) ) {
164 void render_vertexes( uint32_t poly1, uint32_t *vertexes, int num_vertexes, int vertex_size,
169 if( render_mode == RENDER_FULLMOD ) {
170 m = (vertex_size - 3)/2;
173 glBegin( GL_TRIANGLE_STRIP );
175 for( i=0; i<num_vertexes; i++ ) {
176 float *vertexf = (float *)vertexes;
178 if( POLY1_TEXTURED(poly1) ) {
179 if( POLY1_UV16(poly1) ) {
180 glTexCoord2f( halftofloat(vertexes[m+3]>>16),
181 halftofloat(vertexes[m+3]) );
182 argb = vertexes[m+4];
184 glTexCoord2f( vertexf[m+3], vertexf[m+4] );
185 argb = vertexes[m+5];
188 argb = vertexes[m+3];
191 glColor4ub( (GLubyte)(argb >> 16), (GLubyte)(argb >> 8),
192 (GLubyte)argb, (GLubyte)(argb >> 24) );
193 glVertex3f( vertexf[0], vertexf[1], vertexf[2] );
194 vertexes += vertex_size;
201 * Render a simple (not auto-sorted) tile
203 void render_tile( pvraddr_t tile_entry, int render_mode, gboolean cheap_modifier_mode ) {
204 uint32_t poly_bank = MMIO_READ(PVR2,RENDER_POLYBASE);
205 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
207 uint32_t entry = *tile_list++;
208 if( entry >> 28 == 0x0F ) {
210 } else if( entry >> 28 == 0x0E ) {
211 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
213 uint32_t *polygon = (uint32_t *)(video_base + poly_bank + ((entry & 0x000FFFFF) << 2));
214 int is_modified = entry & 0x01000000;
215 int vertex_length = (entry >> 21) & 0x07;
216 int context_length = 3;
217 if( is_modified && !cheap_modifier_mode ) {
223 if( (entry & 0xE0000000) == 0x80000000 ) {
225 int strip_count = ((entry >> 25) & 0x0F)+1;
226 int polygon_length = 3 * vertex_length + context_length;
228 for( i=0; i<strip_count; i++ ) {
229 render_set_context( polygon, render_mode );
230 render_vertexes( *polygon, polygon+context_length, 3, vertex_length,
232 polygon += polygon_length;
234 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
236 int strip_count = (entry >> 25) & 0x0F;
237 int polygon_length = 4 * vertex_length + context_length;
239 for( i=0; i<strip_count; i++ ) {
240 render_set_context( polygon, render_mode );
241 render_vertexes( *polygon, polygon+context_length, 4, vertex_length,
243 polygon += polygon_length;
247 int i, first=-1, last = -1;
248 for( i=0; i<6; i++ ) {
249 if( entry & (0x40000000>>i) ) {
250 if( first == -1 ) first = i;
256 render_set_context(polygon, render_mode);
257 render_vertexes( *polygon, polygon+context_length + (first*vertex_length),
258 (last-first+3), vertex_length, render_mode );
265 void render_autosort_tile( pvraddr_t tile_entry, int render_mode, gboolean cheap_modifier_mode ) {
266 //WARN( "Autosort not implemented yet" );
267 render_tile( tile_entry, render_mode, cheap_modifier_mode );
270 void pvr2_render_tilebuffer( int width, int height, int clipx1, int clipy1,
271 int clipx2, int clipy2 ) {
273 pvraddr_t segmentbase = MMIO_READ( PVR2, RENDER_TILEBASE );
275 gboolean cheap_shadow;
277 int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
278 int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
279 int shadow_cfg = MMIO_READ( PVR2, RENDER_SHADOW );
281 if( obj_config & 0x00200000 ) {
282 if( isp_config & 1 ) {
291 cheap_shadow = shadow_cfg & 0x100 ? TRUE : FALSE;
293 struct tile_segment *segment = (struct tile_segment *)(video_base + segmentbase);
295 struct timeval tv_start, tv_end;
296 gettimeofday(&tv_start, NULL);
297 fprintf( stderr, "Start render at %d.%d\n", tv_start.tv_sec, tv_start.tv_usec );
298 glEnable( GL_SCISSOR_TEST );
299 while( (segment->control & SEGMENT_END) == 0 ) {
300 int tilex = SEGMENT_X(segment->control);
301 int tiley = SEGMENT_Y(segment->control);
305 if( x1 + 32 <= clipx1 ||
309 /* Tile completely clipped, skip */
314 /* Set a scissor on the visible part of the tile */
315 int w = MIN(x1+32, clipx2) - x1;
316 int h = MIN(y1+32, clipy2) - y1;
319 glScissor( x1, height-y1-h, w, h );
321 if( (segment->opaque_ptr & NO_POINTER) == 0 ) {
322 if( (segment->opaquemod_ptr & NO_POINTER) == 0 ) {
325 render_tile( segment->opaque_ptr, RENDER_NORMAL, cheap_shadow );
328 if( (segment->trans_ptr & NO_POINTER) == 0 ) {
329 if( (segment->transmod_ptr & NO_POINTER) == 0 ) {
332 if( tile_sort == 2 || (tile_sort == 1 && (segment->control & SEGMENT_SORT_TRANS)) ) {
333 render_autosort_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
335 render_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
339 if( (segment->punchout_ptr & NO_POINTER) == 0 ) {
340 render_tile( segment->punchout_ptr, RENDER_NORMAL, cheap_shadow );
345 glDisable( GL_SCISSOR_TEST );
347 gettimeofday(&tv_end, NULL);
348 timersub(&tv_end,&tv_start, &tv_start);
349 fprintf( stderr, "Frame took %d.%06ds\n", tv_start.tv_sec, tv_start.tv_usec );
.