postEffect.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 _GFXCUBEMAP_H_
  43. #include "gfx/gfxCubemap.h"
  44. #endif
  45. #ifndef _GFXTARGET_H_
  46. #include "gfx/gfxTarget.h"
  47. #endif
  48. #ifndef _GFXVERTEXBUFFER_H_
  49. #include "gfx/gfxVertexBuffer.h"
  50. #endif
  51. #ifndef _POSTEFFECTCOMMON_H_
  52. #include "postFx/postEffectCommon.h"
  53. #endif
  54. #ifndef _TDICTIONARY_H_
  55. #include "core/util/tDictionary.h"
  56. #endif
  57. #ifndef _MATTEXTURETARGET_H_
  58. #include "materials/matTextureTarget.h"
  59. #endif
  60. #include "T3D/assets/ImageAsset.h"
  61. class GFXStateBlockData;
  62. class Frustum;
  63. class SceneRenderState;
  64. class ConditionerFeature;
  65. ///
  66. GFX_DeclareTextureProfile( PostFxTargetProfile );
  67. ///
  68. class PostEffect : public SimGroup
  69. {
  70. typedef SimGroup Parent;
  71. friend class PostEffectVis;
  72. public:
  73. enum
  74. {
  75. NumTextures = 8,
  76. };
  77. protected:
  78. DECLARE_IMAGEASSET_ARRAY(PostEffect, Texture, NumTextures);
  79. DECLARE_IMAGEASSET_ARRAY_SETGET(PostEffect, Texture);
  80. bool mTexSRGB[NumTextures];
  81. enum
  82. {
  83. NormalTextureType = 0,
  84. CubemapType,
  85. CubemapArrayType,
  86. } mTextureType[NumTextures];
  87. GFXCubemapHandle mCubemapTextures[NumTextures];
  88. GFXCubemapArrayHandle mCubemapArrayTextures[NumTextures];
  89. NamedTexTarget mNamedTarget;
  90. NamedTexTarget mNamedTargetDepthStencil;
  91. GFXTextureObject *mActiveTextures[NumTextures];
  92. NamedTexTarget *mActiveNamedTarget[NumTextures];
  93. RectI mActiveTextureViewport[NumTextures];
  94. GFXStateBlockData *mStateBlockData;
  95. GFXStateBlockRef mStateBlock;
  96. String mShaderName;
  97. GFXShaderRef mShader;
  98. Vector<GFXShaderMacro> mShaderMacros;
  99. GFXShaderConstBufferRef mShaderConsts;
  100. GFXShaderConstHandle *mRTSizeSC;
  101. GFXShaderConstHandle *mOneOverRTSizeSC;
  102. GFXShaderConstHandle *mTexSizeSC[NumTextures];
  103. GFXShaderConstHandle *mRenderTargetParamsSC[NumTextures];
  104. GFXShaderConstHandle *mViewportOffsetSC;
  105. GFXShaderConstHandle *mTargetViewportSC;
  106. GFXShaderConstHandle *mFogDataSC;
  107. GFXShaderConstHandle *mFogColorSC;
  108. GFXShaderConstHandle *mEyePosSC;
  109. GFXShaderConstHandle *mMatWorldToScreenSC;
  110. GFXShaderConstHandle *mMatScreenToWorldSC;
  111. GFXShaderConstHandle *mMatPrevScreenToWorldSC;
  112. GFXShaderConstHandle *mNearFarSC;
  113. GFXShaderConstHandle *mInvNearFarSC;
  114. GFXShaderConstHandle *mWorldToScreenScaleSC;
  115. GFXShaderConstHandle *mProjectionOffsetSC;
  116. GFXShaderConstHandle *mWaterColorSC;
  117. GFXShaderConstHandle *mWaterFogDataSC;
  118. GFXShaderConstHandle *mAmbientColorSC;
  119. GFXShaderConstHandle *mWaterFogPlaneSC;
  120. GFXShaderConstHandle *mWaterDepthGradMaxSC;
  121. GFXShaderConstHandle *mScreenSunPosSC;
  122. GFXShaderConstHandle *mLightDirectionSC;
  123. GFXShaderConstHandle *mCameraForwardSC;
  124. GFXShaderConstHandle *mAccumTimeSC;
  125. GFXShaderConstHandle *mDeltaTimeSC;
  126. GFXShaderConstHandle *mInvCameraMatSC;
  127. GFXShaderConstHandle *mMatCameraToWorldSC;
  128. GFXShaderConstHandle *mInvCameraTransSC;
  129. GFXShaderConstHandle *mMatCameraToScreenSC;
  130. GFXShaderConstHandle *mMatScreenToCameraSC;
  131. bool mAllowReflectPass;
  132. /// If true update the shader.
  133. bool mUpdateShader;
  134. GFXTextureTargetRef mTarget;
  135. String mTargetName;
  136. GFXTexHandle mTargetTex;
  137. String mTargetDepthStencilName;
  138. GFXTexHandle mTargetDepthStencil;
  139. /// If mTargetSize is zero then this scale is
  140. /// used to make a relative texture size to the
  141. /// active render target.
  142. Point2F mTargetScale;
  143. /// If non-zero this is used as the absolute
  144. /// texture target size.
  145. /// @see mTargetScale
  146. Point2I mTargetSize;
  147. GFXFormat mTargetFormat;
  148. /// The color to prefill the named target when
  149. /// first created by the effect.
  150. LinearColorF mTargetClearColor;
  151. PFXRenderTime mRenderTime;
  152. PFXTargetClear mTargetClear;
  153. PFXTargetViewport mTargetViewport;
  154. String mRenderBin;
  155. F32 mRenderPriority;
  156. /// This is true if the effect has been succesfully
  157. /// initialized and all requirements are met for use.
  158. bool mIsValid;
  159. /// True if the effect has been enabled by the manager.
  160. bool mEnabled;
  161. /// Skip processing of this PostEffect and its children even if its parent is enabled.
  162. /// Parent and sibling PostEffects in the chain are still processed.
  163. /// This is intended for debugging purposes.
  164. bool mSkip;
  165. bool mOneFrameOnly;
  166. bool mOnThisFrame;
  167. U32 mShaderReloadKey;
  168. class EffectConst
  169. {
  170. public:
  171. EffectConst( const String &name, const String &val )
  172. : mName( name ),
  173. mHandle( NULL ),
  174. mDirty( true )
  175. {
  176. set( val );
  177. }
  178. EffectConst(const String &name, const F32 &val)
  179. : mName(name),
  180. mHandle(NULL),
  181. mDirty(true)
  182. {
  183. set(val);
  184. }
  185. EffectConst(const String& name, const int& val)
  186. : mName(name),
  187. mHandle(NULL),
  188. mDirty(true)
  189. {
  190. set(val);
  191. }
  192. EffectConst(const String &name, const Point4F &val)
  193. : mName(name),
  194. mHandle(NULL),
  195. mDirty(true)
  196. {
  197. set(val);
  198. }
  199. EffectConst(const String &name, const MatrixF &val)
  200. : mName(name),
  201. mHandle(NULL),
  202. mDirty(true)
  203. {
  204. set(val);
  205. }
  206. EffectConst(const String &name, const Vector<Point4F> &val)
  207. : mName(name),
  208. mHandle(NULL),
  209. mDirty(true)
  210. {
  211. set(val);
  212. }
  213. EffectConst(const String &name, const Vector<MatrixF> &val)
  214. : mName(name),
  215. mHandle(NULL),
  216. mDirty(true)
  217. {
  218. set(val);
  219. }
  220. void set( const String &newVal );
  221. void set(const F32 &newVal);
  222. void set(const int& newVal);
  223. void set(const Point4F &newVal);
  224. void set(const MatrixF &newVal);
  225. void set(const Vector<Point4F> &newVal);
  226. void set(const Vector<MatrixF> &newVal);
  227. void setToBuffer( GFXShaderConstBufferRef buff );
  228. String mName;
  229. GFXShaderConstHandle *mHandle;
  230. String mStringVal;
  231. S32 mIntVal;
  232. F32 mFloatVal;
  233. Point4F mPointVal;
  234. MatrixF mMatrixVal;
  235. Vector<Point4F> mPointArrayVal;
  236. Vector<MatrixF> mMatrixArrayVal;
  237. enum
  238. {
  239. StringType,
  240. IntType,
  241. FloatType,
  242. PointType,
  243. MatrixType,
  244. PointArrayType,
  245. MatrixArrayType
  246. } mValueType;
  247. bool mDirty;
  248. };
  249. typedef HashTable<StringCase,EffectConst*> EffectConstTable;
  250. EffectConstTable mEffectConsts;
  251. ///
  252. virtual void _updateScreenGeometry( const Frustum &frustum,
  253. GFXVertexBufferHandle<PFXVertex> *outVB );
  254. ///
  255. virtual void _setupStateBlock( const SceneRenderState *state );
  256. ///
  257. virtual void _setupConstants( const SceneRenderState *state );
  258. ///
  259. virtual void _setupTransforms();
  260. ///
  261. virtual void _setupTarget( const SceneRenderState *state, bool *outClearTarget );
  262. ///
  263. virtual void _setupTexture( U32 slot, GFXTexHandle &inputTex, const RectI *inTexViewport );
  264. virtual void _setupCubemapTexture(U32 stage, GFXCubemapHandle &inputTex);
  265. virtual void _setupCubemapArrayTexture(U32 slot, GFXCubemapArrayHandle &inputTex);
  266. /// Protected set method for toggling the enabled state.
  267. static bool _setIsEnabled( void *object, const char *index, const char *data );
  268. /// Called from the light manager activate signal.
  269. /// @see LightManager::addActivateCallback
  270. void _onLMActivate( const char*, bool activate )
  271. {
  272. if ( activate )
  273. mUpdateShader = true;
  274. }
  275. /// We handle texture events to release named rendered targets.
  276. /// @see GFXTextureManager::addEventDelegate
  277. void _onTextureEvent( GFXTexCallbackCode code )
  278. {
  279. if ( code == GFXZombify && (mNamedTarget.isRegistered() || mNamedTargetDepthStencil.isRegistered()) )
  280. _cleanTargets();
  281. }
  282. ///
  283. void _updateConditioners();
  284. ///
  285. void _cleanTargets( bool recurse = false );
  286. ///
  287. void _checkRequirements();
  288. ///
  289. GFXTextureObject* _getTargetTexture( U32 index );
  290. public:
  291. /// Constructor.
  292. PostEffect();
  293. /// Destructor.
  294. virtual ~PostEffect();
  295. DECLARE_CONOBJECT(PostEffect);
  296. // SimObject
  297. virtual bool onAdd();
  298. virtual void onRemove();
  299. static void initPersistFields();
  300. /// @name Callbacks
  301. /// @{
  302. DECLARE_CALLBACK( void, onAdd, () );
  303. DECLARE_CALLBACK( void, preProcess, () );
  304. DECLARE_CALLBACK( void, setShaderConsts, () );
  305. DECLARE_CALLBACK( bool, onEnabled, () );
  306. DECLARE_CALLBACK( void, onDisabled, () );
  307. /// @}
  308. virtual void process( const SceneRenderState *state,
  309. GFXTexHandle &inOutTex,
  310. const RectI *inTexViewport = NULL );
  311. ///
  312. void reload();
  313. ///
  314. void enable();
  315. ///
  316. void disable();
  317. /// Dump the shader disassembly to a temporary text file.
  318. /// Returns true and sets outFilename to the file if successful.
  319. bool dumpShaderDisassembly( String &outFilename ) const;
  320. /// Returns the SimSet which contains all PostEffects.
  321. SimSet* getSet() const;
  322. ///
  323. bool isEnabled() const { return mEnabled; }
  324. /// Is set to skip rendering.
  325. bool isSkipped() const { return mSkip; }
  326. /// Set the effect to skip rendering.
  327. void setSkip( bool skip ) { mSkip = skip; }
  328. PFXRenderTime getRenderTime() const { return mRenderTime; }
  329. const String& getRenderBin() const { return mRenderBin; }
  330. F32 getPriority() const { return mRenderPriority; }
  331. void setTexture( U32 index, const String &filePath );
  332. void setTexture(U32 index, const GFXTexHandle& texHandle);
  333. void setCubemapTexture(U32 index, const GFXCubemapHandle &cubemapHandle);
  334. void setCubemapArrayTexture(U32 index, const GFXCubemapArrayHandle &cubemapArrayHandle);
  335. void setShaderMacro( const String &name, const String &value = String::EmptyString );
  336. bool removeShaderMacro( const String &name );
  337. void clearShaderMacros();
  338. ///
  339. void setShaderConst( const String &name, const String &val );
  340. void setShaderConst(const String &name, const F32 &val);
  341. void setShaderConst(const String& name, const int& val);
  342. void setShaderConst(const String &name, const Point4F &val);
  343. void setShaderConst(const String &name, const MatrixF &val);
  344. void setShaderConst(const String &name, const Vector<Point4F> &val);
  345. void setShaderConst(const String &name, const Vector<MatrixF> &val);
  346. void setOnThisFrame( bool enabled ) { mOnThisFrame = enabled; }
  347. bool isOnThisFrame() { return mOnThisFrame; }
  348. void setOneFrameOnly( bool enabled ) { mOneFrameOnly = enabled; }
  349. bool isOneFrameOnly() { return mOneFrameOnly; }
  350. F32 getAspectRatio() const;
  351. GFXShaderRef getShader() { return mShader; }
  352. Vector<GFXShaderMacro>* getShaderMacros() { return &mShaderMacros; }
  353. GFXShaderConstBufferRef getShaderConstBuffer() { return mShaderConsts; }
  354. enum PostEffectRequirements
  355. {
  356. RequiresDepth = BIT(0),
  357. RequiresNormals = BIT(1),
  358. RequiresLightInfo = BIT(2),
  359. };
  360. };
  361. #endif // _POST_EFFECT_H_