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