2
0

probeManager.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 PROBEMANAGER_H
  23. #define PROBEMANAGER_H
  24. #ifndef _TORQUE_STRING_H_
  25. #include "core/util/str.h"
  26. #endif
  27. #ifndef _TSIGNAL_H_
  28. #include "core/util/tSignal.h"
  29. #endif
  30. #ifndef _LIGHTINFO_H_
  31. #include "lighting/lightInfo.h"
  32. #endif
  33. #ifndef _LIGHTQUERY_H_
  34. #include "lighting/lightQuery.h"
  35. #endif
  36. #ifndef _MATRIXSET_H_
  37. #include "math/util/matrixSet.h"
  38. #endif
  39. #ifndef _CUBEMAPDATA_H_
  40. #include "gfx/sim/cubemapData.h"
  41. #endif
  42. #ifndef _MATINSTANCE_H_
  43. #include "materials/matInstance.h"
  44. #endif
  45. #ifndef _MATTEXTURETARGET_H_
  46. #include "materials/matTextureTarget.h"
  47. #endif
  48. #ifndef _GFXPRIMITIVEBUFFER_H_
  49. #include "gfx/gfxPrimitiveBuffer.h"
  50. #endif
  51. #ifndef _GFXVERTEXBUFFER_H_
  52. #include "gfx/gfxVertexBuffer.h"
  53. #endif
  54. #include "core/util/SystemInterfaceList.h"
  55. class SimObject;
  56. class ProbeManager;
  57. class Material;
  58. class ProcessedMaterial;
  59. class SceneManager;
  60. struct SceneData;
  61. class Point3F;
  62. class AvailableSLInterfaces;
  63. class SceneObject;
  64. class GFXShaderConstBuffer;
  65. class GFXShaderConstHandle;
  66. class ShaderConstHandles;
  67. class SceneRenderState;
  68. class RenderDeferredMgr;
  69. class Frustum;
  70. struct ProbeRenderInst : public SystemInterface<ProbeRenderInst>
  71. {
  72. LinearColorF mAmbient;
  73. MatrixF mTransform;
  74. F32 mRadius;
  75. F32 mIntensity;
  76. bool mDirty;
  77. Box3F mBounds;
  78. Point3F mProbePosOffset;
  79. GFXCubemapHandle *mCubemap;
  80. GFXCubemapHandle *mIrradianceCubemap;
  81. GFXTexHandle *mBRDFTexture;
  82. /// The priority of this light used for
  83. /// light and shadow scoring.
  84. F32 mPriority;
  85. /// A temporary which holds the score used
  86. /// when prioritizing lights for rendering.
  87. F32 mScore;
  88. bool mIsSkylight;
  89. /// Whether to render debugging visualizations
  90. /// for this light.
  91. bool mDebugRender;
  92. GFXPrimitiveBufferHandle primBuffer;
  93. GFXVertexBufferHandle<GFXVertexPC> vertBuffer;
  94. U32 numPrims;
  95. U32 numVerts;
  96. Vector< U32 > numIndicesForPoly;
  97. enum ProbeShapeType
  98. {
  99. Sphere = 0, ///< Sphere shaped
  100. Box = 1, ///< Box-based shape
  101. };
  102. ProbeShapeType mProbeShapeType;
  103. //Spherical Harmonics data
  104. LinearColorF mSHTerms[9];
  105. F32 mSHConstants[5];
  106. public:
  107. ProbeRenderInst();
  108. ~ProbeRenderInst();
  109. // Copies data passed in from light
  110. void set(const ProbeRenderInst *probeInfo);
  111. // Accessors
  112. const MatrixF& getTransform() const { return mTransform; }
  113. void setTransform(const MatrixF &xfm) { mTransform = xfm; }
  114. Point3F getPosition() const { return mTransform.getPosition(); }
  115. void setPosition(const Point3F &pos) { mTransform.setPosition(pos); }
  116. VectorF getDirection() const { return mTransform.getForwardVector(); }
  117. void setDirection(const VectorF &val);
  118. const LinearColorF& getAmbient() const { return mAmbient; }
  119. void setAmbient(const LinearColorF &val) { mAmbient = val; }
  120. void setPriority(F32 priority) { mPriority = priority; }
  121. F32 getPriority() const { return mPriority; }
  122. void setScore(F32 score) { mScore = score; }
  123. F32 getScore() const { return mScore; }
  124. bool isDebugRenderingEnabled() const { return mDebugRender; }
  125. void enableDebugRendering(bool value) { mDebugRender = value; }
  126. // Builds the world to light view projection used for
  127. // shadow texture and cookie lookups.
  128. void getWorldToLightProj(MatrixF *outMatrix) const;
  129. void clear();
  130. };
  131. struct ProbeShaderConstants
  132. {
  133. bool mInit;
  134. GFXShaderRef mShader;
  135. GFXShaderConstHandle* mProbeParamsSC;
  136. //Reflection Probes
  137. GFXShaderConstHandle *mProbePositionSC;
  138. GFXShaderConstHandle *mProbeRadiusSC;
  139. GFXShaderConstHandle *mProbeBoxMinSC;
  140. GFXShaderConstHandle *mProbeBoxMaxSC;
  141. GFXShaderConstHandle *mProbeIsSphereSC;
  142. GFXShaderConstHandle *mProbeLocalPosSC;
  143. GFXShaderConstHandle *mProbeCubemapSC;
  144. ProbeShaderConstants();
  145. ~ProbeShaderConstants();
  146. void init(GFXShader* buffer);
  147. void _onShaderReload();
  148. };
  149. typedef Map<GFXShader*, ProbeShaderConstants*> ProbeConstantMap;
  150. class ReflectProbeMatInstance : public MatInstance
  151. {
  152. typedef MatInstance Parent;
  153. protected:
  154. MaterialParameterHandle * mProbeParamsSC;
  155. bool mInternalPass;
  156. GFXStateBlockRef mProjectionState;
  157. public:
  158. ReflectProbeMatInstance(Material &mat) : Parent(mat), mProbeParamsSC(NULL), mInternalPass(false), mProjectionState(NULL) {}
  159. virtual bool init(const FeatureSet &features, const GFXVertexFormat *vertexFormat);
  160. virtual bool setupPass(SceneRenderState *state, const SceneData &sgData);
  161. };
  162. class SkylightMatInstance : public ReflectProbeMatInstance
  163. {
  164. typedef ReflectProbeMatInstance Parent;
  165. public:
  166. SkylightMatInstance(Material &mat) : Parent(mat) {}
  167. };
  168. class ProbeManager
  169. {
  170. public:
  171. struct ReflectProbeMaterialInfo
  172. {
  173. ReflectProbeMatInstance *matInstance;
  174. // { zNear, zFar, 1/zNear, 1/zFar }
  175. MaterialParameterHandle *zNearFarInvNearFar;
  176. // Far frustum plane (World Space)
  177. MaterialParameterHandle *farPlane;
  178. // Far frustum plane (View Space)
  179. MaterialParameterHandle *vsFarPlane;
  180. // -dot( farPlane, eyePos )
  181. MaterialParameterHandle *negFarPlaneDotEye;
  182. // Light Parameters
  183. MaterialParameterHandle *probeLSPos;
  184. MaterialParameterHandle *probeWSPos;
  185. MaterialParameterHandle *attenuation;
  186. MaterialParameterHandle *radius;
  187. MaterialParameterHandle *useCubemap;
  188. MaterialParameterHandle *cubemap;
  189. MaterialParameterHandle *cubeMips;
  190. MaterialParameterHandle *eyePosWorld;
  191. MaterialParameterHandle *bbMin;
  192. MaterialParameterHandle *bbMax;
  193. MaterialParameterHandle *useSphereMode;
  194. MaterialParameterHandle *shTerms[9];
  195. MaterialParameterHandle *shConsts[5];
  196. ReflectProbeMaterialInfo(const String &matName, const GFXVertexFormat *vertexFormat);
  197. virtual ~ReflectProbeMaterialInfo();
  198. void setViewParameters(const F32 zNear,
  199. const F32 zFar,
  200. const Point3F &eyePos,
  201. const PlaneF &farPlane,
  202. const PlaneF &_vsFarPlane);
  203. void setProbeParameters(const ProbeRenderInst *probe, const SceneRenderState* renderState, const MatrixF &worldViewOnly);
  204. };
  205. struct SkylightMaterialInfo : public ReflectProbeMaterialInfo
  206. {
  207. SkylightMaterialInfo(const String &matName, const GFXVertexFormat *vertexFormat);
  208. virtual ~SkylightMaterialInfo();
  209. };
  210. enum SpecialProbeTypesEnum
  211. {
  212. SkylightProbeType,
  213. SpecialProbeTypesCount
  214. };
  215. Vector<U32> mRegisteredProbes;
  216. ProbeManager();
  217. ~ProbeManager();
  218. ///
  219. static ProbeRenderInst* createProbeInfo(ProbeRenderInst* light = NULL);
  220. void registerProbe(U32 probeIdx);
  221. /// The light manager activation signal.
  222. static Signal<void(const char*,bool)> smActivateSignal;
  223. /// Returns the active LM.
  224. static inline ProbeManager* getProbeManager();
  225. // Returns the scene manager passed at activation.
  226. SceneManager* getSceneManager() { return mSceneManager; }
  227. // Called when the lighting manager should become active
  228. virtual void activate( SceneManager *sceneManager );
  229. // Called when we don't want the light manager active (should clean up)
  230. virtual void deactivate();
  231. // Returns the active scene lighting interface for this light manager.
  232. virtual AvailableSLInterfaces* getSceneLightingInterface();
  233. void updateDirtyProbes();
  234. /*// Returns a "default" light info that callers should not free. Used for instances where we don't actually care about
  235. // the light (for example, setting default data for SceneData)
  236. virtual ProbeInfo* getDefaultLight();
  237. /// Returns the special light or the default light if useDefault is true.
  238. /// @see getDefaultLight
  239. virtual ProbeInfo* getSpecialProbe(SpecialProbeTypesEnum type,
  240. bool useDefault = true );
  241. /// Set a special light type.
  242. virtual void setSpecialProbe(SpecialProbeTypesEnum type, ProbeInfo *light );*/
  243. /*void registerSkylight(ProbeInfo *probe, SimObject *obj);
  244. // registered before scene traversal...
  245. virtual void registerProbe(ProbeInfo *light, SimObject *obj );
  246. virtual void unregisterProbe(ProbeInfo *light );
  247. virtual void registerProbes( const Frustum *frustum, bool staticlighting );
  248. virtual void unregisterAllProbes();
  249. /// Returns all unsorted and un-scored lights (both global and local).
  250. void getAllUnsortedProbes( Vector<ProbeInfo*> *list ) const;*/
  251. /// Sets shader constants / textures for light infos
  252. virtual void setProbeInfo( ProcessedMaterial *pmat,
  253. const Material *mat,
  254. const SceneData &sgData,
  255. const SceneRenderState *state,
  256. U32 pass,
  257. GFXShaderConstBuffer *shaderConsts );
  258. /// Allows us to set textures during the Material::setTextureStage call, return true if we've done work.
  259. virtual bool setTextureStage( const SceneData &sgData,
  260. const U32 currTexFlag,
  261. const U32 textureSlot,
  262. GFXShaderConstBuffer *shaderConsts,
  263. ShaderConstHandles *handles );
  264. ReflectProbeMaterialInfo* getReflectProbeMaterial();
  265. SkylightMaterialInfo* getSkylightMaterial();
  266. protected:
  267. /// The current active light manager.
  268. static ProbeManager *smProbeManager;
  269. /// Find the pre-pass render bin on the scene's default render pass.
  270. RenderDeferredMgr* _findDeferredRenderBin();
  271. public:
  272. ProbeShaderConstants* getProbeShaderConstants(GFXShaderConstBuffer* buffer);
  273. // Add a reflection probe to the bin
  274. void setupSkylightProbe(ProbeRenderInst *probeInfo);
  275. void setupSphereReflectionProbe(ProbeRenderInst *probeInfo);
  276. void setupConvexReflectionProbe(ProbeRenderInst *probeInfo);
  277. /// Debug rendering
  278. static bool smRenderReflectionProbes;
  279. protected:
  280. /// This helper function sets the shader constansts
  281. /// for the stock 4 light forward lighting code.
  282. void _update4ProbeConsts( const SceneData &sgData,
  283. MatrixSet &matSet,
  284. GFXShaderConstHandle *probePositionSC,
  285. GFXShaderConstHandle *probeRadiusSC,
  286. GFXShaderConstHandle *probeBoxMinSC,
  287. GFXShaderConstHandle *probeBoxMaxSC,
  288. GFXShaderConstHandle *probeCubemapSC,
  289. GFXShaderConstHandle *probeIsSphereSC,
  290. GFXShaderConstHandle *probeLocalPosSC,
  291. GFXShaderConstBuffer *shaderConsts );
  292. /// The root culling position used for
  293. /// special sun light placement.
  294. /// @see setSpecialLight
  295. Point3F mCullPos;
  296. ///
  297. //virtual void _initLightFields();
  298. /// The scene graph the light manager is associated with.
  299. SceneManager *mSceneManager;
  300. ProbeConstantMap mConstantLookup;
  301. GFXShaderRef mLastShader;
  302. ProbeShaderConstants* mLastConstants;
  303. // Convex geometry for lights
  304. GFXVertexBufferHandle<GFXVertexPC> mSphereGeometry;
  305. GFXPrimitiveBufferHandle mSphereIndices;
  306. U32 mSpherePrimitiveCount;
  307. ReflectProbeMaterialInfo* mReflectProbeMaterial;
  308. SkylightMaterialInfo* mSkylightMaterial;
  309. GFXVertexBufferHandle<GFXVertexPC> getSphereMesh(U32 &outNumPrimitives, GFXPrimitiveBufferHandle &outPrimitives);;
  310. };
  311. ProbeManager* ProbeManager::getProbeManager()
  312. {
  313. if (smProbeManager == nullptr)
  314. {
  315. ProbeManager* probeManager = new ProbeManager();
  316. if (gClientSceneGraph != nullptr)
  317. {
  318. probeManager->activate(gClientSceneGraph);
  319. }
  320. else
  321. {
  322. delete probeManager;
  323. }
  324. }
  325. return smProbeManager;
  326. }
  327. /// Returns the current active light manager.
  328. #define PROBEMGR ProbeManager::getProbeManager()
  329. #endif // PROBEMANAGER_H