4 * Manage the internal vertex/polygon buffers and scene data structure.
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.
24 #include "pvr2/pvr2.h"
25 #include "pvr2/pvr2mmio.h"
26 #include "pvr2/glutil.h"
27 #include "pvr2/scene.h"
29 #define U8TOFLOAT(n) (((float)((n)+1))/256.0)
30 #define POLY_IDX(addr) ( ((uint32_t *)addr) - ((uint32_t *)pvr2_scene.pvr2_pbuf))
32 static void unpack_bgra(uint32_t bgra, float *rgba)
34 rgba[0] = ((float)(((bgra&0x00FF0000)>>16) + 1)) / 256.0;
35 rgba[1] = ((float)(((bgra&0x0000FF00)>>8) + 1)) / 256.0;
36 rgba[2] = ((float)((bgra&0x000000FF) + 1)) / 256.0;
37 rgba[3] = ((float)(((bgra&0xFF000000)>>24) + 1)) / 256.0;
40 static inline uint32_t bgra_to_rgba(uint32_t bgra)
42 return (bgra&0xFF00FF00) | ((bgra&0x00FF0000)>>16) | ((bgra&0x000000FF)<<16);
46 * Convert a half-float (16-bit) FP number to a regular 32-bit float.
47 * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
48 * TODO: Check the correctness of this.
50 static float halftofloat( uint16_t half )
56 temp.i = ((uint32_t)half)<<16;
60 static float parse_fog_density( uint32_t value )
66 u.i = (((value+127)&0xFF)<<23)|((value & 0xFF00)<<7);
70 struct pvr2_scene_struct pvr2_scene;
71 static float scene_shadow_intensity = 0.0;
72 static vertex_buffer_t vbuf = NULL;
74 static void vertex_buffer_map()
76 // Allow 8 vertexes for the background (4+4)
77 uint32_t size = (pvr2_scene.vertex_count + 8) * sizeof(struct vertex_struct);
78 pvr2_scene.vertex_array = vbuf->map(vbuf, size);
81 static void vertex_buffer_unmap()
83 pvr2_scene.vertex_array = vbuf->unmap(vbuf);
87 * Allocate vertex buffer + temporary structures. GL context must have been initialized before this
90 void pvr2_scene_init()
93 vbuf = display_driver->create_vertex_buffer();
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 );
102 * Clear the scene data structures in preparation for fresh data
104 void pvr2_scene_reset()
106 /* Faster to just clear the active entries */
107 for( int i=0; i<pvr2_scene.poly_count; i++ ) {
108 pvr2_scene.buf_to_poly_map[POLY_IDX(pvr2_scene.poly_array[i].context)] = 0;
110 pvr2_scene.poly_count = 0;
111 pvr2_scene.vertex_count = 0;
114 void pvr2_scene_shutdown()
118 g_free( pvr2_scene.poly_array );
119 pvr2_scene.poly_array = NULL;
120 g_free( pvr2_scene.buf_to_poly_map );
121 pvr2_scene.buf_to_poly_map = NULL;
124 static struct polygon_struct *scene_add_polygon( pvraddr_t poly_idx, int vertex_count,
125 shadow_mode_t is_modified )
127 int vert_mul = is_modified != SHADOW_NONE ? 2 : 1;
129 if( pvr2_scene.buf_to_poly_map[poly_idx] != NULL ) {
130 if( vertex_count > pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count ) {
131 pvr2_scene.vertex_count += (vertex_count - pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count) * vert_mul;
132 pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count = vertex_count;
134 return pvr2_scene.buf_to_poly_map[poly_idx];
136 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
137 poly->context = &pvr2_scene.pvr2_pbuf[poly_idx];
138 poly->vertex_count = vertex_count;
139 poly->vertex_index = -1;
140 poly->mod_vertex_index = -1;
142 poly->sub_next = NULL;
143 pvr2_scene.buf_to_poly_map[poly_idx] = poly;
144 pvr2_scene.vertex_count += (vertex_count * vert_mul);
150 * Given a starting polygon, break it at the specified triangle so that the
151 * preceding triangles are retained, and the remainder are contained in a
152 * new sub-polygon. Does not preserve winding.
154 static struct polygon_struct *scene_split_subpolygon( struct polygon_struct *parent, int split_offset )
156 assert( split_offset > 0 && split_offset < (parent->vertex_count-2) );
157 assert( pvr2_scene.poly_count < MAX_POLYGONS );
158 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
159 poly->vertex_count = parent->vertex_count - split_offset;
160 poly->vertex_index = parent->vertex_index + split_offset;
161 if( parent->mod_vertex_index == -1 ) {
162 poly->mod_vertex_index = -1;
164 poly->mod_vertex_index = parent->mod_vertex_index + split_offset;
166 poly->context = parent->context;
168 poly->sub_next = parent->sub_next;
170 parent->sub_next = poly;
171 parent->vertex_count = split_offset + 2;
176 static float scene_get_palette_offset( uint32_t tex )
178 uint32_t fmt = (tex & PVR2_TEX_FORMAT_MASK);
179 if( fmt == PVR2_TEX_FORMAT_IDX4 ) {
180 return ((float)((tex & 0x07E00000) >> 17))/1024.0 + 0.0002;
181 } else if( fmt == PVR2_TEX_FORMAT_IDX8 ) {
182 return ((float)((tex & 0x06000000) >> 17))/1024.0 + 0.0002;
189 * Decode a single PVR2 renderable vertex (opaque/trans/punch-out, but not shadow
191 * @param vert Pointer to output vertex structure
192 * @param poly1 First word of polygon context (needed to understand vertex)
193 * @param poly2 Second word of polygon context
194 * @param pvr2_data Pointer to raw pvr2 vertex data (in VRAM)
195 * @param modify_offset Offset in 32-bit words to the tex/color data. 0 for
196 * the normal vertex, half the vertex length for the modified vertex.
198 static void scene_decode_vertex( struct vertex_struct *vert, uint32_t poly1,
199 uint32_t poly2, uint32_t tex, uint32_t *pvr2_data,
202 gboolean force_alpha = !POLY2_ALPHA_ENABLE(poly2);
203 union pvr2_data_type {
208 data.ival = pvr2_data;
210 vert->x = *data.fval++;
211 vert->y = *data.fval++;
213 float z = *data.fval++;
216 } else if( z != 0 ) {
219 if( z > pvr2_scene.bounds[5] ) {
220 pvr2_scene.bounds[5] = z;
221 } else if( z < pvr2_scene.bounds[4] && z != 0 ) {
222 pvr2_scene.bounds[4] = z;
225 data.ival += modify_offset;
228 if( POLY1_TEXTURED(poly1) ) {
229 if( POLY1_UV16(poly1) ) {
230 vert->u = halftofloat( *data.ival>>16 );
231 vert->v = halftofloat( *data.ival );
234 vert->u = *data.fval++;
235 vert->v = *data.fval++;
238 switch( POLY2_TEX_BLEND(poly2) ) {
239 case 0:/* Convert replace => modulate by setting colour values to 1.0 */
240 vert->rgba[0] = vert->rgba[1] = vert->rgba[2] = vert->rgba[3] = 1.0;
241 vert->tex_mode = 0.0;
242 data.ival++; /* Skip the colour word */
245 vert->tex_mode = 1.0;
246 unpack_bgra(*data.ival++, vert->rgba);
252 vert->tex_mode = 0.0;
253 unpack_bgra(*data.ival++, vert->rgba);
256 vert->r = scene_get_palette_offset(tex);
258 vert->tex_mode = 2.0;
260 unpack_bgra(*data.ival++, vert->rgba);
263 if( POLY1_SPECULAR(poly1) ) {
264 unpack_bgra(*data.ival++, vert->offset_rgba);
266 vert->offset_rgba[0] = 0.0;
267 vert->offset_rgba[1] = 0.0;
268 vert->offset_rgba[2] = 0.0;
269 vert->offset_rgba[3] = 0.0;
278 * Compute texture, colour, and z values for 1 or more result points by interpolating from
279 * a set of 3 input points. The result point(s) must define their x,y.
281 static void scene_compute_vertexes( struct vertex_struct *result,
283 struct vertex_struct *input,
284 gboolean is_solid_shaded )
287 float sx = input[2].x - input[1].x;
288 float sy = input[2].y - input[1].y;
289 float tx = input[0].x - input[1].x;
290 float ty = input[0].y - input[1].y;
292 float detxy = ((sy) * (tx)) - ((ty) * (sx));
294 // If the input points fall on a line, they don't define a usable
295 // polygon - the PVR2 takes the last input point as the result in
297 for( i=0; i<result_count; i++ ) {
298 float x = result[i].x;
299 float y = result[i].y;
300 memcpy( &result[i], &input[2], sizeof(struct vertex_struct) );
306 float sz = input[2].z - input[1].z;
307 float tz = input[0].z - input[1].z;
308 float su = input[2].u - input[1].u;
309 float tu = input[0].u - input[1].u;
310 float sv = input[2].v - input[1].v;
311 float tv = input[0].v - input[1].v;
313 for( i=0; i<result_count; i++ ) {
314 float t = ((result[i].x - input[1].x) * sy -
315 (result[i].y - input[1].y) * sx) / detxy;
316 float s = ((result[i].y - input[1].y) * tx -
317 (result[i].x - input[1].x) * ty) / detxy;
319 float rz = input[1].z + (t*tz) + (s*sz);
320 if( rz > pvr2_scene.bounds[5] ) {
321 pvr2_scene.bounds[5] = rz;
322 } else if( rz < pvr2_scene.bounds[4] ) {
323 pvr2_scene.bounds[4] = rz;
326 result[i].u = input[1].u + (t*tu) + (s*su);
327 result[i].v = input[1].v + (t*tv) + (s*sv);
328 result[i].r = input[1].r; /* Last two components are flat */
329 result[i].tex_mode = input[1].tex_mode;
331 if( is_solid_shaded ) {
332 memcpy( result->rgba, input[2].rgba, sizeof(result->rgba) );
333 memcpy( result->offset_rgba, input[2].offset_rgba, sizeof(result->offset_rgba) );
335 float *rgba0 = input[0].rgba;
336 float *rgba1 = input[1].rgba;
337 float *rgba2 = input[2].rgba;
338 float *rgba3 = result[i].rgba;
339 for( j=0; j<8; j++ ) {
340 float tc = *rgba0++ - *rgba1;
341 float sc = *rgba2++ - *rgba1;
342 float rc = *rgba1++ + (t*tc) + (s*sc);
349 static float scene_compute_lut_fog_vertex( float z, float fog_density, float fog_table[][2] )
355 v.f = z * fog_density;
356 if( v.f < 1.0 ) v.f = 1.0;
357 else if( v.f > 255.9999 ) v.f = 255.9999;
359 uint32_t index = ((v.i >> 18) & 0x0F)|((v.i>>19)&0x70);
360 return fog_table[index][0];
364 * Compute the fog coefficients for all polygons using lookup-table fog. It's
365 * a little more convenient to do this as a separate pass, since we don't have
366 * to worry about computed vertexes.
368 static void scene_compute_lut_fog( )
372 float fog_density = parse_fog_density(MMIO_READ( PVR2, RENDER_FOGCOEFF ));
373 float fog_table[128][2];
375 /* Parse fog table out into floating-point format */
376 for( i=0; i<128; i++ ) {
377 uint32_t ent = MMIO_READ( PVR2, RENDER_FOGTABLE + (i<<2) );
378 fog_table[i][0] = ((float)(((ent&0x0000FF00)>>8) + 1)) / 256.0;
379 fog_table[i][1] = ((float)((ent&0x000000FF) + 1)) / 256.0;
383 for( i=0; i<pvr2_scene.poly_count; i++ ) {
384 int mode = POLY2_FOG_MODE(pvr2_scene.poly_array[i].context[1]);
385 uint32_t index = pvr2_scene.poly_array[i].vertex_index;
386 if( mode == PVR2_POLY_FOG_LOOKUP ) {
387 for( j=0; j<pvr2_scene.poly_array[i].vertex_count; j++ ) {
388 pvr2_scene.vertex_array[index+j].offset_rgba[3] =
389 scene_compute_lut_fog_vertex( pvr2_scene.vertex_array[index+j].z, fog_density, fog_table );
391 } else if( mode == PVR2_POLY_FOG_LOOKUP2 ) {
392 for( j=0; j<pvr2_scene.poly_array[i].vertex_count; j++ ) {
393 pvr2_scene.vertex_array[index+j].rgba[0] = pvr2_scene.fog_lut_colour[0];
394 pvr2_scene.vertex_array[index+j].rgba[1] = pvr2_scene.fog_lut_colour[1];
395 pvr2_scene.vertex_array[index+j].rgba[2] = pvr2_scene.fog_lut_colour[2];
396 pvr2_scene.vertex_array[index+j].rgba[3] =
397 scene_compute_lut_fog_vertex( pvr2_scene.vertex_array[index+j].z, fog_density, fog_table );
398 pvr2_scene.vertex_array[index+j].offset_rgba[3] = 0;
400 } else if( mode == PVR2_POLY_FOG_DISABLED ) {
401 for( j=0; j<pvr2_scene.poly_array[i].vertex_count; j++ ) {
402 pvr2_scene.vertex_array[index+j].offset_rgba[3] = 0;
409 * Manually cull back-facing polygons where we can - this actually saves
410 * us a lot of time vs passing everything to GL to do it.
412 static void scene_backface_cull()
415 unsigned poly_count = pvr2_scene.poly_count; /* Note: we don't want to process any sub-polygons created here */
416 for( poly_idx = 0; poly_idx<poly_count; poly_idx++ ) {
417 uint32_t poly1 = pvr2_scene.poly_array[poly_idx].context[0];
418 if( POLY1_CULL_ENABLE(poly1) ) {
419 struct polygon_struct *poly = &pvr2_scene.poly_array[poly_idx];
420 unsigned vert_idx = poly->vertex_index;
421 unsigned tri_count = poly->vertex_count-2;
422 struct vertex_struct *vert = &pvr2_scene.vertex_array[vert_idx];
424 gboolean ccw = (POLY1_CULL_MODE(poly1) == CULL_CCW);
425 int first_visible = -1, last_visible = -1;
426 for( i=0; i<tri_count; i++ ) {
427 float ux = vert[i+1].x - vert[i].x;
428 float uy = vert[i+1].y - vert[i].y;
429 float vx = vert[i+2].x - vert[i].x;
430 float vy = vert[i+2].y - vert[i].y;
431 float nz = (ux*vy) - (uy*vx);
432 if( ccw ? nz > 0 : nz < 0 ) {
433 /* Surface is visible */
434 if( first_visible == -1 ) {
436 /* Elide the initial hidden triangles (note we don't
437 * need to care about winding anymore here) */
438 poly->vertex_index += i;
439 poly->vertex_count -= i;
440 if( poly->mod_vertex_index != -1 )
441 poly->mod_vertex_index += i;
442 } else if( last_visible != i-1 ) {
443 /* And... here we have to split the polygon. Allocate a new
444 * sub-polygon to hold the vertex references */
445 struct polygon_struct *sub = scene_split_subpolygon(poly, (i-first_visible));
446 poly->vertex_count -= (i-first_visible-1) - last_visible;
452 /* Invert ccw flag for triangle strip processing */
455 if( last_visible == -1 ) {
456 /* No visible surfaces, so we can mark the whole polygon as being vertex-less */
457 poly->vertex_count = 0;
458 } else if( last_visible != tri_count-1 ) {
459 /* Remove final hidden tris */
460 poly->vertex_count -= (tri_count - 1 - last_visible);
466 static void scene_add_cheap_shadow_vertexes( struct vertex_struct *src, struct vertex_struct *dest, int count )
470 for( i=0; i<count; i++ ) {
477 dest->tex_mode = src->tex_mode;
478 dest->rgba[0] = src->rgba[0] * scene_shadow_intensity;
479 dest->rgba[1] = src->rgba[1] * scene_shadow_intensity;
480 dest->rgba[2] = src->rgba[2] * scene_shadow_intensity;
481 dest->rgba[3] = src->rgba[3] * scene_shadow_intensity;
482 dest->offset_rgba[0] = src->offset_rgba[0] * scene_shadow_intensity;
483 dest->offset_rgba[1] = src->offset_rgba[1] * scene_shadow_intensity;
484 dest->offset_rgba[2] = src->offset_rgba[2] * scene_shadow_intensity;
485 dest->offset_rgba[3] = src->offset_rgba[3];
491 static void scene_add_vertexes( pvraddr_t poly_idx, int vertex_length,
492 shadow_mode_t is_modified )
494 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
495 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
496 uint32_t *context = ptr;
499 if( poly->vertex_index == -1 ) {
500 ptr += (is_modified == SHADOW_FULL ? 5 : 3 );
501 poly->vertex_index = pvr2_scene.vertex_index;
503 assert( poly != NULL );
504 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
505 for( i=0; i<poly->vertex_count; i++ ) {
506 scene_decode_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[1], context[2], ptr, 0 );
507 ptr += vertex_length;
510 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
511 poly->mod_vertex_index = pvr2_scene.vertex_index;
512 if( is_modified == SHADOW_FULL ) {
513 int mod_offset = (vertex_length - 3)>>1;
514 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
515 for( i=0; i<poly->vertex_count; i++ ) {
516 scene_decode_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[3], context[4], ptr, mod_offset );
517 ptr += vertex_length;
520 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index],
521 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
522 pvr2_scene.vertex_index += poly->vertex_count;
528 static void scene_add_quad_vertexes( pvraddr_t poly_idx, int vertex_length,
529 shadow_mode_t is_modified )
531 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
532 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
533 uint32_t *context = ptr;
536 if( poly->vertex_index == -1 ) {
537 // Construct it locally and copy to the vertex buffer, as the VBO is
538 // allowed to be horribly slow for reads (ie it could be direct-mapped
540 struct vertex_struct quad[4];
542 assert( poly != NULL );
543 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
544 ptr += (is_modified == SHADOW_FULL ? 5 : 3 );
545 poly->vertex_index = pvr2_scene.vertex_index;
546 for( i=0; i<4; i++ ) {
547 scene_decode_vertex( &quad[i], context[0], context[1], context[2], ptr, 0 );
548 ptr += vertex_length;
550 scene_compute_vertexes( &quad[3], 1, &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
551 // Swap last two vertexes (quad arrangement => tri strip arrangement)
552 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
553 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
554 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
555 pvr2_scene.vertex_index += 4;
558 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
559 poly->mod_vertex_index = pvr2_scene.vertex_index;
560 if( is_modified == SHADOW_FULL ) {
561 int mod_offset = (vertex_length - 3)>>1;
562 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
563 for( i=0; i<4; i++ ) {
564 scene_decode_vertex( &quad[4], context[0], context[3], context[4], ptr, mod_offset );
565 ptr += vertex_length;
567 scene_compute_vertexes( &quad[3], 1, &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
568 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
569 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
570 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
572 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index],
573 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
574 pvr2_scene.vertex_index += poly->vertex_count;
576 pvr2_scene.vertex_index += 4;
581 static void scene_extract_polygons( pvraddr_t tile_entry )
583 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
585 uint32_t entry = *tile_list++;
586 if( entry >> 28 == 0x0F ) {
588 } else if( entry >> 28 == 0x0E ) {
589 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
591 pvraddr_t polyaddr = entry&0x000FFFFF;
592 shadow_mode_t is_modified = (entry & 0x01000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
593 int vertex_length = (entry >> 21) & 0x07;
594 int context_length = 3;
595 if( is_modified == SHADOW_FULL ) {
597 vertex_length <<= 1 ;
601 if( (entry & 0xE0000000) == 0x80000000 ) {
603 int strip_count = ((entry >> 25) & 0x0F)+1;
604 int polygon_length = 3 * vertex_length + context_length;
606 struct polygon_struct *last_poly = NULL;
607 for( i=0; i<strip_count; i++ ) {
608 struct polygon_struct *poly = scene_add_polygon( polyaddr, 3, is_modified );
609 polyaddr += polygon_length;
610 if( last_poly != NULL && last_poly->next == NULL ) {
611 last_poly->next = poly;
615 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
617 int strip_count = ((entry >> 25) & 0x0F)+1;
618 int polygon_length = 4 * vertex_length + context_length;
620 struct polygon_struct *last_poly = NULL;
621 for( i=0; i<strip_count; i++ ) {
622 struct polygon_struct *poly = scene_add_polygon( polyaddr, 4, is_modified );
623 polyaddr += polygon_length;
624 if( last_poly != NULL && last_poly->next == NULL ) {
625 last_poly->next = poly;
632 for( i=5; i>=0; i-- ) {
633 if( entry & (0x40000000>>i) ) {
639 scene_add_polygon( polyaddr, last+3, is_modified );
646 static void scene_extract_vertexes( pvraddr_t tile_entry )
648 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
650 uint32_t entry = *tile_list++;
651 if( entry >> 28 == 0x0F ) {
653 } else if( entry >> 28 == 0x0E ) {
654 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
656 pvraddr_t polyaddr = entry&0x000FFFFF;
657 shadow_mode_t is_modified = (entry & 0x01000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
658 int vertex_length = (entry >> 21) & 0x07;
659 int context_length = 3;
660 if( is_modified == SHADOW_FULL ) {
666 if( (entry & 0xE0000000) == 0x80000000 ) {
668 int strip_count = ((entry >> 25) & 0x0F)+1;
669 int polygon_length = 3 * vertex_length + context_length;
671 for( i=0; i<strip_count; i++ ) {
672 scene_add_vertexes( polyaddr, vertex_length, is_modified );
673 polyaddr += polygon_length;
675 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
677 int strip_count = ((entry >> 25) & 0x0F)+1;
678 int polygon_length = 4 * vertex_length + context_length;
680 for( i=0; i<strip_count; i++ ) {
681 scene_add_quad_vertexes( polyaddr, vertex_length, is_modified );
682 polyaddr += polygon_length;
687 for( i=5; i>=0; i-- ) {
688 if( entry & (0x40000000>>i) ) {
694 scene_add_vertexes( polyaddr, vertex_length, is_modified );
701 static void scene_extract_background( void )
703 uint32_t bgplane = MMIO_READ(PVR2, RENDER_BGPLANE);
704 int vertex_length = (bgplane >> 24) & 0x07;
705 int context_length = 3, i;
706 shadow_mode_t is_modified = (bgplane & 0x08000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
708 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
709 uint32_t *context = &pvr2_scene.pvr2_pbuf[(bgplane & 0x00FFFFFF)>>3];
710 poly->context = context;
711 poly->vertex_count = 4;
712 poly->vertex_index = pvr2_scene.vertex_count;
713 if( is_modified == SHADOW_FULL ) {
717 if( is_modified != SHADOW_NONE ) {
718 poly->mod_vertex_index = pvr2_scene.vertex_count + 4;
719 pvr2_scene.vertex_count += 8;
721 poly->mod_vertex_index = -1;
722 pvr2_scene.vertex_count += 4;
725 context_length += (bgplane & 0x07) * vertex_length;
728 poly->sub_next = NULL;
729 pvr2_scene.bkgnd_poly = poly;
731 struct vertex_struct base_vertexes[3];
732 uint32_t *ptr = context + context_length;
733 for( i=0; i<3; i++ ) {
734 scene_decode_vertex( &base_vertexes[i], context[0], context[1], context[2],
736 ptr += vertex_length;
738 struct vertex_struct *result_vertexes = &pvr2_scene.vertex_array[poly->vertex_index];
739 result_vertexes[0].x = result_vertexes[0].y = 0;
740 result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
741 result_vertexes[1].y = result_vertexes[2].x = 0;
742 result_vertexes[2].y = result_vertexes[3].y = pvr2_scene.buffer_height;
743 scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
745 if( is_modified == SHADOW_FULL ) {
746 int mod_offset = (vertex_length - 3)>>1;
747 ptr = context + context_length;
748 for( i=0; i<3; i++ ) {
749 scene_decode_vertex( &base_vertexes[i], context[0], context[3], context[4],
751 ptr += vertex_length;
753 result_vertexes = &pvr2_scene.vertex_array[poly->mod_vertex_index];
754 result_vertexes[0].x = result_vertexes[0].y = 0;
755 result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
756 result_vertexes[1].y = result_vertexes[2].x = 0;
757 result_vertexes[2].y = result_vertexes[3].y = pvr2_scene.buffer_height;
758 scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
759 } else if( is_modified == SHADOW_CHEAP ) {
760 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index],
761 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
762 pvr2_scene.vertex_index += poly->vertex_count;
768 uint32_t pvr2_scene_buffer_width()
770 return pvr2_scene.buffer_width;
773 uint32_t pvr2_scene_buffer_height()
775 return pvr2_scene.buffer_height;
779 * Extract the current scene into the rendering structures. We run two passes
780 * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts),
781 * second pass extracts the vertex data into the VBO/vertex array.
783 * Difficult to do in single pass as we don't generally know the size of a
784 * polygon for certain until we've seen all tiles containing it. It also means we
785 * can count the vertexes and allocate the appropriate size VBO.
787 * FIXME: accesses into VRAM need to be bounds-checked properly
789 void pvr2_scene_read( void )
794 pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
795 pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
796 pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
797 pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
798 pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
800 uint32_t scaler = MMIO_READ( PVR2, RENDER_SCALER );
801 if( scaler & SCALER_HSCALE ) {
802 /* If the horizontal scaler is in use, we're (in principle) supposed to
803 * divide everything by 2. However in the interests of display quality,
804 * instead we want to render to the unscaled resolution and downsample
805 * only if/when required.
807 pvr2_scene.bounds[1] *= 2;
810 uint32_t fog_col = MMIO_READ( PVR2, RENDER_FOGTBLCOL );
811 unpack_bgra( fog_col, pvr2_scene.fog_lut_colour );
812 fog_col = MMIO_READ( PVR2, RENDER_FOGVRTCOL );
813 unpack_bgra( fog_col, pvr2_scene.fog_vert_colour );
815 uint32_t *tilebuffer = (uint32_t *)(pvr2_main_ram + MMIO_READ( PVR2, RENDER_TILEBASE ));
816 uint32_t *segment = tilebuffer;
817 uint32_t shadow = MMIO_READ(PVR2,RENDER_SHADOW);
818 pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
819 pvr2_scene.pvr2_pbuf = (uint32_t *)(pvr2_main_ram + MMIO_READ(PVR2,RENDER_POLYBASE));
820 pvr2_scene.shadow_mode = shadow & 0x100 ? SHADOW_CHEAP : SHADOW_FULL;
821 scene_shadow_intensity = U8TOFLOAT(shadow&0xFF);
825 int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
826 int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
828 if( (obj_config & 0x00200000) == 0 ) {
829 if( isp_config & 1 ) {
830 pvr2_scene.sort_mode = SORT_NEVER;
832 pvr2_scene.sort_mode = SORT_ALWAYS;
835 pvr2_scene.sort_mode = SORT_TILEFLAG;
838 // Pass 1: Extract polygon list
842 control = *segment++;
843 int tile_x = SEGMENT_X(control);
844 int tile_y = SEGMENT_Y(control);
845 if( tile_x > max_tile_x ) {
848 if( tile_y > max_tile_y ) {
851 for( i=0; i<5; i++ ) {
852 if( (*segment & NO_POINTER) == 0 ) {
853 scene_extract_polygons( *segment );
857 } while( (control & SEGMENT_END) == 0 );
859 pvr2_scene.buffer_width = (max_tile_x+1)<<5;
860 pvr2_scene.buffer_height = (max_tile_y+1)<<5;
862 // Pass 2: Extract vertex data
864 pvr2_scene.vertex_index = 0;
865 segment = tilebuffer;
867 control = *segment++;
868 for( i=0; i<5; i++ ) {
869 if( (*segment & NO_POINTER) == 0 ) {
870 scene_extract_vertexes( *segment );
874 } while( (control & SEGMENT_END) == 0 );
876 scene_extract_background();
877 scene_compute_lut_fog();
878 scene_backface_cull();
880 vertex_buffer_unmap();
883 void pvr2_scene_finished( )
885 vbuf->finished(vbuf);
889 * Dump the current scene to file in a (mostly) human readable form
891 void pvr2_scene_print( FILE *f )
895 fprintf( f, "Polygons: %d\n", pvr2_scene.poly_count );
896 for( i=0; i<pvr2_scene.poly_count; i++ ) {
897 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
898 fprintf( f, " %08X ", (uint32_t)(((unsigned char *)poly->context) - pvr2_main_ram) );
899 switch( poly->vertex_count ) {
900 case 3: fprintf( f, "Tri " ); break;
901 case 4: fprintf( f, "Quad " ); break;
902 default: fprintf( f,"%d-Strip ", poly->vertex_count-2 ); break;
904 fprintf( f, "%08X %08X %08X ", poly->context[0], poly->context[1], poly->context[2] );
905 if( poly->mod_vertex_index != -1 ) {
906 fprintf( f, "%08X %08X\n", poly->context[3], poly->context[5] );
911 for( j=0; j<poly->vertex_count; j++ ) {
912 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->vertex_index+j];
913 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,
914 v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
915 v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
917 if( poly->mod_vertex_index != -1 ) {
918 fprintf( f, " ---\n" );
919 for( j=0; j<poly->vertex_count; j++ ) {
920 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->mod_vertex_index+j];
921 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,
922 v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
923 v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
930 void pvr2_scene_dump()
932 pvr2_scene_print(stdout);
.