4 * Standard OpenGL rendering engine.
6 * Copyright (c) 2005 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
22 #include "pvr2/pvr2.h"
23 #include "pvr2/pvr2mmio.h"
24 #include "pvr2/scene.h"
25 #include "pvr2/glutil.h"
27 #define IS_EMPTY_TILE_LIST(p) ((*((uint32_t *)(pvr2_main_ram+(p))) >> 28) == 0x0F)
29 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
30 GL_GREATER, GL_NOTEQUAL, GL_GEQUAL,
32 int pvr2_poly_srcblend[8] = {
33 GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
34 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
35 GL_ONE_MINUS_DST_ALPHA };
36 int pvr2_poly_dstblend[8] = {
37 GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
38 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
39 GL_ONE_MINUS_DST_ALPHA };
40 int pvr2_poly_texblend[4] = {
48 * Clip the tile bounds to the clipping plane.
49 * @return TRUE if the tile was not clipped completely.
51 static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
53 if( tile[0] < clip[0] ) tile[0] = clip[0];
54 if( tile[1] > clip[1] ) tile[1] = clip[1];
55 if( tile[2] < clip[2] ) tile[2] = clip[2];
56 if( tile[3] > clip[3] ) tile[3] = clip[3];
57 return tile[0] < tile[1] && tile[2] < tile[3];
60 void pvr2_scene_load_textures()
64 texcache_set_config( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03,
65 (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5 );
67 for( i=0; i < pvr2_scene.poly_count; i++ ) {
68 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
69 if( POLY1_TEXTURED(poly->context[0]) ) {
70 poly->tex_id = texcache_get_texture( poly->context[2],
71 POLY2_TEX_WIDTH(poly->context[1]),
72 POLY2_TEX_HEIGHT(poly->context[1]) );
73 if( poly->mod_vertex_index != -1 ) {
74 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
75 poly->mod_tex_id = texcache_get_texture( poly->context[4],
76 POLY2_TEX_WIDTH(poly->context[3]),
77 POLY2_TEX_HEIGHT(poly->context[3]) );
79 poly->mod_tex_id = poly->tex_id;
84 poly->mod_tex_id = -1;
92 * Once-off call to setup the OpenGL context.
94 void pvr2_setup_gl_context()
97 if( glsl_is_supported() ) {
98 if( !glsl_load_shaders( glsl_vertex_shader_src, NULL ) ) {
99 WARN( "Unable to load GL shaders" );
103 texcache_gl_init(); // Allocate texture IDs
104 glCullFace( GL_BACK );
105 glEnable( GL_BLEND );
106 glEnable( GL_DEPTH_TEST );
107 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
108 glMatrixMode(GL_MODELVIEW);
111 #ifdef HAVE_OPENGL_CLAMP_COLOR
112 if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
113 glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
114 glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
118 glEnableClientState( GL_COLOR_ARRAY );
119 glEnableClientState( GL_VERTEX_ARRAY );
120 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
121 glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
122 glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
124 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
129 static void render_set_cull( uint32_t poly1 )
131 switch( POLY1_CULL_MODE(poly1) ) {
134 glDisable( GL_CULL_FACE );
137 glEnable( GL_CULL_FACE );
138 glFrontFace( GL_CW );
141 glEnable( GL_CULL_FACE );
142 glFrontFace( GL_CCW );
148 * Setup the basic context that's shared between normal and modified modes -
151 static void render_set_base_context( uint32_t poly1, GLint depth_mode )
153 if( depth_mode == 0 ) {
154 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
156 glDepthFunc(depth_mode);
159 glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
160 render_set_cull( poly1 );
164 * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
166 static void render_set_tsp_context( uint32_t poly1, uint32_t poly2, uint32_t texture )
168 glShadeModel( POLY1_SHADE_MODEL(poly1) );
170 if( POLY1_SPECULAR(poly1) ) {
171 glEnable(GL_COLOR_SUM);
173 glDisable(GL_COLOR_SUM);
176 if( POLY1_TEXTURED(poly1) ) {
177 glEnable(GL_TEXTURE_2D);
178 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
179 if( POLY2_TEX_CLAMP_U(poly2) ) {
180 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
181 } else if( POLY2_TEX_MIRROR_U(poly2) ) {
182 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB );
184 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
186 if( POLY2_TEX_CLAMP_V(poly2) ) {
187 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
188 } else if( POLY2_TEX_MIRROR_V(poly2) ) {
189 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB );
191 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
194 glDisable( GL_TEXTURE_2D );
197 switch( POLY2_FOG_MODE(poly2) ) {
198 case PVR2_POLY_FOG_LOOKUP:
199 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
202 case PVR2_POLY_FOG_VERTEX:
203 if( POLY1_SPECULAR(poly1) ) {
204 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
207 } /* else fallthrough */
212 int srcblend = POLY2_SRC_BLEND(poly2);
213 int destblend = POLY2_DEST_BLEND(poly2);
214 glBlendFunc( srcblend, destblend );
216 if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
217 WARN( "Accumulation buffer not supported" );
222 * Setup the GL context for the supplied polygon context.
223 * @param context pointer to 3 or 5 words of polygon context
224 * @param depth_mode force depth mode, or 0 to use the polygon's
227 void render_set_context( uint32_t *context, GLint depth_mode )
229 render_set_base_context(context[0], depth_mode);
230 render_set_tsp_context(context[0],context[1],context[2]);
234 static void gl_render_poly( struct polygon_struct *poly, GLint depth_mode )
236 if( poly->tex_id != -1 ) {
237 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
239 if( poly->mod_vertex_index == -1 ) {
240 glDisable( GL_STENCIL_TEST );
241 render_set_context( poly->context, depth_mode );
242 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
244 glEnable( GL_STENCIL_TEST );
245 render_set_base_context( poly->context[0], depth_mode );
246 render_set_tsp_context( poly->context[0], poly->context[1], poly->context[2] );
247 glStencilFunc(GL_EQUAL, 0, 2);
248 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
250 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
251 if( poly->mod_tex_id != -1 ) {
252 glBindTexture(GL_TEXTURE_2D, poly->mod_tex_id);
254 render_set_tsp_context( poly->context[0], poly->context[3], poly->context[4] );
256 glStencilFunc(GL_EQUAL, 2, 2);
257 glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count );
261 static void gl_render_bkgnd( struct polygon_struct *poly )
263 if( poly->tex_id != -1 ) {
264 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
266 render_set_context( poly->context, 0 );
267 glDisable( GL_DEPTH_TEST );
268 glDisable( GL_CULL_FACE );
269 glBlendFunc( GL_ONE, GL_ZERO );
270 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
271 glEnable( GL_CULL_FACE );
272 glEnable( GL_DEPTH_TEST );
275 void gl_render_tilelist( pvraddr_t tile_entry, GLint depth_mode )
277 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
279 struct polygon_struct *poly;
281 if( !IS_TILE_PTR(tile_entry) )
285 uint32_t entry = *tile_list++;
286 switch( entry >> 28 ) {
288 return; // End-of-list
290 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
292 case 0x08: case 0x09: case 0x0A: case 0x0B:
293 strip_count = ((entry >> 25) & 0x0F)+1;
294 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
295 while( strip_count > 0 ) {
296 assert( poly != NULL );
297 gl_render_poly( poly, depth_mode );
303 if( entry & 0x7E000000 ) {
304 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
305 gl_render_poly( poly, depth_mode );
312 * Render the tilelist with depthbuffer updates only.
314 void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
316 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
318 struct polygon_struct *poly;
320 if( !IS_TILE_PTR(tile_entry) )
323 glDisable( GL_TEXTURE_2D );
325 glDisable( GL_COLOR_SUM );
328 uint32_t entry = *tile_list++;
329 switch( entry >> 28 ) {
331 return; // End-of-list
333 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
335 case 0x08: case 0x09: case 0x0A: case 0x0B:
336 strip_count = ((entry >> 25) & 0x0F)+1;
337 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
338 while( strip_count > 0 ) {
339 render_set_base_context(poly->context[0],0);
340 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
346 if( entry & 0x7E000000 ) {
347 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
348 render_set_base_context(poly->context[0],0);
349 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
355 static void drawrect2d( uint32_t tile_bounds[], float z )
358 glVertex3f( tile_bounds[0], tile_bounds[2], z );
359 glVertex3f( tile_bounds[1], tile_bounds[2], z );
360 glVertex3f( tile_bounds[1], tile_bounds[3], z );
361 glVertex3f( tile_bounds[0], tile_bounds[3], z );
365 void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
367 /* A bit of explanation:
368 * In theory it works like this: generate a 1-bit stencil for each polygon
369 * volume, and then AND or OR it against the overall 1-bit tile stencil at
370 * the end of the volume.
372 * The implementation here uses a 2-bit stencil buffer, where each volume
373 * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
374 * to update bit 1 accordingly and clear bit 0.
376 * This could probably be more efficient, but at least it works correctly
380 render_set_cull(poly->context[0]);
381 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
383 int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
384 if( poly_type == PVR2_VOLUME_REGION0 ) {
390 glStencilMask( 0x03 );
391 glStencilFunc(GL_EQUAL, 0x02, 0x03);
392 glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
393 glDisable( GL_CULL_FACE );
394 glDisable( GL_DEPTH_TEST );
396 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
398 glEnable( GL_DEPTH_TEST );
399 glStencilMask( 0x01 );
400 glStencilFunc( GL_ALWAYS, 0, 1 );
401 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
402 } else if( poly_type == PVR2_VOLUME_REGION1 ) {
403 /* This is harder with the standard stencil ops - do it in two passes
404 * 00 => 00 | 00 => 10
405 * 01 => 10 | 01 => 10
406 * 10 => 10 | 10 => 00
407 * 11 => 10 | 11 => 10
409 glStencilMask( 0x02 );
410 glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
411 glDisable( GL_CULL_FACE );
412 glDisable( GL_DEPTH_TEST );
414 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
416 glStencilMask( 0x03 );
417 glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
418 glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
420 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
422 glEnable( GL_DEPTH_TEST );
423 glStencilMask( 0x01 );
424 glStencilFunc( GL_ALWAYS, 0, 1 );
425 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
429 void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
431 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
433 struct polygon_struct *poly;
435 if( !IS_TILE_PTR(tile_entry) )
438 glDisable( GL_TEXTURE_2D );
440 glDisable( GL_COLOR_SUM );
441 glDisable( GL_CULL_FACE );
442 glEnable( GL_STENCIL_TEST );
443 glEnable( GL_DEPTH_TEST );
444 glDepthFunc( GL_LEQUAL );
446 glStencilFunc( GL_ALWAYS, 0, 1 );
447 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
448 glStencilMask( 0x01 );
449 glDepthMask( GL_FALSE );
452 uint32_t entry = *tile_list++;
453 switch( entry >> 28 ) {
455 glDepthMask( GL_TRUE );
456 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
457 return; // End-of-list
459 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
461 case 0x08: case 0x09: case 0x0A: case 0x0B:
462 strip_count = ((entry >> 25) & 0x0F)+1;
463 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
464 while( strip_count > 0 ) {
465 gl_render_modifier_polygon( poly, tile_bounds );
471 if( entry & 0x7E000000 ) {
472 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
473 gl_render_modifier_polygon( poly, tile_bounds );
482 * Render the currently defined scene in pvr2_scene
484 void pvr2_scene_render( render_buffer_t buffer )
487 struct timeval start_tv, tex_tv, end_tv;
489 gettimeofday(&start_tv, NULL);
490 display_driver->set_render_target(buffer);
491 pvr2_check_palette_changed();
492 pvr2_scene_load_textures();
494 gettimeofday( &tex_tv, NULL );
495 uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
496 (tex_tv.tv_usec - start_tv.tv_usec)/1000;
497 DEBUG( "Scene setup in %dms", ms );
499 /* Setup view projection matrix */
500 glMatrixMode(GL_PROJECTION);
502 float nearz = pvr2_scene.bounds[4];
503 float farz = pvr2_scene.bounds[5];
504 if( nearz == farz ) {
507 glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0,
509 float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
510 glAlphaFunc( GL_GEQUAL, alphaRef );
512 /* Clear the buffer (FIXME: May not want always want to do this) */
513 glDisable( GL_SCISSOR_TEST );
514 glDepthMask( GL_TRUE );
515 glStencilMask( 0x03 );
516 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
518 /* Setup vertex array pointers */
519 glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
520 glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
521 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
522 glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
523 glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
524 glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
525 glFogi(GL_FOG_MODE, GL_LINEAR);
526 glFogf(GL_FOG_START, 0.0);
527 glFogf(GL_FOG_END, 1.0);
528 /* Turn on the shaders (if available) */
529 glsl_enable_shaders(TRUE);
531 /* Render the background */
532 gl_render_bkgnd( pvr2_scene.bkgnd_poly );
534 glEnable( GL_SCISSOR_TEST );
536 /* Process the segment list */
537 struct tile_segment *segment = pvr2_scene.segment_list;
539 int tilex = SEGMENT_X(segment->control);
540 int tiley = SEGMENT_Y(segment->control);
542 uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
543 if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
544 continue; // fully clipped, skip tile
547 /* Clip to the visible part of the tile */
548 glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3],
549 tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
550 if( display_driver->capabilities.stencil_bits >= 2 &&
551 IS_TILE_PTR(segment->opaquemod_ptr) &&
552 !IS_EMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
553 /* Don't do this unless there's actually some shadow polygons */
555 /* Use colormask instead of drawbuffer for simplicity */
556 glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
557 gl_render_tilelist_depthonly(segment->opaque_ptr);
558 gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
559 glClear( GL_DEPTH_BUFFER_BIT );
560 glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
562 gl_render_tilelist(segment->opaque_ptr,0);
563 if( IS_TILE_PTR(segment->punchout_ptr) ) {
564 glEnable(GL_ALPHA_TEST );
565 gl_render_tilelist(segment->punchout_ptr, GL_GEQUAL );
566 glDisable(GL_ALPHA_TEST );
568 glDisable( GL_STENCIL_TEST );
570 glClear( GL_STENCIL_BUFFER_BIT );
572 if( IS_TILE_PTR(segment->trans_ptr) ) {
573 if( pvr2_scene.sort_mode == SORT_NEVER ||
574 (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
575 gl_render_tilelist(segment->trans_ptr, 0);
577 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
580 } while( !IS_LAST_SEGMENT(segment++) );
581 glDisable( GL_SCISSOR_TEST );
583 glsl_enable_shaders(FALSE);
585 gettimeofday( &end_tv, NULL );
586 ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
587 (end_tv.tv_usec - tex_tv.tv_usec)/1000;
588 DEBUG( "Scene render in %dms", ms );
.