/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */

		#include "core/shader_tree.h"
		#include "core/shader_block.h"

		using namespace GS::Core;


//------------------------------------------------------------------------------
int					ShaderTree::GetSinkCompatibility(ShaderSinkType sink) const
{
	switch (sink)
	{
		case SinkNormal:		return ShaderInput::Vector3;
		case SinkDiffuse:		return ShaderInput::Vector4;
		case SinkModulate:		return ShaderInput::Float;
		case SinkSpecular:		return ShaderInput::Vector4;
		case SinkGlossiness:	return ShaderInput::Float;
		case SinkConstant:		return ShaderInput::Vector4;
		case SinkOpacity:		return ShaderInput::Float;
		case SinkReflection:	return ShaderInput::Float;
	}
	return ShaderInput::NoData;
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
void				ShaderTree::GatherBlockList(GS::List <ShaderBlock *> &block_list, ShaderBlock *root)
{
	if	(!root)
		return;

	// Gather all children.
	for (uint n = 0; n < root->GetInputCount(); ++n)
		GatherBlockList(block_list, root->GetInput(n));

	// Add self.
	if	(!block_list.Find(root))
		block_list.Append(root);
}

void				ShaderTree::Free()
{
	// Gather all blocks in tree.
	List <ShaderBlock *> block_list;
	for (int n = 0; n < SinkInvalid; ++n)
		GatherBlockList(block_list, sink[n]);

	// Disconnect all sinks.
	for (int n = 0; n < SinkInvalid; ++n)
		sink[n] = NULL;

	// Delete all blocks.
	ListDeleteAllPtr(ShaderBlock *, block_list)
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
ShaderTree::ShaderTree()
{
	for (int n = 0; n < SinkInvalid; ++n)
		sink[n] = 0;
}
ShaderTree::~ShaderTree()
{	Free();	}
//------------------------------------------------------------------------------
