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.
21 #include "pvr2/pvr2.h"
22 #include "pvr2/scene.h"
23 #include "pvr2/glutil.h"
25 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
26 GL_GREATER, GL_NOTEQUAL, GL_GEQUAL,
28 int pvr2_poly_srcblend[8] = {
29 GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
30 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
31 GL_ONE_MINUS_DST_ALPHA };
32 int pvr2_poly_dstblend[8] = {
33 GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
34 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
35 GL_ONE_MINUS_DST_ALPHA };
36 int pvr2_poly_texblend[4] = {
42 int pvr2_render_colour_format[8] = {
43 COLFMT_BGRA1555, COLFMT_RGB565, COLFMT_BGRA4444, COLFMT_BGRA1555,
44 COLFMT_BGR888, COLFMT_BGRA8888, COLFMT_BGRA8888, COLFMT_BGRA4444 };
48 * Clip the tile bounds to the clipping plane.
49 * @return TRUE if the tile was not clipped completely.
51 static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
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];
60 void pvr2_scene_load_textures()
63 for( i=0; i < pvr2_scene.poly_count; i++ ) {
64 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
65 if( POLY1_TEXTURED(poly->context[0]) ) {
66 poly->tex_id = texcache_get_texture( poly->context[2],
67 POLY2_TEX_WIDTH(poly->context[1]),
68 POLY2_TEX_HEIGHT(poly->context[1]) );
69 if( poly->mod_vertex_index != -1 ) {
70 poly->mod_tex_id = texcache_get_texture( poly->context[4],
71 POLY2_TEX_WIDTH(poly->context[3]),
72 POLY2_TEX_HEIGHT(poly->context[3]) );
76 poly->mod_tex_id = -1;
84 * Once-off call to setup the OpenGL context.
86 void pvr2_setup_gl_context()
89 if( glsl_is_supported() ) {
90 if( !glsl_load_shaders( glsl_vertex_shader_src, NULL ) ) {
91 WARN( "Unable to load GL shaders" );
95 texcache_gl_init(); // Allocate texture IDs
96 glCullFace( GL_BACK );
98 glEnable( GL_DEPTH_TEST );
99 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
100 glMatrixMode(GL_MODELVIEW);
103 glEnableClientState( GL_COLOR_ARRAY );
104 glEnableClientState( GL_VERTEX_ARRAY );
105 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
106 glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
108 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
114 * Setup the GL context for the supplied polygon context.
115 * @param context pointer to 3 or 5 words of polygon context
116 * @param modified boolean flag indicating that the modified
117 * version should be used, rather than the normal version.
119 void render_set_context( uint32_t *context, int render_mode )
121 uint32_t poly1 = context[0], poly2, texture;
122 if( render_mode == RENDER_FULLMOD ) {
124 texture = context[4];
127 texture = context[2];
130 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
131 glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
133 switch( POLY1_CULL_MODE(poly1) ) {
136 glDisable( GL_CULL_FACE );
139 glEnable( GL_CULL_FACE );
140 glFrontFace( GL_CW );
143 glEnable( GL_CULL_FACE );
144 glFrontFace( GL_CCW );
148 if( POLY1_SPECULAR(poly1) ) {
149 glEnable(GL_COLOR_SUM);
151 glDisable(GL_COLOR_SUM);
155 if( POLY1_TEXTURED(poly1) ) {
156 int width = POLY2_TEX_WIDTH(poly2);
157 int height = POLY2_TEX_HEIGHT(poly2);
158 glEnable(GL_TEXTURE_2D);
159 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
160 if( POLY2_TEX_CLAMP_U(poly2) ) {
161 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
162 } else if( POLY2_TEX_MIRROR_U(poly2) ) {
163 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB );
165 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
167 if( POLY2_TEX_CLAMP_V(poly2) ) {
168 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
169 } else if( POLY2_TEX_MIRROR_V(poly2) ) {
170 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB );
172 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
175 glDisable( GL_TEXTURE_2D );
178 glShadeModel( POLY1_SHADE_MODEL(poly1) );
180 int srcblend = POLY2_SRC_BLEND(poly2);
181 int destblend = POLY2_DEST_BLEND(poly2);
182 glBlendFunc( srcblend, destblend );
184 if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
185 ERROR( "Accumulation buffer not supported" );
191 static void gl_render_poly( struct polygon_struct *poly )
193 if( poly->tex_id != -1 ) {
194 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
196 render_set_context( poly->context, RENDER_NORMAL );
197 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
200 void gl_render_tilelist( pvraddr_t tile_entry )
202 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
204 struct polygon_struct *poly;
207 uint32_t entry = *tile_list++;
208 switch( entry >> 28 ) {
210 return; // End-of-list
212 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
214 case 0x08: case 0x09: case 0x0A: case 0x0B:
215 strip_count = ((entry >> 25) & 0x0F)+1;
216 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
217 while( strip_count > 0 ) {
218 assert( poly != NULL );
219 gl_render_poly( poly );
225 if( entry & 0x7E000000 ) {
226 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
227 gl_render_poly( poly );
235 * Render the currently defined scene in pvr2_scene
237 void pvr2_scene_render( render_buffer_t buffer )
240 struct timeval start_tv, tex_tv, end_tv;
242 gettimeofday(&start_tv, NULL);
243 display_driver->set_render_target(buffer);
244 pvr2_check_palette_changed();
245 pvr2_scene_load_textures();
247 gettimeofday( &tex_tv, NULL );
248 uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
249 (tex_tv.tv_usec - start_tv.tv_usec)/1000;
250 DEBUG( "Scene setup in %dms", ms );
252 /* Setup view projection matrix */
253 glMatrixMode(GL_PROJECTION);
255 float nearz = pvr2_scene.bounds[4];
256 float farz = pvr2_scene.bounds[5];
257 if( nearz == farz ) {
260 glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0,
262 float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
263 glAlphaFunc( GL_GEQUAL, alphaRef );
265 /* Clear the buffer (FIXME: May not want always want to do this) */
266 glDisable( GL_SCISSOR_TEST );
267 glDepthMask( GL_TRUE );
268 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
270 /* Setup vertex array pointers */
271 glInterleavedArrays(GL_T2F_C4UB_V3F, sizeof(struct vertex_struct), pvr2_scene.vertex_array);
272 glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba );
274 uint32_t bgplane_mode = MMIO_READ(PVR2, RENDER_BGPLANE);
275 uint32_t *bgplane = pvr2_scene.pvr2_pbuf + (((bgplane_mode & 0x00FFFFFF)) >> 3) ;
276 render_backplane( bgplane, pvr2_scene.buffer_width, pvr2_scene.buffer_height, bgplane_mode );
278 glEnable( GL_SCISSOR_TEST );
280 /* Process the segment list */
281 struct tile_segment *segment = pvr2_scene.segment_list;
283 int tilex = SEGMENT_X(segment->control);
284 int tiley = SEGMENT_Y(segment->control);
286 int tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
287 if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
288 continue; // fully clipped, skip tile
291 /* Clip to the visible part of the tile */
292 glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3],
293 tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
294 if( IS_TILE_PTR(segment->opaque_ptr) ) {
295 gl_render_tilelist(segment->opaque_ptr);
297 if( IS_TILE_PTR(segment->trans_ptr) ) {
298 if( pvr2_scene.sort_mode == SORT_NEVER ||
299 (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
300 gl_render_tilelist(segment->trans_ptr);
302 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
305 if( IS_TILE_PTR(segment->punchout_ptr) ) {
306 glEnable(GL_ALPHA_TEST );
307 render_autosort_tile(segment->punchout_ptr, RENDER_NORMAL );
308 glDisable(GL_ALPHA_TEST );
310 } while( !IS_LAST_SEGMENT(segment++) );
311 glDisable( GL_SCISSOR_TEST );
313 gettimeofday( &end_tv, NULL );
314 ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
315 (end_tv.tv_usec - tex_tv.tv_usec)/1000;
316 DEBUG( "Scene render in %dms", ms );
.