postEffect.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 = 6,
  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 *mFogDataSC;
  93. GFXShaderConstHandle *mFogColorSC;
  94. GFXShaderConstHandle *mEyePosSC;
  95. GFXShaderConstHandle *mMatWorldToScreenSC;
  96. GFXShaderConstHandle *mMatScreenToWorldSC;
  97. GFXShaderConstHandle *mMatPrevScreenToWorldSC;
  98. GFXShaderConstHandle *mNearFarSC;
  99. GFXShaderConstHandle *mInvNearFarSC;
  100. GFXShaderConstHandle *mWorldToScreenScaleSC;
  101. GFXShaderConstHandle *mWaterColorSC;
  102. GFXShaderConstHandle *mWaterFogDataSC;
  103. GFXShaderConstHandle *mAmbientColorSC;
  104. GFXShaderConstHandle *mWaterFogPlaneSC;
  105. GFXShaderConstHandle *mWaterDepthGradMaxSC;
  106. GFXShaderConstHandle *mScreenSunPosSC;
  107. GFXShaderConstHandle *mLightDirectionSC;
  108. GFXShaderConstHandle *mCameraForwardSC;
  109. GFXShaderConstHandle *mAccumTimeSC;
  110. GFXShaderConstHandle *mDeltaTimeSC;
  111. GFXShaderConstHandle *mInvCameraMatSC;
  112. bool mAllowReflectPass;
  113. /// If true update the shader.
  114. bool mUpdateShader;
  115. GFXTextureTargetRef mTarget;
  116. String mTargetName;
  117. GFXTexHandle mTargetTex;
  118. String mTargetDepthStencilName;
  119. GFXTexHandle mTargetDepthStencil;
  120. /// If mTargetSize is zero then this scale is
  121. /// used to make a relative texture size to the
  122. /// active render target.
  123. Point2F mTargetScale;
  124. /// If non-zero this is used as the absolute
  125. /// texture target size.
  126. /// @see mTargetScale
  127. Point2I mTargetSize;
  128. GFXFormat mTargetFormat;
  129. /// The color to prefill the named target when
  130. /// first created by the effect.
  131. ColorF mTargetClearColor;
  132. PFXRenderTime mRenderTime;
  133. PFXTargetClear mTargetClear;
  134. String mRenderBin;
  135. F32 mRenderPriority;
  136. /// This is true if the effect has been succesfully
  137. /// initialized and all requirements are met for use.
  138. bool mIsValid;
  139. /// True if the effect has been enabled by the manager.
  140. bool mEnabled;
  141. /// Skip processing of this PostEffect and its children even if its parent is enabled.
  142. /// Parent and sibling PostEffects in the chain are still processed.
  143. /// This is intended for debugging purposes.
  144. bool mSkip;
  145. bool mOneFrameOnly;
  146. bool mOnThisFrame;
  147. U32 mShaderReloadKey;
  148. class EffectConst
  149. {
  150. public:
  151. EffectConst( const String &name, const String &val )
  152. : mName( name ),
  153. mHandle( NULL ),
  154. mDirty( true )
  155. {
  156. set( val );
  157. }
  158. void set( const String &newVal );
  159. void setToBuffer( GFXShaderConstBufferRef buff );
  160. String mName;
  161. GFXShaderConstHandle *mHandle;
  162. String mStringVal;
  163. bool mDirty;
  164. };
  165. typedef HashTable<StringCase,EffectConst*> EffectConstTable;
  166. EffectConstTable mEffectConsts;
  167. ///
  168. virtual void _updateScreenGeometry( const Frustum &frustum,
  169. GFXVertexBufferHandle<PFXVertex> *outVB );
  170. ///
  171. virtual void _setupStateBlock( const SceneRenderState *state );
  172. ///
  173. virtual void _setupConstants( const SceneRenderState *state );
  174. ///
  175. virtual void _setupTransforms();
  176. ///
  177. virtual void _setupTarget( const SceneRenderState *state, bool *outClearTarget );
  178. ///
  179. virtual void _setupTexture( U32 slot, GFXTexHandle &inputTex, const RectI *inTexViewport );
  180. /// Protected set method for toggling the enabled state.
  181. static bool _setIsEnabled( void *object, const char *index, const char *data );
  182. /// Called from the light manager activate signal.
  183. /// @see LightManager::addActivateCallback
  184. void _onLMActivate( const char*, bool activate )
  185. {
  186. if ( activate )
  187. mUpdateShader = true;
  188. }
  189. /// We handle texture events to release named rendered targets.
  190. /// @see GFXTextureManager::addEventDelegate
  191. void _onTextureEvent( GFXTexCallbackCode code )
  192. {
  193. if ( code == GFXZombify && (mNamedTarget.isRegistered() || mNamedTargetDepthStencil.isRegistered()) )
  194. _cleanTargets();
  195. }
  196. ///
  197. void _updateConditioners();
  198. ///
  199. void _cleanTargets( bool recurse = false );
  200. ///
  201. void _checkRequirements();
  202. ///
  203. GFXTextureObject* _getTargetTexture( U32 index );
  204. public:
  205. /// Constructor.
  206. PostEffect();
  207. /// Destructor.
  208. virtual ~PostEffect();
  209. DECLARE_CONOBJECT(PostEffect);
  210. // SimObject
  211. virtual bool onAdd();
  212. virtual void onRemove();
  213. static void initPersistFields();
  214. /// @name Callbacks
  215. /// @{
  216. DECLARE_CALLBACK( void, onAdd, () );
  217. DECLARE_CALLBACK( void, preProcess, () );
  218. DECLARE_CALLBACK( void, setShaderConsts, () );
  219. DECLARE_CALLBACK( bool, onEnabled, () );
  220. DECLARE_CALLBACK( void, onDisabled, () );
  221. /// @}
  222. virtual void process( const SceneRenderState *state,
  223. GFXTexHandle &inOutTex,
  224. const RectI *inTexViewport = NULL );
  225. ///
  226. void reload();
  227. ///
  228. void enable();
  229. ///
  230. void disable();
  231. /// Dump the shader disassembly to a temporary text file.
  232. /// Returns true and sets outFilename to the file if successful.
  233. bool dumpShaderDisassembly( String &outFilename ) const;
  234. /// Returns the SimSet which contains all PostEffects.
  235. SimSet* getSet() const;
  236. ///
  237. bool isEnabled() const { return mEnabled; }
  238. /// Is set to skip rendering.
  239. bool isSkipped() const { return mSkip; }
  240. /// Set the effect to skip rendering.
  241. void setSkip( bool skip ) { mSkip = skip; }
  242. PFXRenderTime getRenderTime() const { return mRenderTime; }
  243. const String& getRenderBin() const { return mRenderBin; }
  244. F32 getPriority() const { return mRenderPriority; }
  245. void setTexture( U32 index, const String &filePath );
  246. void setShaderMacro( const String &name, const String &value = String::EmptyString );
  247. bool removeShaderMacro( const String &name );
  248. void clearShaderMacros();
  249. ///
  250. void setShaderConst( const String &name, const String &val );
  251. void setOnThisFrame( bool enabled ) { mOnThisFrame = enabled; }
  252. bool isOnThisFrame() { return mOnThisFrame; }
  253. void setOneFrameOnly( bool enabled ) { mOneFrameOnly = enabled; }
  254. bool isOneFrameOnly() { return mOneFrameOnly; }
  255. F32 getAspectRatio() const;
  256. enum PostEffectRequirements
  257. {
  258. RequiresDepth = BIT(0),
  259. RequiresNormals = BIT(1),
  260. RequiresLightInfo = BIT(2),
  261. };
  262. };
  263. #endif // _POST_EFFECT_H_