postEffect.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. GFXShaderConstHandle *mSunVisibilitySC; //sunBokeh feature
  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. ColorF mTargetClearColor;
  135. PFXRenderTime mRenderTime;
  136. PFXTargetClear mTargetClear;
  137. PFXTargetViewport mTargetViewport;
  138. String mRenderBin;
  139. F32 mRenderPriority;
  140. /// Test sun visibility feature for sunBokeh
  141. F32 _testVisibility( const SceneRenderState *state );
  142. /// This is true if the effect has been succesfully
  143. /// initialized and all requirements are met for use.
  144. bool mIsValid;
  145. /// True if the effect has been enabled by the manager.
  146. bool mEnabled;
  147. /// Skip processing of this PostEffect and its children even if its parent is enabled.
  148. /// Parent and sibling PostEffects in the chain are still processed.
  149. /// This is intended for debugging purposes.
  150. bool mSkip;
  151. bool mOneFrameOnly;
  152. bool mOnThisFrame;
  153. U32 mShaderReloadKey;
  154. class EffectConst
  155. {
  156. public:
  157. EffectConst( const String &name, const String &val )
  158. : mName( name ),
  159. mHandle( NULL ),
  160. mDirty( true )
  161. {
  162. set( val );
  163. }
  164. void set( const String &newVal );
  165. void setToBuffer( GFXShaderConstBufferRef buff );
  166. String mName;
  167. GFXShaderConstHandle *mHandle;
  168. String mStringVal;
  169. bool mDirty;
  170. };
  171. typedef HashTable<StringCase,EffectConst*> EffectConstTable;
  172. EffectConstTable mEffectConsts;
  173. ///
  174. virtual void _updateScreenGeometry( const Frustum &frustum,
  175. GFXVertexBufferHandle<PFXVertex> *outVB );
  176. ///
  177. virtual void _setupStateBlock( const SceneRenderState *state );
  178. ///
  179. virtual void _setupConstants( const SceneRenderState *state );
  180. ///
  181. virtual void _setupTransforms();
  182. ///
  183. virtual void _setupTarget( const SceneRenderState *state, bool *outClearTarget );
  184. ///
  185. virtual void _setupTexture( U32 slot, GFXTexHandle &inputTex, const RectI *inTexViewport );
  186. /// Protected set method for toggling the enabled state.
  187. static bool _setIsEnabled( void *object, const char *index, const char *data );
  188. /// Called from the light manager activate signal.
  189. /// @see LightManager::addActivateCallback
  190. void _onLMActivate( const char*, bool activate )
  191. {
  192. if ( activate )
  193. mUpdateShader = true;
  194. }
  195. /// We handle texture events to release named rendered targets.
  196. /// @see GFXTextureManager::addEventDelegate
  197. void _onTextureEvent( GFXTexCallbackCode code )
  198. {
  199. if ( code == GFXZombify && (mNamedTarget.isRegistered() || mNamedTargetDepthStencil.isRegistered()) )
  200. _cleanTargets();
  201. }
  202. ///
  203. void _updateConditioners();
  204. ///
  205. void _cleanTargets( bool recurse = false );
  206. ///
  207. void _checkRequirements();
  208. ///
  209. GFXTextureObject* _getTargetTexture( U32 index );
  210. public:
  211. /// Constructor.
  212. PostEffect();
  213. /// Destructor.
  214. virtual ~PostEffect();
  215. DECLARE_CONOBJECT(PostEffect);
  216. // SimObject
  217. virtual bool onAdd();
  218. virtual void onRemove();
  219. static void initPersistFields();
  220. /// @name Callbacks
  221. /// @{
  222. DECLARE_CALLBACK( void, onAdd, () );
  223. DECLARE_CALLBACK( void, preProcess, () );
  224. DECLARE_CALLBACK( void, setShaderConsts, () );
  225. DECLARE_CALLBACK( bool, onEnabled, () );
  226. DECLARE_CALLBACK( void, onDisabled, () );
  227. /// @}
  228. virtual void process( const SceneRenderState *state,
  229. GFXTexHandle &inOutTex,
  230. const RectI *inTexViewport = NULL );
  231. ///
  232. void reload();
  233. ///
  234. void enable();
  235. ///
  236. void disable();
  237. /// Dump the shader disassembly to a temporary text file.
  238. /// Returns true and sets outFilename to the file if successful.
  239. bool dumpShaderDisassembly( String &outFilename ) const;
  240. /// Returns the SimSet which contains all PostEffects.
  241. SimSet* getSet() const;
  242. ///
  243. bool isEnabled() const { return mEnabled; }
  244. /// Is set to skip rendering.
  245. bool isSkipped() const { return mSkip; }
  246. /// Set the effect to skip rendering.
  247. void setSkip( bool skip ) { mSkip = skip; }
  248. PFXRenderTime getRenderTime() const { return mRenderTime; }
  249. const String& getRenderBin() const { return mRenderBin; }
  250. F32 getPriority() const { return mRenderPriority; }
  251. void setTexture( U32 index, const String &filePath );
  252. void setShaderMacro( const String &name, const String &value = String::EmptyString );
  253. bool removeShaderMacro( const String &name );
  254. void clearShaderMacros();
  255. ///
  256. void setShaderConst( const String &name, const String &val );
  257. void setOnThisFrame( bool enabled ) { mOnThisFrame = enabled; }
  258. bool isOnThisFrame() { return mOnThisFrame; }
  259. void setOneFrameOnly( bool enabled ) { mOneFrameOnly = enabled; }
  260. bool isOneFrameOnly() { return mOneFrameOnly; }
  261. F32 getAspectRatio() const;
  262. enum PostEffectRequirements
  263. {
  264. RequiresDepth = BIT(0),
  265. RequiresNormals = BIT(1),
  266. RequiresLightInfo = BIT(2),
  267. };
  268. };
  269. #endif // _POST_EFFECT_H_