#include "BsRasterizerState.h"
#include "BsRenderStateManager.h"
#include "BsRenderAPI.h"
#include "BsRasterizerStateRTTI.h"
#include "BsResources.h"

namespace BansheeEngine
{
	bool RASTERIZER_STATE_DESC::operator == (const RASTERIZER_STATE_DESC& rhs) const
	{
		return polygonMode == rhs.polygonMode &&
			cullMode == rhs.cullMode &&
			depthBias == rhs.depthBias &&
			depthBiasClamp == rhs.depthBiasClamp &&
			slopeScaledDepthBias == rhs.slopeScaledDepthBias &&
			depthClipEnable == rhs.depthClipEnable &&
			scissorEnable == rhs.scissorEnable &&
			multisampleEnable == rhs.multisampleEnable &&
			antialiasedLineEnable == rhs.antialiasedLineEnable;
	}

	RasterizerProperties::RasterizerProperties(const RASTERIZER_STATE_DESC& desc)
		:mData(desc), mHash(RasterizerState::generateHash(desc))
	{ }

	RasterizerStateCore::RasterizerStateCore(const RASTERIZER_STATE_DESC& desc, UINT32 id)
		: mProperties(desc), mId(id)
	{

	}

	RasterizerStateCore::~RasterizerStateCore()
	{

	}

	void RasterizerStateCore::initialize()
	{
		// Since we cache states it's possible this object was already initialized
		// (i.e. multiple sim-states can share a single core-state)
		if (isInitialized())
			return;

		createInternal();
		CoreObjectCore::initialize();
	}

	const RasterizerProperties& RasterizerStateCore::getProperties() const
	{
		return mProperties;
	}

	const SPtr<RasterizerStateCore>& RasterizerStateCore::getDefault()
	{
		return RenderStateCoreManager::instance().getDefaultRasterizerState();
	}

	RasterizerState::RasterizerState(const RASTERIZER_STATE_DESC& desc)
		: mProperties(desc), mId(0)
	{

	}

	RasterizerState::~RasterizerState()
	{

	}

	SPtr<RasterizerStateCore> RasterizerState::getCore() const
	{
		return std::static_pointer_cast<RasterizerStateCore>(mCoreSpecific);
	}

	SPtr<CoreObjectCore> RasterizerState::createCore() const
	{
		SPtr<RasterizerStateCore> core = RenderStateCoreManager::instance()._createRasterizerState(mProperties.mData);
		mId = core->getId(); // Accessing core from sim thread is okay here since core ID is immutable

		return core;
	}

	const RasterizerProperties& RasterizerState::getProperties() const
	{
		return mProperties;
	}

	const RasterizerStatePtr& RasterizerState::getDefault()
	{
		return RenderStateManager::instance().getDefaultRasterizerState();
	}

	RasterizerStatePtr RasterizerState::create(const RASTERIZER_STATE_DESC& desc)
	{
		return RenderStateManager::instance().createRasterizerState(desc);
	}

	UINT64 RasterizerState::generateHash(const RASTERIZER_STATE_DESC& desc)
	{
		size_t hash = 0;
		hash_combine(hash, (UINT32)desc.polygonMode);
		hash_combine(hash, (UINT32)desc.cullMode);
		hash_combine(hash, desc.depthBias);
		hash_combine(hash, desc.depthBiasClamp);
		hash_combine(hash, desc.slopeScaledDepthBias);
		hash_combine(hash, desc.depthClipEnable);
		hash_combine(hash, desc.scissorEnable);
		hash_combine(hash, desc.multisampleEnable);
		hash_combine(hash, desc.antialiasedLineEnable);

		return (UINT64)hash;
	}

	/************************************************************************/
	/* 								RTTI		                     		*/
	/************************************************************************/

	RTTITypeBase* RasterizerState::getRTTIStatic()
	{
		return RasterizerStateRTTI::instance();
	}

	RTTITypeBase* RasterizerState::getRTTI() const
	{
		return RasterizerState::getRTTIStatic();
	}
}