postEffect.h 12 KB

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