postEffect.h 13 KB

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