; Inputs
.alias 		inpos0 		v0		; x = x 		(xxxxxxxx xxxxxxxx) 
								;		x: x (-256 to 511)

								; y = y 		(0aa0dddd yyyyyyyy)
								; 		a: alpha (0 = 0.0, 1 = 0.5, 2 = 1.0, 3 = undefined)
								;		d: depth (0 - 16)
								;       y: y (0 - 255)
								 
								; z = 			(vhtttttt tttttttt)
								;      v: Flip vertical
								;	   h: Flip horizontal
								;      t: tex base

.alias 		intex 		v1		; (u, v)  or  (r, g, b, a)

; Outputs
.out 		outpos 		position
.out 		outtex 		texcoord0
.out 		outcol 		color

; Uniforms
.fvec 		projection[4]		; #0 - 3
.fvec 		textureScale		; #4
.fvec		updateFrame			; #5
.fvec		textureOffset		; #6 - Use this for Hi-res flipping
.fvec		uPadding6			; #7
.fvec		uPadding7			; #8
.fvec		uPadding8			; #9

; Constants
;.constf RGBA8_TO_FLOAT4	(0.00392156862, 0, 0, 0)
.constf RGBA8_TO_FLOAT4		(0.004032258064516, 0, 0, 0)	; Bug fix for 5-bit color conversion
.constf DEPTH 				(0.03125, -256, 0.00390625, 1)
.constf ALPHA_0_0 			(24576, -24576, 2, 0)			; Set the alpha to 2 (slightly more alpha leak, but not visible...)
.constf ALPHA_0_5 			(8192, -8192, 128, 0)
.constf ALPHA_1_0 			(16384, -16384, 255, 0)
.constf V_FLIP				(0, 32768, -1, 8)
.constf H_FLIP				(16384, -16384, -1, 8)
.constf ONE_DIV_128			(0.0078125, -128, 0, 8)
.constf NEGATIVE_ONE		(-1, -1, -1, -1)
.constf ZERO				(0, 0, 0, 0)
.constf ONE					(1, 1, 1, 1)
.constf SMALLALPHA			(1, 0, 0, 0)

.proc main
	mov		r0, inpos0
	mov		r10, intex

	add		r10, textureOffset, r10

	mov		r1, NEGATIVE_ONE		; r1 = -1
	cmp		r0.z, eq, eq, r1.z		; is r0.z (flip+texbase) == -1 
	jmpc	cmp.x, do_not_draw		; if yes, go to do_not_draw

	; computing v-flip
	;
	mov		r2, V_FLIP		
	cmp		r0.z, ge, ge, r2.x		; is r0.z > 0? (or 0x8000 is not set)
	jmpc	cmp.x, no_vflip			; if yes go to no_vflip, else:
	mul		r10.y, r10.y, r2.z		; r10.y = -r10.y 
	add		r10.y, r10.y, r2.w		; r10.y = 8 + r10.y
	add		r0.z, r0.z, r2.y		; r0.z = r0.z + 32768 (removes the 0x8000 bit)
no_vflip:

	; computing h-flip
	;
	mov		r2, H_FLIP		
	cmp		r0.z, lt, lt, r2.x		; is r0.z < 16384? (or 0x4000 is not set)
	jmpc	cmp.x, no_hflip			; if yes go to no_hflip, else:
	mul		r10.x, r10.x, r2.z		; r10.y = -r10.y 
	add		r10.x, r10.x, r2.w		; r10.y = 8 + r10.y
	add		r0.z, r0.z, r2.y		; r0.z = r0.z - 16384 (removes the 0x4000 bit)
no_hflip:

	; computing texturePos
	;
	mov		r8, ONE_DIV_128
	mul		r9.y, r0.z, r8.x		; r9.y = r0.z / 128
	flr		r9.y, r9.y				; r9.y = floor (r9)			(r9.x is the base texture x coordinate)
	mul 	r7.x, r9.y, r8.y		; r7 = - r9.y * 128
	add		r9.x, r0.z, r7.x		; r9.y = r0.z - r9.y * 128	(r9.y is the base texture y coordinate)
	
	mul		r9.xy, r9.xy, r8.w 		; r9 = r9 * 8
	
	add		r10.xy, r10.xy, r9.xy

	; alpha computation
	;
	mov		r11, intex

	mov 	r1, ALPHA_0_0
	cmp 	r0.y, lt, lt, r1.x		; is r0.y < 24576?
	jmpc 	cmp.x, alpha_lt_0_0	; 	if yes then do nothing, else:
	add		r0.y, r0.y, r1.y		; r0.y = r0.y - 24576
	mov		r11.w, r1.z				; Set r11.w (primary color alpha) = 1
alpha_lt_0_0:
	mov 	r1, ALPHA_1_0
	cmp 	r0.y, lt, lt, r1.x		; is r0.y < 16384?
	jmpc 	cmp.x, alpha_lt_1_0	; 	if yes then do nothing, else:
	add		r0.y, r0.y, r1.y		; r0.y = r0.y - 16384
	mov		r11.w, r1.z				; Set r11.w (primary color alpha) = 255
alpha_lt_1_0:
	mov 	r1, ALPHA_0_5
	cmp 	r0.y, lt, lt, r1.x		; is r0.y < 8192?
	jmpc 	cmp.x, alpha_lt_0_5	; 	if yes then do nothing, else:
	add		r0.y, r0.y, r1.y		; r0.y = r0.y - 8192
	mov		r11.w, r1.z				; Set r11.w (primary color alpha) = 128
alpha_lt_0_5:

	;mov 	r1, ALPHA_1_0			; debugging only
	;mov		r11.w, r1.z			; debugging only

	; depth computation
	;
	mov		r1, ZERO
	cmp		r0.y, lt, lt, r1.x		; if r0.y >= 0, 
	jmpc	cmp.x, r0_y_lt_zero		; then do nothing, else:

	mov 	r1, DEPTH
	mul		r0.z, r0.y, r1.z		; r0.z = r0.y / 256
	flr		r0.z, r0.z				; r0.z = floor (r0.y / 256)
	mul 	r2.z, r0.z, r1.y		; r3.z = -floor (r0.y / 256) * 256
	add		r0.y, r0.y, r2.z		; r0.y = r0.y - floor (r0.y / 256) * 256 (depth removed from y)
	mul		r0.z, r0.z, r1.x		; r0.z = r0.z / 16

r0_y_lt_zero:
	mov 	r1, ONE
	mov 	r0.w, r1.w				; set r0.w = 1

	; outpos = projection * in.pos
	dp4 	outpos.x, projection[0].wzyx, r0
	dp4 	outpos.y, projection[1].wzyx, r0
	dp4 	outpos.z, projection[2].wzyx, r0
	dp4 	outpos.w, projection[3].wzyx, r0

	; outtex = in.texcoord
	mul 	outtex, textureScale, r10

	; outcol = RGBA8_TO_FLOAT4(in.color)
	mul 	outcol, RGBA8_TO_FLOAT4.xxxx, r11

	end

do_not_draw:
	mov		outpos, ZERO
	mov		outtex, ZERO
	mov		outcol, ZERO
	
	end

.end