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 *)(video_base+(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()
63 for( i=0; i < pvr2_scene.poly_count; i++ ) {
64 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
65 if( POLY1_TEXTURED(poly->context[0]) ) {
66 poly->tex_id = texcache_get_texture( poly->context[2],
67 POLY2_TEX_WIDTH(poly->context[1]),
68 POLY2_TEX_HEIGHT(poly->context[1]) );
69 if( poly->mod_vertex_index != -1 ) {
70 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
71 poly->mod_tex_id = texcache_get_texture( poly->context[4],
72 POLY2_TEX_WIDTH(poly->context[3]),
73 POLY2_TEX_HEIGHT(poly->context[3]) );
75 poly->mod_tex_id = poly->tex_id;
80 poly->mod_tex_id = -1;
88 * Once-off call to setup the OpenGL context.
90 void pvr2_setup_gl_context()
93 if( glsl_is_supported() ) {
94 if( !glsl_load_shaders( glsl_vertex_shader_src, NULL ) ) {
95 WARN( "Unable to load GL shaders" );
99 texcache_gl_init(); // Allocate texture IDs
100 glCullFace( GL_BACK );
101 glEnable( GL_BLEND );
102 glEnable( GL_DEPTH_TEST );
103 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
104 glMatrixMode(GL_MODELVIEW);
107 #ifdef HAVE_OPENGL_CLAMP_COLOR
108 if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
109 glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
110 glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
114 glEnableClientState( GL_COLOR_ARRAY );
115 glEnableClientState( GL_VERTEX_ARRAY );
116 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
117 glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
118 glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
120 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
125 static void render_set_cull( uint32_t poly1 )
127 switch( POLY1_CULL_MODE(poly1) ) {
130 glDisable( GL_CULL_FACE );
133 glEnable( GL_CULL_FACE );
134 glFrontFace( GL_CW );
137 glEnable( GL_CULL_FACE );
138 glFrontFace( GL_CCW );
144 * Setup the basic context that's shared between normal and modified modes -
147 static void render_set_base_context( uint32_t poly1, GLint depth_mode )
149 if( depth_mode == 0 ) {
150 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
152 glDepthFunc(depth_mode);
155 glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
156 render_set_cull( poly1 );
160 * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
162 static void render_set_tsp_context( uint32_t poly1, uint32_t poly2, uint32_t texture )
164 glShadeModel( POLY1_SHADE_MODEL(poly1) );
166 if( POLY1_SPECULAR(poly1) ) {
167 glEnable(GL_COLOR_SUM);
169 glDisable(GL_COLOR_SUM);
172 if( POLY1_TEXTURED(poly1) ) {
173 glEnable(GL_TEXTURE_2D);
174 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
175 if( POLY2_TEX_CLAMP_U(poly2) ) {
176 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
177 } else if( POLY2_TEX_MIRROR_U(poly2) ) {
178 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB );
180 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
182 if( POLY2_TEX_CLAMP_V(poly2) ) {
183 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
184 } else if( POLY2_TEX_MIRROR_V(poly2) ) {
185 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB );
187 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
190 glDisable( GL_TEXTURE_2D );
193 switch( POLY2_FOG_MODE(poly2) ) {
194 case PVR2_POLY_FOG_LOOKUP:
195 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
198 case PVR2_POLY_FOG_VERTEX:
199 if( POLY1_SPECULAR(poly1) ) {
200 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
203 } /* else fallthrough */
208 int srcblend = POLY2_SRC_BLEND(poly2);
209 int destblend = POLY2_DEST_BLEND(poly2);
210 glBlendFunc( srcblend, destblend );
212 if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
213 WARN( "Accumulation buffer not supported" );
218 * Setup the GL context for the supplied polygon context.
219 * @param context pointer to 3 or 5 words of polygon context
220 * @param depth_mode force depth mode, or 0 to use the polygon's
223 void render_set_context( uint32_t *context, GLint depth_mode )
225 render_set_base_context(context[0], depth_mode);
226 render_set_tsp_context(context[0],context[1],context[2]);
230 static void gl_render_poly( struct polygon_struct *poly, GLint depth_mode )
232 if( poly->tex_id != -1 ) {
233 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
235 if( poly->mod_vertex_index == -1 ) {
236 glDisable( GL_STENCIL_TEST );
237 render_set_context( poly->context, depth_mode );
238 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
240 glEnable( GL_STENCIL_TEST );
241 render_set_base_context( poly->context[0], depth_mode );
242 render_set_tsp_context( poly->context[0], poly->context[1], poly->context[2] );
243 glStencilFunc(GL_EQUAL, 0, 2);
244 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
246 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
247 if( poly->mod_tex_id != -1 ) {
248 glBindTexture(GL_TEXTURE_2D, poly->mod_tex_id);
250 render_set_tsp_context( poly->context[0], poly->context[3], poly->context[4] );
252 glStencilFunc(GL_EQUAL, 2, 2);
253 glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count );
257 static void gl_render_bkgnd( struct polygon_struct *poly )
259 if( poly->tex_id != -1 ) {
260 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
262 render_set_context( poly->context, 0 );
263 glDisable( GL_DEPTH_TEST );
264 glDisable( GL_CULL_FACE );
265 glBlendFunc( GL_ONE, GL_ZERO );
266 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
267 glEnable( GL_CULL_FACE );
268 glEnable( GL_DEPTH_TEST );
271 void gl_render_tilelist( pvraddr_t tile_entry, GLint depth_mode )
273 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
275 struct polygon_struct *poly;
277 if( !IS_TILE_PTR(tile_entry) )
281 uint32_t entry = *tile_list++;
282 switch( entry >> 28 ) {
284 return; // End-of-list
286 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
288 case 0x08: case 0x09: case 0x0A: case 0x0B:
289 strip_count = ((entry >> 25) & 0x0F)+1;
290 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
291 while( strip_count > 0 ) {
292 assert( poly != NULL );
293 gl_render_poly( poly, depth_mode );
299 if( entry & 0x7E000000 ) {
300 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
301 gl_render_poly( poly, depth_mode );
308 * Render the tilelist with depthbuffer updates only.
310 void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
312 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
314 struct polygon_struct *poly;
316 if( !IS_TILE_PTR(tile_entry) )
319 glDisable( GL_TEXTURE_2D );
321 glDisable( GL_COLOR_SUM );
324 uint32_t entry = *tile_list++;
325 switch( entry >> 28 ) {
327 return; // End-of-list
329 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
331 case 0x08: case 0x09: case 0x0A: case 0x0B:
332 strip_count = ((entry >> 25) & 0x0F)+1;
333 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
334 while( strip_count > 0 ) {
335 render_set_base_context(poly->context[0],0);
336 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
342 if( entry & 0x7E000000 ) {
343 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
344 render_set_base_context(poly->context[0],0);
345 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
351 static void drawrect2d( uint32_t tile_bounds[], float z )
354 glVertex3f( tile_bounds[0], tile_bounds[2], z );
355 glVertex3f( tile_bounds[1], tile_bounds[2], z );
356 glVertex3f( tile_bounds[1], tile_bounds[3], z );
357 glVertex3f( tile_bounds[0], tile_bounds[3], z );
361 void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
363 /* A bit of explanation:
364 * In theory it works like this: generate a 1-bit stencil for each polygon
365 * volume, and then AND or OR it against the overall 1-bit tile stencil at
366 * the end of the volume.
368 * The implementation here uses a 2-bit stencil buffer, where each volume
369 * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
370 * to update bit 1 accordingly and clear bit 0.
372 * This could probably be more efficient, but at least it works correctly
376 render_set_cull(poly->context[0]);
377 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
379 int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
380 if( poly_type == PVR2_VOLUME_REGION0 ) {
386 glStencilMask( 0x03 );
387 glStencilFunc(GL_EQUAL, 0x02, 0x03);
388 glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
389 glDisable( GL_CULL_FACE );
390 glDisable( GL_DEPTH_TEST );
392 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
394 glEnable( GL_DEPTH_TEST );
395 glStencilMask( 0x01 );
396 glStencilFunc( GL_ALWAYS, 0, 1 );
397 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
398 } else if( poly_type == PVR2_VOLUME_REGION1 ) {
399 /* This is harder with the standard stencil ops - do it in two passes
400 * 00 => 00 | 00 => 10
401 * 01 => 10 | 01 => 10
402 * 10 => 10 | 10 => 00
403 * 11 => 10 | 11 => 10
405 glStencilMask( 0x02 );
406 glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
407 glDisable( GL_CULL_FACE );
408 glDisable( GL_DEPTH_TEST );
410 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
412 glStencilMask( 0x03 );
413 glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
414 glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
416 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
418 glEnable( GL_DEPTH_TEST );
419 glStencilMask( 0x01 );
420 glStencilFunc( GL_ALWAYS, 0, 1 );
421 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
425 void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
427 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
429 struct polygon_struct *poly;
431 if( !IS_TILE_PTR(tile_entry) )
434 glDisable( GL_TEXTURE_2D );
436 glDisable( GL_COLOR_SUM );
437 glDisable( GL_CULL_FACE );
438 glEnable( GL_STENCIL_TEST );
439 glEnable( GL_DEPTH_TEST );
440 glDepthFunc( GL_LEQUAL );
442 glStencilFunc( GL_ALWAYS, 0, 1 );
443 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
444 glStencilMask( 0x01 );
445 glDepthMask( GL_FALSE );
448 uint32_t entry = *tile_list++;
449 switch( entry >> 28 ) {
451 glDepthMask( GL_TRUE );
452 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
453 return; // End-of-list
455 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
457 case 0x08: case 0x09: case 0x0A: case 0x0B:
458 strip_count = ((entry >> 25) & 0x0F)+1;
459 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
460 while( strip_count > 0 ) {
461 gl_render_modifier_polygon( poly, tile_bounds );
467 if( entry & 0x7E000000 ) {
468 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
469 gl_render_modifier_polygon( poly, tile_bounds );
478 * Render the currently defined scene in pvr2_scene
480 void pvr2_scene_render( render_buffer_t buffer )
483 struct timeval start_tv, tex_tv, end_tv;
485 gettimeofday(&start_tv, NULL);
486 display_driver->set_render_target(buffer);
487 pvr2_check_palette_changed();
488 pvr2_scene_load_textures();
490 gettimeofday( &tex_tv, NULL );
491 uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
492 (tex_tv.tv_usec - start_tv.tv_usec)/1000;
493 DEBUG( "Scene setup in %dms", ms );
495 /* Setup view projection matrix */
496 glMatrixMode(GL_PROJECTION);
498 float nearz = pvr2_scene.bounds[4];
499 float farz = pvr2_scene.bounds[5];
500 if( nearz == farz ) {
503 glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0,
505 float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
506 glAlphaFunc( GL_GEQUAL, alphaRef );
508 /* Clear the buffer (FIXME: May not want always want to do this) */
509 glDisable( GL_SCISSOR_TEST );
510 glDepthMask( GL_TRUE );
511 glStencilMask( 0x03 );
512 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
514 /* Setup vertex array pointers */
515 glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
516 glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
517 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
518 glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
519 glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
520 glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
521 glFogi(GL_FOG_MODE, GL_LINEAR);
522 glFogf(GL_FOG_START, 0.0);
523 glFogf(GL_FOG_END, 1.0);
524 /* Turn on the shaders (if available) */
525 glsl_enable_shaders(TRUE);
527 /* Render the background */
528 gl_render_bkgnd( pvr2_scene.bkgnd_poly );
530 glEnable( GL_SCISSOR_TEST );
532 /* Process the segment list */
533 struct tile_segment *segment = pvr2_scene.segment_list;
535 int tilex = SEGMENT_X(segment->control);
536 int tiley = SEGMENT_Y(segment->control);
538 uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
539 if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
540 continue; // fully clipped, skip tile
543 /* Clip to the visible part of the tile */
544 glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3],
545 tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
546 if( display_driver->capabilities.stencil_bits >= 2 &&
547 IS_TILE_PTR(segment->opaquemod_ptr) &&
548 !IS_EMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
549 /* Don't do this unless there's actually some shadow polygons */
551 /* Use colormask instead of drawbuffer for simplicity */
552 glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
553 gl_render_tilelist_depthonly(segment->opaque_ptr);
554 gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
555 glClear( GL_DEPTH_BUFFER_BIT );
556 glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
558 gl_render_tilelist(segment->opaque_ptr,0);
559 if( IS_TILE_PTR(segment->punchout_ptr) ) {
560 glEnable(GL_ALPHA_TEST );
561 gl_render_tilelist(segment->punchout_ptr, GL_GEQUAL );
562 glDisable(GL_ALPHA_TEST );
564 glDisable( GL_STENCIL_TEST );
566 glClear( GL_STENCIL_BUFFER_BIT );
568 if( IS_TILE_PTR(segment->trans_ptr) ) {
569 if( pvr2_scene.sort_mode == SORT_NEVER ||
570 (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
571 gl_render_tilelist(segment->trans_ptr, 0);
573 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
576 } while( !IS_LAST_SEGMENT(segment++) );
577 glDisable( GL_SCISSOR_TEST );
579 glsl_enable_shaders(FALSE);
581 gettimeofday( &end_tv, NULL );
582 ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
583 (end_tv.tv_usec - tex_tv.tv_usec)/1000;
584 DEBUG( "Scene render in %dms", ms );
.