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.
24 #include "pvr2/pvr2.h"
25 #include "pvr2/glutil.h"
26 #include "pvr2/scene.h"
28 #define VBO_EXT_STRING "GL_ARB_vertex_buffer_object"
29 #define PBO_EXT_STRING "GL_ARB_pixel_buffer_object"
31 static inline uint32_t bgra_to_rgba(uint32_t bgra)
33 return (bgra&0xFF00FF00) | ((bgra&0x00FF0000)>>16) | ((bgra&0x000000FF)<<16);
36 struct pvr2_scene_struct pvr2_scene;
38 static gboolean vbo_init = FALSE;
39 static gboolean vbo_supported = FALSE;
42 * Test for VBO support, and allocate all the system memory needed for the
43 * temporary structures. GL context must have been initialized before this
46 void pvr2_scene_init()
49 if( isGLExtensionSupported(VBO_EXT_STRING) ) {
51 pvr2_scene.vbo_id = 1;
53 pvr2_scene.vertex_array = NULL;
54 pvr2_scene.vertex_array_size = 0;
55 pvr2_scene.poly_array = g_malloc( MAX_POLY_BUFFER_SIZE );
56 pvr2_scene.buf_to_poly_map = g_malloc0( BUF_POLY_MAP_SIZE );
62 * Clear the scene data structures in preparation for fresh data
64 void pvr2_scene_reset()
66 pvr2_scene.poly_count = 0;
67 pvr2_scene.vertex_count = 0;
68 memset( pvr2_scene.buf_to_poly_map, 0, BUF_POLY_MAP_SIZE );
71 void pvr2_scene_shutdown()
74 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
75 glDeleteBuffersARB( 1, &pvr2_scene.vbo_id );
76 pvr2_scene.vbo_id = 0;
78 g_free( pvr2_scene.vertex_array );
79 pvr2_scene.vertex_array = NULL;
81 g_free( pvr2_scene.poly_array );
82 g_free( pvr2_scene.buf_to_poly_map );
86 void *vertex_buffer_map()
89 uint32_t size = pvr2_scene.vertex_count * sizeof(struct vertex_struct);
91 glBindBufferARB( GL_ARRAY_BUFFER_ARB, pvr2_scene.vbo_id );
92 assert( glGetError() == 0 );
93 if( size > pvr2_scene.vertex_array_size ) {
94 glBufferDataARB( GL_ARRAY_BUFFER_ARB, size, NULL, GL_DYNAMIC_DRAW_ARB );
95 int status = glGetError();
97 fprintf( stderr, "Error %08X allocating vertex buffer\n", status );
100 pvr2_scene.vertex_array_size = size;
102 pvr2_scene.vertex_array = glMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );
103 assert(pvr2_scene.vertex_array != NULL );
105 if( size > pvr2_scene.vertex_array_size ) {
106 pvr2_scene.vertex_array = g_realloc( pvr2_scene.vertex_array, size );
109 return pvr2_scene.vertex_array;
112 gboolean vertex_buffer_unmap()
114 if( vbo_supported ) {
115 pvr2_scene.vertex_array = NULL;
116 return glUnmapBufferARB( GL_ARRAY_BUFFER_ARB );
122 static struct polygon_struct *scene_add_polygon( pvraddr_t poly_idx, int vertex_count,
123 gboolean is_modified )
125 int vert_mul = is_modified ? 2 : 1;
127 if( pvr2_scene.buf_to_poly_map[poly_idx] != NULL ) {
128 if( vertex_count > pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count ) {
129 pvr2_scene.vertex_count += (vertex_count - pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count) * vert_mul;
130 pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count = vertex_count;
132 return pvr2_scene.buf_to_poly_map[poly_idx];
134 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
135 poly->context = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE) + (poly_idx<<2));
136 poly->vertex_count = vertex_count;
137 poly->vertex_index = -1;
139 pvr2_scene.buf_to_poly_map[poly_idx] = poly;
140 pvr2_scene.vertex_count += (vertex_count * vert_mul);
146 * Decode a single PVR2 renderable vertex (opaque/trans/punch-out, but not shadow
148 * @param vert Pointer to output vertex structure
149 * @param poly1 First word of polygon context (needed to understand vertex)
150 * @param poly2 Second word of polygon context
151 * @param pvr2_data Pointer to raw pvr2 vertex data (in VRAM)
152 * @param modify_offset Offset in 32-bit words to the tex/color data. 0 for
153 * the normal vertex, half the vertex length for the modified vertex.
155 static void pvr2_decode_render_vertex( struct vertex_struct *vert, uint32_t poly1,
156 uint32_t poly2, uint32_t *pvr2_data,
159 gboolean force_alpha = !POLY2_ALPHA_ENABLE(poly2);
160 union pvr2_data_type {
165 data.ival = pvr2_data;
167 vert->x = *data.fval++;
168 vert->y = *data.fval++;
170 float z = *data.fval++;
171 if( z > pvr2_scene.bounds[5] ) {
172 pvr2_scene.bounds[5] = z;
173 } else if( z < pvr2_scene.bounds[4] && z != 0 ) {
174 pvr2_scene.bounds[4] = z;
177 data.ival += modify_offset;
180 if( POLY1_TEXTURED(poly1) ) {
181 if( POLY1_UV16(poly1) ) {
182 vert->u = halftofloat( *data.ival>>16 );
183 vert->v = halftofloat( *data.ival );
186 vert->u = *data.fval++;
187 vert->v = *data.fval++;
189 if( POLY2_TEX_BLEND(poly2) == 1 ) {
194 vert->rgba = bgra_to_rgba((*data.ival++) | 0xFF000000);
195 if( POLY1_SPECULAR(poly1) ) {
196 vert->offset_rgba = bgra_to_rgba((*data.ival++) | 0xFF000000);
199 vert->rgba = bgra_to_rgba(*data.ival++);
200 if( POLY1_SPECULAR(poly1) ) {
201 vert->offset_rgba = bgra_to_rgba(*data.ival++);
207 * Compute texture, colour, and z values for a result point by interpolating from
208 * a set of 3 input points. The result point must define its x,y.
210 static void scene_compute_vertex( struct vertex_struct *result,
211 struct vertex_struct *input,
212 gboolean is_solid_shaded )
215 float sx = input[2].x - input[1].x;
216 float sy = input[2].y - input[1].y;
217 float tx = input[0].x - input[1].x;
218 float ty = input[0].y - input[1].y;
220 float detxy = ((sy) * (tx)) - ((ty) * (sx));
222 result->z = input[2].z;
223 result->u = input[2].u;
224 result->v = input[2].v;
225 result->rgba = input[2].rgba;
226 result->offset_rgba = input[2].offset_rgba;
229 float t = ((result->x - input[1].x) * sy -
230 (result->y - input[1].y) * sx) / detxy;
231 float s = ((result->y - input[1].y) * tx -
232 (result->x - input[1].x) * ty) / detxy;
234 float sz = input[2].z - input[1].z;
235 float tz = input[0].z - input[1].z;
236 float su = input[2].u - input[1].u;
237 float tu = input[0].u - input[1].u;
238 float sv = input[2].v - input[1].v;
239 float tv = input[0].v - input[1].v;
241 float rz = input[1].z + (t*tz) + (s*sz);
242 if( rz > pvr2_scene.bounds[5] ) {
243 pvr2_scene.bounds[5] = rz;
244 } else if( rz < pvr2_scene.bounds[4] ) {
245 pvr2_scene.bounds[4] = rz;
248 result->u = input[1].u + (t*tu) + (s*su);
249 result->v = input[1].v + (t*tv) + (s*sv);
251 if( is_solid_shaded ) {
252 result->rgba = input[2].rgba;
253 result->offset_rgba = input[2].offset_rgba;
255 uint8_t *rgba0 = (uint8_t *)&input[0].rgba;
256 uint8_t *rgba1 = (uint8_t *)&input[1].rgba;
257 uint8_t *rgba2 = (uint8_t *)&input[2].rgba;
258 uint8_t *rgba3 = (uint8_t *)&result->rgba;
259 for( i=0; i<8; i++ ) { // note: depends on rgba & offset_rgba being adjacent
260 float tc = *rgba0++ - *rgba1;
261 float sc = *rgba2++ - *rgba1;
262 float rc = *rgba1++ + (t*tc) + (s*sc);
265 } else if( rc > 255 ) {
274 static void scene_add_vertexes( pvraddr_t poly_idx, int vertex_length,
275 gboolean is_modified )
277 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
278 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
279 uint32_t *context = ptr;
282 assert( poly != NULL );
283 if( poly->vertex_index == -1 ) {
284 ptr += (is_modified ? 5 : 3 );
285 poly->vertex_index = pvr2_scene.vertex_index;
287 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
288 for( i=0; i<poly->vertex_count; i++ ) {
289 pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[1], ptr, 0 );
290 ptr += vertex_length;
293 int mod_offset = (vertex_length - 3)>>1;
294 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
295 poly->mod_vertex_index = pvr2_scene.vertex_index;
296 for( i=0; i<poly->vertex_count; i++ ) {
297 pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], context[3], ptr, mod_offset );
298 ptr += vertex_length;
304 static void scene_add_quad_vertexes( pvraddr_t poly_idx, int vertex_length,
305 gboolean is_modified )
307 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
308 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
309 uint32_t *context = ptr;
312 if( poly->vertex_index == -1 ) {
313 // Construct it locally and copy to the vertex buffer, as the VBO is
314 // allowed to be horribly slow for reads (ie it could be direct-mapped
316 struct vertex_struct quad[4];
318 assert( poly != NULL );
319 ptr += (is_modified ? 5 : 3 );
320 poly->vertex_index = pvr2_scene.vertex_index;
321 for( i=0; i<4; i++ ) {
322 pvr2_decode_render_vertex( &quad[i], context[0], context[1], ptr, 0 );
323 ptr += vertex_length;
325 scene_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
326 // Swap last two vertexes (quad arrangement => tri strip arrangement)
327 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
328 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
329 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
330 pvr2_scene.vertex_index += 4;
333 int mod_offset = (vertex_length - 3)>>1;
334 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
335 poly->mod_vertex_index = pvr2_scene.vertex_index;
336 for( i=0; i<4; i++ ) {
337 pvr2_decode_render_vertex( &quad[4], context[0], context[3], ptr, mod_offset );
338 ptr += vertex_length;
340 scene_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
341 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(struct vertex_struct)*2 );
342 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+2], &quad[3], sizeof(struct vertex_struct) );
343 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index+3], &quad[2], sizeof(struct vertex_struct) );
344 pvr2_scene.vertex_index += 4;
349 static void scene_extract_polygons( pvraddr_t tile_entry )
351 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
353 uint32_t entry = *tile_list++;
354 if( entry >> 28 == 0x0F ) {
356 } else if( entry >> 28 == 0x0E ) {
357 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
359 pvraddr_t polyaddr = entry&0x000FFFFF;
360 int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
361 int vertex_length = (entry >> 21) & 0x07;
362 int context_length = 3;
365 vertex_length <<= 1 ;
369 if( (entry & 0xE0000000) == 0x80000000 ) {
371 int strip_count = ((entry >> 25) & 0x0F)+1;
372 int polygon_length = 3 * vertex_length + context_length;
374 struct polygon_struct *last_poly = NULL;
375 for( i=0; i<strip_count; i++ ) {
376 struct polygon_struct *poly = scene_add_polygon( polyaddr, 3, is_modified );
377 polyaddr += polygon_length;
378 if( last_poly != NULL && last_poly->next == NULL ) {
379 last_poly->next = poly;
383 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
385 int strip_count = ((entry >> 25) & 0x0F)+1;
386 int polygon_length = 4 * vertex_length + context_length;
388 struct polygon_struct *last_poly = NULL;
389 for( i=0; i<strip_count; i++ ) {
390 struct polygon_struct *poly = scene_add_polygon( polyaddr, 4, is_modified );
391 polyaddr += polygon_length;
392 if( last_poly != NULL && last_poly->next == NULL ) {
393 last_poly->next = poly;
400 for( i=5; i>=0; i-- ) {
401 if( entry & (0x40000000>>i) ) {
407 scene_add_polygon( polyaddr, last+3, is_modified );
414 static void scene_extract_vertexes( pvraddr_t tile_entry )
416 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
418 uint32_t entry = *tile_list++;
419 if( entry >> 28 == 0x0F ) {
421 } else if( entry >> 28 == 0x0E ) {
422 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
424 pvraddr_t polyaddr = entry&0x000FFFFF;
425 int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
426 int vertex_length = (entry >> 21) & 0x07;
427 int context_length = 3;
434 if( (entry & 0xE0000000) == 0x80000000 ) {
436 int strip_count = ((entry >> 25) & 0x0F)+1;
437 int polygon_length = 3 * vertex_length + context_length;
439 for( i=0; i<strip_count; i++ ) {
440 scene_add_vertexes( polyaddr, vertex_length, is_modified );
441 polyaddr += polygon_length;
443 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
445 int strip_count = ((entry >> 25) & 0x0F)+1;
446 int polygon_length = 4 * vertex_length + context_length;
448 for( i=0; i<strip_count; i++ ) {
449 scene_add_quad_vertexes( polyaddr, vertex_length, is_modified );
450 polyaddr += polygon_length;
455 for( i=5; i>=0; i-- ) {
456 if( entry & (0x40000000>>i) ) {
462 scene_add_vertexes( polyaddr, vertex_length, is_modified );
469 uint32_t pvr2_scene_buffer_width()
471 return pvr2_scene.buffer_width;
474 uint32_t pvr2_scene_buffer_height()
476 return pvr2_scene.buffer_height;
480 * Extract the current scene into the rendering structures. We run two passes
481 * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts),
482 * second pass extracts the vertex data into the VBO/vertex array.
484 * Difficult to do in single pass as we don't generally know the size of a
485 * polygon for certain until we've seen all tiles containing it. It also means we
486 * can count the vertexes and allocate the appropriate size VBO.
488 * FIXME: accesses into VRAM need to be bounds-checked properly
490 void pvr2_scene_read( void )
495 pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
496 pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
497 pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
498 pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
499 pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
501 uint32_t *tilebuffer = (uint32_t *)(video_base + MMIO_READ( PVR2, RENDER_TILEBASE ));
502 uint32_t *segment = tilebuffer;
503 pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
504 pvr2_scene.pvr2_pbuf = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE));
505 pvr2_scene.full_shadow = MMIO_READ( PVR2, RENDER_SHADOW ) & 0x100 ? FALSE : TRUE;
509 int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
510 int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
512 if( (obj_config & 0x00200000) == 0 ) {
513 if( isp_config & 1 ) {
514 pvr2_scene.sort_mode = SORT_NEVER;
516 pvr2_scene.sort_mode = SORT_ALWAYS;
519 pvr2_scene.sort_mode = SORT_BYFLAG;
522 // Pass 1: Extract polygon list
526 control = *segment++;
527 int tile_x = SEGMENT_X(control);
528 int tile_y = SEGMENT_Y(control);
529 if( tile_x > max_tile_x ) {
532 if( tile_y > max_tile_y ) {
535 for( i=0; i<5; i++ ) {
536 if( (*segment & NO_POINTER) == 0 ) {
537 scene_extract_polygons( *segment );
541 } while( (control & SEGMENT_END) == 0 );
543 pvr2_scene.buffer_width = (max_tile_x+1)<<5;
544 pvr2_scene.buffer_height = (max_tile_y+1)<<5;
546 if( pvr2_scene.vertex_count > 0 ) {
547 // Pass 2: Extract vertex data
549 pvr2_scene.vertex_index = 0;
550 segment = tilebuffer;
552 control = *segment++;
553 for( i=0; i<5; i++ ) {
554 if( (*segment & NO_POINTER) == 0 ) {
555 scene_extract_vertexes( *segment );
559 } while( (control & SEGMENT_END) == 0 );
561 vertex_buffer_unmap();
.