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