2 * $Id: rendcore.c,v 1.10 2007-01-21 11:28:43 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 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
23 GL_GREATER, GL_NOTEQUAL, GL_GEQUAL,
25 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 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 int pvr2_poly_texblend[4] = {
34 GL_REPLACE, GL_MODULATE, GL_DECAL, GL_MODULATE };
35 int pvr2_render_colour_format[8] = {
36 COLFMT_ARGB1555, COLFMT_RGB565, COLFMT_ARGB4444, COLFMT_ARGB1555,
37 COLFMT_RGB888, COLFMT_ARGB8888, COLFMT_ARGB8888, COLFMT_ARGB4444 };
45 #define SEGMENT_END 0x80000000
46 #define SEGMENT_ZCLEAR 0x40000000
47 #define SEGMENT_SORT_TRANS 0x20000000
48 #define SEGMENT_START 0x10000000
49 #define SEGMENT_X(c) (((c) >> 2) & 0x3F)
50 #define SEGMENT_Y(c) (((c) >> 8) & 0x3F)
51 #define NO_POINTER 0x80000000
53 extern char *video_base;
58 pvraddr_t opaquemod_ptr;
60 pvraddr_t transmod_ptr;
61 pvraddr_t punchout_ptr;
65 * Convert a half-float (16-bit) FP number to a regular 32-bit float.
66 * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
67 * TODO: Check the correctness of this.
69 float halftofloat( uint16_t half )
75 /* int e = ((half & 0x7C00) >> 10) - 15 + 127;
77 temp.i = ((half & 0x8000) << 16) | (e << 23) |
78 ((half & 0x03FF) << 13); */
79 temp.i = ((uint32_t)half)<<16;
85 * Setup the GL context for the supplied polygon context.
86 * @param context pointer to 3 or 5 words of polygon context
87 * @param modified boolean flag indicating that the modified
88 * version should be used, rather than the normal version.
90 void render_set_context( uint32_t *context, int render_mode )
92 uint32_t poly1 = context[0], poly2, texture;
93 if( render_mode == RENDER_FULLMOD ) {
101 if( POLY1_DEPTH_ENABLE(poly1) ) {
102 glEnable( GL_DEPTH_TEST );
103 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
105 glDisable( GL_DEPTH_TEST );
108 switch( POLY1_CULL_MODE(poly1) ) {
111 glDisable( GL_CULL_FACE );
114 glEnable( GL_CULL_FACE );
115 glFrontFace( GL_CW );
118 glEnable( GL_CULL_FACE );
119 glFrontFace( GL_CCW );
123 if( POLY1_TEXTURED(poly1) ) {
124 int width = POLY2_TEX_WIDTH(poly2);
125 int height = POLY2_TEX_HEIGHT(poly2);
126 glEnable(GL_TEXTURE_2D);
127 texcache_get_texture( (texture&0x000FFFFF)<<3, width, height, texture );
128 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, POLY2_TEX_BLEND(poly2) );
129 if( POLY2_TEX_CLAMP_U(poly2) ) {
130 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
132 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
134 if( POLY2_TEX_CLAMP_V(poly2) ) {
135 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
137 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
140 glDisable( GL_TEXTURE_2D );
143 glShadeModel( POLY1_SHADE_MODEL(poly1) );
145 int srcblend = POLY2_SRC_BLEND(poly2);
146 int destblend = POLY2_DEST_BLEND(poly2);
147 glBlendFunc( srcblend, destblend );
150 void render_vertexes( uint32_t poly1, uint32_t *vertexes, int num_vertexes, int vertex_size,
155 if( render_mode == RENDER_FULLMOD ) {
156 m = (vertex_size - 3)/2;
159 glBegin( GL_TRIANGLE_STRIP );
161 for( i=0; i<num_vertexes; i++ ) {
162 float *vertexf = (float *)vertexes;
164 if( POLY1_TEXTURED(poly1) ) {
165 if( POLY1_UV16(poly1) ) {
166 glTexCoord2f( halftofloat(vertexes[m+3]>>16),
167 halftofloat(vertexes[m+3]) );
168 argb = vertexes[m+4];
170 glTexCoord2f( vertexf[m+3], vertexf[m+4] );
171 argb = vertexes[m+5];
174 argb = vertexes[m+3];
177 glColor4ub( (GLubyte)(argb >> 16), (GLubyte)(argb >> 8),
178 (GLubyte)argb, (GLubyte)(argb >> 24) );
179 glVertex3f( vertexf[0], vertexf[1], vertexf[2] );
180 vertexes += vertex_size;
187 * Render a simple (not auto-sorted) tile
189 void render_tile( pvraddr_t tile_entry, int render_mode, gboolean cheap_modifier_mode ) {
190 uint32_t poly_bank = MMIO_READ(PVR2,RENDER_POLYBASE);
191 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
193 uint32_t entry = *tile_list++;
194 if( entry >> 28 == 0x0F ) {
196 } else if( entry >> 28 == 0x0E ) {
197 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
199 uint32_t *polygon = (uint32_t *)(video_base + poly_bank + ((entry & 0x000FFFFF) << 2));
200 int is_modified = entry & 0x01000000;
201 int vertex_length = (entry >> 21) & 0x07;
202 int context_length = 3;
203 if( is_modified && !cheap_modifier_mode ) {
209 if( (entry & 0xE0000000) == 0x80000000 ) {
211 int strip_count = ((entry >> 25) & 0x0F)+1;
212 int polygon_length = 3 * vertex_length + context_length;
214 for( i=0; i<strip_count; i++ ) {
215 render_set_context( polygon, render_mode );
216 render_vertexes( *polygon, polygon+context_length, 3, vertex_length,
218 polygon += polygon_length;
220 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
222 int strip_count = (entry >> 25) & 0x0F;
223 int polygon_length = 4 * vertex_length + context_length;
225 for( i=0; i<strip_count; i++ ) {
226 render_set_context( polygon, render_mode );
227 render_vertexes( *polygon, polygon+context_length, 4, vertex_length,
229 polygon += polygon_length;
233 int i, first=-1, last = -1;
234 for( i=0; i<6; i++ ) {
235 if( entry & (0x40000000>>i) ) {
236 if( first == -1 ) first = i;
242 render_set_context(polygon, render_mode);
243 render_vertexes( *polygon, polygon+context_length + (first*vertex_length),
244 (last-first+3), vertex_length, render_mode );
251 void pvr2_render_tilebuffer( int width, int height, int clipx1, int clipy1,
252 int clipx2, int clipy2 ) {
254 pvraddr_t segmentbase = MMIO_READ( PVR2, RENDER_TILEBASE );
256 gboolean cheap_shadow;
258 int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
259 int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
260 int shadow_cfg = MMIO_READ( PVR2, RENDER_SHADOW );
262 if( (obj_config & 0x00200000) == 0 ) {
263 if( isp_config & 1 ) {
272 cheap_shadow = shadow_cfg & 0x100 ? TRUE : FALSE;
274 struct tile_segment *segment = (struct tile_segment *)(video_base + segmentbase);
276 struct timeval tv_start, tv_end;
277 gettimeofday(&tv_start, NULL);
278 glEnable( GL_SCISSOR_TEST );
280 // fwrite_dump32v( (uint32_t *)segment, sizeof(struct tile_segment), 6, stderr );
281 int tilex = SEGMENT_X(segment->control);
282 int tiley = SEGMENT_Y(segment->control);
286 if( x1 + 32 <= clipx1 ||
290 /* Tile completely clipped, skip */
294 /* Set a scissor on the visible part of the tile */
295 int w = MIN(x1+32, clipx2) - x1;
296 int h = MIN(y1+32, clipy2) - y1;
299 glScissor( x1, height-y1-h, w, h );
301 if( (segment->opaque_ptr & NO_POINTER) == 0 ) {
302 if( (segment->opaquemod_ptr & NO_POINTER) == 0 ) {
305 render_tile( segment->opaque_ptr, RENDER_NORMAL, cheap_shadow );
308 if( (segment->trans_ptr & NO_POINTER) == 0 ) {
309 if( (segment->transmod_ptr & NO_POINTER) == 0 ) {
312 if( tile_sort == 2 ||
313 (tile_sort == 1 && ((segment->control & SEGMENT_SORT_TRANS)==0)) ) {
314 render_autosort_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
316 render_tile( segment->trans_ptr, RENDER_NORMAL, cheap_shadow );
320 if( (segment->punchout_ptr & NO_POINTER) == 0 ) {
321 render_tile( segment->punchout_ptr, RENDER_NORMAL, cheap_shadow );
323 } while( ((segment++)->control & SEGMENT_END) == 0 );
324 glDisable( GL_SCISSOR_TEST );
326 gettimeofday(&tv_end, NULL);
327 timersub(&tv_end,&tv_start, &tv_start);
.