filename | src/pvr2/glrender.c |
changeset | 1137:4799d64b3478 |
prev | 1136:078a2202958a |
next | 1138:3bcb705a7ebc |
author | nkeynes |
date | Sun Oct 24 13:40:52 2010 +1000 (13 years ago) |
permissions | -rw-r--r-- |
last change | Change forced-depth-function lists (autosort, punchout) to just set the depth once at the start of the list Remove unused parameter from render_set_tsp_context |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
3 *
4 * Standard OpenGL rendering engine.
5 *
6 * Copyright (c) 2005 Nathan Keynes.
7 *
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.
12 *
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.
17 */
19 #include <assert.h>
20 #include <sys/time.h>
21 #include "display.h"
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 *)(pvr2_main_ram+(p))) >> 28) == 0x0F)
29 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
30 GL_GREATER, GL_NOTEQUAL, GL_GEQUAL,
31 GL_ALWAYS };
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] = {
41 GL_REPLACE,
42 GL_MODULATE,
43 GL_DECAL,
44 GL_MODULATE
45 };
47 /**
48 * Clip the tile bounds to the clipping plane.
49 * @return TRUE if the tile was not clipped completely.
50 */
51 static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
52 {
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];
58 }
60 void pvr2_scene_load_textures()
61 {
62 int i;
64 texcache_begin_scene( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03,
65 (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5 );
67 for( i=0; i < pvr2_scene.poly_count; i++ ) {
68 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
69 if( POLY1_TEXTURED(poly->context[0]) ) {
70 poly->tex_id = texcache_get_texture( poly->context[1], poly->context[2] );
71 if( poly->mod_vertex_index != -1 ) {
72 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
73 poly->mod_tex_id = texcache_get_texture( poly->context[3], poly->context[4] );
74 } else {
75 poly->mod_tex_id = poly->tex_id;
76 }
77 }
78 } else {
79 poly->tex_id = -1;
80 poly->mod_tex_id = -1;
81 }
82 }
83 }
87 /**
88 * Once-off call to setup the OpenGL context.
89 */
90 void pvr2_setup_gl_context()
91 {
93 if( glsl_is_supported() ) {
94 if( !glsl_load_shaders( ) ) {
95 WARN( "Unable to load GL shaders" );
96 }
97 }
99 texcache_gl_init(); // Allocate texture IDs
100 glDisable( GL_CULL_FACE );
101 glEnable( GL_BLEND );
102 glEnable( GL_DEPTH_TEST );
103 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
104 glMatrixMode(GL_MODELVIEW);
105 glLoadIdentity();
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 );
111 }
112 #endif
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);
121 glClearDepth(0);
122 glClearStencil(0);
124 glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
125 glFogi(GL_FOG_MODE, GL_LINEAR);
126 glFogf(GL_FOG_START, 0.0);
127 glFogf(GL_FOG_END, 1.0);
128 }
130 /**
131 * Setup the basic context that's shared between normal and modified modes -
132 * depth, culling
133 */
134 static void render_set_base_context( uint32_t poly1, gboolean set_depth )
135 {
136 if( set_depth ) {
137 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
138 }
140 glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
141 }
143 /**
144 * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
145 */
146 void render_set_tsp_context( uint32_t poly1, uint32_t poly2 )
147 {
148 glShadeModel( POLY1_SHADE_MODEL(poly1) );
149 if( POLY1_TEXTURED(poly1) ) {
150 glEnable(GL_TEXTURE_2D);
151 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
152 } else {
153 glDisable( GL_TEXTURE_2D );
154 }
156 switch( POLY2_FOG_MODE(poly2) ) {
157 case PVR2_POLY_FOG_LOOKUP:
158 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
159 break;
160 case PVR2_POLY_FOG_VERTEX:
161 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
162 break;
163 }
165 int srcblend = POLY2_SRC_BLEND(poly2);
166 int destblend = POLY2_DEST_BLEND(poly2);
167 glBlendFunc( srcblend, destblend );
169 if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
170 WARN( "Accumulation buffer not supported" );
171 }
172 }
174 /**
175 * Setup the GL context for the supplied polygon context.
176 * @param context pointer to 3 or 5 words of polygon context
177 * @param depth_mode force depth mode, or 0 to use the polygon's
178 * depth mode.
179 */
180 void render_set_context( uint32_t *context, gboolean set_depth )
181 {
182 render_set_base_context(context[0], set_depth);
183 render_set_tsp_context(context[0],context[1]);
184 }
186 static inline void gl_draw_vertexes( struct polygon_struct *poly )
187 {
188 do {
189 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
190 poly = poly->sub_next;
191 } while( poly != NULL );
192 }
194 static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
195 {
196 do {
197 glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
198 poly = poly->sub_next;
199 } while( poly != NULL );
200 }
202 static void gl_render_poly( struct polygon_struct *poly, gboolean set_depth)
203 {
204 if( poly->vertex_count == 0 )
205 return; /* Culled */
207 if( poly->tex_id != -1 ) {
208 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
209 }
210 if( poly->mod_vertex_index == -1 ) {
211 render_set_context( poly->context, set_depth );
212 gl_draw_vertexes(poly);
213 } else {
214 glEnable( GL_STENCIL_TEST );
215 render_set_base_context( poly->context[0], set_depth );
216 render_set_tsp_context( poly->context[0], poly->context[1] );
217 glStencilFunc(GL_EQUAL, 0, 2);
218 gl_draw_vertexes(poly);
220 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
221 if( poly->mod_tex_id != -1 ) {
222 glBindTexture(GL_TEXTURE_2D, poly->mod_tex_id);
223 }
224 render_set_tsp_context( poly->context[0], poly->context[3] );
225 }
226 glStencilFunc(GL_EQUAL, 2, 2);
227 gl_draw_mod_vertexes(poly);
228 glDisable( GL_STENCIL_TEST );
229 }
230 }
232 static void gl_render_bkgnd( struct polygon_struct *poly )
233 {
234 if( poly->tex_id != -1 ) {
235 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
236 }
237 render_set_tsp_context( poly->context[0], poly->context[1] );
238 glDisable( GL_DEPTH_TEST );
239 glBlendFunc( GL_ONE, GL_ZERO );
240 gl_draw_vertexes(poly);
241 glEnable( GL_DEPTH_TEST );
242 }
244 void gl_render_tilelist( pvraddr_t tile_entry, gboolean set_depth )
245 {
246 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
247 int strip_count;
248 struct polygon_struct *poly;
250 if( !IS_TILE_PTR(tile_entry) )
251 return;
253 while(1) {
254 uint32_t entry = *tile_list++;
255 switch( entry >> 28 ) {
256 case 0x0F:
257 return; // End-of-list
258 case 0x0E:
259 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
260 break;
261 case 0x08: case 0x09: case 0x0A: case 0x0B:
262 strip_count = ((entry >> 25) & 0x0F)+1;
263 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
264 while( strip_count > 0 ) {
265 assert( poly != NULL );
266 gl_render_poly( poly, set_depth );
267 poly = poly->next;
268 strip_count--;
269 }
270 break;
271 default:
272 if( entry & 0x7E000000 ) {
273 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
274 gl_render_poly( poly, set_depth );
275 }
276 }
277 }
278 }
280 /**
281 * Render the tilelist with depthbuffer updates only.
282 */
283 void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
284 {
285 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
286 int strip_count;
287 struct polygon_struct *poly;
289 if( !IS_TILE_PTR(tile_entry) )
290 return;
292 glDisable( GL_TEXTURE_2D );
294 while(1) {
295 uint32_t entry = *tile_list++;
296 switch( entry >> 28 ) {
297 case 0x0F:
298 return; // End-of-list
299 case 0x0E:
300 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
301 break;
302 case 0x08: case 0x09: case 0x0A: case 0x0B:
303 strip_count = ((entry >> 25) & 0x0F)+1;
304 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
305 while( strip_count > 0 ) {
306 if( poly->vertex_count != 0 ) {
307 render_set_base_context(poly->context[0],TRUE);
308 gl_draw_vertexes(poly);
309 }
310 poly = poly->next;
311 strip_count--;
312 }
313 break;
314 default:
315 if( entry & 0x7E000000 ) {
316 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
317 if( poly->vertex_count != 0 ) {
318 render_set_base_context(poly->context[0],TRUE);
319 gl_draw_vertexes(poly);
320 }
321 }
322 }
323 }
324 }
326 static void drawrect2d( uint32_t tile_bounds[], float z )
327 {
328 glBegin( GL_QUADS );
329 glVertex3f( tile_bounds[0], tile_bounds[2], z );
330 glVertex3f( tile_bounds[1], tile_bounds[2], z );
331 glVertex3f( tile_bounds[1], tile_bounds[3], z );
332 glVertex3f( tile_bounds[0], tile_bounds[3], z );
333 glEnd();
334 }
336 void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
337 {
338 /* A bit of explanation:
339 * In theory it works like this: generate a 1-bit stencil for each polygon
340 * volume, and then AND or OR it against the overall 1-bit tile stencil at
341 * the end of the volume.
342 *
343 * The implementation here uses a 2-bit stencil buffer, where each volume
344 * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
345 * to update bit 1 accordingly and clear bit 0.
346 *
347 * This could probably be more efficient, but at least it works correctly
348 * now :)
349 */
351 if( poly->vertex_count == 0 )
352 return; /* Culled */
354 gl_draw_vertexes(poly);
358 int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
359 if( poly_type == PVR2_VOLUME_REGION0 ) {
360 /* 00 => 00
361 * 01 => 00
362 * 10 => 10
363 * 11 => 00
364 */
365 glStencilMask( 0x03 );
366 glStencilFunc(GL_EQUAL, 0x02, 0x03);
367 glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
368 glDisable( GL_DEPTH_TEST );
370 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
372 glEnable( GL_DEPTH_TEST );
373 glStencilMask( 0x01 );
374 glStencilFunc( GL_ALWAYS, 0, 1 );
375 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
376 } else if( poly_type == PVR2_VOLUME_REGION1 ) {
377 /* This is harder with the standard stencil ops - do it in two passes
378 * 00 => 00 | 00 => 10
379 * 01 => 10 | 01 => 10
380 * 10 => 10 | 10 => 00
381 * 11 => 10 | 11 => 10
382 */
383 glStencilMask( 0x02 );
384 glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
385 glDisable( GL_DEPTH_TEST );
387 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
389 glStencilMask( 0x03 );
390 glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
391 glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
393 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
395 glEnable( GL_DEPTH_TEST );
396 glStencilMask( 0x01 );
397 glStencilFunc( GL_ALWAYS, 0, 1 );
398 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
399 }
400 }
402 void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
403 {
404 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
405 int strip_count;
406 struct polygon_struct *poly;
408 if( !IS_TILE_PTR(tile_entry) )
409 return;
411 glDisable( GL_TEXTURE_2D );
412 glEnable( GL_STENCIL_TEST );
413 glEnable( GL_DEPTH_TEST );
414 glDepthFunc( GL_LEQUAL );
416 glStencilFunc( GL_ALWAYS, 0, 1 );
417 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
418 glStencilMask( 0x01 );
419 glDepthMask( GL_FALSE );
421 while(1) {
422 uint32_t entry = *tile_list++;
423 switch( entry >> 28 ) {
424 case 0x0F:
425 glDepthMask( GL_TRUE );
426 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
427 glDisable( GL_STENCIL_TEST );
428 return; // End-of-list
429 case 0x0E:
430 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
431 break;
432 case 0x08: case 0x09: case 0x0A: case 0x0B:
433 strip_count = ((entry >> 25) & 0x0F)+1;
434 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
435 while( strip_count > 0 ) {
436 gl_render_modifier_polygon( poly, tile_bounds );
437 poly = poly->next;
438 strip_count--;
439 }
440 break;
441 default:
442 if( entry & 0x7E000000 ) {
443 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
444 gl_render_modifier_polygon( poly, tile_bounds );
445 }
446 }
447 }
449 }
452 /**
453 * Render the currently defined scene in pvr2_scene
454 */
455 void pvr2_scene_render( render_buffer_t buffer )
456 {
457 /* Scene setup */
458 struct timeval start_tv, tex_tv, end_tv;
460 gettimeofday(&start_tv, NULL);
461 display_driver->set_render_target(buffer);
462 pvr2_check_palette_changed();
463 pvr2_scene_load_textures();
465 gettimeofday( &tex_tv, NULL );
466 uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
467 (tex_tv.tv_usec - start_tv.tv_usec)/1000;
468 DEBUG( "Scene setup in %dms", ms );
470 /* Setup view projection matrix */
471 glMatrixMode(GL_PROJECTION);
472 glLoadIdentity();
473 float nearz = pvr2_scene.bounds[4];
474 float farz = pvr2_scene.bounds[5];
475 if( nearz == farz ) {
476 farz*= 4.0;
477 }
478 glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0,
479 -farz, -nearz );
480 float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
481 glAlphaFunc( GL_GEQUAL, alphaRef );
483 /* Clear the buffer (FIXME: May not want always want to do this) */
484 glDisable( GL_SCISSOR_TEST );
485 glDepthMask( GL_TRUE );
486 glStencilMask( 0x03 );
487 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
489 /* Setup vertex array pointers */
490 glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
491 glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
492 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
493 glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
494 glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
495 /* Turn on the shaders (if available) */
496 glsl_set_shader(DEFAULT_PROGRAM);
498 /* Render the background */
499 gl_render_bkgnd( pvr2_scene.bkgnd_poly );
501 glEnable( GL_SCISSOR_TEST );
502 glEnable( GL_COLOR_SUM );
503 glEnable( GL_FOG );
505 /* Process the segment list */
506 struct tile_segment *segment = pvr2_scene.segment_list;
507 do {
508 int tilex = SEGMENT_X(segment->control);
509 int tiley = SEGMENT_Y(segment->control);
511 uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
512 if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
513 continue; // fully clipped, skip tile
514 }
516 /* Clip to the visible part of the tile */
517 glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3],
518 tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
519 if( display_driver->capabilities.stencil_bits >= 2 &&
520 IS_TILE_PTR(segment->opaquemod_ptr) &&
521 !IS_EMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
522 /* Don't do this unless there's actually some shadow polygons */
524 /* Use colormask instead of drawbuffer for simplicity */
525 glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
526 gl_render_tilelist_depthonly(segment->opaque_ptr);
527 gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
528 glClear( GL_DEPTH_BUFFER_BIT );
529 glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
530 }
531 gl_render_tilelist(segment->opaque_ptr,TRUE);
532 if( IS_TILE_PTR(segment->punchout_ptr) ) {
533 glEnable(GL_ALPHA_TEST );
534 glDepthFunc(GL_GEQUAL);
535 gl_render_tilelist(segment->punchout_ptr, FALSE );
536 glDisable(GL_ALPHA_TEST );
537 }
539 if( IS_TILE_PTR(segment->trans_ptr) ) {
540 if( pvr2_scene.sort_mode == SORT_NEVER ||
541 (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
542 gl_render_tilelist(segment->trans_ptr, TRUE);
543 } else {
544 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
545 }
546 }
547 } while( !IS_LAST_SEGMENT(segment++) );
548 glDisable( GL_SCISSOR_TEST );
549 glDisable( GL_COLOR_SUM );
550 glDisable( GL_FOG );
551 glsl_clear_shader();
553 gettimeofday( &end_tv, NULL );
554 ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
555 (end_tv.tv_usec - tex_tv.tv_usec)/1000;
556 DEBUG( "Scene render in %dms", ms );
557 }
.