filename | src/pvr2/glrender.c |
changeset | 1138:3bcb705a7ebc |
prev | 1137:4799d64b3478 |
next | 1139:9af81878480b |
author | nkeynes |
date | Sun Oct 24 15:15:27 2010 +1000 (12 years ago) |
permissions | -rw-r--r-- |
last change | Bind texture 0 instead of enabling/disabling texturing all the time (marginally faster, but also simpler) |
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 = 0;
80 poly->mod_tex_id = 0;
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 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
151 }
153 switch( POLY2_FOG_MODE(poly2) ) {
154 case PVR2_POLY_FOG_LOOKUP:
155 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
156 break;
157 case PVR2_POLY_FOG_VERTEX:
158 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
159 break;
160 }
162 int srcblend = POLY2_SRC_BLEND(poly2);
163 int destblend = POLY2_DEST_BLEND(poly2);
164 glBlendFunc( srcblend, destblend );
166 if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
167 WARN( "Accumulation buffer not supported" );
168 }
169 }
171 /**
172 * Setup the GL context for the supplied polygon context.
173 * @param context pointer to 3 or 5 words of polygon context
174 * @param depth_mode force depth mode, or 0 to use the polygon's
175 * depth mode.
176 */
177 void render_set_context( uint32_t *context, gboolean set_depth )
178 {
179 render_set_base_context(context[0], set_depth);
180 render_set_tsp_context(context[0],context[1]);
181 }
183 static inline void gl_draw_vertexes( struct polygon_struct *poly )
184 {
185 do {
186 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
187 poly = poly->sub_next;
188 } while( poly != NULL );
189 }
191 static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
192 {
193 do {
194 glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
195 poly = poly->sub_next;
196 } while( poly != NULL );
197 }
199 static void gl_render_poly( struct polygon_struct *poly, gboolean set_depth)
200 {
201 if( poly->vertex_count == 0 )
202 return; /* Culled */
204 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
205 if( poly->mod_vertex_index == -1 ) {
206 render_set_context( poly->context, set_depth );
207 gl_draw_vertexes(poly);
208 } else {
209 glEnable( GL_STENCIL_TEST );
210 render_set_base_context( poly->context[0], set_depth );
211 render_set_tsp_context( poly->context[0], poly->context[1] );
212 glStencilFunc(GL_EQUAL, 0, 2);
213 gl_draw_vertexes(poly);
215 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
216 if( poly->mod_tex_id != poly->tex_id ) {
217 glBindTexture(GL_TEXTURE_2D, poly->mod_tex_id);
218 }
219 render_set_tsp_context( poly->context[0], poly->context[3] );
220 }
221 glStencilFunc(GL_EQUAL, 2, 2);
222 gl_draw_mod_vertexes(poly);
223 glDisable( GL_STENCIL_TEST );
224 }
225 }
227 static void gl_render_bkgnd( struct polygon_struct *poly )
228 {
229 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
230 render_set_tsp_context( poly->context[0], poly->context[1] );
231 glDisable( GL_DEPTH_TEST );
232 glBlendFunc( GL_ONE, GL_ZERO );
233 gl_draw_vertexes(poly);
234 glEnable( GL_DEPTH_TEST );
235 }
237 void gl_render_tilelist( pvraddr_t tile_entry, gboolean set_depth )
238 {
239 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
240 int strip_count;
241 struct polygon_struct *poly;
243 if( !IS_TILE_PTR(tile_entry) )
244 return;
246 while(1) {
247 uint32_t entry = *tile_list++;
248 switch( entry >> 28 ) {
249 case 0x0F:
250 return; // End-of-list
251 case 0x0E:
252 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
253 break;
254 case 0x08: case 0x09: case 0x0A: case 0x0B:
255 strip_count = ((entry >> 25) & 0x0F)+1;
256 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
257 while( strip_count > 0 ) {
258 assert( poly != NULL );
259 gl_render_poly( poly, set_depth );
260 poly = poly->next;
261 strip_count--;
262 }
263 break;
264 default:
265 if( entry & 0x7E000000 ) {
266 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
267 gl_render_poly( poly, set_depth );
268 }
269 }
270 }
271 }
273 /**
274 * Render the tilelist with depthbuffer updates only.
275 */
276 void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
277 {
278 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
279 int strip_count;
280 struct polygon_struct *poly;
282 if( !IS_TILE_PTR(tile_entry) )
283 return;
285 while(1) {
286 uint32_t entry = *tile_list++;
287 switch( entry >> 28 ) {
288 case 0x0F:
289 return; // End-of-list
290 case 0x0E:
291 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
292 break;
293 case 0x08: case 0x09: case 0x0A: case 0x0B:
294 strip_count = ((entry >> 25) & 0x0F)+1;
295 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
296 while( strip_count > 0 ) {
297 if( poly->vertex_count != 0 ) {
298 render_set_base_context(poly->context[0],TRUE);
299 gl_draw_vertexes(poly);
300 }
301 poly = poly->next;
302 strip_count--;
303 }
304 break;
305 default:
306 if( entry & 0x7E000000 ) {
307 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
308 if( poly->vertex_count != 0 ) {
309 render_set_base_context(poly->context[0],TRUE);
310 gl_draw_vertexes(poly);
311 }
312 }
313 }
314 }
315 }
317 static void drawrect2d( uint32_t tile_bounds[], float z )
318 {
319 glBegin( GL_QUADS );
320 glVertex3f( tile_bounds[0], tile_bounds[2], z );
321 glVertex3f( tile_bounds[1], tile_bounds[2], z );
322 glVertex3f( tile_bounds[1], tile_bounds[3], z );
323 glVertex3f( tile_bounds[0], tile_bounds[3], z );
324 glEnd();
325 }
327 void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
328 {
329 /* A bit of explanation:
330 * In theory it works like this: generate a 1-bit stencil for each polygon
331 * volume, and then AND or OR it against the overall 1-bit tile stencil at
332 * the end of the volume.
333 *
334 * The implementation here uses a 2-bit stencil buffer, where each volume
335 * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
336 * to update bit 1 accordingly and clear bit 0.
337 *
338 * This could probably be more efficient, but at least it works correctly
339 * now :)
340 */
342 if( poly->vertex_count == 0 )
343 return; /* Culled */
345 gl_draw_vertexes(poly);
349 int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
350 if( poly_type == PVR2_VOLUME_REGION0 ) {
351 /* 00 => 00
352 * 01 => 00
353 * 10 => 10
354 * 11 => 00
355 */
356 glStencilMask( 0x03 );
357 glStencilFunc(GL_EQUAL, 0x02, 0x03);
358 glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
359 glDisable( GL_DEPTH_TEST );
361 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
363 glEnable( GL_DEPTH_TEST );
364 glStencilMask( 0x01 );
365 glStencilFunc( GL_ALWAYS, 0, 1 );
366 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
367 } else if( poly_type == PVR2_VOLUME_REGION1 ) {
368 /* This is harder with the standard stencil ops - do it in two passes
369 * 00 => 00 | 00 => 10
370 * 01 => 10 | 01 => 10
371 * 10 => 10 | 10 => 00
372 * 11 => 10 | 11 => 10
373 */
374 glStencilMask( 0x02 );
375 glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
376 glDisable( GL_DEPTH_TEST );
378 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
380 glStencilMask( 0x03 );
381 glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
382 glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
384 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
386 glEnable( GL_DEPTH_TEST );
387 glStencilMask( 0x01 );
388 glStencilFunc( GL_ALWAYS, 0, 1 );
389 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
390 }
391 }
393 void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
394 {
395 uint32_t *tile_list = (uint32_t *)(pvr2_main_ram+tile_entry);
396 int strip_count;
397 struct polygon_struct *poly;
399 if( !IS_TILE_PTR(tile_entry) )
400 return;
402 glEnable( GL_STENCIL_TEST );
403 glEnable( GL_DEPTH_TEST );
404 glDepthFunc( GL_LEQUAL );
406 glStencilFunc( GL_ALWAYS, 0, 1 );
407 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
408 glStencilMask( 0x01 );
409 glDepthMask( GL_FALSE );
411 while(1) {
412 uint32_t entry = *tile_list++;
413 switch( entry >> 28 ) {
414 case 0x0F:
415 glDepthMask( GL_TRUE );
416 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
417 glDisable( GL_STENCIL_TEST );
418 return; // End-of-list
419 case 0x0E:
420 tile_list = (uint32_t *)(pvr2_main_ram + (entry&0x007FFFFF));
421 break;
422 case 0x08: case 0x09: case 0x0A: case 0x0B:
423 strip_count = ((entry >> 25) & 0x0F)+1;
424 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
425 while( strip_count > 0 ) {
426 gl_render_modifier_polygon( poly, tile_bounds );
427 poly = poly->next;
428 strip_count--;
429 }
430 break;
431 default:
432 if( entry & 0x7E000000 ) {
433 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
434 gl_render_modifier_polygon( poly, tile_bounds );
435 }
436 }
437 }
439 }
442 /**
443 * Render the currently defined scene in pvr2_scene
444 */
445 void pvr2_scene_render( render_buffer_t buffer )
446 {
447 /* Scene setup */
448 struct timeval start_tv, tex_tv, end_tv;
450 gettimeofday(&start_tv, NULL);
451 display_driver->set_render_target(buffer);
452 pvr2_check_palette_changed();
453 pvr2_scene_load_textures();
455 gettimeofday( &tex_tv, NULL );
456 uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
457 (tex_tv.tv_usec - start_tv.tv_usec)/1000;
458 DEBUG( "Scene setup in %dms", ms );
460 /* Setup view projection matrix */
461 glMatrixMode(GL_PROJECTION);
462 glLoadIdentity();
463 float nearz = pvr2_scene.bounds[4];
464 float farz = pvr2_scene.bounds[5];
465 if( nearz == farz ) {
466 farz*= 4.0;
467 }
468 glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0,
469 -farz, -nearz );
470 float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
471 glAlphaFunc( GL_GEQUAL, alphaRef );
473 /* Clear the buffer (FIXME: May not want always want to do this) */
474 glDisable( GL_SCISSOR_TEST );
475 glDepthMask( GL_TRUE );
476 glStencilMask( 0x03 );
477 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
479 /* Setup vertex array pointers */
480 glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
481 glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
482 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
483 glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
484 glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
485 /* Turn on the shaders (if available) */
486 glsl_set_shader(DEFAULT_PROGRAM);
488 /* Render the background */
489 gl_render_bkgnd( pvr2_scene.bkgnd_poly );
491 glEnable( GL_SCISSOR_TEST );
492 glEnable( GL_COLOR_SUM );
493 glEnable( GL_FOG );
494 glEnable( GL_TEXTURE_2D );
496 /* Process the segment list */
497 struct tile_segment *segment = pvr2_scene.segment_list;
498 do {
499 int tilex = SEGMENT_X(segment->control);
500 int tiley = SEGMENT_Y(segment->control);
502 uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
503 if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
504 continue; // fully clipped, skip tile
505 }
507 /* Clip to the visible part of the tile */
508 glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3],
509 tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
510 if( display_driver->capabilities.stencil_bits >= 2 &&
511 IS_TILE_PTR(segment->opaquemod_ptr) &&
512 !IS_EMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
513 /* Don't do this unless there's actually some shadow polygons */
515 /* Use colormask instead of drawbuffer for simplicity */
516 glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
517 gl_render_tilelist_depthonly(segment->opaque_ptr);
518 gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
519 glClear( GL_DEPTH_BUFFER_BIT );
520 glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
521 }
522 gl_render_tilelist(segment->opaque_ptr,TRUE);
523 if( IS_TILE_PTR(segment->punchout_ptr) ) {
524 glEnable(GL_ALPHA_TEST );
525 glDepthFunc(GL_GEQUAL);
526 gl_render_tilelist(segment->punchout_ptr, FALSE );
527 glDisable(GL_ALPHA_TEST );
528 }
530 if( IS_TILE_PTR(segment->trans_ptr) ) {
531 if( pvr2_scene.sort_mode == SORT_NEVER ||
532 (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
533 gl_render_tilelist(segment->trans_ptr, TRUE);
534 } else {
535 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
536 }
537 }
538 } while( !IS_LAST_SEGMENT(segment++) );
539 glDisable( GL_SCISSOR_TEST );
540 glDisable( GL_COLOR_SUM );
541 glDisable( GL_FOG );
542 glsl_clear_shader();
544 gettimeofday( &end_tv, NULL );
545 ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
546 (end_tv.tv_usec - tex_tv.tv_usec)/1000;
547 DEBUG( "Scene render in %dms", ms );
548 }
.