renderProbeMgr.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. #pragma once
  23. #ifndef RENDER_PROBE_MGR_H
  24. #define RENDER_PROBE_MGR_H
  25. #ifndef _RENDERBINMANAGER_H_
  26. #include "renderInstance/renderBinManager.h"
  27. #endif
  28. #ifndef _MATINSTANCE_H_
  29. #include "materials/matInstance.h"
  30. #endif
  31. #ifndef _MATTEXTURETARGET_H_
  32. #include "materials/matTextureTarget.h"
  33. #endif
  34. #ifndef _GFXPRIMITIVEBUFFER_H_
  35. #include "gfx/gfxPrimitiveBuffer.h"
  36. #endif
  37. #ifndef _GFXVERTEXBUFFER_H_
  38. #include "gfx/gfxVertexBuffer.h"
  39. #endif
  40. #include "core/util/SystemInterfaceList.h"
  41. #ifndef _MATERIALS_PROCESSEDSHADERMATERIAL_H_
  42. #include "materials/processedShaderMaterial.h"
  43. #endif
  44. #ifndef _POSTEFFECTCOMMON_H_
  45. #include "postFx/postEffectCommon.h"
  46. #endif
  47. static U32 MAXPROBECOUNT = 50;
  48. class PostEffect;
  49. struct ProbeRenderInst : public SystemInterface<ProbeRenderInst>
  50. {
  51. LinearColorF mAmbient;
  52. MatrixF mTransform;
  53. F32 mRadius;
  54. F32 mIntensity;
  55. bool mDirty;
  56. Box3F mBounds;
  57. Point3F mProbePosOffset;
  58. GFXCubemapHandle mCubemap;
  59. GFXCubemapHandle mIrradianceCubemap;
  60. GFXTexHandle *mBRDFTexture;
  61. /// The priority of this light used for
  62. /// light and shadow scoring.
  63. F32 mPriority;
  64. /// A temporary which holds the score used
  65. /// when prioritizing lights for rendering.
  66. F32 mScore;
  67. bool mIsSkylight;
  68. /// Whether to render debugging visualizations
  69. /// for this light.
  70. bool mDebugRender;
  71. GFXPrimitiveBufferHandle primBuffer;
  72. GFXVertexBufferHandle<GFXVertexPC> vertBuffer;
  73. U32 numPrims;
  74. U32 numVerts;
  75. Vector< U32 > numIndicesForPoly;
  76. enum ProbeShapeType
  77. {
  78. Sphere = 0, ///< Sphere shaped
  79. Box = 1, ///< Box-based shape
  80. };
  81. ProbeShapeType mProbeShapeType;
  82. //Spherical Harmonics data
  83. LinearColorF mSHTerms[9];
  84. F32 mSHConstants[5];
  85. public:
  86. ProbeRenderInst();
  87. ~ProbeRenderInst();
  88. // Copies data passed in from light
  89. void set(const ProbeRenderInst *probeInfo);
  90. // Accessors
  91. const MatrixF& getTransform() const { return mTransform; }
  92. void setTransform(const MatrixF &xfm) { mTransform = xfm; }
  93. Point3F getPosition() const { return mTransform.getPosition(); }
  94. void setPosition(const Point3F &pos) { mTransform.setPosition(pos); }
  95. VectorF getDirection() const { return mTransform.getForwardVector(); }
  96. void setDirection(const VectorF &val);
  97. const LinearColorF& getAmbient() const { return mAmbient; }
  98. void setAmbient(const LinearColorF &val) { mAmbient = val; }
  99. void setPriority(F32 priority) { mPriority = priority; }
  100. F32 getPriority() const { return mPriority; }
  101. void setScore(F32 score) { mScore = score; }
  102. F32 getScore() const { return mScore; }
  103. bool isDebugRenderingEnabled() const { return mDebugRender; }
  104. void enableDebugRendering(bool value) { mDebugRender = value; }
  105. // Builds the world to light view projection used for
  106. // shadow texture and cookie lookups.
  107. void getWorldToLightProj(MatrixF *outMatrix) const;
  108. void clear();
  109. };
  110. struct ProbeShaderConstants
  111. {
  112. bool mInit;
  113. GFXShaderRef mShader;
  114. GFXShaderConstHandle* mProbeParamsSC;
  115. //Reflection Probes
  116. GFXShaderConstHandle *mProbePositionSC;
  117. GFXShaderConstHandle *mProbeRadiusSC;
  118. GFXShaderConstHandle *mProbeBoxMinSC;
  119. GFXShaderConstHandle *mProbeBoxMaxSC;
  120. GFXShaderConstHandle *mProbeIsSphereSC;
  121. GFXShaderConstHandle *mProbeLocalPosSC;
  122. GFXShaderConstHandle *mProbeCubemapSC;
  123. GFXShaderConstHandle *mProbeCountSC;
  124. ProbeShaderConstants();
  125. ~ProbeShaderConstants();
  126. void init(GFXShader* buffer);
  127. void _onShaderReload();
  128. };
  129. typedef Map<GFXShader*, ProbeShaderConstants*> ProbeConstantMap;
  130. class ReflectProbeMatInstance : public MatInstance
  131. {
  132. typedef MatInstance Parent;
  133. protected:
  134. MaterialParameterHandle * mProbeParamsSC;
  135. bool mInternalPass;
  136. GFXStateBlockRef mProjectionState;
  137. ProcessedShaderMaterial* mShaderMat;
  138. public:
  139. ReflectProbeMatInstance(Material &mat) : Parent(mat), mProbeParamsSC(NULL), mInternalPass(false), mProjectionState(NULL) {}
  140. virtual bool init(const FeatureSet &features, const GFXVertexFormat *vertexFormat);
  141. virtual bool setupPass(SceneRenderState *state, const SceneData &sgData);
  142. ProcessedShaderMaterial* getProcessedShaderMaterial() { return mShaderMat; }
  143. };
  144. //**************************************************************************
  145. // RenderObjectMgr
  146. //**************************************************************************
  147. class RenderProbeMgr : public RenderBinManager
  148. {
  149. typedef RenderBinManager Parent;
  150. struct ReflectProbeMaterialInfo
  151. {
  152. ReflectProbeMatInstance *matInstance;
  153. // { zNear, zFar, 1/zNear, 1/zFar }
  154. MaterialParameterHandle *zNearFarInvNearFar;
  155. // Far frustum plane (World Space)
  156. MaterialParameterHandle *farPlane;
  157. // Far frustum plane (View Space)
  158. MaterialParameterHandle *vsFarPlane;
  159. // -dot( farPlane, eyePos )
  160. MaterialParameterHandle *negFarPlaneDotEye;
  161. // Light Parameters
  162. MaterialParameterHandle *probeLSPos;
  163. MaterialParameterHandle *probeWSPos;
  164. MaterialParameterHandle *attenuation;
  165. MaterialParameterHandle *radius;
  166. MaterialParameterHandle *useCubemap;
  167. MaterialParameterHandle *cubemap;
  168. MaterialParameterHandle *cubeMips;
  169. MaterialParameterHandle *eyePosWorld;
  170. MaterialParameterHandle *bbMin;
  171. MaterialParameterHandle *bbMax;
  172. MaterialParameterHandle *useSphereMode;
  173. MaterialParameterHandle *shTerms[9];
  174. MaterialParameterHandle *shConsts[5];
  175. MaterialParameterHandle *probeCount;
  176. ReflectProbeMaterialInfo(const String &matName, const GFXVertexFormat *vertexFormat);
  177. virtual ~ReflectProbeMaterialInfo();
  178. void setViewParameters(const F32 zNear,
  179. const F32 zFar,
  180. const Point3F &eyePos,
  181. const PlaneF &farPlane,
  182. const PlaneF &_vsFarPlane);
  183. void setProbeParameters(const ProbeRenderInst *probe, const SceneRenderState* renderState, const MatrixF &worldViewOnly);
  184. };
  185. enum SpecialProbeTypesEnum
  186. {
  187. SkylightProbeType,
  188. SpecialProbeTypesCount
  189. };
  190. Vector<U32> mRegisteredProbes;
  191. ReflectProbeMaterialInfo* mReflectProbeMaterial;
  192. GFXShaderConstHandle *numProbesSC;
  193. GFXShaderConstHandle *probePositionSC;
  194. GFXShaderConstHandle *probeWorldToObjSC;
  195. GFXShaderConstHandle *probeBBMinSC;
  196. GFXShaderConstHandle *probeBBMaxSC;
  197. GFXShaderConstHandle *probeUseSphereModeSC;
  198. GFXShaderConstHandle *probeRadiusSC;
  199. GFXShaderConstHandle *probeAttenuationSC;
  200. /// The scene graph the light manager is associated with.
  201. //SceneManager *mSceneManager;
  202. ProbeConstantMap mConstantLookup;
  203. GFXShaderRef mLastShader;
  204. GFXShaderConstBufferRef mLastConstants;
  205. ProbeShaderConstants* mLastForwardConstants;
  206. //
  207. //
  208. PostEffect* getProbeArrayEffect();
  209. //
  210. SimObjectPtr<PostEffect> mProbeArrayEffect;
  211. protected:
  212. /// The current active light manager.
  213. static RenderProbeMgr *smProbeManager;
  214. typedef GFXVertexPNTT FarFrustumQuadVert;
  215. GFXVertexBufferHandle<FarFrustumQuadVert> mFarFrustumQuadVerts;
  216. /// This helper function sets the shader constansts
  217. /// for the stock 4 light forward lighting code.
  218. void _update4ProbeConsts(const SceneData &sgData,
  219. MatrixSet &matSet,
  220. GFXShaderConstHandle *probePositionSC,
  221. GFXShaderConstHandle *probeRadiusSC,
  222. GFXShaderConstHandle *probeBoxMinSC,
  223. GFXShaderConstHandle *probeBoxMaxSC,
  224. GFXShaderConstHandle *probeCubemapSC,
  225. GFXShaderConstHandle *probeIsSphereSC,
  226. GFXShaderConstHandle *probeLocalPosSC,
  227. GFXShaderConstBuffer *shaderConsts);
  228. GFXTextureObject * mBrdfTexture;
  229. //Array rendering
  230. U32 mEffectiveProbeCount;
  231. Vector<Point4F> probePositions;
  232. Vector<MatrixF> probeWorldToObj;
  233. Vector<Point4F> probeBBMin;
  234. Vector<Point4F> probeBBMax;
  235. Vector<Point4F> probeUseSphereMode;
  236. Vector<Point4F> probeRadius;
  237. Vector<Point4F> probeAttenuation;
  238. Vector<GFXCubemapHandle> cubeMaps;
  239. Vector<GFXCubemapHandle> irradMaps;
  240. AlignedArray<Point4F> mProbePositions;
  241. AlignedArray<Point4F> mProbeBBMin;
  242. AlignedArray<Point4F> mProbeBBMax;
  243. AlignedArray<float> mProbeUseSphereMode;
  244. AlignedArray<float> mProbeRadius;
  245. AlignedArray<float> mProbeAttenuation;
  246. GFXCubemapArrayHandle mCubemapArray;
  247. GFXCubemapArrayHandle mIrradArray;
  248. public:
  249. RenderProbeMgr();
  250. RenderProbeMgr(RenderInstType riType, F32 renderOrder, F32 processAddOrder);
  251. // RenderBinMgr
  252. void _setupStaticParameters();
  253. void _setupPerFrameParameters(const SceneRenderState *state);
  254. virtual void addElement(RenderInst *inst);
  255. virtual void render(SceneRenderState * state);
  256. virtual void setProbeInfo(ProcessedMaterial *pmat,
  257. const Material *mat,
  258. const SceneData &sgData,
  259. const SceneRenderState *state,
  260. U32 pass,
  261. GFXShaderConstBuffer *shaderConsts);
  262. ProbeShaderConstants* getProbeShaderConstants(GFXShaderConstBuffer* buffer);
  263. // ConsoleObject
  264. static void initPersistFields();
  265. DECLARE_CONOBJECT(RenderProbeMgr);
  266. /// Returns the active LM.
  267. static inline RenderProbeMgr* getProbeManager();
  268. ReflectProbeMaterialInfo* getReflectProbeMaterial();
  269. void registerProbe(U32 probeIdx);
  270. void unregisterProbe(U32 probeIdx);
  271. /// Debug rendering
  272. static bool smRenderReflectionProbes;
  273. };
  274. RenderProbeMgr* RenderProbeMgr::getProbeManager()
  275. {
  276. if (smProbeManager == nullptr)
  277. {
  278. RenderProbeMgr* probeManager = new RenderProbeMgr();
  279. smProbeManager = probeManager;
  280. }
  281. return smProbeManager;
  282. }
  283. #define PROBEMGR RenderProbeMgr::getProbeManager()
  284. #endif // RENDER_PROBE_MGR_H