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


		#include "gpu/gpu_shader_compiler.h"
		#include "gpu/gpu_renderer.h"
		#include "log/file_log.h"
		#include "log/log.h"

		using namespace GS;
		using namespace GPU;


//------------------------------------------------------------------------------
bool		IShaderCompiler::Compile(const Core::Shader &shader, Shader &gpu_shader, const char *binary_cache_id)
{
	if	(!shader.name.IsEmpty())
		__LOG_V__ << "Creating shader '" << shader.name << "'...\n";
	gpu_shader.name = shader.name;

	// Compile shader objects.
	String vertex_source, pixel_source;
	if	(!renderer.TranslateShader(shader, vertex_source, pixel_source))
		return false;

	gpu_shader.vertex = renderer.NewGPUShaderObject();
	gpu_shader.pixel = renderer.NewGPUShaderObject();
	if	(gpu_shader.vertex.IsNull() || gpu_shader.pixel.IsNull())
		return false;

	String vertex_error, pixel_error;
	if	(
			!CompileObject(vertex_source, *gpu_shader.vertex, ShaderObject::Vertex, shader.name, &vertex_error) ||
			!CompileObject(pixel_source, *gpu_shader.pixel, ShaderObject::Pixel, shader.name, &pixel_error)
		)
	{
		__LOG_E__ << "Shader '" << shader.name << "' failed to compile.\n";

//#ifdef	_DEBUG
		FileLog file_log("c:/shader_error.log");
		file_log << "--------------------------------------------------------------------------------\n";
		file_log << "Failed to compile shader '" << shader.name << "'.\n\n";
		file_log << "Vertex error:" << vertex_error << "\n\n";
		file_log << "Pixel error:" << pixel_error << "\n\n";
		file_log << "--------------------------------------------------------------------------------\n";
		file_log << "\n";
		file_log << "VERTEX:\n";
		file_log << "\n";
		file_log << vertex_source.NormalizedEOL(String::EOLUnix);
		file_log << "\n";
		file_log << "PIXEL:\n";
		file_log << "\n";
		file_log << pixel_source.NormalizedEOL(String::EOLUnix);
		file_log << "\n";
		file_log << "--------------------------------------------------------------------------------\n";
		file_log << "\n";
//#endif
		return false;
	}

	return Link(shader, gpu_shader);
}
//------------------------------------------------------------------------------
