4 * Manage the internal vertex/polygon buffers and scene data structure.
5 * Where possible this uses VBOs for the vertex + index data.
7 * Copyright (c) 2005 Nathan Keynes.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
25 #include "pvr2/pvr2.h"
26 #include "pvr2/pvr2mmio.h"
27 #include "pvr2/glutil.h"
28 #include "pvr2/scene.h"
30 #define U8TOFLOAT(n) (((float)((n)+1))/256.0)
31 #define POLY_IDX(addr) ( ((uint32_t *)addr) - ((uint32_t *)pvr2_scene.pvr2_pbuf))
33 static void unpack_bgra(uint32_t bgra, float *rgba)
35 rgba[0] = ((float)(((bgra&0x00FF0000)>>16) + 1)) / 256.0;
36 rgba[1] = ((float)(((bgra&0x0000FF00)>>8) + 1)) / 256.0;
37 rgba[2] = ((float)((bgra&0x000000FF) + 1)) / 256.0;
38 rgba[3] = ((float)(((bgra&0xFF000000)>>24) + 1)) / 256.0;
41 static inline uint32_t bgra_to_rgba(uint32_t bgra)
43 return (bgra&0xFF00FF00) | ((bgra&0x00FF0000)>>16) | ((bgra&0x000000FF)<<16);
47 * Convert a half-float (16-bit) FP number to a regular 32-bit float.
48 * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
49 * TODO: Check the correctness of this.
51 static float halftofloat( uint16_t half )
57 temp.i = ((uint32_t)half)<<16;
61 static float parse_fog_density( uint32_t value )
67 u.i = (((value+127)&0xFF)<<23)|((value & 0xFF00)<<7);
71 struct pvr2_scene_struct pvr2_scene;
73 static gboolean vbo_init = FALSE;
74 static float scene_shadow_intensity = 0.0;
76 #ifdef ENABLE_VERTEX_BUFFER
77 static gboolean vbo_supported = FALSE;
81 * Test for VBO support, and allocate all the system memory needed for the
82 * temporary structures. GL context must have been initialized before this
85 void pvr2_scene_init()
88 #ifdef ENABLE_VERTEX_BUFFER
89 if( isGLVertexBufferSupported() ) {
91 pvr2_scene.vbo_id = 1;
94 pvr2_scene.vertex_array = NULL;
95 pvr2_scene.vertex_array_size = 0;
96 pvr2_scene.poly_array = g_malloc( MAX_POLY_BUFFER_SIZE );
97 pvr2_scene.buf_to_poly_map = g_malloc0( BUF_POLY_MAP_SIZE );
103 * Clear the scene data structures in preparation for fresh data
105 void pvr2_scene_reset()
107 /* Faster to just clear the active entries */
108 for( int i=0; i<pvr2_scene.poly_count; i++ ) {
109 pvr2_scene.buf_to_poly_map[POLY_IDX(pvr2_scene.poly_array[i].context)] = 0;
111 pvr2_scene.poly_count = 0;
112 pvr2_scene.vertex_count = 0;
115 void pvr2_scene_shutdown()
117 #ifdef ENABLE_VERTEX_BUFFER
118 if( vbo_supported ) {
119 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
120 glDeleteBuffersARB( 1, &pvr2_scene.vbo_id );
121 pvr2_scene.vbo_id = 0;
124 g_free( pvr2_scene.vertex_array );
125 pvr2_scene.vertex_array = NULL;
126 #ifdef ENABLE_VERTEX_BUFFER
130 g_free( pvr2_scene.poly_array );
131 pvr2_scene.poly_array = NULL;
132 g_free( pvr2_scene.buf_to_poly_map );
133 pvr2_scene.buf_to_poly_map = NULL;
137 void *vertex_buffer_map()
139 // Allow 8 vertexes for the background (4+4)
140 uint32_t size = (pvr2_scene.vertex_count + 8) * sizeof(struct vertex_struct);
141 #ifdef ENABLE_VERTEX_BUFFER
142 if( vbo_supported ) {
144 glBindBufferARB( GL_ARRAY_BUFFER_ARB, pvr2_scene.vbo_id );
145 if( size > pvr2_scene.vertex_array_size ) {
146 glBufferDataARB( GL_ARRAY_BUFFER_ARB, size, NULL, GL_DYNAMIC_DRAW_ARB );
147 int status = glGetError();
149 fprintf( stderr, "Error %08X allocating vertex buffer\n", status );
152 pvr2_scene.vertex_array_size = size;
154 pvr2_scene.vertex_array = glMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );
155 assert(pvr2_scene.vertex_array != NULL );
158 if( size > pvr2_scene.vertex_array_size ) {
159 pvr2_scene.vertex_array = g_realloc( pvr2_scene.vertex_array, size );
161 #ifdef ENABLE_VERTEX_BUFFER
164 return pvr2_scene.vertex_array;
167 gboolean vertex_buffer_unmap()
169 #ifdef ENABLE_VERTEX_BUFFER
170 if( vbo_supported ) {
171 pvr2_scene.vertex_array = NULL;
172 return glUnmapBufferARB( GL_ARRAY_BUFFER_ARB );
181 static struct polygon_struct *scene_add_polygon( pvraddr_t poly_idx, int vertex_count,
182 shadow_mode_t is_modified )
184 int vert_mul = is_modified != SHADOW_NONE ? 2 : 1;
186 if( pvr2_scene.buf_to_poly_map[poly_idx] != NULL ) {
187 if( vertex_count > pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count ) {
188 pvr2_scene.vertex_count += (vertex_count - pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count) * vert_mul;
189 pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count = vertex_count;
191 return pvr2_scene.buf_to_poly_map[poly_idx];
193 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
194 poly->context = &pvr2_scene.pvr2_pbuf[poly_idx];
195 poly->vertex_count = vertex_count;
196 poly->vertex_index = -1;
197 poly->mod_vertex_index = -1;
199 poly->sub_next = NULL;
200 pvr2_scene.buf_to_poly_map[poly_idx] = poly;
201 pvr2_scene.vertex_count += (vertex_count * vert_mul);
207 * Given a starting polygon, break it at the specified triangle so that the
208 * preceding triangles are retained, and the remainder are contained in a
209 * new sub-polygon. Does not preserve winding.
211 static struct polygon_struct *scene_split_subpolygon( struct polygon_struct *parent, int split_offset )
213 assert( split_offset > 0 && split_offset < (parent->vertex_count-2) );
214 assert( pvr2_scene.poly_count < MAX_POLYGONS );
215 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
216 poly->vertex_count = parent->vertex_count - split_offset;
217 poly->vertex_index = parent->vertex_index + split_offset;
218 if( parent->mod_vertex_index == -1 ) {
219 poly->mod_vertex_index = -1;
221 poly->mod_vertex_index = parent->mod_vertex_index + split_offset;
223 poly->context = parent->context;
225 poly->sub_next = parent->sub_next;
227 parent->sub_next = poly;
228 parent->vertex_count = split_offset + 2;
233 static float scene_get_palette_offset( uint32_t tex )
235 uint32_t fmt = (tex & PVR2_TEX_FORMAT_MASK);
236 if( fmt == PVR2_TEX_FORMAT_IDX4 ) {
237 return ((float)((tex & 0x07E00000) >> 17))/1024.0 + 0.0002;
238 } else if( fmt == PVR2_TEX_FORMAT_IDX8 ) {
239 return ((float)((tex & 0x06000000) >> 17))/1024.0 + 0.0002;
246 * Decode a single PVR2 renderable vertex (opaque/trans/punch-out, but not shadow
248 * @param vert Pointer to output vertex structure
249 * @param poly1 First word of polygon context (needed to understand vertex)
250 * @param poly2 Second word of polygon context
251 * @param pvr2_data Pointer to raw pvr2 vertex data (in VRAM)
252 * @param modify_offset Offset in 32-bit words to the tex/color data. 0 for
253 * the normal vertex, half the vertex length for the modified vertex.
255 static void pvr2_decode_render_vertex( struct vertex_struct *vert, uint32_t poly1,
256 uint32_t poly2, uint32_t tex, uint32_t *pvr2_data,
259 gboolean force_alpha = !POLY2_ALPHA_ENABLE(poly2);
260 union pvr2_data_type {
265 data.ival = pvr2_data;
267 vert->x = *data.fval++;
268 vert->y = *data.fval++;
270 float z = *data.fval++;
273 } else if( z != 0 ) {
276 if( z > pvr2_scene.bounds[5] ) {
277 pvr2_scene.bounds[5] = z;
278 } else if( z < pvr2_scene.bounds[4] && z != 0 ) {
279 pvr2_scene.bounds[4] = z;
282 data.ival += modify_offset;
285 if( POLY1_TEXTURED(poly1) ) {
286 if( POLY1_UV16(poly1) ) {
287 vert->u = halftofloat( *data.ival>>16 );
288 vert->v = halftofloat( *data.ival );
291 vert->u = *data.fval++;
292 vert->v = *data.fval++;
295 switch( POLY2_TEX_BLEND(poly2) ) {
296 case 0:/* Convert replace => modulate by setting colour values to 1.0 */
297 vert->rgba[0] = vert->rgba[1] = vert->rgba[2] = vert->rgba[3] = 1.0;
298 vert->tex_mode = 0.0;
299 data.ival++; /* Skip the colour word */
302 vert->tex_mode = 1.0;
303 unpack_bgra(*data.ival++, vert->rgba);
309 vert->tex_mode = 0.0;
310 unpack_bgra(*data.ival++, vert->rgba);
313 vert->r = scene_get_palette_offset(tex);
315 vert->tex_mode = 2.0;
317 unpack_bgra(*data.ival++, vert->rgba);
320 if( POLY1_SPECULAR(poly1) ) {
321 unpack_bgra(*data.ival++, vert->offset_rgba);
323 vert->offset_rgba[0] = 0.0;
324 vert->offset_rgba[1] = 0.0;
325 vert->offset_rgba[2] = 0.0;
326 vert->offset_rgba[3] = 0.0;
335 * Compute texture, colour, and z values for 1 or more result points by interpolating from
336 * a set of 3 input points. The result point(s) must define their x,y.
338 static void scene_compute_vertexes( struct vertex_struct *result,
340 struct vertex_struct *input,
341 gboolean is_solid_shaded )
344 float sx = input[2].x - input[1].x;
345 float sy = input[2].y - input[1].y;
346 float tx = input[0].x - input[1].x;
347 float ty = input[0].y - input[1].y;
349 float detxy = ((sy) * (tx)) - ((ty) * (sx));
351 // If the input points fall on a line, they don't define a usable
352 // polygon - the PVR2 takes the last input point as the result in
354 for( i=0; i<result_count; i++ ) {
355 float x = result[i].x;
356 float y = result[i].y;
357 memcpy( &result[i], &input[2], sizeof(struct vertex_struct) );
363 float sz = input[2].z - input[1].z;
364 float tz = input[0].z - input[1].z;
365 float su = input[2].u - input[1].u;
366 float tu = input[0].u - input[1].u;
367 float sv = input[2].v - input[1].v;
368 float tv = input[0].v - input[1].v;
370 for( i=0; i<result_count; i++ ) {
371 float t = ((result[i].x - input[1].x) * sy -
372 (result[i].y - input[1].y) * sx) / detxy;
373 float s = ((result[i].y - input[1].y) * tx -
374 (result[i].x - input[1].x) * ty) / detxy;
376 float rz = input[1].z + (t*tz) + (s*sz);
377 if( rz > pvr2_scene.bounds[5] ) {
378 pvr2_scene.bounds[5] = rz;
379 } else if( rz < pvr2_scene.bounds[4] ) {
380 pvr2_scene.bounds[4] = rz;
383 result[i].u = input[1].u + (t*tu) + (s*su);
384 result[i].v = input[1].v + (t*tv) + (s*sv);
385 result[i].r = input[1].r; /* Last two components are flat */
386 result[i].tex_mode = input[1].tex_mode;
388 if( is_solid_shaded ) {
389 memcpy( result->rgba, input[2].rgba, sizeof(result->rgba) );
390 memcpy( result->offset_rgba, input[2].offset_rgba, sizeof(result->offset_rgba) );
392 float *rgba0 = input[0].rgba;
393 float *rgba1 = input[1].rgba;
394 float *rgba2 = input[2].rgba;
395 float *rgba3 = result[i].rgba;
396 for( j=0; j<8; j++ ) {
397 float tc = *rgba0++ - *rgba1;
398 float sc = *rgba2++ - *rgba1;
399 float rc = *rgba1++ + (t*tc) + (s*sc);
406 static float scene_compute_lut_fog_vertex( float z, float fog_density, float fog_table[][2] )
412 v.f = z * fog_density;
413 if( v.f < 1.0 ) v.f = 1.0;
414 else if( v.f > 255.9999 ) v.f = 255.9999;
416 uint32_t index = ((v.i >> 18) & 0x0F)|((v.i>>19)&0x70);
417 return fog_table[index][0];
421 * Compute the fog coefficients for all polygons using lookup-table fog. It's
422 * a little more convenient to do this as a separate pass, since we don't have
423 * to worry about computed vertexes.
425 static void scene_compute_lut_fog( )
429 float fog_density = parse_fog_density(MMIO_READ( PVR2, RENDER_FOGCOEFF ));
430 float fog_table[128][2];
432 /* Parse fog table out into floating-point format */
433 for( i=0; i<128; i++ ) {
434 uint32_t ent = MMIO_READ( PVR2, RENDER_FOGTABLE + (i<<2) );
435 fog_table[i][0] = ((float)(((ent&0x0000FF00)>>8) + 1)) / 256.0;
436 fog_table[i][1] = ((float)((ent&0x000000FF) + 1)) / 256.0;
440 for( i=0; i<pvr2_scene.poly_count; i++ ) {
441 int mode = POLY2_FOG_MODE(pvr2_scene.poly_array[i].context[1]);
442 uint32_t index = pvr2_scene.poly_array[i].vertex_index;
443 if( mode == PVR2_POLY_FOG_LOOKUP ) {
444 for( j=0; j<pvr2_scene.poly_array[i].vertex_count; j++ ) {
445 pvr2_scene.vertex_array[index+j].offset_rgba[3] =
446 scene_compute_lut_fog_vertex( pvr2_scene.vertex_array[index+j].z, fog_density, fog_table );
448 } else if( mode == PVR2_POLY_FOG_LOOKUP2 ) {
449 for( j=0; j<pvr2_scene.poly_array[i].vertex_count; j++ ) {
450 pvr2_scene.vertex_array[index+j].rgba[0] = pvr2_scene.fog_lut_colour[0];
451 pvr2_scene.vertex_array[index+j].rgba[1] = pvr2_scene.fog_lut_colour[1];
452 pvr2_scene.vertex_array[index+j].rgba[2] = pvr2_scene.fog_lut_colour[2];
453 pvr2_scene.vertex_array[index+j].rgba[3] =
454 scene_compute_lut_fog_vertex( pvr2_scene.vertex_array[index+j].z, fog_density, fog_table );
455 pvr2_scene.vertex_array[index+j].offset_rgba[3] = 0;
457 } else if( mode == PVR2_POLY_FOG_DISABLED ) {
458 for( j=0; j<pvr2_scene.poly_array[i].vertex_count; j++ ) {
459 pvr2_scene.vertex_array[index+j].offset_rgba[3] = 0;
466 * Manually cull back-facing polygons where we can - this actually saves
467 * us a lot of time vs passing everything to GL to do it.
469 static void scene_backface_cull()
472 unsigned poly_count = pvr2_scene.poly_count; /* Note: we don't want to process any sub-polygons created here */
473 for( poly_idx = 0; poly_idx<poly_count; poly_idx++ ) {
474 uint32_t poly1 = pvr2_scene.poly_array[poly_idx].context[0];
475 if( POLY1_CULL_ENABLE(poly1) ) {
476 struct polygon_struct *poly = &pvr2_scene.poly_array[poly_idx];
477 unsigned vert_idx = poly->vertex_index;
478 unsigned tri_count = poly->vertex_count-2;
479 struct vertex_struct *vert = &pvr2_scene.vertex_array[vert_idx];
481 gboolean ccw = (POLY1_CULL_MODE(poly1) == CULL_CCW);
482 int first_visible = -1, last_visible = -1;
483 for( i=0; i<tri_count; i++ ) {
484 float ux = vert[i+1].x - vert[i].x;
485 float uy = vert[i+1].y - vert[i].y;
486 float vx = vert[i+2].x - vert[i].x;
487 float vy = vert[i+2].y - vert[i].y;
488 float nz = (ux*vy) - (uy*vx);
489 if( ccw ? nz > 0 : nz < 0 ) {
490 /* Surface is visible */
491 if( first_visible == -1 ) {
493 /* Elide the initial hidden triangles (note we don't
494 * need to care about winding anymore here) */
495 poly->vertex_index += i;
496 poly->vertex_count -= i;
497 if( poly->mod_vertex_index != -1 )
498 poly->mod_vertex_index += i;
499 } else if( last_visible != i-1 ) {
500 /* And... here we have to split the polygon. Allocate a new
501 * sub-polygon to hold the vertex references */
502 struct polygon_struct *sub = scene_split_subpolygon(poly, (i-first_visible));
503 poly->vertex_count -= (i-first_visible-1) - last_visible;
509 /* Invert ccw flag for triangle strip processing */
512 if( last_visible == -1 ) {
513 /* No visible surfaces, so we can mark the whole polygon as being vertex-less */
514 poly->vertex_count = 0;
515 } else if( last_visible != tri_count-1 ) {
516 /* Remove final hidden tris */
517 poly->vertex_count -= (tri_count - 1 - last_visible);
523 static void scene_add_cheap_shadow_vertexes( struct vertex_struct *src, struct vertex_struct *dest, int count )
527 for( i=0; i<count; i++ ) {
534 dest->tex_mode = src->tex_mode;
535 dest->rgba[0] = src->rgba[0] * scene_shadow_intensity;
536 dest->rgba[1] = src->rgba[1] * scene_shadow_intensity;
537 dest->rgba[2] = src->rgba[2] * scene_shadow_intensity;
538 dest->rgba[3] = src->rgba[3] * scene_shadow_intensity;
539 dest->offset_rgba[0] = src->offset_rgba[0] * scene_shadow_intensity;
540 dest->offset_rgba[1] = src->offset_rgba[1] * scene_shadow_intensity;
541 dest->offset_rgba[2] = src->offset_rgba[2] * scene_shadow_intensity;
542 dest->offset_rgba[3] = src->offset_rgba[3];
548 static void scene_add_vertexes( pvraddr_t poly_idx, int vertex_length,
549 shadow_mode_t is_modified )
551 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
552 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
553 uint32_t *context = ptr;
556 if( poly->vertex_index == -1 ) {
557 ptr += (is_modified == SHADOW_FULL ? 5 : 3 );
558 poly->vertex_index = pvr2_scene.vertex_index;
560 assert( poly != NULL );
561 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
562 for( i=0; i<poly->vertex_count; i++ ) {
563 pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[1], context[2], ptr, 0 );
564 ptr += vertex_length;
567 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
568 poly->mod_vertex_index = pvr2_scene.vertex_index;
569 if( is_modified == SHADOW_FULL ) {
570 int mod_offset = (vertex_length - 3)>>1;
571 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
572 for( i=0; i<poly->vertex_count; i++ ) {
573 pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[3], context[4], ptr, mod_offset );
574 ptr += vertex_length;
577 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index],
578 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
579 pvr2_scene.vertex_index += poly->vertex_count;
585 static void scene_add_quad_vertexes( pvraddr_t poly_idx, int vertex_length,
586 shadow_mode_t is_modified )
588 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
589 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
590 uint32_t *context = ptr;
593 if( poly->vertex_index == -1 ) {
594 // Construct it locally and copy to the vertex buffer, as the VBO is
595 // allowed to be horribly slow for reads (ie it could be direct-mapped
597 struct vertex_struct quad[4];
599 assert( poly != NULL );
600 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
601 ptr += (is_modified == SHADOW_FULL ? 5 : 3 );
602 poly->vertex_index = pvr2_scene.vertex_index;
603 for( i=0; i<4; i++ ) {
604 pvr2_decode_render_vertex( &quad[i], context[0], context[1], context[2], ptr, 0 );
605 ptr += vertex_length;
607 scene_compute_vertexes( &quad[3], 1, &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
608 // Swap last two vertexes (quad arrangement => tri strip arrangement)
609 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
610 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
611 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
612 pvr2_scene.vertex_index += 4;
615 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
616 poly->mod_vertex_index = pvr2_scene.vertex_index;
617 if( is_modified == SHADOW_FULL ) {
618 int mod_offset = (vertex_length - 3)>>1;
619 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
620 for( i=0; i<4; i++ ) {
621 pvr2_decode_render_vertex( &quad[4], context[0], context[3], context[4], ptr, mod_offset );
622 ptr += vertex_length;
624 scene_compute_vertexes( &quad[3], 1, &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
625 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
626 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
627 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
629 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index],
630 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
631 pvr2_scene.vertex_index += poly->vertex_count;
633 pvr2_scene.vertex_index += 4;
638 static void scene_extract_polygons( pvraddr_t tile_entry )
640 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
642 uint32_t entry = *tile_list++;
643 if( entry >> 28 == 0x0F ) {
645 } else if( entry >> 28 == 0x0E ) {
646 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
648 pvraddr_t polyaddr = entry&0x000FFFFF;
649 shadow_mode_t is_modified = (entry & 0x01000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
650 int vertex_length = (entry >> 21) & 0x07;
651 int context_length = 3;
652 if( is_modified == SHADOW_FULL ) {
654 vertex_length <<= 1 ;
658 if( (entry & 0xE0000000) == 0x80000000 ) {
660 int strip_count = ((entry >> 25) & 0x0F)+1;
661 int polygon_length = 3 * vertex_length + context_length;
663 struct polygon_struct *last_poly = NULL;
664 for( i=0; i<strip_count; i++ ) {
665 struct polygon_struct *poly = scene_add_polygon( polyaddr, 3, is_modified );
666 polyaddr += polygon_length;
667 if( last_poly != NULL && last_poly->next == NULL ) {
668 last_poly->next = poly;
672 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
674 int strip_count = ((entry >> 25) & 0x0F)+1;
675 int polygon_length = 4 * vertex_length + context_length;
677 struct polygon_struct *last_poly = NULL;
678 for( i=0; i<strip_count; i++ ) {
679 struct polygon_struct *poly = scene_add_polygon( polyaddr, 4, is_modified );
680 polyaddr += polygon_length;
681 if( last_poly != NULL && last_poly->next == NULL ) {
682 last_poly->next = poly;
689 for( i=5; i>=0; i-- ) {
690 if( entry & (0x40000000>>i) ) {
696 scene_add_polygon( polyaddr, last+3, is_modified );
703 static void scene_extract_vertexes( pvraddr_t tile_entry )
705 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
707 uint32_t entry = *tile_list++;
708 if( entry >> 28 == 0x0F ) {
710 } else if( entry >> 28 == 0x0E ) {
711 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
713 pvraddr_t polyaddr = entry&0x000FFFFF;
714 shadow_mode_t is_modified = (entry & 0x01000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
715 int vertex_length = (entry >> 21) & 0x07;
716 int context_length = 3;
717 if( is_modified == SHADOW_FULL ) {
723 if( (entry & 0xE0000000) == 0x80000000 ) {
725 int strip_count = ((entry >> 25) & 0x0F)+1;
726 int polygon_length = 3 * vertex_length + context_length;
728 for( i=0; i<strip_count; i++ ) {
729 scene_add_vertexes( polyaddr, vertex_length, is_modified );
730 polyaddr += polygon_length;
732 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
734 int strip_count = ((entry >> 25) & 0x0F)+1;
735 int polygon_length = 4 * vertex_length + context_length;
737 for( i=0; i<strip_count; i++ ) {
738 scene_add_quad_vertexes( polyaddr, vertex_length, is_modified );
739 polyaddr += polygon_length;
744 for( i=5; i>=0; i-- ) {
745 if( entry & (0x40000000>>i) ) {
751 scene_add_vertexes( polyaddr, vertex_length, is_modified );
758 static void scene_extract_background( void )
760 uint32_t bgplane = MMIO_READ(PVR2, RENDER_BGPLANE);
761 int vertex_length = (bgplane >> 24) & 0x07;
762 int context_length = 3, i;
763 shadow_mode_t is_modified = (bgplane & 0x08000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
765 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
766 uint32_t *context = &pvr2_scene.pvr2_pbuf[(bgplane & 0x00FFFFFF)>>3];
767 poly->context = context;
768 poly->vertex_count = 4;
769 poly->vertex_index = pvr2_scene.vertex_count;
770 if( is_modified == SHADOW_FULL ) {
774 if( is_modified != SHADOW_NONE ) {
775 poly->mod_vertex_index = pvr2_scene.vertex_count + 4;
776 pvr2_scene.vertex_count += 8;
778 poly->mod_vertex_index = -1;
779 pvr2_scene.vertex_count += 4;
782 context_length += (bgplane & 0x07) * vertex_length;
785 poly->sub_next = NULL;
786 pvr2_scene.bkgnd_poly = poly;
788 struct vertex_struct base_vertexes[3];
789 uint32_t *ptr = context + context_length;
790 for( i=0; i<3; i++ ) {
791 pvr2_decode_render_vertex( &base_vertexes[i], context[0], context[1], context[2],
793 ptr += vertex_length;
795 struct vertex_struct *result_vertexes = &pvr2_scene.vertex_array[poly->vertex_index];
796 result_vertexes[0].x = result_vertexes[0].y = 0;
797 result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
798 result_vertexes[1].y = result_vertexes[2].x = 0;
799 result_vertexes[2].y = result_vertexes[3].y = pvr2_scene.buffer_height;
800 scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
802 if( is_modified == SHADOW_FULL ) {
803 int mod_offset = (vertex_length - 3)>>1;
804 ptr = context + context_length;
805 for( i=0; i<3; i++ ) {
806 pvr2_decode_render_vertex( &base_vertexes[i], context[0], context[3], context[4],
808 ptr += vertex_length;
810 result_vertexes = &pvr2_scene.vertex_array[poly->mod_vertex_index];
811 result_vertexes[0].x = result_vertexes[0].y = 0;
812 result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
813 result_vertexes[1].y = result_vertexes[2].x = 0;
814 result_vertexes[2].y = result_vertexes[3].y = pvr2_scene.buffer_height;
815 scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
816 } else if( is_modified == SHADOW_CHEAP ) {
817 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index],
818 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
819 pvr2_scene.vertex_index += poly->vertex_count;
825 uint32_t pvr2_scene_buffer_width()
827 return pvr2_scene.buffer_width;
830 uint32_t pvr2_scene_buffer_height()
832 return pvr2_scene.buffer_height;
836 * Extract the current scene into the rendering structures. We run two passes
837 * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts),
838 * second pass extracts the vertex data into the VBO/vertex array.
840 * Difficult to do in single pass as we don't generally know the size of a
841 * polygon for certain until we've seen all tiles containing it. It also means we
842 * can count the vertexes and allocate the appropriate size VBO.
844 * FIXME: accesses into VRAM need to be bounds-checked properly
846 void pvr2_scene_read( void )
851 pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
852 pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
853 pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
854 pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
855 pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
857 uint32_t scaler = MMIO_READ( PVR2, RENDER_SCALER );
858 if( scaler & SCALER_HSCALE ) {
859 /* If the horizontal scaler is in use, we're (in principle) supposed to
860 * divide everything by 2. However in the interests of display quality,
861 * instead we want to render to the unscaled resolution and downsample
862 * only if/when required.
864 pvr2_scene.bounds[1] *= 2;
867 uint32_t fog_col = MMIO_READ( PVR2, RENDER_FOGTBLCOL );
868 unpack_bgra( fog_col, pvr2_scene.fog_lut_colour );
869 fog_col = MMIO_READ( PVR2, RENDER_FOGVRTCOL );
870 unpack_bgra( fog_col, pvr2_scene.fog_vert_colour );
872 uint32_t *tilebuffer = (uint32_t *)(pvr2_main_ram + MMIO_READ( PVR2, RENDER_TILEBASE ));
873 uint32_t *segment = tilebuffer;
874 uint32_t shadow = MMIO_READ(PVR2,RENDER_SHADOW);
875 pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
876 pvr2_scene.pvr2_pbuf = (uint32_t *)(pvr2_main_ram + MMIO_READ(PVR2,RENDER_POLYBASE));
877 pvr2_scene.shadow_mode = shadow & 0x100 ? SHADOW_CHEAP : SHADOW_FULL;
878 scene_shadow_intensity = U8TOFLOAT(shadow&0xFF);
882 int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
883 int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
885 if( (obj_config & 0x00200000) == 0 ) {
886 if( isp_config & 1 ) {
887 pvr2_scene.sort_mode = SORT_NEVER;
889 pvr2_scene.sort_mode = SORT_ALWAYS;
892 pvr2_scene.sort_mode = SORT_TILEFLAG;
895 // Pass 1: Extract polygon list
899 control = *segment++;
900 int tile_x = SEGMENT_X(control);
901 int tile_y = SEGMENT_Y(control);
902 if( tile_x > max_tile_x ) {
905 if( tile_y > max_tile_y ) {
908 for( i=0; i<5; i++ ) {
909 if( (*segment & NO_POINTER) == 0 ) {
910 scene_extract_polygons( *segment );
914 } while( (control & SEGMENT_END) == 0 );
916 pvr2_scene.buffer_width = (max_tile_x+1)<<5;
917 pvr2_scene.buffer_height = (max_tile_y+1)<<5;
919 // Pass 2: Extract vertex data
921 pvr2_scene.vertex_index = 0;
922 segment = tilebuffer;
924 control = *segment++;
925 for( i=0; i<5; i++ ) {
926 if( (*segment & NO_POINTER) == 0 ) {
927 scene_extract_vertexes( *segment );
931 } while( (control & SEGMENT_END) == 0 );
933 scene_extract_background();
934 scene_compute_lut_fog();
935 scene_backface_cull();
937 vertex_buffer_unmap();
941 * Dump the current scene to file in a (mostly) human readable form
943 void pvr2_scene_print( FILE *f )
947 fprintf( f, "Polygons: %d\n", pvr2_scene.poly_count );
948 for( i=0; i<pvr2_scene.poly_count; i++ ) {
949 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
950 fprintf( f, " %08X ", (uint32_t)(((unsigned char *)poly->context) - pvr2_main_ram) );
951 switch( poly->vertex_count ) {
952 case 3: fprintf( f, "Tri " ); break;
953 case 4: fprintf( f, "Quad " ); break;
954 default: fprintf( f,"%d-Strip ", poly->vertex_count-2 ); break;
956 fprintf( f, "%08X %08X %08X ", poly->context[0], poly->context[1], poly->context[2] );
957 if( poly->mod_vertex_index != -1 ) {
958 fprintf( f, "%08X %08X\n", poly->context[3], poly->context[5] );
963 for( j=0; j<poly->vertex_count; j++ ) {
964 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->vertex_index+j];
965 fprintf( f, " %.5f %.5f %.5f, (%.5f,%.5f) %.5f,%.5f,%.5f,%.5f %.5f %.5f %.5f %.5f\n", v->x, v->y, v->z, v->u, v->v,
966 v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
967 v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
969 if( poly->mod_vertex_index != -1 ) {
970 fprintf( f, " ---\n" );
971 for( j=0; j<poly->vertex_count; j++ ) {
972 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->mod_vertex_index+j];
973 fprintf( f, " %.5f %.5f %.5f, (%.5f,%.5f) %.5f,%.5f,%.5f,%.5f %.5f %.5f %.5f %.5f\n", v->x, v->y, v->z, v->u, v->v,
974 v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
975 v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
982 void pvr2_scene_dump()
984 pvr2_scene_print(stdout);
.