2
0

postEffect.h 12 KB

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