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