filename | src/pvr2/glrender.c |
changeset | 1154:5225c7c059ce |
prev | 1153:00e507e4025c |
next | 1156:d124a1c833cb |
author | nkeynes |
date | Mon Jan 17 21:22:06 2011 +1000 (13 years ago) |
permissions | -rw-r--r-- |
last change | Avoid rebinding textures unnecessarily (actually saves a surprising amount of runtime) |
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/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,
32 GL_ALWAYS };
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] = {
42 GL_REPLACE,
43 GL_MODULATE,
44 GL_DECAL,
45 GL_MODULATE
46 };
48 static gboolean have_shaders = FALSE;
49 static int currentTexId = -1;
51 static inline void bind_texture(int texid)
52 {
53 if( currentTexId != texid ) {
54 currentTexId = texid;
55 glBindTexture(GL_TEXTURE_2D, texid);
56 }
57 }
59 /**
60 * Clip the tile bounds to the clipping plane.
61 * @return TRUE if the tile was not clipped completely.
62 */
63 static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
64 {
65 if( tile[0] < clip[0] ) tile[0] = clip[0];
66 if( tile[1] > clip[1] ) tile[1] = clip[1];
67 if( tile[2] < clip[2] ) tile[2] = clip[2];
68 if( tile[3] > clip[3] ) tile[3] = clip[3];
69 return tile[0] < tile[1] && tile[2] < tile[3];
70 }
72 static void drawrect2d( uint32_t tile_bounds[], float z )
73 {
74 glBegin( GL_QUADS );
75 glVertex3f( tile_bounds[0], tile_bounds[2], z );
76 glVertex3f( tile_bounds[1], tile_bounds[2], z );
77 glVertex3f( tile_bounds[1], tile_bounds[3], z );
78 glVertex3f( tile_bounds[0], tile_bounds[3], z );
79 glEnd();
80 }
82 static void pvr2_scene_load_textures()
83 {
84 int i;
86 texcache_begin_scene( MMIO_READ( PVR2, RENDER_PALETTE ) & 0x03,
87 (MMIO_READ( PVR2, RENDER_TEXSIZE ) & 0x003F) << 5 );
89 for( i=0; i < pvr2_scene.poly_count; i++ ) {
90 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
91 if( POLY1_TEXTURED(poly->context[0]) ) {
92 poly->tex_id = texcache_get_texture( poly->context[1], poly->context[2] );
93 if( poly->mod_vertex_index != -1 ) {
94 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
95 poly->mod_tex_id = texcache_get_texture( poly->context[3], poly->context[4] );
96 } else {
97 poly->mod_tex_id = poly->tex_id;
98 }
99 }
100 } else {
101 poly->tex_id = 0;
102 poly->mod_tex_id = 0;
103 }
104 }
105 }
108 /**
109 * Once-off call to setup the OpenGL context.
110 */
111 void pvr2_setup_gl_context()
112 {
114 if( glsl_is_supported() && isGLMultitextureSupported() ) {
115 if( !glsl_load_shaders( ) ) {
116 WARN( "Unable to load GL shaders" );
117 } else {
118 have_shaders = TRUE;
119 }
120 }
122 texcache_gl_init(); // Allocate texture IDs
123 glDisable( GL_CULL_FACE );
124 glEnable( GL_BLEND );
125 glEnable( GL_DEPTH_TEST );
126 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
127 glMatrixMode(GL_MODELVIEW);
128 glLoadIdentity();
130 #ifdef HAVE_OPENGL_CLAMP_COLOR
131 if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
132 glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
133 glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
134 }
135 #endif
137 glEnableClientState( GL_COLOR_ARRAY );
138 glEnableClientState( GL_VERTEX_ARRAY );
139 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
140 glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
141 glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
143 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
144 glClearDepth(0);
145 glClearStencil(0);
147 glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
148 glFogi(GL_FOG_MODE, GL_LINEAR);
149 glFogf(GL_FOG_START, 0.0);
150 glFogf(GL_FOG_END, 1.0);
152 if( have_shaders ) {
153 glsl_set_shader(DEFAULT_PROGRAM);
154 glsl_set_uniform_int(DEFAULT_PROGRAM, "primary_texture", 0);
155 glsl_set_uniform_int(DEFAULT_PROGRAM, "palette_texture", 1);
156 glsl_clear_shader();
157 }
158 }
160 /**
161 * Setup the basic context that's shared between normal and modified modes -
162 * depth, culling
163 */
164 static void render_set_base_context( uint32_t poly1, gboolean set_depth )
165 {
166 if( set_depth ) {
167 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
168 }
170 glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
171 }
173 /**
174 * Setup the texture/shading settings (TSP) which vary between mod/unmod modes.
175 */
176 static void render_set_tsp_context( uint32_t poly1, uint32_t poly2 )
177 {
178 glShadeModel( POLY1_SHADE_MODEL(poly1) );
180 if( POLY1_TEXTURED(poly1) && !have_shaders ) {
181 if( POLY2_TEX_BLEND(poly2) == 2 )
182 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
183 else
184 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
186 }
188 switch( POLY2_FOG_MODE(poly2) ) {
189 case PVR2_POLY_FOG_LOOKUP:
190 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
191 break;
192 case PVR2_POLY_FOG_VERTEX:
193 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
194 break;
195 }
197 int srcblend = POLY2_SRC_BLEND(poly2);
198 int destblend = POLY2_DEST_BLEND(poly2);
199 glBlendFunc( srcblend, destblend );
201 if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
202 WARN( "Accumulation buffer not supported" );
203 }
204 }
206 /**
207 * Setup the GL context for the supplied polygon context.
208 * @param context pointer to 3 or 5 words of polygon context
209 * @param depth_mode force depth mode, or 0 to use the polygon's
210 * depth mode.
211 */
212 static void render_set_context( uint32_t *context, gboolean set_depth )
213 {
214 render_set_base_context(context[0], set_depth);
215 render_set_tsp_context(context[0],context[1]);
216 }
218 static inline void gl_draw_vertexes( struct polygon_struct *poly )
219 {
220 do {
221 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count);
222 poly = poly->sub_next;
223 } while( poly != NULL );
224 }
226 static inline void gl_draw_mod_vertexes( struct polygon_struct *poly )
227 {
228 do {
229 glDrawArrays(GL_TRIANGLE_STRIP, poly->mod_vertex_index, poly->vertex_count);
230 poly = poly->sub_next;
231 } while( poly != NULL );
232 }
234 static void gl_render_poly( struct polygon_struct *poly, gboolean set_depth)
235 {
236 if( poly->vertex_count == 0 )
237 return; /* Culled */
239 bind_texture(poly->tex_id);
240 if( poly->mod_vertex_index == -1 ) {
241 render_set_context( poly->context, set_depth );
242 gl_draw_vertexes(poly);
243 } else {
244 glEnable( GL_STENCIL_TEST );
245 render_set_base_context( poly->context[0], set_depth );
246 render_set_tsp_context( poly->context[0], poly->context[1] );
247 glStencilFunc(GL_EQUAL, 0, 2);
248 gl_draw_vertexes(poly);
250 if( pvr2_scene.shadow_mode == SHADOW_FULL ) {
251 bind_texture(poly->mod_tex_id);
252 render_set_tsp_context( poly->context[0], poly->context[3] );
253 }
254 glStencilFunc(GL_EQUAL, 2, 2);
255 gl_draw_mod_vertexes(poly);
256 glDisable( GL_STENCIL_TEST );
257 }
258 }
261 static void gl_render_modifier_polygon( struct polygon_struct *poly, uint32_t tile_bounds[] )
262 {
263 /* A bit of explanation:
264 * In theory it works like this: generate a 1-bit stencil for each polygon
265 * volume, and then AND or OR it against the overall 1-bit tile stencil at
266 * the end of the volume.
267 *
268 * The implementation here uses a 2-bit stencil buffer, where each volume
269 * is drawn using only stencil bit 0, and then a 'flush' polygon is drawn
270 * to update bit 1 accordingly and clear bit 0.
271 *
272 * This could probably be more efficient, but at least it works correctly
273 * now :)
274 */
276 if( poly->vertex_count == 0 )
277 return; /* Culled */
279 gl_draw_vertexes(poly);
283 int poly_type = POLY1_VOLUME_MODE(poly->context[0]);
284 if( poly_type == PVR2_VOLUME_REGION0 ) {
285 /* 00 => 00
286 * 01 => 00
287 * 10 => 10
288 * 11 => 00
289 */
290 glStencilMask( 0x03 );
291 glStencilFunc(GL_EQUAL, 0x02, 0x03);
292 glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);
293 glDisable( GL_DEPTH_TEST );
295 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
297 glEnable( GL_DEPTH_TEST );
298 glStencilMask( 0x01 );
299 glStencilFunc( GL_ALWAYS, 0, 1 );
300 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
301 } else if( poly_type == PVR2_VOLUME_REGION1 ) {
302 /* This is harder with the standard stencil ops - do it in two passes
303 * 00 => 00 | 00 => 10
304 * 01 => 10 | 01 => 10
305 * 10 => 10 | 10 => 00
306 * 11 => 10 | 11 => 10
307 */
308 glStencilMask( 0x02 );
309 glStencilOp( GL_INVERT, GL_INVERT, GL_INVERT );
310 glDisable( GL_DEPTH_TEST );
312 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
314 glStencilMask( 0x03 );
315 glStencilFunc( GL_NOTEQUAL,0x02, 0x03);
316 glStencilOp( GL_ZERO, GL_REPLACE, GL_REPLACE );
318 drawrect2d( tile_bounds, pvr2_scene.bounds[4] );
320 glEnable( GL_DEPTH_TEST );
321 glStencilMask( 0x01 );
322 glStencilFunc( GL_ALWAYS, 0, 1 );
323 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
324 }
325 }
327 static void gl_render_bkgnd( struct polygon_struct *poly )
328 {
329 bind_texture(poly->tex_id);
330 render_set_tsp_context( poly->context[0], poly->context[1] );
331 glDisable( GL_DEPTH_TEST );
332 glBlendFunc( GL_ONE, GL_ZERO );
333 gl_draw_vertexes(poly);
334 glEnable( GL_DEPTH_TEST );
335 }
337 void gl_render_triangle( struct polygon_struct *poly, int index )
338 {
339 bind_texture(poly->tex_id);
340 render_set_tsp_context( poly->context[0], poly->context[1] );
341 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index + index, 3 );
343 }
345 void gl_render_tilelist( pvraddr_t tile_entry, gboolean set_depth )
346 {
347 tileentryiter list;
349 FOREACH_TILEENTRY(list, tile_entry) {
350 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
351 if( poly != NULL ) {
352 do {
353 gl_render_poly(poly, set_depth);
354 poly = poly->next;
355 } while( list.strip_count-- > 0 );
356 }
357 }
358 }
360 /**
361 * Render the tilelist with depthbuffer updates only.
362 */
363 static void gl_render_tilelist_depthonly( pvraddr_t tile_entry )
364 {
365 tileentryiter list;
367 FOREACH_TILEENTRY(list, tile_entry) {
368 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
369 if( poly != NULL ) {
370 do {
371 render_set_base_context(poly->context[0],TRUE);
372 gl_draw_vertexes(poly);
373 poly = poly->next;
374 } while( list.strip_count-- > 0 );
375 }
376 }
377 }
379 static void gl_render_modifier_tilelist( pvraddr_t tile_entry, uint32_t tile_bounds[] )
380 {
381 tileentryiter list;
383 if( !IS_TILE_PTR(tile_entry) )
384 return;
386 glEnable( GL_STENCIL_TEST );
387 glEnable( GL_DEPTH_TEST );
388 glStencilFunc( GL_ALWAYS, 0, 1 );
389 glStencilOp( GL_KEEP,GL_INVERT, GL_KEEP );
390 glStencilMask( 0x01 );
391 glDepthFunc( GL_LEQUAL );
392 glDepthMask( GL_FALSE );
394 FOREACH_TILEENTRY(list, tile_entry ) {
395 struct polygon_struct *poly = pvr2_scene.buf_to_poly_map[TILEENTRYITER_POLYADDR(list)];
396 if( poly != NULL ) {
397 do {
398 gl_render_modifier_polygon( poly, tile_bounds );
399 poly = poly->next;
400 } while( list.strip_count-- > 0 );
401 }
402 }
403 glDepthMask( GL_TRUE );
404 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
405 glDisable( GL_STENCIL_TEST );
406 }
408 /**
409 * Render the currently defined scene in pvr2_scene
410 */
411 void pvr2_scene_render( render_buffer_t buffer )
412 {
413 /* Scene setup */
414 struct timeval start_tv, tex_tv, end_tv;
416 gettimeofday(&start_tv, NULL);
417 display_driver->set_render_target(buffer);
418 pvr2_check_palette_changed();
419 pvr2_scene_load_textures();
420 currentTexId = -1;
422 gettimeofday( &tex_tv, NULL );
423 uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
424 (tex_tv.tv_usec - start_tv.tv_usec)/1000;
425 DEBUG( "Scene setup in %dms", ms );
427 /* Setup view projection matrix */
428 glMatrixMode(GL_PROJECTION);
429 glLoadIdentity();
430 float nearz = pvr2_scene.bounds[4];
431 float farz = pvr2_scene.bounds[5];
432 if( nearz == farz ) {
433 farz*= 4.0;
434 }
435 glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0,
436 -farz, -nearz );
437 float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
438 glAlphaFunc( GL_GEQUAL, alphaRef );
440 /* Clear the buffer (FIXME: May not want always want to do this) */
441 glDisable( GL_SCISSOR_TEST );
442 glDepthMask( GL_TRUE );
443 glStencilMask( 0x03 );
444 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
446 /* Setup vertex array pointers */
447 glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
448 glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
449 glTexCoordPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
450 glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
451 glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
452 /* Turn on the shaders (if available) */
453 glsl_set_shader(DEFAULT_PROGRAM);
455 /* Render the background */
456 gl_render_bkgnd( pvr2_scene.bkgnd_poly );
458 glEnable( GL_SCISSOR_TEST );
459 glEnable( GL_COLOR_SUM );
460 glEnable( GL_FOG );
461 glEnable( GL_TEXTURE_2D );
463 /* Process the segment list */
464 struct tile_segment *segment = pvr2_scene.segment_list;
465 do {
466 int tilex = SEGMENT_X(segment->control);
467 int tiley = SEGMENT_Y(segment->control);
469 uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
470 if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
471 continue; // fully clipped, skip tile
472 }
474 /* Clip to the visible part of the tile */
475 glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3],
476 tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
477 if( display_driver->capabilities.stencil_bits >= 2 &&
478 IS_TILE_PTR(segment->opaquemod_ptr) &&
479 !IS_EMPTY_TILE_LIST(segment->opaquemod_ptr) ) {
480 /* Don't do this unless there's actually some shadow polygons */
482 /* Use colormask instead of drawbuffer for simplicity */
483 glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
484 gl_render_tilelist_depthonly(segment->opaque_ptr);
485 gl_render_modifier_tilelist(segment->opaquemod_ptr, tile_bounds);
486 glClear( GL_DEPTH_BUFFER_BIT );
487 glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
488 }
489 gl_render_tilelist(segment->opaque_ptr,TRUE);
490 if( IS_TILE_PTR(segment->punchout_ptr) ) {
491 glEnable(GL_ALPHA_TEST );
492 glDepthFunc(GL_GEQUAL);
493 gl_render_tilelist(segment->punchout_ptr, FALSE );
494 glDisable(GL_ALPHA_TEST );
495 }
497 if( IS_TILE_PTR(segment->trans_ptr) ) {
498 if( pvr2_scene.sort_mode == SORT_NEVER ||
499 (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
500 gl_render_tilelist(segment->trans_ptr, TRUE);
501 } else {
502 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
503 }
504 }
505 } while( !IS_LAST_SEGMENT(segment++) );
506 glDisable( GL_SCISSOR_TEST );
507 glDisable( GL_COLOR_SUM );
508 glDisable( GL_FOG );
509 glsl_clear_shader();
511 gettimeofday( &end_tv, NULL );
512 ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
513 (end_tv.tv_usec - tex_tv.tv_usec)/1000;
514 DEBUG( "Scene render in %dms", ms );
515 }
.