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