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;
41 * Convert a half-float (16-bit) FP number to a regular 32-bit float.
42 * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
43 * TODO: Check the correctness of this.
45 static float halftofloat( uint16_t half )
51 temp.i = ((uint32_t)half)<<16;
55 static float parse_fog_density( uint32_t value )
61 u.i = (((value+127)&0xFF)<<23)|((value & 0xFF00)<<7);
65 struct pvr2_scene_struct pvr2_scene;
66 static float scene_shadow_intensity = 0.0;
67 static vertex_buffer_t vbuf = NULL;
69 static void vertex_buffer_map()
71 // Allow 8 vertexes for the background (4+4)
72 uint32_t size = (pvr2_scene.vertex_count + 8) * sizeof(struct vertex_struct);
73 pvr2_scene.vertex_array = vbuf->map(vbuf, size);
76 static void vertex_buffer_unmap()
78 pvr2_scene.vertex_array = vbuf->unmap(vbuf);
82 * Allocate vertex buffer + temporary structures. GL context must have been initialized before this
85 void pvr2_scene_init()
88 vbuf = display_driver->create_vertex_buffer();
89 pvr2_scene.vertex_array = NULL;
90 pvr2_scene.vertex_array_size = 0;
91 pvr2_scene.poly_array = g_malloc( MAX_POLY_BUFFER_SIZE );
92 pvr2_scene.buf_to_poly_map = g_malloc0( BUF_POLY_MAP_SIZE );
97 * Clear the scene data structures in preparation for fresh data
99 void pvr2_scene_reset()
101 /* Faster to just clear the active entries */
102 for( int i=0; i<pvr2_scene.poly_count; i++ ) {
103 pvr2_scene.buf_to_poly_map[POLY_IDX(pvr2_scene.poly_array[i].context)] = 0;
105 pvr2_scene.poly_count = 0;
106 pvr2_scene.vertex_count = 0;
109 void pvr2_scene_shutdown()
113 g_free( pvr2_scene.poly_array );
114 pvr2_scene.poly_array = NULL;
115 g_free( pvr2_scene.buf_to_poly_map );
116 pvr2_scene.buf_to_poly_map = NULL;
119 static struct polygon_struct *scene_add_polygon( pvraddr_t poly_idx, int vertex_count,
120 shadow_mode_t is_modified )
122 int vert_mul = is_modified != SHADOW_NONE ? 2 : 1;
124 if( pvr2_scene.buf_to_poly_map[poly_idx] != NULL ) {
125 if( vertex_count > pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count ) {
126 pvr2_scene.vertex_count += (vertex_count - pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count) * vert_mul;
127 pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count = vertex_count;
129 return pvr2_scene.buf_to_poly_map[poly_idx];
131 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
132 poly->context = &pvr2_scene.pvr2_pbuf[poly_idx];
133 poly->vertex_count = vertex_count;
134 poly->vertex_index = -1;
135 poly->mod_vertex_index = -1;
137 poly->sub_next = NULL;
138 pvr2_scene.buf_to_poly_map[poly_idx] = poly;
139 pvr2_scene.vertex_count += (vertex_count * vert_mul);
145 * Given a starting polygon, break it at the specified triangle so that the
146 * preceding triangles are retained, and the remainder are contained in a
147 * new sub-polygon. Does not preserve winding.
149 static struct polygon_struct *scene_split_subpolygon( struct polygon_struct *parent, int split_offset )
151 assert( split_offset > 0 && split_offset < (parent->vertex_count-2) );
152 assert( pvr2_scene.poly_count < MAX_POLYGONS );
153 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
154 poly->vertex_count = parent->vertex_count - split_offset;
155 poly->vertex_index = parent->vertex_index + split_offset;
156 if( parent->mod_vertex_index == -1 ) {
157 poly->mod_vertex_index = -1;
159 poly->mod_vertex_index = parent->mod_vertex_index + split_offset;
161 poly->context = parent->context;
163 poly->sub_next = parent->sub_next;
165 parent->sub_next = poly;
166 parent->vertex_count = split_offset + 2;
171 static float scene_get_palette_offset( uint32_t tex )
173 uint32_t fmt = (tex & PVR2_TEX_FORMAT_MASK);
174 if( fmt == PVR2_TEX_FORMAT_IDX4 ) {
175 return ((float)((tex & 0x07E00000) >> 17))/1024.0 + 0.0002;
176 } else if( fmt == PVR2_TEX_FORMAT_IDX8 ) {
177 return ((float)((tex & 0x06000000) >> 17))/1024.0 + 0.0002;
184 * Decode a single PVR2 renderable vertex (opaque/trans/punch-out, but not shadow
186 * @param vert Pointer to output vertex structure
187 * @param poly1 First word of polygon context (needed to understand vertex)
188 * @param poly2 Second word of polygon context
189 * @param pvr2_data Pointer to raw pvr2 vertex data (in VRAM)
190 * @param modify_offset Offset in 32-bit words to the tex/color data. 0 for
191 * the normal vertex, half the vertex length for the modified vertex.
193 static void scene_decode_vertex( struct vertex_struct *vert, uint32_t poly1,
194 uint32_t poly2, uint32_t tex, uint32_t *pvr2_data,
197 gboolean force_alpha = !POLY2_ALPHA_ENABLE(poly2);
198 union pvr2_data_type {
203 data.ival = pvr2_data;
205 vert->x = *data.fval++;
206 vert->y = *data.fval++;
208 float z = *data.fval++;
211 } else if( z != 0 ) {
214 if( z > pvr2_scene.bounds[5] ) {
215 pvr2_scene.bounds[5] = z;
216 } else if( z < pvr2_scene.bounds[4] && z != 0 ) {
217 pvr2_scene.bounds[4] = z;
220 data.ival += modify_offset;
223 if( POLY1_TEXTURED(poly1) ) {
224 if( POLY1_UV16(poly1) ) {
225 vert->u = halftofloat( *data.ival>>16 );
226 vert->v = halftofloat( *data.ival );
229 vert->u = *data.fval++;
230 vert->v = *data.fval++;
233 switch( POLY2_TEX_BLEND(poly2) ) {
234 case 0:/* Convert replace => modulate by setting colour values to 1.0 */
235 vert->rgba[0] = vert->rgba[1] = vert->rgba[2] = vert->rgba[3] = 1.0;
236 vert->tex_mode = 0.0;
237 data.ival++; /* Skip the colour word */
240 vert->tex_mode = 1.0;
241 unpack_bgra(*data.ival++, vert->rgba);
247 vert->tex_mode = 0.0;
248 unpack_bgra(*data.ival++, vert->rgba);
251 vert->r = scene_get_palette_offset(tex);
253 vert->tex_mode = 2.0;
255 unpack_bgra(*data.ival++, vert->rgba);
258 if( POLY1_SPECULAR(poly1) ) {
259 unpack_bgra(*data.ival++, vert->offset_rgba);
261 vert->offset_rgba[0] = 0.0;
262 vert->offset_rgba[1] = 0.0;
263 vert->offset_rgba[2] = 0.0;
264 vert->offset_rgba[3] = 0.0;
273 * Compute texture, colour, and z values for 1 or more result points by interpolating from
274 * a set of 3 input points. The result point(s) must define their x,y.
276 static void scene_compute_vertexes( struct vertex_struct *result,
278 struct vertex_struct *input,
279 gboolean is_solid_shaded )
282 float sx = input[2].x - input[1].x;
283 float sy = input[2].y - input[1].y;
284 float tx = input[0].x - input[1].x;
285 float ty = input[0].y - input[1].y;
287 float detxy = ((sy) * (tx)) - ((ty) * (sx));
289 // If the input points fall on a line, they don't define a usable
290 // polygon - the PVR2 takes the last input point as the result in
292 for( i=0; i<result_count; i++ ) {
293 float x = result[i].x;
294 float y = result[i].y;
295 memcpy( &result[i], &input[2], sizeof(struct vertex_struct) );
301 float sz = input[2].z - input[1].z;
302 float tz = input[0].z - input[1].z;
303 float su = input[2].u - input[1].u;
304 float tu = input[0].u - input[1].u;
305 float sv = input[2].v - input[1].v;
306 float tv = input[0].v - input[1].v;
308 for( i=0; i<result_count; i++ ) {
309 float t = ((result[i].x - input[1].x) * sy -
310 (result[i].y - input[1].y) * sx) / detxy;
311 float s = ((result[i].y - input[1].y) * tx -
312 (result[i].x - input[1].x) * ty) / detxy;
314 float rz = input[1].z + (t*tz) + (s*sz);
315 if( rz > pvr2_scene.bounds[5] ) {
316 pvr2_scene.bounds[5] = rz;
317 } else if( rz < pvr2_scene.bounds[4] ) {
318 pvr2_scene.bounds[4] = rz;
321 result[i].u = input[1].u + (t*tu) + (s*su);
322 result[i].v = input[1].v + (t*tv) + (s*sv);
323 result[i].r = input[1].r; /* Last two components are flat */
324 result[i].tex_mode = input[1].tex_mode;
326 if( is_solid_shaded ) {
327 memcpy( result->rgba, input[2].rgba, sizeof(result->rgba) );
328 memcpy( result->offset_rgba, input[2].offset_rgba, sizeof(result->offset_rgba) );
330 float *rgba0 = input[0].rgba;
331 float *rgba1 = input[1].rgba;
332 float *rgba2 = input[2].rgba;
333 float *rgba3 = result[i].rgba;
334 for( j=0; j<8; j++ ) {
335 float tc = *rgba0++ - *rgba1;
336 float sc = *rgba2++ - *rgba1;
337 float rc = *rgba1++ + (t*tc) + (s*sc);
344 static float scene_compute_lut_fog_vertex( float z, float fog_density, float fog_table[][2] )
350 v.f = z * fog_density;
351 if( v.f < 1.0 ) v.f = 1.0;
352 else if( v.f > 255.9999 ) v.f = 255.9999;
354 uint32_t index = ((v.i >> 18) & 0x0F)|((v.i>>19)&0x70);
355 return fog_table[index][0];
359 * Compute the fog coefficients for all polygons using lookup-table fog. It's
360 * a little more convenient to do this as a separate pass, since we don't have
361 * to worry about computed vertexes.
363 static void scene_compute_lut_fog( )
367 float fog_density = parse_fog_density(MMIO_READ( PVR2, RENDER_FOGCOEFF ));
368 float fog_table[128][2];
370 /* Parse fog table out into floating-point format */
371 for( i=0; i<128; i++ ) {
372 uint32_t ent = MMIO_READ( PVR2, RENDER_FOGTABLE + (i<<2) );
373 fog_table[i][0] = ((float)(((ent&0x0000FF00)>>8) + 1)) / 256.0;
374 fog_table[i][1] = ((float)((ent&0x000000FF) + 1)) / 256.0;
378 for( i=0; i<pvr2_scene.poly_count; i++ ) {
379 int mode = POLY2_FOG_MODE(pvr2_scene.poly_array[i].context[1]);
380 uint32_t index = pvr2_scene.poly_array[i].vertex_index;
381 if( mode == PVR2_POLY_FOG_LOOKUP ) {
382 for( j=0; j<pvr2_scene.poly_array[i].vertex_count; j++ ) {
383 float fog = scene_compute_lut_fog_vertex( pvr2_scene.vertex_array[index+j].z, fog_density, fog_table );
384 if( display_driver->capabilities.has_sl )
385 pvr2_scene.vertex_array[index+j].offset_rgba[3] = -fog;
387 pvr2_scene.vertex_array[index+j].offset_rgba[3] = fog;
389 } else if( mode == PVR2_POLY_FOG_LOOKUP2 ) {
390 for( j=0; j<pvr2_scene.poly_array[i].vertex_count; j++ ) {
391 pvr2_scene.vertex_array[index+j].rgba[0] = pvr2_scene.fog_lut_colour[0];
392 pvr2_scene.vertex_array[index+j].rgba[1] = pvr2_scene.fog_lut_colour[1];
393 pvr2_scene.vertex_array[index+j].rgba[2] = pvr2_scene.fog_lut_colour[2];
394 pvr2_scene.vertex_array[index+j].rgba[3] =
395 scene_compute_lut_fog_vertex( pvr2_scene.vertex_array[index+j].z, fog_density, fog_table );
396 pvr2_scene.vertex_array[index+j].offset_rgba[3] = 0;
398 } else if( mode == PVR2_POLY_FOG_DISABLED ) {
399 for( j=0; j<pvr2_scene.poly_array[i].vertex_count; j++ ) {
400 pvr2_scene.vertex_array[index+j].offset_rgba[3] = 0;
407 * Manually cull back-facing polygons where we can - this actually saves
408 * us a lot of time vs passing everything to GL to do it.
410 static void scene_backface_cull()
413 unsigned poly_count = pvr2_scene.poly_count; /* Note: we don't want to process any sub-polygons created here */
414 for( poly_idx = 0; poly_idx<poly_count; poly_idx++ ) {
415 uint32_t poly1 = pvr2_scene.poly_array[poly_idx].context[0];
416 if( POLY1_CULL_ENABLE(poly1) ) {
417 struct polygon_struct *poly = &pvr2_scene.poly_array[poly_idx];
418 unsigned vert_idx = poly->vertex_index;
419 unsigned tri_count = poly->vertex_count-2;
420 struct vertex_struct *vert = &pvr2_scene.vertex_array[vert_idx];
422 gboolean ccw = (POLY1_CULL_MODE(poly1) == CULL_CCW);
423 int first_visible = -1, last_visible = -1;
424 for( i=0; i<tri_count; i++ ) {
425 float ux = vert[i+1].x - vert[i].x;
426 float uy = vert[i+1].y - vert[i].y;
427 float vx = vert[i+2].x - vert[i].x;
428 float vy = vert[i+2].y - vert[i].y;
429 float nz = (ux*vy) - (uy*vx);
430 if( ccw ? nz > 0 : nz < 0 ) {
431 /* Surface is visible */
432 if( first_visible == -1 ) {
434 /* Elide the initial hidden triangles (note we don't
435 * need to care about winding anymore here) */
436 poly->vertex_index += i;
437 poly->vertex_count -= i;
438 if( poly->mod_vertex_index != -1 )
439 poly->mod_vertex_index += i;
440 } else if( last_visible != i-1 ) {
441 /* And... here we have to split the polygon. Allocate a new
442 * sub-polygon to hold the vertex references */
443 struct polygon_struct *sub = scene_split_subpolygon(poly, (i-first_visible));
444 poly->vertex_count -= (i-first_visible-1) - last_visible;
450 /* Invert ccw flag for triangle strip processing */
453 if( last_visible == -1 ) {
454 /* No visible surfaces, so we can mark the whole polygon as being vertex-less */
455 poly->vertex_count = 0;
456 } else if( last_visible != tri_count-1 ) {
457 /* Remove final hidden tris */
458 poly->vertex_count -= (tri_count - 1 - last_visible);
464 static void scene_add_cheap_shadow_vertexes( struct vertex_struct *src, struct vertex_struct *dest, int count )
468 for( i=0; i<count; i++ ) {
475 dest->tex_mode = src->tex_mode;
476 dest->rgba[0] = src->rgba[0] * scene_shadow_intensity;
477 dest->rgba[1] = src->rgba[1] * scene_shadow_intensity;
478 dest->rgba[2] = src->rgba[2] * scene_shadow_intensity;
479 dest->rgba[3] = src->rgba[3] * scene_shadow_intensity;
480 dest->offset_rgba[0] = src->offset_rgba[0] * scene_shadow_intensity;
481 dest->offset_rgba[1] = src->offset_rgba[1] * scene_shadow_intensity;
482 dest->offset_rgba[2] = src->offset_rgba[2] * scene_shadow_intensity;
483 dest->offset_rgba[3] = src->offset_rgba[3];
489 static void scene_add_vertexes( pvraddr_t poly_idx, int vertex_length,
490 shadow_mode_t is_modified )
492 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
493 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
494 uint32_t *context = ptr;
497 if( poly->vertex_index == -1 ) {
498 ptr += (is_modified == SHADOW_FULL ? 5 : 3 );
499 poly->vertex_index = pvr2_scene.vertex_index;
501 assert( poly != NULL );
502 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
503 for( i=0; i<poly->vertex_count; i++ ) {
504 scene_decode_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[1], context[2], ptr, 0 );
505 ptr += vertex_length;
508 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
509 poly->mod_vertex_index = pvr2_scene.vertex_index;
510 if( is_modified == SHADOW_FULL ) {
511 int mod_offset = (vertex_length - 3)>>1;
512 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
513 for( i=0; i<poly->vertex_count; i++ ) {
514 scene_decode_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[3], context[4], ptr, mod_offset );
515 ptr += vertex_length;
518 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index],
519 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
520 pvr2_scene.vertex_index += poly->vertex_count;
526 static void scene_add_quad_vertexes( pvraddr_t poly_idx, int vertex_length,
527 shadow_mode_t is_modified )
529 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
530 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
531 uint32_t *context = ptr;
534 if( poly->vertex_index == -1 ) {
535 // Construct it locally and copy to the vertex buffer, as the VBO is
536 // allowed to be horribly slow for reads (ie it could be direct-mapped
538 struct vertex_struct quad[4];
540 assert( poly != NULL );
541 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
542 ptr += (is_modified == SHADOW_FULL ? 5 : 3 );
543 poly->vertex_index = pvr2_scene.vertex_index;
544 for( i=0; i<4; i++ ) {
545 scene_decode_vertex( &quad[i], context[0], context[1], context[2], ptr, 0 );
546 ptr += vertex_length;
548 scene_compute_vertexes( &quad[3], 1, &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
549 // Swap last two vertexes (quad arrangement => tri strip arrangement)
550 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
551 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
552 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
553 if( !POLY1_GOURAUD_SHADED(context[0]) ) {
554 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
555 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+1].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
558 pvr2_scene.vertex_index += 4;
561 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
562 poly->mod_vertex_index = pvr2_scene.vertex_index;
563 if( is_modified == SHADOW_FULL ) {
564 int mod_offset = (vertex_length - 3)>>1;
565 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
566 for( i=0; i<4; i++ ) {
567 scene_decode_vertex( &quad[4], context[0], context[3], context[4], ptr, mod_offset );
568 ptr += vertex_length;
570 scene_compute_vertexes( &quad[3], 1, &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
571 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
572 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
573 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
574 if( !POLY1_GOURAUD_SHADED(context[0]) ) {
575 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
576 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+1].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
579 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index],
580 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
581 pvr2_scene.vertex_index += poly->vertex_count;
583 pvr2_scene.vertex_index += 4;
588 static void scene_extract_polygons( pvraddr_t tile_entry )
590 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
592 uint32_t entry = *tile_list++;
593 if( entry >> 28 == 0x0F ) {
595 } else if( entry >> 28 == 0x0E ) {
596 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
598 pvraddr_t polyaddr = entry&0x000FFFFF;
599 shadow_mode_t is_modified = (entry & 0x01000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
600 int vertex_length = (entry >> 21) & 0x07;
601 int context_length = 3;
602 if( is_modified == SHADOW_FULL ) {
604 vertex_length <<= 1 ;
608 if( (entry & 0xE0000000) == 0x80000000 ) {
610 int strip_count = ((entry >> 25) & 0x0F)+1;
611 int polygon_length = 3 * vertex_length + context_length;
613 struct polygon_struct *last_poly = NULL;
614 for( i=0; i<strip_count; i++ ) {
615 struct polygon_struct *poly = scene_add_polygon( polyaddr, 3, is_modified );
616 polyaddr += polygon_length;
617 if( last_poly != NULL && last_poly->next == NULL ) {
618 last_poly->next = poly;
622 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
624 int strip_count = ((entry >> 25) & 0x0F)+1;
625 int polygon_length = 4 * vertex_length + context_length;
627 struct polygon_struct *last_poly = NULL;
628 for( i=0; i<strip_count; i++ ) {
629 struct polygon_struct *poly = scene_add_polygon( polyaddr, 4, is_modified );
630 polyaddr += polygon_length;
631 if( last_poly != NULL && last_poly->next == NULL ) {
632 last_poly->next = poly;
639 for( i=5; i>=0; i-- ) {
640 if( entry & (0x40000000>>i) ) {
646 scene_add_polygon( polyaddr, last+3, is_modified );
653 static void scene_extract_vertexes( pvraddr_t tile_entry )
655 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
657 uint32_t entry = *tile_list++;
658 if( entry >> 28 == 0x0F ) {
660 } else if( entry >> 28 == 0x0E ) {
661 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
663 pvraddr_t polyaddr = entry&0x000FFFFF;
664 shadow_mode_t is_modified = (entry & 0x01000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
665 int vertex_length = (entry >> 21) & 0x07;
666 int context_length = 3;
667 if( is_modified == SHADOW_FULL ) {
673 if( (entry & 0xE0000000) == 0x80000000 ) {
675 int strip_count = ((entry >> 25) & 0x0F)+1;
676 int polygon_length = 3 * vertex_length + context_length;
678 for( i=0; i<strip_count; i++ ) {
679 scene_add_vertexes( polyaddr, vertex_length, is_modified );
680 polyaddr += polygon_length;
682 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
684 int strip_count = ((entry >> 25) & 0x0F)+1;
685 int polygon_length = 4 * vertex_length + context_length;
687 for( i=0; i<strip_count; i++ ) {
688 scene_add_quad_vertexes( polyaddr, vertex_length, is_modified );
689 polyaddr += polygon_length;
694 for( i=5; i>=0; i-- ) {
695 if( entry & (0x40000000>>i) ) {
701 scene_add_vertexes( polyaddr, vertex_length, is_modified );
708 static void scene_extract_background( void )
710 uint32_t bgplane = MMIO_READ(PVR2, RENDER_BGPLANE);
711 int vertex_length = (bgplane >> 24) & 0x07;
712 int context_length = 3, i;
713 shadow_mode_t is_modified = (bgplane & 0x08000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
715 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
716 uint32_t *context = &pvr2_scene.pvr2_pbuf[(bgplane & 0x00FFFFFF)>>3];
717 poly->context = context;
718 poly->vertex_count = 4;
719 poly->vertex_index = pvr2_scene.vertex_count;
720 if( is_modified == SHADOW_FULL ) {
724 if( is_modified != SHADOW_NONE ) {
725 poly->mod_vertex_index = pvr2_scene.vertex_count + 4;
726 pvr2_scene.vertex_count += 8;
728 poly->mod_vertex_index = -1;
729 pvr2_scene.vertex_count += 4;
732 context_length += (bgplane & 0x07) * vertex_length;
735 poly->sub_next = NULL;
736 pvr2_scene.bkgnd_poly = poly;
738 struct vertex_struct base_vertexes[3];
739 uint32_t *ptr = context + context_length;
740 for( i=0; i<3; i++ ) {
741 scene_decode_vertex( &base_vertexes[i], context[0], context[1], context[2],
743 ptr += vertex_length;
745 struct vertex_struct *result_vertexes = &pvr2_scene.vertex_array[poly->vertex_index];
746 result_vertexes[0].x = result_vertexes[0].y = 0;
747 result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
748 result_vertexes[1].y = result_vertexes[2].x = 0;
749 result_vertexes[2].y = result_vertexes[3].y = pvr2_scene.buffer_height;
750 scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
752 if( is_modified == SHADOW_FULL ) {
753 int mod_offset = (vertex_length - 3)>>1;
754 ptr = context + context_length;
755 for( i=0; i<3; i++ ) {
756 scene_decode_vertex( &base_vertexes[i], context[0], context[3], context[4],
758 ptr += vertex_length;
760 result_vertexes = &pvr2_scene.vertex_array[poly->mod_vertex_index];
761 result_vertexes[0].x = result_vertexes[0].y = 0;
762 result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
763 result_vertexes[1].y = result_vertexes[2].x = 0;
764 result_vertexes[2].y = result_vertexes[3].y = pvr2_scene.buffer_height;
765 scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
766 } else if( is_modified == SHADOW_CHEAP ) {
767 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index],
768 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
769 pvr2_scene.vertex_index += poly->vertex_count;
775 uint32_t pvr2_scene_buffer_width()
777 return pvr2_scene.buffer_width;
780 uint32_t pvr2_scene_buffer_height()
782 return pvr2_scene.buffer_height;
786 * Extract the current scene into the rendering structures. We run two passes
787 * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts),
788 * second pass extracts the vertex data into the VBO/vertex array.
790 * Difficult to do in single pass as we don't generally know the size of a
791 * polygon for certain until we've seen all tiles containing it. It also means we
792 * can count the vertexes and allocate the appropriate size VBO.
794 * FIXME: accesses into VRAM need to be bounds-checked properly
796 void pvr2_scene_read( void )
801 pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
802 pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
803 pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
804 pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
805 pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
807 uint32_t scaler = MMIO_READ( PVR2, RENDER_SCALER );
808 if( scaler & SCALER_HSCALE ) {
809 /* If the horizontal scaler is in use, we're (in principle) supposed to
810 * divide everything by 2. However in the interests of display quality,
811 * instead we want to render to the unscaled resolution and downsample
812 * only if/when required.
814 pvr2_scene.bounds[1] *= 2;
817 uint32_t fog_col = MMIO_READ( PVR2, RENDER_FOGTBLCOL );
818 unpack_bgra( fog_col, pvr2_scene.fog_lut_colour );
819 fog_col = MMIO_READ( PVR2, RENDER_FOGVRTCOL );
820 unpack_bgra( fog_col, pvr2_scene.fog_vert_colour );
822 uint32_t *tilebuffer = (uint32_t *)(pvr2_main_ram + MMIO_READ( PVR2, RENDER_TILEBASE ));
823 uint32_t *segment = tilebuffer;
824 uint32_t shadow = MMIO_READ(PVR2,RENDER_SHADOW);
825 pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
826 pvr2_scene.pvr2_pbuf = (uint32_t *)(pvr2_main_ram + MMIO_READ(PVR2,RENDER_POLYBASE));
827 pvr2_scene.shadow_mode = shadow & 0x100 ? SHADOW_CHEAP : SHADOW_FULL;
828 scene_shadow_intensity = U8TOFLOAT(shadow&0xFF);
832 int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
833 int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
835 if( (obj_config & 0x00200000) == 0 ) {
836 if( isp_config & 1 ) {
837 pvr2_scene.sort_mode = SORT_NEVER;
839 pvr2_scene.sort_mode = SORT_ALWAYS;
842 pvr2_scene.sort_mode = SORT_TILEFLAG;
845 // Pass 1: Extract polygon list
849 control = *segment++;
850 int tile_x = SEGMENT_X(control);
851 int tile_y = SEGMENT_Y(control);
852 if( tile_x > max_tile_x ) {
855 if( tile_y > max_tile_y ) {
858 for( i=0; i<5; i++ ) {
859 if( (*segment & NO_POINTER) == 0 ) {
860 scene_extract_polygons( *segment );
864 } while( (control & SEGMENT_END) == 0 );
866 pvr2_scene.buffer_width = (max_tile_x+1)<<5;
867 pvr2_scene.buffer_height = (max_tile_y+1)<<5;
869 // Pass 2: Extract vertex data
871 pvr2_scene.vertex_index = 0;
872 segment = tilebuffer;
874 control = *segment++;
875 for( i=0; i<5; i++ ) {
876 if( (*segment & NO_POINTER) == 0 ) {
877 scene_extract_vertexes( *segment );
881 } while( (control & SEGMENT_END) == 0 );
883 scene_extract_background();
884 scene_compute_lut_fog();
885 scene_backface_cull();
887 vertex_buffer_unmap();
890 void pvr2_scene_finished( )
892 vbuf->finished(vbuf);
896 * Dump the current scene to file in a (mostly) human readable form
898 void pvr2_scene_print( FILE *f )
902 fprintf( f, "Polygons: %d\n", pvr2_scene.poly_count );
903 for( i=0; i<pvr2_scene.poly_count; i++ ) {
904 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
905 fprintf( f, " %08X ", (uint32_t)(((unsigned char *)poly->context) - pvr2_main_ram) );
906 switch( poly->vertex_count ) {
907 case 3: fprintf( f, "Tri " ); break;
908 case 4: fprintf( f, "Quad " ); break;
909 default: fprintf( f,"%d-Strip ", poly->vertex_count-2 ); break;
911 fprintf( f, "%08X %08X %08X ", poly->context[0], poly->context[1], poly->context[2] );
912 if( poly->mod_vertex_index != -1 ) {
913 fprintf( f, "%08X %08X\n", poly->context[3], poly->context[5] );
918 for( j=0; j<poly->vertex_count; j++ ) {
919 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->vertex_index+j];
920 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,
921 v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
922 v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
924 if( poly->mod_vertex_index != -1 ) {
925 fprintf( f, " ---\n" );
926 for( j=0; j<poly->vertex_count; j++ ) {
927 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->mod_vertex_index+j];
928 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,
929 v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
930 v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
937 void pvr2_scene_dump()
939 pvr2_scene_print(stdout);
.