Search
lxdream.org :: lxdream/src/pvr2/render.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/render.c
changeset 635:76c63aac3590
prev561:533f6b478071
next639:162ee7614b60
author nkeynes
date Thu Feb 14 13:54:11 2008 +0000 (12 years ago)
branchlxdream-render
permissions -rw-r--r--
last change Commit render work in progress. Main changes:
* Preliminary OSMesa support
* Move the generic gl code out to pvr2/
* Implement scene data structure + reader
* Remove the 1/z adjustments
file annotate diff log raw
nkeynes@100
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@100
     3
 *
nkeynes@189
     4
 * PVR2 Renderer support. This part is primarily
nkeynes@100
     5
 *
nkeynes@100
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@100
     7
 *
nkeynes@100
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@100
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@100
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@100
    11
 * (at your option) any later version.
nkeynes@100
    12
 *
nkeynes@100
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@100
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@100
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@100
    16
 * GNU General Public License for more details.
nkeynes@100
    17
 */
nkeynes@100
    18
nkeynes@429
    19
#include <sys/time.h>
nkeynes@429
    20
#include <time.h>
nkeynes@635
    21
#include <math.h>
nkeynes@100
    22
#include "pvr2/pvr2.h"
nkeynes@100
    23
#include "asic.h"
nkeynes@103
    24
nkeynes@103
    25
nkeynes@132
    26
int pvr2_render_trace = 0;
nkeynes@108
    27
nkeynes@429
    28
#if 0
nkeynes@429
    29
int pvr2_render_font_list = -1;
nkeynes@132
    30
int glPrintf( int x, int y, const char *fmt, ... )
nkeynes@108
    31
{
nkeynes@108
    32
    va_list ap;     /* our argument pointer */
nkeynes@108
    33
    char buf[256];
nkeynes@108
    34
    int len;
nkeynes@108
    35
    if (fmt == NULL)    /* if there is no string to draw do nothing */
nkeynes@429
    36
        return 0;
nkeynes@108
    37
    va_start(ap, fmt); 
nkeynes@108
    38
    len = vsnprintf(buf, sizeof(buf), fmt, ap);
nkeynes@108
    39
    va_end(ap);
nkeynes@108
    40
nkeynes@108
    41
nkeynes@108
    42
    glPushAttrib(GL_LIST_BIT);
nkeynes@132
    43
    glDisable( GL_DEPTH_TEST );
nkeynes@132
    44
    glDisable( GL_BLEND );
nkeynes@132
    45
    glDisable( GL_TEXTURE_2D );
nkeynes@132
    46
    glDisable( GL_ALPHA_TEST );
nkeynes@189
    47
    glDisable( GL_CULL_FACE );
nkeynes@108
    48
    glListBase(pvr2_render_font_list - 32);
nkeynes@132
    49
    glColor3f( 1.0, 1.0, 1.0 );
nkeynes@132
    50
    glRasterPos2i( x, y );
nkeynes@108
    51
    glCallLists(len, GL_UNSIGNED_BYTE, buf);
nkeynes@108
    52
    glPopAttrib();
nkeynes@108
    53
nkeynes@108
    54
    return len;
nkeynes@108
    55
}
nkeynes@429
    56
#endif
nkeynes@108
    57
nkeynes@189
    58
void glDrawGrid( int width, int height )
nkeynes@189
    59
{
nkeynes@189
    60
    int i;
nkeynes@189
    61
    glDisable( GL_DEPTH_TEST );
nkeynes@189
    62
    glLineWidth(1);
nkeynes@189
    63
    
nkeynes@189
    64
    glBegin( GL_LINES );
nkeynes@189
    65
    glColor4f( 1.0, 1.0, 1.0, 1.0 );
nkeynes@189
    66
    for( i=32; i<width; i+=32 ) {
nkeynes@189
    67
	glVertex3f( i, 0.0, 3.0 );
nkeynes@189
    68
	glVertex3f( i,height-1, 3.0 );
nkeynes@189
    69
    }
nkeynes@189
    70
nkeynes@189
    71
    for( i=32; i<height; i+=32 ) {
nkeynes@189
    72
	glVertex3f( 0.0, i, 3.0 );
nkeynes@189
    73
	glVertex3f( width, i, 3.0 );
nkeynes@189
    74
    }
nkeynes@189
    75
    glEnd();
nkeynes@189
    76
	
nkeynes@189
    77
}
nkeynes@189
    78
nkeynes@103
    79
/**
nkeynes@103
    80
 * Prepare the OpenGL context to receive instructions for a new frame.
nkeynes@103
    81
 */
nkeynes@352
    82
static void pvr2_render_prepare_context( render_buffer_t buffer,
nkeynes@635
    83
					 float nearz, float farz )
nkeynes@103
    84
{
nkeynes@103
    85
    /* Select and initialize the render context */
nkeynes@352
    86
    display_driver->set_render_target(buffer);
nkeynes@429
    87
#if 0
nkeynes@132
    88
    if( pvr2_render_font_list == -1 ) {
nkeynes@132
    89
	pvr2_render_font_list = video_glx_load_font( "-*-helvetica-*-r-normal--16-*-*-*-p-*-iso8859-1");
nkeynes@132
    90
    }
nkeynes@429
    91
#endif
nkeynes@337
    92
    pvr2_check_palette_changed();
nkeynes@337
    93
nkeynes@103
    94
    /* Setup the display model */
nkeynes@103
    95
    glShadeModel(GL_SMOOTH);
nkeynes@103
    96
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
nkeynes@103
    97
    glMatrixMode(GL_PROJECTION);
nkeynes@103
    98
    glLoadIdentity();
nkeynes@635
    99
    glOrtho( 0, buffer->width, buffer->height, 0, -nearz, -farz );
nkeynes@103
   100
    glMatrixMode(GL_MODELVIEW);
nkeynes@103
   101
    glLoadIdentity();
nkeynes@108
   102
    glCullFace( GL_BACK );
nkeynes@308
   103
    glEnable( GL_BLEND );
nkeynes@103
   104
nkeynes@103
   105
    /* Clear out the buffers */
nkeynes@189
   106
    glDisable( GL_SCISSOR_TEST );
nkeynes@103
   107
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
nkeynes@299
   108
    glClearDepth(0);
nkeynes@103
   109
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
nkeynes@477
   110
    glEnableClientState( GL_COLOR_ARRAY );
nkeynes@477
   111
    glEnableClientState( GL_VERTEX_ARRAY );
nkeynes@103
   112
}
nkeynes@103
   113
nkeynes@128
   114
/**
nkeynes@103
   115
 * Render a complete scene into the OpenGL back buffer.
nkeynes@103
   116
 * Note: this will probably need to be broken up eventually once timings are
nkeynes@100
   117
 * determined.
nkeynes@100
   118
 */
nkeynes@352
   119
void pvr2_render_scene( render_buffer_t buffer )
nkeynes@100
   120
{
nkeynes@429
   121
    struct timeval tva, tvb;
nkeynes@429
   122
nkeynes@429
   123
    gettimeofday(&tva, NULL);
nkeynes@100
   124
nkeynes@635
   125
    float farz, nearz;
nkeynes@635
   126
    pvr2_render_find_z_range( &nearz, &farz );
nkeynes@635
   127
    float bgplanez = MMIO_READF( PVR2, RENDER_FARCLIP );
nkeynes@635
   128
    pvr2_render_prepare_context( buffer, nearz, farz );
nkeynes@103
   129
nkeynes@191
   130
    int clip_x = MMIO_READ( PVR2, RENDER_HCLIP ) & 0x03FF;
nkeynes@191
   131
    int clip_y = MMIO_READ( PVR2, RENDER_VCLIP ) & 0x03FF;
nkeynes@191
   132
    int clip_width = ((MMIO_READ( PVR2, RENDER_HCLIP ) >> 16) & 0x03FF) - clip_x + 1;
nkeynes@191
   133
    int clip_height= ((MMIO_READ( PVR2, RENDER_VCLIP ) >> 16) & 0x03FF) - clip_y + 1;
nkeynes@103
   134
nkeynes@352
   135
    /* Fog setup goes here? */
nkeynes@103
   136
nkeynes@128
   137
    /* Render the background plane */
nkeynes@352
   138
nkeynes@191
   139
    uint32_t bgplane_mode = MMIO_READ(PVR2, RENDER_BGPLANE);
nkeynes@189
   140
    uint32_t *display_list = 
nkeynes@191
   141
	(uint32_t *)mem_get_region(PVR2_RAM_BASE + MMIO_READ( PVR2, RENDER_POLYBASE ));
nkeynes@189
   142
nkeynes@189
   143
    uint32_t *bgplane = display_list + (((bgplane_mode & 0x00FFFFFF)) >> 3) ;
nkeynes@352
   144
    render_backplane( bgplane, buffer->width, buffer->height, bgplane_mode );
nkeynes@352
   145
    
nkeynes@352
   146
    pvr2_render_tilebuffer( buffer->width, buffer->height, clip_x, clip_y, 
nkeynes@189
   147
			    clip_x + clip_width, clip_y + clip_height );
nkeynes@429
   148
nkeynes@429
   149
    gettimeofday( &tvb, NULL );
nkeynes@429
   150
    uint32_t ms = (tvb.tv_sec - tva.tv_sec) * 1000 + 
nkeynes@429
   151
	(tvb.tv_usec - tva.tv_usec)/1000;
nkeynes@429
   152
    DEBUG( "Rendered frame %d to %08X in %dms", pvr2_get_frame_count(), buffer->address, ms );
nkeynes@100
   153
}
.