gvtx.s 13.1 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

#include "gfx_regs.h"

#ifdef	F3DLP_GBI
	ERROR !!!!! ERROR !!!!!
#endif

 ############################################################################	
 #
 # Transform, project, and viewport map as we load into the points buffer.
 #
 # $1 holds n and v0
 # in_bufp holds pointer to data
 #
 # This version does 2 points at a time, 2 per vector register,
 # (could use software pipelining to do two more).
 #
 # WARNING: many of the constants, pipelining, etc. reflect the
 # layout of the points buffer, etc. Be careful.
 #
	# v0 field done in the delay slot that brought us here.
#ifdef	F3DEX_GBI
	# $9 に n の値を代入する.
		lbu	$9, (0-6)(dinp)
#else
		srl	$1, $1, 4	# pick off 'n' field 
		addi	$5, $1, 1	# n is really (n-1)
#endif
	
 # these can't be moved up because of the label/directive problem...	
 # don't use regs 1-4, so we can call MatCat...
.name gmode,	$3

.name n,	$5
.name v0,	$6
.name voutp,	$7
.name tmp,	$8
.name i,	$9
.name flg1,	$13
.name flg2,	$14
.name clr1,	$15
.name clr2,	$16

#ifdef	NEWCOM
/*
	doLight で使用するスカラーレジスタ
	$1, $3, $7, $8, $15, $16

	clip への復帰時に破壊してもよいレジスタ
	$19, $20, $21

	$10:NG	$11:NG	$17:NG  $18:NG	$19:OK	$20:OK	$21:OK
 */
.name goutp2,	$20
.name goutp,	$21
.name voutp2,	$19
.unname	gfx0
.name vtxid,	$25
#endif

.name vpscale,	$v0
.name vptrans,	$v1
.name vin12,	$v2
.name vtmp,	$v3
.name persp12i,	$v4
.name persp12f,	$v5
.name scrn12i,	$v6
.name scrn12f,	$v7

.name mtx0,	$v8
.name mtx1,	$v9
.name mtx2,	$v10
.name mtx3,	$v11
.name mtf0,	$v12
.name mtf1,	$v13
.name mtf2,	$v14
.name mtf3,	$v15
.name txtscale,	$v17
.name st12,	$v18
.name wscl,	$v19
	
.name voutbaki, $v21
.name voutbakf, $v20
.name vout12i,	$v29
.name vout12f,	$v28
.name invW12i,	$v27
.name invW12f,	$v26
	
#ifndef	F3DEX_GBI
	# 直接ロードするので必要なし
		addi	i, n, 0			# initialize loop counter
#endif
		ldv	vin12[0],  0(in_bufp)	# load first vert to transform
		ldv	vin12[8], 16(in_bufp)	# load second vert to transform
		
	# handle loads where v0 is not zero...
#ifdef	F3DEX_GBI
	# ロード遅延対策の為
		srl	i, i, 2
	# gbi で 2 倍値を設定しテーブル参照する
		jal	getScaleTrans
#ifdef	NEWCOM
	# NEWCOM 仕様
	#  voutp テーブルを参照するためのポインタを取得
	#  必要でないデータを捨てる位置の設定
		addi	vtxid, $1, 0
		addi	goutp,  zero, RSP_SCRATCH_FLIGHT
		addi	goutp2, zero, RSP_SCRATCH_FLIGHT
#else
		lhu	voutp, RSP_VADDR_TABLE($1)
#endif
#else
		addi	voutp, zero, RSP_POINTS_OFFSET
		sll	tmp, v0, 5	# offset = v0 *	sizeof(point_buffer)
		sll	v0, v0, 3
		add	tmp, v0, tmp	# (v0*32)+(v0*8)

		jal	getScaleTrans		# get screen scale & translate
		add	voutp, voutp, tmp	# delay slot
#endif
	
	# get texture coordinate scale; jump to matrix load routine
		llv	txtscale[0], RSP_STATE_TEX_SCALE_S(rsp_state)
		jal	getMatrix
		llv	txtscale[8], RSP_STATE_TEX_SCALE_S(rsp_state)
 ### JUMP OCCURS to getMatrix: subroutine


xfm_loop:
 	# do MP matrix multiplication:	
	# This is clever. We multiply each ROW of the matrix
	# by one of the scalar point coordinates, using the
	# accumulator to sum up the matrix columns.
	#
	# (could call MatCat, slower but less code...)
	#
	# do SU loads (colors and texture coords) while we do this...
	#
		vmudn	vout12f, mtf0, vin12[0h]
	llv	st12[0], ( 0+8)(in_bufp)
		vmadh	vout12f, mtx0, vin12[0h]
	lw	clr1, ( 0+12)(in_bufp)
		vmadn	vout12f, mtf1, vin12[1h]
	lw	clr2, (16+12)(in_bufp)
		vmadh	vout12f, mtx1, vin12[1h]
.name lighton,	$1
	andi	lighton,gmode,(G_LIGHTING_H)
		vmadn	vout12f, mtf2, vin12[2h]
#ifdef	CLIPSWITCH
	# F3DLX のクリップ ON/OFF 機能の実装
	andi	gfx1, gmode, (G_CLIPPING_H)
#endif
		vmadh	vout12f, mtx2, vin12[2h]
#ifdef	NEWCOM
	# NEWCOM 仕様 / voutp アドレスを毎回テーブル参照する
	lhu	voutp, RSP_VADDR_TABLE(vtxid)
#endif	
		vmadn	vout12f, mtf3, vconst[1]	# w = 1.0
	llv	st12[8], (16+8)(in_bufp)
		vmadh	vout12i, mtx3, vconst[1]
	
#ifdef FASTLIGHT3D
	bne	lighton, zero, doLight		# do lighting here if on
#endif /* FASTLIGHT3D */
	addi	in_bufp, in_bufp, 32		# start working on next vtx

 ### BRANCH OCCURS TO doLight: subroutine if lighting is on

.unname lighton

lightReturn:
		# scale texture coordinates:
		vmudm	st12, st12, txtscale		# apply scale
#ifdef	CLIPSWITCH
	# F3DLX のクリップ ON/OFF 機能の実装
	beq	gfx1, zero, ClipSkipped
#endif
#ifdef	NEWCOM
	# NEWCOM 仕様 / vout2 アドレスを毎回テーブル参照する
		lhu	voutp2, RSP_VADDR_TABLE+2(vtxid)
#endif	
screenCalc:
	
        # save the vout12i and vout12f into voutbaki and voutbakf
        # scale down 'w' by wscl[0] to keep 1/w reasonably large (DxF -> D)
	# do SU load (vertex flags) during this compute.
	# do CLIP store during this compute.
	# CLIP CODES are 0000 0zyx 0000 0ZYX where zyx=-z,-y,-x and ZYX=+z,+y,+x
	# The high 16 bit clipcodes are for trivial rejection and the low 16 bit
	# clipcodes are for clipping and trivial accepting

	lsv	voutbaki[0], (CLIP_SELECT+6)(zero)	# multiplier

#ifdef NEAR_CLIP_OFF
  /* draw stuff even when it is in front of the near clipping plane */
		vmudn	voutbakf, vout12f, voutbaki[0]	# mult x frustrum
	mfc2	flg1, vout12i[6]			#n w comp of vtx 1
	andi	flg1, flg1, 0x8000			#n  neg bit
	srl	flg1, flg1, 13				#n  in place
		vmadh	voutbaki, vout12i, voutbaki[0]	#
	mfc2	flg2, vout12i[14]			# w comp of vtx 2
	andi	flg2, flg2, 0x8000			#  neg bit
	srl	flg2, flg2, 9				#  in place

		vch	vtmp, vout12i, vout12i[3h]	# rejection clipcodes
	or	flg2, flg2, flg1			#n  vtx 1 & 2
		vcl	vtmp, vout12f, vout12f[3h]	# rejection clipcodes
	cfc2	flg1, $vcc 				# rejection clipcodes

		vch	vtmp, vout12i, voutbaki[3h]	# clip/accept clipcodes
#ifdef	NEWCOM
	# NEWCOM 仕様 / goutp, goutp2 アドレスを取得する
	addi	goutp,  voutp,  0
	addi	goutp2, voutp2, 0
#endif	
		vcl	vtmp, vout12f, voutbakf[3h]	# clip/accept clipcodes

 #n	andi	tmp,  flg1, 0x0707			# reject clipcodes - vtx 1
 #n	andi	flg1, flg1, 0x7070			# reject clipcodes - vtx 2
	andi	tmp,  flg1, 0x0703			#n reject clipcodes - vtx 1
	andi	flg1, flg1, 0x7030			#n reject clipcodes - vtx 2
	or	flg1, flg1, flg2			#n complete with nearclip
	sll	tmp,  tmp,  4				# vtx 1 in 0x00007070
	sll	flg1, flg1, 16				# vtx 2 in 0x70700000
	or	flg1, flg1, tmp				# all reject clipcodes in place

#else /* NEAR_CLIP_OFF */
  /* Don't draw anything in front of the near clipping plane */
		vmudn	voutbakf, vout12f, voutbaki[0]	# mult x frustrum
		vmadh	voutbaki, vout12i, voutbaki[0]	#
		vch	vtmp, vout12i, vout12i[3h]	# rejection clipcodes
#ifdef	NEWCOM
	# NEWCOM 仕様 / goutp, goutp2 アドレスを取得する
	addi	goutp,  voutp,  0
	addi	goutp2, voutp2, 0
#endif
		vcl	vtmp, vout12f, vout12f[3h]	# rejection clipcodes
	cfc2	flg1, $vcc 				# rejection clipcodes

		vch	vtmp, vout12i, voutbaki[3h]	# clip/accept clipcodes
		vcl	vtmp, vout12f, voutbakf[3h]	# clip/accept clipcodes

	andi	tmp,  flg1, 0x0707			# reject clipcodes - vtx 1
	andi	flg1, flg1, 0x7070			# reject clipcodes - vtx 2
	sll	tmp,  tmp,  4				# vtx 1 in 0x00007070
	sll	flg1, flg1, 16				# vtx 2 in 0x70700000
	or	flg1, flg1, tmp				# all reject clipcodes in place

#endif /* NEAR_CLIP_OFF */


	cfc2	flg2, $vcc 				# clip/accept clipcodes

	andi	tmp,  flg2, 0x0707			# clip/accept clipcodes - vtx 1
#ifdef	CLIPSWITCH
ClipSkipped:
#endif
					vadd    voutbaki, vout12i, vconst[0]	
	andi	flg2, flg2, 0x7070			# clip/accept clipcodes - vtx 2
					vadd    voutbakf, vout12f, vconst[0]
	sll	flg2, flg2, 12				# vtx 2 in 0x07070000
					vmudl   vout12f, vout12f, wscl[0]
	or	tmp,  tmp,  flg2			# all clip/accept clipcodes in place
					vmadm   vout12i, vout12i, wscl[0]
	or	tmp,  tmp,  flg1			# all clipcodes in place
					vmadn   vout12f, vconst, vconst[0]
#ifdef	F3DEX_GBI
	jal	NewtonDiv
#ifdef	NEWCOM
	sh	tmp, ( 0+RSP_PTS_CC)(goutp)
#else
	sh	tmp, ( 0+RSP_PTS_CC)(voutp)
#endif
#else
	sh	tmp, ( 0+RSP_PTS_CC)(voutp)
	
	# project (divide by w)
	# W and invW registers are aligned for the Newton's call...
		jal	NewtonDiv
		lh	flg1, ( 0+6-32)(in_bufp)	# delay slot
#endif
	
	# max 1/w if negative for screen calc
					vge	scrn12i, invW12i, vconst[0]
#ifdef	NEWCOM	
	sdv	voutbaki[0], ( 0+RSP_PTS_X_INT)(goutp)
					vmrg	scrn12i, invW12i, vconst1[0]
	sdv	voutbakf[0], ( 0+RSP_PTS_X_FRAC)(goutp)

#else
	sdv	voutbaki[0], ( 0+RSP_PTS_X_INT)(voutp)
					vmrg	scrn12i, invW12i, vconst1[0]
	sdv	voutbakf[0], ( 0+RSP_PTS_X_FRAC)(voutp)
#endif					
					vmudl	persp12f, voutbakf, invW12f[3h]
#ifdef	NEWCOM
	# vtxid の更新
	addi	vtxid, vtxid, 4
#endif
					vmadm	persp12f, voutbaki, invW12f[3h]
					vmadn	persp12f, voutbakf, scrn12i[3h]
					vmadh	persp12i, voutbaki, scrn12i[3h]
	
	# image space (viewport scale and translate)
	# increment input pointer and load next verts to transform 
	# while we compute...
	#
 	# The viewport scale and translate has a built-in multiplier
	# of 4.0 which converts screen coords to S11.2.
	#
#ifdef	F3DEX_GBI
	addi	i, i, -2
#else
	addi	i, i, -1
#endif
        # scale down x,y,z to compensate for prev scaling down of w (DxF -> D)
                        vmudl   persp12f, persp12f, wscl[0]
                        vmadm   persp12i, persp12i, wscl[0]
                        vmadn   persp12f, vconst, vconst[0]

.name fogon,	$12
	andi 	fogon,gmode,G_FOG_H	# fog on?
	ldv	vin12[0],  0(in_bufp)
					# screen translate
					vmudh	scrn12f, vptrans, vconst[1]
	ldv	vin12[8], 16(in_bufp)
					# screen scale
					vmadn	scrn12f, persp12f, vpscale
	# Clamp x,y,z to +/- 2K boundary (Z clamps 0...0x7FFF)
	ldv	vout12i[0], VCONST_SCREENCLAMP(zero)
					vmadh	scrn12i,  persp12i, vpscale
	ldv	vout12i[8], VCONST_SCREENCLAMP(zero)
					vmadn	scrn12f, vconst, vconst[0]

		vge	scrn12i,scrn12i,vout12i[1q]	# clamp xy -0x3fe, z 0
	sw	clr1,       ( 0+RSP_PTS_R_NX)(voutp)
#ifdef	NOSUBPIXEL
		vor	vout12f, vconst, vconst1[1]	# vout12f[5] = 0xfffc
#endif
	beq	fogon,zero,storeVertex	# skip fog if turned off
		vlt	scrn12i,scrn12i,vout12i[0q]	# clamp xy 0x3fe, z max

 ### BRANCH OCCRS to storeVertex: if fog is off

 #
 # FOG
 # screen z is a s10.16 number in scrn12i, scrn12f at this point
 #

/**************** START TURN OFF FOG FOR SPACE */

	lqv	vtmp[0], RSP_FOG_FACTOR(zero)
	vmudn	persp12f, persp12f, vtmp[0]
	vmadh	persp12i, persp12i, vtmp[0]
	vadd	persp12i, persp12i, vtmp[1]
	vge	persp12i, persp12i, vconst[0]	# clamp: min=0x00
	vlt	persp12i, persp12i, vtmp[2]	# clamp: max=0xff

	sbv	persp12i[5],(0+RSP_PTS_R_NX+3)(voutp)  # store forg into vtx2 color
	sw	clr2, ( 0+RSP_PTS_XS)(voutp)	   # temp storage place
	sbv	persp12i[13],( 0+RSP_PTS_XS+3)(voutp)  # insert fog into vtx1 color
 	lw	clr2, ( 0+RSP_PTS_XS)(voutp)	   # load color with fog


/**************** END TURN OFF FOG FOR SPACE *************/
.unname fogon


    # output transformed vertex information:
    # 	3D points for clipping, w
    #  	screen points
    #  	vertex flag
    #  	clip code
    #  	texture coord
    #  	colors/normals

	# always write the rest of the first point: (weird order to
	# avoid stalls)

    # MOVE THESE UP TO INTO SOME OF THE VU CODE?
storeVertex:
#ifdef	NOSUBPIXEL
	vadd	persp12i, scrn12i, vconst[2]
#endif
		slv	st12[0],    ( 0+RSP_PTS_S)(voutp)
 		sdv	scrn12i[0], ( 0+RSP_PTS_XS)(voutp)
#ifdef	NOSUBPIXEL
	vand	persp12i, persp12i, vout12f[5]
#endif
 		ssv	scrn12f[4], ( 0+RSP_PTS_ZSF)(voutp)
  		ssv	invW12i[6], ( 0+RSP_PTS_INVW_INT)(voutp)
#ifdef	F3DEX_GBI
#  ifdef	NOSUBPIXEL
 		ssv	invW12f[6], ( 0+RSP_PTS_INVW_FRAC)(voutp)	
		bltz	i, xfm_done
		ssv	persp12i[2],( 0+RSP_PTS_YS)(voutp)
#  else
		bltz	i, xfm_done
 		ssv	invW12f[6], ( 0+RSP_PTS_INVW_FRAC)(voutp)
#  endif
#else
 		ssv	invW12f[6], ( 0+RSP_PTS_INVW_FRAC)(voutp)
	
	# maybe write the 2nd point:
		blez	i, xfm_done
		addi	i, i, -1
#endif

#ifdef	NEWCOM
		sdv	voutbaki[8], ( 0+RSP_PTS_X_INT)(goutp2)
		sdv	voutbakf[8], ( 0+RSP_PTS_X_FRAC)(goutp2)
		slv	st12[8],     ( 0+RSP_PTS_S)(voutp2)
		sw	clr2,        ( 0+RSP_PTS_R_NX)(voutp2)
 		sdv	scrn12i[8],  ( 0+RSP_PTS_XS)(voutp2)
#ifdef	NOSUBPIXEL
		ssv	persp12i[10],( 0+RSP_PTS_YS)(voutp2)
#endif
 		ssv	scrn12f[12], ( 0+RSP_PTS_ZSF)(voutp2)
  		ssv	invW12i[14], ( 0+RSP_PTS_INVW_INT)(voutp2)
  		ssv	invW12f[14], ( 0+RSP_PTS_INVW_FRAC)(voutp2)
		sw	tmp,         ( 0+RSP_PTS_CC)(goutp2)
#else	/* NEWCOM */
		sdv	voutbaki[8], (40+RSP_PTS_X_INT)(voutp)
		sdv	voutbakf[8], (40+RSP_PTS_X_FRAC)(voutp)
		slv	st12[8],     (40+RSP_PTS_S)(voutp)
		sw	clr2,        (40+RSP_PTS_R_NX)(voutp)
 		sdv	scrn12i[8],  (40+RSP_PTS_XS)(voutp)
#ifdef	NOSUBPIXEL
		ssv	persp12i[10],(40+RSP_PTS_YS)(voutp)
#endif
 		ssv	scrn12f[12], (40+RSP_PTS_ZSF)(voutp)
  		ssv	invW12i[14], (40+RSP_PTS_INVW_INT)(voutp)
  		ssv	invW12f[14], (40+RSP_PTS_INVW_FRAC)(voutp)
		sw	tmp,         (40+RSP_PTS_CC)(voutp)
			
	# prepare for the next pair of points
		addi	voutp, voutp, 80	# increment output pointer
						# (with padding)
#endif	/* NEWCOM */

		bgtz	i, xfm_loop	# for (i=n_pts; i>0; i--) {
 # The xfm_done: label is now in gdma.s
 # xfm_done:	nop
	
	
	# transform normals and shade
.unname gmode
	
.unname n
.unname v0
.unname voutp
.unname tmp
.unname i

.unname flg1
.unname flg2
.unname clr1
.unname clr2

#ifdef	NEWCOM
.unname goutp
.unname voutp2
.unname goutp2
.unname vtxid
.name	gfx0, $25
#endif
	
.unname vpscale
.unname vptrans
.unname vin12
.unname voutbaki
.unname voutbakf
.unname vout12i
.unname vout12f
.unname invW12i
.unname invW12f
.unname vtmp
.unname persp12i
.unname persp12f
.unname scrn12i
.unname scrn12f

.unname mtx0
.unname mtx1
.unname mtx2
.unname mtx3
.unname mtf0
.unname mtf1
.unname mtf2
.unname mtf3
.unname txtscale
.unname st12
.unname wscl

 #
 #
 #
 ############################################################################