BsLightRendering.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "Renderer/BsRendererMaterial.h"
  6. #include "Renderer/BsParamBlocks.h"
  7. #include "Renderer/BsLight.h"
  8. namespace bs
  9. {
  10. class Bounds;
  11. namespace ct
  12. {
  13. struct SceneInfo;
  14. class RendererViewGroup;
  15. /** @addtogroup RenderBeast
  16. * @{
  17. */
  18. /** Maximum number of lights that can influence an object when basic forward rendering is used. */
  19. static constexpr UINT32 STANDARD_FORWARD_MAX_NUM_LIGHTS = 8;
  20. /** Information about a single light, as seen by the lighting shader. */
  21. struct LightData
  22. {
  23. Vector3 position;
  24. float attRadius;
  25. Vector3 direction;
  26. float luminance;
  27. Vector3 spotAngles;
  28. float attRadiusSqrdInv;
  29. Vector3 color;
  30. float srcRadius;
  31. Vector3 shiftedLightPosition;
  32. float padding;
  33. };
  34. /** Renderer information specific to a single light. */
  35. class RendererLight
  36. {
  37. public:
  38. RendererLight(Light* light);
  39. /** Populates the structure with light parameters. */
  40. void getParameters(LightData& output) const;
  41. /**
  42. * Populates the provided parameter block buffer with information about the light. Provided buffer's structure
  43. * must match PerLightParamDef.
  44. */
  45. void getParameters(SPtr<GpuParamBlockBuffer>& buffer) const;
  46. /**
  47. * Calculates the light position that is shifted in order to account for area spot lights. For non-spot lights
  48. * this method will return normal light position. The position will be shifted back from the light direction,
  49. * magnitude of the shift depending on the source radius.
  50. */
  51. Vector3 getShiftedLightPosition() const;
  52. Light* internal;
  53. };
  54. /** Container for all GBuffer textures. */
  55. struct GBufferTextures
  56. {
  57. SPtr<Texture> albedo;
  58. SPtr<Texture> normals;
  59. SPtr<Texture> roughMetal;
  60. SPtr<Texture> depth;
  61. };
  62. /** Allows you to easily bind GBuffer textures to some material. */
  63. class GBufferParams
  64. {
  65. public:
  66. GBufferParams(GpuProgramType type, const SPtr<GpuParams>& gpuParams);
  67. /** Binds the GBuffer textures to the pipeline. */
  68. void bind(const GBufferTextures& gbuffer);
  69. private:
  70. SPtr<GpuParams> mParams;
  71. GpuParamTexture mGBufferA;
  72. GpuParamTexture mGBufferB;
  73. GpuParamTexture mGBufferC;
  74. GpuParamTexture mGBufferDepth;
  75. };
  76. /**
  77. * Contains lights that are visible from a specific set of views, determined by scene information provided to
  78. * setLights().
  79. */
  80. class VisibleLightData
  81. {
  82. public:
  83. VisibleLightData();
  84. /**
  85. * Updates the internal buffers with a new set of lights. Before calling make sure that light visibility has
  86. * been calculated for the provided view group.
  87. */
  88. void update(const SceneInfo& sceneInfo, const RendererViewGroup& viewGroup);
  89. /** Returns a GPU bindable buffer containing information about every light. */
  90. SPtr<GpuBuffer> getLightBuffer() const { return mLightBuffer; }
  91. /**
  92. * Scans the list of lights visible in the view frustum to find the ones influencing the object described by
  93. * the provided bounds. A maximum number of STANDARD_FORWARD_MAX_NUM_LIGHTS will be output. If there are more
  94. * influencing lights, only the most important ones will be returned.
  95. *
  96. * The lights will be output in the following order: directional, radial, spot. @p counts will contain the number
  97. * of directional lights (component 'x'), number of radial lights (component 'y') and number of spot lights
  98. * (component 'z');
  99. *
  100. * update() must have been called with most recent scene/view information before calling this method.
  101. */
  102. void gatherInfluencingLights(const Bounds& bounds, const LightData* (&output)[STANDARD_FORWARD_MAX_NUM_LIGHTS],
  103. Vector3I& counts) const;
  104. /** Returns the number of directional lights in the lights buffer. */
  105. UINT32 getNumDirLights() const { return mNumLights[0]; }
  106. /** Returns the number of radial point lights in the lights buffer. */
  107. UINT32 getNumRadialLights() const { return mNumLights[1]; }
  108. /** Returns the number of spot point lights in the lights buffer. */
  109. UINT32 getNumSpotLights() const { return mNumLights[2]; }
  110. /** Returns the number of visible lights of the specified type. */
  111. UINT32 getNumLights(LightType type) const { return mNumLights[(UINT32)type]; }
  112. /** Returns the number of visible shadowed lights of the specified type. */
  113. UINT32 getNumShadowedLights(LightType type) const { return mNumShadowedLights[(UINT32)type]; }
  114. /** Returns the number of visible unshadowed lights of the specified type. */
  115. UINT32 getNumUnshadowedLights(LightType type) const { return mNumLights[(UINT32)type] - mNumShadowedLights[(UINT32)type]; }
  116. /** Returns a list of all visible lights of the specified type. */
  117. const Vector<const RendererLight*>& getLights(LightType type) const { return mVisibleLights[(UINT32)type]; }
  118. private:
  119. SPtr<GpuBuffer> mLightBuffer;
  120. UINT32 mNumLights[(UINT32)LightType::Count];
  121. UINT32 mNumShadowedLights[(UINT32)LightType::Count];
  122. // These are rebuilt every call to update()
  123. Vector<const RendererLight*> mVisibleLights[(UINT32)LightType::Count];
  124. Vector<LightData> mVisibleLightData;
  125. };
  126. BS_PARAM_BLOCK_BEGIN(TiledLightingParamDef)
  127. BS_PARAM_BLOCK_ENTRY(Vector4I, gLightCounts)
  128. BS_PARAM_BLOCK_ENTRY(Vector2I, gLightStrides)
  129. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  130. BS_PARAM_BLOCK_END
  131. extern TiledLightingParamDef gTiledLightingParamDef;
  132. /** Shader that performs a lighting pass over data stored in the Gbuffer. */
  133. class TiledDeferredLightingMat : public RendererMaterial<TiledDeferredLightingMat>
  134. {
  135. RMAT_DEF_CUSTOMIZED("TiledDeferredLighting.bsl");
  136. /** Helper method used for initializing variations of this material. */
  137. template<UINT32 msaa>
  138. static const ShaderVariation& getVariation()
  139. {
  140. static ShaderVariation variation = ShaderVariation({
  141. ShaderVariation::Param("MSAA_COUNT", msaa)
  142. });
  143. return variation;
  144. }
  145. public:
  146. TiledDeferredLightingMat();
  147. /** Binds the material for rendering, sets up parameters and executes it. */
  148. void execute(const RendererView& view, const VisibleLightData& lightData, const GBufferTextures& gbuffer,
  149. const SPtr<Texture>& lightAccumTex, const SPtr<GpuBuffer>& lightAccumBuffer, const SPtr<Texture>& msaaCoverage);
  150. /** Returns the material variation matching the provided parameters. */
  151. static TiledDeferredLightingMat* getVariation(UINT32 msaaCount);
  152. private:
  153. UINT32 mSampleCount;
  154. GBufferParams mGBufferParams;
  155. GpuParamBuffer mLightBufferParam;
  156. GpuParamLoadStoreTexture mOutputTextureParam;
  157. GpuParamBuffer mOutputBufferParam;
  158. GpuParamTexture mMSAACoverageTexParam;
  159. SPtr<GpuParamBlockBuffer> mParamBuffer;
  160. static const UINT32 TILE_SIZE;
  161. };
  162. BS_PARAM_BLOCK_BEGIN(FlatFramebufferToTextureParamDef)
  163. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  164. BS_PARAM_BLOCK_ENTRY(INT32, gSampleCount)
  165. BS_PARAM_BLOCK_END
  166. /** Shader that copies a flattened framebuffer into a multisampled texture. */
  167. class FlatFramebufferToTextureMat : public RendererMaterial<FlatFramebufferToTextureMat>
  168. {
  169. RMAT_DEF("FlatFramebufferToTexture.bsl");
  170. public:
  171. FlatFramebufferToTextureMat();
  172. /** Binds the material for rendering, sets up parameters and executes it. */
  173. void execute(const SPtr<GpuBuffer>& flatFramebuffer, const SPtr<Texture>& target);
  174. private:
  175. GpuParamBuffer mInputParam;
  176. SPtr<GpuParamBlockBuffer> mParamBuffer;
  177. };
  178. BS_PARAM_BLOCK_BEGIN(LightsParamDef)
  179. BS_PARAM_BLOCK_ENTRY_ARRAY(LightData, gLights, STANDARD_FORWARD_MAX_NUM_LIGHTS)
  180. BS_PARAM_BLOCK_END
  181. extern LightsParamDef gLightsParamDef;
  182. BS_PARAM_BLOCK_BEGIN(LightAndReflProbeParamsParamDef)
  183. BS_PARAM_BLOCK_ENTRY(Vector4I, gLightOffsets)
  184. BS_PARAM_BLOCK_ENTRY(int, gReflProbeCount)
  185. BS_PARAM_BLOCK_END
  186. extern LightAndReflProbeParamsParamDef gLightAndReflProbeParamsParamDef;
  187. /** @} */
  188. }}