deargui-vpl/ref/virtools/Samples/Behaviors/Shader/Sources/ShaderManagerCG.h

378 lines
13 KiB
C++

#ifndef ShaderManagerCG_H
#define ShaderManagerCG_H "$Id:$"
#if defined(MULTIPLESHADERMANAGER)
#include "ShaderManagerInterface.h"
#else
#include "CKBaseManager.h"
#endif
#include "RCKShaderCG.h"
#include "CKRasterizer.h"
#include "ShaderDescriptorCG.h"
#ifdef CompileShader
#undef CompileShader
#endif
namespace VCKGL15Rasterizer
{
class CKGLRasterizerContext;
}
typedef XArray<ShaderDescriptorCG::ExposedParamMeaning*> ExposedParamMeaningArrayCG;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Shader Manager Class
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#if defined(MULTIPLESHADERMANAGER)
class ShaderManagerCG : public ShaderManagerInterface
#else
class ShaderManagerCG : public CKShaderManager
#endif
{
public:
ShaderManagerCG(CKContext *Context);
virtual ~ShaderManagerCG();
//--------------------------------------------------------------
// Methods to implement
//--------------------------------------------------------------
//--- Called to save manager data. return NULL if nothing to save...
virtual CKStateChunk* SaveData(CKFile* SavedFile);
//--- Called to load manager data.
virtual CKERROR LoadData(CKStateChunk *chunk,CKFile* LoadedFile);
//--- Called before the end of a CKContext::ClearAll operation.
virtual CKERROR PreClearAll();
//--- Called at the end of a CKContext::ClearAll operation.
virtual CKERROR PostClearAll();
//--- Called at deletion of a CKContext
virtual CKERROR OnCKEnd();
//--- Called at Play
virtual CKERROR OnCKPlay();
//--- Called at the end of the creation of a CKContext.
virtual CKERROR OnCKInit();
//--- Called at the end of a load operation.
virtual CKERROR PostLoad() { return CK_OK; }
//--- Called at the end of a save operation.
virtual CKERROR PostSave() { return CK_OK; }
virtual CKERROR OnRasterizerEvent(CKRST_EVENTS Event,CKRenderContext* dev);
#if !defined(MULTIPLESHADERMANAGER)
//--- Returns list of functions implemented by the manager.
virtual CKDWORD GetValidFunctionsMask() {
return CKShaderManager::GetValidFunctionsMask() |
CKMANAGER_FUNC_PreClearAll|
CKMANAGER_FUNC_PostClearAll|
CKMANAGER_FUNC_OnCKEnd|
CKMANAGER_FUNC_OnCKPlay|
CKMANAGER_FUNC_OnCKInit|
CKMANAGER_FUNC_PostLoad|
CKMANAGER_FUNC_PostSave|
CKMANAGER_FUNC_OnRasterizerEvent;
}
#endif
//--------------------------------------------------------------
// Unused methods
//--------------------------------------------------------------
// virtual CKERROR SequenceAddedToScene(CKScene *scn,CK_ID *objids,int count) { return CK_OK; }
// virtual CKERROR SequenceRemovedFromScene(CKScene *scn,CK_ID *objids,int count){ return CK_OK; }
// virtual CKERROR OnPostBackToFront(CKRenderContext* dev) { return CK_OK; }
// virtual CKERROR OnPreBackToFront(CKRenderContext* dev) { return CK_OK; }
// virtual CKERROR PreClearAll() { return CK_OK; }
// virtual CKERROR OnPostCopy(CKDependenciesContext& context) { return CK_OK; }
// virtual CKERROR OnPreCopy(CKDependenciesContext& context) { return CK_OK; }
// virtual CKERROR SequenceDeleted(CK_ID *objids,int count) { return CK_OK; }
// virtual CKERROR SequenceToBeDeleted(CK_ID *objids,int count) { return CK_OK; }
// virtual CKERROR OnPostFullScreen(BOOL Going2Fullscreen,CKRenderContext* dev) { return CK_OK; }
// virtual CKERROR OnPreFullScreen(BOOL Going2Fullscreen, CKRenderContext* dev) { return CK_OK; }
// virtual CKERROR PreLoad() { return CK_OK; }
// virtual CKERROR PostLaunchScene(CKScene* OldScene,CKScene* NewScene) { return CK_OK; }
// virtual CKERROR PreLaunchScene(CKScene* OldScene,CKScene* NewScene) { return CK_OK; }
// virtual CKERROR OnCKPause() { return CK_OK; }
// virtual CKERROR OnCKPlay() { return CK_OK; }
// virtual CKERROR PostProcess() { return CK_OK; }
// virtual CKERROR PreProcess() { return CK_OK; }
// virtual CKERROR OnPostRender(CKRenderContext* dev) { return CK_OK; }
// virtual CKERROR OnPreRender(CKRenderContext* dev) { return CK_OK; }
// virtual CKERROR OnCKPostReset() { return CK_OK; }
// virtual CKERROR OnCKReset() { return CK_OK; }
// virtual CKERROR PreSave() { return CK_OK; }
// virtual CKERROR OnPostSpriteRender(CKRenderContext* dev) { return CK_OK; }
protected:
virtual void OnCreateDevice(CKRenderContext* rc);
virtual void OnDestroyDevice(CKRenderContext* rc);
virtual void OnLostDevice(CKRenderContext* rc);
virtual void OnResetDevice(CKRenderContext* rc);
//-----------------------------------------------------------------------//
// Support
//-----------------------------------------------------------------------//
public:
// Is supported only if the driver is Direct X 9.
virtual CKBOOL IsSupported() const;
virtual CKBOOL IsSupportedAndWarnIfNot();
virtual void GetVSPSVersion(float& vs, float& ps) const;
virtual CKBOOL IsPreCleared() const { XASSERT(0); return FALSE; }
//-----------------------------------------------------------------------//
// Shader Compilation & Automatics variables
//-----------------------------------------------------------------------//
public:
virtual CKShader* CreateShader(
const XString* name = NULL,
const XString* text = NULL,
BOOL uniqueName = TRUE );
virtual void DeleteShader(CKShader* fx);
virtual CKShader* CreateShaderFromFile(const CKSTRING filename);
virtual CKShader* CreateShaderFromFiles(const CKSTRING HLSLfilename, const CKSTRING CgFXfilename = NULL);
virtual bool SaveShaderToFile(const XString& filename, CKShader* fx);
virtual bool CompileShader(CKShader* fx, XClassArray<XString> &output);
virtual bool CompileShaderOutput(CKShader* fx, const CKSTRING funcname,
const CKSTRING target, XClassArray<XString> &output, XArray<char>& text);
virtual CKShader* GetShaderByName(const XBaseString& name);
virtual int GetNumShaders() const;
virtual CKShader* GetShader(int pos);
virtual void BeginShaders( CKRenderContext* rc );
virtual void EndShaders( CKRenderContext* rc );
virtual int GetSemanticIndexFromString( XString& iStr );
const XClassArray<XString>& GetSemanticOriginalNames();
virtual void GetSemanticDesc( int iSemIndex, XString*& oSemDesc );
virtual void* GetIncludeManager(){ return 0; }
const XClassArray<XString>& GetAnnotationOriginalNames();
virtual CK_SHADER_MANAGER_TYPE GetCurrentShaderManagerType() const {return CKSM_CGFX;}
virtual int GetTangentSpaceCreationMode() const { return m_TangentSpaceCreationMode; };
virtual void SetTangentSpaceCreationMode( ShaderTSCreationMode iTSCreationMode ){ m_TangentSpaceCreationMode = iTSCreationMode; };
public:
ShaderDescriptorCG::ManagerCG& GetDescriptorManager(){ return m_ShaderDescriptorManager; }
protected:
BOOL _ReadStringFromFile(const XString& filename, XArray<char> &s) const;
BOOL _WriteStringToFile(const XString& filename, const XString &s) const;
BOOL _WinExec(const XString& command);
XString m_FXCCommand;
#if defined(MULTIPLESHADERMANAGER)
friend class ShaderManagerWrapper;
int* m_NextCompID;
#else
int m_NextCompID;
#endif
ShaderDescriptorCG::ManagerCG m_ShaderDescriptorManager;
enum {
ShaderReplacementMode_RenameNew = 0,
ShaderReplacementMode_ReplaceOldByNew,
ShaderReplacementMode_KeepOldAndDontUseNew
};
int m_ReplacementMode;
enum {
ShaderNMOSavingMode_SaveAllShaders = 0,
ShaderNMOSavingMode_SaveOnlyUsedShaders,
ShaderNMOSavingMode_DontSaveAnyShader
};
int m_NMOSavingMode;
int m_TangentSpaceCreationMode;
public:
XArray<CGcontext> m_CgContext;
//-----------------------------------------------------------------------//
// Default Shader
//-----------------------------------------------------------------------//
public:
virtual CKShader* GetDefaultShader();
virtual CKShader* GetDefaultShaderI();
virtual CKShader* GetShadowShader();
//-----------------------------------------------------------------------//
// Exposed Parameters Management
//
// m_hashParamIndex: a hash table for retrieving the index of store CKParams
// m_paramLinker: array of ExposedLoclalParamsPerRC indexed per RC index.
// D3DHandleArray:
//-----------------------------------------------------------------------//
public:
XArray<CKRenderContext*> m_rcList;
XHashTable<int, CKParameterLocal*> m_hashParamIndex;
XClassArray<ExposedParamMeaningArrayCG> m_paramMeaningLinker;
public:
void _AddRCForExposedParameters();
void _RemoveRCForExposedParameters( int rcIndex );
void _UnregisterExposedParameter( CKParameterLocal* p );
//--- Meaning related
void _RegisterExposedParameterMeaning( CKParameterLocal* p, ShaderDescriptorCG::ExposedParamMeaning* paramMeaning, int rcIndex );
ShaderDescriptorCG::ExposedParamMeaning* _GetRegisteredParamMeaning( CKParameterLocal* p, int rcIndex );
/****************************************************************
Summary: Gets the render context index.
Arguments:
iRc: the render context
Return Value:
The render context index or -1 if not found.
Remarks:
Gets the render context index among the whole list of render
context.
See Also:
*******************************************************************/
int _GetRenderContextIndex( CKRenderContext* rc );
/*******************************************************************
Summary: Called when a RenderContext gets created or destroyed
to update the Shader Manager's inner list of render contexts
*******************************************************************/
virtual void OnRenderContextCreated( CKRenderContext* rc );
virtual void OnRenderContextDestroyed( CKRenderContext* rc );
///////////////////////////////
// Cg program caching system //
///////////////////////////////
/** Enable a program
* If a set of constant is provided in 'cstStore', they will be updated as well
*/
void EnableProgram(CGprogram prog);
void EnableProgram(CKRasterizerContext* rc, ShaderConstantStore *cstStore);
// Deactivate any program for the given domain
void DeactivateProgram(CGdomain domain);
void AddPassModifiedRenderState(int passIndex, CachedRenderStateBase *state);
// Flush modified render states
void FlushModifiedRenderStates(int passIndex, CKRasterizerContext* rc);
void ClearModifiedRenderStates();
// Texture stages management : When using a fragment program, we don't care about extra
// sampler that may be bound during the invocation of the shader.
// However, when using the fixed fragment pipeline, we need to unbound unwanted texture before
// rendering.
// Signal the last used target for a texture stage; The texture stage is marked as 'used'
void SignalTextureStageUsed(int stage, GLenum textureTarget);
// Unbind all texture stages that are not signaled as 'used' by the bitfield (nth bit set for nth stage)
// This will usually be called when transitionning from a programmable fragment shader to a fixed fragment shader
// to ensure that no unwanted texture are bound.
// This way, we mimic the behavior of cgResetPassState
void DisableTextureStages(CKRasterizerContext *rc, CKDWORD stagesMask);
////////////////
// CG helpers //
////////////////
// get the number of technique in a cg effect
static int GetCGEffectTechniqueCount(CGeffect effect);
private:
CGprogram m_CurrentCGProgram[NUMBER_OF_CG_DOMAIN];
CGprofile m_CurrentProfile[NUMBER_OF_CG_DOMAIN];
// Enable a program profile for the given domain (CG_PROFILE_UNKNOWN to disable)
void EnableProfile(CGdomain domain, CGprofile profile);
private:
// For each pass of current effect, give the list of render state that need to be reevaluated
// This usually happen when one of the parameter it depends on (1 most of the time)
// has been modified. Such a parameter can be modified after begin pass, so we need to track them here
// so we can update the render state before the draw
struct PerPassModifiedRenderStates
{
XArray<class CachedRenderStateBase *> RenderStates;
};
XClassArray<PerPassModifiedRenderStates> m_PerPassModifiedRenderStates;
private:
int m_UsedTextureStageBits; // For each texture stage, signal if that stage is currently bound
//
GLenum m_TextureStageTargets[32]; // For each bound stage, give the current bound texture target (1D, 2D ...)
public:
struct CGContextToRasterizer
{
CGcontext cgContext;
VCKGL15Rasterizer::CKGLRasterizerContext *glRasterizerContext;
};
static XArray<CGContextToRasterizer> m_CGContextToRasterizer; // linear serach should be ok (1 context in most cases)
// associate a cgContext with its rasterizer
static void RegisterCGContext(CGcontext cgContext, VCKGL15Rasterizer::CKGLRasterizerContext *glRasterizerContext);
// unregister a cg context
static void UnregisterCGContext(CGcontext cgContext);
// get the rasterizer associated with a cg context
static VCKGL15Rasterizer::CKGLRasterizerContext *GetRasterizerFromCGContext(CGcontext cgContext);
};
//-----------------------------------------------------------------------------------------
inline void ShaderManagerCG::AddPassModifiedRenderState(int passIndex, CachedRenderStateBase *state)
{
if (passIndex >= m_PerPassModifiedRenderStates.Size())
{
m_PerPassModifiedRenderStates.Resize(passIndex + 1);
}
m_PerPassModifiedRenderStates[passIndex].RenderStates.PushBack(state);
}
#ifdef _DEBUG
static void errorCallback();
static ShaderManagerCG* theShaderManager;
#endif
#endif