4 * Manage the internal vertex/polygon buffers and scene data structure.
5 * Where possible this uses VBOs for the vertex + index data.
7 * Copyright (c) 2005 Nathan Keynes.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
25 #include "pvr2/pvr2.h"
26 #include "pvr2/glutil.h"
27 #include "pvr2/scene.h"
30 static inline uint32_t bgra_to_rgba(uint32_t bgra)
32 return (bgra&0xFF00FF00) | ((bgra&0x00FF0000)>>16) | ((bgra&0x000000FF)<<16);
36 * Convert a half-float (16-bit) FP number to a regular 32-bit float.
37 * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
38 * TODO: Check the correctness of this.
40 static float halftofloat( uint16_t half )
46 temp.i = ((uint32_t)half)<<16;
54 struct pvr2_scene_struct pvr2_scene;
56 static gboolean vbo_init = FALSE;
57 static gboolean vbo_supported = FALSE;
60 * Test for VBO support, and allocate all the system memory needed for the
61 * temporary structures. GL context must have been initialized before this
64 void pvr2_scene_init()
67 #ifdef ENABLE_VERTEX_BUFFER
68 if( isGLVertexBufferSupported() ) {
70 pvr2_scene.vbo_id = 1;
73 pvr2_scene.vertex_array = NULL;
74 pvr2_scene.vertex_array_size = 0;
75 pvr2_scene.poly_array = g_malloc( MAX_POLY_BUFFER_SIZE );
76 pvr2_scene.buf_to_poly_map = g_malloc0( BUF_POLY_MAP_SIZE );
82 * Clear the scene data structures in preparation for fresh data
84 void pvr2_scene_reset()
86 pvr2_scene.poly_count = 0;
87 pvr2_scene.vertex_count = 0;
88 memset( pvr2_scene.buf_to_poly_map, 0, BUF_POLY_MAP_SIZE );
91 void pvr2_scene_shutdown()
93 #ifdef ENABLE_VERTEX_BUFFER
95 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
96 glDeleteBuffersARB( 1, &pvr2_scene.vbo_id );
97 pvr2_scene.vbo_id = 0;
100 g_free( pvr2_scene.vertex_array );
101 pvr2_scene.vertex_array = NULL;
102 #ifdef ENABLE_VERTEX_BUFFER
106 g_free( pvr2_scene.poly_array );
107 pvr2_scene.poly_array = NULL;
108 g_free( pvr2_scene.buf_to_poly_map );
109 pvr2_scene.buf_to_poly_map = NULL;
113 void *vertex_buffer_map()
116 uint32_t size = pvr2_scene.vertex_count * sizeof(struct vertex_struct);
117 #ifdef ENABLE_VERTEX_BUFFER
118 if( vbo_supported ) {
119 glBindBufferARB( GL_ARRAY_BUFFER_ARB, pvr2_scene.vbo_id );
120 if( size > pvr2_scene.vertex_array_size ) {
121 glBufferDataARB( GL_ARRAY_BUFFER_ARB, size, NULL, GL_DYNAMIC_DRAW_ARB );
122 int status = glGetError();
124 fprintf( stderr, "Error %08X allocating vertex buffer\n", status );
127 pvr2_scene.vertex_array_size = size;
129 pvr2_scene.vertex_array = glMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );
130 assert(pvr2_scene.vertex_array != NULL );
133 if( size > pvr2_scene.vertex_array_size ) {
134 pvr2_scene.vertex_array = g_realloc( pvr2_scene.vertex_array, size );
136 #ifdef ENABLE_VERTEX_BUFFER
139 return pvr2_scene.vertex_array;
142 gboolean vertex_buffer_unmap()
144 #ifdef ENABLE_VERTEX_BUFFER
145 if( vbo_supported ) {
146 pvr2_scene.vertex_array = NULL;
147 return glUnmapBufferARB( GL_ARRAY_BUFFER_ARB );
156 static struct polygon_struct *scene_add_polygon( pvraddr_t poly_idx, int vertex_count,
157 gboolean is_modified )
159 int vert_mul = is_modified ? 2 : 1;
161 if( pvr2_scene.buf_to_poly_map[poly_idx] != NULL ) {
162 if( vertex_count > pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count ) {
163 pvr2_scene.vertex_count += (vertex_count - pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count) * vert_mul;
164 pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count = vertex_count;
166 return pvr2_scene.buf_to_poly_map[poly_idx];
168 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
169 poly->context = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE) + (poly_idx<<2));
170 poly->vertex_count = vertex_count;
171 poly->vertex_index = -1;
172 poly->mod_vertex_index = -1;
174 pvr2_scene.buf_to_poly_map[poly_idx] = poly;
175 pvr2_scene.vertex_count += (vertex_count * vert_mul);
181 * Decode a single PVR2 renderable vertex (opaque/trans/punch-out, but not shadow
183 * @param vert Pointer to output vertex structure
184 * @param poly1 First word of polygon context (needed to understand vertex)
185 * @param poly2 Second word of polygon context
186 * @param pvr2_data Pointer to raw pvr2 vertex data (in VRAM)
187 * @param modify_offset Offset in 32-bit words to the tex/color data. 0 for
188 * the normal vertex, half the vertex length for the modified vertex.
190 static void pvr2_decode_render_vertex( struct vertex_struct *vert, uint32_t poly1,
191 uint32_t poly2, uint32_t *pvr2_data,
194 gboolean force_alpha = !POLY2_ALPHA_ENABLE(poly2);
195 union pvr2_data_type {
200 data.ival = pvr2_data;
202 vert->x = *data.fval++;
203 vert->y = *data.fval++;
205 float z = *data.fval++;
208 } else if( z != 0 ) {
211 if( z > pvr2_scene.bounds[5] ) {
212 pvr2_scene.bounds[5] = z;
213 } else if( z < pvr2_scene.bounds[4] && z != 0 ) {
214 pvr2_scene.bounds[4] = z;
217 data.ival += modify_offset;
220 if( POLY1_TEXTURED(poly1) ) {
221 if( POLY1_UV16(poly1) ) {
222 vert->u = halftofloat( *data.ival>>16 );
223 vert->v = halftofloat( *data.ival );
226 vert->u = *data.fval++;
227 vert->v = *data.fval++;
229 if( POLY2_TEX_BLEND(poly2) == 1 ) {
234 vert->rgba = bgra_to_rgba((*data.ival++) | 0xFF000000);
235 if( POLY1_SPECULAR(poly1) ) {
236 vert->offset_rgba = bgra_to_rgba((*data.ival++) | 0xFF000000);
238 vert->offset_rgba = 0;
241 vert->rgba = bgra_to_rgba(*data.ival++);
242 if( POLY1_SPECULAR(poly1) ) {
243 vert->offset_rgba = bgra_to_rgba(*data.ival++);
245 vert->offset_rgba = 0;
251 * Compute texture, colour, and z values for a result point by interpolating from
252 * a set of 3 input points. The result point must define its x,y.
254 static void scene_compute_vertex( struct vertex_struct *result,
255 struct vertex_struct *input,
256 gboolean is_solid_shaded )
259 float sx = input[2].x - input[1].x;
260 float sy = input[2].y - input[1].y;
261 float tx = input[0].x - input[1].x;
262 float ty = input[0].y - input[1].y;
264 float detxy = ((sy) * (tx)) - ((ty) * (sx));
266 result->z = input[2].z;
267 result->u = input[2].u;
268 result->v = input[2].v;
269 result->rgba = input[2].rgba;
270 result->offset_rgba = input[2].offset_rgba;
273 float t = ((result->x - input[1].x) * sy -
274 (result->y - input[1].y) * sx) / detxy;
275 float s = ((result->y - input[1].y) * tx -
276 (result->x - input[1].x) * ty) / detxy;
278 float sz = input[2].z - input[1].z;
279 float tz = input[0].z - input[1].z;
280 float su = input[2].u - input[1].u;
281 float tu = input[0].u - input[1].u;
282 float sv = input[2].v - input[1].v;
283 float tv = input[0].v - input[1].v;
285 float rz = input[1].z + (t*tz) + (s*sz);
286 if( rz > pvr2_scene.bounds[5] ) {
287 pvr2_scene.bounds[5] = rz;
288 } else if( rz < pvr2_scene.bounds[4] ) {
289 pvr2_scene.bounds[4] = rz;
292 result->u = input[1].u + (t*tu) + (s*su);
293 result->v = input[1].v + (t*tv) + (s*sv);
295 if( is_solid_shaded ) {
296 result->rgba = input[2].rgba;
297 result->offset_rgba = input[2].offset_rgba;
299 uint8_t *rgba0 = (uint8_t *)&input[0].rgba;
300 uint8_t *rgba1 = (uint8_t *)&input[1].rgba;
301 uint8_t *rgba2 = (uint8_t *)&input[2].rgba;
302 uint8_t *rgba3 = (uint8_t *)&result->rgba;
303 for( i=0; i<8; i++ ) { // note: depends on rgba & offset_rgba being adjacent
304 float tc = *rgba0++ - *rgba1;
305 float sc = *rgba2++ - *rgba1;
306 float rc = *rgba1++ + (t*tc) + (s*sc);
309 } else if( rc > 255 ) {
318 static void scene_add_vertexes( pvraddr_t poly_idx, int vertex_length,
319 gboolean is_modified )
321 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
322 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
323 uint32_t *context = ptr;
326 if( poly->vertex_index == -1 ) {
327 ptr += (is_modified ? 5 : 3 );
328 poly->vertex_index = pvr2_scene.vertex_index;
330 assert( poly != NULL );
331 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
332 for( i=0; i<poly->vertex_count; i++ ) {
333 pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[1], ptr, 0 );
334 ptr += vertex_length;
337 int mod_offset = (vertex_length - 3)>>1;
338 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
339 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
340 poly->mod_vertex_index = pvr2_scene.vertex_index;
341 for( i=0; i<poly->vertex_count; i++ ) {
342 pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[3], ptr, mod_offset );
343 ptr += vertex_length;
349 static void scene_add_quad_vertexes( pvraddr_t poly_idx, int vertex_length,
350 gboolean is_modified )
352 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
353 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
354 uint32_t *context = ptr;
357 if( poly->vertex_index == -1 ) {
358 // Construct it locally and copy to the vertex buffer, as the VBO is
359 // allowed to be horribly slow for reads (ie it could be direct-mapped
361 struct vertex_struct quad[4];
363 assert( poly != NULL );
364 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
365 ptr += (is_modified ? 5 : 3 );
366 poly->vertex_index = pvr2_scene.vertex_index;
367 for( i=0; i<4; i++ ) {
368 pvr2_decode_render_vertex( &quad[i], context[0], context[1], ptr, 0 );
369 ptr += vertex_length;
371 scene_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
372 // Swap last two vertexes (quad arrangement => tri strip arrangement)
373 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
374 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
375 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
376 pvr2_scene.vertex_index += 4;
379 int mod_offset = (vertex_length - 3)>>1;
380 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
381 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
382 poly->mod_vertex_index = pvr2_scene.vertex_index;
383 for( i=0; i<4; i++ ) {
384 pvr2_decode_render_vertex( &quad[4], context[0], context[3], ptr, mod_offset );
385 ptr += vertex_length;
387 scene_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
388 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
389 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
390 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
391 pvr2_scene.vertex_index += 4;
396 static void scene_extract_polygons( pvraddr_t tile_entry )
398 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
400 uint32_t entry = *tile_list++;
401 if( entry >> 28 == 0x0F ) {
403 } else if( entry >> 28 == 0x0E ) {
404 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
406 pvraddr_t polyaddr = entry&0x000FFFFF;
407 int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
408 int vertex_length = (entry >> 21) & 0x07;
409 int context_length = 3;
412 vertex_length <<= 1 ;
416 if( (entry & 0xE0000000) == 0x80000000 ) {
418 int strip_count = ((entry >> 25) & 0x0F)+1;
419 int polygon_length = 3 * vertex_length + context_length;
421 struct polygon_struct *last_poly = NULL;
422 for( i=0; i<strip_count; i++ ) {
423 struct polygon_struct *poly = scene_add_polygon( polyaddr, 3, is_modified );
424 polyaddr += polygon_length;
425 if( last_poly != NULL && last_poly->next == NULL ) {
426 last_poly->next = poly;
430 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
432 int strip_count = ((entry >> 25) & 0x0F)+1;
433 int polygon_length = 4 * vertex_length + context_length;
435 struct polygon_struct *last_poly = NULL;
436 for( i=0; i<strip_count; i++ ) {
437 struct polygon_struct *poly = scene_add_polygon( polyaddr, 4, is_modified );
438 polyaddr += polygon_length;
439 if( last_poly != NULL && last_poly->next == NULL ) {
440 last_poly->next = poly;
447 for( i=5; i>=0; i-- ) {
448 if( entry & (0x40000000>>i) ) {
454 scene_add_polygon( polyaddr, last+3, is_modified );
461 static void scene_extract_vertexes( pvraddr_t tile_entry )
463 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
465 uint32_t entry = *tile_list++;
466 if( entry >> 28 == 0x0F ) {
468 } else if( entry >> 28 == 0x0E ) {
469 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
471 pvraddr_t polyaddr = entry&0x000FFFFF;
472 int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
473 int vertex_length = (entry >> 21) & 0x07;
474 int context_length = 3;
481 if( (entry & 0xE0000000) == 0x80000000 ) {
483 int strip_count = ((entry >> 25) & 0x0F)+1;
484 int polygon_length = 3 * vertex_length + context_length;
486 for( i=0; i<strip_count; i++ ) {
487 scene_add_vertexes( polyaddr, vertex_length, is_modified );
488 polyaddr += polygon_length;
490 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
492 int strip_count = ((entry >> 25) & 0x0F)+1;
493 int polygon_length = 4 * vertex_length + context_length;
495 for( i=0; i<strip_count; i++ ) {
496 scene_add_quad_vertexes( polyaddr, vertex_length, is_modified );
497 polyaddr += polygon_length;
502 for( i=5; i>=0; i-- ) {
503 if( entry & (0x40000000>>i) ) {
509 scene_add_vertexes( polyaddr, vertex_length, is_modified );
516 uint32_t pvr2_scene_buffer_width()
518 return pvr2_scene.buffer_width;
521 uint32_t pvr2_scene_buffer_height()
523 return pvr2_scene.buffer_height;
527 * Extract the current scene into the rendering structures. We run two passes
528 * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts),
529 * second pass extracts the vertex data into the VBO/vertex array.
531 * Difficult to do in single pass as we don't generally know the size of a
532 * polygon for certain until we've seen all tiles containing it. It also means we
533 * can count the vertexes and allocate the appropriate size VBO.
535 * FIXME: accesses into VRAM need to be bounds-checked properly
537 void pvr2_scene_read( void )
542 pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
543 pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
544 pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
545 pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
546 pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
548 uint32_t *tilebuffer = (uint32_t *)(video_base + MMIO_READ( PVR2, RENDER_TILEBASE ));
549 uint32_t *segment = tilebuffer;
550 pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
551 pvr2_scene.pvr2_pbuf = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE));
552 pvr2_scene.full_shadow = MMIO_READ( PVR2, RENDER_SHADOW ) & 0x100 ? FALSE : TRUE;
556 int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
557 int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
559 if( (obj_config & 0x00200000) == 0 ) {
560 if( isp_config & 1 ) {
561 pvr2_scene.sort_mode = SORT_NEVER;
563 pvr2_scene.sort_mode = SORT_ALWAYS;
566 pvr2_scene.sort_mode = SORT_TILEFLAG;
569 // Pass 1: Extract polygon list
573 control = *segment++;
574 int tile_x = SEGMENT_X(control);
575 int tile_y = SEGMENT_Y(control);
576 if( tile_x > max_tile_x ) {
579 if( tile_y > max_tile_y ) {
582 for( i=0; i<5; i++ ) {
583 if( (*segment & NO_POINTER) == 0 ) {
584 scene_extract_polygons( *segment );
588 } while( (control & SEGMENT_END) == 0 );
590 pvr2_scene.buffer_width = (max_tile_x+1)<<5;
591 pvr2_scene.buffer_height = (max_tile_y+1)<<5;
593 if( pvr2_scene.vertex_count > 0 ) {
594 // Pass 2: Extract vertex data
596 pvr2_scene.vertex_index = 0;
597 segment = tilebuffer;
599 control = *segment++;
600 for( i=0; i<5; i++ ) {
601 if( (*segment & NO_POINTER) == 0 ) {
602 scene_extract_vertexes( *segment );
606 } while( (control & SEGMENT_END) == 0 );
607 vertex_buffer_unmap();
612 * Dump the current scene to file in a (mostly) human readable form
614 void pvr2_scene_dump( FILE *f )
618 fprintf( f, "Polygons: %d\n", pvr2_scene.poly_count );
619 for( i=0; i<pvr2_scene.poly_count; i++ ) {
620 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
621 fprintf( f, " %08X ", ((char *)poly->context) - video_base );
622 switch( poly->vertex_count ) {
623 case 3: fprintf( f, "Tri " ); break;
624 case 4: fprintf( f, "Quad " ); break;
625 default: fprintf( f,"%d-Strip ", poly->vertex_count-2 ); break;
627 fprintf( f, "%08X %08X %08X ", poly->context[0], poly->context[1], poly->context[2] );
628 if( poly->mod_vertex_index != -1 ) {
629 fprintf( f, "%08X %08X\n", poly->context[3], poly->context[5] );
634 for( j=0; j<poly->vertex_count; j++ ) {
635 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->vertex_index+j];
636 fprintf( f, " %.5f %.5f %.5f, (%.5f,%.5f) %08X %08X\n", v->x, v->y, v->z, v->u, v->v,
637 v->rgba, v->offset_rgba );
639 if( poly->mod_vertex_index != -1 ) {
640 fprintf( f, " ---\n" );
641 for( j=0; j<poly->vertex_count; j++ ) {
642 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->mod_vertex_index+j];
643 fprintf( f, " %.5f %.5f %.5f, (%.5f,%.5f) %08X %08X\n", v->x, v->y, v->z, v->u, v->v,
644 v->rgba, v->offset_rgba );
.