2 * $Id: render.c,v 1.10 2006-06-27 09:32:09 nkeynes Exp $
4 * PVR2 Renderer support. This is where the real work happens.
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 #define POLY_COLOUR_PACKED 0x00000000
24 #define POLY_COLOUR_FLOAT 0x00000010
25 #define POLY_COLOUR_INTENSITY 0x00000020
26 #define POLY_COLOUR_INTENSITY_PREV 0x00000030
28 static int pvr2_poly_vertexes[4] = { 3, 4, 6, 8 };
29 static int pvr2_poly_type[4] = { GL_TRIANGLES, GL_QUADS, GL_TRIANGLE_STRIP, GL_TRIANGLE_STRIP };
30 static int pvr2_poly_depthmode[8] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
31 GL_GREATER, GL_NOTEQUAL, GL_GEQUAL,
33 static int pvr2_poly_srcblend[8] = {
34 GL_ZERO, GL_ONE, GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
35 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
36 GL_ONE_MINUS_DST_ALPHA };
37 static int pvr2_poly_dstblend[8] = {
38 GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
39 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA,
40 GL_ONE_MINUS_DST_ALPHA };
41 static int pvr2_poly_texblend[4] = {
42 GL_REPLACE, GL_BLEND, GL_DECAL, GL_MODULATE };
43 static int pvr2_render_colour_format[8] = {
44 COLFMT_ARGB1555, COLFMT_RGB565, COLFMT_ARGB4444, COLFMT_ARGB1555,
45 COLFMT_RGB888, COLFMT_ARGB8888, COLFMT_ARGB8888, COLFMT_ARGB4444 };
47 #define POLY_STRIP_TYPE(poly) ( pvr2_poly_type[((poly->command)>>18)&0x03] )
48 #define POLY_STRIP_VERTEXES(poly) ( pvr2_poly_vertexes[((poly->command)>>18)&0x03] )
49 #define POLY_DEPTH_MODE(poly) ( pvr2_poly_depthmode[poly->poly_cfg>>29] )
50 #define POLY_DEPTH_WRITE(poly) ((poly->poly_cfg&0x04000000) == 0 )
51 #define POLY_TEX_WIDTH(poly) ( 1<< (((poly->poly_mode >> 3) & 0x07 ) + 3) )
52 #define POLY_TEX_HEIGHT(poly) ( 1<< (((poly->poly_mode) & 0x07 ) + 3) )
53 #define POLY_BLEND_SRC(poly) ( pvr2_poly_srcblend[(poly->poly_mode) >> 29] )
54 #define POLY_BLEND_DEST(poly) ( pvr2_poly_dstblend[((poly->poly_mode)>>26)&0x07] )
55 #define POLY_TEX_BLEND(poly) ( pvr2_poly_texblend[((poly->poly_mode) >> 6)&0x03] )
56 #define POLY_COLOUR_TYPE(poly) ( poly->command & 0x00000030 )
59 * Describes a rendering buffer that's actually held in GL, for when we need
60 * to fetch the bits back to vram.
62 typedef struct pvr2_render_buffer {
63 sh4addr_t render_addr; /* The actual address rendered to in pvr ram */
64 uint32_t size; /* Length of rendering region in bytes */
67 } *pvr2_render_buffer_t;
69 struct pvr2_render_buffer front_buffer;
70 struct pvr2_render_buffer back_buffer;
72 struct tile_descriptor {
74 struct tile_pointers {
77 uint32_t opaque_mod_ptr;
79 uint32_t trans_mod_ptr;
80 uint32_t punchout_ptr;
84 /* Textured polygon */
87 uint32_t poly_cfg; /* Bitmask */
88 uint32_t poly_mode; /* texture/blending mask */
89 uint32_t texture; /* texture data */
96 struct pvr2_specular_highlight {
108 struct pvr2_vertex_packed {
116 struct pvr2_vertex_float {
123 struct pvr2_vertex_packed pack;
124 struct pvr2_vertex_float flt;
127 typedef struct pvr2_bgplane_packed {
128 uint32_t poly_cfg, poly_mode;
129 uint32_t texture_mode;
136 } *pvr2_bgplane_packed_t;
140 void pvr2_render_copy_to_sh4( pvr2_render_buffer_t buffer,
141 gboolean backBuffer );
143 int pvr2_render_font_list = -1;
144 int pvr2_render_trace = 0;
146 int glPrintf( int x, int y, const char *fmt, ... )
148 va_list ap; /* our argument pointer */
151 if (fmt == NULL) /* if there is no string to draw do nothing */
154 len = vsnprintf(buf, sizeof(buf), fmt, ap);
158 glPushAttrib(GL_LIST_BIT);
159 glDisable( GL_DEPTH_TEST );
160 glDisable( GL_BLEND );
161 glDisable( GL_TEXTURE_2D );
162 glDisable( GL_ALPHA_TEST );
163 glListBase(pvr2_render_font_list - 32);
164 glColor3f( 1.0, 1.0, 1.0 );
165 glRasterPos2i( x, y );
166 glCallLists(len, GL_UNSIGNED_BYTE, buf);
173 gboolean pvr2_render_init( void )
175 front_buffer.render_addr = -1;
176 back_buffer.render_addr = -1;
180 * Invalidate any caching on the supplied address. Specifically, if it falls
181 * within either the front buffer or back buffer, flush the buffer back to
182 * PVR2 ram (note that front buffer flush may be corrupt under some
185 gboolean pvr2_render_invalidate( sh4addr_t address )
187 address = address & 0x1FFFFFFF;
188 if( front_buffer.render_addr != -1 &&
189 front_buffer.render_addr <= address &&
190 (front_buffer.render_addr + front_buffer.size) > address ) {
191 pvr2_render_copy_to_sh4( &front_buffer, FALSE );
192 front_buffer.render_addr = -1;
194 } else if( back_buffer.render_addr != -1 &&
195 back_buffer.render_addr <= address &&
196 (back_buffer.render_addr + back_buffer.size) > address ) {
197 pvr2_render_copy_to_sh4( &back_buffer, TRUE );
198 back_buffer.render_addr = -1;
205 * Display a rendered frame if one is available.
206 * @param address An address in PVR ram (0500000 range).
207 * @return TRUE if a frame was available to be displayed, otherwise false.
209 gboolean pvr2_render_display_frame( uint32_t address )
211 if( front_buffer.render_addr == address ) {
212 /* Current front buffer is already displayed, so do nothing
213 * and tell the caller that all is well.
217 if( back_buffer.render_addr == address ) {
218 /* The more useful case - back buffer is to be displayed. Swap
221 display_driver->display_back_buffer();
222 front_buffer = back_buffer;
223 back_buffer.render_addr = -1;
230 * Prepare the OpenGL context to receive instructions for a new frame.
232 static void pvr2_render_prepare_context( sh4addr_t render_addr,
233 uint32_t width, uint32_t height,
234 uint32_t colour_format,
236 gboolean texture_target )
238 /* Select and initialize the render context */
239 display_driver->set_render_format( width, height, colour_format, texture_target );
241 if( pvr2_render_font_list == -1 ) {
242 pvr2_render_font_list = video_glx_load_font( "-*-helvetica-*-r-normal--16-*-*-*-p-*-iso8859-1");
245 if( back_buffer.render_addr != -1 &&
246 back_buffer.render_addr != render_addr ) {
247 /* There's a current back buffer, and we're rendering somewhere else -
248 * flush the back buffer back to vram and start a new back buffer
250 pvr2_render_copy_to_sh4( &back_buffer, TRUE );
253 if( front_buffer.render_addr == render_addr ) {
254 /* In case we've been asked to render to the current front buffer -
255 * invalidate the front buffer and render to the back buffer, ensuring
256 * we swap at the next frame display.
258 front_buffer.render_addr = -1;
260 back_buffer.render_addr = render_addr;
261 back_buffer.width = width;
262 back_buffer.height = height;
263 back_buffer.colour_format = colour_format;
264 back_buffer.size = width * height * colour_format_bytes[colour_format];
266 /* Setup the display model */
267 glDrawBuffer(GL_BACK);
268 glShadeModel(GL_SMOOTH);
269 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
270 glViewport( 0, 0, width, height );
271 glMatrixMode(GL_PROJECTION);
273 glOrtho( 0, width, height, 0, bgplanez, -4 );
274 glMatrixMode(GL_MODELVIEW);
276 glCullFace( GL_BACK );
278 /* Clear out the buffers */
279 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
280 glClearDepth(bgplanez);
281 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
284 static void pvr2_dump_display_list( uint32_t * display_list, uint32_t length )
287 gboolean vertex = FALSE;
288 for( i =0; i<length>>2; i++ ) {
291 fprintf( stderr, "\n" );
292 fprintf( stderr, "%08X:", i*4 );
293 if( display_list[i] == 0xE0000000 ||
294 display_list[i] == 0xF0000000 )
298 if( vertex && (i%8) > 0 && (i%8) < 4 )
299 fprintf( stderr, " %f", ((float *)display_list)[i] );
301 fprintf( stderr, " %08X", display_list[i] );
303 fprintf( stderr, "\n" );
306 static void pvr2_render_display_list( uint32_t *display_list, uint32_t length )
308 uint32_t *cmd_ptr = display_list;
309 int strip_length = 0, vertex_count = 0;
311 gboolean textured = FALSE;
312 gboolean shaded = FALSE;
313 struct pvr2_poly *poly;
314 if( pvr2_render_trace ) {
315 fprintf( stderr, "-------- %d\n", pvr2_get_frame_count() );
316 pvr2_dump_display_list( display_list, length );
318 while( cmd_ptr < display_list+(length>>2) ) {
319 unsigned int cmd = *cmd_ptr >> 24;
321 case PVR2_CMD_POLY_OPAQUE:
322 case PVR2_CMD_POLY_TRANS:
323 case PVR2_CMD_POLY_PUNCHOUT:
324 poly = (struct pvr2_poly *)cmd_ptr;
325 if( poly->command & PVR2_POLY_TEXTURED ) {
326 uint32_t addr = PVR2_TEX_ADDR(poly->texture);
327 int width = POLY_TEX_WIDTH(poly);
328 int height = POLY_TEX_HEIGHT(poly);
329 glEnable( GL_TEXTURE_2D );
330 texcache_get_texture( addr, width, height, poly->texture );
332 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, POLY_TEX_BLEND(poly) );
335 glDisable( GL_TEXTURE_2D );
337 glBlendFunc( POLY_BLEND_SRC(poly), POLY_BLEND_DEST(poly) );
338 if( poly->command & PVR2_POLY_SPECULAR ) {
339 /* Second block expected */
341 if( POLY_DEPTH_WRITE(poly) ) {
342 glEnable( GL_DEPTH_TEST );
343 glDepthFunc( POLY_DEPTH_MODE(poly) );
345 glDisable( GL_DEPTH_TEST );
348 switch( (poly->poly_cfg >> 27) & 0x03 ) {
351 glDisable( GL_CULL_FACE );
354 glEnable( GL_CULL_FACE );
355 glFrontFace( GL_CW );
358 glEnable( GL_CULL_FACE );
359 glFrontFace( GL_CCW );
361 strip_length = POLY_STRIP_VERTEXES( poly );
362 colour_type = POLY_COLOUR_TYPE( poly );
364 if( poly->command & PVR2_POLY_SHADED ) {
369 if( poly->poly_mode & PVR2_POLY_MODE_TEXALPHA ) {
370 glDisable( GL_BLEND );
372 glEnable( GL_BLEND );
376 case PVR2_CMD_MOD_OPAQUE:
377 case PVR2_CMD_MOD_TRANS:
380 case PVR2_CMD_END_OF_LIST:
382 case PVR2_CMD_VERTEX_LAST:
383 case PVR2_CMD_VERTEX:
384 if( vertex_count == 0 ) {
385 glBegin( GL_TRIANGLE_STRIP );
389 struct pvr2_vertex_packed *vertex = (struct pvr2_vertex_packed *)cmd_ptr;
391 glTexCoord2f( vertex->s, vertex->t );
393 if( shaded || vertex_count == 1) {
394 switch( colour_type ) {
395 case POLY_COLOUR_PACKED:
396 glColor4ub( vertex->colour >> 16, vertex->colour >> 8,
397 vertex->colour, vertex->colour >> 24 );
402 if( shaded || vertex_count == 1 ) {
403 switch( colour_type ) {
404 case POLY_COLOUR_PACKED:
405 glColor4ub( vertex->colour >> 16, vertex->colour >> 8,
406 vertex->colour, vertex->colour >> 24 );
408 case POLY_COLOUR_FLOAT:
410 struct pvr2_vertex_float *v = (struct pvr2_vertex_float *)cmd_ptr;
411 glColor4f( v->r, v->g, v->b, v->a );
418 glVertex3f( vertex->x, vertex->y, vertex->z );
420 if( cmd == PVR2_CMD_VERTEX_LAST ) {
426 ERROR( "Unhandled command %08X in display list", *cmd_ptr );
427 pvr2_dump_display_list( display_list, length );
430 cmd_ptr += 8; /* Next record */
434 #define MIN3( a,b,c ) ((a) < (b) ? ( (a) < (c) ? (a) : (c) ) : ((b) < (c) ? (b) : (c)) )
435 #define MAX3( a,b,c ) ((a) > (b) ? ( (a) > (c) ? (a) : (c) ) : ((b) > (c) ? (b) : (c)) )
438 * Render the background plane as best we can. Unfortunately information
439 * is a little scant, to say the least.
441 void pvr2_render_draw_backplane( uint32_t mode, uint32_t *poly )
443 if( (mode >> 24) == 0x01 ) {
444 /* Packed colour. I think */
445 pvr2_bgplane_packed_t bg = (pvr2_bgplane_packed_t)poly;
446 if( bg->colour1 != bg->colour2 || bg->colour2 != bg->colour3 ) {
447 WARN( "Multiple background colours specified. Confused" );
449 float x1 = MIN3( bg->x1, bg->x2, bg->x3 );
450 float y1 = MIN3( bg->y1, bg->y2, bg->y3 );
451 float x2 = MAX3( bg->x1, bg->x2, bg->x3 );
452 float y2 = MAX3( bg->y1, bg->y2, bg->y3 );
453 float z = MIN3( bg->z1, bg->z2, bg->z3 );
454 glDisable( GL_TEXTURE_2D );
455 glDisable( GL_DEPTH_TEST );
456 glColor3ub( (uint8_t)(bg->colour1 >> 16), (uint8_t)(bg->colour1 >> 8),
457 (uint8_t)bg->colour1 );
459 glVertex3f( x1, y1, z );
460 glVertex3f( x2, y1, z );
461 glVertex3f( x2, y2, z );
462 glVertex3f( x1, y2, z );
465 WARN( "Unknown bgplane mode: %08X", mode );
466 fwrite_dump( poly, 48, stderr );
471 * Render a complete scene into the OpenGL back buffer.
472 * Note: this will probably need to be broken up eventually once timings are
475 void pvr2_render_scene( )
477 struct tile_descriptor *tile_desc =
478 (struct tile_descriptor *)mem_get_region(PVR2_RAM_BASE + MMIO_READ( PVR2, TILEBASE ));
480 uint32_t render_addr = MMIO_READ( PVR2, RENDADDR1 );
481 gboolean render_to_tex;
482 if( render_addr & 0x01000000 ) {
483 render_addr = (render_addr & 0x00FFFFFF) + PVR2_RAM_BASE_INT;
484 /* Heuristic - if we're rendering to the interlaced region we're
485 * probably creating a texture rather than rendering actual output.
486 * We can optimise for this case a little
488 render_to_tex = TRUE;
489 WARN( "Render to texture not supported properly yet" );
491 render_addr = (render_addr & 0x00FFFFFF) + PVR2_RAM_BASE;
492 render_to_tex = FALSE;
495 float bgplanez = MMIO_READF( PVR2, BGPLANEZ );
496 uint32_t render_mode = MMIO_READ( PVR2, RENDMODE );
497 int width = 640; /* FIXME - get this from the tile buffer */
499 int colour_format = pvr2_render_colour_format[render_mode&0x07];
500 pvr2_render_prepare_context( render_addr, width, height, colour_format,
501 bgplanez, render_to_tex );
503 uint32_t *display_list =
504 (uint32_t *)mem_get_region(PVR2_RAM_BASE + MMIO_READ( PVR2, OBJBASE ));
506 uint32_t display_length = *display_list++;
508 int clip_x = MMIO_READ( PVR2, HCLIP ) & 0x03FF;
509 int clip_y = MMIO_READ( PVR2, VCLIP ) & 0x03FF;
510 int clip_width = ((MMIO_READ( PVR2, HCLIP ) >> 16) & 0x03FF) - clip_x + 1;
511 int clip_height= ((MMIO_READ( PVR2, VCLIP ) >> 16) & 0x03FF) - clip_y + 1;
513 if( clip_x == 0 && clip_y == 0 && clip_width == width && clip_height == height ) {
514 glDisable( GL_SCISSOR_TEST );
516 glEnable( GL_SCISSOR_TEST );
517 glScissor( clip_x, clip_y, clip_width, clip_height );
520 /* Fog setup goes here */
522 /* Render the background plane */
523 uint32_t bgplane_mode = MMIO_READ(PVR2, BGPLANE);
524 uint32_t *bgplane = display_list + (((bgplane_mode & 0x00FFFFFF)) >> 3) - 1;
525 pvr2_render_draw_backplane( bgplane_mode, bgplane );
527 /* Render the display list */
528 pvr2_render_display_list( display_list, display_length );
530 /* Post-render cleanup and update */
532 /* Add frame, fps, etc data */
533 glPrintf( 4, 16, "Frame %d", pvr2_get_frame_count() );
535 /* Generate end of render event */
536 asic_event( EVENT_PVR_RENDER_DONE );
537 DEBUG( "Rendered frame %d", pvr2_get_frame_count() );
542 * Flush the indicated render buffer back to PVR. Caller is responsible for
543 * tracking whether there is actually anything in the buffer.
545 * @param buffer A render buffer indicating the address to store to, and the
546 * format the data needs to be in.
547 * @param backBuffer TRUE to flush the back buffer, FALSE for
550 void pvr2_render_copy_to_sh4( pvr2_render_buffer_t buffer,
551 gboolean backBuffer )
553 if( buffer->render_addr == -1 )
555 GLenum type, format = GL_RGBA;
556 int size = buffer->width * buffer->height;
558 switch( buffer->colour_format ) {
560 type = GL_UNSIGNED_SHORT_5_6_5;
565 type = GL_UNSIGNED_INT;
567 size = (size<<1)+size;
569 case COLFMT_ARGB1555:
570 type = GL_UNSIGNED_SHORT_5_5_5_1;
573 case COLFMT_ARGB4444:
574 type = GL_UNSIGNED_SHORT_4_4_4_4;
577 case COLFMT_ARGB8888:
578 type = GL_UNSIGNED_INT_8_8_8_8;
585 glReadBuffer( GL_BACK );
587 glReadBuffer( GL_FRONT );
590 if( buffer->render_addr & 0xFF000000 == 0x04000000 ) {
591 /* Interlaced buffer. Go the double copy... :( */
593 glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
594 pvr2_vram64_write( buffer->render_addr, target, size );
596 /* Regular buffer - go direct */
597 char *target = mem_get_region( buffer->render_addr );
598 glReadPixels( 0, 0, buffer->width, buffer->height, format, type, target );
604 * Copy data from PVR ram into the GL render buffer.
606 * @param buffer A render buffer indicating the address to read from, and the
607 * format the data is in.
608 * @param backBuffer TRUE to write the back buffer, FALSE for
611 void pvr2_render_copy_from_sh4( pvr2_render_buffer_t buffer,
612 gboolean backBuffer )
614 if( buffer->render_addr == -1 )
616 GLenum type, format = GL_RGBA;
617 int size = buffer->width * buffer->height;
619 switch( buffer->colour_format ) {
621 type = GL_UNSIGNED_SHORT_5_6_5;
626 type = GL_UNSIGNED_INT;
628 size = (size<<1)+size;
630 case COLFMT_ARGB1555:
631 type = GL_UNSIGNED_SHORT_5_5_5_1;
634 case COLFMT_ARGB4444:
635 type = GL_UNSIGNED_SHORT_4_4_4_4;
638 case COLFMT_ARGB8888:
639 type = GL_UNSIGNED_INT_8_8_8_8;
645 glDrawBuffer( GL_BACK );
647 glDrawBuffer( GL_FRONT );
650 glRasterPos2i( 0, 0 );
651 if( buffer->render_addr & 0xFF000000 == 0x04000000 ) {
652 /* Interlaced buffer. Go the double copy... :( */
654 pvr2_vram64_read( target, buffer->render_addr, size );
655 glDrawPixels( buffer->width, buffer->height,
656 format, type, target );
658 /* Regular buffer - go direct */
659 char *target = mem_get_region( buffer->render_addr );
660 glDrawPixels( buffer->width, buffer->height,
661 format, type, target );
.