//////////////////////////////////////////////////////////////////////////////
//
// Asura_Chunk_ResourceFile_PC_MRPShaders.cpp
//
// Copyright (c) 2007 Rebellion. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////

/******************************************************************************/
/* Included Files                                                             */
/******************************************************************************/

#include "Core/Asura.h"

#if defined ASURA_USE_MATERIAL_RESPONSE_SYSTEM

#include "ChunkSystem/Asura_Chunk_ResourceFile_PC_MRPShaders.h"

// Asura Platform-independent
#include "ChunkSystem/Asura_Chunk_Stream.h"
#include "Debugging/Asura_Assert.h"
#include "Debugging/Asura_Logfile.h"

// Asura PC
#include "Rendering/Asura_Direct3D_MaterialResponse.h"
#if defined(ASURA_USE_MATERIAL_RESPONSE_SYSTEM) && (((ASURA_LOAD_EFFECT_FILES == 1) && !defined( ASURA_NO_LOAD_EFFECTS_FILES_FROM_FILES)))
#include "Rendering/Asura_Direct3D_MRPShaderCompile.h"
#endif
/******************************************************************************/
/* Asura_Chunk_ResourceFile_PC_MRPShaders Class Functions                     */
/******************************************************************************/

////////////////////////////////////////////////////////////////////////////////
//
// Asura_Chunk_ResourceFile_PC_MRPShaders::Process()
//
// Processes a chunk containing material response pixel shaders for PC
//
////////////////////////////////////////////////////////////////////////////////

bool Asura_Chunk_ResourceFile_PC_MRPShaders::Process( const char* szFilename,
													  Asura_Chunk_Stream& xStream )
{
	Asura_Assertf( Version == iCURRENT_VERSION,
		("Invalid version number (%d) for MRP shaders chunk. Re-export needed.", Version) );

	if( Version != iCURRENT_VERSION ) return false;

	u_int uNumShaders;
	xStream >> uNumShaders;

	for( u_int u = 0; u < uNumShaders; ++u )
	{
		Asura_Hash_ID uShaderHash;
		xStream >> uShaderHash;

		u_int uShaderSize;
		xStream >> uShaderSize;

		Asura_Assertf( uShaderSize > 0, ("Material response pixel shader 0x%X has zero size.",
			uShaderHash) );

		if( uShaderSize > 0 )
		{
			void* const pShaderData = Asura_Allocate( uShaderSize );
			if( pShaderData )
			{

				xStream.Read( uShaderSize, pShaderData );

				//const u_int uNumSamplers = Asura_MaterialResponse::NUMBER_OF_TEXTURE_SLOTS * 2;
				//u_int auSamplers[ uNumSamplers ];
				//xStream.Read( uNumSamplers, auSamplers );

#if defined(ASURA_USE_MATERIAL_RESPONSE_SYSTEM) && (((ASURA_LOAD_EFFECT_FILES == 1) && !defined( ASURA_NO_LOAD_EFFECTS_FILES_FROM_FILES)))
				if (Asura_Direct3D_MRPShaderCompile::GetLoadShadersFromFile())
				{
					void* pvShaderData2;
					u_int uShaderSize;

					Asura_Direct3D_MRPShaderCompile::AddMRPShader( uShaderHash, &pvShaderData2, &uShaderSize);
					Asura_Direct3D_MaterialResponse::AddPixelShader( uShaderHash,
						pvShaderData2 ? pvShaderData2 : pShaderData/*, auSamplers*/ );

				} 
				else 
#endif
				{
					Asura_Direct3D_MaterialResponse::AddPixelShader( uShaderHash,
						pShaderData/*, auSamplers*/ );
				}
				Asura_Deallocate( pShaderData );
			}
			else
			{
				Asura_Assert( false, "Failed to allocate memory for pixel shader." );
				xStream.Skip( uShaderSize );
			}
		}
	}

	Asura_OutputToDebugger_Retail(( "Loaded %u shaders from an MRPS chunk.\n", uNumShaders ));

	return true;
}

#endif // defined ASURA_USE_MATERIAL_RESPONSE_SYSTEM
