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"
24 int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
25 GL_GREATER, GL_NOTEQUAL, GL_GEQUAL,
27 int pvr2_poly_srcblend[8] = {
28 GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
29 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
30 GL_ONE_MINUS_DST_ALPHA };
31 int pvr2_poly_dstblend[8] = {
32 GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
33 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
34 GL_ONE_MINUS_DST_ALPHA };
35 int pvr2_poly_texblend[4] = {
41 int pvr2_render_colour_format[8] = {
42 COLFMT_BGRA1555, COLFMT_RGB565, COLFMT_BGRA4444, COLFMT_BGRA1555,
43 COLFMT_BGR888, COLFMT_BGRA8888, COLFMT_BGRA8888, COLFMT_BGRA4444 };
47 * Clip the tile bounds to the clipping plane.
48 * @return TRUE if the tile was not clipped completely.
50 static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
52 if( tile[0] < clip[0] ) tile[0] = clip[0];
53 if( tile[1] > clip[1] ) tile[1] = clip[1];
54 if( tile[2] < clip[2] ) tile[2] = clip[2];
55 if( tile[3] > clip[3] ) tile[3] = clip[3];
56 return tile[0] < tile[1] && tile[2] < tile[3];
59 void pvr2_scene_load_textures()
62 for( i=0; i < pvr2_scene.poly_count; i++ ) {
63 struct polygon_struct *poly = &pvr2_scene.poly_array[i];
64 if( POLY1_TEXTURED(poly->context[0]) ) {
65 poly->tex_id = texcache_get_texture( poly->context[2],
66 POLY2_TEX_WIDTH(poly->context[1]),
67 POLY2_TEX_HEIGHT(poly->context[1]) );
68 if( poly->mod_vertex_index != -1 ) {
69 poly->mod_tex_id = texcache_get_texture( poly->context[4],
70 POLY2_TEX_WIDTH(poly->context[3]),
71 POLY2_TEX_HEIGHT(poly->context[3]) );
75 poly->mod_tex_id = -1;
83 * Once-off call to setup the OpenGL context.
85 void pvr2_setup_gl_context()
87 texcache_gl_init(); // Allocate texture IDs
88 glCullFace( GL_BACK );
90 glEnable( GL_DEPTH_TEST );
91 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
92 glMatrixMode(GL_MODELVIEW);
95 glEnableClientState( GL_COLOR_ARRAY );
96 glEnableClientState( GL_VERTEX_ARRAY );
97 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
98 glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
100 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
106 * Setup the GL context for the supplied polygon context.
107 * @param context pointer to 3 or 5 words of polygon context
108 * @param modified boolean flag indicating that the modified
109 * version should be used, rather than the normal version.
111 void render_set_context( uint32_t *context, int render_mode )
113 uint32_t poly1 = context[0], poly2, texture;
114 if( render_mode == RENDER_FULLMOD ) {
116 texture = context[4];
119 texture = context[2];
122 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
123 glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
125 switch( POLY1_CULL_MODE(poly1) ) {
128 glDisable( GL_CULL_FACE );
131 glEnable( GL_CULL_FACE );
132 glFrontFace( GL_CW );
135 glEnable( GL_CULL_FACE );
136 glFrontFace( GL_CCW );
140 if( POLY1_SPECULAR(poly1) ) {
141 glEnable(GL_COLOR_SUM);
143 glDisable(GL_COLOR_SUM);
147 if( POLY1_TEXTURED(poly1) ) {
148 int width = POLY2_TEX_WIDTH(poly2);
149 int height = POLY2_TEX_HEIGHT(poly2);
150 glEnable(GL_TEXTURE_2D);
151 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pvr2_poly_texblend[POLY2_TEX_BLEND(poly2)] );
152 if( POLY2_TEX_CLAMP_U(poly2) ) {
153 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
154 } else if( POLY2_TEX_MIRROR_U(poly2) ) {
155 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB );
157 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
159 if( POLY2_TEX_CLAMP_V(poly2) ) {
160 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
161 } else if( POLY2_TEX_MIRROR_V(poly2) ) {
162 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB );
164 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
167 glDisable( GL_TEXTURE_2D );
170 glShadeModel( POLY1_SHADE_MODEL(poly1) );
172 int srcblend = POLY2_SRC_BLEND(poly2);
173 int destblend = POLY2_DEST_BLEND(poly2);
174 glBlendFunc( srcblend, destblend );
176 if( POLY2_SRC_BLEND_TARGET(poly2) || POLY2_DEST_BLEND_TARGET(poly2) ) {
177 ERROR( "Accumulation buffer not supported" );
183 static void gl_render_poly( struct polygon_struct *poly )
185 if( poly->tex_id != -1 ) {
186 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
188 render_set_context( poly->context, RENDER_NORMAL );
189 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
192 void gl_render_tilelist( pvraddr_t tile_entry )
194 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
196 struct polygon_struct *poly;
199 uint32_t entry = *tile_list++;
200 switch( entry >> 28 ) {
202 return; // End-of-list
204 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
206 case 0x08: case 0x09: case 0x0A: case 0x0B:
207 strip_count = ((entry >> 25) & 0x0F)+1;
208 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
209 while( strip_count > 0 ) {
210 assert( poly != NULL );
211 gl_render_poly( poly );
217 if( entry & 0x7E000000 ) {
218 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
219 gl_render_poly( poly );
227 * Render the currently defined scene in pvr2_scene
229 void pvr2_scene_render( render_buffer_t buffer )
232 struct timeval start_tv, tex_tv, end_tv;
234 gettimeofday(&start_tv, NULL);
235 display_driver->set_render_target(buffer);
236 pvr2_check_palette_changed();
237 pvr2_scene_load_textures();
239 gettimeofday( &tex_tv, NULL );
240 uint32_t ms = (tex_tv.tv_sec - start_tv.tv_sec) * 1000 +
241 (tex_tv.tv_usec - start_tv.tv_usec)/1000;
242 DEBUG( "Scene setup in %dms", ms );
244 /* Setup view projection matrix */
245 glMatrixMode(GL_PROJECTION);
247 float nearz = pvr2_scene.bounds[4];
248 float farz = pvr2_scene.bounds[5];
249 if( nearz == farz ) {
252 glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0,
254 float alphaRef = ((float)(MMIO_READ(PVR2, RENDER_ALPHA_REF)&0xFF)+1)/256.0;
255 glAlphaFunc( GL_GEQUAL, alphaRef );
257 /* Clear the buffer (FIXME: May not want always want to do this) */
258 glDisable( GL_SCISSOR_TEST );
259 glDepthMask( GL_TRUE );
260 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
262 /* Setup vertex array pointers */
263 glInterleavedArrays(GL_T2F_C4UB_V3F, sizeof(struct vertex_struct), pvr2_scene.vertex_array);
264 glSecondaryColorPointerEXT(4, GL_UNSIGNED_BYTE, sizeof(struct vertex_struct), &pvr2_scene.vertex_array[0].offset_rgba );
266 uint32_t bgplane_mode = MMIO_READ(PVR2, RENDER_BGPLANE);
267 uint32_t *bgplane = pvr2_scene.pvr2_pbuf + (((bgplane_mode & 0x00FFFFFF)) >> 3) ;
268 render_backplane( bgplane, pvr2_scene.buffer_width, pvr2_scene.buffer_height, bgplane_mode );
270 glEnable( GL_SCISSOR_TEST );
272 /* Process the segment list */
273 struct tile_segment *segment = pvr2_scene.segment_list;
275 int tilex = SEGMENT_X(segment->control);
276 int tiley = SEGMENT_Y(segment->control);
278 int tile_bounds[4] = { tilex << 5, (tilex+1)<<5, tiley<<5, (tiley+1)<<5 };
279 if( !clip_tile_bounds(tile_bounds, pvr2_scene.bounds) ) {
280 continue; // fully clipped, skip tile
283 /* Clip to the visible part of the tile */
284 glScissor( tile_bounds[0], pvr2_scene.buffer_height-tile_bounds[3],
285 tile_bounds[1]-tile_bounds[0], tile_bounds[3] - tile_bounds[2] );
286 if( IS_TILE_PTR(segment->opaque_ptr) ) {
287 gl_render_tilelist(segment->opaque_ptr);
289 if( IS_TILE_PTR(segment->trans_ptr) ) {
290 if( pvr2_scene.sort_mode == SORT_NEVER ||
291 (pvr2_scene.sort_mode == SORT_TILEFLAG && (segment->control&SEGMENT_SORT_TRANS))) {
292 gl_render_tilelist(segment->trans_ptr);
294 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
297 if( IS_TILE_PTR(segment->punchout_ptr) ) {
298 glEnable(GL_ALPHA_TEST );
299 render_autosort_tile(segment->punchout_ptr, RENDER_NORMAL );
300 glDisable(GL_ALPHA_TEST );
302 } while( !IS_LAST_SEGMENT(segment++) );
303 glDisable( GL_SCISSOR_TEST );
305 gettimeofday( &end_tv, NULL );
306 ms = (end_tv.tv_sec - tex_tv.tv_sec) * 1000 +
307 (end_tv.tv_usec - tex_tv.tv_usec)/1000;
308 DEBUG( "Scene render in %dms", ms );
.