gvtx.s 10.6 KB

#include "gfx_regs.h"

 ############################################################################	
 #
 # 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.
		srl	$1, $1, 4	# pick off 'n' field 
		addi	$5, $1, 1	# n is really (n-1)

 # 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
	
.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
	
		addi	i, n, 0			# initialize loop counter
		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...
		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

	# 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]
		vmadh	vout12f, mtx2, vin12[2h]
		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

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
		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
		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
					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]
	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

	# max 1/w if negative for screen calc
					vge	scrn12i, invW12i, vconst[0]
	sdv	voutbaki[0], ( 0+RSP_PTS_X_INT)(voutp)
					vmrg	scrn12i, invW12i, vconst1[0]
	sdv	voutbakf[0], ( 0+RSP_PTS_X_FRAC)(voutp)
					
					vmudl	persp12f, voutbakf, invW12f[3h]
					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.
	#
	addi	i, i, -1

        # 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)
	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 SUPER
	# round the screen coordinates to nearest integer pixel.
	# this helps prevent cracks in the reduced-precision triangle setup.
	# use of 'clr1' register and 'persp12i' register is sloppy; 
	# we know these registers can be trashed at this point, I
	# just keep the name to help with future register allocation
	# problems...
		ori	clr1, zero, 0xfffc
		mtc2	clr1, persp12i[0]
		vadd	scrn12i, scrn12i, vconst[2]
		vand	scrn12i, scrn12i, persp12i[0]
#endif
		slv	st12[0],    ( 0+RSP_PTS_S)(voutp)
 		sdv	scrn12i[0], ( 0+RSP_PTS_XS)(voutp)
 		ssv	scrn12f[4], ( 0+RSP_PTS_ZSF)(voutp)
  		ssv	invW12i[6], ( 0+RSP_PTS_INVW_INT)(voutp)
 		ssv	invW12f[6], ( 0+RSP_PTS_INVW_FRAC)(voutp)
	
	# maybe write the 2nd point:
		blez	i, xfm_done
		addi	i, i, -1
		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)
 		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)
		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
	
.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

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