//
// Asura_RenderStates.cpp
//
// Created by Kevin Lea - 05/10/2001 16:39:27
// 
// Copyright (C) 2001 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"

// Contains Direct3D specific code for changing the render state
#include "Rendering/Asura_Render.h"
#include "Rendering/Asura_Direct3D.h" 
#include "Rendering/Asura_Direct3D_Shaders.h"
#include "Rendering/Asura_RenderStates.h"
#if 01


void Asura_Render_States::SetTextureIndex(int t, int i)
{

#if 1
	TextureIndex[t]=i;

	if ( i==ASURA_TEXTURE_NONE ) 
	{
		SCOPEDPIXEVENT((0xff222222, L"Cleared texture in stage %i", t))
		//For untextured polygons , or missing textures , use a default white texture instead.
		//See Asura_Direct3D::CreateUntexturedTexture(). RAR 10/03/2005
		Asura_Direct3D::D3DDevice->SetTexture( t, Asura_Direct3D::GetUntexturedTexture() );
	}
	else 
	{
		// GetDirect3DTextureByIndex() automatically returns the UntexturedTexture if the texture is not loaded.
		SCOPEDPIXEVENT((0xff222222, L"Set texture in stage %i", t))
		Asura_Direct3D::D3DDevice->SetTexture( t, Asura_Direct3D::GetDirect3DTextureByIndex(i) );
	}
#endif
}

void Asura_Render_States::SetWireFrameMode(bool b)
{
	SCOPEDPIXEVENT((0xff222222, L"Switched to %s rendering", b?L"wireframe":L"solid"))
	WireFrameMode=b;

	if (b)
	{
		Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
	}
	else
	{
		Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
	}
}


void Asura_Render_States::SetDitheringMode(bool b)
{
	SCOPEDPIXEVENT((0xff222222, L"%s dithering", b?L"Enabled":L"Disabled"))
	DitheringMode=b;

	Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_DITHERENABLE, b);
}



void Asura_Render_States::SetZBufferEnabled(bool b)
{
	SCOPEDPIXEVENT((0xff222222, L"%s Z buffer testing", b?L"Enabled":L"Disabled"))
	ZBufferEnabled=b;

	if (b)
	{
		Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
	}
	else													  
	{
		Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
	}
}

void Asura_Render_States::SetStencilEnabled(bool b)
{
	SCOPEDPIXEVENT((0xff222222, L"%s Stencil", b?L"Enable":L"Disable"))
	StencilEnabled=b;

	if (b)
	{
		Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
	}
	else													  
	{
		Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
	}
}



void Asura_Render_States::SetZBufferCompareMode(enum ASURA_ZBUFFERCMP_ID mode)
{
	ZBufferCompareMode = mode;

	switch (mode)
	{
		case ASURA_ZBUFFERCMP_LESSEQUAL:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set Z compare mode to LESSEQUAL"));
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
			break;		
		}
		case ASURA_ZBUFFERCMP_ALWAYS:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set Z compare mode to ALWAYS"));
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
			break;		
		}
		case ASURA_ZBUFFERCMP_EQUALS:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set Z compare mode to EQUALS"));
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_EQUAL);
			break;
		}
		case ASURA_ZBUFFERCMP_GREATER:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set Z compare mode to GREATER"));
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_GREATER);
			break;
		}
	}
}

void Asura_Render_States::SetZBufferWriteEnabled(bool b)
{
	SCOPEDPIXEVENT((0xff222222, L"%s Z buffer writes", b?L"Enabled":L"Disabled"))

	ZBufferWriteEnabled=b;

	Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_ZWRITEENABLE, b);
}

void Asura_Render_States::SetAlphaBlending(bool b)
{
	SCOPEDPIXEVENT((0xff222222, L"%s alpha blending", b?L"Enabled":L"Disabled"))
	AlphaBlendingEnabled = b;
	Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, b);
}

void Asura_Render_States::SetTranslucencyMode(enum ASURA_TRANSLUCENCY_MODE mode)
{
	TranslucencyMode=mode;

	switch(TranslucencyMode)
	{
	 	case ASURA_TRANSLUCENCY_OFF:
		{
			SCOPEDPIXEVENT((0xff222222, L"Turned translucency off"));
			if (AlphaBlendingEnabled) SetAlphaBlending(false);

			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_ONE);
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ZERO);
			break;
		}
	 	case ASURA_TRANSLUCENCY_NORMAL:
		{
			SCOPEDPIXEVENT((0xff222222, L"Turned translucency on, NORMAL mode"));
			if (!AlphaBlendingEnabled) SetAlphaBlending(true);

			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
			break;
		}
	 	case ASURA_TRANSLUCENCY_COLOUR:
		{
			SCOPEDPIXEVENT((0xff222222, L"Turned translucency on, COLOUR mode"));
			if (!AlphaBlendingEnabled) SetAlphaBlending(true);

			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_DESTCOLOR);
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ZERO);
			break;
		}
		case ASURA_TRANSLUCENCY_DOUBLECOLOUR:
		{
			SCOPEDPIXEVENT((0xff222222, L"Turned translucency on, DOUBLECOLOUR mode"));
			if (!AlphaBlendingEnabled) SetAlphaBlending(true);

			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_DESTCOLOR);
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_SRCCOLOR);//ZERO);
			break;
		}
	 	case ASURA_TRANSLUCENCY_INVCOLOUR:
		{
			SCOPEDPIXEVENT((0xff222222, L"Turned translucency on, INVCOLOUR mode"));
			if (!AlphaBlendingEnabled) SetAlphaBlending(true);

			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_ZERO);
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCCOLOR);
			break;
		}
  		case ASURA_TRANSLUCENCY_GLOWING:
		{
			SCOPEDPIXEVENT((0xff222222, L"Turned translucency on, GLOWING mode"));
			if (!AlphaBlendingEnabled) SetAlphaBlending(true);

			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE);
			break;
		}
  		case ASURA_TRANSLUCENCY_DARKENINGCOLOUR:
		{
			SCOPEDPIXEVENT((0xff222222, L"Turned translucency on, DARKENINGCOLOUR mode"));
			if (!AlphaBlendingEnabled) SetAlphaBlending(true);

			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_INVDESTCOLOR);
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ZERO);
			break;
		}
		case ASURA_TRANSLUCENCY_JUSTSETZ:
		{
			SCOPEDPIXEVENT((0xff222222, L"Turned translucency on, JUSTSETZ mode"));
			if (!AlphaBlendingEnabled) SetAlphaBlending(true);

			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_ZERO);
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE);
			break;
		}
		case ASURA_TRANSLUCENCY_ALPHACOMPARE:
		{
			SCOPEDPIXEVENT((0xff222222, L"Turned translucency on, ALPHACOMPARE mode"));
			if (!AlphaBlendingEnabled) SetAlphaBlending(true);

			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_DESTALPHA);
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ZERO);
			break;
		}
		case ASURA_TRANSLUCENCY_UGH:
		{
			SCOPEDPIXEVENT((0xff222222, L"Turned translucency on, UGH mode"));
			if (!AlphaBlendingEnabled) SetAlphaBlending(true);

			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_DESTCOLOR);
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE);
			break;
		}
		case ASURA_TRANSLUCENCY_ADD:
		{
			SCOPEDPIXEVENT((0xff222222, L"Turned translucency on, ADD mode"));
			if (!AlphaBlendingEnabled) SetAlphaBlending(true);

			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_ONE);
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE);
			break;
		}
		default:
			break;
	}
}

void Asura_Render_States::SetTextureOpMode(enum ASURA_TEXTUREOP_ID mode)
{
	Asura_Assert( ASURA_TEXTUREOP_INVALID != mode, "Not a valid TextureOpMode to ask for");
	
	// Pixel shaders take priority over fixed function processing.
	// If we want to set a texture op, ensure we've disabled pixel shaders
	Asura_Direct3D_Shaders::RequestPixelShader( ASURA_D3D_PS_NONE );

	TextureOpMode = mode;

	switch(mode)
	{
		case ASURA_TEXTUREOP_CUSTOM:
		{
			// DLH - 28 June 2006 15:07:16 - Special case value to allow the programmer to play with the Texture Stage States and not make the cached mode invalid
			break;
		}
		default:
		case ASURA_TEXTUREOP_SINGLE_PASS:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to SINGLE_PASS"));
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_MODULATE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,   D3DTOP_DISABLE);
			break;
		}
		case ASURA_TEXTUREOP_COLOUR:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to COLOUR"));
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG2);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG2);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,   D3DTOP_DISABLE);
			break;
		}
		case ASURA_TEXTUREOP_SINGLE_PASS_2X:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to SINGLE_PASS_2X"));
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE2X);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_MODULATE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,   D3DTOP_DISABLE);
			break;
		}
		case ASURA_TEXTUREOP_WOLFVISION:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to WOLFVISION"));
		#if 1
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_DOTPRODUCT3);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);//|D3DTA_COMPLEMENT  );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_TEXTURE);

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_MODULATE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

			//Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,   D3DTOP_DISABLE);

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_ADDSIGNED);
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG1, D3DTA_CURRENT|D3DTA_COMPLEMENT);
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG2, D3DTA_DIFFUSE);
		#else
			// KJL 21/11/00 15:29:40 - add smooth on geforce!
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_ADDSMOOTH);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);//|D3DTA_COMPLEMENT  );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE);//|D3DTA_COMPLEMENT );

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_MODULATE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_DOTPRODUCT3);
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG2, D3DTA_CURRENT );
		#endif

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,  D3DTOP_MODULATE);
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );	

			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_COLOROP,   D3DTOP_DISABLE);
			break;
		}
		case ASURA_TEXTUREOP_MODULATE:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to MODULATE"));
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_MODULATE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
			
			
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_MODULATE);
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG2, D3DTA_TEXTURE);

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );	

			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_COLOROP,   D3DTOP_DISABLE);
	
			break;
		}

		case ASURA_TEXTUREOP_PAINTERLY:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to PAINTERLY"));
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_MODULATE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
			
			
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_MODULATE);
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG2, D3DTA_TEXTURE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );	

			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_COLOROP, D3DTOP_DISABLE);
			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_COLORARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_COLORARG2, D3DTA_CURRENT );

			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_ALPHAARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
			
			Asura_Direct3D::D3DDevice->SetTextureStageState(3,D3DTSS_COLOROP, D3DTOP_DISABLE);	
			break;
		}

		case ASURA_TEXTUREOP_TOON:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to TOON"));
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_MODULATE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
			
			
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_MODULATE);
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG2, D3DTA_TEXTURE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,  D3DTOP_MODULATE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );	

			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_COLOROP, D3DTOP_DISABLE);
			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_COLORARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_COLORARG2, D3DTA_CURRENT );

			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_ALPHAOP,  D3DTOP_MODULATE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_ALPHAARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(3,D3DTSS_COLOROP, D3DTOP_DISABLE);
			break;
		}
		case ASURA_TEXTUREOP_SELFILLUMINATE:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to SELF_ILLUMINATE"));
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE2X);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,	D3DTOP_MODULATEALPHA_ADDCOLOR);
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG1, D3DTA_CURRENT);
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG2, D3DTA_TEXTURE);

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
				
			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_COLOROP, D3DTOP_DISABLE);
	

			break;
		}
		case ASURA_TEXTUREOP_SPECULAR:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to SPECULAR"));
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,	D3DTOP_SELECTARG1);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_TEXTURE );
						
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,	D3DTOP_MODULATE);
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,	D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );	
			
			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_COLOROP,   D3DTOP_DISABLE);
			break;

		}
		case ASURA_TEXTUREOP_OPAQUE:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to OPAQUE"));
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_TEXTURE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,   D3DTOP_DISABLE);

			break;
		}

		case ASURA_TEXTUREOP_LIGHTPASS:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to LIGHTPASS"));
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,	D3DTOP_MODULATE);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
						
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,	D3DTOP_MODULATE);
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG2, D3DTA_TEXTURE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,	D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );	
			
			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_COLOROP,   D3DTOP_DISABLE);
			break;
		}
		case ASURA_TEXTUREOP_PLAINTEXTURE:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to PLAINTEXTURE"));
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
			#if 0
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,   D3DTOP_DISABLE);
			#else

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,  D3DTOP_MODULATE2X);
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG2, D3DTA_TEXTURE);

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );	

			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_COLOROP,   D3DTOP_DISABLE);
			#endif
			break;
		}
		case ASURA_TEXTUREOP_BRIGHTGREEN:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to BRIGHTGREEN"));
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TFACTOR );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG2 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,   D3DTOP_DISABLE);

			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_TEXTUREFACTOR,0x50ff80);

			break;
		}
		case ASURA_TEXTUREOP_REFLECTIVE:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to REFLECTIVE"));
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TFACTOR );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,	D3DTOP_BLENDCURRENTALPHA);
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG1, D3DTA_TEXTURE);
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLORARG2, D3DTA_CURRENT);

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
	
			Asura_Direct3D::D3DDevice->SetTextureStageState(2,D3DTSS_COLOROP, D3DTOP_DISABLE);
			
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_TEXTUREFACTOR,0xffffffff);

			break;
		}
		case ASURA_TEXTUREOP_JUSTALPHA:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to JUSTALPHA"));
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG2);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,   D3DTOP_DISABLE);

			break;
		}
        
		case ASURA_TEXTUREOP_COPYALPHA:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to COPYALPHA"));
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE | D3DTA_ALPHAREPLICATE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE);

			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,  D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,   D3DTOP_DISABLE);

			break;
		}
        
        case ASURA_TEXTUREOP_WHITE:
        {
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to WHITE"));
		    Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
		    Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TFACTOR     );
		    Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE     );

		    Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG2 );
		    Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE     );
		    Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE     );

		    Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_DISABLE    );

		    Asura_Direct3D::D3DDevice->SetRenderState( D3DRS_TEXTUREFACTOR, 0xffffff );
            break;
        }

		case ASURA_TEXTUREOP_DUALTEXTURE:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to DUALTEXTURE"));
			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP,	D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1,	D3DTA_TEXTURE );

			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,	D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1,	D3DTA_TEXTURE );

			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP,	D3DTOP_BLENDDIFFUSEALPHA);
			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_COLORARG1,	D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_COLORARG2,	D3DTA_TEXTURE );

			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,	D3DTOP_BLENDDIFFUSEALPHA );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG1,	D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG2,	D3DTA_TEXTURE );

			Asura_Direct3D::D3DDevice->SetTextureStageState( 2, D3DTSS_COLOROP,	D3DTOP_DISABLE );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 2, D3DTSS_ALPHAOP,	D3DTOP_DISABLE );
			break;
		}

		case ASURA_TEXTUREOP_LERP:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set texture-op mode to LERP"));
			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP,	D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1,	D3DTA_TEXTURE );

			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,	D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1,	D3DTA_TEXTURE );

			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP,	D3DTOP_LERP);
			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_COLORARG0,	D3DTA_TFACTOR );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_COLORARG1,	D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_COLORARG2,	D3DTA_TEXTURE );

			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,	D3DTOP_LERP );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG0,	D3DTA_TFACTOR );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG1,	D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG2,	D3DTA_TEXTURE );
		
			Asura_Direct3D::D3DDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_MODULATE);
			Asura_Direct3D::D3DDevice->SetTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 2, D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState( 2, D3DTSS_ALPHAOP,  D3DTOP_MODULATE );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 2, D3DTSS_ALPHAARG1, D3DTA_CURRENT );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 2, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState( 3, D3DTSS_COLOROP,	D3DTOP_DISABLE );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 3, D3DTSS_ALPHAOP,	D3DTOP_DISABLE );
			break;
		}

		case ASURA_TEXTUREOP_VIEWTEXTURECOLOUR:
		{
			SCOPEDPIXEVENT(( 0XFF222222, L"Set texture-op mode to VIEWTEXTURECOLOUR" ));

			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );

			break;
		}

		case ASURA_TEXTUREOP_VIEWTEXTUREALPHA:
		{
			SCOPEDPIXEVENT(( 0XFF222222, L"Set texture-op mode to VIEWTEXTUREALPHA" ));

			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE | D3DTA_ALPHAREPLICATE );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );

			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
			Asura_Direct3D::D3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );

			break;
		}
	}
}

void Asura_Render_States::SetAlphaTest(bool b)
{
	AlphaTest=b;

	SCOPEDPIXEVENT((0xff222222, L"%s alpha testing", b?L"Enabled":L"Disabled"));

	Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, b);

	Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_ALPHAREF, m_iAlphaTestRef);
	Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);
}

void Asura_Render_States::SetDynamicLights(bool b)
{
	SCOPEDPIXEVENT((0xff222222, L"%s dynamic lights", b?L"Enabled":L"Disabled"));
	DynamicLights = b;

	Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_LIGHTING, b);
}

void Asura_Render_States::SetCullMode(enum ASURA_CULLMODE_ID eMode)
{
	m_eCullMode = eMode;

	switch(eMode)
	{
		default:
		case ASURA_CULLMODE_NONE:
		{
			SCOPEDPIXEVENT((0xff222222, L"Turned off backface culling"));
			Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
			break;
		}

		case ASURA_CULLMODE_CW:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set backface culling to CW faces"));
			if (Asura_Render::ShouldRenderMirrored())
			{
				Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
			}
			else
			{
				Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
			}
			break;
		}

		case ASURA_CULLMODE_CCW:
		{
			SCOPEDPIXEVENT((0xff222222, L"Set backface culling to CCW faces"));
			if (Asura_Render::ShouldRenderMirrored())
			{
				Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
			}
			else
			{
				Asura_Direct3D::D3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
			}

			break;
		}

	}
}

#endif
