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/glutil.h"
25 #include "pvr2/scene.h"
26 #include "pvr2/tileiter.h"
27 #include "pvr2/shaders.h"
30 #include "OpenGL/CGLCurrent.h"
31 #include "OpenGL/CGLMacro.h"
33 static CGLContextObj CGL_MACRO_CONTEXT;
36 #define IS_NONEMPTY_TILE_LIST(p) (IS_TILE_PTR(p) && ((*((uint32_t *)(pvr2_main_ram+(p))) >> 28) != 0x0F))
38 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
39 GL_GREATER, GL_NOTEQUAL, GL_GEQUAL,
41 int pvr2_poly_srcblend[8] = {
42 GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
43 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
44 GL_ONE_MINUS_DST_ALPHA };
45 int pvr2_poly_dstblend[8] = {
46 GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
47 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
48 GL_ONE_MINUS_DST_ALPHA };
50 static gboolean have_shaders = FALSE;
51 static int currentTexId = -1;
53 static inline void bind_texture(int texid)
55 if( currentTexId != texid ) {
57 glBindTexture(GL_TEXTURE_2D, texid);
62 * Clip the tile bounds to the clipping plane.
63 * @return TRUE if the tile was not clipped completely.
65 static gboolean clip_tile_bounds( uint32_t *tile, uint32_t *clip )
67 if( tile[0] < clip[0] ) tile[0] = clip[0];
68 if( tile[1] > clip[1] ) tile[1] = clip[1];
69 if( tile[2] < clip[2] ) tile[2] = clip[2];
70 if( tile[3] > clip[3] ) tile[3] = clip[3];
71 return tile[0] < tile[1] && tile[2] < tile[3];
74 static void drawrect2d( uint32_t tile_bounds[], float z )
76 /* FIXME: Find a non-fixed-func way to do this */
77 #ifdef HAVE_OPENGL_FIXEDFUNC
78 glBegin( GL_TRIANGLE_STRIP );
79 glVertex3f( tile_bounds[0], tile_bounds[2], z );
80 glVertex3f( tile_bounds[1], tile_bounds[2], z );
81 glVertex3f( tile_bounds[0], tile_bounds[3], z );
82 glVertex3f( tile_bounds[1], tile_bounds[3], z );
87 static void pvr2_scene_load_textures()
91 texcache_begin_scene( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03,
92 (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5 );
94 for( i=0; i < pvr2_scene.poly_count; i++ ) {
95 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
96 if( POLY1_TEXTURED(poly->context[0]) ) {
97 poly->tex_id = texcache_get_texture( poly->context[1], poly->context[2] );
98 if( poly->mod_vertex_index != -1 ) {
99 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
100 poly->mod_tex_id = texcache_get_texture( poly->context[3], poly->context[4] );
102 poly->mod_tex_id = poly->tex_id;
107 poly->mod_tex_id = 0;
114 * Once-off call to setup the OpenGL context.
116 void pvr2_setup_gl_context()
118 have_shaders = display_driver->capabilities.has_sl;
120 CGL_MACRO_CONTEXT = CGLGetCurrentContext();
122 texcache_gl_init(); // Allocate texture IDs
124 /* Global settings */
125 glDisable( GL_CULL_FACE );
126 glEnable( GL_BLEND );
127 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
129 #ifdef HAVE_OPENGL_CLAMP_COLOR
130 if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
131 glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
132 glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
136 #ifdef HAVE_OPENGL_FIXEDFUNC
137 /* Setup defaults for perspective correction + matrices */
138 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
139 glMatrixMode(GL_MODELVIEW);
141 glMatrixMode(GL_PROJECTION);
145 #ifdef HAVE_OPENGL_CLEAR_DEPTHF
153 void pvr2_shutdown_gl_context()
155 texcache_gl_shutdown();
159 * Setup the basic context that's shared between normal and modified modes -
162 static void render_set_base_context( uint32_t poly1, gboolean set_depth )
165 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
168 glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
172 * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
174 static void render_set_tsp_context( uint32_t poly1, uint32_t poly2 )
176 #ifdef HAVE_OPENGL_FIXEDFUNC
177 glShadeModel( POLY1_SHADE_MODEL(poly1) );
179 if( !have_shaders ) {
180 if( POLY1_TEXTURED(poly1) ) {
181 if( POLY2_TEX_BLEND(poly2) == 2 )
182 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
184 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
188 switch( POLY2_FOG_MODE(poly2) ) {
189 case PVR2_POLY_FOG_LOOKUP:
190 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
192 case PVR2_POLY_FOG_VERTEX:
193 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
199 int srcblend = POLY2_SRC_BLEND(poly2);
200 int destblend = POLY2_DEST_BLEND(poly2);
201 glBlendFunc( srcblend, destblend );
203 if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
204 WARN( "Accumulation buffer not supported" );
209 * Setup the GL context for the supplied polygon context.
210 * @param context pointer to 3 or 5 words of polygon context
211 * @param depth_mode force depth mode, or 0 to use the polygon's
214 static void render_set_context( uint32_t *context, gboolean set_depth )
216 render_set_base_context(context[0], set_depth);
217 render_set_tsp_context(context[0],context[1]);
220 static inline void gl_draw_vertexes( struct polygon_struct *poly )
223 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
224 poly = poly->sub_next;
225 } while( poly != NULL );
228 static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
231 glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
232 poly = poly->sub_next;
233 } while( poly != NULL );
236 static void gl_render_poly( struct polygon_struct *poly, gboolean set_depth)
238 if( poly->vertex_count == 0 )
241 bind_texture(poly->tex_id);
242 if( poly->mod_vertex_index == -1 ) {
243 render_set_context( poly->context, set_depth );
244 gl_draw_vertexes(poly);
246 glEnable( GL_STENCIL_TEST );
247 render_set_base_context( poly->context[0], set_depth );
248 render_set_tsp_context( poly->context[0], poly->context[1] );
249 glStencilFunc(GL_EQUAL, 0, 2);
250 gl_draw_vertexes(poly);
252 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
253 bind_texture(poly->mod_tex_id);
254 render_set_tsp_context( poly->context[0], poly->context[3] );
256 glStencilFunc(GL_EQUAL, 2, 2);
257 gl_draw_mod_vertexes(poly);
258 glDisable( GL_STENCIL_TEST );
263 static void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
265 /* A bit of explanation:
266 * In theory it works like this: generate a 1-bit stencil for each polygon
267 * volume, and then AND or OR it against the overall 1-bit tile stencil at
268 * the end of the volume.
270 * The implementation here uses a 2-bit stencil buffer, where each volume
271 * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
272 * to update bit 1 accordingly and clear bit 0.
274 * This could probably be more efficient, but at least it works correctly
278 if( poly->vertex_count == 0 )
281 gl_draw_vertexes(poly);
285 int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
286 if( poly_type == PVR2_VOLUME_REGION0 ) {
292 glStencilMask( 0x03 );
293 glStencilFunc(GL_EQUAL, 0x02, 0x03);
294 glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
295 glDisable( GL_DEPTH_TEST );
297 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
299 glEnable( GL_DEPTH_TEST );
300 glStencilMask( 0x01 );
301 glStencilFunc( GL_ALWAYS, 0, 1 );
302 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
303 } else if( poly_type == PVR2_VOLUME_REGION1 ) {
304 /* This is harder with the standard stencil ops - do it in two passes
305 * 00 => 00 | 00 => 10
306 * 01 => 10 | 01 => 10
307 * 10 => 10 | 10 => 00
308 * 11 => 10 | 11 => 10
310 glStencilMask( 0x02 );
311 glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
312 glDisable( GL_DEPTH_TEST );
314 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
316 glStencilMask( 0x03 );
317 glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
318 glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
320 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
322 glEnable( GL_DEPTH_TEST );
323 glStencilMask( 0x01 );
324 glStencilFunc( GL_ALWAYS, 0, 1 );
325 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
329 static void gl_render_bkgnd( struct polygon_struct *poly )
331 bind_texture(poly->tex_id);
332 render_set_tsp_context( poly->context[0], poly->context[1] );
333 glDisable( GL_DEPTH_TEST );
334 glBlendFunc( GL_ONE, GL_ZERO );
335 gl_draw_vertexes(poly);
336 glEnable( GL_DEPTH_TEST );
339 void gl_render_triangle( struct polygon_struct *poly, int index )
341 bind_texture(poly->tex_id);
342 render_set_tsp_context( poly->context[0], poly->context[1] );
343 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index + index, 3 );
347 void gl_render_tilelist( pvraddr_t tile_entry, gboolean set_depth )
351 FOREACH_TILEENTRY(list, tile_entry) {
352 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
355 gl_render_poly(poly, set_depth);
357 } while( list.strip_count-- > 0 );
363 * Render the tilelist with depthbuffer updates only.
365 static void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
369 FOREACH_TILEENTRY(list, tile_entry) {
370 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
373 render_set_base_context(poly->context[0],TRUE);
374 gl_draw_vertexes(poly);
376 } while( list.strip_count-- > 0 );
381 static void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
385 FOREACH_TILEENTRY(list, tile_entry ) {
386 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
389 gl_render_modifier_polygon( poly, tile_bounds );
391 } while( list.strip_count-- > 0 );
397 #ifdef HAVE_OPENGL_FIXEDFUNC
398 void pvr2_scene_setup_fixed( GLfloat *viewMatrix )
400 glLoadMatrixf(viewMatrix);
401 glEnable( GL_DEPTH_TEST );
404 glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
405 glFogi(GL_FOG_MODE, GL_LINEAR);
406 glFogf(GL_FOG_START, 0.0);
407 glFogf(GL_FOG_END, 1.0);
409 glEnable( GL_ALPHA_TEST );
410 glAlphaFunc( GL_GEQUAL, 0 );
412 glEnable( GL_COLOR_SUM );
414 glEnableClientState( GL_VERTEX_ARRAY );
415 glEnableClientState( GL_COLOR_ARRAY );
416 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
417 glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
418 glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
420 /* Vertex array pointers */
421 glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
422 glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
423 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
424 glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
425 glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
428 void pvr2_scene_set_alpha_fixed( float alphaRef )
430 glAlphaFunc( GL_GEQUAL, alphaRef );
433 void pvr2_scene_cleanup_fixed()
435 glDisable( GL_COLOR_SUM );
437 glDisable( GL_ALPHA_TEST );
438 glDisable( GL_DEPTH_TEST );
440 glDisableClientState( GL_VERTEX_ARRAY );
441 glDisableClientState( GL_COLOR_ARRAY );
442 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
443 glDisableClientState( GL_SECONDARY_COLOR_ARRAY );
444 glDisableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
448 void pvr2_scene_setup_fixed( GLfloat *viewMatrix )
451 void pvr2_scene_set_alpha_fixed( float alphaRef )
454 void pvr2_scene_cleanup_fixed()
459 void pvr2_scene_setup_shader( GLfloat *viewMatrix )
461 glEnable( GL_DEPTH_TEST );
463 glsl_use_pvr2_shader();
464 glsl_set_pvr2_shader_view_matrix(viewMatrix);
465 glsl_set_pvr2_shader_fog_colour1(pvr2_scene.fog_vert_colour);
466 glsl_set_pvr2_shader_fog_colour2(pvr2_scene.fog_lut_colour);
467 glsl_set_pvr2_shader_in_vertex_vec3_pointer(&pvr2_scene.vertex_array[0].x, sizeof(struct vertex_struct));
468 glsl_set_pvr2_shader_in_colour_pointer(&pvr2_scene.vertex_array[0].rgba[0], sizeof(struct vertex_struct));
469 glsl_set_pvr2_shader_in_colour2_pointer(&pvr2_scene.vertex_array[0].offset_rgba[0], sizeof(struct vertex_struct));
470 glsl_set_pvr2_shader_in_texcoord_pointer(&pvr2_scene.vertex_array[0].u, sizeof(struct vertex_struct));
471 glsl_set_pvr2_shader_alpha_ref(0.0);
472 glsl_set_pvr2_shader_primary_texture(0);
473 glsl_set_pvr2_shader_palette_texture(1);
476 void pvr2_scene_cleanup_shader( )
480 glDisable( GL_DEPTH_TEST );
483 void pvr2_scene_set_alpha_shader( float alphaRef )
485 glsl_set_pvr2_shader_alpha_ref(alphaRef);
489 * Render the currently defined scene in pvr2_scene
491 void pvr2_scene_render( render_buffer_t buffer )
494 struct timeval start_tv, tex_tv, end_tv;
496 GLfloat viewMatrix[16];
497 uint32_t clip_bounds[4];
500 gettimeofday(&start_tv, NULL);
501 display_driver->set_render_target(buffer);
502 pvr2_check_palette_changed();
503 pvr2_scene_load_textures();
506 gettimeofday( &tex_tv, NULL );
507 uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
508 (tex_tv.tv_usec - start_tv.tv_usec)/1000;
509 DEBUG( "Texture load in %dms", ms );
511 float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
512 float nearz = pvr2_scene.bounds[4];
513 float farz = pvr2_scene.bounds[5];
514 if( nearz == farz ) {
518 /* Generate integer clip boundaries */
519 for( i=0; i<4; i++ ) {
520 clip_bounds[i] = (uint32_t)pvr2_scene.bounds[i];
523 defineOrthoMatrix(viewMatrix, pvr2_scene.buffer_width, pvr2_scene.buffer_height, -farz, -nearz);
526 pvr2_scene_setup_shader(viewMatrix);
528 pvr2_scene_setup_fixed(viewMatrix);
532 /* Clear the buffer (FIXME: May not want always want to do this) */
533 glDisable( GL_SCISSOR_TEST );
534 glDepthMask( GL_TRUE );
535 glStencilMask( 0x03 );
536 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
538 /* Render the background */
539 gl_render_bkgnd( pvr2_scene.bkgnd_poly );
541 glEnable( GL_SCISSOR_TEST );
542 glEnable( GL_TEXTURE_2D );
544 struct tile_segment *segment;
546 #define FOREACH_SEGMENT(segment) \
547 segment = pvr2_scene.segment_list; \
549 int tilex = SEGMENT_X(segment->control); \
550 int tiley = SEGMENT_Y(segment->control); \
552 uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 }; \
553 if( !clip_tile_bounds(tile_bounds, clip_bounds) ) { \
556 #define END_FOREACH_SEGMENT() \
557 } while( !IS_LAST_SEGMENT(segment++) );
558 #define CLIP_TO_SEGMENT() \
559 glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] )
561 /* Build up the opaque stencil map */
562 if( display_driver->capabilities.stencil_bits >= 2 ) {
563 glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
564 FOREACH_SEGMENT(segment)
565 if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
567 gl_render_tilelist_depthonly(segment->opaque_ptr);
569 END_FOREACH_SEGMENT()
571 glEnable( GL_STENCIL_TEST );
572 glStencilFunc( GL_ALWAYS, 0, 1 );
573 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
574 glStencilMask( 0x01 );
575 glDepthFunc( GL_LEQUAL );
576 glDepthMask( GL_FALSE );
577 FOREACH_SEGMENT(segment)
578 if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
580 gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
582 END_FOREACH_SEGMENT()
583 glDepthMask( GL_TRUE );
584 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
585 glDisable( GL_SCISSOR_TEST );
586 glClear( GL_DEPTH_BUFFER_BIT );
587 glEnable( GL_SCISSOR_TEST );
588 glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
591 /* Render the opaque polygons */
592 FOREACH_SEGMENT(segment)
594 gl_render_tilelist(segment->opaque_ptr,TRUE);
595 END_FOREACH_SEGMENT()
596 glDisable( GL_STENCIL_TEST );
598 /* Render the punch-out polygons */
600 pvr2_scene_set_alpha_shader(alphaRef);
602 pvr2_scene_set_alpha_fixed(alphaRef);
603 glDepthFunc(GL_GEQUAL);
604 FOREACH_SEGMENT(segment)
606 gl_render_tilelist(segment->punchout_ptr, FALSE );
607 END_FOREACH_SEGMENT()
609 pvr2_scene_set_alpha_shader(0.0);
611 pvr2_scene_set_alpha_fixed(0.0);
613 /* Render the translucent polygons */
614 FOREACH_SEGMENT(segment)
615 if( IS_NONEMPTY_TILE_LIST(segment->trans_ptr) ) {
617 if( pvr2_scene.sort_mode == SORT_NEVER ||
618 (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
619 gl_render_tilelist(segment->trans_ptr, TRUE);
621 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
624 END_FOREACH_SEGMENT()
626 glDisable( GL_SCISSOR_TEST );
629 pvr2_scene_cleanup_shader();
631 pvr2_scene_cleanup_fixed();
634 pvr2_scene_finished();
636 gettimeofday( &end_tv, NULL );
637 ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
638 (end_tv.tv_usec - tex_tv.tv_usec)/1000;
639 DEBUG( "Scene render in %dms", ms );
.