/*
	Trampoline Sim.
	Copyright (c) 2009, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//=====================================================================================================================
/// @brief		Represents a uniform variable in a vertex or pixel shader.
//=====================================================================================================================

#include "Precompiled.h"
#include "Graphics\ShaderUniform.h"
#include "Graphics\Display.h"
#include "Graphics\Texture.h"

using namespace Game;

//=====================================================================================================================
/// @brief	Default constructor: creates an invalid uniform
//=====================================================================================================================

ShaderUniform::ShaderUniform()
{
	m_constant_table	= 0;
	m_constant_handle	= 0;
	m_d3d_device		= 0;
}

//=====================================================================================================================
/// @brief	Copy constructor. Creates the uniform from the other uniform.
///	@param	other - the other shader uniform to construct from
//=====================================================================================================================

ShaderUniform::ShaderUniform( const ShaderUniform & other )
{
	// See if valid:

	if ( other.IsValid() )
	{
		// Valid: copy the params

		m_constant_handle	= other.m_constant_handle;
		m_constant_table	= other.m_constant_table;
		m_d3d_device		= other.m_d3d_device;

		// Increase ref counts:

		m_d3d_device->AddRef();
		m_constant_table->AddRef();
	}
	else
	{
		// Not valid:

		m_constant_table	= 0;
		m_constant_handle	= 0;
		m_d3d_device		= 0;
	}
}

//=====================================================================================================================
/// @brief	Assignment operator. Assigns the other uniform to this one.
/// @param	other - the other shader uniform to assign from
//=====================================================================================================================

ShaderUniform & ShaderUniform::operator = ( const ShaderUniform & other )
{
	// Make sure not the same:

	if ( & other != this )
	{
		// Store our previous things here:

		IDirect3DDevice9 * previous_device = m_d3d_device;
		ID3DXConstantTable * previous_table = m_constant_table;

		// Right: see if the other one is valid:

		if ( other.IsValid() )
		{
			// Valid: copy everything over

			m_d3d_device		= other.m_d3d_device;
			m_constant_table	= other.m_constant_table;
			m_constant_handle	= other.m_constant_handle;

			// Increase ref count:

			m_d3d_device->AddRef();
			m_constant_table->AddRef();
		}
		else
		{
			// Not valid:

			m_d3d_device		= 0;
			m_constant_table	= 0;
			m_constant_handle	= 0;
		}

		// Reduce refs to previous things:
		
		if ( previous_table		) previous_table->Release();
		if ( previous_device	) previous_device->Release();
	}

	// Return this object

	return * this;
}

//=====================================================================================================================
/// @brief	Creates the uniform variable from a handle to a variable in a constant table
/// @param	device - direct3d device the shader is for
/// @param	constant_table - pointer to the d3d constant table for this shader
/// @param	constant_table_handle - handle to the actual d3d constant itself
//=====================================================================================================================

ShaderUniform::ShaderUniform( IDirect3DDevice9 * device , ID3DXConstantTable * constant_table , D3DXHANDLE constant_table_handle )
{
	// Save all data:

	m_constant_table	= constant_table;
	m_constant_handle	= constant_table_handle; 
	m_d3d_device		= device;

	// See if we're good:

	if ( IsValid() == false )
	{
		m_constant_table	= 0;
		m_constant_handle	= 0;
		m_d3d_device		= 0;
	}
	else
	{
		// Need to increase ref counts:

		m_constant_table->AddRef(); m_d3d_device->AddRef();
	}
}	

//=====================================================================================================================
/// @brief	Destructor for the shader uniform.
//=====================================================================================================================

ShaderUniform::~ShaderUniform()
{
	// Release this interface:

	if ( m_constant_table != 0 )
	{
		m_constant_table->Release();
	}

	// .. And this one:

	if ( m_d3d_device != 0 )
	{
		m_d3d_device->Release();
	}
}

//=====================================================================================================================
///	@brief		Sets the constant as a float
///	@param		value - value to set to
/// @pre		Uniform must be valid
//=====================================================================================================================

void ShaderUniform::SetFloat( float value ) const
{
	// Make sure ok:
	
	ENSURE( IsValid() , "Uniform must be valid" );

	// Set
		
	m_constant_table->SetFloat( m_d3d_device , m_constant_handle , value );
}

//=====================================================================================================================
/// @brief		Sets the constant as a vector 2
///	@param		value - value to set to
/// @pre		Uniform must be valid
//=====================================================================================================================

void ShaderUniform::SetVec2f( const Vec2f & value ) const
{
	// Make sure ok:
	
	ENSURE( IsValid() , "Uniform must be valid" );	

	// Convert to a directx vector:
	
	D3DXVECTOR4 v = value.ToDxVec4();
	
	// Set the value:
		
	m_constant_table->SetVector( m_d3d_device , m_constant_handle , &v );
}

//=====================================================================================================================
/// @brief		Sets the constant as a vector 3
///	@param		value - value to set to
/// @pre		Uniform must be valid
//=====================================================================================================================

void ShaderUniform::SetVec3f( const Vec3f & value ) const
{
	// Make sure ok:
	
	ENSURE( IsValid() , "Uniform must be valid" );
	
	// Convert to a directx vector:
	
	D3DXVECTOR4 v = value.ToDxVec4();
	
	// Set the value:
		
	m_constant_table->SetVector( m_d3d_device , m_constant_handle , &v );
}

//=====================================================================================================================
/// @brief		Sets the constant as a vector 4
///	@param		value - value to set to
/// @pre		Uniform must be valid
//=====================================================================================================================

void ShaderUniform::SetVec4f( const Vec4f & value ) const
{
	// Make sure ok:
	
	ENSURE( IsValid() , "Uniform must be valid" );
	
	// Convert to a directx vector:
	
	D3DXVECTOR4 v = value.ToDxVec4();
	
	// Set the value:
		
	m_constant_table->SetVector( m_d3d_device , m_constant_handle , &v );	
}

//=====================================================================================================================
///	@brief		Sets the constant as a boolean
///	@param		value - value to set to
/// @pre		Uniform must be valid
//=====================================================================================================================

void ShaderUniform::SetBool( bool value ) const
{
	// Make sure ok:
	
	ENSURE( IsValid() , "Uniform must be valid" );

	// Set

	m_constant_table->SetBool( m_d3d_device , m_constant_handle , value );
}

//=====================================================================================================================
///	@brief		Sets the constant as a integer
///	@param		value - value to set to
/// @pre		Uniform must be valid
//=====================================================================================================================

void ShaderUniform::SetInt( int value ) const
{
	// Make sure ok:
	
	ENSURE( IsValid() , "Uniform must be valid" );
	
	// Set:

	m_constant_table->SetInt( m_d3d_device , m_constant_handle , value );
}

//=====================================================================================================================
///	@brief		Sets the constant as a 4x4 matrix
///	@param		value - value to set to
/// @pre		Uniform must be valid
//=====================================================================================================================

void ShaderUniform::SetMatrix( const Matrix4f & value ) const
{
	// Make sure ok:
	
	ENSURE( IsValid() , "Uniform must be valid" );
	
	// Convert to DX matrix:

	D3DXMATRIX m = value.ToDxMatrix();

	// Set:

	m_constant_table->SetMatrix( m_d3d_device , m_constant_handle , &m );
}

//=====================================================================================================================
///	@brief		If this is a texture uniform this function returns which sampler unit the texture should be bound to
/// @returns	Unit this texture variable is bound to
/// @pre		Uniform must be valid
//=====================================================================================================================

int ShaderUniform::GetSamplerUnit() const
{
	// Make sure ok:
	
	ENSURE( IsValid() , "Uniform must be valid" );
	
	// Return if success:

	return m_constant_table->GetSamplerIndex( m_constant_handle );
}

//=====================================================================================================================
/// @brief		Binds the given texture to the texture unit of the shader uniform
/// @param		t - texture unit to bind to the uniform texture variable
/// @pre		Uniform must be valid
/// @pre		Texture must be valid
//=====================================================================================================================

void ShaderUniform::BindTexture( Texture & t )
{
	// Make sure ok:
	
	ENSURE( IsValid()	, "Uniform must be valid" );
	ENSURE( t.IsValid()	, "Texture must be valid" );
	
	// Do the apply:
	
	t.Apply( GetSamplerUnit() );
}