filename | src/pvr2/scene.c |
changeset | 934:3acd3b3ee6d1 |
prev | 863:a5e5310061e2 |
next | 1066:ddffe9d2b332 |
author | nkeynes |
date | Wed Jun 03 10:29:16 2009 +0000 (14 years ago) |
permissions | -rw-r--r-- |
last change | Allow multiple input hooks to be registered for the same key - useful to allow eg binding the same hotkey for run/stop so that it works as a toggle. |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
3 *
4 * Manage the internal vertex/polygon buffers and scene data structure.
5 * Where possible this uses VBOs for the vertex + index data.
6 *
7 * Copyright (c) 2005 Nathan Keynes.
8 *
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.
13 *
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.
18 */
20 #include <assert.h>
21 #include <string.h>
22 #include <math.h>
23 #include "lxdream.h"
24 #include "display.h"
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)
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;
72 static gboolean vbo_init = FALSE;
73 static float scene_shadow_intensity = 0.0;
75 #ifdef ENABLE_VERTEX_BUFFER
76 static gboolean vbo_supported = FALSE;
77 #endif
79 /**
80 * Test for VBO support, and allocate all the system memory needed for the
81 * temporary structures. GL context must have been initialized before this
82 * point.
83 */
84 void pvr2_scene_init()
85 {
86 if( !vbo_init ) {
87 #ifdef ENABLE_VERTEX_BUFFER
88 if( isGLVertexBufferSupported() ) {
89 vbo_supported = TRUE;
90 pvr2_scene.vbo_id = 1;
91 }
92 #endif
93 pvr2_scene.vertex_array = NULL;
94 pvr2_scene.vertex_array_size = 0;
95 pvr2_scene.poly_array = g_malloc( MAX_POLY_BUFFER_SIZE );
96 pvr2_scene.buf_to_poly_map = g_malloc0( BUF_POLY_MAP_SIZE );
97 vbo_init = TRUE;
98 }
99 }
101 /**
102 * Clear the scene data structures in preparation for fresh data
103 */
104 void pvr2_scene_reset()
105 {
106 pvr2_scene.poly_count = 0;
107 pvr2_scene.vertex_count = 0;
108 memset( pvr2_scene.buf_to_poly_map, 0, BUF_POLY_MAP_SIZE );
109 }
111 void pvr2_scene_shutdown()
112 {
113 #ifdef ENABLE_VERTEX_BUFFER
114 if( vbo_supported ) {
115 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
116 glDeleteBuffersARB( 1, &pvr2_scene.vbo_id );
117 pvr2_scene.vbo_id = 0;
118 } else {
119 #endif
120 g_free( pvr2_scene.vertex_array );
121 pvr2_scene.vertex_array = NULL;
122 #ifdef ENABLE_VERTEX_BUFFER
123 }
124 #endif
126 g_free( pvr2_scene.poly_array );
127 pvr2_scene.poly_array = NULL;
128 g_free( pvr2_scene.buf_to_poly_map );
129 pvr2_scene.buf_to_poly_map = NULL;
130 vbo_init = FALSE;
131 }
133 void *vertex_buffer_map()
134 {
135 // Allow 8 vertexes for the background (4+4)
136 uint32_t size = (pvr2_scene.vertex_count + 8) * sizeof(struct vertex_struct);
137 #ifdef ENABLE_VERTEX_BUFFER
138 if( vbo_supported ) {
139 glGetError();
140 glBindBufferARB( GL_ARRAY_BUFFER_ARB, pvr2_scene.vbo_id );
141 if( size > pvr2_scene.vertex_array_size ) {
142 glBufferDataARB( GL_ARRAY_BUFFER_ARB, size, NULL, GL_DYNAMIC_DRAW_ARB );
143 int status = glGetError();
144 if( status != 0 ) {
145 fprintf( stderr, "Error %08X allocating vertex buffer\n", status );
146 abort();
147 }
148 pvr2_scene.vertex_array_size = size;
149 }
150 pvr2_scene.vertex_array = glMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );
151 assert(pvr2_scene.vertex_array != NULL );
152 } else {
153 #endif
154 if( size > pvr2_scene.vertex_array_size ) {
155 pvr2_scene.vertex_array = g_realloc( pvr2_scene.vertex_array, size );
156 }
157 #ifdef ENABLE_VERTEX_BUFFER
158 }
159 #endif
160 return pvr2_scene.vertex_array;
161 }
163 gboolean vertex_buffer_unmap()
164 {
165 #ifdef ENABLE_VERTEX_BUFFER
166 if( vbo_supported ) {
167 pvr2_scene.vertex_array = NULL;
168 return glUnmapBufferARB( GL_ARRAY_BUFFER_ARB );
169 } else {
170 return TRUE;
171 }
172 #else
173 return TRUE;
174 #endif
175 }
177 static struct polygon_struct *scene_add_polygon( pvraddr_t poly_idx, int vertex_count,
178 shadow_mode_t is_modified )
179 {
180 int vert_mul = is_modified != SHADOW_NONE ? 2 : 1;
182 if( pvr2_scene.buf_to_poly_map[poly_idx] != NULL ) {
183 if( vertex_count > pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count ) {
184 pvr2_scene.vertex_count += (vertex_count - pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count) * vert_mul;
185 pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count = vertex_count;
186 }
187 return pvr2_scene.buf_to_poly_map[poly_idx];
188 } else {
189 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
190 poly->context = &pvr2_scene.pvr2_pbuf[poly_idx];
191 poly->vertex_count = vertex_count;
192 poly->vertex_index = -1;
193 poly->mod_vertex_index = -1;
194 poly->next = NULL;
195 pvr2_scene.buf_to_poly_map[poly_idx] = poly;
196 pvr2_scene.vertex_count += (vertex_count * vert_mul);
197 return poly;
198 }
199 }
201 /**
202 * Decode a single PVR2 renderable vertex (opaque/trans/punch-out, but not shadow
203 * volume)
204 * @param vert Pointer to output vertex structure
205 * @param poly1 First word of polygon context (needed to understand vertex)
206 * @param poly2 Second word of polygon context
207 * @param pvr2_data Pointer to raw pvr2 vertex data (in VRAM)
208 * @param modify_offset Offset in 32-bit words to the tex/color data. 0 for
209 * the normal vertex, half the vertex length for the modified vertex.
210 */
211 static void pvr2_decode_render_vertex( struct vertex_struct *vert, uint32_t poly1,
212 uint32_t poly2, uint32_t *pvr2_data,
213 int modify_offset )
214 {
215 gboolean force_alpha = !POLY2_ALPHA_ENABLE(poly2);
216 union pvr2_data_type {
217 uint32_t *ival;
218 float *fval;
219 } data;
221 data.ival = pvr2_data;
223 vert->x = *data.fval++;
224 vert->y = *data.fval++;
226 float z = *data.fval++;
227 if( !isfinite(z) ) {
228 z = 0;
229 } else if( z != 0 ) {
230 z = 1/z;
231 }
232 if( z > pvr2_scene.bounds[5] ) {
233 pvr2_scene.bounds[5] = z;
234 } else if( z < pvr2_scene.bounds[4] && z != 0 ) {
235 pvr2_scene.bounds[4] = z;
236 }
237 vert->z = z;
238 data.ival += modify_offset;
241 if( POLY1_TEXTURED(poly1) ) {
242 if( POLY1_UV16(poly1) ) {
243 vert->u = halftofloat( *data.ival>>16 );
244 vert->v = halftofloat( *data.ival );
245 data.ival++;
246 } else {
247 vert->u = *data.fval++;
248 vert->v = *data.fval++;
249 }
250 if( POLY2_TEX_BLEND(poly2) == 1 ) {
251 force_alpha = TRUE;
252 }
253 }
254 unpack_bgra(*data.ival++, vert->rgba);
255 if( POLY1_SPECULAR(poly1) ) {
256 unpack_bgra(*data.ival++, vert->offset_rgba);
257 } else {
258 vert->offset_rgba[0] = 0.0;
259 vert->offset_rgba[1] = 0.0;
260 vert->offset_rgba[2] = 0.0;
261 vert->offset_rgba[3] = 0.0;
262 }
264 if( force_alpha ) {
265 vert->rgba[3] = 1.0;
266 }
267 }
269 /**
270 * Compute texture, colour, and z values for 1 or more result points by interpolating from
271 * a set of 3 input points. The result point(s) must define their x,y.
272 */
273 static void scene_compute_vertexes( struct vertex_struct *result,
274 int result_count,
275 struct vertex_struct *input,
276 gboolean is_solid_shaded )
277 {
278 int i,j;
279 float sx = input[2].x - input[1].x;
280 float sy = input[2].y - input[1].y;
281 float tx = input[0].x - input[1].x;
282 float ty = input[0].y - input[1].y;
284 float detxy = ((sy) * (tx)) - ((ty) * (sx));
285 if( detxy == 0 ) {
286 // If the input points fall on a line, they don't define a usable
287 // polygon - the PVR2 takes the last input point as the result in
288 // this case.
289 for( i=0; i<result_count; i++ ) {
290 float x = result[i].x;
291 float y = result[i].y;
292 memcpy( &result[i], &input[2], sizeof(struct vertex_struct) );
293 result[i].x = x;
294 result[i].y = y;
295 }
296 return;
297 }
298 float sz = input[2].z - input[1].z;
299 float tz = input[0].z - input[1].z;
300 float su = input[2].u - input[1].u;
301 float tu = input[0].u - input[1].u;
302 float sv = input[2].v - input[1].v;
303 float tv = input[0].v - input[1].v;
305 for( i=0; i<result_count; i++ ) {
306 float t = ((result[i].x - input[1].x) * sy -
307 (result[i].y - input[1].y) * sx) / detxy;
308 float s = ((result[i].y - input[1].y) * tx -
309 (result[i].x - input[1].x) * ty) / detxy;
311 float rz = input[1].z + (t*tz) + (s*sz);
312 if( rz > pvr2_scene.bounds[5] ) {
313 pvr2_scene.bounds[5] = rz;
314 } else if( rz < pvr2_scene.bounds[4] ) {
315 pvr2_scene.bounds[4] = rz;
316 }
317 result[i].z = rz;
318 result[i].u = input[1].u + (t*tu) + (s*su);
319 result[i].v = input[1].v + (t*tv) + (s*sv);
321 if( is_solid_shaded ) {
322 memcpy( result->rgba, input[2].rgba, sizeof(result->rgba) );
323 memcpy( result->offset_rgba, input[2].offset_rgba, sizeof(result->offset_rgba) );
324 } else {
325 float *rgba0 = input[0].rgba;
326 float *rgba1 = input[1].rgba;
327 float *rgba2 = input[2].rgba;
328 float *rgba3 = result[i].rgba;
329 for( j=0; j<8; j++ ) {
330 float tc = *rgba0++ - *rgba1;
331 float sc = *rgba2++ - *rgba1;
332 float rc = *rgba1++ + (t*tc) + (s*sc);
333 *rgba3++ = rc;
334 }
335 }
336 }
337 }
339 static float scene_compute_lut_fog_vertex( float z, float fog_density, float fog_table[][2] )
340 {
341 union {
342 uint32_t i;
343 float f;
344 } v;
345 v.f = z * fog_density;
346 if( v.f < 1.0 ) v.f = 1.0;
347 else if( v.f > 255.9999 ) v.f = 255.9999;
349 uint32_t index = ((v.i >> 18) & 0x0F)|((v.i>>19)&0x70);
350 return fog_table[index][0];
351 }
353 /**
354 * Compute the fog coefficients for all polygons using lookup-table fog. It's
355 * a little more convenient to do this as a separate pass, since we don't have
356 * to worry about computed vertexes.
357 */
358 static void scene_compute_lut_fog( )
359 {
360 int i,j;
362 float fog_density = parse_fog_density(MMIO_READ( PVR2, RENDER_FOGCOEFF ));
363 float fog_table[128][2];
365 /* Parse fog table out into floating-point format */
366 for( i=0; i<128; i++ ) {
367 uint32_t ent = MMIO_READ( PVR2, RENDER_FOGTABLE + (i<<2) );
368 fog_table[i][0] = ((float)(((ent&0x0000FF00)>>8) + 1)) / 256.0;
369 fog_table[i][1] = ((float)((ent&0x000000FF) + 1)) / 256.0;
370 }
373 for( i=0; i<pvr2_scene.poly_count; i++ ) {
374 int mode = POLY2_FOG_MODE(pvr2_scene.poly_array[i].context[1]);
375 if( mode == PVR2_POLY_FOG_LOOKUP ) {
376 uint32_t index = pvr2_scene.poly_array[i].vertex_index;
377 for( j=0; j<=pvr2_scene.poly_array[i].vertex_count; j++ ) {
378 pvr2_scene.vertex_array[index+j].offset_rgba[3] =
379 scene_compute_lut_fog_vertex( pvr2_scene.vertex_array[index+j].z, fog_density, fog_table );
380 }
381 } else if( mode == PVR2_POLY_FOG_LOOKUP2 ) {
382 uint32_t index = pvr2_scene.poly_array[i].vertex_index;
383 for( j=0; j<=pvr2_scene.poly_array[i].vertex_count; j++ ) {
384 pvr2_scene.vertex_array[index+j].rgba[0] = pvr2_scene.fog_lut_colour[0];
385 pvr2_scene.vertex_array[index+j].rgba[1] = pvr2_scene.fog_lut_colour[1];
386 pvr2_scene.vertex_array[index+j].rgba[2] = pvr2_scene.fog_lut_colour[2];
387 pvr2_scene.vertex_array[index+j].rgba[3] =
388 scene_compute_lut_fog_vertex( pvr2_scene.vertex_array[index+j].z, fog_density, fog_table );
389 }
390 }
391 }
392 }
394 static void scene_add_cheap_shadow_vertexes( struct vertex_struct *src, struct vertex_struct *dest, int count )
395 {
396 unsigned int i, j;
398 for( i=0; i<count; i++ ) {
399 dest->x = src->x;
400 dest->y = src->y;
401 dest->z = src->z;
402 dest->u = src->u;
403 dest->v = src->v;
404 dest->rgba[0] = src->rgba[0] * scene_shadow_intensity;
405 dest->rgba[1] = src->rgba[1] * scene_shadow_intensity;
406 dest->rgba[2] = src->rgba[2] * scene_shadow_intensity;
407 dest->rgba[3] = src->rgba[3] * scene_shadow_intensity;
408 dest->offset_rgba[0] = src->offset_rgba[0] * scene_shadow_intensity;
409 dest->offset_rgba[1] = src->offset_rgba[1] * scene_shadow_intensity;
410 dest->offset_rgba[2] = src->offset_rgba[2] * scene_shadow_intensity;
411 dest->offset_rgba[3] = src->offset_rgba[3];
412 dest++;
413 src++;
414 }
415 }
417 static void scene_add_vertexes( pvraddr_t poly_idx, int vertex_length,
418 shadow_mode_t is_modified )
419 {
420 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
421 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
422 uint32_t *context = ptr;
423 unsigned int i;
425 if( poly->vertex_index == -1 ) {
426 ptr += (is_modified == SHADOW_FULL ? 5 : 3 );
427 poly->vertex_index = pvr2_scene.vertex_index;
429 assert( poly != NULL );
430 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
431 for( i=0; i<poly->vertex_count; i++ ) {
432 pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[1], ptr, 0 );
433 ptr += vertex_length;
434 }
435 if( is_modified ) {
436 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
437 poly->mod_vertex_index = pvr2_scene.vertex_index;
438 if( is_modified == SHADOW_FULL ) {
439 int mod_offset = (vertex_length - 3)>>1;
440 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
441 for( i=0; i<poly->vertex_count; i++ ) {
442 pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[3], ptr, mod_offset );
443 ptr += vertex_length;
444 }
445 } else {
446 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index],
447 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
448 pvr2_scene.vertex_index += poly->vertex_count;
449 }
450 }
451 }
452 }
454 static void scene_add_quad_vertexes( pvraddr_t poly_idx, int vertex_length,
455 shadow_mode_t is_modified )
456 {
457 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
458 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
459 uint32_t *context = ptr;
460 unsigned int i;
462 if( poly->vertex_index == -1 ) {
463 // Construct it locally and copy to the vertex buffer, as the VBO is
464 // allowed to be horribly slow for reads (ie it could be direct-mapped
465 // vram).
466 struct vertex_struct quad[4];
468 assert( poly != NULL );
469 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
470 ptr += (is_modified == SHADOW_FULL ? 5 : 3 );
471 poly->vertex_index = pvr2_scene.vertex_index;
472 for( i=0; i<4; i++ ) {
473 pvr2_decode_render_vertex( &quad[i], context[0], context[1], ptr, 0 );
474 ptr += vertex_length;
475 }
476 scene_compute_vertexes( &quad[3], 1, &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
477 // Swap last two vertexes (quad arrangement => tri strip arrangement)
478 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
479 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
480 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
481 pvr2_scene.vertex_index += 4;
483 if( is_modified ) {
484 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
485 poly->mod_vertex_index = pvr2_scene.vertex_index;
486 if( is_modified == SHADOW_FULL ) {
487 int mod_offset = (vertex_length - 3)>>1;
488 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
489 for( i=0; i<4; i++ ) {
490 pvr2_decode_render_vertex( &quad[4], context[0], context[3], ptr, mod_offset );
491 ptr += vertex_length;
492 }
493 scene_compute_vertexes( &quad[3], 1, &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
494 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
495 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
496 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
497 } else {
498 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index],
499 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
500 pvr2_scene.vertex_index += poly->vertex_count;
501 }
502 pvr2_scene.vertex_index += 4;
503 }
504 }
505 }
507 static void scene_extract_polygons( pvraddr_t tile_entry )
508 {
509 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
510 do {
511 uint32_t entry = *tile_list++;
512 if( entry >> 28 == 0x0F ) {
513 break;
514 } else if( entry >> 28 == 0x0E ) {
515 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
516 } else {
517 pvraddr_t polyaddr = entry&0x000FFFFF;
518 shadow_mode_t is_modified = (entry & 0x01000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
519 int vertex_length = (entry >> 21) & 0x07;
520 int context_length = 3;
521 if( is_modified == SHADOW_FULL ) {
522 context_length = 5;
523 vertex_length <<= 1 ;
524 }
525 vertex_length += 3;
527 if( (entry & 0xE0000000) == 0x80000000 ) {
528 /* Triangle(s) */
529 int strip_count = ((entry >> 25) & 0x0F)+1;
530 int polygon_length = 3 * vertex_length + context_length;
531 int i;
532 struct polygon_struct *last_poly = NULL;
533 for( i=0; i<strip_count; i++ ) {
534 struct polygon_struct *poly = scene_add_polygon( polyaddr, 3, is_modified );
535 polyaddr += polygon_length;
536 if( last_poly != NULL && last_poly->next == NULL ) {
537 last_poly->next = poly;
538 }
539 last_poly = poly;
540 }
541 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
542 /* Sprite(s) */
543 int strip_count = ((entry >> 25) & 0x0F)+1;
544 int polygon_length = 4 * vertex_length + context_length;
545 int i;
546 struct polygon_struct *last_poly = NULL;
547 for( i=0; i<strip_count; i++ ) {
548 struct polygon_struct *poly = scene_add_polygon( polyaddr, 4, is_modified );
549 polyaddr += polygon_length;
550 if( last_poly != NULL && last_poly->next == NULL ) {
551 last_poly->next = poly;
552 }
553 last_poly = poly;
554 }
555 } else {
556 /* Polygon */
557 int i, last = -1;
558 for( i=5; i>=0; i-- ) {
559 if( entry & (0x40000000>>i) ) {
560 last = i;
561 break;
562 }
563 }
564 if( last != -1 ) {
565 scene_add_polygon( polyaddr, last+3, is_modified );
566 }
567 }
568 }
569 } while( 1 );
570 }
572 static void scene_extract_vertexes( pvraddr_t tile_entry )
573 {
574 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
575 do {
576 uint32_t entry = *tile_list++;
577 if( entry >> 28 == 0x0F ) {
578 break;
579 } else if( entry >> 28 == 0x0E ) {
580 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
581 } else {
582 pvraddr_t polyaddr = entry&0x000FFFFF;
583 shadow_mode_t is_modified = (entry & 0x01000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
584 int vertex_length = (entry >> 21) & 0x07;
585 int context_length = 3;
586 if( is_modified == SHADOW_FULL ) {
587 context_length = 5;
588 vertex_length <<=1 ;
589 }
590 vertex_length += 3;
592 if( (entry & 0xE0000000) == 0x80000000 ) {
593 /* Triangle(s) */
594 int strip_count = ((entry >> 25) & 0x0F)+1;
595 int polygon_length = 3 * vertex_length + context_length;
596 int i;
597 for( i=0; i<strip_count; i++ ) {
598 scene_add_vertexes( polyaddr, vertex_length, is_modified );
599 polyaddr += polygon_length;
600 }
601 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
602 /* Sprite(s) */
603 int strip_count = ((entry >> 25) & 0x0F)+1;
604 int polygon_length = 4 * vertex_length + context_length;
605 int i;
606 for( i=0; i<strip_count; i++ ) {
607 scene_add_quad_vertexes( polyaddr, vertex_length, is_modified );
608 polyaddr += polygon_length;
609 }
610 } else {
611 /* Polygon */
612 int i, last = -1;
613 for( i=5; i>=0; i-- ) {
614 if( entry & (0x40000000>>i) ) {
615 last = i;
616 break;
617 }
618 }
619 if( last != -1 ) {
620 scene_add_vertexes( polyaddr, vertex_length, is_modified );
621 }
622 }
623 }
624 } while( 1 );
625 }
627 static void scene_extract_background( void )
628 {
629 uint32_t bgplane = MMIO_READ(PVR2, RENDER_BGPLANE);
630 int vertex_length = (bgplane >> 24) & 0x07;
631 int context_length = 3, i;
632 shadow_mode_t is_modified = (bgplane & 0x08000000) ? pvr2_scene.shadow_mode : SHADOW_NONE;
634 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
635 uint32_t *context = &pvr2_scene.pvr2_pbuf[(bgplane & 0x00FFFFFF)>>3];
636 poly->context = context;
637 poly->vertex_count = 4;
638 poly->vertex_index = pvr2_scene.vertex_count;
639 if( is_modified == SHADOW_FULL ) {
640 context_length = 5;
641 vertex_length <<= 1;
642 }
643 if( is_modified != SHADOW_NONE ) {
644 poly->mod_vertex_index = pvr2_scene.vertex_count + 4;
645 pvr2_scene.vertex_count += 8;
646 } else {
647 poly->mod_vertex_index = -1;
648 pvr2_scene.vertex_count += 4;
649 }
650 vertex_length += 3;
651 context_length += (bgplane & 0x07) * vertex_length;
653 poly->next = NULL;
654 pvr2_scene.bkgnd_poly = poly;
656 struct vertex_struct base_vertexes[3];
657 uint32_t *ptr = context + context_length;
658 for( i=0; i<3; i++ ) {
659 pvr2_decode_render_vertex( &base_vertexes[i], context[0], context[1],
660 ptr, 0 );
661 ptr += vertex_length;
662 }
663 struct vertex_struct *result_vertexes = &pvr2_scene.vertex_array[poly->vertex_index];
664 result_vertexes[0].x = result_vertexes[0].y = 0;
665 result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
666 result_vertexes[1].y = result_vertexes[2].x = 0;
667 result_vertexes[2].y = result_vertexes[3].y = pvr2_scene.buffer_height;
668 scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
670 if( is_modified == SHADOW_FULL ) {
671 int mod_offset = (vertex_length - 3)>>1;
672 ptr = context + context_length;
673 for( i=0; i<3; i++ ) {
674 pvr2_decode_render_vertex( &base_vertexes[i], context[0], context[3],
675 ptr, mod_offset );
676 ptr += vertex_length;
677 }
678 result_vertexes = &pvr2_scene.vertex_array[poly->mod_vertex_index];
679 result_vertexes[0].x = result_vertexes[0].y = 0;
680 result_vertexes[1].x = result_vertexes[3].x = pvr2_scene.buffer_width;
681 result_vertexes[1].y = result_vertexes[2].x = 0;
682 result_vertexes[2].y = result_vertexes[3].y = pvr2_scene.buffer_height;
683 scene_compute_vertexes( result_vertexes, 4, base_vertexes, !POLY1_GOURAUD_SHADED(context[0]) );
684 } else if( is_modified == SHADOW_CHEAP ) {
685 scene_add_cheap_shadow_vertexes( &pvr2_scene.vertex_array[poly->vertex_index],
686 &pvr2_scene.vertex_array[poly->mod_vertex_index], poly->vertex_count );
687 pvr2_scene.vertex_index += poly->vertex_count;
688 }
690 }
693 uint32_t pvr2_scene_buffer_width()
694 {
695 return pvr2_scene.buffer_width;
696 }
698 uint32_t pvr2_scene_buffer_height()
699 {
700 return pvr2_scene.buffer_height;
701 }
703 /**
704 * Extract the current scene into the rendering structures. We run two passes
705 * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts),
706 * second pass extracts the vertex data into the VBO/vertex array.
707 *
708 * Difficult to do in single pass as we don't generally know the size of a
709 * polygon for certain until we've seen all tiles containing it. It also means we
710 * can count the vertexes and allocate the appropriate size VBO.
711 *
712 * FIXME: accesses into VRAM need to be bounds-checked properly
713 */
714 void pvr2_scene_read( void )
715 {
716 pvr2_scene_init();
717 pvr2_scene_reset();
719 pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
720 pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
721 pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
722 pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
723 pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
725 uint32_t scaler = MMIO_READ( PVR2, RENDER_SCALER );
726 if( scaler & SCALER_HSCALE ) {
727 /* If the horizontal scaler is in use, we're (in principle) supposed to
728 * divide everything by 2. However in the interests of display quality,
729 * instead we want to render to the unscaled resolution and downsample
730 * only if/when required.
731 */
732 pvr2_scene.bounds[1] *= 2;
733 }
735 uint32_t fog_col = MMIO_READ( PVR2, RENDER_FOGTBLCOL );
736 unpack_bgra( fog_col, pvr2_scene.fog_lut_colour );
737 fog_col = MMIO_READ( PVR2, RENDER_FOGVRTCOL );
738 unpack_bgra( fog_col, pvr2_scene.fog_vert_colour );
740 uint32_t *tilebuffer = (uint32_t *)(pvr2_main_ram + MMIO_READ( PVR2, RENDER_TILEBASE ));
741 uint32_t *segment = tilebuffer;
742 uint32_t shadow = MMIO_READ(PVR2,RENDER_SHADOW);
743 pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
744 pvr2_scene.pvr2_pbuf = (uint32_t *)(pvr2_main_ram + MMIO_READ(PVR2,RENDER_POLYBASE));
745 pvr2_scene.shadow_mode = shadow & 0x100 ? SHADOW_CHEAP : SHADOW_FULL;
746 scene_shadow_intensity = U8TOFLOAT(shadow&0xFF);
748 int max_tile_x = 0;
749 int max_tile_y = 0;
750 int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
751 int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
753 if( (obj_config & 0x00200000) == 0 ) {
754 if( isp_config & 1 ) {
755 pvr2_scene.sort_mode = SORT_NEVER;
756 } else {
757 pvr2_scene.sort_mode = SORT_ALWAYS;
758 }
759 } else {
760 pvr2_scene.sort_mode = SORT_TILEFLAG;
761 }
763 // Pass 1: Extract polygon list
764 uint32_t control;
765 int i;
766 do {
767 control = *segment++;
768 int tile_x = SEGMENT_X(control);
769 int tile_y = SEGMENT_Y(control);
770 if( tile_x > max_tile_x ) {
771 max_tile_x = tile_x;
772 }
773 if( tile_y > max_tile_y ) {
774 max_tile_y = tile_y;
775 }
776 for( i=0; i<5; i++ ) {
777 if( (*segment & NO_POINTER) == 0 ) {
778 scene_extract_polygons( *segment );
779 }
780 segment++;
781 }
782 } while( (control & SEGMENT_END) == 0 );
784 pvr2_scene.buffer_width = (max_tile_x+1)<<5;
785 pvr2_scene.buffer_height = (max_tile_y+1)<<5;
787 // Pass 2: Extract vertex data
788 vertex_buffer_map();
789 pvr2_scene.vertex_index = 0;
790 segment = tilebuffer;
791 do {
792 control = *segment++;
793 for( i=0; i<5; i++ ) {
794 if( (*segment & NO_POINTER) == 0 ) {
795 scene_extract_vertexes( *segment );
796 }
797 segment++;
798 }
799 } while( (control & SEGMENT_END) == 0 );
801 scene_extract_background();
802 scene_compute_lut_fog();
804 vertex_buffer_unmap();
805 }
807 /**
808 * Dump the current scene to file in a (mostly) human readable form
809 */
810 void pvr2_scene_dump( FILE *f )
811 {
812 int i,j;
814 fprintf( f, "Polygons: %d\n", pvr2_scene.poly_count );
815 for( i=0; i<pvr2_scene.poly_count; i++ ) {
816 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
817 fprintf( f, " %08X ", ((unsigned char *)poly->context) - pvr2_main_ram );
818 switch( poly->vertex_count ) {
819 case 3: fprintf( f, "Tri " ); break;
820 case 4: fprintf( f, "Quad " ); break;
821 default: fprintf( f,"%d-Strip ", poly->vertex_count-2 ); break;
822 }
823 fprintf( f, "%08X %08X %08X ", poly->context[0], poly->context[1], poly->context[2] );
824 if( poly->mod_vertex_index != -1 ) {
825 fprintf( f, "%08X %08X\n", poly->context[3], poly->context[5] );
826 } else {
827 fprintf( f, "\n" );
828 }
830 for( j=0; j<poly->vertex_count; j++ ) {
831 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->vertex_index+j];
832 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,
833 v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
834 v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
835 }
836 if( poly->mod_vertex_index != -1 ) {
837 fprintf( f, " ---\n" );
838 for( j=0; j<poly->vertex_count; j++ ) {
839 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->mod_vertex_index+j];
840 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,
841 v->rgba[0], v->rgba[1], v->rgba[2], v->rgba[3],
842 v->offset_rgba[0], v->offset_rgba[1], v->offset_rgba[2], v->offset_rgba[3] );
843 }
844 }
845 }
847 }
.