Torque3D Documentation / _generateds / renderProbeMgr.h

renderProbeMgr.h

Engine/source/renderInstance/renderProbeMgr.h

More...

Classes:

Public Defines

define
PROBEMGR() ()

Public Typedefs

ProbeConstantMap 

Public Variables

Detailed Description

Public Defines

PROBEMGR() ()
RENDER_PROBE_MGR_H() 

Public Typedefs

typedef Map< GFXShader *, ProbeShaderConstants * > ProbeConstantMap 

Public Variables

U32 MAXPROBECOUNT 
  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#pragma once
 24#ifndef RENDER_PROBE_MGR_H
 25#define RENDER_PROBE_MGR_H
 26
 27#ifndef _RENDERBINMANAGER_H_
 28#include "renderInstance/renderBinManager.h"
 29#endif
 30#ifndef _MATINSTANCE_H_
 31#include "materials/matInstance.h"
 32#endif
 33#ifndef _MATTEXTURETARGET_H_
 34#include "materials/matTextureTarget.h"
 35#endif
 36#ifndef _GFXPRIMITIVEBUFFER_H_
 37#include "gfx/gfxPrimitiveBuffer.h"
 38#endif
 39#ifndef _GFXVERTEXBUFFER_H_
 40#include "gfx/gfxVertexBuffer.h"
 41#endif
 42
 43#include "core/util/systemInterfaceList.h"
 44
 45#ifndef _MATERIALS_PROCESSEDSHADERMATERIAL_H_
 46#include "materials/processedShaderMaterial.h"
 47#endif
 48#ifndef _POSTEFFECTCOMMON_H_
 49#include "postFx/postEffectCommon.h"
 50#endif
 51
 52#ifndef _REFLECTOR_H_
 53#include "scene/reflector.h"
 54#endif
 55
 56static U32 MAXPROBECOUNT = 50;
 57
 58class PostEffect;
 59class ReflectionProbe;
 60
 61struct ProbeRenderInst
 62{
 63   bool mIsEnabled;
 64
 65   MatrixF mTransform;
 66
 67   F32 mRadius;
 68
 69   bool mDirty;
 70
 71   Box3F mBounds;
 72   Point3F mExtents;
 73   Point3F mPosition;
 74   Point3F mProbeRefOffset;
 75   Point3F mProbeRefScale;
 76   F32 mAtten;
 77
 78   GFXCubemapHandle mPrefilterCubemap;
 79   GFXCubemapHandle mIrradianceCubemap;
 80
 81   /// The priority of this light used for
 82   /// light and shadow scoring.
 83   F32 mPriority;
 84
 85   /// A temporary which holds the score used
 86   /// when prioritizing lights for rendering.
 87   F32 mScore;
 88
 89   enum ProbeShapeType
 90   {
 91      Box = 0,            ///< Sphere shaped
 92      Sphere = 1,               ///< Box-based shape
 93      Skylight = 2
 94   };
 95
 96   ProbeShapeType mProbeShapeType;
 97
 98   U32 mCubemapIndex;
 99
100   U32 mProbeIdx;
101
102public:
103
104   ProbeRenderInst();
105   ~ProbeRenderInst();
106
107   // Copies data passed in from light
108   void set(const ProbeRenderInst *probeInfo);
109
110   // Accessors
111   const MatrixF& getTransform() const { return mTransform; }
112   void setTransform(const MatrixF &xfm) { mTransform = xfm; }
113
114   Point3F getPosition() const { return mPosition; }
115   void setPosition(const Point3F &pos) { mPosition = pos; }
116
117   void setPriority(F32 priority) { mPriority = priority; }
118   F32 getPriority() const { return mPriority; }
119
120   void setScore(F32 score) { mScore = score; }
121   F32 getScore() const { return mScore; }
122
123   void clear();
124
125   inline bool operator==(const ProbeRenderInst& b) const
126   {
127      return mProbeIdx == b.mProbeIdx;
128   }
129};
130
131struct ProbeShaderConstants
132{
133   bool mInit;
134
135   GFXShaderRef mShader;
136   
137   //Reflection Probes
138   GFXShaderConstHandle *mProbePositionSC;
139   GFXShaderConstHandle *mProbeRefPosSC;
140   GFXShaderConstHandle *mRefScaleSC;
141   GFXShaderConstHandle *mWorldToObjArraySC;
142   GFXShaderConstHandle *mProbeConfigDataSC;
143   GFXShaderConstHandle *mProbeSpecularCubemapSC;
144   GFXShaderConstHandle *mProbeIrradianceCubemapSC;
145   GFXShaderConstHandle *mProbeCountSC;
146
147   GFXShaderConstHandle *mBRDFTextureMap;
148
149   GFXShaderConstHandle *mSkylightCubemapIdxSC;
150
151   ProbeShaderConstants();
152   ~ProbeShaderConstants();
153
154   void init(GFXShader* buffer);
155
156   bool isValid();
157
158   void _onShaderReload();
159};
160
161typedef Map<GFXShader*, ProbeShaderConstants*> ProbeConstantMap;
162
163struct ProbeDataSet
164{
165   Vector<Point4F> probePositionArray;
166   Vector<Point4F> refScaleArray;
167   Vector<Point4F> probeRefPositionArray;
168   Vector<Point4F> probeConfigArray;
169
170   Vector<MatrixF> probeWorldToObjArray;
171
172   S32 skyLightIdx;
173
174   U32 effectiveProbeCount;
175   U32 maxProbeCount;
176
177   ProbeDataSet()
178   {
179      probePositionArray.setSize(0);
180      refScaleArray.setSize(0);
181      probeRefPositionArray.setSize(0);
182      probeConfigArray.setSize(0);
183
184      probeWorldToObjArray.setSize(0);
185
186      skyLightIdx = -1;
187      effectiveProbeCount = 0;
188      maxProbeCount = 0;
189   }
190
191   ProbeDataSet(U32 _maxProbeCount)
192   {
193      maxProbeCount = _maxProbeCount;
194
195      probePositionArray.setSize(maxProbeCount);
196      refScaleArray.setSize(maxProbeCount);
197      probeRefPositionArray.setSize(maxProbeCount);
198      probeConfigArray.setSize(maxProbeCount);
199
200      probeWorldToObjArray.setSize(maxProbeCount);
201
202      skyLightIdx = -1;
203      effectiveProbeCount = 0;
204   }
205};
206
207struct ProbeTextureArrayData
208{
209   GFXTexHandle BRDFTexture;
210   GFXCubemapArrayHandle prefilterArray;
211   GFXCubemapArrayHandle irradianceArray;
212};
213
214//**************************************************************************
215// RenderObjectMgr
216//**************************************************************************
217class RenderProbeMgr : public RenderBinManager
218{
219   typedef RenderBinManager Parent;
220
221   Vector<ProbeRenderInst*> mRegisteredProbes;
222
223   bool mProbesDirty;
224   
225   Vector<ProbeRenderInst>  mActiveProbes;
226public:
227   //maximum number of allowed probes
228   static const U32 PROBE_MAX_COUNT = 250;
229   //maximum number of rendered probes per frame adjust as needed
230   static const U32 PROBE_MAX_FRAME = 8;
231   //number of slots to allocate at once in the cubemap array
232   static const U32 PROBE_ARRAY_SLOT_BUFFER_SIZE = 10;
233
234   static const U32 PROBE_IRRAD_SIZE = 64;
235   static const U32 PROBE_PREFILTER_SIZE = 64;
236   static const GFXFormat PROBE_FORMAT = GFXFormatR16G16B16A16F;// GFXFormatR8G8B8A8;// when hdr fixed GFXFormatR16G16B16A16F; look into bc6h compression
237   static const U32 INVALID_CUBE_SLOT = U32_MAX;
238
239   static F32 smMaxProbeDrawDistance;
240   static S32 smMaxProbesPerFrame;
241
242private:
243   //Array rendering
244   U32 mEffectiveProbeCount;
245   S32 mMipCount;
246
247   bool            mHasSkylight;
248   S32             mSkylightCubemapIdx;
249
250   //number of cubemaps
251   U32 mCubeMapCount;
252   //number of cubemap slots allocated
253   U32 mCubeSlotCount;
254   //array of cubemap slots, due to the editor these may be mixed around as probes are added and deleted
255   bool mCubeMapSlots[PROBE_MAX_COUNT];
256
257   GFXCubemapArrayHandle mPrefilterArray;
258   GFXCubemapArrayHandle mIrradianceArray;
259
260   //Utilized in forward rendering
261   ProbeConstantMap mConstantLookup;
262   GFXShaderRef mLastShader;
263   ProbeShaderConstants* mLastConstants;
264
265   //
266   SimObjectPtr<PostEffect> mProbeArrayEffect;
267
268   //Default skylight, used for shape editors, etc
269   ProbeRenderInst* mDefaultSkyLight;
270
271   GFXTexHandle mBRDFTexture;
272
273   ProbeDataSet mProbeData;
274   ///Prevents us from saving out the cubemaps(for now) but allows us the full HDR range on the in-memory cubemap captures
275   bool mUseHDRCaptures;
276
277   U32 mPrefilterMipLevels;
278   U32 mPrefilterSize;
279public:
280   RenderProbeMgr();
281   RenderProbeMgr(RenderInstType riType, F32 renderOrder, F32 processAddOrder);
282   virtual ~RenderProbeMgr();
283   virtual bool onAdd();
284   virtual void onRemove();
285
286   // ConsoleObject
287   static void initPersistFields();
288   static void consoleInit();
289
290   DECLARE_CONOBJECT(RenderProbeMgr);
291
292protected:
293   /// The current active light manager.
294   static RenderProbeMgr *smProbeManager;
295
296   /// This helper function sets the shader constansts
297   /// for the stock 4 light forward lighting code.
298   void _update4ProbeConsts(const SceneData &sgData,
299      MatrixSet &matSet,
300      ProbeShaderConstants *probeShaderConsts,
301      GFXShaderConstBuffer *shaderConsts);
302
303   void _setupStaticParameters();
304   void _setupPerFrameParameters(const SceneRenderState *state);
305   virtual void addElement(RenderInst* inst) {};
306   virtual void render(SceneRenderState * state);
307
308   ProbeShaderConstants* getProbeShaderConstants(GFXShaderConstBuffer* buffer);
309
310   PostEffect* getProbeArrayEffect();
311
312
313   U32 _findNextEmptyCubeSlot()
314   {
315      for (U32 i = 0; i < PROBE_MAX_COUNT; i++)
316      {
317         if (!mCubeMapSlots[i])
318            return i;
319      }
320      return INVALID_CUBE_SLOT;
321   }
322
323public:
324   // RenderBinMgr
325   void updateProbes();
326
327   /// Returns the active LM.
328   static inline RenderProbeMgr* getProbeManager();
329
330   void registerProbe(ProbeRenderInst* newProbe);
331   void unregisterProbe(U32 probeIdx);
332   void submitProbe(const ProbeRenderInst& newProbe);
333
334   static S32 QSORT_CALLBACK _probeScoreCmp(const ProbeRenderInst* a, const  ProbeRenderInst* b);
335
336   virtual void setProbeInfo(ProcessedMaterial *pmat,
337      const Material *mat,
338      const SceneData &sgData,
339      const SceneRenderState *state,
340      U32 pass,
341      GFXShaderConstBuffer *shaderConsts);
342
343   void setupSGData(SceneData& data, const SceneRenderState* state, LightInfo* light);
344   
345   void updateProbeTexture(ProbeRenderInst* probeInfo);
346
347   void reloadTextures();
348
349   /// Debug rendering
350   static bool smRenderReflectionProbes;
351
352   void bakeProbe(ReflectionProbe *probeInfo);
353   void bakeProbes();
354
355   void getProbeTextureData(ProbeTextureArrayData* probeTextureSet);
356   S32 getSkylightIndex() { return mSkylightCubemapIdx; }
357   //accumulates the best fit of probes given the object position
358   void getBestProbes(const Point3F& objPosition, ProbeDataSet* probeDataSet);
359};
360
361RenderProbeMgr* RenderProbeMgr::getProbeManager()
362{
363   if (smProbeManager == nullptr)
364   {
365      RenderProbeMgr* probeManager = new RenderProbeMgr();
366
367      smProbeManager = probeManager;
368   }
369
370   return smProbeManager;
371}
372
373#define PROBEMGR RenderProbeMgr::getProbeManager()
374
375#endif // RENDER_PROBE_MGR_H
376