/*
 * This source file is part of KEOS (Free 3D Engine)
 * For the latest info, see http://www.keosengine.org/
 * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
 * http://www.gnu.org/copyleft/lesser.txt.
 *
 */
#include "KeosShadersLoader.h"
#include "KeosRenderSystem.h"
#include "KeosException.h"

namespace Keos
{
	//=======================================================================
    // CShadersLoader implementation
    //=======================================================================

    //-----------------------------------------------------------------------
	CShadersLoader::CShadersLoader(TShaderType Type) :
	m_Type(Type)
	{
		// Callback error
		cgSetErrorCallback(&OnError);

		// Context creation
		m_Context = cgCreateContext();
	}

	//-----------------------------------------------------------------------
	CShadersLoader::~CShadersLoader()
	{
		// @todo : cette destruction bug sous OpenGL, impossible de savoir pourquoi

		// Context destruction
		if (m_Context)
			cgDestroyContext(m_Context);
	}

	//-----------------------------------------------------------------------
	IShaderBase* CShadersLoader::LoadFromFile(const String& strFilename)
	{
		if (m_Type == SHADER_VERTEX && !Renderer->GetCapabilities()->HasCapability(CAP_VERTEX_PROGRAM))
			LOADINGFAILED_EXCEPT(strFilename, "Vertex programs (shaders) not supported !","CShadersLoader::LoadFromFile");

		if (m_Type == SHADER_PIXEL && !Renderer->GetCapabilities()->HasCapability(CAP_FRAGMENT_PROGRAM))
			LOADINGFAILED_EXCEPT(strFilename, "Fragment programs (pixel shaders) not supported !","CShadersLoader::LoadFromFile");

		// Load and compilation of the shader
		CGprogram Program;
		try
		{
			Program = cgCreateProgramFromFile(m_Context,
											CG_SOURCE,
											strFilename.c_str(),
											Renderer->GetShaderProfile(m_Type),
											"main",
											const_cast<const char**>(Renderer->GetShaderOptions(m_Type)));
		}
		catch (CException& E)
		{
            LOADINGFAILED_EXCEPT(strFilename, E.what() + std::string("\n") + cgGetLastListing(m_Context),"CShadersLoader::LoadFromFile");
		}

		return Renderer->CreateShader(Program, m_Type);
	}

	//-----------------------------------------------------------------------
	void CShadersLoader::OnError()
	{
		throw CException(cgGetErrorString(cgGetError()));
	}


} // namespace Keos
