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);
153 void pvr2_shutdown_gl_context()
155 texcache_gl_shutdown();
156 pvr2_destroy_render_buffers();
160 * Setup the basic context that's shared between normal and modified modes -
163 static void render_set_base_context( uint32_t poly1, gboolean set_depth )
166 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
169 glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
173 * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
175 static void render_set_tsp_context( uint32_t poly1, uint32_t poly2 )
177 #ifdef HAVE_OPENGL_FIXEDFUNC
178 glShadeModel( POLY1_SHADE_MODEL(poly1) );
180 if( !have_shaders ) {
181 if( POLY1_TEXTURED(poly1) ) {
182 if( POLY2_TEX_BLEND(poly2) == 2 )
183 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
185 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
189 switch( POLY2_FOG_MODE(poly2) ) {
190 case PVR2_POLY_FOG_LOOKUP:
191 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
193 case PVR2_POLY_FOG_VERTEX:
194 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
200 int srcblend = POLY2_SRC_BLEND(poly2);
201 int destblend = POLY2_DEST_BLEND(poly2);
202 glBlendFunc( srcblend, destblend );
204 if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
205 WARN( "Accumulation buffer not supported" );
210 * Setup the GL context for the supplied polygon context.
211 * @param context pointer to 3 or 5 words of polygon context
212 * @param depth_mode force depth mode, or 0 to use the polygon's
215 static void render_set_context( uint32_t *context, gboolean set_depth )
217 render_set_base_context(context[0], set_depth);
218 render_set_tsp_context(context[0],context[1]);
221 static inline void gl_draw_vertexes( struct polygon_struct *poly )
224 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
225 poly = poly->sub_next;
226 } while( poly != NULL );
229 static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
232 glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
233 poly = poly->sub_next;
234 } while( poly != NULL );
237 static void gl_render_poly( struct polygon_struct *poly, gboolean set_depth)
239 if( poly->vertex_count == 0 )
242 bind_texture(poly->tex_id);
243 if( poly->mod_vertex_index == -1 ) {
244 render_set_context( poly->context, set_depth );
245 gl_draw_vertexes(poly);
247 glEnable( GL_STENCIL_TEST );
248 render_set_base_context( poly->context[0], set_depth );
249 render_set_tsp_context( poly->context[0], poly->context[1] );
250 glStencilFunc(GL_EQUAL, 0, 2);
251 gl_draw_vertexes(poly);
253 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
254 bind_texture(poly->mod_tex_id);
255 render_set_tsp_context( poly->context[0], poly->context[3] );
257 glStencilFunc(GL_EQUAL, 2, 2);
258 gl_draw_mod_vertexes(poly);
259 glDisable( GL_STENCIL_TEST );
264 static void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
266 /* A bit of explanation:
267 * In theory it works like this: generate a 1-bit stencil for each polygon
268 * volume, and then AND or OR it against the overall 1-bit tile stencil at
269 * the end of the volume.
271 * The implementation here uses a 2-bit stencil buffer, where each volume
272 * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
273 * to update bit 1 accordingly and clear bit 0.
275 * This could probably be more efficient, but at least it works correctly
279 if( poly->vertex_count == 0 )
282 gl_draw_vertexes(poly);
286 int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
287 if( poly_type == PVR2_VOLUME_REGION0 ) {
293 glStencilMask( 0x03 );
294 glStencilFunc(GL_EQUAL, 0x02, 0x03);
295 glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
296 glDisable( GL_DEPTH_TEST );
298 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
300 glEnable( GL_DEPTH_TEST );
301 glStencilMask( 0x01 );
302 glStencilFunc( GL_ALWAYS, 0, 1 );
303 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
304 } else if( poly_type == PVR2_VOLUME_REGION1 ) {
305 /* This is harder with the standard stencil ops - do it in two passes
306 * 00 => 00 | 00 => 10
307 * 01 => 10 | 01 => 10
308 * 10 => 10 | 10 => 00
309 * 11 => 10 | 11 => 10
311 glStencilMask( 0x02 );
312 glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
313 glDisable( GL_DEPTH_TEST );
315 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
317 glStencilMask( 0x03 );
318 glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
319 glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
321 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
323 glEnable( GL_DEPTH_TEST );
324 glStencilMask( 0x01 );
325 glStencilFunc( GL_ALWAYS, 0, 1 );
326 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
330 static void gl_render_bkgnd( struct polygon_struct *poly )
332 bind_texture(poly->tex_id);
333 render_set_tsp_context( poly->context[0], poly->context[1] );
334 glDisable( GL_DEPTH_TEST );
335 glBlendFunc( GL_ONE, GL_ZERO );
336 gl_draw_vertexes(poly);
337 glEnable( GL_DEPTH_TEST );
340 void gl_render_triangle( struct polygon_struct *poly, int index )
342 bind_texture(poly->tex_id);
343 render_set_tsp_context( poly->context[0], poly->context[1] );
344 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index + index, 3 );
348 void gl_render_tilelist( pvraddr_t tile_entry, gboolean set_depth )
352 FOREACH_TILEENTRY(list, tile_entry) {
353 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
356 gl_render_poly(poly, set_depth);
358 } while( list.strip_count-- > 0 );
364 * Render the tilelist with depthbuffer updates only.
366 static void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
370 FOREACH_TILEENTRY(list, tile_entry) {
371 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
374 render_set_base_context(poly->context[0],TRUE);
375 gl_draw_vertexes(poly);
377 } while( list.strip_count-- > 0 );
382 static void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
386 FOREACH_TILEENTRY(list, tile_entry ) {
387 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
390 gl_render_modifier_polygon( poly, tile_bounds );
392 } while( list.strip_count-- > 0 );
398 #ifdef HAVE_OPENGL_FIXEDFUNC
399 void pvr2_scene_setup_fixed( GLfloat *viewMatrix )
401 glLoadMatrixf(viewMatrix);
402 glEnable( GL_DEPTH_TEST );
405 glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
406 glFogi(GL_FOG_MODE, GL_LINEAR);
407 glFogf(GL_FOG_START, 0.0);
408 glFogf(GL_FOG_END, 1.0);
410 glEnable( GL_ALPHA_TEST );
411 glAlphaFunc( GL_GEQUAL, 0 );
413 glEnable( GL_COLOR_SUM );
415 glEnableClientState( GL_VERTEX_ARRAY );
416 glEnableClientState( GL_COLOR_ARRAY );
417 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
418 glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
419 glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
421 /* Vertex array pointers */
422 glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
423 glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
424 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
425 glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
426 glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
429 void pvr2_scene_set_alpha_fixed( float alphaRef )
431 glAlphaFunc( GL_GEQUAL, alphaRef );
434 void pvr2_scene_cleanup_fixed()
436 glDisable( GL_COLOR_SUM );
438 glDisable( GL_ALPHA_TEST );
439 glDisable( GL_DEPTH_TEST );
441 glDisableClientState( GL_VERTEX_ARRAY );
442 glDisableClientState( GL_COLOR_ARRAY );
443 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
444 glDisableClientState( GL_SECONDARY_COLOR_ARRAY );
445 glDisableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
449 void pvr2_scene_setup_fixed( GLfloat *viewMatrix )
452 void pvr2_scene_set_alpha_fixed( float alphaRef )
455 void pvr2_scene_cleanup_fixed()
460 void pvr2_scene_setup_shader( GLfloat *viewMatrix )
462 glEnable( GL_DEPTH_TEST );
464 glsl_use_pvr2_shader();
465 glsl_set_pvr2_shader_view_matrix(viewMatrix);
466 glsl_set_pvr2_shader_fog_colour1(pvr2_scene.fog_vert_colour);
467 glsl_set_pvr2_shader_fog_colour2(pvr2_scene.fog_lut_colour);
468 glsl_set_pvr2_shader_in_vertex_vec3_pointer(&pvr2_scene.vertex_array[0].x, sizeof(struct vertex_struct));
469 glsl_set_pvr2_shader_in_colour_pointer(&pvr2_scene.vertex_array[0].rgba[0], sizeof(struct vertex_struct));
470 glsl_set_pvr2_shader_in_colour2_pointer(&pvr2_scene.vertex_array[0].offset_rgba[0], sizeof(struct vertex_struct));
471 glsl_set_pvr2_shader_in_texcoord_pointer(&pvr2_scene.vertex_array[0].u, sizeof(struct vertex_struct));
472 glsl_set_pvr2_shader_alpha_ref(0.0);
473 glsl_set_pvr2_shader_primary_texture(0);
474 glsl_set_pvr2_shader_palette_texture(1);
477 void pvr2_scene_cleanup_shader( )
481 glDisable( GL_DEPTH_TEST );
484 void pvr2_scene_set_alpha_shader( float alphaRef )
486 glsl_set_pvr2_shader_alpha_ref(alphaRef);
490 * Render the currently defined scene in pvr2_scene
492 void pvr2_scene_render( render_buffer_t buffer )
495 struct timeval start_tv, tex_tv, end_tv;
497 GLfloat viewMatrix[16];
498 uint32_t clip_bounds[4];
501 gettimeofday(&start_tv, NULL);
502 display_driver->set_render_target(buffer);
503 pvr2_check_palette_changed();
504 pvr2_scene_load_textures();
507 gettimeofday( &tex_tv, NULL );
508 uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
509 (tex_tv.tv_usec - start_tv.tv_usec)/1000;
510 DEBUG( "Texture load in %dms", ms );
512 float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
513 float nearz = pvr2_scene.bounds[4];
514 float farz = pvr2_scene.bounds[5];
515 if( nearz == farz ) {
519 /* Generate integer clip boundaries */
520 for( i=0; i<4; i++ ) {
521 clip_bounds[i] = (uint32_t)pvr2_scene.bounds[i];
524 defineOrthoMatrix(viewMatrix, pvr2_scene.buffer_width, pvr2_scene.buffer_height, -farz, -nearz);
527 pvr2_scene_setup_shader(viewMatrix);
529 pvr2_scene_setup_fixed(viewMatrix);
533 /* Clear the buffer (FIXME: May not want always want to do this) */
534 glDisable( GL_SCISSOR_TEST );
535 glDepthMask( GL_TRUE );
536 glStencilMask( 0x03 );
537 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
539 /* Render the background */
540 gl_render_bkgnd( pvr2_scene.bkgnd_poly );
542 glEnable( GL_SCISSOR_TEST );
543 glEnable( GL_TEXTURE_2D );
545 struct tile_segment *segment;
547 #define FOREACH_SEGMENT(segment) \
548 segment = pvr2_scene.segment_list; \
550 int tilex = SEGMENT_X(segment->control); \
551 int tiley = SEGMENT_Y(segment->control); \
553 uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 }; \
554 if( !clip_tile_bounds(tile_bounds, clip_bounds) ) { \
557 #define END_FOREACH_SEGMENT() \
558 } while( !IS_LAST_SEGMENT(segment++) );
559 #define CLIP_TO_SEGMENT() \
560 glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] )
562 /* Build up the opaque stencil map */
563 if( display_driver->capabilities.stencil_bits >= 2 ) {
564 glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
565 FOREACH_SEGMENT(segment)
566 if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
568 gl_render_tilelist_depthonly(segment->opaque_ptr);
570 END_FOREACH_SEGMENT()
572 glEnable( GL_STENCIL_TEST );
573 glStencilFunc( GL_ALWAYS, 0, 1 );
574 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
575 glStencilMask( 0x01 );
576 glDepthFunc( GL_LEQUAL );
577 glDepthMask( GL_FALSE );
578 FOREACH_SEGMENT(segment)
579 if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
581 gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
583 END_FOREACH_SEGMENT()
584 glDepthMask( GL_TRUE );
585 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
586 glDisable( GL_SCISSOR_TEST );
587 glClear( GL_DEPTH_BUFFER_BIT );
588 glEnable( GL_SCISSOR_TEST );
589 glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
592 /* Render the opaque polygons */
593 FOREACH_SEGMENT(segment)
595 gl_render_tilelist(segment->opaque_ptr,TRUE);
596 END_FOREACH_SEGMENT()
597 glDisable( GL_STENCIL_TEST );
599 /* Render the punch-out polygons */
601 pvr2_scene_set_alpha_shader(alphaRef);
603 pvr2_scene_set_alpha_fixed(alphaRef);
604 glDepthFunc(GL_GEQUAL);
605 FOREACH_SEGMENT(segment)
607 gl_render_tilelist(segment->punchout_ptr, FALSE );
608 END_FOREACH_SEGMENT()
610 pvr2_scene_set_alpha_shader(0.0);
612 pvr2_scene_set_alpha_fixed(0.0);
614 /* Render the translucent polygons */
615 FOREACH_SEGMENT(segment)
616 if( IS_NONEMPTY_TILE_LIST(segment->trans_ptr) ) {
618 if( pvr2_scene.sort_mode == SORT_NEVER ||
619 (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
620 gl_render_tilelist(segment->trans_ptr, TRUE);
622 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
625 END_FOREACH_SEGMENT()
627 glDisable( GL_SCISSOR_TEST );
630 pvr2_scene_cleanup_shader();
632 pvr2_scene_cleanup_fixed();
635 pvr2_scene_finished();
637 gettimeofday( &end_tv, NULL );
638 ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
639 (end_tv.tv_usec - tex_tv.tv_usec)/1000;
640 DEBUG( "Scene render in %dms", ms );
.