BsLightRendering.h 6.4 KB

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