; Copyright (c) 2015 Yuri Kunde Schlesner
; Copyright (c) 2016 Jeffrey Pfau
;
; This Source Code Form is subject to the terms of the Mozilla Public
; License, v. 2.0. If a copy of the MPL was not distributed with this
; file, You can obtain one at http://mozilla.org/MPL/2.0/.

; Uniforms
.fvec projectionMtx[4]
.fvec textureMtx[2]

; Constants
.constf consts1(0.0, 1.0, -0.5, -1.0)

; Outputs : here only position and color
.out out_pos position
.out out_tc0 texcoord0
.out out_col color

; Inputs : here we have only vertices
.alias in_pos v0
.alias in_tc0 v1
.alias in_col v2

.gsh
.proc main
	; Set up the vertex endpoints
	mov r0.xy, in_pos.xy
	mov r0.zw, consts1.zy
	add r1.xy, r0.xy, in_pos.zw

	dp4 r2.x, projectionMtx[0], r0
	dp4 r2.y, projectionMtx[1], r0
	dp4 r2.z, projectionMtx[2], r0
	dp4 r2.w, projectionMtx[3], r0

	dp4 r3.x, projectionMtx[0], r1
	dp4 r3.y, projectionMtx[1], r1
	dp4 r3.z, projectionMtx[2], r1
	dp4 r3.w, projectionMtx[3], r1

	; Set up the texture endpoints
	mov r0.xy, in_tc0.xy
	mov r0.zw, consts1.xy
	add r1.xy, r0.xy, in_tc0.zw

	dp4 r4.x, textureMtx[0], r0
	dp4 r4.y, textureMtx[1], r0
	mov r4.zw, consts1.xy

	dp4 r5.x, textureMtx[0], r1
	dp4 r5.y, textureMtx[1], r1
	mov r5.zw, consts1.xy

	; Emit top-left
	setemit 0
	mov out_pos.xyzw, r2.xyzw
	mov out_tc0.xyzw, r4.xyzw
	mov out_col, in_col
	emit

	; Emit bottom-left
	setemit 1
	mov out_pos.x, r2.x
	mov out_pos.y, r3.y
	mov out_pos.z, consts1.z
	mov out_pos.w, consts1.y
	mov out_tc0.x, r5.x
	mov out_tc0.y, r4.y
	mov out_tc0.z, consts1.x
	mov out_tc0.w, consts1.y
	mov out_col, in_col
	emit

	; Emit bottom-right
	setemit 2, prim
	mov out_pos.xyzw, r3.xyzw
	mov out_tc0.xyzw, r5.xyzw
	mov out_col, in_col
	emit

	; Emit top-right
	setemit 1, prim inv
	mov out_pos.x, r3.x
	mov out_pos.y, r2.y
	mov out_pos.z, consts1.z
	mov out_pos.w, consts1.y
	mov out_tc0.x, r4.x
	mov out_tc0.y, r5.y
	mov out_tc0.z, consts1.x
	mov out_tc0.w, consts1.y
	mov out_col, in_col
	emit

	end
.end