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 struct pvr2_scene_struct pvr2_scene;
33 static gboolean vbo_init = FALSE;
34 static gboolean vbo_supported = FALSE;
37 * Test for VBO support, and allocate all the system memory needed for the
38 * temporary structures. GL context must have been initialized before this
41 void pvr2_scene_init()
44 if( isGLExtensionSupported(VBO_EXT_STRING) ) {
46 pvr2_scene.vbo_id = 1;
48 pvr2_scene.vertex_array = NULL;
49 pvr2_scene.vertex_array_size = 0;
50 pvr2_scene.poly_array = g_malloc( MAX_POLY_BUFFER_SIZE );
51 pvr2_scene.buf_to_poly_map = g_malloc0( BUF_POLY_MAP_SIZE );
56 void pvr2_scene_shutdown()
59 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
60 glDeleteBuffersARB( 1, &pvr2_scene.vbo_id );
61 pvr2_scene.vbo_id = 0;
63 g_free( pvr2_scene.vertex_array );
64 pvr2_scene.vertex_array = NULL;
66 g_free( pvr2_scene.poly_array );
67 g_free( pvr2_scene.buf_to_poly_map );
71 void *vertex_buffer_map()
73 uint32_t size = pvr2_scene.vertex_count * sizeof(struct vertex_struct);
75 glBindBufferARB( GL_ARRAY_BUFFER_ARB, pvr2_scene.vbo_id );
77 if( size > pvr2_scene.vertex_array_size ) {
78 glBufferDataARB( GL_ARRAY_BUFFER_ARB, size, NULL, GL_DYNAMIC_DRAW_ARB );
79 assert( glGetError() == 0 );
81 pvr2_scene.vertex_array = glMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB );
82 assert(pvr2_scene.vertex_array != NULL );
84 if( size > pvr2_scene.vertex_array_size ) {
85 pvr2_scene.vertex_array = g_realloc( pvr2_scene.vertex_array, size );
88 return pvr2_scene.vertex_array;
91 gboolean vertex_buffer_unmap()
94 pvr2_scene.vertex_array = NULL;
95 return glUnmapBufferARB( GL_ARRAY_BUFFER_ARB );
101 static struct polygon_struct *vertex_buffer_add_polygon( pvraddr_t poly_idx, int vertex_count,
102 gboolean is_modified )
104 int vert_mul = is_modified ? 2 : 1;
106 if( pvr2_scene.buf_to_poly_map[poly_idx] != NULL ) {
107 if( vertex_count > pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count ) {
108 pvr2_scene.vertex_count += (vertex_count - pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count) * vert_mul;
109 pvr2_scene.buf_to_poly_map[poly_idx]->vertex_count = vertex_count;
111 return pvr2_scene.buf_to_poly_map[poly_idx];
113 struct polygon_struct *poly = &pvr2_scene.poly_array[pvr2_scene.poly_count++];
114 poly->context = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE) + (poly_idx<<2));
115 poly->vertex_count = vertex_count;
116 poly->vertex_index = -1;
117 pvr2_scene.buf_to_poly_map[poly_idx] = poly;
118 pvr2_scene.vertex_count += (vertex_count * vert_mul);
124 * Decode a single PVR2 renderable vertex (opaque/trans/punch-out, but not shadow
126 * @param vert Pointer to output vertex structure
127 * @param poly1 First word of polygon context (needed to understand vertex)
128 * @param pvr2_data Pointer to raw pvr2 vertex data (in VRAM)
129 * @param modify_offset Offset in 32-bit words to the tex/color data. 0 for
130 * the normal vertex, half the vertex length for the modified vertex.
132 static void pvr2_decode_render_vertex( struct vertex_struct *vert, uint32_t poly1,
133 uint32_t *pvr2_data, int modify_offset )
135 union pvr2_data_type {
140 data.ival = pvr2_data;
142 vert->x = *data.fval++;
143 vert->y = *data.fval++;
145 float z = *data.fval++;
146 if( z > pvr2_scene.bounds[5] ) {
147 pvr2_scene.bounds[5] = z;
148 } else if( z < pvr2_scene.bounds[4] && z != 0 ) {
149 pvr2_scene.bounds[4] = z;
152 data.ival += modify_offset;
155 if( POLY1_TEXTURED(poly1) ) {
156 if( POLY1_UV16(poly1) ) {
157 vert->u = halftofloat( *data.ival>>16 );
158 vert->v = halftofloat( *data.ival );
161 vert->u = *data.fval++;
162 vert->v = *data.fval++;
165 vert->rgba = *data.ival++;
166 if( POLY1_SPECULAR(poly1) ) {
167 vert->offset_rgba = *data.ival++;
172 * Compute texture, colour, and z values for a result point by interpolating from
173 * a set of 3 input points. The result point must define its x,y.
175 static void vertex_buffer_compute_vertex( struct vertex_struct *result,
176 struct vertex_struct *input,
177 gboolean is_solid_shaded )
180 float sx = input[2].x - input[1].x;
181 float sy = input[2].y - input[1].y;
182 float tx = input[0].x - input[1].x;
183 float ty = input[0].y - input[1].y;
185 float detxy = ((sy) * (tx)) - ((ty) * (sx));
187 result->z = input[2].z;
188 result->u = input[2].u;
189 result->v = input[2].v;
190 result->rgba = input[2].rgba;
191 result->offset_rgba = input[2].offset_rgba;
194 float t = ((result->x - input[1].x) * sy -
195 (result->y - input[1].y) * sx) / detxy;
196 float s = ((result->y - input[1].y) * tx -
197 (result->x - input[1].x) * ty) / detxy;
199 float sz = input[2].z - input[1].z;
200 float tz = input[0].z - input[1].z;
201 float su = input[2].u - input[1].u;
202 float tu = input[0].u - input[1].u;
203 float sv = input[2].v - input[1].v;
204 float tv = input[0].v - input[1].v;
206 float rz = input[1].z + (t*tz) + (s*sz);
207 if( rz > pvr2_scene.bounds[5] ) {
208 pvr2_scene.bounds[5] = rz;
209 } else if( rz < pvr2_scene.bounds[4] ) {
210 pvr2_scene.bounds[4] = rz;
213 result->u = input[1].u + (t*tu) + (s*su);
214 result->v = input[1].v + (t*tv) + (s*sv);
216 if( is_solid_shaded ) {
217 result->rgba = input[2].rgba;
218 result->offset_rgba = input[2].offset_rgba;
220 uint8_t *rgba0 = (uint8_t *)&input[0].rgba;
221 uint8_t *rgba1 = (uint8_t *)&input[1].rgba;
222 uint8_t *rgba2 = (uint8_t *)&input[2].rgba;
223 uint8_t *rgba3 = (uint8_t *)&result->rgba;
224 for( i=0; i<8; i++ ) { // note: depends on rgba & offset_rgba being adjacent
225 float tc = *rgba0++ - *rgba1;
226 float sc = *rgba2++ - *rgba1;
227 float rc = *rgba1++ + (t*tc) + (s*sc);
230 } else if( rc > 255 ) {
239 static void vertex_buffer_add_vertexes( pvraddr_t poly_idx, int vertex_length,
240 gboolean is_modified )
242 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
243 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
244 uint32_t *context = ptr;
247 assert( poly != NULL );
248 if( poly->vertex_index == -1 ) {
249 ptr += (is_modified ? 5 : 3 );
250 poly->vertex_index = pvr2_scene.vertex_index;
251 assert( pvr2_scene.vertex_index + poly->vertex_count <= pvr2_scene.vertex_count );
252 for( i=0; i<poly->vertex_count; i++ ) {
253 pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], ptr, 0 );
254 ptr += vertex_length;
257 int mod_offset = (vertex_length - 3)>>1;
258 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
259 poly->mod_vertex_index = pvr2_scene.vertex_index;
260 for( i=0; i<poly->vertex_count; i++ ) {
261 pvr2_decode_render_vertex( &pvr2_scene.vertex_array[pvr2_scene.vertex_index++], context[0], ptr, mod_offset );
262 ptr += vertex_length;
268 static void vertex_buffer_add_quad_vertexes( pvraddr_t poly_idx, int vertex_length,
269 gboolean is_modified )
271 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[poly_idx];
272 uint32_t *ptr = &pvr2_scene.pvr2_pbuf[poly_idx];
273 uint32_t *context = ptr;
276 if( poly->vertex_index == -1 ) {
277 // Construct it locally and copy to the vertex buffer, as the VBO is
278 // allowed to be horribly slow for reads (ie it could be direct-mapped
280 struct vertex_struct quad[4];
282 assert( poly != NULL );
283 ptr += (is_modified ? 5 : 3 );
284 poly->vertex_index = pvr2_scene.vertex_index;
285 for( i=0; i<4; i++ ) {
286 pvr2_decode_render_vertex( &quad[i], context[0], ptr, 0 );
287 ptr += vertex_length;
289 vertex_buffer_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
290 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(quad) );
291 pvr2_scene.vertex_index += 4;
294 int mod_offset = (vertex_length - 3)>>1;
295 ptr = &pvr2_scene.pvr2_pbuf[poly_idx] + 5;
296 poly->mod_vertex_index = pvr2_scene.vertex_index;
297 for( i=0; i<4; i++ ) {
298 pvr2_decode_render_vertex( &quad[4], context[0], ptr, mod_offset );
299 ptr += vertex_length;
301 vertex_buffer_compute_vertex( &quad[3], &quad[0], !POLY1_GOURAUD_SHADED(context[0]) );
302 memcpy( &pvr2_scene.vertex_array[pvr2_scene.vertex_index], quad, sizeof(quad) );
303 pvr2_scene.vertex_index += 4;
308 static void vertex_buffer_extract_polygons( pvraddr_t tile_entry )
310 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
312 uint32_t entry = *tile_list++;
313 if( entry >> 28 == 0x0F ) {
315 } else if( entry >> 28 == 0x0E ) {
316 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
318 pvraddr_t polyaddr = entry&0x000FFFFF;
319 int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
320 int vertex_length = (entry >> 21) & 0x07;
321 int context_length = 3;
324 vertex_length <<= 1 ;
328 if( (entry & 0xE0000000) == 0x80000000 ) {
330 int strip_count = ((entry >> 25) & 0x0F)+1;
331 int polygon_length = 3 * vertex_length + context_length;
333 struct polygon_struct *last_poly = NULL;
334 for( i=0; i<strip_count; i++ ) {
335 struct polygon_struct *poly = vertex_buffer_add_polygon( polyaddr, 3, is_modified );
336 polyaddr += polygon_length;
337 if( last_poly != NULL && last_poly->next == NULL ) {
338 last_poly->next = poly;
342 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
344 int strip_count = ((entry >> 25) & 0x0F)+1;
345 int polygon_length = 4 * vertex_length + context_length;
347 struct polygon_struct *last_poly = NULL;
348 for( i=0; i<strip_count; i++ ) {
349 struct polygon_struct *poly = vertex_buffer_add_polygon( polyaddr, 4, is_modified );
350 polyaddr += polygon_length;
351 if( last_poly != NULL && last_poly->next == NULL ) {
352 last_poly->next = poly;
359 for( i=5; i>=0; i-- ) {
360 if( entry & (0x40000000>>i) ) {
366 vertex_buffer_add_polygon( polyaddr, last+3, is_modified );
373 static void vertex_buffer_extract_vertexes( pvraddr_t tile_entry )
375 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
377 uint32_t entry = *tile_list++;
378 if( entry >> 28 == 0x0F ) {
380 } else if( entry >> 28 == 0x0E ) {
381 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
383 pvraddr_t polyaddr = entry&0x000FFFFF;
384 int is_modified = (entry & 0x01000000) && pvr2_scene.full_shadow;
385 int vertex_length = (entry >> 21) & 0x07;
386 int context_length = 3;
393 if( (entry & 0xE0000000) == 0x80000000 ) {
395 int strip_count = ((entry >> 25) & 0x0F)+1;
396 int polygon_length = 3 * vertex_length + context_length;
398 for( i=0; i<strip_count; i++ ) {
399 vertex_buffer_add_vertexes( polyaddr, vertex_length, is_modified );
400 polyaddr += polygon_length;
402 } else if( (entry & 0xE0000000) == 0xA0000000 ) {
404 int strip_count = ((entry >> 25) & 0x0F)+1;
405 int polygon_length = 4 * vertex_length + context_length;
407 for( i=0; i<strip_count; i++ ) {
408 vertex_buffer_add_quad_vertexes( polyaddr, vertex_length, is_modified );
409 polyaddr += polygon_length;
414 for( i=5; i>=0; i-- ) {
415 if( entry & (0x40000000>>i) ) {
421 vertex_buffer_add_vertexes( polyaddr, vertex_length, is_modified );
429 * Extract the current scene into the rendering structures. We run two passes
430 * - first pass extracts the polygons into pvr2_scene.poly_array (finding vertex counts),
431 * second pass extracts the vertex data into the VBO/vertex array.
433 * Difficult to do in single pass as we don't generally know the size of a
434 * polygon for certain until we've seen all tiles containing it. It also means we
435 * can count the vertexes and allocate the appropriate size VBO.
437 * FIXME: accesses into VRAM need to be bounds-checked properly
439 void pvr2_scene_read( void )
443 pvr2_scene.poly_count = 0;
444 pvr2_scene.vertex_count = 0;
445 pvr2_scene.bounds[0] = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
446 pvr2_scene.bounds[1] = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) + 1;
447 pvr2_scene.bounds[2] = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
448 pvr2_scene.bounds[3] = ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) + 1;
449 pvr2_scene.bounds[4] = pvr2_scene.bounds[5] = MMIO_READF( PVR2, RENDER_FARCLIP );
451 uint32_t *tilebuffer = (uint32_t *)(video_base + MMIO_READ( PVR2, RENDER_TILEBASE ));
452 uint32_t *segment = tilebuffer;
453 pvr2_scene.segment_list = (struct tile_segment *)tilebuffer;
454 pvr2_scene.pvr2_pbuf = (uint32_t *)(video_base + MMIO_READ(PVR2,RENDER_POLYBASE));
455 pvr2_scene.full_shadow = MMIO_READ( PVR2, RENDER_SHADOW ) & 0x100 ? FALSE : TRUE;
459 int obj_config = MMIO_READ( PVR2, RENDER_OBJCFG );
460 int isp_config = MMIO_READ( PVR2, RENDER_ISPCFG );
462 if( (obj_config & 0x00200000) == 0 ) {
463 if( isp_config & 1 ) {
464 pvr2_scene.sort_mode = SORT_NEVER;
466 pvr2_scene.sort_mode = SORT_ALWAYS;
469 pvr2_scene.sort_mode = SORT_BYFLAG;
472 // Pass 1: Extract polygon list
476 control = *segment++;
477 int tile_x = SEGMENT_X(control);
478 int tile_y = SEGMENT_Y(control);
479 if( tile_x > max_tile_x ) {
482 if( tile_y > max_tile_y ) {
485 for( i=0; i<5; i++ ) {
486 if( (*segment & NO_POINTER) == 0 ) {
487 vertex_buffer_extract_polygons( *segment );
491 } while( (control & SEGMENT_END) == 0 );
493 pvr2_scene.buffer_width = (max_tile_x+1)<<5;
494 pvr2_scene.buffer_height = (max_tile_y+1)<<5;
496 if( pvr2_scene.vertex_count > 0 ) {
497 // Pass 2: Extract vertex data
499 pvr2_scene.vertex_index = 0;
500 segment = tilebuffer;
502 control = *segment++;
503 for( i=0; i<5; i++ ) {
504 if( (*segment & NO_POINTER) == 0 ) {
505 vertex_buffer_extract_vertexes( *segment );
509 } while( (control & SEGMENT_END) == 0 );
511 vertex_buffer_unmap();
516 * Render the data in the scene structure. The GL target should already be setup.
517 * Note: thar be GL code here.
519 void vertex_buffer_render()
523 glEnable( GL_SCISSOR_TEST );
526 struct tile_segment *segment = pvr2_scene.segment_list;
530 } while( !IS_LAST_SEGMENT(segment) );
531 glDisable( GL_SCISSOR_TEST );
.