BsLightRendering.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 { 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. /** Container for all GBuffer textures. */
  50. struct GBufferTextures
  51. {
  52. SPtr<Texture> albedo;
  53. SPtr<Texture> normals;
  54. SPtr<Texture> roughMetal;
  55. SPtr<Texture> depth;
  56. };
  57. /** Allows you to easily bind GBuffer textures to some material. */
  58. class GBufferParams
  59. {
  60. public:
  61. GBufferParams(GpuProgramType type, const SPtr<GpuParams>& gpuParams);
  62. /** Binds the GBuffer textures to the pipeline. */
  63. void bind(const GBufferTextures& gbuffer);
  64. private:
  65. SPtr<GpuParams> mParams;
  66. GpuParamTexture mGBufferA;
  67. GpuParamTexture mGBufferB;
  68. GpuParamTexture mGBufferC;
  69. GpuParamTexture mGBufferDepth;
  70. };
  71. /**
  72. * Contains lights that are visible from a specific set of views, determined by scene information provided to
  73. * setLights().
  74. */
  75. class VisibleLightData
  76. {
  77. public:
  78. VisibleLightData();
  79. /**
  80. * Updates the internal buffers with a new set of lights. Before calling make sure that light visibility has
  81. * been calculated for the provided view group.
  82. */
  83. void update(const SceneInfo& sceneInfo, const RendererViewGroup& viewGroup);
  84. /** Returns a GPU bindable buffer containing information about every light. */
  85. SPtr<GpuBuffer> getLightBuffer() const { return mLightBuffer; }
  86. /** Returns the number of directional lights in the lights buffer. */
  87. UINT32 getNumDirLights() const { return mNumLights[0]; }
  88. /** Returns the number of radial point lights in the lights buffer. */
  89. UINT32 getNumRadialLights() const { return mNumLights[1]; }
  90. /** Returns the number of spot point lights in the lights buffer. */
  91. UINT32 getNumSpotLights() const { return mNumLights[2]; }
  92. /** Returns the number of visible lights of the specified type. */
  93. UINT32 getNumLights(LightType type) const { return mNumLights[(UINT32)type]; }
  94. /** Returns the number of visible shadowed lights of the specified type. */
  95. UINT32 getNumShadowedLights(LightType type) const { return mNumShadowedLights[(UINT32)type]; }
  96. /** Returns the number of visible unshadowed lights of the specified type. */
  97. UINT32 getNumUnshadowedLights(LightType type) const { return mNumLights[(UINT32)type] - mNumShadowedLights[(UINT32)type]; }
  98. /** Returns a list of all visible lights of the specified type. */
  99. const Vector<const RendererLight*>& getLights(LightType type) const { return mVisibleLights[(UINT32)type]; }
  100. private:
  101. SPtr<GpuBuffer> mLightBuffer;
  102. UINT32 mNumLights[(UINT32)LightType::Count];
  103. UINT32 mNumShadowedLights[(UINT32)LightType::Count];
  104. // These are rebuilt every call to setLights()
  105. Vector<const RendererLight*> mVisibleLights[(UINT32)LightType::Count];
  106. // Helpers to avoid memory allocations
  107. Vector<LightData> mLightDataTemp;
  108. };
  109. BS_PARAM_BLOCK_BEGIN(TiledLightingParamDef)
  110. BS_PARAM_BLOCK_ENTRY(Vector4I, gLightCounts)
  111. BS_PARAM_BLOCK_ENTRY(Vector2I, gLightStrides)
  112. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  113. BS_PARAM_BLOCK_END
  114. extern TiledLightingParamDef gTiledLightingParamDef;
  115. /** Shader that performs a lighting pass over data stored in the Gbuffer. */
  116. class TiledDeferredLightingMat : public RendererMaterial<TiledDeferredLightingMat>
  117. {
  118. RMAT_DEF_CUSTOMIZED("TiledDeferredLighting.bsl");
  119. /** Helper method used for initializing variations of this material. */
  120. template<UINT32 msaa>
  121. static const ShaderVariation& getVariation()
  122. {
  123. static ShaderVariation variation = ShaderVariation({
  124. ShaderVariation::Param("MSAA_COUNT", msaa)
  125. });
  126. return variation;
  127. }
  128. public:
  129. TiledDeferredLightingMat();
  130. /** Binds the material for rendering, sets up parameters and executes it. */
  131. void execute(const RendererView& view, const VisibleLightData& lightData, const GBufferTextures& gbuffer,
  132. const SPtr<Texture>& lightAccumTex, const SPtr<GpuBuffer>& lightAccumBuffer, const SPtr<Texture>& msaaCoverage);
  133. /** Returns the material variation matching the provided parameters. */
  134. static TiledDeferredLightingMat* getVariation(UINT32 msaaCount);
  135. private:
  136. UINT32 mSampleCount;
  137. GBufferParams mGBufferParams;
  138. GpuParamBuffer mLightBufferParam;
  139. GpuParamLoadStoreTexture mOutputTextureParam;
  140. GpuParamBuffer mOutputBufferParam;
  141. GpuParamTexture mMSAACoverageTexParam;
  142. SPtr<GpuParamBlockBuffer> mParamBuffer;
  143. static const UINT32 TILE_SIZE;
  144. };
  145. BS_PARAM_BLOCK_BEGIN(FlatFramebufferToTextureParamDef)
  146. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  147. BS_PARAM_BLOCK_ENTRY(INT32, gSampleCount)
  148. BS_PARAM_BLOCK_END
  149. /** Shader that copies a flattened framebuffer into a multisampled texture. */
  150. class FlatFramebufferToTextureMat : public RendererMaterial<FlatFramebufferToTextureMat>
  151. {
  152. RMAT_DEF("FlatFramebufferToTexture.bsl");
  153. public:
  154. FlatFramebufferToTextureMat();
  155. /** Binds the material for rendering, sets up parameters and executes it. */
  156. void execute(const SPtr<GpuBuffer>& flatFramebuffer, const SPtr<Texture>& target);
  157. private:
  158. GpuParamBuffer mInputParam;
  159. SPtr<GpuParamBlockBuffer> mParamBuffer;
  160. };
  161. /** @} */
  162. }}