postEffect.h 12 KB

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