; constants
.constf const0(0.0, 0.5, 1.0, 2.0)
.constf const1(0.00392156886, 3.05185094e-005, 0.00001, 0.00787401574)

.alias ZERO      const0.x
.alias HALF      const0.y
.alias ONE       const0.z
.alias TWO       const0.w
.alias INV_BYTE  const1.x
.alias INV_SHORT const1.y
.alias EPS       const1.z
.alias INV_127   const1.w

; uniforms
.fvec uViewPos
.fvec uViewProj[4]
.fvec uBasis[2]
.fvec uLightPos[4]
.fvec uLightColor[4]
.fvec uMaterial
.alias MAT_AMBIENT uMaterial.y
.alias MAT_ALPHA   uMaterial.w

; in
.alias aCoord    v0
.alias aNormal   v1
.alias aTexCoord v2
.alias aColor    v3
.alias aLight    v4

; variables
.alias lv0    r0
.alias lv1    r1
.alias lv2    r2
.alias lv3    r3
.alias normal r4
.alias att    r5
.alias light  r6
.alias pos    r7
.alias tmp    r8
.alias size   r4
.alias vv     r6

; out
.out vPosition position
.out vTexCoord texcoord0
.out vColor    color

.proc main
; pos = aCoord + mulQuatPos(uBasis, aTexCoord.zw)
    mov pos.z, ZERO
    mov pos.xy, aTexCoord.zw
    mul size.xyz, uBasis[0], pos.zxy
    mad size.xyz, pos.xyz, uBasis[0].zxy, -size
    mad size.xyz, pos.yzx, uBasis[0].w,  size
    mul tmp.xyz, uBasis[0].zxy, size.xyz
    mad size.xyz, size.yzx, uBasis[0].yzx, -tmp
    mad pos.xyz, size, TWO, pos
    add size.xyz, uBasis[1], aCoord
    add pos.xyz, pos, size
    mov pos.w, ONE

; vPosition = uViewProj * pos
    mul tmp, uViewProj[0], pos.x
    mad tmp, pos.y, uViewProj[1], tmp
    mad tmp, pos.z, uViewProj[2], tmp
    mad vPosition, pos.w, uViewProj[3], tmp

; vColor = premultAlpha(aLight / 255)
    mul tmp, INV_BYTE, aLight
    mul tmp.xyz, tmp, tmp.w
    mov vColor, tmp

; vTecCoord = aTexCoord/32767
    mul vTexCoord, INV_SHORT, aTexCoord

    end
.end
