Search
lxdream.org :: lxdream/src/pvr2/tacore.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/tacore.c
changeset 753:1fe39c3a9bbc
prev736:a02d1475ccfd
next801:92b518a2e915
author nkeynes
date Wed Jul 30 22:50:44 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Bug #61: OpenBSD support
(Modified) patch from bsdmaniak, thanks!
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * PVR2 Tile Accelerator implementation
     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  */
    18 #include <string.h>
    19 #include "lxdream.h"
    20 #include "pvr2/pvr2.h"
    21 #include "pvr2/pvr2mmio.h"
    22 #include "asic.h"
    23 #include "dream.h"
    25 #define STATE_IDLE                 0
    26 #define STATE_IN_LIST              1
    27 #define STATE_IN_POLYGON           2
    28 #define STATE_EXPECT_POLY_BLOCK2   3
    29 #define STATE_EXPECT_VERTEX_BLOCK2 4
    30 #define STATE_ERROR                5
    31 #define STATE_EXPECT_END_VERTEX_BLOCK2 7
    33 #define TA_CMD(i) ( (i) >> 29 )
    34 #define TA_CMD_END_LIST 0
    35 #define TA_CMD_CLIP 1
    36 #define TA_CMD_POLYGON_CONTEXT 4
    37 #define TA_CMD_SPRITE_CONTEXT 5
    38 #define TA_CMD_VERTEX 7
    40 #define TA_LIST_NONE -1
    41 #define TA_LIST_OPAQUE 0
    42 #define TA_LIST_OPAQUE_MOD 1
    43 #define TA_LIST_TRANS 2
    44 #define TA_LIST_TRANS_MOD 3
    45 #define TA_LIST_PUNCH_OUT 4
    46 #define TA_IS_MODIFIER_LIST(list) (list == TA_LIST_OPAQUE_MOD || list == TA_LIST_TRANS_MOD)
    48 #define TA_GROW_UP 0
    49 #define TA_GROW_DOWN 1
    51 #define TA_VERTEX_NONE                        -1
    52 #define TA_VERTEX_PACKED                      0x00
    53 #define TA_VERTEX_TEX_PACKED                  0x08
    54 #define TA_VERTEX_TEX_SPEC_PACKED             0x0C
    55 #define TA_VERTEX_TEX_UV16_PACKED             0x09
    56 #define TA_VERTEX_TEX_UV16_SPEC_PACKED        0x0D
    57 #define TA_VERTEX_FLOAT                       0x10
    58 #define TA_VERTEX_TEX_FLOAT                   0x18
    59 #define TA_VERTEX_TEX_SPEC_FLOAT              0x1C
    60 #define TA_VERTEX_TEX_UV16_FLOAT              0x19
    61 #define TA_VERTEX_TEX_UV16_SPEC_FLOAT         0x1D
    62 #define TA_VERTEX_INTENSITY                   0x20
    63 #define TA_VERTEX_TEX_INTENSITY               0x28
    64 #define TA_VERTEX_TEX_SPEC_INTENSITY          0x2C
    65 #define TA_VERTEX_TEX_UV16_INTENSITY          0x29
    66 #define TA_VERTEX_TEX_UV16_SPEC_INTENSITY     0x2D
    67 #define TA_VERTEX_PACKED_MOD                  0x40
    68 #define TA_VERTEX_TEX_PACKED_MOD              0x48
    69 #define TA_VERTEX_TEX_SPEC_PACKED_MOD         0x4C
    70 #define TA_VERTEX_TEX_UV16_PACKED_MOD         0x49
    71 #define TA_VERTEX_TEX_UV16_SPEC_PACKED_MOD    0x4D
    72 #define TA_VERTEX_INTENSITY_MOD               0x60
    73 #define TA_VERTEX_TEX_INTENSITY_MOD           0x68
    74 #define TA_VERTEX_TEX_SPEC_INTENSITY_MOD      0x6C
    75 #define TA_VERTEX_TEX_UV16_INTENSITY_MOD      0x69
    76 #define TA_VERTEX_TEX_UV16_SPEC_INTENSITY_MOD 0x6D
    77 #define TA_VERTEX_SPRITE                      0x80
    78 #define TA_VERTEX_TEX_SPRITE                  0x88
    79 #define TA_VERTEX_MOD_VOLUME                  0x81
    80 #define TA_VERTEX_LISTLESS                    0xFF
    82 #define TA_IS_NORMAL_POLY() (ta_status.current_vertex_type < TA_VERTEX_SPRITE)
    84 static int strip_lengths[4] = {3,4,6,8}; /* in vertexes */
    85 #define TA_POLYCMD_LISTTYPE(i) ( ((i) >> 24) & 0x0F )
    86 #define TA_POLYCMD_USELENGTH(i) ( i & 0x00800000 )
    87 #define TA_POLYCMD_LENGTH(i)  strip_lengths[((i >> 18) & 0x03)]
    88 #define TA_POLYCMD_CLIP(i)  ((i>>16)&0x03)
    89 #define TA_POLYCMD_CLIP_NONE 0
    90 #define TA_POLYCMD_CLIP_INSIDE 2
    91 #define TA_POLYCMD_CLIP_OUTSIDE 3
    92 #define TA_POLYCMD_COLOURFMT(i)  (i & 0x00000030)
    93 #define TA_POLYCMD_COLOURFMT_ARGB32 0x00000000
    94 #define TA_POLYCMD_COLOURFMT_FLOAT 0x00000010
    95 #define TA_POLYCMD_COLOURFMT_INTENSITY 0x00000020
    96 #define TA_POLYCMD_COLOURFMT_LASTINT 0x00000030
    98 #define TA_POLYCMD_MODIFIED 0x00000080
    99 #define TA_POLYCMD_FULLMOD  0x00000040
   100 #define TA_POLYCMD_TEXTURED 0x00000008
   101 #define TA_POLYCMD_SPECULAR 0x00000004
   102 #define TA_POLYCMD_SHADED 0x00000002
   103 #define TA_POLYCMD_UV16 0x00000001
   105 #define TA_POLYCMD_IS_SPECULAR(i) ((i & 0x0000000C)==0x0000000C) /* Only applies to textured polys */
   106 #define TA_POLYCMD_IS_FULLMOD(i) ((i & 0x000000C0)==0x000000C0)
   109 #define TA_IS_END_VERTEX(i) (i & 0x10000000)
   111 /** Note these are not the IEEE 754 definitions - the TA treats NANs
   112  * as if they were INFs of the appropriate sign.
   113  */
   114 #define TA_IS_INF(f) (((*((uint32_t *)&f)) & 0xFF800000) == 0x7F800000)
   115 #define TA_IS_NINF(f) (((*((uint32_t *)&f)) & 0xFF800000) == 0xFF800000)
   117 #define MIN3( x1, x2, x3 ) ( (x1)<(x2)? ((x1)<(x3)?(x1):(x3)) : ((x2)<(x3)?(x2):(x3)) )
   118 #define MAX3( x1, x2, x3 ) ( (x1)>(x2)? ((x1)>(x3)?(x1):(x3)) : ((x2)>(x3)?(x2):(x3)) )
   120 #define TILESLOT( x, y ) (ta_status.current_tile_matrix + (ta_status.current_tile_size * (y * ta_status.width+ x) << 2))
   122 extern char *video_base;
   123 #define PVRRAM(addr) (*(uint32_t *)(video_base + ((addr)&PVR2_RAM_MASK)))
   125 struct pvr2_ta_vertex {
   126     float x,y,z;
   127     uint32_t detail[8]; /* 0-8 detail words */
   128 };
   130 struct tile_bounds {
   131     int32_t x1, y1, x2, y2;
   132 };
   134 struct pvr2_ta_status {
   135     int32_t state;
   136     int32_t width, height; /* Tile resolution, ie 20x15 */
   137     int32_t tilelist_dir; /* Growth direction of the tilelist, 0 = up, 1 = down */
   138     uint32_t tilelist_size; /* Size of the tilelist segments */
   139     uint32_t tilelist_start; /* Initial address of the tilelist */
   140     uint32_t polybuf_start; /* Initial bank address of the polygon buffer (ie &0x00F00000) */
   141     int32_t current_vertex_type;
   142     uint32_t accept_vertexes; /* 0 = NO, 1 = YES */
   143     int32_t vertex_count; /* index of last start-vertex seen, or -1 if no vertexes 
   144      * are present
   145      */
   146     uint32_t max_vertex;     /* Maximum number of vertexes in the current polygon (3/4/6/8) */
   147     uint32_t current_list_type;
   148     uint32_t current_tile_matrix; /* Memory location of the first tile for the current list. */
   149     uint32_t current_tile_size; /* Size of the tile matrix space  in 32-bit words (0/8/16/32)*/
   150     uint32_t intensity1, intensity2;
   151     struct tile_bounds clip;
   152     int32_t clip_mode;
   153     /**
   154      * Current working object
   155      */
   156     int32_t poly_context_size;
   157     int32_t poly_vertex_size;
   158     int32_t poly_parity;
   159     uint32_t poly_context[5];
   160     uint32_t poly_pointer;
   161     struct tile_bounds last_triangle_bounds;
   162     struct pvr2_ta_vertex poly_vertex[8];
   163     uint32_t debug_output;
   164 };
   166 static struct pvr2_ta_status ta_status;
   168 static int tilematrix_sizes[4] = {0,8,16,32};
   170 /**
   171  * Convenience union - ta data is either 32-bit integer or 32-bit float.
   172  */
   173 union ta_data {
   174     unsigned int i;
   175     float f;
   176 };
   179 void pvr2_ta_reset() {
   180     ta_status.state = STATE_ERROR; /* State not valid until initialized */
   181     ta_status.debug_output = 0;
   182 }
   184 void pvr2_ta_save_state( FILE *f )
   185 {
   186     fwrite( &ta_status, sizeof(ta_status), 1, f );
   187 }
   189 int pvr2_ta_load_state( FILE *f )
   190 {
   191     if( fread( &ta_status, sizeof(ta_status), 1, f ) != 1 )
   192         return 1;
   193     return 0;
   194 }
   196 void pvr2_ta_init() {
   197     ta_status.state = STATE_IDLE;
   198     ta_status.current_list_type = -1;
   199     ta_status.current_vertex_type = -1;
   200     ta_status.poly_parity = 0;
   201     ta_status.vertex_count = 0;
   202     ta_status.max_vertex = 3;
   203     ta_status.current_vertex_type = TA_VERTEX_LISTLESS;
   204     ta_status.poly_vertex_size = 0;
   205     memset(&ta_status.poly_context[1], 0, 4);
   206     ta_status.last_triangle_bounds.x1 = -1;
   207     ta_status.accept_vertexes = TRUE;
   208     ta_status.clip.x1 = 0;
   209     ta_status.clip.y1 = 0;
   210     ta_status.clip_mode = TA_POLYCMD_CLIP_NONE;
   212     uint32_t size = MMIO_READ( PVR2, TA_TILESIZE );
   213     ta_status.width = (size & 0xFFFF) + 1;
   214     ta_status.height = (size >> 16) + 1;
   215     ta_status.clip.x2 = ta_status.width-1;
   216     ta_status.clip.y2 = ta_status.height-1;
   217     uint32_t control = MMIO_READ( PVR2, TA_TILECFG );
   218     ta_status.tilelist_dir = (control >> 20) & 0x01;
   219     ta_status.tilelist_size = tilematrix_sizes[ (control & 0x03) ];
   220     MMIO_WRITE( PVR2, TA_POLYPOS, MMIO_READ( PVR2, TA_POLYBASE ) );
   221     uint32_t plistpos = MMIO_READ( PVR2, TA_LISTBASE ) >> 2;
   222     if( ta_status.tilelist_dir == TA_GROW_DOWN ) {
   223         plistpos -= ta_status.tilelist_size;
   224     }
   225     MMIO_WRITE( PVR2, TA_LISTPOS, plistpos );
   226     ta_status.tilelist_start = plistpos;
   227     ta_status.polybuf_start = MMIO_READ( PVR2, TA_POLYBASE ) & 0x00F00000;
   228 }
   230 static uint32_t parse_float_colour( float a, float r, float g, float b ) {
   231     int ai,ri,gi,bi;
   233     if( TA_IS_INF(a) ) {
   234         ai = 255;
   235     } else {
   236         ai = 256 * CLAMP(a,0.0,1.0) - 1;
   237         if( ai < 0 ) ai = 0;
   238     }
   239     if( TA_IS_INF(r) ) {
   240         ri = 255;
   241     } else {
   242         ri = 256 * CLAMP(r,0.0,1.0) - 1;
   243         if( ri < 0 ) ri = 0;
   244     }
   245     if( TA_IS_INF(g) ) {
   246         gi = 255;
   247     } else {
   248         gi = 256 * CLAMP(g,0.0,1.0) - 1;
   249         if( gi < 0 ) gi = 0;
   250     }
   251     if( TA_IS_INF(b) ) {
   252         bi = 255;
   253     } else {
   254         bi = 256 * CLAMP(b,0.0,1.0) - 1;
   255         if( bi < 0 ) bi = 0;
   256     }
   257     return (ai << 24) | (ri << 16) | (gi << 8) | bi;
   258 }
   260 static uint32_t parse_intensity_colour( uint32_t base, float intensity )
   261 {
   262     unsigned int i = (unsigned int)(256 * CLAMP(intensity, 0.0,1.0));
   264     return
   265     (((((base & 0xFF) * i) & 0xFF00) |
   266             (((base & 0xFF00) * i) & 0xFF0000) |
   267             (((base & 0xFF0000) * i) & 0xFF000000)) >> 8) |
   268             (base & 0xFF000000);
   269 }
   271 /**
   272  * Initialize the specified TA list.
   273  */
   274 static void ta_init_list( unsigned int listtype ) {
   275     int config = MMIO_READ( PVR2, TA_TILECFG );
   276     int tile_matrix = MMIO_READ( PVR2, TA_TILEBASE );
   277     int list_end = MMIO_READ( PVR2, TA_LISTEND );
   279     ta_status.current_tile_matrix = tile_matrix;
   281     /* If the list grows down, the end must be < tile matrix start. 
   282      * If it grows up, the end must be > tile matrix start.
   283      * Don't ask me why, it just does...
   284      */
   285     if( ((ta_status.tilelist_dir == TA_GROW_DOWN && list_end <= tile_matrix) ||
   286             (ta_status.tilelist_dir == TA_GROW_UP && list_end >= tile_matrix )) &&
   287             listtype <= TA_LIST_PUNCH_OUT ) {
   288         int i;
   289         uint32_t *p;
   290         for( i=0; i < listtype; i++ ) {
   291             int size = tilematrix_sizes[(config & 0x03)] << 2;
   292             ta_status.current_tile_matrix += ta_status.width * ta_status.height * size;
   293             config >>= 4;
   294         }
   295         ta_status.current_tile_size = tilematrix_sizes[(config & 0x03)];
   297         /* Initialize each tile to 0xF0000000 */
   298         if( ta_status.current_tile_size != 0 ) {
   299             p = (uint32_t *)(video_base + ta_status.current_tile_matrix);
   300             for( i=0; i< ta_status.width * ta_status.height; i++ ) {
   301                 *p = 0xF0000000;
   302                 p += ta_status.current_tile_size;
   303             }
   304         }
   305     } else {
   306         ta_status.current_tile_size = 0;
   307     }
   309     if( tile_matrix == list_end ) {
   310         ta_status.current_tile_size = 0;
   311     }
   313     ta_status.state = STATE_IN_LIST;
   314     ta_status.current_list_type = listtype;
   315     ta_status.last_triangle_bounds.x1 = -1;
   316 }
   318 static int list_events[5] = {EVENT_PVR_OPAQUE_DONE, EVENT_PVR_OPAQUEMOD_DONE, 
   319         EVENT_PVR_TRANS_DONE, EVENT_PVR_TRANSMOD_DONE,
   320         EVENT_PVR_PUNCHOUT_DONE };
   322 static void ta_end_list() {
   323     if( ta_status.current_list_type != TA_LIST_NONE ) {
   324         asic_event( list_events[ta_status.current_list_type] );
   325     }
   326     ta_status.current_list_type = TA_LIST_NONE;
   327     ta_status.current_vertex_type = TA_VERTEX_LISTLESS;
   328     ta_status.poly_vertex_size = 0;
   329     memset(&ta_status.poly_context[1], 0, 4);
   330     ta_status.state = STATE_IDLE;
   331 }
   333 static void ta_bad_input_error() {
   334     asic_event( EVENT_PVR_BAD_INPUT );
   335 }
   337 /**
   338  * Write data out to the polygon buffer.
   339  * If the end-of-buffer is reached, asserts EVENT_PVR_PRIM_ALLOC_FAIL
   340  * @param data to be written
   341  * @param length Number of 32-bit words to write.
   342  * @return number of words actually written
   343  */
   344 static int ta_write_polygon_buffer( uint32_t *data, int length )
   345 {
   346     int rv;
   347     int posn = MMIO_READ( PVR2, TA_POLYPOS );
   348     int end = MMIO_READ( PVR2, TA_POLYEND );
   349     uint32_t *target = (uint32_t *)(video_base + posn);
   350     for( rv=0; rv < length; rv++ ) {
   351         if( posn == end ) {
   352             asic_event( EVENT_PVR_PRIM_ALLOC_FAIL );
   353             //	    ta_status.state = STATE_ERROR;
   354             break;
   355         }
   356         if( posn < PVR2_RAM_SIZE ) {
   357             *target++ = *data++;
   358         }
   359         posn += 4;
   360     }
   362     MMIO_WRITE( PVR2, TA_POLYPOS, posn );
   363     return rv;
   364 }
   366 #define TA_NO_ALLOC 0xFFFFFFFF
   368 /**
   369  * Allocate a new tile list block from the grow space and update the
   370  * word at reference to be a link to the new block.
   371  */
   372 static uint32_t ta_alloc_tilelist( uint32_t reference ) {
   373     uint32_t posn = MMIO_READ( PVR2, TA_LISTPOS );
   374     uint32_t limit = MMIO_READ( PVR2, TA_LISTEND ) >> 2;
   375     uint32_t newposn;
   376     if( ta_status.tilelist_dir == TA_GROW_DOWN ) {
   377         newposn = posn - ta_status.tilelist_size;
   378         if( posn == limit ) {
   379             PVRRAM(posn<<2) = 0xF0000000;
   380             PVRRAM(reference) = 0xE0000000 | (posn<<2);
   381             return TA_NO_ALLOC;
   382         } else if( posn < limit ) {
   383             PVRRAM(reference) = 0xE0000000 | (posn<<2);
   384             return TA_NO_ALLOC;
   385         } else if( newposn <= limit ) {
   386         } else if( newposn <= (limit + ta_status.tilelist_size) ) {
   387             asic_event( EVENT_PVR_MATRIX_ALLOC_FAIL );
   388             MMIO_WRITE( PVR2, TA_LISTPOS, newposn );
   389         } else {
   390             MMIO_WRITE( PVR2, TA_LISTPOS, newposn );
   391         }
   392         PVRRAM(reference) = 0xE0000000 | (posn<<2);
   393         return posn << 2;
   394     } else {
   395         newposn = posn + ta_status.tilelist_size;
   396         if( posn == limit ) {
   397             PVRRAM(posn<<2) = 0xF0000000;
   398             PVRRAM(reference) = 0xE0000000 | (posn<<2);
   399             return TA_NO_ALLOC;
   400         } else if ( posn > limit ) {
   401             PVRRAM(reference) = 0xE0000000 | (posn<<2);
   402             return TA_NO_ALLOC;
   403         } else if( newposn >= limit ) {
   404         } else if( newposn >= (limit - ta_status.tilelist_size) ) {
   405             asic_event( EVENT_PVR_MATRIX_ALLOC_FAIL );
   406             MMIO_WRITE( PVR2, TA_LISTPOS, newposn );
   407         } else {
   408             MMIO_WRITE( PVR2, TA_LISTPOS, newposn );
   409         }	    
   410         PVRRAM(reference) = 0xE0000000 | (posn<<2);
   411         return posn << 2;
   412     }
   413 }
   415 /**
   416  * Write a tile entry out to the matrix.
   417  */
   418 static void ta_write_tile_entry( int x, int y, uint32_t tile_entry ) {
   419     uint32_t tile = TILESLOT(x,y);
   420     uint32_t tilestart = tile;
   421     uint32_t value;
   422     uint32_t lasttri = 0;
   423     int i;
   425     if( ta_status.clip_mode == TA_POLYCMD_CLIP_OUTSIDE &&
   426             x >= ta_status.clip.x1 && x <= ta_status.clip.x2 &&
   427             y >= ta_status.clip.y1 && y <= ta_status.clip.y2 ) {
   428         /* Tile clipped out */
   429         return;
   430     }
   432     if( (tile_entry & 0x80000000) && 
   433             ta_status.last_triangle_bounds.x1 != -1 &&
   434             ta_status.last_triangle_bounds.x1 <= x &&
   435             ta_status.last_triangle_bounds.x2 >= x &&
   436             ta_status.last_triangle_bounds.y1 <= y &&
   437             ta_status.last_triangle_bounds.y2 >= y ) {
   438         /* potential for triangle stacking */
   439         lasttri = tile_entry & 0xE1E00000;
   440     }
   443     if( PVRRAM(tile) == 0xF0000000 ) {
   444         PVRRAM(tile) = tile_entry;
   445         PVRRAM(tile+4) = 0xF0000000;
   446         return;
   447     }
   449     while(1) {
   450         value = PVRRAM(tile);
   451         for( i=1; i<ta_status.current_tile_size; i++ ) {
   452             tile += 4;
   453             uint32_t nextval = PVRRAM(tile);
   454             if( nextval == 0xF0000000 ) {
   455                 if( lasttri != 0 && lasttri == (value&0xE1E00000) ) {
   456                     int count = (value & 0x1E000000) + 0x02000000;
   457                     if( count < 0x20000000 ) {
   458                         PVRRAM(tile-4) = (value & 0xE1FFFFFF) | count;
   459                         return;
   460                     }
   461                 }
   462                 if( i < ta_status.current_tile_size-1 ) {
   463                     PVRRAM(tile) = tile_entry;
   464                     PVRRAM(tile+4) = 0xF0000000;
   465                     return;
   466                 }
   467             }
   468             value = nextval;
   469         }
   471         if( value == 0xF0000000 ) {
   472             tile = ta_alloc_tilelist(tile);
   473             if( tile != TA_NO_ALLOC ) {
   474                 PVRRAM(tile) = tile_entry;
   475                 PVRRAM(tile+4) = 0xF0000000;
   476             }
   477             return;
   478         } else if( (value & 0xFF000000) == 0xE0000000 ) {
   479             value &= 0x00FFFFFF;
   480             if( value == tilestart )
   481                 return; /* Loop */
   482             tilestart = tile = value;
   483         } else {
   484             /* This should never happen */
   485             return;
   486         }
   487     }
   488 }
   490 /**
   491  * Write a completed polygon out to the memory buffers 
   492  * OPTIMIZEME: This is not terribly efficient at the moment.
   493  */
   494 static void ta_commit_polygon( ) {
   495     int i, x, y;
   496     int tx[ta_status.vertex_count], ty[ta_status.vertex_count];
   497     struct tile_bounds triangle_bound[ta_status.vertex_count - 2];
   498     struct tile_bounds polygon_bound;
   499     uint32_t poly_context[5];
   501     memcpy( poly_context, ta_status.poly_context, ta_status.poly_context_size * 4 );
   503     /* Compute the tile coordinates for each vertex (need to be careful with
   504      * clamping here)
   505      */
   506     for( i=0; i<ta_status.vertex_count; i++ ) {
   507         if( ta_status.poly_vertex[i].x < 0.0 || TA_IS_NINF(ta_status.poly_vertex[i].x) ) {
   508             tx[i] = -1;
   509         } else if( ta_status.poly_vertex[i].x > (float)INT_MAX || TA_IS_INF(ta_status.poly_vertex[i].x) ) {
   510             tx[i] = INT_MAX/32;
   511         } else {
   512             tx[i] = (int)(ta_status.poly_vertex[i].x / 32.0);
   513         }
   514         if( ta_status.poly_vertex[i].y < 0.0 || TA_IS_NINF(ta_status.poly_vertex[i].y)) {
   515             ty[i] = -1;
   516         } else if( ta_status.poly_vertex[i].y > (float)INT_MAX || TA_IS_INF(ta_status.poly_vertex[i].y) ) {
   517             ty[i] = INT_MAX/32;
   518         } else {
   519             ty[i] = (int)(ta_status.poly_vertex[i].y / 32.0);
   520         }
   522     }
   524     /* Compute bounding box for each triangle individually, as well
   525      * as the overall polygon.
   526      */
   528     for( i=0; i<ta_status.vertex_count-2; i++ ) {
   529         triangle_bound[i].x1 = MIN3(tx[i],tx[i+1],tx[i+2]);
   530         triangle_bound[i].x2 = MAX3(tx[i],tx[i+1],tx[i+2]);
   531         triangle_bound[i].y1 = MIN3(ty[i],ty[i+1],ty[i+2]);
   532         triangle_bound[i].y2 = MAX3(ty[i],ty[i+1],ty[i+2]);
   533         if( i == 0 ) {
   534             polygon_bound.x1 = triangle_bound[0].x1;
   535             polygon_bound.y1 = triangle_bound[0].y1;
   536             polygon_bound.x2 = triangle_bound[0].x2;
   537             polygon_bound.y2 = triangle_bound[0].y2;
   538         } else {
   539             polygon_bound.x1 = MIN(polygon_bound.x1, triangle_bound[i].x1);
   540             polygon_bound.x2 = MAX(polygon_bound.x2, triangle_bound[i].x2);
   541             polygon_bound.y1 = MIN(polygon_bound.y1, triangle_bound[i].y1);
   542             polygon_bound.y2 = MAX(polygon_bound.y2, triangle_bound[i].y2);
   543         }
   544     }
   546     /* Clamp the polygon bounds to the frustum */
   547     if( polygon_bound.x1 < 0 ) polygon_bound.x1 = 0;
   548     if( polygon_bound.x2 >= ta_status.width ) polygon_bound.x2 = ta_status.width-1;
   549     if( polygon_bound.y1 < 0 ) polygon_bound.y1 = 0;
   550     if( polygon_bound.y2 >= ta_status.width ) polygon_bound.y2 = ta_status.height-1;
   552     /* Set the "single tile" flag if it's entirely contained in 1 tile */
   553     if( polygon_bound.x1 == polygon_bound.x2 &&
   554             polygon_bound.y1 == polygon_bound.y2 ) {
   555         poly_context[0] |= 0x00200000;
   556     }
   558     /* If the polygon is entirely clipped, don't even write the polygon data */
   559     switch( ta_status.clip_mode ) {
   560     case TA_POLYCMD_CLIP_NONE:
   561         if( polygon_bound.x2 < 0 || polygon_bound.x1 >= ta_status.width ||
   562                 polygon_bound.y2 < 0 || polygon_bound.y1 >= ta_status.height ) {
   563             return;
   564         }
   565         break;
   566     case TA_POLYCMD_CLIP_INSIDE:
   567         if( polygon_bound.x2 < ta_status.clip.x1 || polygon_bound.x1 > ta_status.clip.x2 ||
   568                 polygon_bound.y2 < ta_status.clip.y1 || polygon_bound.y1 > ta_status.clip.y2 ) {
   569             return;
   570         } else {
   571             /* Clamp to clip bounds */
   572             if( polygon_bound.x1 < ta_status.clip.x1 ) polygon_bound.x1 = ta_status.clip.x1;
   573             if( polygon_bound.x2 > ta_status.clip.x2 ) polygon_bound.x2 = ta_status.clip.x2;
   574             if( polygon_bound.y1 < ta_status.clip.y1 ) polygon_bound.y1 = ta_status.clip.y1;
   575             if( polygon_bound.y2 > ta_status.clip.y2 ) polygon_bound.y2 = ta_status.clip.y2;
   576         }
   577         break;
   578     case TA_POLYCMD_CLIP_OUTSIDE:
   579         if( polygon_bound.x1 >= ta_status.clip.x1 && polygon_bound.x2 <= ta_status.clip.x2 &&
   580                 polygon_bound.y1 >= ta_status.clip.y1 && polygon_bound.y2 <= ta_status.clip.y2 ) {
   581             return;
   582         }
   583         break;
   584     }
   586     /* Ok, we're good to go - write out the polygon first */
   587     uint32_t tile_entry = (MMIO_READ( PVR2, TA_POLYPOS ) - ta_status.polybuf_start) >> 2 | 
   588     ta_status.poly_pointer;
   590     int status = ta_write_polygon_buffer( poly_context, ta_status.poly_context_size );
   591     if( status == 0 ) {
   592         /* No memory available - abort */
   593         return;
   594     } else {
   595         for( i=0; i<ta_status.vertex_count && status != 0; i++ ) {
   596             status = ta_write_polygon_buffer( (uint32_t *)(&ta_status.poly_vertex[i]), 3 + ta_status.poly_vertex_size );
   597         }
   598     }
   600     if( ta_status.current_tile_size == 0 ) {
   601         /* No memory for tile entry, so don't write anything */
   602         return;
   603     }
   605     /* And now the tile entries. Triangles are different from everything else */
   606     if( ta_status.vertex_count == 3 ) {
   607         tile_entry |= 0x80000000;
   608         for( y=polygon_bound.y1; y<=polygon_bound.y2; y++ ) {
   609             for( x=polygon_bound.x1; x<=polygon_bound.x2; x++ ) {
   610                 ta_write_tile_entry( x,y,tile_entry );
   611             }
   612         }
   613         ta_status.last_triangle_bounds.x1 = polygon_bound.x1;
   614         ta_status.last_triangle_bounds.y1 = polygon_bound.y1;
   615         ta_status.last_triangle_bounds.x2 = polygon_bound.x2;
   616         ta_status.last_triangle_bounds.y2 = polygon_bound.y2;
   617     } else if( ta_status.current_vertex_type == TA_VERTEX_SPRITE ||
   618             ta_status.current_vertex_type == TA_VERTEX_TEX_SPRITE ) {
   619         tile_entry |= 0xA0000000;
   620         for( y=polygon_bound.y1; y<=polygon_bound.y2; y++ ) {
   621             for( x=polygon_bound.x1; x<=polygon_bound.x2; x++ ) {
   622                 ta_write_tile_entry( x,y,tile_entry );
   623             }
   624         }
   625         ta_status.last_triangle_bounds.x1 = polygon_bound.x1;
   626         ta_status.last_triangle_bounds.y1 = polygon_bound.y1;
   627         ta_status.last_triangle_bounds.x2 = polygon_bound.x2;
   628         ta_status.last_triangle_bounds.y2 = polygon_bound.y2;
   629     } else {
   630         for( y=polygon_bound.y1; y<=polygon_bound.y2; y++ ) {
   631             for( x=polygon_bound.x1; x<=polygon_bound.x2; x++ ) {
   632                 uint32_t entry = tile_entry;
   633                 for( i=0; i<ta_status.vertex_count-2; i++ ) {
   634                     if( triangle_bound[i].x1 <= x && triangle_bound[i].x2 >= x &&
   635                             triangle_bound[i].y1 <= y && triangle_bound[i].y2 >= y ) {
   636                         entry |= (0x40000000>>i);
   637                     }
   638                 }
   639                 ta_write_tile_entry( x, y, entry );
   640             }
   641         }
   642         ta_status.last_triangle_bounds.x1 = -1;
   643     }
   644 }
   646 /**
   647  * Variant of ta_split_polygon called when vertex_count == max_vertex, but 
   648  * the client hasn't sent the LAST VERTEX flag. Commit the poly as normal
   649  * first, then start a new poly with the first 2 vertexes taken from the 
   650  * current one.
   651  */
   652 static void ta_split_polygon() {
   653     ta_commit_polygon();
   654     if( TA_IS_NORMAL_POLY() ) { 
   655         /* This only applies to ordinary polys - Sprites + modifier lists are
   656          * handled differently
   657          */
   658         if( ta_status.vertex_count == 3 ) {
   659             /* Triangles use an odd/even scheme */
   660             if( ta_status.poly_parity == 0 ) {
   661                 memcpy( &ta_status.poly_vertex[0], &ta_status.poly_vertex[2], 
   662                         sizeof(struct pvr2_ta_vertex) );
   663                 ta_status.poly_parity = 1;
   664             } else {
   665                 memcpy( &ta_status.poly_vertex[1], &ta_status.poly_vertex[2],
   666                         sizeof(struct pvr2_ta_vertex) );
   667                 ta_status.poly_parity = 0;
   668             }
   669         } else {
   670             /* Everything else just uses the last 2 vertexes in order */
   671             memcpy( &ta_status.poly_vertex[0], &ta_status.poly_vertex[ta_status.vertex_count-2], 
   672                     sizeof(struct pvr2_ta_vertex)*2 );
   673             ta_status.poly_parity = 0;
   674         }
   675         ta_status.vertex_count = 2;
   676     } else {
   677         ta_status.vertex_count = 0;
   678     }
   679 }
   681 /**
   682  * Parse the polygon context block and setup the internal state to receive
   683  * vertexes.
   684  * @param data 32 bytes of parameter data.
   685  */
   686 static void ta_parse_polygon_context( union ta_data *data ) {
   687     int colourfmt = TA_POLYCMD_COLOURFMT(data[0].i);
   688     if( TA_POLYCMD_USELENGTH(data[0].i) ) {
   689         ta_status.max_vertex = TA_POLYCMD_LENGTH(data[0].i);
   690     }
   691     ta_status.clip_mode = TA_POLYCMD_CLIP(data[0].i);
   692     if( ta_status.clip_mode == 1 ) { /* Reserved - treat as CLIP_INSIDE */
   693         ta_status.clip_mode = TA_POLYCMD_CLIP_INSIDE;
   694     }
   695     ta_status.vertex_count = 0;
   696     ta_status.poly_context[0] = 
   697         (data[1].i & 0xFC1FFFFF) | ((data[0].i & 0x0B) << 22);
   698     ta_status.poly_context[1] = data[2].i;
   699     ta_status.poly_context[3] = data[4].i;
   700     ta_status.poly_parity = 0;
   701     if( data[0].i & TA_POLYCMD_TEXTURED ) {
   702         ta_status.current_vertex_type = data[0].i & 0x0D;
   703         ta_status.poly_context[2] = data[3].i;
   704         ta_status.poly_context[4] = data[5].i;
   705         if( data[0].i & TA_POLYCMD_SPECULAR ) {
   706             ta_status.poly_context[0] |= 0x01000000;
   707             ta_status.poly_vertex_size = 4;
   708         } else {
   709             ta_status.poly_vertex_size = 3;
   710         }
   711         if( data[0].i & TA_POLYCMD_UV16 ) {
   712             ta_status.poly_vertex_size--;
   713         }
   714     } else {
   715         ta_status.current_vertex_type = 0;
   716         ta_status.poly_vertex_size = 1;
   717         ta_status.poly_context[2] = 0;
   718         ta_status.poly_context[4] = 0;
   719     }
   721     ta_status.poly_pointer = (ta_status.poly_vertex_size << 21);
   722     ta_status.poly_context_size = 3;
   723     if( data[0].i & TA_POLYCMD_MODIFIED ) {
   724         ta_status.poly_pointer |= 0x01000000;
   725         if( data[0].i & TA_POLYCMD_FULLMOD ) {
   726             ta_status.poly_context_size = 5;
   727             ta_status.poly_vertex_size <<= 1;
   728             ta_status.current_vertex_type |= 0x40;
   729             /* Modified/float not supported - behaves as per last intensity */
   730             if( colourfmt == TA_POLYCMD_COLOURFMT_FLOAT ) {
   731                 colourfmt = TA_POLYCMD_COLOURFMT_LASTINT;
   732             }
   733         }
   734     }
   736     if( colourfmt == TA_POLYCMD_COLOURFMT_INTENSITY ) {
   737         if( TA_POLYCMD_IS_FULLMOD(data[0].i) ||
   738                 TA_POLYCMD_IS_SPECULAR(data[0].i) ) {
   739             ta_status.state = STATE_EXPECT_POLY_BLOCK2;
   740         } else {
   741             ta_status.intensity1 = 
   742                 parse_float_colour( data[4].f, data[5].f, data[6].f, data[7].f );
   743         }
   744     } else if( colourfmt == TA_POLYCMD_COLOURFMT_LASTINT ) {
   745         colourfmt = TA_POLYCMD_COLOURFMT_INTENSITY;
   746     }
   748     ta_status.current_vertex_type |= colourfmt;
   749 }
   751 /**
   752  * Parse the modifier volume context block and setup the internal state to 
   753  * receive modifier vertexes.
   754  * @param data 32 bytes of parameter data.
   755  */
   756 static void ta_parse_modifier_context( union ta_data *data ) {
   757     ta_status.current_vertex_type = TA_VERTEX_MOD_VOLUME;
   758     ta_status.poly_vertex_size = 0;
   759     ta_status.clip_mode = TA_POLYCMD_CLIP(data[0].i);
   760     if( ta_status.clip_mode == 1 ) { /* Reserved - treat as CLIP_INSIDE */
   761         ta_status.clip_mode = TA_POLYCMD_CLIP_INSIDE;
   762     }
   763     ta_status.poly_context_size = 3;
   764     ta_status.poly_context[0] = (data[1].i & 0xFC1FFFFF) |
   765     ((data[0].i & 0x0B)<<22);
   766     if( TA_POLYCMD_IS_SPECULAR(data[0].i) ) {
   767         ta_status.poly_context[0] |= 0x01000000;
   768     }
   769     ta_status.poly_context[1] = 0;
   770     ta_status.poly_context[2] = 0;
   771     ta_status.vertex_count = 0;
   772     ta_status.max_vertex = 3;
   773     ta_status.poly_pointer = 0;
   774 }
   776 /**
   777  * Parse the sprite context block and setup the internal state to receive
   778  * vertexes.
   779  * @param data 32 bytes of parameter data.
   780  */
   781 static void ta_parse_sprite_context( union ta_data *data ) {
   782     ta_status.poly_context_size = 3;
   783     ta_status.poly_context[0] = (data[1].i & 0xFC1FFFFF) |
   784     ((data[0].i & 0x0B)<<22) | 0x00400000;
   785     ta_status.clip_mode = TA_POLYCMD_CLIP(data[0].i);
   786     if( ta_status.clip_mode == 1 ) { /* Reserved - treat as CLIP_INSIDE */
   787         ta_status.clip_mode = TA_POLYCMD_CLIP_INSIDE;
   788     }
   789     if( TA_POLYCMD_IS_SPECULAR(data[0].i) ) {
   790         ta_status.poly_context[0] |= 0x01000000;
   791     }
   792     ta_status.poly_context[1] = data[2].i;
   793     ta_status.poly_context[2] = data[3].i;
   794     if( data[0].i & TA_POLYCMD_TEXTURED ) {
   795         ta_status.poly_vertex_size = 2;
   796         ta_status.poly_vertex[2].detail[1] = data[4].i;
   797         ta_status.current_vertex_type = TA_VERTEX_TEX_SPRITE;
   798     } else {
   799         ta_status.poly_vertex_size = 1;
   800         ta_status.poly_vertex[2].detail[0] = data[4].i;
   801         ta_status.current_vertex_type = TA_VERTEX_SPRITE;
   802     }
   803     ta_status.vertex_count = 0;
   804     ta_status.max_vertex = 4;
   805     ta_status.poly_pointer = (ta_status.poly_vertex_size << 21);
   806 }
   808 /**
   809  * Copy the last read vertex into all vertexes up to max_vertex. Used for
   810  * Aborted polygons under some circumstances.
   811  */
   812 static void ta_fill_vertexes( ) {
   813     int i;
   814     for( i=ta_status.vertex_count; i<ta_status.max_vertex; i++ ) {
   815         memcpy( &ta_status.poly_vertex[i], &ta_status.poly_vertex[ta_status.vertex_count-1],
   816                 sizeof( struct pvr2_ta_vertex ) );
   817     }
   818 }
   820 static void ta_parse_vertex( union ta_data *data ) {
   821     struct pvr2_ta_vertex *vertex = &ta_status.poly_vertex[ta_status.vertex_count];
   822     vertex->x = data[1].f;
   823     vertex->y = data[2].f;
   824     vertex->z = data[3].f;
   826     switch( ta_status.current_vertex_type ) {
   827     case TA_VERTEX_PACKED:
   828         vertex->detail[0] = data[6].i;
   829         break;
   830     case TA_VERTEX_FLOAT:
   831         vertex->detail[0] = parse_float_colour( data[4].f, data[5].f, data[6].f, data[7].f );
   832         break;
   833     case TA_VERTEX_INTENSITY:
   834         vertex->detail[0] = parse_intensity_colour( ta_status.intensity1, data[6].f );
   835         break;
   837     case TA_VERTEX_TEX_SPEC_PACKED:
   838         vertex->detail[3] = data[7].i; /* ARGB */
   839         /* Fallthrough */
   840     case TA_VERTEX_TEX_PACKED:
   841         vertex->detail[0] = data[4].i; /* U */
   842         vertex->detail[1] = data[5].i; /* V */
   843         vertex->detail[2] = data[6].i; /* ARGB */
   844         break;
   845     case TA_VERTEX_TEX_UV16_SPEC_PACKED:
   846         vertex->detail[2] = data[7].i; /* ARGB */
   847         /* Fallthrough */
   848     case TA_VERTEX_TEX_UV16_PACKED:
   849         vertex->detail[0] = data[4].i; /* UV */
   850         vertex->detail[1] = data[6].i; /* ARGB */
   851         break;
   853     case TA_VERTEX_TEX_FLOAT:
   854     case TA_VERTEX_TEX_SPEC_FLOAT:
   855         vertex->detail[0] = data[4].i; /* U */
   856         vertex->detail[1] = data[5].i; /* UV */
   857         ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   858         break;
   859     case TA_VERTEX_TEX_UV16_FLOAT:
   860     case TA_VERTEX_TEX_UV16_SPEC_FLOAT:
   861         vertex->detail[0] = data[4].i; /* UV */
   862         ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   863         break;
   865     case TA_VERTEX_TEX_SPEC_INTENSITY:
   866         vertex->detail[3] = parse_intensity_colour( ta_status.intensity2, data[7].f );
   867         /* Fallthrough */
   868     case TA_VERTEX_TEX_INTENSITY:
   869         vertex->detail[0] = data[4].i; /* U */
   870         vertex->detail[1] = data[5].i; /* V */
   871         vertex->detail[2] = parse_intensity_colour( ta_status.intensity1, data[6].f );
   872         break;
   873     case TA_VERTEX_TEX_UV16_SPEC_INTENSITY:
   874         vertex->detail[2] = parse_intensity_colour( ta_status.intensity2, data[7].f );
   875         /* Fallthrough */
   876     case TA_VERTEX_TEX_UV16_INTENSITY:
   877         vertex->detail[0] = data[4].i; /* UV */
   878         vertex->detail[1] = parse_intensity_colour( ta_status.intensity1, data[6].f );
   879         break;
   881     case TA_VERTEX_PACKED_MOD:
   882         vertex->detail[0] = data[4].i; /* ARGB */
   883         vertex->detail[1] = data[5].i; /* ARGB */
   884         break;
   885     case TA_VERTEX_INTENSITY_MOD:
   886         vertex->detail[0] = parse_intensity_colour( ta_status.intensity1, data[4].f );
   887         vertex->detail[1] = parse_intensity_colour( ta_status.intensity2, data[5].f );
   888         break;
   890     case TA_VERTEX_TEX_SPEC_PACKED_MOD:
   891         vertex->detail[3] = data[7].i; /* ARGB0 */
   892         /* Fallthrough */
   893     case TA_VERTEX_TEX_PACKED_MOD:
   894         vertex->detail[0] = data[4].i; /* U0 */
   895         vertex->detail[1] = data[5].i; /* V0 */
   896         vertex->detail[2] = data[6].i; /* ARGB0 */
   897         ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   898         break;
   899     case TA_VERTEX_TEX_UV16_SPEC_PACKED_MOD:
   900         vertex->detail[2] = data[7].i; /* ARGB0 */
   901         /* Fallthrough */
   902     case TA_VERTEX_TEX_UV16_PACKED_MOD:
   903         vertex->detail[0] = data[4].i; /* UV0 */
   904         vertex->detail[1] = data[6].i; /* ARGB0 */
   905         ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   906         break;
   908     case TA_VERTEX_TEX_SPEC_INTENSITY_MOD:
   909         vertex->detail[3] = parse_intensity_colour( ta_status.intensity1, data[7].f );
   910         /* Fallthrough */
   911     case TA_VERTEX_TEX_INTENSITY_MOD:
   912         vertex->detail[0] = data[4].i; /* U0 */
   913         vertex->detail[1] = data[5].i; /* V0 */
   914         vertex->detail[2] = parse_intensity_colour( ta_status.intensity1, data[6].f );
   915         ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   916         break;
   917     case TA_VERTEX_TEX_UV16_SPEC_INTENSITY_MOD:
   918         vertex->detail[2] = parse_intensity_colour( ta_status.intensity1, data[7].f );
   919         /* Fallthrough */
   920     case TA_VERTEX_TEX_UV16_INTENSITY_MOD:
   921         vertex->detail[0] = data[4].i; /* UV0 */
   922         vertex->detail[1] = parse_intensity_colour( ta_status.intensity1, data[6].f );
   923         ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   924         break;
   926     case TA_VERTEX_SPRITE:
   927     case TA_VERTEX_TEX_SPRITE:
   928     case TA_VERTEX_MOD_VOLUME:
   929     case TA_VERTEX_LISTLESS:
   930         vertex++;
   931         vertex->x = data[4].f;
   932         vertex->y = data[5].f;
   933         vertex->z = data[6].f;
   934         vertex++;
   935         vertex->x = data[7].f;
   936         ta_status.vertex_count += 2;
   937         ta_status.state = STATE_EXPECT_VERTEX_BLOCK2;
   938         break;
   939     }
   940     ta_status.vertex_count++;
   941 }
   943 static void ta_parse_vertex_block2( union ta_data *data ) {
   944     struct pvr2_ta_vertex *vertex = &ta_status.poly_vertex[ta_status.vertex_count-1];
   946     switch( ta_status.current_vertex_type ) {
   947     case TA_VERTEX_TEX_SPEC_FLOAT:
   948         vertex->detail[3] = parse_float_colour( data[4].f, data[5].f, data[6].f, data[7].f );
   949         /* Fallthrough */
   950     case TA_VERTEX_TEX_FLOAT:
   951         vertex->detail[2] = parse_float_colour( data[0].f, data[1].f, data[2].f, data[3].f );
   952         break;
   953     case TA_VERTEX_TEX_UV16_SPEC_FLOAT:
   954         vertex->detail[2] = parse_float_colour( data[4].f, data[5].f, data[6].f, data[7].f );
   955         /* Fallthrough */
   956     case TA_VERTEX_TEX_UV16_FLOAT:
   957         vertex->detail[1] = parse_float_colour( data[0].f, data[1].f, data[2].f, data[3].f );
   958         break;
   959     case TA_VERTEX_TEX_PACKED_MOD:
   960         vertex->detail[3] = data[0].i; /* U1 */
   961         vertex->detail[4] = data[1].i; /* V1 */
   962         vertex->detail[5] = data[2].i; /* ARGB1 */
   963         break;
   964     case TA_VERTEX_TEX_SPEC_PACKED_MOD:
   965         vertex->detail[4] = data[0].i; /* U1 */
   966         vertex->detail[5] = data[1].i; /* V1 */
   967         vertex->detail[6] = data[2].i; /* ARGB1 */
   968         vertex->detail[7] = data[3].i; /* ARGB1 */
   969         break;
   970     case TA_VERTEX_TEX_UV16_PACKED_MOD:
   971         vertex->detail[2] = data[0].i; /* UV1 */
   972         vertex->detail[3] = data[2].i; /* ARGB1 */
   973         break;
   974     case TA_VERTEX_TEX_UV16_SPEC_PACKED_MOD:
   975         vertex->detail[3] = data[0].i; /* UV1 */
   976         vertex->detail[4] = data[2].i; /* ARGB1 */
   977         vertex->detail[5] = data[3].i; /* ARGB1 */
   978         break;
   980     case TA_VERTEX_TEX_INTENSITY_MOD:
   981         vertex->detail[3] = data[0].i; /* U1 */
   982         vertex->detail[4] = data[1].i; /* V1 */
   983         vertex->detail[5] = parse_intensity_colour( ta_status.intensity2, data[2].f ); /* ARGB1 */
   984         break;
   985     case TA_VERTEX_TEX_SPEC_INTENSITY_MOD:
   986         vertex->detail[4] = data[0].i; /* U1 */
   987         vertex->detail[5] = data[1].i; /* V1 */
   988         vertex->detail[6] = parse_intensity_colour( ta_status.intensity2, data[2].f ); /* ARGB1 */
   989         vertex->detail[7] = parse_intensity_colour( ta_status.intensity2, data[3].f ); /* ARGB1 */
   990         break;
   991     case TA_VERTEX_TEX_UV16_INTENSITY_MOD:
   992         vertex->detail[2] = data[0].i; /* UV1 */
   993         vertex->detail[3] = parse_intensity_colour( ta_status.intensity2, data[2].f ); /* ARGB1 */
   994         break;
   995     case TA_VERTEX_TEX_UV16_SPEC_INTENSITY_MOD:
   996         vertex->detail[3] = data[0].i; /* UV1 */
   997         vertex->detail[4] = parse_intensity_colour( ta_status.intensity2, data[2].f ); /* ARGB1 */
   998         vertex->detail[5] = parse_intensity_colour( ta_status.intensity2, data[3].f ); /* ARGB1 */
   999         break;
  1001     case TA_VERTEX_SPRITE:
  1002         vertex->y = data[0].f;
  1003         vertex->z = data[1].f;
  1004         vertex++;
  1005         ta_status.vertex_count++;
  1006         vertex->x = data[2].f;
  1007         vertex->y = data[3].f;
  1008         vertex->z = 0;
  1009         vertex->detail[0] = 0;
  1010         ta_status.poly_vertex[0].detail[0] = 0;
  1011         ta_status.poly_vertex[1].detail[0] = 0;
  1012         break;
  1013     case TA_VERTEX_TEX_SPRITE:
  1014         vertex->y = data[0].f;
  1015         vertex->z = data[1].f;
  1016         vertex++;
  1017         ta_status.vertex_count++;
  1018         vertex->x = data[2].f;
  1019         vertex->y = data[3].f;
  1020         vertex->z = 0;
  1021         vertex->detail[0] = 0;
  1022         vertex->detail[1] = 0;
  1023         ta_status.poly_vertex[0].detail[0] = data[5].i;
  1024         ta_status.poly_vertex[0].detail[1] = 0;
  1025         ta_status.poly_vertex[1].detail[0] = data[6].i;
  1026         ta_status.poly_vertex[1].detail[1] = 0;
  1027         ta_status.poly_vertex[2].detail[0] = data[7].i;
  1028         break;
  1029     case TA_VERTEX_MOD_VOLUME:
  1030     case TA_VERTEX_LISTLESS:
  1031         vertex->y = data[0].f;
  1032         vertex->z = data[1].f;
  1033         break;
  1035     ta_status.state = STATE_IN_POLYGON;
  1038 /**
  1039  * Process 1 32-byte block of ta data
  1040  */
  1041 void pvr2_ta_process_block( unsigned char *input ) {
  1042     union ta_data *data = (union ta_data *)input;
  1044     switch( ta_status.state ) {
  1045     case STATE_ERROR:
  1046         /* Fatal error raised - stop processing until reset */
  1047         return;
  1049     case STATE_EXPECT_POLY_BLOCK2:
  1050         /* This is always a pair of floating-point colours */
  1051         ta_status.intensity1 = 
  1052             parse_float_colour( data[0].f, data[1].f, data[2].f, data[3].f );
  1053         ta_status.intensity2 =
  1054             parse_float_colour( data[4].f, data[5].f, data[6].f, data[7].f );
  1055         ta_status.state = STATE_IN_LIST;
  1056         break;
  1058     case STATE_EXPECT_VERTEX_BLOCK2:
  1059         ta_parse_vertex_block2( data );
  1060         if( ta_status.vertex_count == ta_status.max_vertex ) {
  1061             ta_split_polygon();
  1063         break;
  1065     case STATE_EXPECT_END_VERTEX_BLOCK2:
  1066         ta_parse_vertex_block2( data );
  1067         if( ta_status.vertex_count < 3 ) {
  1068             ta_bad_input_error();
  1069         } else {
  1070             ta_commit_polygon();
  1072         ta_status.vertex_count = 0;
  1073         ta_status.poly_parity = 0;
  1074         ta_status.state = STATE_IN_LIST;
  1075         break;
  1076     case STATE_IN_LIST:
  1077     case STATE_IN_POLYGON:
  1078     case STATE_IDLE:
  1079         switch( TA_CMD( data->i ) ) {
  1080         case TA_CMD_END_LIST:
  1081             if( ta_status.state == STATE_IN_POLYGON ) {
  1082                 ta_bad_input_error();
  1083                 ta_end_list();
  1084                 ta_status.state = STATE_ERROR; /* Abort further processing */
  1085             } else {
  1086                 ta_end_list();
  1088             break;
  1089         case TA_CMD_CLIP:
  1090             if( ta_status.state == STATE_IN_POLYGON ) {
  1091                 ta_bad_input_error();
  1092                 ta_status.accept_vertexes = FALSE;
  1093                 /* Enter stuffed up mode */
  1095             ta_status.clip.x1 = data[4].i & 0x3F;
  1096             ta_status.clip.y1 = data[5].i & 0x0F;
  1097             ta_status.clip.x2 = data[6].i & 0x3F;
  1098             ta_status.clip.y2 = data[7].i & 0x0F;
  1099             if( ta_status.clip.x2 >= ta_status.width )
  1100                 ta_status.clip.x2 = ta_status.width - 1;
  1101             if( ta_status.clip.y2 >= ta_status.height )
  1102                 ta_status.clip.y2 = ta_status.height - 1;
  1103             break;
  1104         case TA_CMD_POLYGON_CONTEXT:
  1105             if( ta_status.state == STATE_IDLE ) {
  1106                 ta_init_list( TA_POLYCMD_LISTTYPE( data->i ) );
  1109             if( ta_status.vertex_count != 0 ) {
  1110                 /* Error, and not a very well handled one either */
  1111                 ta_bad_input_error();
  1112                 ta_status.accept_vertexes = FALSE;
  1113             } else {
  1114                 if( TA_IS_MODIFIER_LIST( ta_status.current_list_type ) ) {
  1115                     ta_parse_modifier_context(data);
  1116                 } else {
  1117                     ta_parse_polygon_context(data);
  1120             break;
  1121         case TA_CMD_SPRITE_CONTEXT:
  1122             if( ta_status.state == STATE_IDLE ) {
  1123                 ta_init_list( TA_POLYCMD_LISTTYPE( data->i ) );
  1126             if( ta_status.vertex_count != 0 ) {
  1127                 ta_fill_vertexes();
  1128                 ta_commit_polygon();
  1131             ta_parse_sprite_context(data);
  1132             break;
  1133         case TA_CMD_VERTEX:
  1134             ta_status.state = STATE_IN_POLYGON;
  1135             ta_parse_vertex(data);
  1137             if( ta_status.state == STATE_EXPECT_VERTEX_BLOCK2 ) {
  1138                 if( TA_IS_END_VERTEX(data[0].i) ) {
  1139                     ta_status.state = STATE_EXPECT_END_VERTEX_BLOCK2;
  1141             } else if( TA_IS_END_VERTEX(data->i) ) {
  1142                 if( ta_status.vertex_count < 3 ) {
  1143                     ta_bad_input_error();
  1144                 } else {
  1145                     ta_commit_polygon();
  1147                 ta_status.vertex_count = 0;
  1148                 ta_status.poly_parity = 0;
  1149                 ta_status.state = STATE_IN_LIST;
  1150             } else if( ta_status.vertex_count == ta_status.max_vertex ) {
  1151                 ta_split_polygon();
  1153             break;
  1154         default:
  1155             if( ta_status.state == STATE_IN_POLYGON ) {
  1156                 ta_bad_input_error();
  1159         break;
  1164 /**
  1165  * Find the first polygon or sprite context in the supplied buffer of TA
  1166  * data.
  1167  * @return A pointer to the context, or NULL if it cannot be found 
  1168  */
  1169 uint32_t *pvr2_ta_find_polygon_context( uint32_t *buf, uint32_t length )
  1171     uint32_t *poly;
  1172     for( poly = buf; poly < buf+(length>>2); poly += 8 ) {
  1173         if( TA_CMD(*poly) == TA_CMD_POLYGON_CONTEXT ||
  1174             TA_CMD(*poly) == TA_CMD_SPRITE_CONTEXT ) {
  1175             return poly;
  1178     return NULL;
  1181 /**
  1182  * Write a block of data to the tile accelerator, adding the data to the 
  1183  * current scene. We don't make any particular attempt to interpret the data
  1184  * at this stage, deferring that until render time.
  1186  * Currently copies the data verbatim to the vertex buffer, processing only
  1187  * far enough to generate the correct end-of-list events. Tile buffer is
  1188  * entirely ignored.
  1189  */
  1190 void pvr2_ta_write( unsigned char *buf, uint32_t length )
  1192     if( ta_status.debug_output ) {
  1193         fwrite_dump32( (uint32_t *)buf, length, stderr );
  1196     for( ; length >=32; length -= 32 ) {
  1197         pvr2_ta_process_block( buf );
  1198         buf += 32;
.