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"
28 #define IS_EMPTY_TILE_LIST(p) ((*((uint32_t *)(pvr2_main_ram+(p))) >> 28) == 0x0F)
30 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
31 GL_GREATER, GL_NOTEQUAL, GL_GEQUAL,
33 int pvr2_poly_srcblend[8] = {
34 GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
35 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
36 GL_ONE_MINUS_DST_ALPHA };
37 int pvr2_poly_dstblend[8] = {
38 GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
39 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
40 GL_ONE_MINUS_DST_ALPHA };
41 int pvr2_poly_texblend[4] = {
48 static gboolean have_shaders = FALSE;
51 * Clip the tile bounds to the clipping plane.
52 * @return TRUE if the tile was not clipped completely.
54 static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
56 if( tile[0] < clip[0] ) tile[0] = clip[0];
57 if( tile[1] > clip[1] ) tile[1] = clip[1];
58 if( tile[2] < clip[2] ) tile[2] = clip[2];
59 if( tile[3] > clip[3] ) tile[3] = clip[3];
60 return tile[0] < tile[1] && tile[2] < tile[3];
63 static void drawrect2d( uint32_t tile_bounds[], float z )
66 glVertex3f( tile_bounds[0], tile_bounds[2], z );
67 glVertex3f( tile_bounds[1], tile_bounds[2], z );
68 glVertex3f( tile_bounds[1], tile_bounds[3], z );
69 glVertex3f( tile_bounds[0], tile_bounds[3], z );
73 void pvr2_scene_load_textures()
77 texcache_begin_scene( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03,
78 (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5 );
80 for( i=0; i < pvr2_scene.poly_count; i++ ) {
81 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
82 if( POLY1_TEXTURED(poly->context[0]) ) {
83 poly->tex_id = texcache_get_texture( poly->context[1], poly->context[2] );
84 if( poly->mod_vertex_index != -1 ) {
85 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
86 poly->mod_tex_id = texcache_get_texture( poly->context[3], poly->context[4] );
88 poly->mod_tex_id = poly->tex_id;
100 * Once-off call to setup the OpenGL context.
102 void pvr2_setup_gl_context()
105 if( glsl_is_supported() && isGLMultitextureSupported() ) {
106 if( !glsl_load_shaders( ) ) {
107 WARN( "Unable to load GL shaders" );
113 texcache_gl_init(); // Allocate texture IDs
114 glDisable( GL_CULL_FACE );
115 glEnable( GL_BLEND );
116 glEnable( GL_DEPTH_TEST );
117 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
118 glMatrixMode(GL_MODELVIEW);
121 #ifdef HAVE_OPENGL_CLAMP_COLOR
122 if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
123 glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
124 glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
128 glEnableClientState( GL_COLOR_ARRAY );
129 glEnableClientState( GL_VERTEX_ARRAY );
130 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
131 glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
132 glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
134 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
138 glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
139 glFogi(GL_FOG_MODE, GL_LINEAR);
140 glFogf(GL_FOG_START, 0.0);
141 glFogf(GL_FOG_END, 1.0);
144 glsl_set_shader(DEFAULT_PROGRAM);
145 glsl_set_uniform_int(DEFAULT_PROGRAM, "primary_texture", 0);
146 glsl_set_uniform_int(DEFAULT_PROGRAM, "palette_texture", 1);
152 * Setup the basic context that's shared between normal and modified modes -
155 static void render_set_base_context( uint32_t poly1, gboolean set_depth )
158 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
161 glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
165 * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
167 void render_set_tsp_context( uint32_t poly1, uint32_t poly2 )
169 glShadeModel( POLY1_SHADE_MODEL(poly1) );
171 if( POLY1_TEXTURED(poly1) && !have_shaders ) {
172 if( POLY2_TEX_BLEND(poly2) == 2 )
173 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
175 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
179 switch( POLY2_FOG_MODE(poly2) ) {
180 case PVR2_POLY_FOG_LOOKUP:
181 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
183 case PVR2_POLY_FOG_VERTEX:
184 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
188 int srcblend = POLY2_SRC_BLEND(poly2);
189 int destblend = POLY2_DEST_BLEND(poly2);
190 glBlendFunc( srcblend, destblend );
192 if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
193 WARN( "Accumulation buffer not supported" );
198 * Setup the GL context for the supplied polygon context.
199 * @param context pointer to 3 or 5 words of polygon context
200 * @param depth_mode force depth mode, or 0 to use the polygon's
203 void render_set_context( uint32_t *context, gboolean set_depth )
205 render_set_base_context(context[0], set_depth);
206 render_set_tsp_context(context[0],context[1]);
209 static inline void gl_draw_vertexes( struct polygon_struct *poly )
212 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
213 poly = poly->sub_next;
214 } while( poly != NULL );
217 static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
220 glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
221 poly = poly->sub_next;
222 } while( poly != NULL );
225 void gl_render_poly( struct polygon_struct *poly, gboolean set_depth)
227 if( poly->vertex_count == 0 )
230 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
231 if( poly->mod_vertex_index == -1 ) {
232 render_set_context( poly->context, set_depth );
233 gl_draw_vertexes(poly);
235 glEnable( GL_STENCIL_TEST );
236 render_set_base_context( poly->context[0], set_depth );
237 render_set_tsp_context( poly->context[0], poly->context[1] );
238 glStencilFunc(GL_EQUAL, 0, 2);
239 gl_draw_vertexes(poly);
241 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
242 if( poly->mod_tex_id != poly->tex_id ) {
243 glBindTexture(GL_TEXTURE_2D, poly->mod_tex_id);
245 render_set_tsp_context( poly->context[0], poly->context[3] );
247 glStencilFunc(GL_EQUAL, 2, 2);
248 gl_draw_mod_vertexes(poly);
249 glDisable( GL_STENCIL_TEST );
254 static void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
256 /* A bit of explanation:
257 * In theory it works like this: generate a 1-bit stencil for each polygon
258 * volume, and then AND or OR it against the overall 1-bit tile stencil at
259 * the end of the volume.
261 * The implementation here uses a 2-bit stencil buffer, where each volume
262 * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
263 * to update bit 1 accordingly and clear bit 0.
265 * This could probably be more efficient, but at least it works correctly
269 if( poly->vertex_count == 0 )
272 gl_draw_vertexes(poly);
276 int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
277 if( poly_type == PVR2_VOLUME_REGION0 ) {
283 glStencilMask( 0x03 );
284 glStencilFunc(GL_EQUAL, 0x02, 0x03);
285 glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
286 glDisable( GL_DEPTH_TEST );
288 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
290 glEnable( GL_DEPTH_TEST );
291 glStencilMask( 0x01 );
292 glStencilFunc( GL_ALWAYS, 0, 1 );
293 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
294 } else if( poly_type == PVR2_VOLUME_REGION1 ) {
295 /* This is harder with the standard stencil ops - do it in two passes
296 * 00 => 00 | 00 => 10
297 * 01 => 10 | 01 => 10
298 * 10 => 10 | 10 => 00
299 * 11 => 10 | 11 => 10
301 glStencilMask( 0x02 );
302 glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
303 glDisable( GL_DEPTH_TEST );
305 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
307 glStencilMask( 0x03 );
308 glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
309 glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
311 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
313 glEnable( GL_DEPTH_TEST );
314 glStencilMask( 0x01 );
315 glStencilFunc( GL_ALWAYS, 0, 1 );
316 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
320 static void gl_render_bkgnd( struct polygon_struct *poly )
322 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
323 render_set_tsp_context( poly->context[0], poly->context[1] );
324 glDisable( GL_DEPTH_TEST );
325 glBlendFunc( GL_ONE, GL_ZERO );
326 gl_draw_vertexes(poly);
327 glEnable( GL_DEPTH_TEST );
330 void gl_render_tilelist( pvraddr_t tile_entry, gboolean set_depth )
334 FOREACH_TILEENTRY(list, tile_entry) {
335 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
338 gl_render_poly(poly, set_depth);
340 } while( list.strip_count-- > 0 );
346 * Render the tilelist with depthbuffer updates only.
348 static void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
352 FOREACH_TILEENTRY(list, tile_entry) {
353 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
356 render_set_base_context(poly->context[0],TRUE);
357 gl_draw_vertexes(poly);
359 } while( list.strip_count-- > 0 );
364 static void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
368 if( !IS_TILE_PTR(tile_entry) )
371 glEnable( GL_STENCIL_TEST );
372 glEnable( GL_DEPTH_TEST );
373 glStencilFunc( GL_ALWAYS, 0, 1 );
374 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
375 glStencilMask( 0x01 );
376 glDepthFunc( GL_LEQUAL );
377 glDepthMask( GL_FALSE );
379 FOREACH_TILEENTRY(list, tile_entry ) {
380 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
383 gl_render_modifier_polygon( poly, tile_bounds );
385 } while( list.strip_count-- > 0 );
388 glDepthMask( GL_TRUE );
389 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
390 glDisable( GL_STENCIL_TEST );
393 static void gl_render_stencil( )
395 struct tile_segment *segment = pvr2_scene.segment_list;
396 if( display_driver->capabilities.stencil_bits >= 2 ) {
397 glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
399 int tilex = SEGMENT_X(segment->control);
400 int tiley = SEGMENT_Y(segment->control);
402 uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
403 if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
404 continue; // fully clipped, skip tile
407 /* Clip to the visible part of the tile */
408 glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3],
409 tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
410 if( IS_TILE_PTR(segment->opaquemod_ptr) &&
411 !IS_EMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
412 gl_render_tilelist_depthonly(segment->opaque_ptr);
413 gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
415 } while( !IS_LAST_SEGMENT(segment++) );
416 glClear( GL_DEPTH_BUFFER_BIT );
417 glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
423 * Render the currently defined scene in pvr2_scene
425 void pvr2_scene_render( render_buffer_t buffer )
428 struct timeval start_tv, tex_tv, end_tv;
430 gettimeofday(&start_tv, NULL);
431 display_driver->set_render_target(buffer);
432 pvr2_check_palette_changed();
433 pvr2_scene_load_textures();
435 gettimeofday( &tex_tv, NULL );
436 uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
437 (tex_tv.tv_usec - start_tv.tv_usec)/1000;
438 DEBUG( "Scene setup in %dms", ms );
440 /* Setup view projection matrix */
441 glMatrixMode(GL_PROJECTION);
443 float nearz = pvr2_scene.bounds[4];
444 float farz = pvr2_scene.bounds[5];
445 if( nearz == farz ) {
448 glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0,
450 float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
451 glAlphaFunc( GL_GEQUAL, alphaRef );
453 /* Clear the buffer (FIXME: May not want always want to do this) */
454 glDisable( GL_SCISSOR_TEST );
455 glDepthMask( GL_TRUE );
456 glStencilMask( 0x03 );
457 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
459 /* Setup vertex array pointers */
460 glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
461 glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
462 glTexCoordPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
463 glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
464 glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
465 /* Turn on the shaders (if available) */
466 glsl_set_shader(DEFAULT_PROGRAM);
468 /* Render the background */
469 gl_render_bkgnd( pvr2_scene.bkgnd_poly );
471 glEnable( GL_SCISSOR_TEST );
472 glEnable( GL_COLOR_SUM );
474 glEnable( GL_TEXTURE_2D );
478 glDisable( GL_SCISSOR_TEST );
479 untile_list(pvr2_scene.segment_list, 0, 0);
480 glEnable( GL_SCISSOR_TEST );
482 /* Process the segment list */
483 struct tile_segment *segment = pvr2_scene.segment_list;
485 int tilex = SEGMENT_X(segment->control);
486 int tiley = SEGMENT_Y(segment->control);
488 uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
489 if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
490 continue; // fully clipped, skip tile
493 /* Clip to the visible part of the tile */
494 glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3],
495 tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
496 if( IS_TILE_PTR(segment->punchout_ptr) ) {
497 glEnable(GL_ALPHA_TEST );
498 glDepthFunc(GL_GEQUAL);
499 gl_render_tilelist(segment->punchout_ptr, FALSE );
500 glDisable(GL_ALPHA_TEST );
503 if( IS_TILE_PTR(segment->trans_ptr) ) {
504 if( pvr2_scene.sort_mode == SORT_NEVER ||
505 (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
506 gl_render_tilelist(segment->trans_ptr, TRUE);
508 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
511 } while( !IS_LAST_SEGMENT(segment++) );
512 glDisable( GL_SCISSOR_TEST );
513 glDisable( GL_COLOR_SUM );
517 gettimeofday( &end_tv, NULL );
518 ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
519 (end_tv.tv_usec - tex_tv.tv_usec)/1000;
520 DEBUG( "Scene render in %dms", ms );
.