BsLightRendering.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. Light* internal;
  42. };
  43. /** Allows you to easily bind GBuffer textures to some material. */
  44. class GBufferParams
  45. {
  46. public:
  47. GBufferParams(const SPtr<Material>& material, const SPtr<GpuParamsSet>& paramsSet);
  48. /** Binds the GBuffer textures to the pipeline. */
  49. void bind(const RenderTargets& renderTargets);
  50. private:
  51. MaterialParamTexture mGBufferA;
  52. MaterialParamTexture mGBufferB;
  53. MaterialParamTexture mGBufferC;
  54. MaterialParamTexture mGBufferDepth;
  55. };
  56. /**
  57. * Contains lights that are visible from a specific set of views, determined by scene information provided to
  58. * setLights().
  59. */
  60. class VisibleLightData
  61. {
  62. public:
  63. VisibleLightData();
  64. /**
  65. * Updates the internal buffers with a new set of lights. Before calling make sure that light visibility has
  66. * been calculated for the provided view group.
  67. */
  68. void update(const SceneInfo& sceneInfo, const RendererViewGroup& viewGroup);
  69. /** Returns a GPU bindable buffer containing information about every light. */
  70. SPtr<GpuBuffer> getLightBuffer() const { return mLightBuffer; }
  71. /** Returns the number of directional lights in the lights buffer. */
  72. UINT32 getNumDirLights() const { return mNumLights[0]; }
  73. /** Returns the number of radial point lights in the lights buffer. */
  74. UINT32 getNumRadialLights() const { return mNumLights[1]; }
  75. /** Returns the number of spot point lights in the lights buffer. */
  76. UINT32 getNumSpotLights() const { return mNumLights[2]; }
  77. /** Returns the number of visible lights of the specified type. */
  78. UINT32 getNumLights(LightType type) const { return mNumLights[(UINT32)type]; }
  79. /** Returns the number of visible shadowed lights of the specified type. */
  80. UINT32 getNumShadowedLights(LightType type) const { return mNumShadowedLights[(UINT32)type]; }
  81. /** Returns the number of visible unshadowed lights of the specified type. */
  82. UINT32 getNumUnshadowedLights(LightType type) const { return mNumLights[(UINT32)type] - mNumShadowedLights[(UINT32)type]; }
  83. /** Returns a list of all visible lights of the specified type. */
  84. const Vector<const RendererLight*>& getLights(LightType type) const { return mVisibleLights[(UINT32)type]; }
  85. private:
  86. SPtr<GpuBuffer> mLightBuffer;
  87. UINT32 mNumLights[(UINT32)LightType::Count];
  88. UINT32 mNumShadowedLights[(UINT32)LightType::Count];
  89. // These are rebuilt every call to setLights()
  90. Vector<const RendererLight*> mVisibleLights[(UINT32)LightType::Count];
  91. // Helpers to avoid memory allocations
  92. Vector<LightData> mLightDataTemp;
  93. };
  94. BS_PARAM_BLOCK_BEGIN(TiledLightingParamDef)
  95. BS_PARAM_BLOCK_ENTRY(Vector4I, gLightCounts)
  96. BS_PARAM_BLOCK_ENTRY(Vector2I, gLightStrides)
  97. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  98. BS_PARAM_BLOCK_END
  99. extern TiledLightingParamDef gTiledLightingParamDef;
  100. /** Functionality common to all versions of TiledDeferredLightingMat<T>. */
  101. class TiledDeferredLighting
  102. {
  103. public:
  104. TiledDeferredLighting(const SPtr<Material>& material, const SPtr<GpuParamsSet>& paramsSet, UINT32 sampleCount);
  105. /** Binds the material for rendering, sets up parameters and executes it. */
  106. void execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera, bool noLighting,
  107. bool noShadows);
  108. /** Binds all the active lights. */
  109. void setLights(const VisibleLightData& lightData);
  110. static const UINT32 TILE_SIZE;
  111. private:
  112. UINT32 mSampleCount;
  113. SPtr<Material> mMaterial;
  114. SPtr<GpuParamsSet> mParamsSet;
  115. GBufferParams mGBufferParams;
  116. Vector4I mUnshadowedLightCounts;
  117. Vector4I mLightCounts;
  118. Vector2I mLightStrides;
  119. GpuParamBuffer mLightBufferParam;
  120. GpuParamLoadStoreTexture mOutputTextureParam;
  121. GpuParamBuffer mOutputBufferParam;
  122. SPtr<GpuParamBlockBuffer> mParamBuffer;
  123. };
  124. /** Interface implemented by all versions of TTiledDeferredLightingMat<T>. */
  125. class ITiledDeferredLightingMat
  126. {
  127. public:
  128. virtual ~ITiledDeferredLightingMat() {}
  129. /** @copydoc TiledDeferredLighting::execute() */
  130. virtual void execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera,
  131. bool noLighting, bool noShadows) = 0;
  132. /** @copydoc TiledDeferredLighting::setLights() */
  133. virtual void setLights(const VisibleLightData& lightData) = 0;
  134. };
  135. /** Shader that performs a lighting pass over data stored in the Gbuffer. */
  136. template<int MSAA_COUNT>
  137. class TTiledDeferredLightingMat : public ITiledDeferredLightingMat, public RendererMaterial<TTiledDeferredLightingMat<MSAA_COUNT>>
  138. {
  139. RMAT_DEF("TiledDeferredLighting.bsl");
  140. public:
  141. TTiledDeferredLightingMat();
  142. /** @copydoc ITiledDeferredLightingMat::execute() */
  143. void execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera,
  144. bool noLighting, bool noShaodws) override;
  145. /** @copydoc ITiledDeferredLightingMat::setLights() */
  146. void setLights(const VisibleLightData& lightData) override;
  147. private:
  148. TiledDeferredLighting mInternal;
  149. };
  150. /** Contains instances for all types of tile deferred lighting materials. */
  151. class TiledDeferredLightingMaterials
  152. {
  153. public:
  154. TiledDeferredLightingMaterials();
  155. ~TiledDeferredLightingMaterials();
  156. /**
  157. * Returns a version of the tile-deferred lighting material that matches the parameters.
  158. *
  159. * @param[in] msaa Number of samples per pixel.
  160. */
  161. ITiledDeferredLightingMat* get(UINT32 msaa);
  162. private:
  163. ITiledDeferredLightingMat* mInstances[4];
  164. };
  165. BS_PARAM_BLOCK_BEGIN(FlatFramebufferToTextureParamDef)
  166. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  167. BS_PARAM_BLOCK_ENTRY(INT32, gSampleCount)
  168. BS_PARAM_BLOCK_END
  169. /** Shader that copies a flattened framebuffer into a multisampled texture. */
  170. class FlatFramebufferToTextureMat : public RendererMaterial<FlatFramebufferToTextureMat>
  171. {
  172. RMAT_DEF("FlatFramebufferToTexture.bsl");
  173. public:
  174. FlatFramebufferToTextureMat();
  175. /** Binds the material for rendering, sets up parameters and executes it. */
  176. void execute(const SPtr<GpuBuffer>& flatFramebuffer, const SPtr<Texture>& target);
  177. private:
  178. GpuParamBuffer mInputParam;
  179. SPtr<GpuParamBlockBuffer> mParamBuffer;
  180. };
  181. /** @} */
  182. }}