postEffect.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _POST_EFFECT_H_
  23. #define _POST_EFFECT_H_
  24. #ifndef _SIMSET_H_
  25. #include "console/simSet.h"
  26. #endif
  27. #ifndef _MPOINT3_H_
  28. #include "math/mPoint3.h"
  29. #endif
  30. #ifndef _MPOINT2_H_
  31. #include "math/mPoint2.h"
  32. #endif
  33. #ifndef _GFXSHADER_H_
  34. #include "gfx/gfxShader.h"
  35. #endif
  36. #ifndef _GFXSTATEBLOCK_H_
  37. #include "gfx/gfxStateBlock.h"
  38. #endif
  39. #ifndef _GFXTEXTUREHANDLE_H_
  40. #include "gfx/gfxTextureHandle.h"
  41. #endif
  42. #ifndef _GFXTARGET_H_
  43. #include "gfx/gfxTarget.h"
  44. #endif
  45. #ifndef _GFXVERTEXBUFFER_H_
  46. #include "gfx/gfxVertexBuffer.h"
  47. #endif
  48. #ifndef _POSTEFFECTCOMMON_H_
  49. #include "postFx/postEffectCommon.h"
  50. #endif
  51. #ifndef _TDICTIONARY_H_
  52. #include "core/util/tDictionary.h"
  53. #endif
  54. #ifndef _MATTEXTURETARGET_H_
  55. #include "materials/matTextureTarget.h"
  56. #endif
  57. class GFXStateBlockData;
  58. class Frustum;
  59. class SceneRenderState;
  60. class ConditionerFeature;
  61. ///
  62. GFX_DeclareTextureProfile( PostFxTargetProfile );
  63. ///
  64. class PostEffect : public SimGroup
  65. {
  66. typedef SimGroup Parent;
  67. friend class PostEffectVis;
  68. public:
  69. enum
  70. {
  71. NumTextures = 8,
  72. };
  73. protected:
  74. FileName mTexFilename[NumTextures];
  75. GFXTexHandle mTextures[NumTextures];
  76. NamedTexTarget mNamedTarget;
  77. NamedTexTarget mNamedTargetDepthStencil;
  78. GFXTextureObject *mActiveTextures[NumTextures];
  79. NamedTexTarget *mActiveNamedTarget[NumTextures];
  80. RectI mActiveTextureViewport[NumTextures];
  81. GFXStateBlockData *mStateBlockData;
  82. GFXStateBlockRef mStateBlock;
  83. String mShaderName;
  84. GFXShaderRef mShader;
  85. Vector<GFXShaderMacro> mShaderMacros;
  86. GFXShaderConstBufferRef mShaderConsts;
  87. GFXShaderConstHandle *mRTSizeSC;
  88. GFXShaderConstHandle *mOneOverRTSizeSC;
  89. GFXShaderConstHandle *mTexSizeSC[NumTextures];
  90. GFXShaderConstHandle *mRenderTargetParamsSC[NumTextures];
  91. GFXShaderConstHandle *mViewportOffsetSC;
  92. GFXShaderConstHandle *mTargetViewportSC;
  93. GFXShaderConstHandle *mFogDataSC;
  94. GFXShaderConstHandle *mFogColorSC;
  95. GFXShaderConstHandle *mEyePosSC;
  96. GFXShaderConstHandle *mMatWorldToScreenSC;
  97. GFXShaderConstHandle *mMatScreenToWorldSC;
  98. GFXShaderConstHandle *mMatPrevScreenToWorldSC;
  99. GFXShaderConstHandle *mNearFarSC;
  100. GFXShaderConstHandle *mInvNearFarSC;
  101. GFXShaderConstHandle *mWorldToScreenScaleSC;
  102. GFXShaderConstHandle *mProjectionOffsetSC;
  103. GFXShaderConstHandle *mWaterColorSC;
  104. GFXShaderConstHandle *mWaterFogDataSC;
  105. GFXShaderConstHandle *mAmbientColorSC;
  106. GFXShaderConstHandle *mWaterFogPlaneSC;
  107. GFXShaderConstHandle *mWaterDepthGradMaxSC;
  108. GFXShaderConstHandle *mScreenSunPosSC;
  109. GFXShaderConstHandle *mLightDirectionSC;
  110. GFXShaderConstHandle *mCameraForwardSC;
  111. GFXShaderConstHandle *mAccumTimeSC;
  112. GFXShaderConstHandle *mDeltaTimeSC;
  113. GFXShaderConstHandle *mInvCameraMatSC;
  114. bool mAllowReflectPass;
  115. /// If true update the shader.
  116. bool mUpdateShader;
  117. GFXTextureTargetRef mTarget;
  118. String mTargetName;
  119. GFXTexHandle mTargetTex;
  120. String mTargetDepthStencilName;
  121. GFXTexHandle mTargetDepthStencil;
  122. /// If mTargetSize is zero then this scale is
  123. /// used to make a relative texture size to the
  124. /// active render target.
  125. Point2F mTargetScale;
  126. /// If non-zero this is used as the absolute
  127. /// texture target size.
  128. /// @see mTargetScale
  129. Point2I mTargetSize;
  130. GFXFormat mTargetFormat;
  131. /// The color to prefill the named target when
  132. /// first created by the effect.
  133. ColorF mTargetClearColor;
  134. PFXRenderTime mRenderTime;
  135. PFXTargetClear mTargetClear;
  136. PFXTargetViewport mTargetViewport;
  137. String mRenderBin;
  138. F32 mRenderPriority;
  139. /// This is true if the effect has been succesfully
  140. /// initialized and all requirements are met for use.
  141. bool mIsValid;
  142. /// True if the effect has been enabled by the manager.
  143. bool mEnabled;
  144. /// Skip processing of this PostEffect and its children even if its parent is enabled.
  145. /// Parent and sibling PostEffects in the chain are still processed.
  146. /// This is intended for debugging purposes.
  147. bool mSkip;
  148. bool mOneFrameOnly;
  149. bool mOnThisFrame;
  150. U32 mShaderReloadKey;
  151. class EffectConst
  152. {
  153. public:
  154. EffectConst( const String &name, const String &val )
  155. : mName( name ),
  156. mHandle( NULL ),
  157. mDirty( true )
  158. {
  159. set( val );
  160. }
  161. void set( const String &newVal );
  162. void setToBuffer( GFXShaderConstBufferRef buff );
  163. String mName;
  164. GFXShaderConstHandle *mHandle;
  165. String mStringVal;
  166. bool mDirty;
  167. };
  168. typedef HashTable<StringCase,EffectConst*> EffectConstTable;
  169. EffectConstTable mEffectConsts;
  170. ///
  171. virtual void _updateScreenGeometry( const Frustum &frustum,
  172. GFXVertexBufferHandle<PFXVertex> *outVB );
  173. ///
  174. virtual void _setupStateBlock( const SceneRenderState *state );
  175. ///
  176. virtual void _setupConstants( const SceneRenderState *state );
  177. ///
  178. virtual void _setupTransforms();
  179. ///
  180. virtual void _setupTarget( const SceneRenderState *state, bool *outClearTarget );
  181. ///
  182. virtual void _setupTexture( U32 slot, GFXTexHandle &inputTex, const RectI *inTexViewport );
  183. /// Protected set method for toggling the enabled state.
  184. static bool _setIsEnabled( void *object, const char *index, const char *data );
  185. /// Called from the light manager activate signal.
  186. /// @see LightManager::addActivateCallback
  187. void _onLMActivate( const char*, bool activate )
  188. {
  189. if ( activate )
  190. mUpdateShader = true;
  191. }
  192. /// We handle texture events to release named rendered targets.
  193. /// @see GFXTextureManager::addEventDelegate
  194. void _onTextureEvent( GFXTexCallbackCode code )
  195. {
  196. if ( code == GFXZombify && (mNamedTarget.isRegistered() || mNamedTargetDepthStencil.isRegistered()) )
  197. _cleanTargets();
  198. }
  199. ///
  200. void _updateConditioners();
  201. ///
  202. void _cleanTargets( bool recurse = false );
  203. ///
  204. void _checkRequirements();
  205. ///
  206. GFXTextureObject* _getTargetTexture( U32 index );
  207. public:
  208. /// Constructor.
  209. PostEffect();
  210. /// Destructor.
  211. virtual ~PostEffect();
  212. DECLARE_CONOBJECT(PostEffect);
  213. // SimObject
  214. virtual bool onAdd();
  215. virtual void onRemove();
  216. static void initPersistFields();
  217. /// @name Callbacks
  218. /// @{
  219. DECLARE_CALLBACK( void, onAdd, () );
  220. DECLARE_CALLBACK( void, preProcess, () );
  221. DECLARE_CALLBACK( void, setShaderConsts, () );
  222. DECLARE_CALLBACK( bool, onEnabled, () );
  223. DECLARE_CALLBACK( void, onDisabled, () );
  224. /// @}
  225. virtual void process( const SceneRenderState *state,
  226. GFXTexHandle &inOutTex,
  227. const RectI *inTexViewport = NULL );
  228. ///
  229. void reload();
  230. ///
  231. void enable();
  232. ///
  233. void disable();
  234. /// Dump the shader disassembly to a temporary text file.
  235. /// Returns true and sets outFilename to the file if successful.
  236. bool dumpShaderDisassembly( String &outFilename ) const;
  237. /// Returns the SimSet which contains all PostEffects.
  238. SimSet* getSet() const;
  239. ///
  240. bool isEnabled() const { return mEnabled; }
  241. /// Is set to skip rendering.
  242. bool isSkipped() const { return mSkip; }
  243. /// Set the effect to skip rendering.
  244. void setSkip( bool skip ) { mSkip = skip; }
  245. PFXRenderTime getRenderTime() const { return mRenderTime; }
  246. const String& getRenderBin() const { return mRenderBin; }
  247. F32 getPriority() const { return mRenderPriority; }
  248. void setTexture( U32 index, const String &filePath );
  249. void setShaderMacro( const String &name, const String &value = String::EmptyString );
  250. bool removeShaderMacro( const String &name );
  251. void clearShaderMacros();
  252. ///
  253. void setShaderConst( const String &name, const String &val );
  254. void setOnThisFrame( bool enabled ) { mOnThisFrame = enabled; }
  255. bool isOnThisFrame() { return mOnThisFrame; }
  256. void setOneFrameOnly( bool enabled ) { mOneFrameOnly = enabled; }
  257. bool isOneFrameOnly() { return mOneFrameOnly; }
  258. F32 getAspectRatio() const;
  259. enum PostEffectRequirements
  260. {
  261. RequiresDepth = BIT(0),
  262. RequiresNormals = BIT(1),
  263. RequiresLightInfo = BIT(2),
  264. };
  265. };
  266. #endif // _POST_EFFECT_H_