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 glBegin( GL_TRIANGLE_STRIP );
77 glVertex3f( tile_bounds[0], tile_bounds[2], z );
78 glVertex3f( tile_bounds[1], tile_bounds[2], z );
79 glVertex3f( tile_bounds[0], tile_bounds[3], z );
80 glVertex3f( tile_bounds[1], tile_bounds[3], z );
84 static void pvr2_scene_load_textures()
88 texcache_begin_scene( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03,
89 (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5 );
91 for( i=0; i < pvr2_scene.poly_count; i++ ) {
92 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
93 if( POLY1_TEXTURED(poly->context[0]) ) {
94 poly->tex_id = texcache_get_texture( poly->context[1], poly->context[2] );
95 if( poly->mod_vertex_index != -1 ) {
96 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
97 poly->mod_tex_id = texcache_get_texture( poly->context[3], poly->context[4] );
99 poly->mod_tex_id = poly->tex_id;
104 poly->mod_tex_id = 0;
111 * Once-off call to setup the OpenGL context.
113 void pvr2_setup_gl_context()
115 if( glsl_is_supported() && isGLMultitextureSupported() ) {
116 if( !glsl_load_shaders( ) ) {
117 WARN( "Unable to load GL shaders" );
124 CGL_MACRO_CONTEXT = CGLGetCurrentContext();
126 texcache_gl_init(have_shaders); // Allocate texture IDs
128 /* Global settings */
129 glDisable( GL_CULL_FACE );
130 glEnable( GL_BLEND );
131 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
133 #ifdef HAVE_OPENGL_CLAMP_COLOR
134 if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
135 glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
136 glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
140 #ifdef HAVE_OPENGL_FIXEDFUNC
141 /* Setup defaults for perspective correction + matrices */
142 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
143 glMatrixMode(GL_MODELVIEW);
145 glMatrixMode(GL_PROJECTION);
149 #ifdef HAVE_OPENGL_CLEAR_DEPTHF
158 * Setup the basic context that's shared between normal and modified modes -
161 static void render_set_base_context( uint32_t poly1, gboolean set_depth )
164 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
167 glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
171 * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
173 static void render_set_tsp_context( uint32_t poly1, uint32_t poly2 )
175 glShadeModel( POLY1_SHADE_MODEL(poly1) );
177 #ifdef HAVE_OPENGL_FIXEDFUNC
178 if( !have_shaders ) {
179 if( POLY1_TEXTURED(poly1) ) {
180 if( POLY2_TEX_BLEND(poly2) == 2 )
181 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
183 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
187 switch( POLY2_FOG_MODE(poly2) ) {
188 case PVR2_POLY_FOG_LOOKUP:
189 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
191 case PVR2_POLY_FOG_VERTEX:
192 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
198 int srcblend = POLY2_SRC_BLEND(poly2);
199 int destblend = POLY2_DEST_BLEND(poly2);
200 glBlendFunc( srcblend, destblend );
202 if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
203 WARN( "Accumulation buffer not supported" );
208 * Setup the GL context for the supplied polygon context.
209 * @param context pointer to 3 or 5 words of polygon context
210 * @param depth_mode force depth mode, or 0 to use the polygon's
213 static void render_set_context( uint32_t *context, gboolean set_depth )
215 render_set_base_context(context[0], set_depth);
216 render_set_tsp_context(context[0],context[1]);
219 static inline void gl_draw_vertexes( struct polygon_struct *poly )
222 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
223 poly = poly->sub_next;
224 } while( poly != NULL );
227 static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
230 glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
231 poly = poly->sub_next;
232 } while( poly != NULL );
235 static void gl_render_poly( struct polygon_struct *poly, gboolean set_depth)
237 if( poly->vertex_count == 0 )
240 bind_texture(poly->tex_id);
241 if( poly->mod_vertex_index == -1 ) {
242 render_set_context( poly->context, set_depth );
243 gl_draw_vertexes(poly);
245 glEnable( GL_STENCIL_TEST );
246 render_set_base_context( poly->context[0], set_depth );
247 render_set_tsp_context( poly->context[0], poly->context[1] );
248 glStencilFunc(GL_EQUAL, 0, 2);
249 gl_draw_vertexes(poly);
251 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
252 bind_texture(poly->mod_tex_id);
253 render_set_tsp_context( poly->context[0], poly->context[3] );
255 glStencilFunc(GL_EQUAL, 2, 2);
256 gl_draw_mod_vertexes(poly);
257 glDisable( GL_STENCIL_TEST );
262 static void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
264 /* A bit of explanation:
265 * In theory it works like this: generate a 1-bit stencil for each polygon
266 * volume, and then AND or OR it against the overall 1-bit tile stencil at
267 * the end of the volume.
269 * The implementation here uses a 2-bit stencil buffer, where each volume
270 * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
271 * to update bit 1 accordingly and clear bit 0.
273 * This could probably be more efficient, but at least it works correctly
277 if( poly->vertex_count == 0 )
280 gl_draw_vertexes(poly);
284 int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
285 if( poly_type == PVR2_VOLUME_REGION0 ) {
291 glStencilMask( 0x03 );
292 glStencilFunc(GL_EQUAL, 0x02, 0x03);
293 glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
294 glDisable( GL_DEPTH_TEST );
296 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
298 glEnable( GL_DEPTH_TEST );
299 glStencilMask( 0x01 );
300 glStencilFunc( GL_ALWAYS, 0, 1 );
301 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
302 } else if( poly_type == PVR2_VOLUME_REGION1 ) {
303 /* This is harder with the standard stencil ops - do it in two passes
304 * 00 => 00 | 00 => 10
305 * 01 => 10 | 01 => 10
306 * 10 => 10 | 10 => 00
307 * 11 => 10 | 11 => 10
309 glStencilMask( 0x02 );
310 glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
311 glDisable( GL_DEPTH_TEST );
313 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
315 glStencilMask( 0x03 );
316 glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
317 glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
319 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
321 glEnable( GL_DEPTH_TEST );
322 glStencilMask( 0x01 );
323 glStencilFunc( GL_ALWAYS, 0, 1 );
324 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
328 static void gl_render_bkgnd( struct polygon_struct *poly )
330 bind_texture(poly->tex_id);
331 render_set_tsp_context( poly->context[0], poly->context[1] );
332 glDisable( GL_DEPTH_TEST );
333 glBlendFunc( GL_ONE, GL_ZERO );
334 gl_draw_vertexes(poly);
335 glEnable( GL_DEPTH_TEST );
338 void gl_render_triangle( struct polygon_struct *poly, int index )
340 bind_texture(poly->tex_id);
341 render_set_tsp_context( poly->context[0], poly->context[1] );
342 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index + index, 3 );
346 void gl_render_tilelist( pvraddr_t tile_entry, gboolean set_depth )
350 FOREACH_TILEENTRY(list, tile_entry) {
351 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
354 gl_render_poly(poly, set_depth);
356 } while( list.strip_count-- > 0 );
362 * Render the tilelist with depthbuffer updates only.
364 static void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
368 FOREACH_TILEENTRY(list, tile_entry) {
369 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
372 render_set_base_context(poly->context[0],TRUE);
373 gl_draw_vertexes(poly);
375 } while( list.strip_count-- > 0 );
380 static void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
384 FOREACH_TILEENTRY(list, tile_entry ) {
385 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
388 gl_render_modifier_polygon( poly, tile_bounds );
390 } while( list.strip_count-- > 0 );
396 #ifdef HAVE_OPENGL_FIXEDFUNC
397 void pvr2_scene_setup_fixed( GLfloat *viewMatrix )
399 glLoadMatrixf(viewMatrix);
400 glEnable( GL_DEPTH_TEST );
403 glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
404 glFogi(GL_FOG_MODE, GL_LINEAR);
405 glFogf(GL_FOG_START, 0.0);
406 glFogf(GL_FOG_END, 1.0);
408 glEnable( GL_ALPHA_TEST );
409 glAlphaFunc( GL_GEQUAL, 0 );
411 glEnable( GL_COLOR_SUM );
413 glEnableClientState( GL_VERTEX_ARRAY );
414 glEnableClientState( GL_COLOR_ARRAY );
415 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
416 glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
417 glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
419 /* Vertex array pointers */
420 glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
421 glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
422 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
423 glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
424 glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
427 void pvr2_scene_set_alpha_fixed( float alphaRef )
429 glAlphaFunc( GL_GEQUAL, alphaRef );
432 void pvr2_scene_cleanup_fixed()
434 glDisable( GL_COLOR_SUM );
436 glDisable( GL_ALPHA_TEST );
437 glDisable( GL_DEPTH_TEST );
439 glDisableClientState( GL_VERTEX_ARRAY );
440 glDisableClientState( GL_COLOR_ARRAY );
441 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
442 glDisableClientState( GL_SECONDARY_COLOR_ARRAY );
443 glDisableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
447 void pvr2_scene_setup_fixed( GLfloat *viewMatrix )
450 void pvr2_scene_set_alpha_fixed( float alphaRef )
453 void pvr2_scene_cleanup_fixed()
458 void pvr2_scene_setup_shader( GLfloat *viewMatrix )
460 glEnable( GL_DEPTH_TEST );
462 glsl_use_pvr2_shader();
463 glsl_set_pvr2_shader_view_matrix(viewMatrix);
464 glsl_set_pvr2_shader_fog_colour1(pvr2_scene.fog_vert_colour);
465 glsl_set_pvr2_shader_fog_colour2(pvr2_scene.fog_lut_colour);
466 glsl_set_pvr2_shader_in_vertex_vec3_pointer(&pvr2_scene.vertex_array[0].x, sizeof(struct vertex_struct));
467 glsl_set_pvr2_shader_in_colour_pointer(&pvr2_scene.vertex_array[0].rgba[0], sizeof(struct vertex_struct));
468 glsl_set_pvr2_shader_in_colour2_pointer(&pvr2_scene.vertex_array[0].offset_rgba[0], sizeof(struct vertex_struct));
469 glsl_set_pvr2_shader_in_texcoord_pointer(&pvr2_scene.vertex_array[0].u, sizeof(struct vertex_struct));
470 glsl_set_pvr2_shader_alpha_ref(0.0);
471 glsl_set_pvr2_shader_primary_texture(0);
472 glsl_set_pvr2_shader_palette_texture(1);
475 void pvr2_scene_cleanup_shader( )
479 glDisable( GL_DEPTH_TEST );
482 void pvr2_scene_set_alpha_shader( float alphaRef )
484 glsl_set_pvr2_shader_alpha_ref(alphaRef);
488 * Render the currently defined scene in pvr2_scene
490 void pvr2_scene_render( render_buffer_t buffer )
493 struct timeval start_tv, tex_tv, end_tv;
495 GLfloat viewMatrix[16];
496 uint32_t clip_bounds[4];
499 gettimeofday(&start_tv, NULL);
500 display_driver->set_render_target(buffer);
501 pvr2_check_palette_changed();
502 pvr2_scene_load_textures();
505 gettimeofday( &tex_tv, NULL );
506 uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
507 (tex_tv.tv_usec - start_tv.tv_usec)/1000;
508 DEBUG( "Texture load in %dms", ms );
510 float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
511 float nearz = pvr2_scene.bounds[4];
512 float farz = pvr2_scene.bounds[5];
513 if( nearz == farz ) {
517 /* Generate integer clip boundaries */
518 for( i=0; i<4; i++ ) {
519 clip_bounds[i] = (uint32_t)pvr2_scene.bounds[i];
522 defineOrthoMatrix(viewMatrix, pvr2_scene.buffer_width, pvr2_scene.buffer_height, -farz, -nearz);
525 pvr2_scene_setup_shader(viewMatrix);
527 pvr2_scene_setup_fixed(viewMatrix);
531 /* Clear the buffer (FIXME: May not want always want to do this) */
532 glDisable( GL_SCISSOR_TEST );
533 glDepthMask( GL_TRUE );
534 glStencilMask( 0x03 );
535 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
537 /* Render the background */
538 gl_render_bkgnd( pvr2_scene.bkgnd_poly );
540 glEnable( GL_SCISSOR_TEST );
541 glEnable( GL_TEXTURE_2D );
543 struct tile_segment *segment;
545 #define FOREACH_SEGMENT(segment) \
546 segment = pvr2_scene.segment_list; \
548 int tilex = SEGMENT_X(segment->control); \
549 int tiley = SEGMENT_Y(segment->control); \
551 uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 }; \
552 if( !clip_tile_bounds(tile_bounds, clip_bounds) ) { \
555 #define END_FOREACH_SEGMENT() \
556 } while( !IS_LAST_SEGMENT(segment++) );
557 #define CLIP_TO_SEGMENT() \
558 glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3], tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] )
560 /* Build up the opaque stencil map */
561 if( display_driver->capabilities.stencil_bits >= 2 ) {
562 glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
563 FOREACH_SEGMENT(segment)
564 if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
566 gl_render_tilelist_depthonly(segment->opaque_ptr);
568 END_FOREACH_SEGMENT()
570 glEnable( GL_STENCIL_TEST );
571 glStencilFunc( GL_ALWAYS, 0, 1 );
572 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
573 glStencilMask( 0x01 );
574 glDepthFunc( GL_LEQUAL );
575 glDepthMask( GL_FALSE );
576 FOREACH_SEGMENT(segment)
577 if( IS_NONEMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
579 gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
581 END_FOREACH_SEGMENT()
582 glDepthMask( GL_TRUE );
583 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
584 glDisable( GL_SCISSOR_TEST );
585 glClear( GL_DEPTH_BUFFER_BIT );
586 glEnable( GL_SCISSOR_TEST );
587 glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
590 /* Render the opaque polygons */
591 FOREACH_SEGMENT(segment)
593 gl_render_tilelist(segment->opaque_ptr,TRUE);
594 END_FOREACH_SEGMENT()
595 glDisable( GL_STENCIL_TEST );
597 /* Render the punch-out polygons */
599 pvr2_scene_set_alpha_shader(alphaRef);
601 pvr2_scene_set_alpha_fixed(alphaRef);
602 glDepthFunc(GL_GEQUAL);
603 FOREACH_SEGMENT(segment)
605 gl_render_tilelist(segment->punchout_ptr, FALSE );
606 END_FOREACH_SEGMENT()
608 pvr2_scene_set_alpha_shader(0.0);
610 pvr2_scene_set_alpha_fixed(0.0);
612 /* Render the translucent polygons */
613 FOREACH_SEGMENT(segment)
614 if( IS_NONEMPTY_TILE_LIST(segment->trans_ptr) ) {
616 if( pvr2_scene.sort_mode == SORT_NEVER ||
617 (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
618 gl_render_tilelist(segment->trans_ptr, TRUE);
620 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
623 END_FOREACH_SEGMENT()
625 glDisable( GL_SCISSOR_TEST );
628 pvr2_scene_cleanup_shader();
630 pvr2_scene_cleanup_fixed();
633 pvr2_scene_finished();
635 gettimeofday( &end_tv, NULL );
636 ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
637 (end_tv.tv_usec - tex_tv.tv_usec)/1000;
638 DEBUG( "Scene render in %dms", ms );
.