//////////////////////////////////////////////////////////////////////////////
//
// AVP_Platform_Stencil_Setup.cpp
//
// Author : Andrew Southgate / Ben Weston
// Date   : 2/5/2008 
//
// Copyright (c) 2008 Rebellion. All rights reserved.
//
// JL - 01 April 2009 - Edited PC version used in PC-CLI, PC-CLI does not use
// directX.
//
//////////////////////////////////////////////////////////////////////////////


// I N C L U D E S ///////////////////////////////////////////////////////////

#include "Core/Asura.h"

#include "Core/Asura_MaterialResponse_System.h"

#include "Rendering/Asura_Render.h"

#include "Rendering/Asura_Direct3D.h"
#include "FSFX/AVP_FSFX.h"


#include "Rendering/AVP_Platform_Stencil_Setup.h"


//
// Defines
//

// mask of all bits that could ever be used by a material context
#define AVP_MATERIAL_CONTEXT_STENCIL_MASK	(AVP_STENCIL_MASK_VISION)

//
// Variables
//

// allow stencil test to be performed on any bits that aren't used by this function
D3DCMPFUNC	AVP_PlatformStencilSetup::s_uFunction = D3DCMP_ALWAYS;
u_int 		AVP_PlatformStencilSetup::s_uTestRef = 0;
u_int 		AVP_PlatformStencilSetup::s_uTestMask = 0;

//
// Functions
//


//
// AVP_PlatformStencilSetup::SetTestFunction
//
// Allow materials to both write to & test against the stencil buffer. e.g. for predator cloak transition whilst in heat vision mode (PS3).
// These parameters will be combined with the ones used by the material "project context" (which we use as stencil flags)
// uRef & uMask must use different bits to those defined in AVP_MATERIAL_CONTEXT_STENCIL_MASK
//
void AVP_PlatformStencilSetup::SetTestFunction( D3DCMPFUNC uFunction, u_int uRef, u_int uMask )
{
	Asura_Assertf((uMask & AVP_MATERIAL_CONTEXT_STENCIL_MASK) == 0, ("AVP_PlatformStencilSetup::SetTestFunction - Error: mask overlaps bits used by material context\n Requested mask = %02x, material mask = %02x\n", uMask, AVP_MATERIAL_CONTEXT_STENCIL_MASK));
	Asura_Assertf((uRef & AVP_MATERIAL_CONTEXT_STENCIL_MASK) == 0, ("AVP_PlatformStencilSetup::SetTestFunction - Warning: ref value overlaps material context mask\n Requested ref = %02x, material mask = %02x\n", uRef, AVP_MATERIAL_CONTEXT_STENCIL_MASK));

	s_uTestMask = uMask & ~AVP_MATERIAL_CONTEXT_STENCIL_MASK;  //clear the bits just in case
	s_uTestRef = uRef & ~AVP_MATERIAL_CONTEXT_STENCIL_MASK;
	s_uFunction = uFunction;
}


//
// AVP_PlatformStencilSetup::ResetTestFunction
//
// Restore the default material context function (always write)
//
void AVP_PlatformStencilSetup::ResetTestFunction( void )
{
	s_uFunction = D3DCMP_ALWAYS;
	s_uTestRef = 0;
	s_uTestMask = 0;
}


//
// AVP_PlatformStencilSetup::MaterialContextCallback
//
// When the material has a "context" value, write it to the stencil buffer.
//
void AVP_PlatformStencilSetup::MaterialContextCallback(u_int uPrimaryContext, u_int uSecondaryContext) {}
