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 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
28 GL_GREATER, GL_NOTEQUAL, GL_GEQUAL,
30 int pvr2_poly_srcblend[8] = {
31 GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
32 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
33 GL_ONE_MINUS_DST_ALPHA };
34 int pvr2_poly_dstblend[8] = {
35 GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
36 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
37 GL_ONE_MINUS_DST_ALPHA };
38 int pvr2_poly_texblend[4] = {
46 * Clip the tile bounds to the clipping plane.
47 * @return TRUE if the tile was not clipped completely.
49 static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
51 if( tile[0] < clip[0] ) tile[0] = clip[0];
52 if( tile[1] > clip[1] ) tile[1] = clip[1];
53 if( tile[2] < clip[2] ) tile[2] = clip[2];
54 if( tile[3] > clip[3] ) tile[3] = clip[3];
55 return tile[0] < tile[1] && tile[2] < tile[3];
58 void pvr2_scene_load_textures()
61 for( i=0; i < pvr2_scene.poly_count; i++ ) {
62 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
63 if( POLY1_TEXTURED(poly->context[0]) ) {
64 poly->tex_id = texcache_get_texture( poly->context[2],
65 POLY2_TEX_WIDTH(poly->context[1]),
66 POLY2_TEX_HEIGHT(poly->context[1]) );
67 if( poly->mod_vertex_index != -1 ) {
68 poly->mod_tex_id = texcache_get_texture( poly->context[4],
69 POLY2_TEX_WIDTH(poly->context[3]),
70 POLY2_TEX_HEIGHT(poly->context[3]) );
74 poly->mod_tex_id = -1;
82 * Once-off call to setup the OpenGL context.
84 void pvr2_setup_gl_context()
87 if( glsl_is_supported() ) {
88 if( !glsl_load_shaders( glsl_vertex_shader_src, NULL ) ) {
89 WARN( "Unable to load GL shaders" );
93 texcache_gl_init(); // Allocate texture IDs
94 glCullFace( GL_BACK );
96 glEnable( GL_DEPTH_TEST );
97 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
98 glMatrixMode(GL_MODELVIEW);
101 #ifdef HAVE_OPENGL_CLAMP_COLOR
102 if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
103 glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
104 glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
108 glEnableClientState( GL_COLOR_ARRAY );
109 glEnableClientState( GL_VERTEX_ARRAY );
110 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
111 glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
112 glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
114 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
120 * Setup the GL context for the supplied polygon context.
121 * @param context pointer to 3 or 5 words of polygon context
122 * @param modified boolean flag indicating that the modified
123 * version should be used, rather than the normal version.
125 void render_set_context( uint32_t *context, int render_mode )
127 uint32_t poly1 = context[0], poly2, texture;
128 if( render_mode == RENDER_FULLMOD ) {
130 texture = context[4];
133 texture = context[2];
136 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
137 glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
139 switch( POLY1_CULL_MODE(poly1) ) {
142 glDisable( GL_CULL_FACE );
145 glEnable( GL_CULL_FACE );
146 glFrontFace( GL_CW );
149 glEnable( GL_CULL_FACE );
150 glFrontFace( GL_CCW );
154 if( POLY1_SPECULAR(poly1) ) {
155 glEnable(GL_COLOR_SUM);
157 glDisable(GL_COLOR_SUM);
161 if( POLY1_TEXTURED(poly1) ) {
162 glEnable(GL_TEXTURE_2D);
163 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
164 if( POLY2_TEX_CLAMP_U(poly2) ) {
165 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
166 } else if( POLY2_TEX_MIRROR_U(poly2) ) {
167 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB );
169 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
171 if( POLY2_TEX_CLAMP_V(poly2) ) {
172 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
173 } else if( POLY2_TEX_MIRROR_V(poly2) ) {
174 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB );
176 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
179 glDisable( GL_TEXTURE_2D );
182 switch( POLY2_FOG_MODE(poly2) ) {
183 case PVR2_POLY_FOG_LOOKUP:
184 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
187 case PVR2_POLY_FOG_VERTEX:
188 if( POLY1_SPECULAR(poly1) ) {
189 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
192 } /* else fallthrough */
198 glShadeModel( POLY1_SHADE_MODEL(poly1) );
200 int srcblend = POLY2_SRC_BLEND(poly2);
201 int destblend = POLY2_DEST_BLEND(poly2);
202 glBlendFunc( srcblend, destblend );
204 if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
205 ERROR( "Accumulation buffer not supported" );
211 static void gl_render_poly( struct polygon_struct *poly )
213 if( poly->tex_id != -1 ) {
214 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
216 render_set_context( poly->context, RENDER_NORMAL );
217 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
221 static void gl_render_bkgnd( struct polygon_struct *poly )
223 if( poly->tex_id != -1 ) {
224 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
226 render_set_context( poly->context, RENDER_NORMAL );
227 glDisable( GL_DEPTH_TEST );
228 glDisable( GL_CULL_FACE );
229 glBlendFunc( GL_ONE, GL_ZERO );
230 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
231 glEnable( GL_CULL_FACE );
232 glEnable( GL_DEPTH_TEST );
237 void gl_render_tilelist( pvraddr_t tile_entry )
239 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
241 struct polygon_struct *poly;
244 uint32_t entry = *tile_list++;
245 switch( entry >> 28 ) {
247 return; // End-of-list
249 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
251 case 0x08: case 0x09: case 0x0A: case 0x0B:
252 strip_count = ((entry >> 25) & 0x0F)+1;
253 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
254 while( strip_count > 0 ) {
255 assert( poly != NULL );
256 gl_render_poly( poly );
262 if( entry & 0x7E000000 ) {
263 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
264 gl_render_poly( poly );
272 * Render the currently defined scene in pvr2_scene
274 void pvr2_scene_render( render_buffer_t buffer )
277 struct timeval start_tv, tex_tv, end_tv;
279 gettimeofday(&start_tv, NULL);
280 display_driver->set_render_target(buffer);
281 pvr2_check_palette_changed();
282 pvr2_scene_load_textures();
284 gettimeofday( &tex_tv, NULL );
285 uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
286 (tex_tv.tv_usec - start_tv.tv_usec)/1000;
287 DEBUG( "Scene setup in %dms", ms );
289 /* Setup view projection matrix */
290 glMatrixMode(GL_PROJECTION);
292 float nearz = pvr2_scene.bounds[4];
293 float farz = pvr2_scene.bounds[5];
294 if( nearz == farz ) {
297 glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0,
299 float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
300 glAlphaFunc( GL_GEQUAL, alphaRef );
302 /* Clear the buffer (FIXME: May not want always want to do this) */
303 glDisable( GL_SCISSOR_TEST );
304 glDepthMask( GL_TRUE );
305 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
307 /* Setup vertex array pointers */
308 glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
309 glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
310 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
311 glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
312 glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
313 glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
314 glFogi(GL_FOG_MODE, GL_LINEAR);
315 glFogf(GL_FOG_START, 0.0);
316 glFogf(GL_FOG_END, 1.0);
317 /* Turn on the shaders (if available) */
318 glsl_enable_shaders(TRUE);
320 /* Render the background */
321 gl_render_bkgnd( pvr2_scene.bkgnd_poly );
323 glEnable( GL_SCISSOR_TEST );
325 /* Process the segment list */
326 struct tile_segment *segment = pvr2_scene.segment_list;
328 int tilex = SEGMENT_X(segment->control);
329 int tiley = SEGMENT_Y(segment->control);
331 uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
332 if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
333 continue; // fully clipped, skip tile
336 /* Clip to the visible part of the tile */
337 glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3],
338 tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
339 if( IS_TILE_PTR(segment->opaque_ptr) ) {
340 gl_render_tilelist(segment->opaque_ptr);
342 if( IS_TILE_PTR(segment->trans_ptr) ) {
343 if( pvr2_scene.sort_mode == SORT_NEVER ||
344 (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
345 gl_render_tilelist(segment->trans_ptr);
347 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
350 if( IS_TILE_PTR(segment->punchout_ptr) ) {
351 glEnable(GL_ALPHA_TEST );
352 render_autosort_tile(segment->punchout_ptr, RENDER_NORMAL );
353 glDisable(GL_ALPHA_TEST );
355 } while( !IS_LAST_SEGMENT(segment++) );
356 glDisable( GL_SCISSOR_TEST );
358 glsl_enable_shaders(FALSE);
360 gettimeofday( &end_tv, NULL );
361 ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
362 (end_tv.tv_usec - tex_tv.tv_usec)/1000;
363 DEBUG( "Scene render in %dms", ms );
.