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 );
117 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
123 * Setup the GL context for the supplied polygon context.
124 * @param context pointer to 3 or 5 words of polygon context
125 * @param modified boolean flag indicating that the modified
126 * version should be used, rather than the normal version.
128 void render_set_context( uint32_t *context, int render_mode )
130 uint32_t poly1 = context[0], poly2, texture;
131 if( render_mode == RENDER_FULLMOD ) {
133 texture = context[4];
136 texture = context[2];
139 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
140 glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
142 switch( POLY1_CULL_MODE(poly1) ) {
145 glDisable( GL_CULL_FACE );
148 glEnable( GL_CULL_FACE );
149 glFrontFace( GL_CW );
152 glEnable( GL_CULL_FACE );
153 glFrontFace( GL_CCW );
157 if( POLY1_SPECULAR(poly1) ) {
158 glEnable(GL_COLOR_SUM);
160 glDisable(GL_COLOR_SUM);
164 if( POLY1_TEXTURED(poly1) ) {
165 glEnable(GL_TEXTURE_2D);
166 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
167 if( POLY2_TEX_CLAMP_U(poly2) ) {
168 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
169 } else if( POLY2_TEX_MIRROR_U(poly2) ) {
170 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB );
172 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
174 if( POLY2_TEX_CLAMP_V(poly2) ) {
175 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
176 } else if( POLY2_TEX_MIRROR_V(poly2) ) {
177 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB );
179 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
182 glDisable( GL_TEXTURE_2D );
185 glShadeModel( POLY1_SHADE_MODEL(poly1) );
187 int srcblend = POLY2_SRC_BLEND(poly2);
188 int destblend = POLY2_DEST_BLEND(poly2);
189 glBlendFunc( srcblend, destblend );
191 if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
192 ERROR( "Accumulation buffer not supported" );
198 static void gl_render_poly( struct polygon_struct *poly )
200 if( poly->tex_id != -1 ) {
201 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
203 render_set_context( poly->context, RENDER_NORMAL );
204 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
208 static void gl_render_bkgnd( struct polygon_struct *poly )
210 if( poly->tex_id != -1 ) {
211 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
213 render_set_context( poly->context, RENDER_NORMAL );
214 glDisable( GL_DEPTH_TEST );
215 glDisable( GL_CULL_FACE );
216 glBlendFunc( GL_ONE, GL_ZERO );
217 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
218 glEnable( GL_CULL_FACE );
219 glEnable( GL_DEPTH_TEST );
224 void gl_render_tilelist( pvraddr_t tile_entry )
226 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
228 struct polygon_struct *poly;
231 uint32_t entry = *tile_list++;
232 switch( entry >> 28 ) {
234 return; // End-of-list
236 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
238 case 0x08: case 0x09: case 0x0A: case 0x0B:
239 strip_count = ((entry >> 25) & 0x0F)+1;
240 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
241 while( strip_count > 0 ) {
242 assert( poly != NULL );
243 gl_render_poly( poly );
249 if( entry & 0x7E000000 ) {
250 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
251 gl_render_poly( poly );
259 * Render the currently defined scene in pvr2_scene
261 void pvr2_scene_render( render_buffer_t buffer )
264 struct timeval start_tv, tex_tv, end_tv;
266 gettimeofday(&start_tv, NULL);
267 display_driver->set_render_target(buffer);
268 pvr2_check_palette_changed();
269 pvr2_scene_load_textures();
271 gettimeofday( &tex_tv, NULL );
272 uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
273 (tex_tv.tv_usec - start_tv.tv_usec)/1000;
274 DEBUG( "Scene setup in %dms", ms );
276 /* Setup view projection matrix */
277 glMatrixMode(GL_PROJECTION);
279 float nearz = pvr2_scene.bounds[4];
280 float farz = pvr2_scene.bounds[5];
281 if( nearz == farz ) {
284 glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0,
286 float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
287 glAlphaFunc( GL_GEQUAL, alphaRef );
289 /* Clear the buffer (FIXME: May not want always want to do this) */
290 glDisable( GL_SCISSOR_TEST );
291 glDepthMask( GL_TRUE );
292 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
294 /* Setup vertex array pointers */
295 glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].x);
296 glColorPointer(4, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].rgba[0]);
297 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].u);
298 glSecondaryColorPointerEXT(3, GL_FLOAT, sizeof(struct vertex_struct), pvr2_scene.vertex_array[0].offset_rgba );
300 /* Turn on the shaders (if available) */
301 glsl_enable_shaders(TRUE);
303 /* Render the background */
304 gl_render_bkgnd( pvr2_scene.bkgnd_poly );
306 glEnable( GL_SCISSOR_TEST );
308 /* Process the segment list */
309 struct tile_segment *segment = pvr2_scene.segment_list;
311 int tilex = SEGMENT_X(segment->control);
312 int tiley = SEGMENT_Y(segment->control);
314 uint32_t tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
315 if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
316 continue; // fully clipped, skip tile
319 /* Clip to the visible part of the tile */
320 glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3],
321 tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
322 if( IS_TILE_PTR(segment->opaque_ptr) ) {
323 gl_render_tilelist(segment->opaque_ptr);
325 if( IS_TILE_PTR(segment->trans_ptr) ) {
326 if( pvr2_scene.sort_mode == SORT_NEVER ||
327 (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
328 gl_render_tilelist(segment->trans_ptr);
330 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
333 if( IS_TILE_PTR(segment->punchout_ptr) ) {
334 glEnable(GL_ALPHA_TEST );
335 render_autosort_tile(segment->punchout_ptr, RENDER_NORMAL );
336 glDisable(GL_ALPHA_TEST );
338 } while( !IS_LAST_SEGMENT(segment++) );
339 glDisable( GL_SCISSOR_TEST );
341 glsl_enable_shaders(FALSE);
343 gettimeofday( &end_tv, NULL );
344 ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
345 (end_tv.tv_usec - tex_tv.tv_usec)/1000;
346 DEBUG( "Scene render in %dms", ms );
.