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] = {
44 int pvr2_render_colour_format[8] = {
45 COLFMT_BGRA1555, COLFMT_RGB565, COLFMT_BGRA4444, COLFMT_BGRA1555,
46 COLFMT_BGR888, COLFMT_BGRA8888, COLFMT_BGRA8888, COLFMT_BGRA4444 };
50 * Clip the tile bounds to the clipping plane.
51 * @return TRUE if the tile was not clipped completely.
53 static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
55 if( tile[0] < clip[0] ) tile[0] = clip[0];
56 if( tile[1] > clip[1] ) tile[1] = clip[1];
57 if( tile[2] < clip[2] ) tile[2] = clip[2];
58 if( tile[3] > clip[3] ) tile[3] = clip[3];
59 return tile[0] < tile[1] && tile[2] < tile[3];
62 void pvr2_scene_load_textures()
65 for( i=0; i < pvr2_scene.poly_count; i++ ) {
66 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
67 if( POLY1_TEXTURED(poly->context[0]) ) {
68 poly->tex_id = texcache_get_texture( poly->context[2],
69 POLY2_TEX_WIDTH(poly->context[1]),
70 POLY2_TEX_HEIGHT(poly->context[1]) );
71 if( poly->mod_vertex_index != -1 ) {
72 poly->mod_tex_id = texcache_get_texture( poly->context[4],
73 POLY2_TEX_WIDTH(poly->context[3]),
74 POLY2_TEX_HEIGHT(poly->context[3]) );
78 poly->mod_tex_id = -1;
86 * Once-off call to setup the OpenGL context.
88 void pvr2_setup_gl_context()
91 if( glsl_is_supported() ) {
92 if( !glsl_load_shaders( glsl_vertex_shader_src, NULL ) ) {
93 WARN( "Unable to load GL shaders" );
97 texcache_gl_init(); // Allocate texture IDs
98 glCullFace( GL_BACK );
100 glEnable( GL_DEPTH_TEST );
101 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
102 glMatrixMode(GL_MODELVIEW);
105 #ifdef HAVE_OPENGL_CLAMP_COLOR
106 if( isGLExtensionSupported("GL_ARB_color_buffer_float") ) {
107 glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE );
108 glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE );
112 glEnableClientState( GL_COLOR_ARRAY );
113 glEnableClientState( GL_VERTEX_ARRAY );
114 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
115 glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
116 glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
118 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
124 * Setup the GL context for the supplied polygon context.
125 * @param context pointer to 3 or 5 words of polygon context
126 * @param modified boolean flag indicating that the modified
127 * version should be used, rather than the normal version.
129 void render_set_context( uint32_t *context, int render_mode )
131 uint32_t poly1 = context[0], poly2, texture;
132 if( render_mode == RENDER_FULLMOD ) {
134 texture = context[4];
137 texture = context[2];
140 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
141 glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
143 switch( POLY1_CULL_MODE(poly1) ) {
146 glDisable( GL_CULL_FACE );
149 glEnable( GL_CULL_FACE );
150 glFrontFace( GL_CW );
153 glEnable( GL_CULL_FACE );
154 glFrontFace( GL_CCW );
158 if( POLY1_SPECULAR(poly1) ) {
159 glEnable(GL_COLOR_SUM);
161 glDisable(GL_COLOR_SUM);
165 if( POLY1_TEXTURED(poly1) ) {
166 glEnable(GL_TEXTURE_2D);
167 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
168 if( POLY2_TEX_CLAMP_U(poly2) ) {
169 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
170 } else if( POLY2_TEX_MIRROR_U(poly2) ) {
171 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB );
173 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
175 if( POLY2_TEX_CLAMP_V(poly2) ) {
176 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
177 } else if( POLY2_TEX_MIRROR_V(poly2) ) {
178 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB );
180 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
183 glDisable( GL_TEXTURE_2D );
186 switch( POLY2_FOG_MODE(poly2) ) {
187 case PVR2_POLY_FOG_LOOKUP:
188 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_lut_colour );
191 case PVR2_POLY_FOG_VERTEX:
192 if( POLY1_SPECULAR(poly1) ) {
193 glFogfv( GL_FOG_COLOR, pvr2_scene.fog_vert_colour );
196 } /* else fallthrough */
202 glShadeModel( POLY1_SHADE_MODEL(poly1) );
204 int srcblend = POLY2_SRC_BLEND(poly2);
205 int destblend = POLY2_DEST_BLEND(poly2);
206 glBlendFunc( srcblend, destblend );
208 if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
209 ERROR( "Accumulation buffer not supported" );
215 static void gl_render_poly( struct polygon_struct *poly )
217 if( poly->tex_id != -1 ) {
218 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
220 render_set_context( poly->context, RENDER_NORMAL );
221 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
225 static void gl_render_bkgnd( struct polygon_struct *poly )
227 if( poly->tex_id != -1 ) {
228 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
230 render_set_context( poly->context, RENDER_NORMAL );
231 glDisable( GL_DEPTH_TEST );
232 glDisable( GL_CULL_FACE );
233 glBlendFunc( GL_ONE, GL_ZERO );
234 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
235 glEnable( GL_CULL_FACE );
236 glEnable( GL_DEPTH_TEST );
241 void gl_render_tilelist( pvraddr_t tile_entry )
243 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
245 struct polygon_struct *poly;
248 uint32_t entry = *tile_list++;
249 switch( entry >> 28 ) {
251 return; // End-of-list
253 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
255 case 0x08: case 0x09: case 0x0A: case 0x0B:
256 strip_count = ((entry >> 25) & 0x0F)+1;
257 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
258 while( strip_count > 0 ) {
259 assert( poly != NULL );
260 gl_render_poly( poly );
266 if( entry & 0x7E000000 ) {
267 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
268 gl_render_poly( poly );
276 * Render the currently defined scene in pvr2_scene
278 void pvr2_scene_render( render_buffer_t buffer )
281 struct timeval start_tv, tex_tv, end_tv;
283 gettimeofday(&start_tv, NULL);
284 display_driver->set_render_target(buffer);
285 pvr2_check_palette_changed();
286 pvr2_scene_load_textures();
288 gettimeofday( &tex_tv, NULL );
289 uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
290 (tex_tv.tv_usec - start_tv.tv_usec)/1000;
291 DEBUG( "Scene setup in %dms", ms );
293 /* Setup view projection matrix */
294 glMatrixMode(GL_PROJECTION);
296 float nearz = pvr2_scene.bounds[4];
297 float farz = pvr2_scene.bounds[5];
298 if( nearz == farz ) {
301 glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0,
303 float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
304 glAlphaFunc( GL_GEQUAL, alphaRef );
306 /* Clear the buffer (FIXME: May not want always want to do this) */
307 glDisable( GL_SCISSOR_TEST );
308 glDepthMask( GL_TRUE );
309 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
311 /* Setup vertex array pointers */
312 glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
313 glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
314 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
315 glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
316 glFogCoordPointerEXT(GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba[3] );
317 glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
318 glFogi(GL_FOG_MODE, GL_LINEAR);
319 glFogf(GL_FOG_START, 0.0);
320 glFogf(GL_FOG_END, 1.0);
321 /* Turn on the shaders (if available) */
322 glsl_enable_shaders(TRUE);
324 /* Render the background */
325 gl_render_bkgnd( pvr2_scene.bkgnd_poly );
327 glEnable( GL_SCISSOR_TEST );
329 /* Process the segment list */
330 struct tile_segment *segment = pvr2_scene.segment_list;
332 int tilex = SEGMENT_X(segment->control);
333 int tiley = SEGMENT_Y(segment->control);
335 uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
336 if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
337 continue; // fully clipped, skip tile
340 /* Clip to the visible part of the tile */
341 glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3],
342 tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
343 if( IS_TILE_PTR(segment->opaque_ptr) ) {
344 gl_render_tilelist(segment->opaque_ptr);
346 if( IS_TILE_PTR(segment->trans_ptr) ) {
347 if( pvr2_scene.sort_mode == SORT_NEVER ||
348 (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
349 gl_render_tilelist(segment->trans_ptr);
351 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
354 if( IS_TILE_PTR(segment->punchout_ptr) ) {
355 glEnable(GL_ALPHA_TEST );
356 render_autosort_tile(segment->punchout_ptr, RENDER_NORMAL );
357 glDisable(GL_ALPHA_TEST );
359 } while( !IS_LAST_SEGMENT(segment++) );
360 glDisable( GL_SCISSOR_TEST );
362 glsl_enable_shaders(FALSE);
364 gettimeofday( &end_tv, NULL );
365 ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
366 (end_tv.tv_usec - tex_tv.tv_usec)/1000;
367 DEBUG( "Scene render in %dms", ms );
.