postEffect.h 12 KB

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