filename | src/pvr2/scene.c |
changeset | 1257:e1314ad3e7cc |
prev | 1240:190df8a791ca |
next | 1298:d0eb2307b847 |
author | nkeynes |
date | Wed Mar 21 14:45:27 2012 +1000 (12 years ago) |
permissions | -rw-r--r-- |
last change | Make sure we destroy the render buffers as part of the GL shutdown (fixes bug with render buffers not being recreated when we re-init the GL). |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
3 *
4 * Manage the internal vertex/polygon buffers and scene data structure.
5 *
6 * Copyright (c) 2005 Nathan Keynes.
7 *
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.
12 *
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.
17 */
19 #include <assert.h>
20 #include <string.h>
21 #include <math.h>
22 #include "lxdream.h"
23 #include "display.h"
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)
33 {
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;
38 }
40 static inline uint32_t bgra_to_rgba(uint32_t bgra)
41 {
42 return (bgra&0xFF00FF00) | ((bgra&0x00FF0000)>>16) | ((bgra&0x000000FF)<<16);
43 }
45 /**
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.
49 */
50 static float halftofloat( uint16_t half )
51 {
52 union {
53 float f;
54 uint32_t i;
55 } temp;
56 temp.i = ((uint32_t)half)<<16;
57 return temp.f;
58 }
60 static float parse_fog_density( uint32_t value )
61 {
62 union {
63 uint32_t i;
64 float f;
65 } u;
66 u.i = (((value+127)&0xFF)<<23)|((value & 0xFF00)<<7);
67 return u.f;
68 }
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()
75 {
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);
79 }
81 static void vertex_buffer_unmap()
82 {
83 pvr2_scene.vertex_array = vbuf->unmap(vbuf);
84 }
86 /**
87 * Allocate vertex buffer + temporary structures. GL context must have been initialized before this
88 * point.
89 */
90 void pvr2_scene_init()
91 {
92 if( vbuf == NULL ) {
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 );
98 }
99 }
101 /**
102 * Clear the scene data structures in preparation for fresh data
103 */
104 void pvr2_scene_reset()
105 {
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;
109 }
110 pvr2_scene.poly_count = 0;
111 pvr2_scene.vertex_count = 0;
112 }
114 void pvr2_scene_shutdown()
115 {
116 vbuf->destroy(vbuf);
117 vbuf = NULL;
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;
122 }
124 static struct polygon_struct *scene_add_polygon( pvraddr_t poly_idx, int vertex_count,
125 shadow_mode_t is_modified )
126 {
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;
133 }
134 return pvr2_scene.buf_to_poly_map[poly_idx];
135 } else {
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;
141 poly->next = NULL;
142 poly->sub_next = NULL;
143 pvr2_scene.buf_to_poly_map[poly_idx] = poly;
144 pvr2_scene.vertex_count += (vertex_count * vert_mul);
145 return poly;
146 }
147 }
149 /**
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.
153 */
154 static struct polygon_struct *scene_split_subpolygon( struct polygon_struct *parent, int split_offset )
155 {
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;
163 } else {
164 poly->mod_vertex_index = parent->mod_vertex_index + split_offset;
165 }
166 poly->context = parent->context;
167 poly->next = NULL;
168 poly->sub_next = parent->sub_next;
170 parent->sub_next = poly;
171 parent->vertex_count = split_offset + 2;
173 return poly;
174 }
176 static float scene_get_palette_offset( uint32_t tex )
177 {
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;
183 } else {
184 return -1.0;
185 }
186 }
188 /**
189 * Decode a single PVR2 renderable vertex (opaque/trans/punch-out, but not shadow
190 * volume)
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.
197 */
198 static void scene_decode_vertex( struct vertex_struct *vert, uint32_t poly1,
199 uint32_t poly2, uint32_t tex, uint32_t *pvr2_data,
200 int modify_offset )
201 {
202 gboolean force_alpha = !POLY2_ALPHA_ENABLE(poly2);
203 union pvr2_data_type {
204 uint32_t *ival;
205 float *fval;
206 } data;
208 data.ival = pvr2_data;
210 vert->x = *data.fval++;
211 vert->y = *data.fval++;
213 float z = *data.fval++;
214 if( !isfinite(z) ) {
215 z = 0;
216 } else if( z != 0 ) {
217 z = 1/z;
218 }
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;
223 }
224 vert->z = 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 );
232 data.ival++;
233 } else {
234 vert->u = *data.fval++;
235 vert->v = *data.fval++;
236 }
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 */
243 break;
244 case 2: /* Decal */
245 vert->tex_mode = 1.0;
246 unpack_bgra(*data.ival++, vert->rgba);
247 break;
248 case 1:
249 force_alpha = TRUE;
250 /* fall-through */
251 default:
252 vert->tex_mode = 0.0;
253 unpack_bgra(*data.ival++, vert->rgba);
254 break;
255 }
256 vert->r = scene_get_palette_offset(tex);
257 } else {
258 vert->tex_mode = 2.0;
259 vert->r = -1.0;
260 unpack_bgra(*data.ival++, vert->rgba);
261 }
263 if( POLY1_SPECULAR(poly1) ) {
264 unpack_bgra(*data.ival++, vert->offset_rgba);
265 } else {
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;
270 }
272 if( force_alpha ) {
273 vert->rgba[3] = 1.0;
274 }
275 }
277 /**
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.
280 */
281 static void scene_compute_vertexes( struct vertex_struct *result,
282 int result_count,
283 struct vertex_struct *input,
284 gboolean is_solid_shaded )
285 {
286 int i,j;
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));
293 if( detxy == 0 ) {
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
296 // this case.
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) );
301 result[i].x = x;
302 result[i].y = y;
303 }
304 return;
305 }
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;
324 }
325 result[i].z = 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) );
334 } else {
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);
343 *rgba3++ = rc;
344 }
345 }
346 }
347 }
349 static float scene_compute_lut_fog_vertex( float z, float fog_density, float fog_table[][2] )
350 {
351 union {
352 uint32_t i;
353 float f;
354 } v;
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];
361 }
363 /**
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.
367 */
368 static void scene_compute_lut_fog( )
369 {
370 int i,j;
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;
380 }
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 float fog = scene_compute_lut_fog_vertex( pvr2_scene.vertex_array[index+j].z, fog_density, fog_table );
389 if( display_driver->capabilities.has_sl )
390 pvr2_scene.vertex_array[index+j].offset_rgba[3] = -fog;
391 else
392 pvr2_scene.vertex_array[index+j].offset_rgba[3] = fog;
393 }
394 } else if( mode == PVR2_POLY_FOG_LOOKUP2 ) {
395 for( j=0; j<pvr2_scene.poly_array[i].vertex_count; j++ ) {
396 pvr2_scene.vertex_array[index+j].rgba[0] = pvr2_scene.fog_lut_colour[0];
397 pvr2_scene.vertex_array[index+j].rgba[1] = pvr2_scene.fog_lut_colour[1];
398 pvr2_scene.vertex_array[index+j].rgba[2] = pvr2_scene.fog_lut_colour[2];
399 pvr2_scene.vertex_array[index+j].rgba[3] =
400 scene_compute_lut_fog_vertex( pvr2_scene.vertex_array[index+j].z, fog_density, fog_table );
401 pvr2_scene.vertex_array[index+j].offset_rgba[3] = 0;
402 }
403 } else if( mode == PVR2_POLY_FOG_DISABLED ) {
404 for( j=0; j<pvr2_scene.poly_array[i].vertex_count; j++ ) {
405 pvr2_scene.vertex_array[index+j].offset_rgba[3] = 0;
406 }
407 }
408 }
409 }
411 /**
412 * Manually cull back-facing polygons where we can - this actually saves
413 * us a lot of time vs passing everything to GL to do it.
414 */
415 static void scene_backface_cull()
416 {
417 unsigned poly_idx;
418 unsigned poly_count = pvr2_scene.poly_count; /* Note: we don't want to process any sub-polygons created here */
419 for( poly_idx = 0; poly_idx<poly_count; poly_idx++ ) {
420 uint32_t poly1 = pvr2_scene.poly_array[poly_idx].context[0];
421 if( POLY1_CULL_ENABLE(poly1) ) {
422 struct polygon_struct *poly = &pvr2_scene.poly_array[poly_idx];
423 unsigned vert_idx = poly->vertex_index;
424 unsigned tri_count = poly->vertex_count-2;
425 struct vertex_struct *vert = &pvr2_scene.vertex_array[vert_idx];
426 unsigned i;
427 gboolean ccw = (POLY1_CULL_MODE(poly1) == CULL_CCW);
428 int first_visible = -1, last_visible = -1;
429 for( i=0; i<tri_count; i++ ) {
430 float ux = vert[i+1].x - vert[i].x;
431 float uy = vert[i+1].y - vert[i].y;
432 float vx = vert[i+2].x - vert[i].x;
433 float vy = vert[i+2].y - vert[i].y;
434 float nz = (ux*vy) - (uy*vx);
435 if( ccw ? nz > 0 : nz < 0 ) {
436 /* Surface is visible */
437 if( first_visible == -1 ) {
438 first_visible = i;
439 /* Elide the initial hidden triangles (note we don't
440 * need to care about winding anymore here) */
441 poly->vertex_index += i;
442 poly->vertex_count -= i;
443 if( poly->mod_vertex_index != -1 )
444 poly->mod_vertex_index += i;
445 } else if( last_visible != i-1 ) {
446 /* And... here we have to split the polygon. Allocate a new
447 * sub-polygon to hold the vertex references */
448 struct polygon_struct *sub = scene_split_subpolygon(poly, (i-first_visible));
449 poly->vertex_count -= (i-first_visible-1) - last_visible;
450 first_visible = i;
451 poly = sub;
452 }
453 last_visible = i;
454 } /* Else culled */
455 /* Invert ccw flag for triangle strip processing */
456 ccw = !ccw;
457 }
458 if( last_visible == -1 ) {
459 /* No visible surfaces, so we can mark the whole polygon as being vertex-less */
460 poly->vertex_count = 0;
461 } else if( last_visible != tri_count-1 ) {
462 /* Remove final hidden tris */
463 poly->vertex_count -= (tri_count - 1 - last_visible);
464 }
465 }
466 }
467 }
469 static void scene_add_cheap_shadow_vertexes( struct vertex_struct *src, struct vertex_struct *dest, int count )
470 {
471 unsigned int i, j;
473 for( i=0; i<count; i++ ) {
474 dest->x = src->x;
475 dest->y = src->y;
476 dest->z = src->z;
477 dest->u = src->u;
478 dest->v = src->v;
479 dest->r = src->r;
480 dest->tex_mode = src->tex_mode;
481 dest->rgba[0] = src->rgba[0] * scene_shadow_intensity;
482 dest->rgba[1] = src->rgba[1] * scene_shadow_intensity;
483 dest->rgba[2] = src->rgba[2] * scene_shadow_intensity;
484 dest->rgba[3] = src->rgba[3] * scene_shadow_intensity;
485 dest->offset_rgba[0] = src->offset_rgba[0] * scene_shadow_intensity;
486 dest->offset_rgba[1] = src->offset_rgba[1] * scene_shadow_intensity;
487 dest->offset_rgba[2] = src->offset_rgba[2] * scene_shadow_intensity;
488 dest->offset_rgba[3] = src->offset_rgba[3];
489 dest++;
490 src++;
491 }
492 }
494 static void scene_add_vertexes( pvraddr_t poly_idx, int vertex_length,
495 shadow_mode_t is_modified )
496 {
497 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
498 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
499 uint32_t *context = ptr;
500 unsigned int i;
502 if( poly->vertex_index == -1 ) {
503 ptr += (is_modified == SHADOW_FULL ? 5 : 3 );
504 poly->vertex_index = pvr2_scene.vertex_index;
506 assert( poly != NULL );
507 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
508 for( i=0; i<poly->vertex_count; i++ ) {
509 scene_decode_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[1], context[2], ptr, 0 );
510 ptr += vertex_length;
511 }
512 if( is_modified ) {
513 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
514 poly->mod_vertex_index = pvr2_scene.vertex_index;
515 if( is_modified == SHADOW_FULL ) {
516 int mod_offset = (vertex_length - 3)>>1;
517 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
518 for( i=0; i<poly->vertex_count; i++ ) {
519 scene_decode_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[3], context[4], ptr, mod_offset );
520 ptr += vertex_length;
521 }
522 } else {
523 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index],
524 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
525 pvr2_scene.vertex_index += poly->vertex_count;
526 }
527 }
528 }
529 }
531 static void scene_add_quad_vertexes( pvraddr_t poly_idx, int vertex_length,
532 shadow_mode_t is_modified )
533 {
534 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
535 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
536 uint32_t *context = ptr;
537 unsigned int i;
539 if( poly->vertex_index == -1 ) {
540 // Construct it locally and copy to the vertex buffer, as the VBO is
541 // allowed to be horribly slow for reads (ie it could be direct-mapped
542 // vram).
543 struct vertex_struct quad[4];
545 assert( poly != NULL );
546 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
547 ptr += (is_modified == SHADOW_FULL ? 5 : 3 );
548 poly->vertex_index = pvr2_scene.vertex_index;
549 for( i=0; i<4; i++ ) {
550 scene_decode_vertex( &quad[i], context[0], context[1], context[2], ptr, 0 );
551 ptr += vertex_length;
552 }
553 scene_compute_vertexes( &quad[3], 1, &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
554 // Swap last two vertexes (quad arrangement => tri strip arrangement)
555 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
556 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
557 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
558 if( !POLY1_GOURAUD_SHADED(context[0]) ) {
559 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
560 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+1].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
561 }
563 pvr2_scene.vertex_index += 4;
565 if( is_modified ) {
566 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
567 poly->mod_vertex_index = pvr2_scene.vertex_index;
568 if( is_modified == SHADOW_FULL ) {
569 int mod_offset = (vertex_length - 3)>>1;
570 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
571 for( i=0; i<4; i++ ) {
572 scene_decode_vertex( &quad[4], context[0], context[3], context[4], ptr, mod_offset );
573 ptr += vertex_length;
574 }
575 scene_compute_vertexes( &quad[3], 1, &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
576 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
577 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
578 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
579 if( !POLY1_GOURAUD_SHADED(context[0]) ) {
580 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
581 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+1].rgba, &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3].rgba, sizeof(float)*8 );
582 }
583 } else {
584 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index],
585 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
586 pvr2_scene.vertex_index += poly->vertex_count;
587 }
588 pvr2_scene.vertex_index += 4;
589 }
590 }
591 }
593 static void scene_extract_polygons( pvraddr_t tile_entry )
594 {
595 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
596 do {
597 uint32_t entry = *tile_list++;
598 if( entry >> 28 == 0x0F ) {
599 break;
600 } else if( entry >> 28 == 0x0E ) {
601 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
602 } else {
603 pvraddr_t polyaddr = entry&0x000FFFFF;
604 shadow_mode_t is_modified = (entry & 0x01000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
605 int vertex_length = (entry >> 21) & 0x07;
606 int context_length = 3;
607 if( is_modified == SHADOW_FULL ) {
608 context_length = 5;
609 vertex_length <<= 1 ;
610 }
611 vertex_length += 3;
613 if( (entry & 0xE0000000) == 0x80000000 ) {
614 /* Triangle(s) */
615 int strip_count = ((entry >> 25) & 0x0F)+1;
616 int polygon_length = 3 * vertex_length + context_length;
617 int i;
618 struct polygon_struct *last_poly = NULL;
619 for( i=0; i<strip_count; i++ ) {
620 struct polygon_struct *poly = scene_add_polygon( polyaddr, 3, is_modified );
621 polyaddr += polygon_length;
622 if( last_poly != NULL && last_poly->next == NULL ) {
623 last_poly->next = poly;
624 }
625 last_poly = poly;
626 }
627 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
628 /* Sprite(s) */
629 int strip_count = ((entry >> 25) & 0x0F)+1;
630 int polygon_length = 4 * vertex_length + context_length;
631 int i;
632 struct polygon_struct *last_poly = NULL;
633 for( i=0; i<strip_count; i++ ) {
634 struct polygon_struct *poly = scene_add_polygon( polyaddr, 4, is_modified );
635 polyaddr += polygon_length;
636 if( last_poly != NULL && last_poly->next == NULL ) {
637 last_poly->next = poly;
638 }
639 last_poly = poly;
640 }
641 } else {
642 /* Polygon */
643 int i, last = -1;
644 for( i=5; i>=0; i-- ) {
645 if( entry & (0x40000000>>i) ) {
646 last = i;
647 break;
648 }
649 }
650 if( last != -1 ) {
651 scene_add_polygon( polyaddr, last+3, is_modified );
652 }
653 }
654 }
655 } while( 1 );
656 }
658 static void scene_extract_vertexes( pvraddr_t tile_entry )
659 {
660 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
661 do {
662 uint32_t entry = *tile_list++;
663 if( entry >> 28 == 0x0F ) {
664 break;
665 } else if( entry >> 28 == 0x0E ) {
666 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
667 } else {
668 pvraddr_t polyaddr = entry&0x000FFFFF;
669 shadow_mode_t is_modified = (entry & 0x01000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
670 int vertex_length = (entry >> 21) & 0x07;
671 int context_length = 3;
672 if( is_modified == SHADOW_FULL ) {
673 context_length = 5;
674 vertex_length <<=1 ;
675 }
676 vertex_length += 3;
678 if( (entry & 0xE0000000) == 0x80000000 ) {
679 /* Triangle(s) */
680 int strip_count = ((entry >> 25) & 0x0F)+1;
681 int polygon_length = 3 * vertex_length + context_length;
682 int i;
683 for( i=0; i<strip_count; i++ ) {
684 scene_add_vertexes( polyaddr, vertex_length, is_modified );
685 polyaddr += polygon_length;
686 }
687 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
688 /* Sprite(s) */
689 int strip_count = ((entry >> 25) & 0x0F)+1;
690 int polygon_length = 4 * vertex_length + context_length;
691 int i;
692 for( i=0; i<strip_count; i++ ) {
693 scene_add_quad_vertexes( polyaddr, vertex_length, is_modified );
694 polyaddr += polygon_length;
695 }
696 } else {
697 /* Polygon */
698 int i, last = -1;
699 for( i=5; i>=0; i-- ) {
700 if( entry & (0x40000000>>i) ) {
701 last = i;
702 break;
703 }
704 }
705 if( last != -1 ) {
706 scene_add_vertexes( polyaddr, vertex_length, is_modified );
707 }
708 }
709 }
710 } while( 1 );
711 }
713 static void scene_extract_background( void )
714 {
715 uint32_t bgplane = MMIO_READ(PVR2, RENDER_BGPLANE);
716 int vertex_length = (bgplane >> 24) & 0x07;
717 int context_length = 3, i;
718 shadow_mode_t is_modified = (bgplane & 0x08000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
720 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
721 uint32_t *context = &pvr2_scene.pvr2_pbuf[(bgplane & 0x00FFFFFF)>>3];
722 poly->context = context;
723 poly->vertex_count = 4;
724 poly->vertex_index = pvr2_scene.vertex_count;
725 if( is_modified == SHADOW_FULL ) {
726 context_length = 5;
727 vertex_length <<= 1;
728 }
729 if( is_modified != SHADOW_NONE ) {
730 poly->mod_vertex_index = pvr2_scene.vertex_count + 4;
731 pvr2_scene.vertex_count += 8;
732 } else {
733 poly->mod_vertex_index = -1;
734 pvr2_scene.vertex_count += 4;
735 }
736 vertex_length += 3;
737 context_length += (bgplane & 0x07) * vertex_length;
739 poly->next = NULL;
740 poly->sub_next = NULL;
741 pvr2_scene.bkgnd_poly = poly;
743 struct vertex_struct base_vertexes[3];
744 uint32_t *ptr = context + context_length;
745 for( i=0; i<3; i++ ) {
746 scene_decode_vertex( &base_vertexes[i], context[0], context[1], context[2],
747 ptr, 0 );
748 ptr += vertex_length;
749 }
750 struct vertex_struct *result_vertexes = &pvr2_scene.vertex_array[poly->vertex_index];
751 result_vertexes[0].x = result_vertexes[0].y = 0;
752 result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
753 result_vertexes[1].y = result_vertexes[2].x = 0;
754 result_vertexes[2].y = result_vertexes[3].y = pvr2_scene.buffer_height;
755 scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
757 if( is_modified == SHADOW_FULL ) {
758 int mod_offset = (vertex_length - 3)>>1;
759 ptr = context + context_length;
760 for( i=0; i<3; i++ ) {
761 scene_decode_vertex( &base_vertexes[i], context[0], context[3], context[4],
762 ptr, mod_offset );
763 ptr += vertex_length;
764 }
765 result_vertexes = &pvr2_scene.vertex_array[poly->mod_vertex_index];
766 result_vertexes[0].x = result_vertexes[0].y = 0;
767 result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
768 result_vertexes[1].y = result_vertexes[2].x = 0;
769 result_vertexes[2].y = result_vertexes[3].y = pvr2_scene.buffer_height;
770 scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
771 } else if( is_modified == SHADOW_CHEAP ) {
772 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index],
773 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
774 pvr2_scene.vertex_index += poly->vertex_count;
775 }
777 }
780 uint32_t pvr2_scene_buffer_width()
781 {
782 return pvr2_scene.buffer_width;
783 }
785 uint32_t pvr2_scene_buffer_height()
786 {
787 return pvr2_scene.buffer_height;
788 }
790 /**
791 * Extract the current scene into the rendering structures. We run two passes
792 * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts),
793 * second pass extracts the vertex data into the VBO/vertex array.
794 *
795 * Difficult to do in single pass as we don't generally know the size of a
796 * polygon for certain until we've seen all tiles containing it. It also means we
797 * can count the vertexes and allocate the appropriate size VBO.
798 *
799 * FIXME: accesses into VRAM need to be bounds-checked properly
800 */
801 void pvr2_scene_read( void )
802 {
803 pvr2_scene_init();
804 pvr2_scene_reset();
806 pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
807 pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
808 pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
809 pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
810 pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
812 uint32_t scaler = MMIO_READ( PVR2, RENDER_SCALER );
813 if( scaler & SCALER_HSCALE ) {
814 /* If the horizontal scaler is in use, we're (in principle) supposed to
815 * divide everything by 2. However in the interests of display quality,
816 * instead we want to render to the unscaled resolution and downsample
817 * only if/when required.
818 */
819 pvr2_scene.bounds[1] *= 2;
820 }
822 uint32_t fog_col = MMIO_READ( PVR2, RENDER_FOGTBLCOL );
823 unpack_bgra( fog_col, pvr2_scene.fog_lut_colour );
824 fog_col = MMIO_READ( PVR2, RENDER_FOGVRTCOL );
825 unpack_bgra( fog_col, pvr2_scene.fog_vert_colour );
827 uint32_t *tilebuffer = (uint32_t *)(pvr2_main_ram + MMIO_READ( PVR2, RENDER_TILEBASE ));
828 uint32_t *segment = tilebuffer;
829 uint32_t shadow = MMIO_READ(PVR2,RENDER_SHADOW);
830 pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
831 pvr2_scene.pvr2_pbuf = (uint32_t *)(pvr2_main_ram + MMIO_READ(PVR2,RENDER_POLYBASE));
832 pvr2_scene.shadow_mode = shadow & 0x100 ? SHADOW_CHEAP : SHADOW_FULL;
833 scene_shadow_intensity = U8TOFLOAT(shadow&0xFF);
835 int max_tile_x = 0;
836 int max_tile_y = 0;
837 int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
838 int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
840 if( (obj_config & 0x00200000) == 0 ) {
841 if( isp_config & 1 ) {
842 pvr2_scene.sort_mode = SORT_NEVER;
843 } else {
844 pvr2_scene.sort_mode = SORT_ALWAYS;
845 }
846 } else {
847 pvr2_scene.sort_mode = SORT_TILEFLAG;
848 }
850 // Pass 1: Extract polygon list
851 uint32_t control;
852 int i;
853 do {
854 control = *segment++;
855 int tile_x = SEGMENT_X(control);
856 int tile_y = SEGMENT_Y(control);
857 if( tile_x > max_tile_x ) {
858 max_tile_x = tile_x;
859 }
860 if( tile_y > max_tile_y ) {
861 max_tile_y = tile_y;
862 }
863 for( i=0; i<5; i++ ) {
864 if( (*segment & NO_POINTER) == 0 ) {
865 scene_extract_polygons( *segment );
866 }
867 segment++;
868 }
869 } while( (control & SEGMENT_END) == 0 );
871 pvr2_scene.buffer_width = (max_tile_x+1)<<5;
872 pvr2_scene.buffer_height = (max_tile_y+1)<<5;
874 // Pass 2: Extract vertex data
875 vertex_buffer_map();
876 pvr2_scene.vertex_index = 0;
877 segment = tilebuffer;
878 do {
879 control = *segment++;
880 for( i=0; i<5; i++ ) {
881 if( (*segment & NO_POINTER) == 0 ) {
882 scene_extract_vertexes( *segment );
883 }
884 segment++;
885 }
886 } while( (control & SEGMENT_END) == 0 );
888 scene_extract_background();
889 scene_compute_lut_fog();
890 scene_backface_cull();
892 vertex_buffer_unmap();
893 }
895 void pvr2_scene_finished( )
896 {
897 vbuf->finished(vbuf);
898 }
900 /**
901 * Dump the current scene to file in a (mostly) human readable form
902 */
903 void pvr2_scene_print( FILE *f )
904 {
905 int i,j;
907 fprintf( f, "Polygons: %d\n", pvr2_scene.poly_count );
908 for( i=0; i<pvr2_scene.poly_count; i++ ) {
909 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
910 fprintf( f, " %08X ", (uint32_t)(((unsigned char *)poly->context) - pvr2_main_ram) );
911 switch( poly->vertex_count ) {
912 case 3: fprintf( f, "Tri " ); break;
913 case 4: fprintf( f, "Quad " ); break;
914 default: fprintf( f,"%d-Strip ", poly->vertex_count-2 ); break;
915 }
916 fprintf( f, "%08X %08X %08X ", poly->context[0], poly->context[1], poly->context[2] );
917 if( poly->mod_vertex_index != -1 ) {
918 fprintf( f, "%08X %08X\n", poly->context[3], poly->context[5] );
919 } else {
920 fprintf( f, "\n" );
921 }
923 for( j=0; j<poly->vertex_count; j++ ) {
924 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->vertex_index+j];
925 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,
926 v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
927 v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
928 }
929 if( poly->mod_vertex_index != -1 ) {
930 fprintf( f, " ---\n" );
931 for( j=0; j<poly->vertex_count; j++ ) {
932 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->mod_vertex_index+j];
933 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,
934 v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
935 v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
936 }
937 }
938 }
940 }
942 void pvr2_scene_dump()
943 {
944 pvr2_scene_print(stdout);
945 }
.