BsLightRendering.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsRenderBeastPrerequisites.h"
  5. #include "BsRendererMaterial.h"
  6. #include "BsParamBlocks.h"
  7. #include "BsLight.h"
  8. namespace bs { namespace ct
  9. {
  10. struct SceneInfo;
  11. class RendererViewGroup;
  12. /** @addtogroup RenderBeast
  13. * @{
  14. */
  15. /** Information about a single light, as seen by the lighting shader. */
  16. struct LightData
  17. {
  18. Vector3 position;
  19. float attRadius;
  20. Vector3 direction;
  21. float luminance;
  22. Vector3 spotAngles;
  23. float attRadiusSqrdInv;
  24. Vector3 color;
  25. float srcRadius;
  26. Vector3 shiftedLightPosition;
  27. float padding;
  28. };
  29. /** Renderer information specific to a single light. */
  30. class RendererLight
  31. {
  32. public:
  33. RendererLight(Light* light);
  34. /** Populates the structure with light parameters. */
  35. void getParameters(LightData& output) const;
  36. /**
  37. * Populates the provided parameter block buffer with information about the light. Provided buffer's structure
  38. * must match PerLightParamDef.
  39. */
  40. void getParameters(SPtr<GpuParamBlockBuffer>& buffer) const;
  41. /**
  42. * Calculates the light position that is shifted in order to account for area spot lights. For non-spot lights
  43. * this method will return normal light position. The position will be shifted back from the light direction,
  44. * magnitude of the shift depending on the source radius.
  45. */
  46. Vector3 getShiftedLightPosition() const;
  47. Light* internal;
  48. };
  49. /** Allows you to easily bind GBuffer textures to some material. */
  50. class GBufferParams
  51. {
  52. public:
  53. GBufferParams(const SPtr<Material>& material, const SPtr<GpuParamsSet>& paramsSet);
  54. /** Binds the GBuffer textures to the pipeline. */
  55. void bind(const RenderTargets& renderTargets);
  56. private:
  57. SPtr<Material> mMaterial;
  58. SPtr<GpuParamsSet> mParamsSet;
  59. MaterialParamTexture mGBufferA;
  60. MaterialParamTexture mGBufferB;
  61. MaterialParamTexture mGBufferC;
  62. MaterialParamTexture mGBufferDepth;
  63. };
  64. /**
  65. * Contains lights that are visible from a specific set of views, determined by scene information provided to
  66. * setLights().
  67. */
  68. class VisibleLightData
  69. {
  70. public:
  71. VisibleLightData();
  72. /**
  73. * Updates the internal buffers with a new set of lights. Before calling make sure that light visibility has
  74. * been calculated for the provided view group.
  75. */
  76. void update(const SceneInfo& sceneInfo, const RendererViewGroup& viewGroup);
  77. /** Returns a GPU bindable buffer containing information about every light. */
  78. SPtr<GpuBuffer> getLightBuffer() const { return mLightBuffer; }
  79. /** Returns the number of directional lights in the lights buffer. */
  80. UINT32 getNumDirLights() const { return mNumLights[0]; }
  81. /** Returns the number of radial point lights in the lights buffer. */
  82. UINT32 getNumRadialLights() const { return mNumLights[1]; }
  83. /** Returns the number of spot point lights in the lights buffer. */
  84. UINT32 getNumSpotLights() const { return mNumLights[2]; }
  85. /** Returns the number of visible lights of the specified type. */
  86. UINT32 getNumLights(LightType type) const { return mNumLights[(UINT32)type]; }
  87. /** Returns the number of visible shadowed lights of the specified type. */
  88. UINT32 getNumShadowedLights(LightType type) const { return mNumShadowedLights[(UINT32)type]; }
  89. /** Returns the number of visible unshadowed lights of the specified type. */
  90. UINT32 getNumUnshadowedLights(LightType type) const { return mNumLights[(UINT32)type] - mNumShadowedLights[(UINT32)type]; }
  91. /** Returns a list of all visible lights of the specified type. */
  92. const Vector<const RendererLight*>& getLights(LightType type) const { return mVisibleLights[(UINT32)type]; }
  93. private:
  94. SPtr<GpuBuffer> mLightBuffer;
  95. UINT32 mNumLights[(UINT32)LightType::Count];
  96. UINT32 mNumShadowedLights[(UINT32)LightType::Count];
  97. // These are rebuilt every call to setLights()
  98. Vector<const RendererLight*> mVisibleLights[(UINT32)LightType::Count];
  99. // Helpers to avoid memory allocations
  100. Vector<LightData> mLightDataTemp;
  101. };
  102. BS_PARAM_BLOCK_BEGIN(TiledLightingParamDef)
  103. BS_PARAM_BLOCK_ENTRY(Vector4I, gLightCounts)
  104. BS_PARAM_BLOCK_ENTRY(Vector2I, gLightStrides)
  105. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  106. BS_PARAM_BLOCK_END
  107. extern TiledLightingParamDef gTiledLightingParamDef;
  108. /** Functionality common to all versions of TiledDeferredLightingMat<T>. */
  109. class TiledDeferredLighting
  110. {
  111. public:
  112. TiledDeferredLighting(const SPtr<Material>& material, const SPtr<GpuParamsSet>& paramsSet, UINT32 sampleCount);
  113. /** Binds the material for rendering, sets up parameters and executes it. */
  114. void execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera, bool noLighting,
  115. bool noShadows);
  116. /** Binds all the active lights. */
  117. void setLights(const VisibleLightData& lightData);
  118. static const UINT32 TILE_SIZE;
  119. private:
  120. UINT32 mSampleCount;
  121. SPtr<Material> mMaterial;
  122. SPtr<GpuParamsSet> mParamsSet;
  123. GBufferParams mGBufferParams;
  124. Vector4I mUnshadowedLightCounts;
  125. Vector4I mLightCounts;
  126. Vector2I mLightStrides;
  127. GpuParamBuffer mLightBufferParam;
  128. GpuParamLoadStoreTexture mOutputTextureParam;
  129. GpuParamBuffer mOutputBufferParam;
  130. SPtr<GpuParamBlockBuffer> mParamBuffer;
  131. };
  132. /** Interface implemented by all versions of TTiledDeferredLightingMat<T>. */
  133. class ITiledDeferredLightingMat
  134. {
  135. public:
  136. virtual ~ITiledDeferredLightingMat() {}
  137. /** @copydoc TiledDeferredLighting::execute() */
  138. virtual void execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera,
  139. bool noLighting, bool noShadows) = 0;
  140. /** @copydoc TiledDeferredLighting::setLights() */
  141. virtual void setLights(const VisibleLightData& lightData) = 0;
  142. };
  143. /** Shader that performs a lighting pass over data stored in the Gbuffer. */
  144. template<int MSAA_COUNT>
  145. class TTiledDeferredLightingMat : public ITiledDeferredLightingMat, public RendererMaterial<TTiledDeferredLightingMat<MSAA_COUNT>>
  146. {
  147. RMAT_DEF("TiledDeferredLighting.bsl");
  148. public:
  149. TTiledDeferredLightingMat();
  150. /** @copydoc ITiledDeferredLightingMat::execute() */
  151. void execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera,
  152. bool noLighting, bool noShaodws) override;
  153. /** @copydoc ITiledDeferredLightingMat::setLights() */
  154. void setLights(const VisibleLightData& lightData) override;
  155. private:
  156. TiledDeferredLighting mInternal;
  157. };
  158. /** Contains instances for all types of tile deferred lighting materials. */
  159. class TiledDeferredLightingMaterials
  160. {
  161. public:
  162. TiledDeferredLightingMaterials();
  163. ~TiledDeferredLightingMaterials();
  164. /**
  165. * Returns a version of the tile-deferred lighting material that matches the parameters.
  166. *
  167. * @param[in] msaa Number of samples per pixel.
  168. */
  169. ITiledDeferredLightingMat* get(UINT32 msaa);
  170. private:
  171. ITiledDeferredLightingMat* mInstances[4];
  172. };
  173. BS_PARAM_BLOCK_BEGIN(FlatFramebufferToTextureParamDef)
  174. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  175. BS_PARAM_BLOCK_ENTRY(INT32, gSampleCount)
  176. BS_PARAM_BLOCK_END
  177. /** Shader that copies a flattened framebuffer into a multisampled texture. */
  178. class FlatFramebufferToTextureMat : public RendererMaterial<FlatFramebufferToTextureMat>
  179. {
  180. RMAT_DEF("FlatFramebufferToTexture.bsl");
  181. public:
  182. FlatFramebufferToTextureMat();
  183. /** Binds the material for rendering, sets up parameters and executes it. */
  184. void execute(const SPtr<GpuBuffer>& flatFramebuffer, const SPtr<Texture>& target);
  185. private:
  186. GpuParamBuffer mInputParam;
  187. SPtr<GpuParamBlockBuffer> mParamBuffer;
  188. };
  189. /** @} */
  190. }}