postEffect.h 13 KB

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