postEffect.h 9.3 KB

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