gsetup1.s 36.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
	
 ##########################################################################
 #
 # Triangle Setup Routine.
 # When entering this code we have a points buffer full of points,
 # and registers r1, r2, r3 point to the three vertices of a triangle.
 #
 ##########################################################################
	
#ifdef SETUP_ALONE
#include <rsp.h>
#include "mbi.h"

		.text	beginSetup
	
#include "gdmem.h"
#include "gfx_regs.h"
#endif	


 # ########################### CLIP TEST #################################
.name   minp,           $1
.name   midp,           $2
.name   maxp,           $3
.name   tmp,            $8
 #cc,name	rejectmask,	$6
.name   tmp2,           $9
.name   ccor,           $11     # OR of all points' clip codes
.name   ccand,          $12     # AND of all points' clip codes


                .ent    clipAndSetup
clipAndSetup:
 # ########################### CLIP TEST #################################

        lh      ccor, (RSP_PTS_CC)(maxp)	# or Clip Codes together &
        lh      tmp, (RSP_PTS_CC)(midp)	# and Clip Codes together
        lh      tmp2, (RSP_PTS_CC)(minp)	#
	and	ccand, ccor, tmp		#
	or      ccor, ccor, tmp                 #
	and	ccand, ccand, tmp2		#
	andi	ccand, ccand, 0x7070		# only see reject +/- xyz
	bne	ccand, zero, GfxDone		# Trivial rejection ?
	or      ccor, ccor, tmp2                #
 ### BRANCH OCCURS TO GfxDone: IF TRIVIALLY REJECTED

 	andi	ccor, ccor, 0x4343		# only see clip/accept +/- xyz
 # 	andi	ccor, ccor, 0x0707		# only see clip/accept +/- xyz
	bne     ccor, zero, startClip           # if ccor is 0, no clipping

 ### JUMP OCCURS to doClip or startClip: IF clipping is neccessary
 ### NOTE: delay slot is first instruction of beginSetup:
        
                .end    clipAndSetup

.unname ccor
.unname ccand
.unname minp
.unname midp
.unname maxp
.unname tmp
.unname tmp2
 #cc.unname rejectmask
 # ########################### END CLIP TEST #############################



#define one4th	vconst[4]
	

#define	LOWX	0	/* used to index elements of edge vectors */
#define	LOWY	1
#define MIDX	2
#define MIDY	3
#define HIGHX	4
#define HIGHY	5

	
/* scalar registers: */
.name	minp,		$1
.name	midp,		$2
.name	maxp,		$3
.name	miny, 		$9

.name	tmp,		$7
.name	flatp,		$4
.name	rdp_cmd,	$5
.name	rdp_flg,	$6
.name	dscratchp,	$8
.name	midy, 		$10
.name	maxy, 		$11
.name	negR, 		$12
.name	rendState,	$13

/* these are "global", used for both edge and attribute setup */
.name	DxXDyi,		$v0
.name	DxXDyf,		$v1
.name	yf,		$v2
.name	xHighf,		$v3
.name	EDel,		$v4
.name	invri,		$v27
.name	invrf,		$v26

/* these registers are dynamic, allocated and released as they are used */
.name	ri,		$v29
.name	rf,		$v28
.name	Hd,		$v9
.name	Md,		$v10
.name	Ld,		$v11
.name	td,		$v12
.name	vmin, 		$v13
.name	vmid, 		$v14	
.name	vmax, 		$v15	
	
	# begin back-face test:
	#
	# Back-face test is the sign of the plane equation BEFORE VERTEX
	# SORT, tested with the CULL_FRONT or CULL_BACK flags.
	# We toggle a bit during the sort and possibly correct the
	# pleq sign afterwards...
	#
.name	frontrej, $14
.name	backrej,  $15
.name	doreject, $16
.name	signr,    $17
	
.name	jnk,	$v16
.name	t1i,	$v17
.name	t1f,	$v18
.name	t2i,	$v19
.name	t2f,	$v20
	
		.ent	beginSetup


	
beginSetup:
	# load screen coordinates (pre-sort):
		llv	vmax[0], RSP_PTS_XS(maxp)	# element 0 is x,
 ### JUMP OCCURS to doClip or startClip: IF clipping is neccessary
		llv	vmid[0], RSP_PTS_XS(midp)	# element 1 is y
		llv	vmin[0], RSP_PTS_XS(minp)
	
	lh	miny, RSP_PTS_YS(minp)		# get the y's (BEGIN SETUP)
	lh	midy, RSP_PTS_YS(midp)
			vsub	Md, vmid, vmin
	lh	maxy, RSP_PTS_YS(maxp)
			vsub	Hd, vmax, vmin
			vsub	td, vmin, vmid
	lw	rendState, RSP_STATE_RENDER(rsp_state)
	
	# compute the partial products...
	# careful with the math here...
			vmudh	jnk, Hd, Md[1]
			vsar	t1i, t1i, t1i[0]
			vsar	t1f, t1f, t1f[1]
			vmudh	jnk, td, Hd[1]
			vsar	t2i, t2i, t2i[0]
			vsar	t2f, t2f, t2f[1]
 # hsa : Mon May  8 21:46:02 PDT 1995
 #			nop	# avoid rsp bug with register locking.
			vaddc	rf, t1f, t2f
			vadd	ri, t1i, t2i
	
	# do back-face test. sign of 'r' is back-face rejection test.	
	#
	# If (r == 0), triangle is NULL, we should bail out completely.
	 		vor	jnk, ri, rf
	andi	frontrej, rendState, G_CULL_FRONT
	 		mfc2	tmp, jnk[0]
	andi	backrej, rendState, G_CULL_BACK
	  		beq	tmp, zero, SetupReject
			# note delay slot
	
	# if (r < 0) then triangle is a back-face.
		vlt	jnk, ri, vconst[0]
		mfc2	signr, jnk[0]
		addi	dscratchp, zero, RSP_SETUP_TMP_OFFSET   # delay
		sra	signr, signr, 31
		and	backrej, backrej, signr
		xori	signr, signr, 0xffff
		and	frontrej, frontrej, signr
		or	tmp, backrej, frontrej
  		bgtz	tmp, SetupReject
		addi	negR, zero, 0	# in delay slot...
.unname	frontrej
.unname	backrej
.unname	doreject
.unname	signr
	
.unname jnk
.unname t1i
.unname t1f
.unname t2i
.unname t2f
 # end back-face test:

	# y-sort. Remember, input screen coords are S11.2
	#
	# This sort code was written by Gudrun Achtenhagen, gudrun@engr.sgi.com
	# Contact her if there are any bugs. :-)
	#
    swap1:	slt	tmp, midy, miny		#if midy>miny, tmp gets 0
	    	blez	tmp, swap2		#if tmp>0, branch 
		add	tmp, midy, $0		#put midy in tmp	
		add	midy, miny, $0		#put miny in midy	
		add	miny, tmp, $0		#put tmp in miny	
		addu	tmp, midp, $0		#put midp in tmp	
		addu	midp, minp, $0		#put minp in midp	
		addu	minp, tmp, $0		#put tmp in minp	
		xori	negR, negR, 0x0001
    swap2:	slt	tmp, maxy, midy		#if maxy>midy, tmp gets 0
		blez	tmp, sortDone		#if tmp>0, branch 
		add	tmp, maxy, $0		#put maxy in tmp
		add	maxy, midy, $0		#put midy in maxy
		add	midy, tmp, $0		#put midy in tmp
		addu	tmp, maxp, $0		#put maxp in tmp
		addu	maxp, midp, $0		#put midp in maxp
		addu	midp, tmp, $0		#put tmp in midp
		j	swap1
		xori	negR, negR, 0x0001
    sortDone:
	
	# load screen coordinates:	(S11.2)
		llv	vmax[0], RSP_PTS_XS(maxp)	# element 0 is x,
		llv	vmid[0], RSP_PTS_XS(midp)	# element 1 is y
		llv	vmin[0], RSP_PTS_XS(minp)

	# possibly negate R
		blez	negR, posiR
 		vsub	EDel, vmax, vmid	# delay slot, low deltas
		vmudn	rf, rf, vconst[3]	# negate R
		vmadh	ri, ri, vconst[3]
 		vmadn	rf, vconst, vconst[0]
	posiR:
	# compute edge deltas:		(S11.2)	
	# (Need to do this again after the sort)
	# save out vertex pointers for attribute processing
	# while doing this.
			 		vsub	Md, vmid, vmin
	sw	maxp, 0(dscratchp)
			 		vsub	Hd, vmax, vmin
	sw	midp, 4(dscratchp)

	
.unname	td
.unname	vmin
.unname	vmid
.unname	vmax
	
	# identify left- or right-major triangle:
	# if (r < 0) dir = 0 else dir = 1
	#
.name	vtmp,   $v12
		vlt	vtmp, ri, vconst[0]
	sw	minp, 8(dscratchp)
	
	# align these for Newton
		vmov	ri[3], ri[0]
		vmov	rf[3], rf[0]
	
	# align these for speed later.
		vmov	EDel[MIDX], Md[0]
		vmov	EDel[MIDY], Md[1]
		vmov	EDel[HIGHX], Hd[0]
	mfc2	tmp, vtmp[0]
		vmov	EDel[HIGHY], Hd[1]
.unname	negR
	
.unname	Hd
.unname	Md
.unname	Ld
	
.unname vtmp
.unname ri
.unname rf
			
.name	invEDeli,	$v7
.name	invEDelf,	$v8
.name	EDeli,		$v9
.name	EDelf,		$v10

	#
	# compute 1/r
	# R is about 10 bits accurate coming from the rcp table.
	# We need to do a Newton's iteration pass here to get more
	# precision. Each iteration should get another 10 bits...
	#
	# (r and invr registers are coordinated with Newton routine)
	#
		jal	NewtonDiv
		addi	rdp_flg, zero, 0x80	# delay slot
	
		bltz	tmp, rightMajor
		# note delay slot:
			vmudm	EDeli, EDel, vconst[4]	# make S15.16
		addi	rdp_flg, zero, 0x0	# left-major
	
    rightMajor:
	# Ldx/Ldy, Mdx/Mdy, Hdx/Hdy:
	#
	# Since the rcp ROM is 10 bits, that's good enough for
	# the edge slopes. Newton's doesn't help.
	#
	# Get triangle command from state and construct the proper RDP
	# command while we do this.
	#
	
	lb	tmp, RSP_STATE_TRI(rsp_state)
		# first part of mult is from delay slot
			vmadn	EDelf, vconst, vconst[0]
	
			vrcp	invEDelf[LOWY], EDel[LOWY]	# 1.0/Ldy
			vrcph	invEDeli[LOWY], vconst[0]
	ori	rdp_cmd, tmp, G_TRI_FILL
	
	# stick in tile number
	lb	tmp, RSP_STATE_TEX_TILE(rsp_state)
			vrcp	invEDelf[MIDY], EDel[MIDY]	# 1.0/Mdy
	or	rdp_flg, rdp_flg, tmp
			vrcph	invEDeli[MIDY], vconst[0]
	
			vrcp	invEDelf[HIGHY], EDel[HIGHY]	# 1.0/Hdy
			vrcph	invEDeli[HIGHY], vconst[0]
	
	# open for output
#if !(defined(OUTPUT_DRAM)||defined(OUTPUT_FIFO))
		jal	OutputOpen
		addi	$18, zero, 176 	# worst case guess (delay slot)
#endif /* !(OUTPUT_DRAM || OUTPUT_FIFO) */

	#
	# We used to shift down the rcp results all the way,
	# then do the multiply. If we don't shift it down all the
	# way, do the mult, then shift some more, we get better
	# precision on the degenerate cases.
	#
	 		vmudl	invEDelf, invEDelf, vconst1[4]	# make S15.16
 # hsa : Mon May  8 21:46:02 PDT 1995
 #	 		vmudl	invEDelf, invEDelf, vconst1[2]	# make S15.16
	sb	rdp_cmd, 0(outp)	# output rdp command
	 		vmadm	invEDeli, invEDeli, vconst1[4]
 # hsa : Mon May  8 21:46:02 PDT 1995
 #	 		vmadm	invEDeli, invEDeli, vconst1[2]
	sb	rdp_flg, 1(outp)	# output poly flag
	  		vmadn	invEDelf, vconst, vconst[0]

	# Do some other work during the pipeline delay:
	# We scale up EDel so that later, during the attribute computation,
	# the 1/r multiply gives us the right S15.16 aligned answer.
	
		vmudh	EDel, EDel, vconst[5]	# mult by 4 for attributes
	
.name 	xi,	$v12
.name 	xf,	$v13
	
	# x setup:	(S15.16)
	# we load these into unusual elements so we can group the
	# multiplies later during the X adjust step...
	# (finish edge slopes while we do this)
	# The slope answer will end up in the Y element...
	
	lsv	xi[(LOWX*2)],  RSP_PTS_XS(midp)
			vmudl	DxXDyf, invEDelf, EDelf[0q]	# Ldx / Ldy
	lsv	xi[(MIDX*2)],  RSP_PTS_XS(minp) # same as high
			vmadm	DxXDyf, invEDeli, EDelf[0q]	# Mdx / Mdy
	lsv	xi[(HIGHX*2)], RSP_PTS_XS(minp)
			vmadn	DxXDyf, invEDelf, EDeli[0q]	# Hdx / Hdy
	# y setup:	(S11.2)
	sll	tmp, miny, 14	# get frac part of high y-coord
			vmadh	DxXDyi, invEDeli, EDeli[0q]
	mtc2	tmp, yf[0]
			vmadn	DxXDyf, vconst, vconst[0]
	
	# shift down some more...
	#

	# we may be able to tolerate some slop, and not do a 2-part
	# shift, once the bow-tie fix is in hardware
	# Tue May 16 15:14:34 PDT 1995
	#
 		vmudl	DxXDyf, DxXDyf, vconst[4]
 		vmadm	DxXDyi, DxXDyi, vconst[4]
  		vmadn	DxXDyf, vconst, vconst[0]

 		vmudl	invEDelf, invEDelf, vconst[4]
 		vmadm	invEDeli, invEDeli, vconst[4]
  		vmadn	invEDelf, vconst, vconst[0]
.unname	EDeli
.unname	EDelf
	
.name vtmp,	$v16

#ifdef _HW_VERSION_1
	# bow-tie fix
	# possibly adjust low y.
	# Linearly interpolate between High and low y, based
	# on the slope difference... (colinearity)
	# Phil's latest try (This time, for sure...)
.name vtmpi,	$v17
.name vtmpf,	$v18
		vsubc	vtmpf, DxXDyf, DxXDyf[HIGHY]	# delta slope
		vsub	vtmpi, DxXDyi, DxXDyi[HIGHY]

		vaddc	vtmp, vtmpf, vconst1[5]		# +0x8000
		vadd	vtmpi, vtmpi, vconst[0]

		mfc2	tmp, vtmpi[(LOWY*2)]
 		bne	tmp, zero, DontTest		# if a/2 >1/2, skip

		vabs	vtmpf, vtmpf, vtmpf		# abs delta slope
		vmudl	vtmpf, vtmpf, vconst1[5]	# scale by 0x8000
		vmudl	vtmpf, vtmpf, vtmpf		# square
		vrcp	vtmpf[LOWY], vtmpf[LOWY]	# reciprocal
		vrcph	vtmp[LOWY], vconst[0]		# reciprocal

		mtc2	miny, vtmpf[(LOWY*2)]	# yh
		mtc2	maxy, vtmpi[(LOWY*2)]	# yl

		vsub	vtmpf, vtmpi, vtmpf		# yl-yh
		vmudm	vtmp, vtmpf, vtmp		# (yl-yh)*alpha/2
		vadd	vtmp, vtmp, vtmp		# (yl-yh)*alpha
		vsub	vtmpi, vtmpi, vtmp		# yl - (yl-yh)*alpha

 # clamp yl?
 #		vch	vtmpi, vtmpi, vconst[6]
	
		mfc2	maxy, vtmpi[(LOWY*2)]	# update yl


#ifdef SHOW_BOWTIE
		ori	rdp_cmd, tmp, (G_TRI_FILL)
		sb	rdp_cmd, 0(outp)	# output rdp command
#endif
.unname vtmpi
.unname vtmpf
	
.name slop, $14
	
	DontTest:

	#
	# This is an incredible hack...
	# We always load TEX_LOD and subtract it from ylow.
	# Usually this is 0, but if the user wants to change
	# gbi.h they can send another value down, and we can
	# 'increase' the safety of the bowtie rejection at
	# the expense of ugly pictures. This is a way to identify
	# and verify bowtie hangs without mucking with ucode or
	# apps...
	#

		lbu	slop, RSP_STATE_TEX_LOD(rsp_state)
 #		ori	slop, zero, 16
		sub	maxy, maxy, slop
.unname slop
#endif
	
	#
	# end of bow-tie avoidance code...
	#

	# clear lower bits of slope fractions to match the edgewalker
	# only use this chopped frac to back up the starting point.
	# pass the complete slope to the stepper.
	sh	maxy, 2(outp)	# output y coords S11.2
			vand	vtmp, DxXDyf, vconst1[1]
	
	# Check DxXDy for "nearly-horizontal". Make horizontal, if so.
	# (only a single-precision clamp)
	sh	midy, 4(outp)
			vcr	DxXDyi, DxXDyi, vconst1[6]
	
	# translate S11.2 x's to S15.16.
	sh	miny, 6(outp)
		vmudm	xi, xi, one4th
		vmadn	xf, vconst, vconst[0]
	
.unname miny
.unname midy
.unname maxy
	
	# adjust x's to proper place:
	#
	# xHigh = xHigh - DxXHDy * yHigh.frac
	# xMid  = xHigh - DxXMDy * yHigh.frac
	# xLow  = xMid 			(already on sub-pixel grid)
	#
	# Start the output while we do this...
	#
.name xHighi,	$v9
.name t1i,	$v10
.name t1f,	$v11
	
	# Clever use of registers, careful where answer ends up.
	# Remember, the DxXDy slopes are in the Y elements...
	# Do the mult for both equations at once, since we
	# lined up the registers that way.
	#
		ssv	DxXDyi[(LOWY*2)], 12(outp)
				vmudl	t1f, vtmp, yf[0]
		ssv	DxXDyf[(LOWY*2)], 14(outp)
				vmadm	t1i, DxXDyi, yf[0]
		ssv	DxXDyi[(HIGHY*2)], 20(outp)
				vmadn	t1f, vconst, vconst[0]
		ssv	DxXDyf[(HIGHY*2)], 22(outp)
	
	# do both subtracts at the same time, since we sneakily
	# lined up xi/xf that way...
 				vsubc	xHighf, xf, t1f[1q]
		ssv	xi[(LOWX*2)],  8(outp)	# output xLow
 				vsub	xHighi, xi, t1i[1q]
		ssv	xf[(LOWX*2)], 10(outp)
		ssv	DxXDyi[(MIDY*2)], 28(outp)
		ssv	DxXDyf[(MIDY*2)], 30(outp)
		ssv	xHighi[(HIGHX*2)], 16(outp)	# output xHigh
		ssv	xHighf[(HIGHX*2)], 18(outp)
		ssv	xHighi[(MIDX*2)],  24(outp)	# output xMid
		ssv	xHighf[(MIDX*2)],  26(outp)
		# moved increment of outp down below...
.unname xi
.unname xf
.unname xHighi
.unname t1i
.unname t1f
.unname vtmp

#ifdef _HW_VERSION_1
	#
	# Begin tests for other degenerate cases:
	#
.name	Hslope, $14
.name	Mslope, $15
.name	Lslope,	$16
.name	diff,	$17
.name	delta,	$18
.name	ylow,	$19
.name	ymid,	$20
.name	vdiffi, $v10
.name	vdifff, $v11
	#
	# This tests for thin "inside-out" triangles
	#
 		lw	Mslope, 28(outp)
		lw	Hslope, 20(outp)
		sll	tmp, rdp_flg, 24
 		sub	diff, Hslope, Mslope
		xor	diff, diff, tmp
 		bltz	diff, SetupDone
		# note delay slot
.unname	Hslope
.unname	Mslope
.unname	Lslope
.unname	diff
.unname	delta
.unname	ylow
.unname	ymid
.unname vdiffi
.unname vdifff
	
    DontAdjust:	
#endif
	
		
	#
	# Begin attribute setup:
	#
/*
 * at this point, the only registers in use should be:
 *	
 *	$v0	DxXDyi
 *	$v1	DxXDyf
 *	$v2	yf
 *	$v3	xHighf
 *	$v4	EDel
 *	$v27	invri
 *	$v26	invrf
 *	$v7	invEDeli
 *	$v8	invEDelf
 *
 */

	#
	# Texture setup. If we aren't doing texturing, we can skip
	# around this.
	#
	# We write out the vertex pointers, then loop through each
	# vertex. This makes for the most compact code.
	#
	# Note that L for LOD is computed *after* this pass, since
	# it needs the plane eqn deltas...
	#
		andi	tmp, rdp_cmd, G_RDP_TRI_TXTR_MASK
		addi	outp, outp, 32	# increment output pointer
		blez	tmp, AttributeSetup
		# note delay slot.

.name	ptpp,	$14
.name	ptp,	$15
.name	toutp,	$16
	
.name	ptTXi,	$v9	# these registers hold S, T, 1/W, L
.name	ptTXf,	$v10	# for each vertex.
.name	allWi,	$v11
.name	allWf,	$v12
.name	nearWi,	$v13
.name	nearWf,	$v14
.name	vtmpi,	$v15
.name	vtmpf,	$v16
.name	invWf,	$v17
.name   wscl,   $v18
.name	invWi,	$v19

#ifndef _HW_VERSION_1
.name	stmaxi,	$v20
.name	stmaxf,	$v21
#endif


	# load all the W's:
		lsv	allWi[0], RSP_PTS_W_INT(minp)
		lsv	allWf[0], RSP_PTS_W_FRAC(minp)
		lsv	allWi[2], RSP_PTS_W_INT(midp)
		lsv	allWf[2], RSP_PTS_W_FRAC(midp)
		lsv	allWi[4], RSP_PTS_W_INT(maxp)
		lsv	allWf[4], RSP_PTS_W_FRAC(maxp)

        # scale W's down to match 1/w
                lsv     wscl[0], RSP_STATE_PERSPNORM(rsp_state)
                vmudl   allWf, allWf, wscl[0]
                vmadm   allWi, allWi, wscl[0]
                vmadn   allWf, vconst, vconst[0]


	# find nearest W:
	addi	ptpp, dscratchp, 8	# also the loop counter
			vsubc	vtmpf,  allWf, allWf[1]
	addi	toutp, dscratchp, 16
			vlt	nearWi, allWi, allWi[1]
			vmrg	nearWf, allWf, allWf[1]
			vsubc	vtmpf,  nearWf, allWf[2]
			vlt	nearWi, nearWi, allWi[2]
			vmrg	nearWf, nearWf, allWf[2]

	# if we are in HW2, the following scale is not necessary,
	# we'll do it later.
#ifdef _HW_VERSION_1
	#
	# subtract a small amount so no nw/w will be > 1.0
	#
 #			vsubc	nearWf, nearWf, vconst1[3]
 #			vsub	nearWi, nearWi, vconst[0]
 # bug, divides texture coords by 2 ??????
 # NOTE: This scales all textures by 0.8
  			vmudl	nearWf, nearWf, vconst1[7]
 			vmadm	nearWi, nearWi, vconst1[7]
 			vmadn	nearWf, vconst, vconst[0]
#endif

#ifndef _HW_VERSION_1
	# find max S' and T' coordinates for LOD normalization
		vmov	stmaxi[0], vconst1[5]	# max neg. num
		vmov	stmaxi[1], vconst1[5]	# max neg. num
#endif
	
	# loop through min, mid, and max:
TexPerspLoop:
		lw	ptp, 0(ptpp)	# get point pointer
		
	# load S and T:
		llv	ptTXi[0], RSP_PTS_S(ptp)
	
	# Load 1/W saved from vertex transform.
	# Stick a magic number in for w. Later during the vmult
	# this will scale and shift the frac up where we want it 
	# for the attribute calculations.
	lsv	invWf[0], RSP_PTS_INVW_FRAC(ptp)
	lsv	invWi[0], RSP_PTS_INVW_INT(ptp)
			vmov	ptTXi[2], vconst1[0]
	
	# normalize the W's:
	# (this is NW/W)
	# (we can't cheat here; we need the double-precision multiply
	# in order to handle all kinds of w's, including orthographic...)
		vmudl	allWf, nearWf, invWf[0]
		vmadm	allWf, nearWi, invWf[0]
		vmadn	allWf, nearWf, invWi[0]
		vmadh	allWi, nearWi, invWi[0]

 # try clamping nw/w to 1.0, fix the shuffle... (not)
 # THIS DOES NOT WORK AS WELL AS SCALING (see above)
 #		vlt	allWi, allWi, vconst[1]
 #		vmrg	allWf, allWf, vconst[0]
	
	# multiply (S, T, W, L) by normalized 1/W's
		vmudm	vtmpf, ptTXi, allWf[0]
		vmadh	ptTXi, ptTXi, allWi[0]
	addi	toutp, toutp, 16
		vmadn	ptTXf, vconst, vconst[0]
	
	# output to scratch memory:
		sdv	ptTXi[0],  (0-16)(toutp)
		sdv	ptTXf[0],  (8-16)(toutp)

#ifndef _HW_VERSION_1
	# find max S' and T' coordinates for LOD normalization
		vge	stmaxi, stmaxi, ptTXi
		vmrg	stmaxf, stmaxf, ptTXf
#endif
		bne	ptpp, dscratchp, TexPerspLoop
		addi	ptpp, ptpp, -4	# delay slot

	# store nearW for LOD computation later...
		ssv	nearWi[0], 68(dscratchp)
		ssv	nearWf[0], 76(dscratchp)
	
#ifndef _HW_VERSION_1
	# store max S' and T' coordinates for LOD normalization
		slv	stmaxi[0], 64(dscratchp)
		slv	stmaxf[0], 72(dscratchp)
#endif
	
.unname	ptpp
.unname	ptp
.unname	toutp
	
.unname	ptTXi
.unname	ptTXf
.unname	allWi
.unname	allWf
.unname	nearWi
.unname	nearWf
.unname	vtmpi
.unname	vtmpf
.unname	invWf
.unname wscl
.unname	invWi
	
#ifndef _HW_VERSION_1
.unname	stmaxi
.unname	stmaxf
#endif
	
.name	doLOD,	$14	# flag to control LOD processing code re-use
	
.name	Hdai,	$v9
.name	Hdaf,	$v10
.name	Mdai,	$v11
.name	Mdaf,	$v12
.name	adei,	$v13
.name	adef,	$v14
.name	ainiti, $v15
.name	ainitf, $v16
.name	tHdai, 	$v17
.name	tHdaf, 	$v18
.name	tMdai, 	$v19
.name	tMdaf, 	$v20
.name	amin,	$v21
.name	aminf,	$v22
.name	amid,	$v23
.name	amidf,	$v24
.name	amax,	$v25
.name	amaxf,	$v5
.name	vjunk,	$v6
.name	vjunkf,	$v28

	
AttributeSetup:
	#
	# If we aren't doing any attributes at all, let's
	# bail out early.
	# (clear out all the bits while we do this)
	#
		andi	tmp, rdp_cmd, (G_RDP_TRI_ZBUFF_MASK | G_RDP_TRI_TXTR_MASK  | G_RDP_TRI_SHADE_MASK)
		blez	tmp, SetupDone
		vxor	ainitf,	vconst, vconst	# delay slot
	
	#
	# Collect all the attributes
	# in a vector (r,g,b,a,s,t,w,z) with one left over (l). 
	# l (and z again) are computed in a second pass.
	
	# load attributes:
	# load smooth-shading colors first.
	# RGBA, use fancy packed load, then shift.
	# DMEM alignment is crucial here!
	
	# add .5 to the colors in order to work around a hardware
	# bug regarding span start color inprecision
	# Thu Jun  8 18:49:52 PDT 1995
	
	 		vadd	aminf, ainitf, vconst1[5]
		addi	doLOD, zero, 0
	 		vadd	amidf, ainitf, vconst1[5]
		luv	amax[0], RSP_PTS_R_NX(maxp)
	 		vadd	amaxf, ainitf, vconst1[5]
		luv	amin[0], RSP_PTS_R_NX(minp)
	
	# test for flat shading
		andi	tmp, rendState, G_SHADING_SMOOTH
		bgtz	tmp, smoothShade
		luv	amid[0], RSP_PTS_R_NX(midp)	# delay slot
	
	# load flat-shading colors instead: (use same vertex)
		luv	amax[0], RSP_PTS_R_NX(flatp)
		luv	amin[0], RSP_PTS_R_NX(flatp)
		luv	amid[0], RSP_PTS_R_NX(flatp)
		
	smoothShade:	
		vmudm	amax, amax, vconst[7]	# multiply by 1/512.0 to
		vmudm	amin, amin, vconst[7]	# move things into lower byte.
		vmudm	amid, amid, vconst[7]

	# load S, T, and W:
	# These have been previously computed and stored in scratch memory.
		ldv	amin[8],  (16 +  0)(dscratchp)
		ldv	aminf[8], (16 +  8)(dscratchp)
		ldv	amid[8],  (16 + 16)(dscratchp)
		ldv	amidf[8], (16 + 24)(dscratchp)
		ldv	amax[8],  (16 + 32)(dscratchp)
		ldv	amaxf[8], (16 + 40)(dscratchp)

#ifdef _HW_VERSION_1
    LODpass:		# let LOD-compute jump in here and re-use code
	
	# load z's.
	# Use the proper 'screen-space' Z. (we have to re-load these
	# for the LOD pass because we ran out of registers and trashed
	# them)
#else
	# load z's.
	# Use the proper 'screen-space' Z.
#endif
		lsv	amin[14], RSP_PTS_ZS(minp)
		lsv	aminf[14], RSP_PTS_ZSF(minp)
		lsv	amid[14], RSP_PTS_ZS(midp)
		lsv	amidf[14], RSP_PTS_ZSF(midp)
		lsv	amax[14], RSP_PTS_ZS(maxp)
		lsv	amaxf[14], RSP_PTS_ZSF(maxp)
	
	# compute attribute deltas: (S15.16) watch alignment!
  		vsubc	Mdaf, amidf, aminf
 		vsub	Mdai, amid, amin
 		vsubc	tHdaf, aminf, amaxf
		vsub	tHdai, amin, amax
 		vsubc	Hdaf, amaxf, aminf
		vsub	Hdai, amax, amin
		vsubc	tMdaf, aminf, amidf
		vsub	tMdai, amin, amid
	#
	# The plane equation attribute computation:
	# (be careful, destination is a source)
	#

	#
	# These multiplies use the full precision of the accumulator.
	# They are basically 32-bit integer multiplies, but the
	# fractional component is also included, although only the
	# upper 32-bits of answer are used.
	#
	# See note up above about why EDel is being scaled up.
	#
	# S15.16 * S11.4 = SS26.20
	# (we only use the upper SS26.4, which we'll multiply
	# by 1/r below)
	#

#if 0
		# compute DeAtt directly, divide Hda/ydelta, 
		# instead of:	  de = dy + dx * DxXHDy
		vmudl	vjunk, Hdaf, invEDelf[HIGHY]
		vmadm	vjunk, Hdai, invEDelf[HIGHY]
		vmadn	adef,  Hdaf, invEDeli[HIGHY]
		vmadh	adei,  Hdai, invEDeli[HIGHY]
	
		# DxAtt = Mdy*Hda - Hdy*Mda
		vmudn	vjunk, Hdaf, EDel[MIDY]
		vmadh	vjunk, Hdai, EDel[MIDY]
		vmadn	vjunk, tMdaf, EDel[HIGHY]
 		vmadh	vjunk, tMdai, EDel[HIGHY]
		vsar	Hdai, Hdai, Hdai[0]
		vsar	Hdaf, Hdaf, Hdaf[1]
	
		# DyAtt = Hdx*Mda - Mdx*Hda
		vmudn	vjunk, Mdaf, EDel[HIGHX]
		vmadh	vjunk, Mdai, EDel[HIGHX]
		vmadn	vjunk, tHdaf, EDel[MIDX]
		vmadh	vjunk, tHdai, EDel[MIDX]
		vsar	Mdai, Mdai, Mdai[0]
		vsar	Mdaf, Mdaf, Mdaf[1]
		
	# divide by r (S4.27)
	# This multiply results in the proper S15.16 attributes
	# that we need (texture is S10.21)
	#
		vmudl	vjunk, Hdaf, invrf[3]
		vmadm	vjunk, Hdai, invrf[3]
		vmadn	Hdaf, Hdaf, invri[3]
		vmadh	Hdai,  Hdai, invri[3]

		vmudl	vjunk, Mdaf, invrf[3]
		vmadm	vjunk, Mdai, invrf[3]
		vmadn	Mdaf, Mdaf, invri[3]
		vmadh	Mdai,  Mdai, invri[3]
#endif
	/*
	 * We'd like to do it the above way, it's faster and
	 * shorter. But the precision errors keep biting us.
	 * If we do it this way, the errors are minimized:
	 */
		# DxAtt = Mdy*Hda - Hdy*Mda
		vmudn	vjunk, Hdaf, EDel[MIDY]
		vmadh	vjunk, Hdai, EDel[MIDY]
		vmadn	vjunk, tMdaf, EDel[HIGHY]
		vmadh	vjunk, tMdai, EDel[HIGHY]
		vsar	Hdai, Hdai, Hdai[0]
		vsar	Hdaf, Hdaf, Hdaf[1]
	
		# DyAtt = Hdx*Mda - Mdx*Hda
		vmudn	vjunk, Mdaf, EDel[HIGHX]
		vmadh	vjunk, Mdai, EDel[HIGHX]
		vmadn	vjunk, tHdaf, EDel[MIDX]
		vmadh	vjunk, tHdai, EDel[MIDX]
		vsar	Mdai, Mdai, Mdai[0]
		vsar	Mdaf, Mdaf, Mdaf[1]
		
	# divide by r (S4.27)
	#
	# This multiply results in the proper S15.16 attributes
	# that we need (texture is S10.21)
	#
		vmudl	vjunk, Hdaf, invrf[3]
		vmadm	vjunk, Hdai, invrf[3]
		vmadn	Hdaf,  Hdaf, invri[3]
		vmadh	Hdai,  Hdai, invri[3]

		vmudl	vjunk, Mdaf, invrf[3]
		vmadm	vjunk, Mdai, invrf[3]
		vmadn	Mdaf,  Mdaf, invri[3]
		vmadh	Mdai,  Mdai, invri[3]
	
		# convert to edge slope representation:
		#   de = dy + dx * DxXHDy
		vmudn	vjunk, Mdaf, vconst[1]
		vmadh	vjunk, Mdai, vconst[1]	# use accum for add...
		vmadl	vjunk, Hdaf, DxXDyf[HIGHY]
		vmadm	vjunk, Hdai, DxXDyf[HIGHY]
		vmadn	adef,  Hdaf, DxXDyi[HIGHY]
		vmadh	adei,  Hdai, DxXDyi[HIGHY]
	
	
.unname vjunk
.unname vjunkf
.name	pp1i,	$v6
.name	pp1f,	$v28
		# attribute X adjust:
		#   att = att - (de * yHigh.frac)
			vmudl	pp1f, adef,   yf[0]
			vmadm	pp1i, adei,   yf[0]
			vmadn	pp1f, vconst, vconst[0]
			vsubc	ainitf, aminf, pp1f
#ifdef _HW_VERSION_1
	# if this was the L-pass, jump back there.
	bgtz	doLOD, finishLOD
#endif
			vsub	ainiti, amin,   pp1i	# delay slot
.unname	pp1i
.unname	pp1f
	
		andi	tmp, rdp_cmd, G_RDP_TRI_SHADE_MASK
	#
	# All done.	
	# Write out the proper record to the RDP, based on the drawing
	# modes.
	#		
	# (get ready for next test in the branch delay slots)

	# write out shade
		blez	tmp, outputTXTR
		andi	tmp, rdp_cmd, G_RDP_TRI_TXTR_MASK	# delay
	
		sdv	ainiti[0],  0(outp)
		sdv	Hdai[0],    8(outp)
		sdv	ainitf[0], 16(outp)
		sdv	Hdaf[0],   24(outp)	
		sdv	adei[0],   32(outp)
		sdv	Mdai[0],   40(outp)	
		sdv	adef[0],   48(outp)
		sdv	Mdaf[0],   56(outp)	
		addi	outp, outp, 64	# increment output pointer

#ifndef _HW_VERSION_1
	# write out texture
  outputTXTR:	blez	tmp, outputZBUF
		andi	tmp, rdp_cmd, G_RDP_TRI_ZBUFF_MASK	# delay
	#
	# Scale texture parameters to ensure that they all remain
	# in-bounds for the hardware LOD computation:
	#
	
	# free up some registers
.unname	amaxf
.unname	tHdai
.unname	tHdaf
.unname	tMdai
.unname	tMdaf
.unname	amin
.unname	aminf
.unname	amid
.unname	amidf
	
.name scalei,	$v5
.name scalef,	$v6
.name vtmpf,	$v17
.name coordMi,	$v18
.name coordMf,	$v19
.name t1i,	$v20
.name t1f,	$v21
.name absdxi,	$v22
.name absdyi,	$v23
.name absdei,	$v24
	
	# find abs() of all the slopes:
	# (sloppy, single-precision test only)
	# use the original fractional vector when needed
		vabs	absdxi, Hdai, Hdai
		vabs	absdyi, Mdai, Mdai
		vabs	absdei, adei, adei

	# load maxS', maxT', and nearW into vector
		ldv	coordMi[8], 64(dscratchp)
		ldv	coordMf[8], 72(dscratchp)

	# compute |coordMax| + |d*dx| + |d*dy|
	# first add the deltas:
		vaddc	t1f, Mdaf, Hdaf
		vadd	t1i, absdyi, absdxi
	# put coordMax into accumulator:
		vmudn	vtmpf, coordMf, vconst[1]
		vmadh	vtmpf, coordMi, vconst[1]
	# mult deltas by 2, add to accumulator:
		vmadn	t1f, t1f, vconst[2]
		vmadh	t1i, t1i, vconst[2]
	
	# find max of scale factors
		vsubc	vtmpf,  t1f, t1f[5]
		vge	scalei, t1i, t1i[5]
		vmrg	scalef, t1f, t1f[5]
		vsubc	vtmpf,  scalef, t1f[6]
		vge	scalei, scalei, t1i[6]
		vmrg	scalef, scalef, t1f[6]

	# scale down scale factor, to be S15.16 before divide:
		addi	$16, zero, 0x0040
		mtc2	$16, vtmpf[0]
		nop
                vmudl   scalef, scalef, vtmpf[0]
                vmadm   scalei, scalei, vtmpf[0]
                vmadn   scalef, vconst, vconst[0]
	
	# compute 1/scalefactor
	# sloppy, Newton's not needed?
		vrcph	vtmpf[0], scalei[4]
		vrcpl	scalef[0], scalef[4]
		vrcph	scalei[0], vconst[0]
	
	# convert to s15.16
		vmudn	scalef, scalef, vconst[2]
		vmadh	scalei, scalei, vconst[2]

	# this safety scale ensures scale is always < 1.0
	# (needed due to sloppy divide above)
		vmudl	scalef, scalef, vconst1[7]
		vmadm	scalei, scalei, vconst1[7]
		vmadn	scalef, vconst, vconst[0]

	/* optimize the above code... */
	
.unname absdxi
.unname absdyi
.unname absdei
.unname coordMi
.unname coordMf
.unname t1i
.unname t1f
.unname vtmpf
	
.name tiniti,	$v17
.name tinitf,	$v18
.name tHdai,	$v19
.name tHdaf,	$v20
.name tMdai,	$v21
.name tMdaf,	$v22
.name tadei,	$v23
.name tadef,	$v24

	# scale init, dx, dy, de
		vmudl	tinitf, ainitf, scalef[0]
		vmadm	tinitf, ainiti, scalef[0]
		vmadn	tinitf, ainitf, scalei[0]
		vmadh	tiniti, ainiti, scalei[0]

		vmudl	tHdaf, Hdaf, scalef[0]
		vmadm	tHdaf, Hdai, scalef[0]
		vmadn	tHdaf, Hdaf, scalei[0]
		vmadh	tHdai, Hdai, scalei[0]

	# just to show that we're using HW2 code...
	vmov	tiniti[7], vconst1[7]
	vmov	tinitf[7], vconst1[7]
	
		vmudl	tMdaf, Mdaf, scalef[0]
		vmadm	tMdaf, Mdai, scalef[0]
		vmadn	tMdaf, Mdaf, scalei[0]
		vmadh	tMdai, Mdai, scalei[0]

		vmudl	tadef, adef, scalef[0]
		vmadm	tadef, adei, scalef[0]
		vmadn	tadef, adef, scalei[0]
		vmadh	tadei, adei, scalei[0]

	# write out texture parameters:
		sdv	tiniti[8],  0(outp)
		sdv	tHdai[8],    8(outp)
		sdv	tinitf[8], 16(outp)
		sdv	tHdaf[8],   24(outp)	
		sdv	tadei[8],   32(outp)
		sdv	tMdai[8],   40(outp)	
		sdv	tadef[8],   48(outp)
		sdv	tMdaf[8],   56(outp)	
	
.unname scalei
.unname scalef
.unname tiniti
.unname tinitf
.unname tHdai
.unname tHdaf
.unname tMdai
.unname tMdaf
.unname tadei
.unname tadef
	
	# restore some registers
.name	amaxf,	$v5
.name	tHdai, 	$v17
.name	tHdaf, 	$v18
.name	tMdai, 	$v19
.name	tMdaf, 	$v20
.name	amin,	$v21
.name	aminf,	$v22
.name	amid,	$v23
.name	amidf,	$v24

#else	/*  _HW_VERSION_1 */
	# write out texture
  outputTXTR:	blez	tmp, outputZBUF
		andi	tmp, rdp_cmd, G_RDP_TRI_ZBUFF_MASK	# delay

		sdv	ainiti[8],  0(outp)
		sdv	Hdai[8],    8(outp)
		sdv	ainitf[8], 16(outp)
		sdv	Hdaf[8],   24(outp)	
		sdv	adei[8],   32(outp)
		sdv	Mdai[8],   40(outp)	
		sdv	adef[8],   48(outp)
		sdv	Mdaf[8],   56(outp)	

	# LOD computation:
	# We have to do this *after* plane eqn computations because
	# the formulas for computing L at each vertex need
	# some of the S/T/Z information.
	#

	# Register management is a little sloppy, unname these
	# registers that aren't being used to make room. But
	# we must re-.name them below for the output and Z...
	#
.unname	tHdaf
.unname	tMdai
.unname	tMdaf
.unname	amin
.unname	aminf
.unname	amid
.unname	amidf
.unname	amax
	

	# free up some registers
.unname	amaxf
.unname	ainiti
.unname	ainitf
.unname	tHdai
	
	#
	# "old" partial-derivative method, single precision.
	#
.name ptp, 	$16
.name ptpp, 	$17
	
.name nearWi,	$v5
.name nearWf,	$v6
	
.name delWi,	$v15
.name delWf,	$v16
.name minC,	$v17

.name t1i,	$v18
.name t1f,	$v19
.name t2i,	$v20
.name t2f,	$v21
.name delCi,	$v22
.name delCf,	$v23
.name thisWi,	$v24
.name thisWf,	$v25
.name vjunk,	$v28

	    addi	doLOD, zero, 1
	
	# load plane eqn W deltas: Hda[6], Hda[6], Mda[6], Mda[6]
		vmov	delWi[0], Hdai[6]
		vmov	delWi[1], Hdai[6]
		vmov	delWi[2], Mdai[6]
		vmov	delWi[3], Mdai[6]
	
	# load nearW (previously saved)
	    lsv	nearWi[0], 68(dscratchp)
	    lsv	nearWf[0], 76(dscratchp)
	
	# load plane eqn S/T deltas: Hda[4] Hda[5] Mda[4] Mda[5]
	    llv		delCi[0],  8(outp)	# Hda S & T
	    llv		delCf[0], 24(outp)
	    llv		delCi[4], 40(outp)	# Mda S & T
	    llv		delCf[4], 56(outp)

	# calc 1.0/nearW
	    vrcph	t1f[0],    nearWi[0]	# w = 1/(1/w)
	    vrcpl	nearWf[0], nearWf[0]
	    vrcph	nearWi[0], vconst[0]
	# convert to s15.16
		vmudn	nearWf, nearWf, vconst[2]
		vmadh	nearWi, nearWi, vconst[2]

	    addi	ptpp, dscratchp, 8	# loop counter & data pointer

 # take this out for now...
 #	# scale up by (integer) user factor: (horrible reuse of registers!)
 #		lb	ptp, RSP_STATE_TEX_LOD(rsp_state)	# note reg
 #		mtc2	ptp, thisWi[0]				# note reg
 #		vmudn	delCf, delCf, thisWi[0]
 #		vmadh	delCi, delCi, thisWi[0]
	
	
            lsv     vjunk[0], RSP_STATE_PERSPNORM(rsp_state)
	
	# do min/mid/max as a loop (save code space)
	# assumes minp, midp, and maxp still stored at dscratchp:
LODvtxLoop:
	# load original W's
	    lw		ptp, 0(ptpp)
	
	# load this W:	(are these w's scaled by PERSPNORM?)
	    lsv		thisWi[0], RSP_PTS_W_INT(ptp)
	    lsv		thisWf[0], RSP_PTS_W_FRAC(ptp)

	# hack
            vmudl   thisWf, thisWf, vjunk[0]
            vmadm   thisWi, thisWi, vjunk[0]
            vmadn   thisWf, vconst, vconst[0]
	
	# load original s & t's, dup'd [s t s t]
		llv	minC[0], RSP_PTS_S(ptp)
		llv	minC[4], RSP_PTS_S(ptp)

	# compute w/nearW:
		vmudl	t1f,    thisWf, nearWf[0]
		vmadm	t1f,    thisWi, nearWf[0]
		vmadn	thisWf, thisWf, nearWi[0]
		vmadh	thisWi, thisWi, nearWi[0]
	
	# multiply orig coords, by plane eqn W's
		vmudh	t2f, delWi, minC
		vsar	t1i, t1i, t1i[0]
		vsar	t1f, t1f, t1f[2]

	# convert to S15.16
		vmudn	t1f, t1f, vconst[2]
		vmadh	t1i, t1i, vconst[2]

	# subtract plane eqn (S,T) - above result
		vsubc	t1f, delCf, t1f
		vsub	t1i, delCi, t1i
	
	# multiply above by W/nearW
		vmudl	t2f, t1f, thisWf[0]
		vmadm	t2f, t1i, thisWf[0]
		vmadn	t2f, t1f, thisWi[0]
		vmadh	t2i, t1i, thisWi[0]

	# compute abs() of ds/dx, ds,dy, dt/dx, dt/dy
	# NOTE: BOGUS. No douple precision vabs() function.  Since we use
	# S10.21 texture coords we get the right answer when positive, and
	# S10.5 corrent bits when negative, which should be enough (?)
		vabs	t2i, t2i, t2i

	# find max of abs() of (ds/dx, ds,dy, dt/dx, dt/dy)
	# (use sloppy but cheep single precision compare)
		vge	t2i, t2i, t2i[1]
 # hsa : Mon May  8 21:46:02 PDT 1995
 #		vmrg	t2f, t2f, t2f[1]
		vge	t2i, t2i, t2i[2]
 #		vmrg	t2f, t2f, t2f[2]
		vge	t1i, t2i, t2i[3]
 #		vmrg	t1f, t2f, t2f[3]

	# write LOD (max from above) to scratch mem
	    ssv		t1i[0], (64+0)(ptpp)
 #	    ssv		t1f[0], (64+2)(ptpp)

	    bne		ptpp, dscratchp, LODvtxLoop
	    addi	ptpp, ptpp, -4
 #### BRANCH OCCURS TO LODvtxLoop: for next of 3 vertices

.unname ptp
.unname ptpp
.unname delWi
.unname delWf
.unname minC
.unname t1i
.unname t1f
.unname t2i
.unname t2f
.unname delCi
.unname delCf
.unname thisWi
.unname thisWf
.unname nearWi
.unname nearWf
.unname vjunk
	
	# restore some registers
.name	amaxf,	$v5
.name	ainiti, $v15
.name	ainitf, $v16
.name	tHdai, 	$v17
	

.name	tHdaf, 	$v18
.name	tMdai, 	$v19
.name	tMdaf, 	$v20
.name	amin,	$v21
.name	aminf,	$v22
.name	amid,	$v23
.name	amidf,	$v24
.name	amax,	$v25
	
	# load min/mid/max L from scratch mem
		lsv	amin[0],  (64+8)(dscratchp)
		lsv	aminf[0], (64+10)(dscratchp)
		lsv	amid[0],  (64+4)(dscratchp)
		lsv	amidf[0], (64+6)(dscratchp)
		lsv	amax[0],  (64+0)(dscratchp)

	# jump to plane eqn computation
	# Notice that this will re-compute Z as well (no bother).
		j	LODpass
		lsv	amaxf[0], (64+2)(dscratchp)	# delay slot

  finishLOD:
	# output L info in correct locations:
		ssv	ainiti[0],  6(outp)	# ainit
		ssv	Hdai[0],   14(outp)	# DLDx
		ssv	ainitf[0], 22(outp)	# ainit.f
		ssv	Hdaf[0],   30(outp)	# DLDx.f
		ssv	adei[0],   38(outp)	# DLDe
		ssv	Mdai[0],   46(outp)	# DLDy
		ssv	adef[0],   54(outp)	# DLDe.f
		ssv	Mdaf[0],   62(outp)	# DLDy.f
#endif
		addi	outp, outp, 64	# increment output pointer

  outputZBUF:	
	#
	# clever note about the Z-processing:
	#
	# We save code by re-computing the Z again during the
	# LOD pass (if taken), rather than work around it. There
	# is no computational penalty, since it's just another
	# element in the vector. If there is no texturing, 
	# then we use the Z computed on the first pass.
	#
		blez	tmp, SetupDone
		# note delay slot
	#
	# Scale Z-values up, screen coordinates were limited
	# to 10 integer bits, but the hardware floating point format
	# needs valid bits in the upper range for best performance.
	#
		vmudn	adef, adef, vconst1[4]
		vmadh	adei, adei, vconst1[4]
		vmadn	adef, vconst, vconst[0]
	
		vmudn	aminf, aminf, vconst1[4]
		vmadh	amin, amin, vconst1[4]
		vmadn	aminf, vconst, vconst[0]

		vmudn	Hdaf, Hdaf, vconst1[4]
		vmadh	Hdai, Hdai, vconst1[4]
		vmadn	Hdaf, vconst, vconst[0]
	
		vmudn	Mdaf, Mdaf, vconst1[4]
		vmadh	Mdai, Mdai, vconst1[4]
		vmadn	Mdaf, vconst, vconst[0]
	
.name	pp1i,	$v6
.name	pp1f,	$v28
		# re-compute attribute X adjust after the Z scale:
		#   att = att - (de * yHigh.frac)
		vmudl	pp1f, adef,   yf[0]
		vmadm	pp1i, adei,   yf[0]
		vmadn	pp1f, vconst, vconst[0]
		vsubc	ainitf, aminf, pp1f
		vsub	ainiti, amin,   pp1i
.unname	pp1i
.unname	pp1f
		ssv	adei[14],    8(outp)	# output z stuff.
		ssv	adef[14],   10(outp)
		ssv	Hdai[14],    4(outp)
		ssv	Hdaf[14],    6(outp)	
		ssv	Mdai[14],   12(outp)	
		ssv	Mdaf[14],   14(outp)	
		ssv	ainiti[14],  0(outp)
		ssv	ainitf[14],  2(outp)
		addi	outp, outp, 16	# increment output pointer

SetupDone:			# done or rejected. do any clean-up.
		jal	OutputClose
		# note delay slot

SetupReject:	# no OutputClose needed...
		nop
 		jr	return_save
		nop

		.end	beginSetup

/* un-name scalar registers: */
.unname	minp
.unname	midp
.unname	maxp
.unname	flatp
.unname	rdp_cmd
.unname	rdp_flg
.unname	tmp
.unname	dscratchp
.unname	rendState
.unname	doLOD
	
/* un-name vector registers: */
.unname	DxXDyi
.unname	DxXDyf
.unname	yf
.unname	xHighf
.unname	EDel
.unname	invri
.unname	invrf
.unname	invEDeli
.unname	invEDelf
	
.unname	Hdai
.unname	Hdaf
.unname	Mdai
.unname	Mdaf
.unname	adei
.unname	adef
.unname	ainiti
.unname	ainitf
.unname	tHdai
.unname	tHdaf
.unname	tMdai
.unname	tMdaf
.unname	amin
.unname	aminf
.unname	amid
.unname	amidf
.unname	amax
.unname	amaxf

#if 0
	# test for thorough register un-naming.
.name r1, $1
.name r2, $2
.name r3, $3
.name r4, $4
.name r5, $5
.name r6, $6
.name r7, $7
.name r8, $8
.name r9, $9
.name r10, $10
.name r11, $11
.name r12, $12
.name r13, $13
.name r14, $14
.name r15, $15
.name r16, $16
.name r17, $17
.name r18, $18
.name r19, $19
.name r20, $20

.name vv0, $v0
.name vv1, $v1
.name vv2, $v2
.name vv3, $v3
.name vv4, $v4
.name vv5, $v5
.name vv6, $v6
.name vv7, $v7
.name vv8, $v8
.name vv9, $v9
.name vv10, $v10
.name vv11, $v11
.name vv12, $v12
.name vv13, $v13
.name vv14, $v14
.name vv15, $v15
.name vv16, $v16
.name vv17, $v17
.name vv18, $v18
.name vv19, $v19
.name vv20, $v20
.name vv21, $v21
.name vv22, $v22
.name vv23, $v23
.name vv24, $v24
.name vv25, $v25
.name vv26, $v26
.name vv27, $v27
.name vv28, $v28
.name vv29, $v29
	
#endif