postEffect.h

Engine/source/postFx/postEffect.h

More...

Classes:

Public Functions

GFX_DeclareTextureProfile(PostFxTargetProfile )

Detailed Description

Public Functions

GFX_DeclareTextureProfile(PostFxTargetProfile )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _POST_EFFECT_H_
 25#define _POST_EFFECT_H_
 26
 27#ifndef _SIMSET_H_
 28#include "console/simSet.h"
 29#endif
 30#ifndef _MPOINT3_H_
 31#include "math/mPoint3.h"
 32#endif
 33#ifndef _MPOINT2_H_
 34#include "math/mPoint2.h"
 35#endif
 36#ifndef _GFXSHADER_H_
 37#include "gfx/gfxShader.h"
 38#endif
 39#ifndef _GFXSTATEBLOCK_H_
 40#include "gfx/gfxStateBlock.h"
 41#endif
 42#ifndef _GFXTEXTUREHANDLE_H_
 43#include "gfx/gfxTextureHandle.h"
 44#endif
 45#ifndef _GFXCUBEMAP_H_
 46#include "gfx/gfxCubemap.h"
 47#endif
 48#ifndef _GFXTARGET_H_
 49#include "gfx/gfxTarget.h"
 50#endif
 51#ifndef _GFXVERTEXBUFFER_H_
 52#include "gfx/gfxVertexBuffer.h"
 53#endif
 54#ifndef _POSTEFFECTCOMMON_H_
 55#include "postFx/postEffectCommon.h"
 56#endif
 57#ifndef _TDICTIONARY_H_
 58#include "core/util/tDictionary.h"
 59#endif
 60#ifndef _MATTEXTURETARGET_H_
 61#include "materials/matTextureTarget.h"
 62#endif
 63
 64class GFXStateBlockData;
 65class Frustum;
 66class SceneRenderState;
 67class ConditionerFeature;
 68
 69
 70///
 71GFX_DeclareTextureProfile( PostFxTargetProfile );
 72
 73
 74
 75
 76///
 77class PostEffect : public SimGroup
 78{
 79   typedef SimGroup Parent;
 80
 81   friend class PostEffectVis;
 82
 83public:
 84
 85   enum
 86   {
 87      NumTextures = 8,
 88   };
 89
 90protected:
 91
 92   FileName mTexFilename[NumTextures];
 93   bool mTexSRGB[NumTextures];
 94
 95   enum
 96   {
 97      NormalTextureType = 0,
 98      CubemapType,
 99      CubemapArrayType,
100   } mTextureType[NumTextures];
101
102   GFXTexHandle mTextures[NumTextures];
103   GFXCubemapHandle mCubemapTextures[NumTextures];
104   GFXCubemapArrayHandle mCubemapArrayTextures[NumTextures];
105
106   NamedTexTarget mNamedTarget;
107   NamedTexTarget mNamedTargetDepthStencil; 
108
109   GFXTextureObject *mActiveTextures[NumTextures];
110
111   NamedTexTarget *mActiveNamedTarget[NumTextures];
112
113   RectI mActiveTextureViewport[NumTextures];
114
115   GFXStateBlockData *mStateBlockData;
116
117   GFXStateBlockRef mStateBlock;
118
119   String mShaderName;
120
121   GFXShaderRef mShader;
122
123   Vector<GFXShaderMacro> mShaderMacros;
124   
125   GFXShaderConstBufferRef mShaderConsts;
126
127   GFXShaderConstHandle *mRTSizeSC;
128   GFXShaderConstHandle *mOneOverRTSizeSC;
129
130   GFXShaderConstHandle *mTexSizeSC[NumTextures];
131   GFXShaderConstHandle *mRenderTargetParamsSC[NumTextures];
132
133   GFXShaderConstHandle *mViewportOffsetSC;
134
135   GFXShaderConstHandle *mTargetViewportSC;
136
137   GFXShaderConstHandle *mFogDataSC;
138   GFXShaderConstHandle *mFogColorSC;
139   GFXShaderConstHandle *mEyePosSC;
140   GFXShaderConstHandle *mMatWorldToScreenSC;
141   GFXShaderConstHandle *mMatScreenToWorldSC;
142   GFXShaderConstHandle *mMatPrevScreenToWorldSC;
143   GFXShaderConstHandle *mNearFarSC;
144   GFXShaderConstHandle *mInvNearFarSC;   
145   GFXShaderConstHandle *mWorldToScreenScaleSC;
146   GFXShaderConstHandle *mProjectionOffsetSC;
147   GFXShaderConstHandle *mWaterColorSC;
148   GFXShaderConstHandle *mWaterFogDataSC;     
149   GFXShaderConstHandle *mAmbientColorSC;
150   GFXShaderConstHandle *mWaterFogPlaneSC;
151   GFXShaderConstHandle *mWaterDepthGradMaxSC;
152   GFXShaderConstHandle *mScreenSunPosSC;
153   GFXShaderConstHandle *mLightDirectionSC;
154   GFXShaderConstHandle *mCameraForwardSC;
155   GFXShaderConstHandle *mAccumTimeSC;
156   GFXShaderConstHandle *mDeltaTimeSC;
157   GFXShaderConstHandle *mInvCameraMatSC;
158   GFXShaderConstHandle *mMatCameraToWorldSC;
159   GFXShaderConstHandle *mInvCameraTransSC;
160   GFXShaderConstHandle *mMatCameraToScreenSC;
161   GFXShaderConstHandle *mMatScreenToCameraSC;
162
163   bool mAllowReflectPass;
164
165   /// If true update the shader.
166   bool mUpdateShader;   
167
168   GFXTextureTargetRef mTarget;
169
170   String mTargetName;
171   GFXTexHandle mTargetTex;
172
173   String mTargetDepthStencilName;
174   GFXTexHandle mTargetDepthStencil;
175
176   /// If mTargetSize is zero then this scale is
177   /// used to make a relative texture size to the
178   /// active render target.
179   Point2F mTargetScale;
180
181   /// If non-zero this is used as the absolute
182   /// texture target size.
183   /// @see mTargetScale
184   Point2I mTargetSize;
185
186   GFXFormat mTargetFormat;
187
188   /// The color to prefill the named target when
189   /// first created by the effect.
190   LinearColorF mTargetClearColor;
191
192   PFXRenderTime mRenderTime;
193   PFXTargetClear mTargetClear;
194   PFXTargetViewport mTargetViewport;
195
196   String mRenderBin;
197
198   F32 mRenderPriority;
199
200   /// This is true if the effect has been succesfully
201   /// initialized and all requirements are met for use.
202   bool mIsValid;
203
204   /// True if the effect has been enabled by the manager.
205   bool mEnabled;
206   
207   /// Skip processing of this PostEffect and its children even if its parent is enabled. 
208   /// Parent and sibling PostEffects in the chain are still processed.
209   /// This is intended for debugging purposes.
210   bool mSkip;
211
212   bool mOneFrameOnly;
213   bool mOnThisFrame;  
214
215   U32 mShaderReloadKey;
216
217   class EffectConst
218   {
219   public:
220
221      EffectConst( const String &name, const String &val )
222         : mName( name ), 
223           mHandle( NULL ),
224           mDirty( true )
225      {
226         set( val );
227      }
228
229      EffectConst(const String &name, const F32 &val)
230         : mName(name),
231         mHandle(NULL),
232         mDirty(true)
233      {
234         set(val);
235      }
236
237      EffectConst(const String& name, const int& val)
238         : mName(name),
239         mHandle(NULL),
240         mDirty(true)
241      {
242         set(val);
243      }
244
245      EffectConst(const String &name, const Point4F &val)
246         : mName(name),
247         mHandle(NULL),
248         mDirty(true)
249      {
250         set(val);
251      }
252
253      EffectConst(const String &name, const MatrixF &val)
254         : mName(name),
255         mHandle(NULL),
256         mDirty(true)
257      {
258         set(val);
259      }
260
261      EffectConst(const String &name, const Vector<Point4F> &val)
262         : mName(name),
263         mHandle(NULL),
264         mDirty(true)
265      {
266         set(val);
267      }
268
269      EffectConst(const String &name, const Vector<MatrixF> &val)
270         : mName(name),
271         mHandle(NULL),
272         mDirty(true)
273      {
274         set(val);
275      }
276
277      void set( const String &newVal );
278      void set(const F32 &newVal);
279      void set(const int& newVal);
280      void set(const Point4F &newVal);
281      void set(const MatrixF &newVal);
282      void set(const Vector<Point4F> &newVal);
283      void set(const Vector<MatrixF> &newVal);
284
285      void setToBuffer( GFXShaderConstBufferRef buff );
286
287      String mName;
288
289      GFXShaderConstHandle *mHandle;
290
291      String mStringVal;
292
293      S32     mIntVal;
294      F32     mFloatVal;
295      Point4F mPointVal;
296      MatrixF mMatrixVal;
297
298      Vector<Point4F> mPointArrayVal;
299      Vector<MatrixF> mMatrixArrayVal;
300
301      enum
302      {
303         StringType,
304         IntType,
305         FloatType,
306         PointType,
307         MatrixType,
308         PointArrayType,
309         MatrixArrayType
310      } mValueType;
311
312      bool mDirty;
313   };
314
315   typedef HashTable<StringCase,EffectConst*> EffectConstTable;
316
317   EffectConstTable mEffectConsts;
318
319   ///
320   virtual void _updateScreenGeometry( const Frustum &frustum,
321                                       GFXVertexBufferHandle<PFXVertex> *outVB );
322
323   ///
324   virtual void _setupStateBlock( const SceneRenderState *state );
325
326   /// 
327   virtual void _setupConstants( const SceneRenderState *state );
328
329   ///
330   virtual void _setupTransforms();
331
332   ///
333   virtual void _setupTarget( const SceneRenderState *state, bool *outClearTarget );
334
335   ///
336   virtual void _setupTexture( U32 slot, GFXTexHandle &inputTex, const RectI *inTexViewport );
337   virtual void _setupCubemapTexture(U32 stage, GFXCubemapHandle &inputTex);
338   virtual void _setupCubemapArrayTexture(U32 slot, GFXCubemapArrayHandle &inputTex);
339
340
341   /// Protected set method for toggling the enabled state.
342   static bool _setIsEnabled( void *object, const char *index, const char *data );
343
344   /// Called from the light manager activate signal.
345   /// @see LightManager::addActivateCallback
346   void _onLMActivate( const char*, bool activate )
347   {
348      if ( activate ) 
349         mUpdateShader = true; 
350   }
351
352   /// We handle texture events to release named rendered targets.
353   /// @see GFXTextureManager::addEventDelegate
354   void _onTextureEvent( GFXTexCallbackCode code )
355   {
356      if ( code == GFXZombify && (mNamedTarget.isRegistered() || mNamedTargetDepthStencil.isRegistered()) )
357         _cleanTargets();
358   }
359
360   ///
361   void _updateConditioners();
362
363   ///
364   void _cleanTargets( bool recurse = false );
365
366   /// 
367   void _checkRequirements();
368
369   ///
370   GFXTextureObject* _getTargetTexture( U32 index );
371
372public:
373
374   /// Constructor.
375   PostEffect();
376
377   /// Destructor.
378   virtual ~PostEffect();
379
380   DECLARE_CONOBJECT(PostEffect);
381
382   // SimObject
383   virtual bool onAdd();
384   virtual void onRemove();
385   static void initPersistFields();
386
387   /// @name Callbacks
388   /// @{
389
390   DECLARE_CALLBACK( void, onAdd, () );
391   DECLARE_CALLBACK( void, preProcess, () );
392   DECLARE_CALLBACK( void, setShaderConsts, () );
393   DECLARE_CALLBACK( bool, onEnabled, () );
394   DECLARE_CALLBACK( void, onDisabled, () );
395
396   /// @}
397
398   virtual void process(   const SceneRenderState *state, 
399                           GFXTexHandle &inOutTex,
400                           const RectI *inTexViewport = NULL );
401
402   /// 
403   void reload();
404
405   /// 
406   void enable();
407
408   /// 
409   void disable();
410
411   /// Dump the shader disassembly to a temporary text file.
412   /// Returns true and sets outFilename to the file if successful.
413   bool dumpShaderDisassembly( String &outFilename ) const;
414
415   /// Returns the SimSet which contains all PostEffects.
416   SimSet* getSet() const;
417
418   ///
419   bool isEnabled() const { return mEnabled; }
420
421   /// Is set to skip rendering.
422   bool isSkipped() const { return mSkip; }
423
424   /// Set the effect to skip rendering.
425   void setSkip( bool skip ) { mSkip = skip; }
426
427   PFXRenderTime getRenderTime() const { return mRenderTime; }
428
429   const String& getRenderBin() const { return mRenderBin; }
430
431   F32 getPriority() const { return mRenderPriority; }
432
433   void setTexture( U32 index, const String &filePath );
434   void setTexture(U32 index, const GFXTexHandle& texHandle);
435   void setCubemapTexture(U32 index, const GFXCubemapHandle &cubemapHandle);
436   void setCubemapArrayTexture(U32 index, const GFXCubemapArrayHandle &cubemapArrayHandle);
437
438   void setShaderMacro( const String &name, const String &value = String::EmptyString );
439   bool removeShaderMacro( const String &name );
440   void clearShaderMacros();
441
442   ///
443   void setShaderConst( const String &name, const String &val );   
444   void setShaderConst(const String &name, const F32 &val);
445   void setShaderConst(const String& name, const int& val);
446   void setShaderConst(const String &name, const Point4F &val);
447   void setShaderConst(const String &name, const MatrixF &val);
448   void setShaderConst(const String &name, const Vector<Point4F> &val);
449   void setShaderConst(const String &name, const Vector<MatrixF> &val);
450
451   void setOnThisFrame( bool enabled ) { mOnThisFrame = enabled; }
452   bool isOnThisFrame() { return mOnThisFrame; }
453   void setOneFrameOnly( bool enabled ) { mOneFrameOnly = enabled; }
454   bool isOneFrameOnly() { return mOneFrameOnly; }   
455
456   F32 getAspectRatio() const;
457
458   GFXShaderRef getShader() { return mShader; }
459   Vector<GFXShaderMacro>* getShaderMacros() { return &mShaderMacros; }
460   GFXShaderConstBufferRef getShaderConstBuffer() { return mShaderConsts; }
461   
462
463   enum PostEffectRequirements
464   {
465      RequiresDepth      = BIT(0),
466      RequiresNormals    = BIT(1),
467      RequiresLightInfo  = BIT(2),
468   };
469};
470
471#endif // _POST_EFFECT_H_
472