filename | src/pvr2/glrender.c |
changeset | 736:a02d1475ccfd |
prev | 687:6bdc2b7032ea |
next | 847:2089244671d2 |
author | nkeynes |
date | Thu Aug 07 23:53:17 2008 +0000 (15 years ago) |
permissions | -rw-r--r-- |
last change | Add ability to bind a render buffer to a texture, with output going to the texture. (RTT work in progress) |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
3 *
4 * Standard OpenGL rendering engine.
5 *
6 * Copyright (c) 2005 Nathan Keynes.
7 *
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.
12 *
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.
17 */
19 #include <assert.h>
20 #include <sys/time.h>
21 #include "display.h"
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,
29 GL_ALWAYS };
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] = {
39 GL_REPLACE,
40 GL_MODULATE,
41 GL_DECAL,
42 GL_MODULATE
43 };
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 };
49 /**
50 * Clip the tile bounds to the clipping plane.
51 * @return TRUE if the tile was not clipped completely.
52 */
53 static gboolean clip_tile_bounds( uint32_t *tile, float *clip )
54 {
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];
60 }
62 void pvr2_scene_load_textures()
63 {
64 int i;
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]) );
75 }
76 } else {
77 poly->tex_id = -1;
78 poly->mod_tex_id = -1;
79 }
80 }
81 }
85 /**
86 * Once-off call to setup the OpenGL context.
87 */
88 void pvr2_setup_gl_context()
89 {
91 if( glsl_is_supported() ) {
92 if( !glsl_load_shaders( glsl_vertex_shader_src, NULL ) ) {
93 WARN( "Unable to load GL shaders" );
94 }
95 }
97 texcache_gl_init(); // Allocate texture IDs
98 glCullFace( GL_BACK );
99 glEnable( GL_BLEND );
100 glEnable( GL_DEPTH_TEST );
101 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
102 glMatrixMode(GL_MODELVIEW);
103 glLoadIdentity();
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 );
109 }
110 #endif
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);
118 glClearDepth(0);
119 glClearStencil(0);
120 }
122 /**
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.
127 */
128 void render_set_context( uint32_t *context, int render_mode )
129 {
130 uint32_t poly1 = context[0], poly2, texture;
131 if( render_mode == RENDER_FULLMOD ) {
132 poly2 = context[3];
133 texture = context[4];
134 } else {
135 poly2 = context[1];
136 texture = context[2];
137 }
139 glDepthFunc( POLY1_DEPTH_MODE(poly1) );
140 glDepthMask( POLY1_DEPTH_WRITE(poly1) ? GL_TRUE : GL_FALSE );
142 switch( POLY1_CULL_MODE(poly1) ) {
143 case CULL_NONE:
144 case CULL_SMALL:
145 glDisable( GL_CULL_FACE );
146 break;
147 case CULL_CCW:
148 glEnable( GL_CULL_FACE );
149 glFrontFace( GL_CW );
150 break;
151 case CULL_CW:
152 glEnable( GL_CULL_FACE );
153 glFrontFace( GL_CCW );
154 break;
155 }
157 if( POLY1_SPECULAR(poly1) ) {
158 glEnable(GL_COLOR_SUM);
159 } else {
160 glDisable(GL_COLOR_SUM);
161 }
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 );
171 } else {
172 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
173 }
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 );
178 } else {
179 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
180 }
181 } else {
182 glDisable( GL_TEXTURE_2D );
183 }
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" );
193 }
195 }
198 static void gl_render_poly( struct polygon_struct *poly )
199 {
200 if( poly->tex_id != -1 ) {
201 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
202 }
203 render_set_context( poly->context, RENDER_NORMAL );
204 glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index, poly->vertex_count );
205 }
208 static void gl_render_bkgnd( struct polygon_struct *poly )
209 {
210 if( poly->tex_id != -1 ) {
211 glBindTexture(GL_TEXTURE_2D, poly->tex_id);
212 }
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 );
220 }
224 void gl_render_tilelist( pvraddr_t tile_entry )
225 {
226 uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
227 int strip_count;
228 struct polygon_struct *poly;
230 while(1) {
231 uint32_t entry = *tile_list++;
232 switch( entry >> 28 ) {
233 case 0x0F:
234 return; // End-of-list
235 case 0x0E:
236 tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
237 break;
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 );
244 poly = poly->next;
245 strip_count--;
246 }
247 break;
248 default:
249 if( entry & 0x7E000000 ) {
250 poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
251 gl_render_poly( poly );
252 }
253 }
254 }
255 }
258 /**
259 * Render the currently defined scene in pvr2_scene
260 */
261 void pvr2_scene_render( render_buffer_t buffer )
262 {
263 /* Scene setup */
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);
278 glLoadIdentity();
279 float nearz = pvr2_scene.bounds[4];
280 float farz = pvr2_scene.bounds[5];
281 if( nearz == farz ) {
282 farz*= 4.0;
283 }
284 glOrtho( 0, pvr2_scene.buffer_width, pvr2_scene.buffer_height, 0,
285 -farz, -nearz );
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;
310 do {
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
317 }
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);
324 }
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);
329 } else {
330 render_autosort_tile(segment->trans_ptr, RENDER_NORMAL );
331 }
332 }
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 );
337 }
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 );
347 }
.