//
// Asura_D3D_VS_Tools_Botanicals.cpp
//
// Created by DMR - 16 Feb 2006
//
// Copyright (C) 2004 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"

// These are tools-only shaders
// They will fail to compile if we are not building ASURA_TOOLS
#if defined ASURA_TOOLS

#if defined( ASURA_USE_BOTANICALS)

// This file's header.
#include "Rendering/Asura_Direct3D_Shaders.h"

// Vertex Declarator
// (for D3DGenericVertex structure)

static Direct3D_VertexElement auVSD[] =
{
	{	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 },
	{	0, 36,	D3DDECLTYPE_FLOAT2,		D3DDECLMETHOD_DEFAULT,	D3DDECLUSAGE_TEXCOORD,	1 },
	D3DDECL_END()
};

const char* const szVS_TOOLS_BOTANICALS_LINEARFOG_CODE =

// Botanicals shader. Linear fog
// Expected constants:
//   c0-3   : ViewProj matrix
//	 c4	    : Fog scaling values (x,y), min/max (z,w)
//   c30-33 : World matrix
"vs.1.1 \n"
"dcl_position0	v0 \n"
"dcl_color0		v1 \n"
"dcl_texcoord0	v2 \n"
// Do the position
"m4x4 r1, v0, c30 \n"
"m4x4 r0, r1, c0 \n"
"mov oPos, r0 \n"
// Diffuse
"mov oD0, v1 \n"
// Texcoords
"mov oT0, v2\n"
// Fog
"mad r0.x, r0.w, c4.x, c4.y \n"
"max r0.x, r0.x, c4.z \n"
"min oFog, r0.x, c4.w \n";

// Register the shader with Asura
const Asura_Direct3D_VertexShader xVS_TOOLS_BOTANICALS_LINEARFOG_SHADER(ASURA_D3D_VS_TOOLS_BOTANICALS_LINEARFOG, szVS_TOOLS_BOTANICALS_LINEARFOG_CODE, auVSD);


const char* const szVS_TOOLS_BOTANICALS_GRAPHFOG_CODE =

// Botanicals shader. Linear fog
// Expected constants:
//   c0-3   : ViewProj matrix
//	 c4	    : Fog scaling values (x,y), min/max (z,w)
//   c10-13 : World matrix
"vs.1.1 \n"
"dcl_position0	v0 \n"
"dcl_color0		v1 \n"
"dcl_texcoord0	v2 \n"
// Do the position
"m4x4 r1, v0, c30 \n"
"m4x4 r0, r1, c0 \n"
"mov oPos, r0 \n"
// Diffuse
"mov oD0, v1 \n"
// Texcoords
"mov oT0, v2\n"
// Fog
// our distance from the camera (in metres) is in r0.w (oPos.w)
// c5.x: 1 / (fardistance - neardistance)
// c5.y: (-neardistance / (fardistance - neardistance))
"mad r1.z, r0.w, c5.x, c5.y \n"
"min r1.z, r1.z, c10.y \n"
"max r1.z, r1.z, c10.x \n"
// so r1.z is now in the [0..1] range, 1 being farthest and 0 being nearest.
// decide which part of the spline we're in
// a 0.0 in r1.x means we want c+0, a 1.0 means we want c+1
"sge r0.x, r1.z, c5.z \n"
"mov a0.x, r0.x \n"
// rescale r1.z for this part of the spline
"mad r1.z, r1.z, c[6+a0.x].x, c[6+a0.x].y \n"
// we want to build (r1.z^3, r1.z^2, r1.z, 1) for easy calc
"mov r1.w, c5.w \n"
"mul r1.y, r1.z, r1.z \n"
"mul r1.x, r1.y, r1.z \n"
// dp4 to generate the output value
"dp4 oFog, r1, c[8+a0.x] \n";

// Register the shader with Asura
const Asura_Direct3D_VertexShader xVS_TOOLS_BOTANICALS_GRAPHFOG_SHADER(ASURA_D3D_VS_TOOLS_BOTANICALS_GRAPHFOG, szVS_TOOLS_BOTANICALS_GRAPHFOG_CODE, auVSD);

#endif // #if defined( ASURA_USE_BOTANICALS)

#endif // defined ASURA_TOOLS