//////////////////////////////////////////////////////////////////////////////
//
// Asura_D3D_VS_FlowingWater.cpp
//
// Created by AAM - 20/04/2004
//
// Copyright (c) 2004 Rebellion. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////

/******************************************************************************/
/* Included Files                                                             */
/******************************************************************************/

#include "Core/Asura.h"

#include "Rendering/Asura_Direct3D_Shaders.h"

/******************************************************************************/
/* Global Constants                                                           */
/******************************************************************************/

// Vertex stream declaration for the Asura_D3DMinimalVertex structure
/*
static Direct3D_VertexElement g_ax_Vertex_Decl[] = 
{
	{0,  0,  D3DDECLTYPE_FLOAT3,	D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION,	0},
	{0,  12, D3DDECLTYPE_FLOAT3,	D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,		0},
	{0,  24, D3DDECLTYPE_D3DCOLOR,	D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR,		0},
	{0,  28, D3DDECLTYPE_FLOAT2,	D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD,	0},

	D3DDECL_END()
};
*/
static Direct3D_VertexElement g_ax_Vertex_Decl[] =
{
	{	0,	0,	D3DDECLTYPE_FLOAT3,		D3DDECLMETHOD_DEFAULT,	D3DDECLUSAGE_POSITION,	0 },
	{	0, 12,	D3DDECLTYPE_D3DCOLOR,	D3DDECLMETHOD_DEFAULT,	D3DDECLUSAGE_NORMAL,	0 },
	{	0, 16,	D3DDECLTYPE_D3DCOLOR,	D3DDECLMETHOD_DEFAULT,	D3DDECLUSAGE_COLOR,		0 },
	{	0, 20,	D3DDECLTYPE_SHORT2,		D3DDECLMETHOD_DEFAULT,	D3DDECLUSAGE_TEXCOORD,	0 },
	{	0, 24,	D3DDECLTYPE_SHORT2,		D3DDECLMETHOD_DEFAULT,	D3DDECLUSAGE_TEXCOORD,	1 },
	D3DDECL_END()
};

const char* const szCODE =

// Expected constants:
// c0-c3	View-proj matrix
// c4.x		u texture offset
// c4.y		v texture offset
// c7.x		constant k0 [= (amp u) sin (phase u)]
// c7.y		constant k1 [= (amp v) sin (phase v)]
// c7.z		8 pi
// c10		(pi, 1/2, 2 pi, 1 / (2 pi)		(Constants for wrap into [-Pi, Pi])
// c11		(1, -1/(2!), 1/(4!), -1/(6!)	(Constants for cos Taylor series expansion)
// c12		(1, -1/(3!), 1/(5!), -1/(7!)	(Constants for sin Taylor series expansion)

// y coord is perturbed by k0 cos (2 pi u) + k1 sin (8 pi v)

"vs.1.1 \n"

" dcl_position	v0 \n"
" dcl_normal	v1 \n"
" dcl_color		v2 \n"
" dcl_texcoord0	v3 \n"

// Get the position into r3 so we can modify the y-coord later
"mov r3, v0 \n"
// decompress texcoords into r4
"mad r4.xy, v3.xy, c95.xx, c95.yy \n"

// Get 2 pi * u tex coord into r1.x
"mul r1.x, c10.z, r4.x \n"

// Wrap it to [-Pi, Pi]
"mad  r0.x,  r1.x, c10.w,  c10.y \n"
"expp r0.y,  r0.x \n"
"mad  r0.x,  r0.y, c10.z, -c10.x \n"

// Get the cos of it into r0.x
"dst  r2.xy, r0.x, r0.x		; generate 1, (r0.x)^2, .. (r0.x)^6 \n"
"mul  r2.z,  r2.y, r2.y \n"
"mul  r2.w,  r2.y, r2.z \n"
//"mul  r0,    r2,   r0.x		; generate r0.x, (r0.x)^3, .., (r0.x)^7 \n"
//"dp4  r0.y,  r0,   c12		; compute sin(r0.x) \n"
"dp4  r0.x,  r2,   c11		; compute cos(r0.x) \n"

// Scale it by k0 and add to y-coord
"mad r3.y, c7.x, r0.x, r3.y \n"

// Get 8 pi * v tex coord into r1.x
"mul r1.x, c7.z, r4.y \n"

// Wrap it to [-Pi, Pi]
"mad  r0.x,  r1.x, c10.w,  c10.y \n"
"expp r0.y,  r0.x \n"
"mad  r0.x,  r0.y, c10.z, -c10.x \n"

// Get the sin of it into r0.y
"dst  r2.xy, r0.x, r0.x		; generate 1, (r0.x)^2, .. (r0.x)^6 \n"
"mul  r2.z,  r2.y, r2.y \n"
"mul  r2.w,  r2.y, r2.z \n"
"mul  r0,    r2,   r0.x		; generate r0.x, (r0.x)^3, .., (r0.x)^7 \n"
"dp4  r0.y,  r0,   c12		; compute sin(r0.x) \n"
//"dp4  r0.x,  r2,   c11		; compute cos(r0.x) \n"

// Scale it by k1 and add to y-coord
"mad r3.y, c7.y, r0.y, r3.y \n"

// Shove into output registers, shifting tex coords
"m4x4 oPos, r3, c0 \n"
"mov oD0, v2 \n"
"add oT0.xy, r4.xy, c4.xy";

// Register the shader with Asura

const Asura_Direct3D_VertexShader xSHADER(ASURA_D3D_VS_FLOWINGWATER, szCODE, g_ax_Vertex_Decl);
