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"
29 #define VBO_EXT_STRING "GL_ARB_vertex_buffer_object"
30 #define PBO_EXT_STRING "GL_ARB_pixel_buffer_object"
32 static inline uint32_t bgra_to_rgba(uint32_t bgra)
34 return (bgra&0xFF00FF00) | ((bgra&0x00FF0000)>>16) | ((bgra&0x000000FF)<<16);
38 * Convert a half-float (16-bit) FP number to a regular 32-bit float.
39 * Source is 1-bit sign, 5-bit exponent, 10-bit mantissa.
40 * TODO: Check the correctness of this.
42 static float halftofloat( uint16_t half )
48 temp.i = ((uint32_t)half)<<16;
56 struct pvr2_scene_struct pvr2_scene;
58 static gboolean vbo_init = FALSE;
59 static gboolean vbo_supported = FALSE;
62 * Test for VBO support, and allocate all the system memory needed for the
63 * temporary structures. GL context must have been initialized before this
66 void pvr2_scene_init()
69 #ifdef ENABLE_VERTEX_BUFFER
70 if( isGLExtensionSupported(VBO_EXT_STRING) ) {
72 pvr2_scene.vbo_id = 1;
75 pvr2_scene.vertex_array = NULL;
76 pvr2_scene.vertex_array_size = 0;
77 pvr2_scene.poly_array = g_malloc( MAX_POLY_BUFFER_SIZE );
78 pvr2_scene.buf_to_poly_map = g_malloc0( BUF_POLY_MAP_SIZE );
84 * Clear the scene data structures in preparation for fresh data
86 void pvr2_scene_reset()
88 pvr2_scene.poly_count = 0;
89 pvr2_scene.vertex_count = 0;
90 memset( pvr2_scene.buf_to_poly_map, 0, BUF_POLY_MAP_SIZE );
93 void pvr2_scene_shutdown()
95 #ifdef ENABLE_VERTEX_BUFFER
97 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
98 glDeleteBuffersARB( 1, &pvr2_scene.vbo_id );
99 pvr2_scene.vbo_id = 0;
102 g_free( pvr2_scene.vertex_array );
103 pvr2_scene.vertex_array = NULL;
104 #ifdef ENABLE_VERTEX_BUFFER
108 g_free( pvr2_scene.poly_array );
109 pvr2_scene.poly_array = NULL;
110 g_free( pvr2_scene.buf_to_poly_map );
111 pvr2_scene.buf_to_poly_map = NULL;
115 void *vertex_buffer_map()
118 uint32_t size = pvr2_scene.vertex_count * sizeof(struct vertex_struct);
119 #ifdef ENABLE_VERTEX_BUFFER
120 if( vbo_supported ) {
121 glBindBufferARB( GL_ARRAY_BUFFER_ARB, pvr2_scene.vbo_id );
122 if( size > pvr2_scene.vertex_array_size ) {
123 glBufferDataARB( GL_ARRAY_BUFFER_ARB, size, NULL, GL_DYNAMIC_DRAW_ARB );
124 int status = glGetError();
126 fprintf( stderr, "Error %08X allocating vertex buffer\n", status );
129 pvr2_scene.vertex_array_size = size;
131 pvr2_scene.vertex_array = glMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );
132 assert(pvr2_scene.vertex_array != NULL );
135 if( size > pvr2_scene.vertex_array_size ) {
136 pvr2_scene.vertex_array = g_realloc( pvr2_scene.vertex_array, size );
138 #ifdef ENABLE_VERTEX_BUFFER
141 return pvr2_scene.vertex_array;
144 gboolean vertex_buffer_unmap()
146 #ifdef ENABLE_VERTEX_BUFFER
147 if( vbo_supported ) {
148 pvr2_scene.vertex_array = NULL;
149 return glUnmapBufferARB( GL_ARRAY_BUFFER_ARB );
158 static struct polygon_struct *scene_add_polygon( pvraddr_t poly_idx, int vertex_count,
159 gboolean is_modified )
161 int vert_mul = is_modified ? 2 : 1;
163 if( pvr2_scene.buf_to_poly_map[poly_idx] != NULL ) {
164 if( vertex_count > pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count ) {
165 pvr2_scene.vertex_count += (vertex_count - pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count) * vert_mul;
166 pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count = vertex_count;
168 return pvr2_scene.buf_to_poly_map[poly_idx];
170 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
171 poly->context = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE) + (poly_idx<<2));
172 poly->vertex_count = vertex_count;
173 poly->vertex_index = -1;
174 poly->mod_vertex_index = -1;
176 pvr2_scene.buf_to_poly_map[poly_idx] = poly;
177 pvr2_scene.vertex_count += (vertex_count * vert_mul);
183 * Decode a single PVR2 renderable vertex (opaque/trans/punch-out, but not shadow
185 * @param vert Pointer to output vertex structure
186 * @param poly1 First word of polygon context (needed to understand vertex)
187 * @param poly2 Second word of polygon context
188 * @param pvr2_data Pointer to raw pvr2 vertex data (in VRAM)
189 * @param modify_offset Offset in 32-bit words to the tex/color data. 0 for
190 * the normal vertex, half the vertex length for the modified vertex.
192 static void pvr2_decode_render_vertex( struct vertex_struct *vert, uint32_t poly1,
193 uint32_t poly2, uint32_t *pvr2_data,
196 gboolean force_alpha = !POLY2_ALPHA_ENABLE(poly2);
197 union pvr2_data_type {
202 data.ival = pvr2_data;
204 vert->x = *data.fval++;
205 vert->y = *data.fval++;
207 float z = *data.fval++;
210 } else if( z != 0 ) {
213 if( z > pvr2_scene.bounds[5] ) {
214 pvr2_scene.bounds[5] = z;
215 } else if( z < pvr2_scene.bounds[4] && z != 0 ) {
216 pvr2_scene.bounds[4] = z;
219 data.ival += modify_offset;
222 if( POLY1_TEXTURED(poly1) ) {
223 if( POLY1_UV16(poly1) ) {
224 vert->u = halftofloat( *data.ival>>16 );
225 vert->v = halftofloat( *data.ival );
228 vert->u = *data.fval++;
229 vert->v = *data.fval++;
231 if( POLY2_TEX_BLEND(poly2) == 1 ) {
236 vert->rgba = bgra_to_rgba((*data.ival++) | 0xFF000000);
237 if( POLY1_SPECULAR(poly1) ) {
238 vert->offset_rgba = bgra_to_rgba((*data.ival++) | 0xFF000000);
240 vert->offset_rgba = 0;
243 vert->rgba = bgra_to_rgba(*data.ival++);
244 if( POLY1_SPECULAR(poly1) ) {
245 vert->offset_rgba = bgra_to_rgba(*data.ival++);
247 vert->offset_rgba = 0;
253 * Compute texture, colour, and z values for a result point by interpolating from
254 * a set of 3 input points. The result point must define its x,y.
256 static void scene_compute_vertex( struct vertex_struct *result,
257 struct vertex_struct *input,
258 gboolean is_solid_shaded )
261 float sx = input[2].x - input[1].x;
262 float sy = input[2].y - input[1].y;
263 float tx = input[0].x - input[1].x;
264 float ty = input[0].y - input[1].y;
266 float detxy = ((sy) * (tx)) - ((ty) * (sx));
268 result->z = input[2].z;
269 result->u = input[2].u;
270 result->v = input[2].v;
271 result->rgba = input[2].rgba;
272 result->offset_rgba = input[2].offset_rgba;
275 float t = ((result->x - input[1].x) * sy -
276 (result->y - input[1].y) * sx) / detxy;
277 float s = ((result->y - input[1].y) * tx -
278 (result->x - input[1].x) * ty) / detxy;
280 float sz = input[2].z - input[1].z;
281 float tz = input[0].z - input[1].z;
282 float su = input[2].u - input[1].u;
283 float tu = input[0].u - input[1].u;
284 float sv = input[2].v - input[1].v;
285 float tv = input[0].v - input[1].v;
287 float rz = input[1].z + (t*tz) + (s*sz);
288 if( rz > pvr2_scene.bounds[5] ) {
289 pvr2_scene.bounds[5] = rz;
290 } else if( rz < pvr2_scene.bounds[4] ) {
291 pvr2_scene.bounds[4] = rz;
294 result->u = input[1].u + (t*tu) + (s*su);
295 result->v = input[1].v + (t*tv) + (s*sv);
297 if( is_solid_shaded ) {
298 result->rgba = input[2].rgba;
299 result->offset_rgba = input[2].offset_rgba;
301 uint8_t *rgba0 = (uint8_t *)&input[0].rgba;
302 uint8_t *rgba1 = (uint8_t *)&input[1].rgba;
303 uint8_t *rgba2 = (uint8_t *)&input[2].rgba;
304 uint8_t *rgba3 = (uint8_t *)&result->rgba;
305 for( i=0; i<8; i++ ) { // note: depends on rgba & offset_rgba being adjacent
306 float tc = *rgba0++ - *rgba1;
307 float sc = *rgba2++ - *rgba1;
308 float rc = *rgba1++ + (t*tc) + (s*sc);
311 } else if( rc > 255 ) {
320 static void scene_add_vertexes( pvraddr_t poly_idx, int vertex_length,
321 gboolean is_modified )
323 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
324 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
325 uint32_t *context = ptr;
328 if( poly->vertex_index == -1 ) {
329 ptr += (is_modified ? 5 : 3 );
330 poly->vertex_index = pvr2_scene.vertex_index;
332 assert( poly != NULL );
333 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
334 for( i=0; i<poly->vertex_count; i++ ) {
335 pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[1], ptr, 0 );
336 ptr += vertex_length;
339 int mod_offset = (vertex_length - 3)>>1;
340 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
341 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
342 poly->mod_vertex_index = pvr2_scene.vertex_index;
343 for( i=0; i<poly->vertex_count; i++ ) {
344 pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[3], ptr, mod_offset );
345 ptr += vertex_length;
351 static void scene_add_quad_vertexes( pvraddr_t poly_idx, int vertex_length,
352 gboolean is_modified )
354 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
355 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
356 uint32_t *context = ptr;
359 if( poly->vertex_index == -1 ) {
360 // Construct it locally and copy to the vertex buffer, as the VBO is
361 // allowed to be horribly slow for reads (ie it could be direct-mapped
363 struct vertex_struct quad[4];
365 assert( poly != NULL );
366 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
367 ptr += (is_modified ? 5 : 3 );
368 poly->vertex_index = pvr2_scene.vertex_index;
369 for( i=0; i<4; i++ ) {
370 pvr2_decode_render_vertex( &quad[i], context[0], context[1], ptr, 0 );
371 ptr += vertex_length;
373 scene_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
374 // Swap last two vertexes (quad arrangement => tri strip arrangement)
375 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
376 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
377 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
378 pvr2_scene.vertex_index += 4;
381 int mod_offset = (vertex_length - 3)>>1;
382 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
383 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
384 poly->mod_vertex_index = pvr2_scene.vertex_index;
385 for( i=0; i<4; i++ ) {
386 pvr2_decode_render_vertex( &quad[4], context[0], context[3], ptr, mod_offset );
387 ptr += vertex_length;
389 scene_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
390 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
391 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
392 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
393 pvr2_scene.vertex_index += 4;
398 static void scene_extract_polygons( pvraddr_t tile_entry )
400 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
402 uint32_t entry = *tile_list++;
403 if( entry >> 28 == 0x0F ) {
405 } else if( entry >> 28 == 0x0E ) {
406 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
408 pvraddr_t polyaddr = entry&0x000FFFFF;
409 int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
410 int vertex_length = (entry >> 21) & 0x07;
411 int context_length = 3;
414 vertex_length <<= 1 ;
418 if( (entry & 0xE0000000) == 0x80000000 ) {
420 int strip_count = ((entry >> 25) & 0x0F)+1;
421 int polygon_length = 3 * vertex_length + context_length;
423 struct polygon_struct *last_poly = NULL;
424 for( i=0; i<strip_count; i++ ) {
425 struct polygon_struct *poly = scene_add_polygon( polyaddr, 3, is_modified );
426 polyaddr += polygon_length;
427 if( last_poly != NULL && last_poly->next == NULL ) {
428 last_poly->next = poly;
432 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
434 int strip_count = ((entry >> 25) & 0x0F)+1;
435 int polygon_length = 4 * vertex_length + context_length;
437 struct polygon_struct *last_poly = NULL;
438 for( i=0; i<strip_count; i++ ) {
439 struct polygon_struct *poly = scene_add_polygon( polyaddr, 4, is_modified );
440 polyaddr += polygon_length;
441 if( last_poly != NULL && last_poly->next == NULL ) {
442 last_poly->next = poly;
449 for( i=5; i>=0; i-- ) {
450 if( entry & (0x40000000>>i) ) {
456 scene_add_polygon( polyaddr, last+3, is_modified );
463 static void scene_extract_vertexes( pvraddr_t tile_entry )
465 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
467 uint32_t entry = *tile_list++;
468 if( entry >> 28 == 0x0F ) {
470 } else if( entry >> 28 == 0x0E ) {
471 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
473 pvraddr_t polyaddr = entry&0x000FFFFF;
474 int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
475 int vertex_length = (entry >> 21) & 0x07;
476 int context_length = 3;
483 if( (entry & 0xE0000000) == 0x80000000 ) {
485 int strip_count = ((entry >> 25) & 0x0F)+1;
486 int polygon_length = 3 * vertex_length + context_length;
488 for( i=0; i<strip_count; i++ ) {
489 scene_add_vertexes( polyaddr, vertex_length, is_modified );
490 polyaddr += polygon_length;
492 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
494 int strip_count = ((entry >> 25) & 0x0F)+1;
495 int polygon_length = 4 * vertex_length + context_length;
497 for( i=0; i<strip_count; i++ ) {
498 scene_add_quad_vertexes( polyaddr, vertex_length, is_modified );
499 polyaddr += polygon_length;
504 for( i=5; i>=0; i-- ) {
505 if( entry & (0x40000000>>i) ) {
511 scene_add_vertexes( polyaddr, vertex_length, is_modified );
518 uint32_t pvr2_scene_buffer_width()
520 return pvr2_scene.buffer_width;
523 uint32_t pvr2_scene_buffer_height()
525 return pvr2_scene.buffer_height;
529 * Extract the current scene into the rendering structures. We run two passes
530 * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts),
531 * second pass extracts the vertex data into the VBO/vertex array.
533 * Difficult to do in single pass as we don't generally know the size of a
534 * polygon for certain until we've seen all tiles containing it. It also means we
535 * can count the vertexes and allocate the appropriate size VBO.
537 * FIXME: accesses into VRAM need to be bounds-checked properly
539 void pvr2_scene_read( void )
544 pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
545 pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
546 pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
547 pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
548 pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
550 uint32_t *tilebuffer = (uint32_t *)(video_base + MMIO_READ( PVR2, RENDER_TILEBASE ));
551 uint32_t *segment = tilebuffer;
552 pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
553 pvr2_scene.pvr2_pbuf = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE));
554 pvr2_scene.full_shadow = MMIO_READ( PVR2, RENDER_SHADOW ) & 0x100 ? FALSE : TRUE;
558 int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
559 int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
561 if( (obj_config & 0x00200000) == 0 ) {
562 if( isp_config & 1 ) {
563 pvr2_scene.sort_mode = SORT_NEVER;
565 pvr2_scene.sort_mode = SORT_ALWAYS;
568 pvr2_scene.sort_mode = SORT_TILEFLAG;
571 // Pass 1: Extract polygon list
575 control = *segment++;
576 int tile_x = SEGMENT_X(control);
577 int tile_y = SEGMENT_Y(control);
578 if( tile_x > max_tile_x ) {
581 if( tile_y > max_tile_y ) {
584 for( i=0; i<5; i++ ) {
585 if( (*segment & NO_POINTER) == 0 ) {
586 scene_extract_polygons( *segment );
590 } while( (control & SEGMENT_END) == 0 );
592 pvr2_scene.buffer_width = (max_tile_x+1)<<5;
593 pvr2_scene.buffer_height = (max_tile_y+1)<<5;
595 if( pvr2_scene.vertex_count > 0 ) {
596 // Pass 2: Extract vertex data
598 pvr2_scene.vertex_index = 0;
599 segment = tilebuffer;
601 control = *segment++;
602 for( i=0; i<5; i++ ) {
603 if( (*segment & NO_POINTER) == 0 ) {
604 scene_extract_vertexes( *segment );
608 } while( (control & SEGMENT_END) == 0 );
609 vertex_buffer_unmap();
614 * Dump the current scene to file in a (mostly) human readable form
616 void pvr2_scene_dump( FILE *f )
620 fprintf( f, "Polygons: %d\n", pvr2_scene.poly_count );
621 for( i=0; i<pvr2_scene.poly_count; i++ ) {
622 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
623 fprintf( f, " %08X ", ((char *)poly->context) - video_base );
624 switch( poly->vertex_count ) {
625 case 3: fprintf( f, "Tri " ); break;
626 case 4: fprintf( f, "Quad " ); break;
627 default: fprintf( f,"%d-Strip ", poly->vertex_count-2 ); break;
629 fprintf( f, "%08X %08X %08X ", poly->context[0], poly->context[1], poly->context[2] );
630 if( poly->mod_vertex_index != -1 ) {
631 fprintf( f, "%08X %08X\n", poly->context[3], poly->context[5] );
636 for( j=0; j<poly->vertex_count; j++ ) {
637 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->vertex_index+j];
638 fprintf( f, " %.5f %.5f %.5f, (%.5f,%.5f) %08X %08X\n", v->x, v->y, v->z, v->u, v->v,
639 v->rgba, v->offset_rgba );
641 if( poly->mod_vertex_index != -1 ) {
642 fprintf( f, " ---\n" );
643 for( j=0; j<poly->vertex_count; j++ ) {
644 struct vertex_struct *v = &pvr2_scene.vertex_array[poly->mod_vertex_index+j];
645 fprintf( f, " %.5f %.5f %.5f, (%.5f,%.5f) %08X %08X\n", v->x, v->y, v->z, v->u, v->v,
646 v->rgba, v->offset_rgba );
.