Search
lxdream.org :: lxdream/src/pvr2/rendsort.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/rendsort.c
changeset 876:78cd32021472
prev865:e10c081f4b81
next893:8eae02de411a
author nkeynes
date Thu Oct 16 04:46:33 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Enhance triangle sort to be a little more precise
file annotate diff log raw
nkeynes@222
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@222
     3
 *
nkeynes@222
     4
 * PVR2 renderer routines for depth sorted polygons
nkeynes@222
     5
 *
nkeynes@222
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@222
     7
 *
nkeynes@222
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@222
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@222
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@222
    11
 * (at your option) any later version.
nkeynes@222
    12
 *
nkeynes@222
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@222
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@222
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@222
    16
 * GNU General Public License for more details.
nkeynes@222
    17
 */
nkeynes@222
    18
#include <sys/time.h>
nkeynes@653
    19
#include <string.h>
nkeynes@653
    20
#include <assert.h>
nkeynes@222
    21
#include "pvr2/pvr2.h"
nkeynes@653
    22
#include "pvr2/scene.h"
nkeynes@222
    23
#include "asic.h"
nkeynes@222
    24
nkeynes@222
    25
#define MIN3( a,b,c ) ((a) < (b) ? ( (a) < (c) ? (a) : (c) ) : ((b) < (c) ? (b) : (c)) )
nkeynes@222
    26
#define MAX3( a,b,c ) ((a) > (b) ? ( (a) > (c) ? (a) : (c) ) : ((b) > (c) ? (b) : (c)) )
nkeynes@876
    27
#define EPSILON 0.0001
nkeynes@222
    28
nkeynes@653
    29
struct sort_triangle {
nkeynes@653
    30
    struct polygon_struct *poly;
nkeynes@653
    31
    int triangle_num; // triangle number in the poly, from 0
nkeynes@876
    32
    /* plane equation */
nkeynes@876
    33
    float mx, my, mz, d;
nkeynes@876
    34
    float bounds[6]; /* x1,x2,y1,y2,z1,z2 */
nkeynes@222
    35
};
nkeynes@222
    36
nkeynes@318
    37
#define SENTINEL 0xDEADBEEF
nkeynes@318
    38
nkeynes@222
    39
/**
nkeynes@222
    40
 * Count the number of triangles in the list starting at the given 
nkeynes@222
    41
 * pvr memory address.
nkeynes@222
    42
 */
nkeynes@876
    43
static int sort_count_triangles( pvraddr_t tile_entry ) {
nkeynes@222
    44
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@222
    45
    int count = 0;
nkeynes@222
    46
    while(1) {
nkeynes@736
    47
        uint32_t entry = *tile_list++;
nkeynes@736
    48
        if( entry >> 28 == 0x0F ) {
nkeynes@736
    49
            break;
nkeynes@736
    50
        } else if( entry >> 28 == 0x0E ) {
nkeynes@736
    51
            tile_list = (uint32_t *)(video_base+(entry&0x007FFFFF));
nkeynes@736
    52
        } else if( entry >> 29 == 0x04 ) { /* Triangle array */
nkeynes@736
    53
            count += ((entry >> 25) & 0x0F)+1;
nkeynes@736
    54
        } else if( entry >> 29 == 0x05 ) { /* Quad array */
nkeynes@736
    55
            count += ((((entry >> 25) & 0x0F)+1)<<1);
nkeynes@736
    56
        } else { /* Polygon */
nkeynes@736
    57
            int i;
nkeynes@736
    58
            for( i=0; i<6; i++ ) {
nkeynes@736
    59
                if( entry & (0x40000000>>i) ) {
nkeynes@736
    60
                    count++;
nkeynes@736
    61
                }
nkeynes@736
    62
            }
nkeynes@736
    63
        }
nkeynes@222
    64
    }
nkeynes@222
    65
    return count;
nkeynes@222
    66
}
nkeynes@222
    67
nkeynes@876
    68
static void sort_add_triangle( struct sort_triangle *triangle, struct polygon_struct *poly, int index )
nkeynes@876
    69
{
nkeynes@876
    70
    struct vertex_struct *vertexes = &pvr2_scene.vertex_array[poly->vertex_index+index];
nkeynes@876
    71
    triangle->poly = poly;
nkeynes@876
    72
    triangle->triangle_num = index;
nkeynes@876
    73
nkeynes@876
    74
    /* Compute triangle bounding-box */
nkeynes@876
    75
    triangle->bounds[0] = MIN3(vertexes[0].x,vertexes[1].x,vertexes[2].x);
nkeynes@876
    76
    triangle->bounds[1] = MAX3(vertexes[0].x,vertexes[1].x,vertexes[2].x);
nkeynes@876
    77
    triangle->bounds[2] = MIN3(vertexes[0].y,vertexes[1].y,vertexes[2].y);
nkeynes@876
    78
    triangle->bounds[3] = MAX3(vertexes[0].y,vertexes[1].y,vertexes[2].y);
nkeynes@876
    79
    triangle->bounds[4] = MIN3(vertexes[0].z,vertexes[1].z,vertexes[2].z);
nkeynes@876
    80
    triangle->bounds[5] = MAX3(vertexes[0].z,vertexes[1].z,vertexes[2].z);
nkeynes@876
    81
nkeynes@876
    82
    /* Compute plane equation */
nkeynes@876
    83
    float sx = vertexes[1].x - vertexes[0].x;
nkeynes@876
    84
    float sy = vertexes[1].y - vertexes[0].y;
nkeynes@876
    85
    float sz = vertexes[1].z - vertexes[0].z;
nkeynes@876
    86
    float tx = vertexes[2].x - vertexes[0].x;
nkeynes@876
    87
    float ty = vertexes[2].y - vertexes[0].y;
nkeynes@876
    88
    float tz = vertexes[2].z - vertexes[0].z;
nkeynes@876
    89
    triangle->mx = sy*tz - sz*ty;
nkeynes@876
    90
    triangle->my = sz*tx - sx*tz;
nkeynes@876
    91
    triangle->mz = sx*ty - sy*tx;
nkeynes@876
    92
    triangle->d = -vertexes[0].x*triangle->mx - 
nkeynes@876
    93
                  vertexes[0].y*triangle->my - 
nkeynes@876
    94
                  vertexes[0].z*triangle->mz;
nkeynes@876
    95
}
nkeynes@876
    96
nkeynes@653
    97
/**
nkeynes@653
    98
 * Extract a triangle list from the tile (basically indexes into the polygon list, plus
nkeynes@653
    99
 * computing maxz while we go through it
nkeynes@653
   100
 */
nkeynes@653
   101
int sort_extract_triangles( pvraddr_t tile_entry, struct sort_triangle *triangles )
nkeynes@222
   102
{
nkeynes@222
   103
    uint32_t *tile_list = (uint32_t *)(video_base+tile_entry);
nkeynes@862
   104
    int strip_count;
nkeynes@862
   105
    struct polygon_struct *poly;
nkeynes@862
   106
    int count = 0, i;
nkeynes@862
   107
nkeynes@222
   108
    while(1) {
nkeynes@736
   109
        uint32_t entry = *tile_list++;
nkeynes@862
   110
        switch( entry >> 28 ) {
nkeynes@862
   111
        case 0x0F:
nkeynes@862
   112
            return count; // End-of-list
nkeynes@862
   113
        case 0x0E:
nkeynes@862
   114
            tile_list = (uint32_t *)(video_base + (entry&0x007FFFFF));
nkeynes@736
   115
            break;
nkeynes@862
   116
        case 0x08: case 0x09: case 0x0A: case 0x0B:
nkeynes@862
   117
            strip_count = ((entry >> 25) & 0x0F)+1;
nkeynes@862
   118
            poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@862
   119
            while( strip_count > 0 ) {
nkeynes@862
   120
                assert( poly != NULL );
nkeynes@876
   121
                for( i=0; i<poly->vertex_count-2; i++ ) {
nkeynes@876
   122
                    sort_add_triangle( &triangles[count], poly, i );
nkeynes@736
   123
                    count++;
nkeynes@736
   124
                }
nkeynes@862
   125
                poly = poly->next;
nkeynes@862
   126
                strip_count--;
nkeynes@862
   127
            }
nkeynes@862
   128
            break;
nkeynes@862
   129
        default:
nkeynes@862
   130
            if( entry & 0x7E000000 ) {
nkeynes@862
   131
                poly = pvr2_scene.buf_to_poly_map[entry&0x000FFFFF];
nkeynes@736
   132
                for( i=0; i<6; i++ ) {
nkeynes@736
   133
                    if( entry & (0x40000000>>i) ) {
nkeynes@876
   134
                        sort_add_triangle( &triangles[count], poly, i );
nkeynes@736
   135
                        count++;
nkeynes@736
   136
                    }
nkeynes@736
   137
                }
nkeynes@736
   138
            }
nkeynes@736
   139
        }
nkeynes@862
   140
    }       
nkeynes@862
   141
nkeynes@222
   142
}
nkeynes@222
   143
nkeynes@876
   144
void sort_render_triangles( struct sort_triangle **triangles, int num_triangles )
nkeynes@222
   145
{
nkeynes@429
   146
    int i;
nkeynes@222
   147
    for( i=0; i<num_triangles; i++ ) {
nkeynes@876
   148
        struct polygon_struct *poly = triangles[i]->poly;
nkeynes@736
   149
        if( poly->tex_id != -1 ) {
nkeynes@736
   150
            glBindTexture(GL_TEXTURE_2D, poly->tex_id);
nkeynes@736
   151
        }
nkeynes@865
   152
        render_set_context( poly->context, GL_GEQUAL );
nkeynes@736
   153
        glDepthMask(GL_FALSE);
nkeynes@736
   154
        /* Fix cull direction */
nkeynes@876
   155
        if( triangles[i]->triangle_num & 1 ) {
nkeynes@736
   156
            glCullFace(GL_FRONT);
nkeynes@736
   157
        } else {
nkeynes@736
   158
            glCullFace(GL_BACK);
nkeynes@736
   159
        }
nkeynes@736
   160
nkeynes@876
   161
        glDrawArrays(GL_TRIANGLE_STRIP, poly->vertex_index + triangles[i]->triangle_num, 3 );
nkeynes@222
   162
    }
nkeynes@222
   163
}
nkeynes@222
   164
nkeynes@876
   165
static int sort_triangle_compare( const void *a, const void *b ) 
nkeynes@222
   166
{
nkeynes@653
   167
    const struct sort_triangle *tri1 = a;
nkeynes@653
   168
    const struct sort_triangle *tri2 = b;
nkeynes@876
   169
    if( tri1->bounds[5] <= tri2->bounds[4] ) 
nkeynes@876
   170
        return 1; /* tri1 is entirely under tri2 */
nkeynes@876
   171
    else if( tri2->bounds[5] <= tri1->bounds[4] )
nkeynes@876
   172
        return -1;  /* tri2 is entirely under tri1 */
nkeynes@876
   173
    else if( tri1->bounds[1] <= tri2->bounds[0] ||
nkeynes@876
   174
             tri2->bounds[1] <= tri1->bounds[0] ||
nkeynes@876
   175
             tri1->bounds[3] <= tri2->bounds[2] ||
nkeynes@876
   176
             tri2->bounds[3] <= tri1->bounds[2] )
nkeynes@876
   177
        return 0; /* tri1 and tri2 don't actually overlap at all */
nkeynes@876
   178
    else { 
nkeynes@876
   179
        struct vertex_struct *tri1v = &pvr2_scene.vertex_array[tri1->poly->vertex_index + tri1->triangle_num];
nkeynes@876
   180
        struct vertex_struct *tri2v = &pvr2_scene.vertex_array[tri2->poly->vertex_index + tri2->triangle_num];
nkeynes@876
   181
        float v[3];
nkeynes@876
   182
        int i;
nkeynes@876
   183
        for( i=0; i<3; i++ ) {
nkeynes@876
   184
            v[i] = tri1->mx * tri2v[i].x + tri1->my * tri2v[i].y + tri1->mz * tri2v[i].z + tri1->d;
nkeynes@876
   185
            if( v[i] > -EPSILON && v[i] < EPSILON ) v[i] = 0;
nkeynes@876
   186
        }
nkeynes@876
   187
        if( v[0] == 0 && v[1] == 0 && v[2] == 0 ) {
nkeynes@876
   188
            return 0; /* coplanar */
nkeynes@876
   189
        }
nkeynes@876
   190
        if( (v[0] >=0 && v[1] >= 0 && v[2] >= 0) ||
nkeynes@876
   191
            (v[0] <= 0 && v[1] <= 0 && v[2] <= 0) ) {
nkeynes@876
   192
            /* Tri is on one side of the plane. Pick an arbitrary point to determine which side */
nkeynes@876
   193
            float t1z = -(tri1->mx * tri2v[0].x + tri1->my * tri2v[0].y + tri1->d) / tri1->mz;
nkeynes@876
   194
            return tri2v[0].z - t1z;
nkeynes@876
   195
        }
nkeynes@876
   196
        
nkeynes@876
   197
        /* If the above test failed, then tri2 intersects tri1's plane. This
nkeynes@876
   198
         * doesn't necessarily mean the triangles intersect (although they may).
nkeynes@876
   199
         * For now just return 0, and come back to this later as it's a fairly
nkeynes@876
   200
         * uncommon case in practice. 
nkeynes@876
   201
         */
nkeynes@876
   202
        return 0;
nkeynes@876
   203
    }
nkeynes@222
   204
}
nkeynes@222
   205
nkeynes@876
   206
/**
nkeynes@876
   207
 * This is pretty much a standard merge sort (Unfortunately can't depend on
nkeynes@876
   208
 * the system to provide one. Note we can't use quicksort here - the sort
nkeynes@876
   209
 * must be stable to preserve the order of coplanar triangles.
nkeynes@876
   210
 */
nkeynes@876
   211
static void sort_triangles( struct sort_triangle **triangles, int num_triangles, struct sort_triangle **out )
nkeynes@222
   212
{
nkeynes@876
   213
    if( num_triangles > 2 ) {
nkeynes@876
   214
        int l = num_triangles>>1, r=num_triangles-l, i=0,j=0;
nkeynes@876
   215
        struct sort_triangle *left[l];
nkeynes@876
   216
        struct sort_triangle *right[r];
nkeynes@876
   217
        sort_triangles( triangles, l, left );
nkeynes@876
   218
        sort_triangles( triangles+l, r, right );
nkeynes@876
   219
        
nkeynes@876
   220
        /* Merge */
nkeynes@876
   221
        while(1) {
nkeynes@876
   222
            if( sort_triangle_compare(left[i], right[j]) <= 0 ) {
nkeynes@876
   223
                *out++ = left[i++];
nkeynes@876
   224
                if( i == l ) {
nkeynes@876
   225
                    memcpy( out, &right[j], (r-j)*sizeof(struct sort_triangle *) );        
nkeynes@876
   226
                    break;
nkeynes@876
   227
                }
nkeynes@876
   228
            } else {
nkeynes@876
   229
                *out++ = right[j++];
nkeynes@876
   230
                if( j == r ) {
nkeynes@876
   231
                    memcpy( out, &left[i], (l-i)*sizeof(struct sort_triangle *) );
nkeynes@876
   232
                    break;
nkeynes@876
   233
                }
nkeynes@876
   234
            }
nkeynes@876
   235
        }
nkeynes@876
   236
    } else if( num_triangles == 2 ) {
nkeynes@876
   237
        if( sort_triangle_compare(triangles[0], triangles[1]) <= 0 ) {
nkeynes@876
   238
            out[0] = triangles[0];
nkeynes@876
   239
            out[1] = triangles[1];
nkeynes@876
   240
        } else {
nkeynes@876
   241
            struct sort_triangle *tmp = triangles[0];
nkeynes@876
   242
            out[0] = triangles[1];
nkeynes@876
   243
            out[1] = tmp;
nkeynes@876
   244
        }
nkeynes@876
   245
    } else {
nkeynes@876
   246
        out[0] = triangles[0];
nkeynes@876
   247
    }
nkeynes@222
   248
} 
nkeynes@736
   249
nkeynes@653
   250
void render_autosort_tile( pvraddr_t tile_entry, int render_mode ) 
nkeynes@222
   251
{
nkeynes@653
   252
    int num_triangles = sort_count_triangles(tile_entry);
nkeynes@222
   253
    if( num_triangles == 0 ) {
nkeynes@736
   254
        return; /* nothing to do */
nkeynes@222
   255
    } else if( num_triangles == 1 ) { /* Triangle can hardly overlap with itself */
nkeynes@865
   256
        gl_render_tilelist(tile_entry, GL_LEQUAL);
nkeynes@222
   257
    } else { /* Ooh boy here we go... */
nkeynes@876
   258
        int i;
nkeynes@736
   259
        struct sort_triangle triangles[num_triangles+1];
nkeynes@876
   260
        struct sort_triangle *triangle_order[num_triangles+1];
nkeynes@736
   261
        triangles[num_triangles].poly = (void *)SENTINEL;
nkeynes@876
   262
        for( i=0; i<num_triangles; i++ ) {
nkeynes@876
   263
            triangle_order[i] = &triangles[i];
nkeynes@876
   264
        }
nkeynes@736
   265
        int extracted_triangles = sort_extract_triangles(tile_entry, triangles);
nkeynes@736
   266
        assert( extracted_triangles == num_triangles );
nkeynes@876
   267
        sort_triangles( triangle_order, num_triangles, triangle_order );
nkeynes@876
   268
        sort_render_triangles(triangle_order, num_triangles);
nkeynes@736
   269
        glCullFace(GL_BACK);
nkeynes@736
   270
        assert( triangles[num_triangles].poly == (void *)SENTINEL );
nkeynes@222
   271
    }
nkeynes@222
   272
}
.