2 * $Id: render.c,v 1.17 2007-01-21 11:28:43 nkeynes Exp $
4 * PVR2 Renderer support. This part is primarily
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.
19 #include "pvr2/pvr2.h"
23 * Describes a rendering buffer that's actually held in GL, for when we need
24 * to fetch the bits back to vram.
26 typedef struct pvr2_render_buffer {
27 sh4addr_t render_addr; /* The actual address rendered to in pvr ram */
28 uint32_t size; /* Length of rendering region in bytes */
31 } *pvr2_render_buffer_t;
33 struct pvr2_render_buffer front_buffer;
34 struct pvr2_render_buffer back_buffer;
36 typedef struct pvr2_bgplane_packed {
37 uint32_t poly_cfg, poly_mode;
38 uint32_t texture_mode;
45 } *pvr2_bgplane_packed_t;
49 void pvr2_render_copy_to_sh4( pvr2_render_buffer_t buffer,
50 gboolean backBuffer );
52 int pvr2_render_font_list = -1;
53 int pvr2_render_trace = 0;
55 int glPrintf( int x, int y, const char *fmt, ... )
57 va_list ap; /* our argument pointer */
60 if (fmt == NULL) /* if there is no string to draw do nothing */
63 len = vsnprintf(buf, sizeof(buf), fmt, ap);
67 glPushAttrib(GL_LIST_BIT);
68 glDisable( GL_DEPTH_TEST );
69 glDisable( GL_BLEND );
70 glDisable( GL_TEXTURE_2D );
71 glDisable( GL_ALPHA_TEST );
72 glDisable( GL_CULL_FACE );
73 glListBase(pvr2_render_font_list - 32);
74 glColor3f( 1.0, 1.0, 1.0 );
75 glRasterPos2i( x, y );
76 glCallLists(len, GL_UNSIGNED_BYTE, buf);
82 void glDrawGrid( int width, int height )
85 glDisable( GL_DEPTH_TEST );
89 glColor4f( 1.0, 1.0, 1.0, 1.0 );
90 for( i=32; i<width; i+=32 ) {
91 glVertex3f( i, 0.0, 3.0 );
92 glVertex3f( i,height-1, 3.0 );
95 for( i=32; i<height; i+=32 ) {
96 glVertex3f( 0.0, i, 3.0 );
97 glVertex3f( width, i, 3.0 );
104 gboolean pvr2_render_init( void )
106 front_buffer.render_addr = -1;
107 back_buffer.render_addr = -1;
111 * Invalidate any caching on the supplied address. Specifically, if it falls
112 * within either the front buffer or back buffer, flush the buffer back to
113 * PVR2 ram (note that front buffer flush may be corrupt under some
116 gboolean pvr2_render_invalidate( sh4addr_t address )
118 address = address & 0x1FFFFFFF;
119 if( front_buffer.render_addr != -1 &&
120 front_buffer.render_addr <= address &&
121 (front_buffer.render_addr + front_buffer.size) > address ) {
122 pvr2_render_copy_to_sh4( &front_buffer, FALSE );
123 front_buffer.render_addr = -1;
125 } else if( back_buffer.render_addr != -1 &&
126 back_buffer.render_addr <= address &&
127 (back_buffer.render_addr + back_buffer.size) > address ) {
128 pvr2_render_copy_to_sh4( &back_buffer, TRUE );
129 back_buffer.render_addr = -1;
136 * Display a rendered frame if one is available.
137 * @param address An address in PVR ram (0500000 range).
138 * @return TRUE if a frame was available to be displayed, otherwise false.
140 gboolean pvr2_render_display_frame( uint32_t address )
142 if( front_buffer.render_addr == address ) {
143 /* Current front buffer is already displayed, so do nothing
144 * and tell the caller that all is well.
148 if( back_buffer.render_addr == address ) {
149 /* The more useful case - back buffer is to be displayed. Swap
152 display_driver->display_back_buffer();
153 front_buffer = back_buffer;
154 back_buffer.render_addr = -1;
161 * Prepare the OpenGL context to receive instructions for a new frame.
163 static void pvr2_render_prepare_context( sh4addr_t render_addr,
164 uint32_t width, uint32_t height,
165 uint32_t colour_format,
167 gboolean texture_target )
169 /* Select and initialize the render context */
170 display_driver->set_render_format( width, height, colour_format, texture_target );
172 if( pvr2_render_font_list == -1 ) {
173 pvr2_render_font_list = video_glx_load_font( "-*-helvetica-*-r-normal--16-*-*-*-p-*-iso8859-1");
176 if( back_buffer.render_addr != -1 &&
177 back_buffer.render_addr != render_addr ) {
178 /* There's a current back buffer, and we're rendering somewhere else -
179 * flush the back buffer back to vram and start a new back buffer
181 pvr2_render_copy_to_sh4( &back_buffer, TRUE );
184 if( front_buffer.render_addr == render_addr ) {
185 /* In case we've been asked to render to the current front buffer -
186 * invalidate the front buffer and render to the back buffer, ensuring
187 * we swap at the next frame display.
189 front_buffer.render_addr = -1;
191 back_buffer.render_addr = render_addr;
192 back_buffer.width = width;
193 back_buffer.height = height;
194 back_buffer.colour_format = colour_format;
195 back_buffer.size = width * height * colour_format_bytes[colour_format];
197 /* Setup the display model */
198 glDrawBuffer(GL_BACK);
199 glShadeModel(GL_SMOOTH);
200 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
201 glViewport( 0, 0, width, height );
202 glMatrixMode(GL_PROJECTION);
204 glOrtho( 0, width, height, 0, bgplanez, -4 );
205 glMatrixMode(GL_MODELVIEW);
207 glCullFace( GL_BACK );
208 glEnable( GL_BLEND );
210 /* Clear out the buffers */
211 glDisable( GL_SCISSOR_TEST );
212 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
214 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
219 * Render a complete scene into the OpenGL back buffer.
220 * Note: this will probably need to be broken up eventually once timings are
223 void pvr2_render_scene( )
225 struct tile_descriptor *tile_desc =
226 (struct tile_descriptor *)mem_get_region(PVR2_RAM_BASE + MMIO_READ( PVR2, RENDER_TILEBASE ));
228 uint32_t render_addr = MMIO_READ( PVR2, RENDER_ADDR1 );
229 gboolean render_to_tex;
230 if( render_addr & 0x01000000 ) {
231 render_addr = (render_addr & 0x00FFFFFF) + PVR2_RAM_BASE_INT;
232 /* Heuristic - if we're rendering to the interlaced region we're
233 * probably creating a texture rather than rendering actual output.
234 * We can optimise for this case a little
236 render_to_tex = TRUE;
237 WARN( "Render to texture not supported properly yet" );
239 render_addr = (render_addr & 0x00FFFFFF) + PVR2_RAM_BASE;
240 render_to_tex = FALSE;
243 float bgplanez = MMIO_READF( PVR2, RENDER_FARCLIP );
244 uint32_t render_mode = MMIO_READ( PVR2, RENDER_MODE );
245 int width = 640; /* FIXME - get this from the tile buffer */
247 int colour_format = pvr2_render_colour_format[render_mode&0x07];
248 pvr2_render_prepare_context( render_addr, width, height, colour_format,
249 bgplanez, render_to_tex );
251 int clip_x = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
252 int clip_y = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
253 int clip_width = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) - clip_x + 1;
254 int clip_height= ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) - clip_y + 1;
256 /* Fog setup goes here */
258 /* Render the background plane */
259 uint32_t bgplane_mode = MMIO_READ(PVR2, RENDER_BGPLANE);
260 uint32_t *display_list =
261 (uint32_t *)mem_get_region(PVR2_RAM_BASE + MMIO_READ( PVR2, RENDER_POLYBASE ));
263 uint32_t *bgplane = display_list + (((bgplane_mode & 0x00FFFFFF)) >> 3) ;
264 render_backplane( bgplane, width, height, bgplane_mode );
266 pvr2_render_tilebuffer( width, height, clip_x, clip_y,
267 clip_x + clip_width, clip_y + clip_height );
269 /* Post-render cleanup and update */
271 /* Add frame, fps, etc data */
272 //glDrawGrid( width, height );
273 glPrintf( 4, 16, "Frame %d", pvr2_get_frame_count() );
274 /* Generate end of render event */
275 asic_event( EVENT_PVR_RENDER_DONE );
276 DEBUG( "Rendered frame %d", pvr2_get_frame_count() );
281 * Flush the indicated render buffer back to PVR. Caller is responsible for
282 * tracking whether there is actually anything in the buffer.
284 * @param buffer A render buffer indicating the address to store to, and the
285 * format the data needs to be in.
286 * @param backBuffer TRUE to flush the back buffer, FALSE for
289 void pvr2_render_copy_to_sh4( pvr2_render_buffer_t buffer,
290 gboolean backBuffer )
292 if( buffer->render_addr == -1 )
294 GLenum type, format = GL_RGBA;
295 int line_size = buffer->width, size;
297 switch( buffer->colour_format ) {
299 type = GL_UNSIGNED_SHORT_5_6_5;
304 type = GL_UNSIGNED_INT;
306 line_size = (line_size<<1)+line_size;
308 case COLFMT_ARGB1555:
309 type = GL_UNSIGNED_SHORT_5_5_5_1;
312 case COLFMT_ARGB4444:
313 type = GL_UNSIGNED_SHORT_4_4_4_4;
316 case COLFMT_ARGB8888:
317 type = GL_UNSIGNED_INT_8_8_8_8;
321 size = line_size * buffer->height;
325 glReadBuffer( GL_BACK );
327 glReadBuffer( GL_FRONT );
330 if( buffer->render_addr & 0xFF000000 == 0x04000000 ) {
331 /* Interlaced buffer. Go the double copy... :( */
333 glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
334 pvr2_vram64_write( buffer->render_addr, target, size );
338 glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
339 pvr2_vram_write_invert( buffer->render_addr, target, size, line_size );
345 * Copy data from PVR ram into the GL render buffer.
347 * @param buffer A render buffer indicating the address to read from, and the
348 * format the data is in.
349 * @param backBuffer TRUE to write the back buffer, FALSE for
352 void pvr2_render_copy_from_sh4( pvr2_render_buffer_t buffer,
353 gboolean backBuffer )
355 if( buffer->render_addr == -1 )
357 GLenum type, format = GL_RGBA;
358 int size = buffer->width * buffer->height;
360 switch( buffer->colour_format ) {
362 type = GL_UNSIGNED_SHORT_5_6_5;
367 type = GL_UNSIGNED_INT;
369 size = (size<<1)+size;
371 case COLFMT_ARGB1555:
372 type = GL_UNSIGNED_SHORT_5_5_5_1;
375 case COLFMT_ARGB4444:
376 type = GL_UNSIGNED_SHORT_4_4_4_4;
379 case COLFMT_ARGB8888:
380 type = GL_UNSIGNED_INT_8_8_8_8;
386 glDrawBuffer( GL_BACK );
388 glDrawBuffer( GL_FRONT );
391 glRasterPos2i( 0, 0 );
392 if( buffer->render_addr & 0xFF000000 == 0x04000000 ) {
393 /* Interlaced buffer. Go the double copy... :( */
395 pvr2_vram64_read( target, buffer->render_addr, size );
396 glDrawPixels( buffer->width, buffer->height,
397 format, type, target );
399 /* Regular buffer - go direct */
400 char *target = mem_get_region( buffer->render_addr );
401 glDrawPixels( buffer->width, buffer->height,
402 format, type, target );
.