BsLightRendering.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "BsReflectionProbeSampling.h"
  8. namespace bs { namespace ct
  9. {
  10. /** @addtogroup RenderBeast
  11. * @{
  12. */
  13. /** Information about a single light, as seen by the lighting shader. */
  14. struct LightData
  15. {
  16. Vector3 position;
  17. float radius;
  18. Vector3 direction;
  19. float intensity;
  20. Vector3 spotAngles;
  21. float radiusSqrdInv;
  22. Vector3 color;
  23. };
  24. /** Renderer information specific to a single light. */
  25. class RendererLight
  26. {
  27. public:
  28. RendererLight(Light* light);
  29. /** Populates the structure with light parameters. */
  30. void getParameters(LightData& output) const;
  31. /** Gets the internal light representation. */
  32. Light* getInternal() const { return mInternal; }
  33. private:
  34. Light* mInternal;
  35. };
  36. /** Contains GPU buffers used by the renderer to manipulate lights. */
  37. class GPULightData
  38. {
  39. public:
  40. GPULightData();
  41. /** Updates the internal buffers with a new set of lights. */
  42. void setLights(const Vector<LightData>& lightData, UINT32 numDirLights, UINT32 numRadialLights,
  43. UINT32 numSpotLights);
  44. /** Returns a GPU bindable buffer containing information about every light. */
  45. SPtr<GpuBuffer> getLightBuffer() const { return mLightBuffer; }
  46. /** Returns the number of directional lights in the lights buffer. */
  47. UINT32 getNumDirLights() const { return mNumLights[0]; }
  48. /** Returns the number of radial point lights in the lights buffer. */
  49. UINT32 getNumRadialLights() const { return mNumLights[1]; }
  50. /** Returns the number of spot point lights in the lights buffer. */
  51. UINT32 getNumSpotLights() const { return mNumLights[2]; }
  52. private:
  53. SPtr<GpuBuffer> mLightBuffer;
  54. UINT32 mNumLights[3];
  55. };
  56. BS_PARAM_BLOCK_BEGIN(TiledLightingParamDef)
  57. BS_PARAM_BLOCK_ENTRY(Vector3I, gLightOffsets)
  58. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  59. BS_PARAM_BLOCK_END
  60. extern TiledLightingParamDef gTiledLightingParamDef;
  61. /** Functionality common to all versions of TiledDeferredLightingMat<T>. */
  62. class TiledDeferredLighting
  63. {
  64. public:
  65. TiledDeferredLighting(const SPtr<Material>& material, const SPtr<GpuParamsSet>& paramsSet, UINT32 sampleCount);
  66. /** Binds the material for rendering, sets up parameters and executes it. */
  67. void execute(const SPtr<RenderTargets>& gbuffer, const SPtr<GpuParamBlockBuffer>& perCamera,
  68. const SPtr<Texture>& preintegratedGF, bool noLighting);
  69. /** Binds all the active lights. */
  70. void setLights(const GPULightData& lightData);
  71. /** Binds all the active reflection probes. */
  72. void setReflectionProbes(const GPUReflProbeData& probeData, const SPtr<Texture>& reflectionCubemaps);
  73. /** Binds the sky reflection. If no sky reflection set to null. */
  74. void setSkyReflections(const SPtr<Texture>& skyReflections);
  75. /**
  76. * Generates a 2D 2-channel texture containing a pre-integrated G and F factors of the microfactet BRDF. This is an
  77. * approximation used for image based lighting, so we can avoid sampling environment maps for each light. Works in
  78. * tandem with the importance sampled reflection cubemaps.
  79. *
  80. * (u, v) = (NoV, roughness)
  81. * (r, g) = (scale, bias)
  82. */
  83. static SPtr<Texture> generatePreintegratedEnvBRDF();
  84. static const UINT32 TILE_SIZE;
  85. private:
  86. UINT32 mSampleCount;
  87. SPtr<Material> mMaterial;
  88. SPtr<GpuParamsSet> mParamsSet;
  89. GpuParamTexture mGBufferA;
  90. GpuParamTexture mGBufferB;
  91. GpuParamTexture mGBufferC;
  92. GpuParamTexture mGBufferDepth;
  93. GpuParamTexture mSkyCubemapTexParam;
  94. GpuParamTexture mReflectionProbeCubemapsParam;
  95. GpuParamTexture mPreintegratedEnvBRDFParam;
  96. GpuParamBuffer mReflectionProbesParam;
  97. Vector3I mLightOffsets;
  98. GpuParamBuffer mLightBufferParam;
  99. GpuParamLoadStoreTexture mOutputTextureParam;
  100. GpuParamBuffer mOutputBufferParam;
  101. SPtr<GpuParamBlockBuffer> mParamBuffer;
  102. SPtr<GpuParamBlockBuffer> mReflectionsParamBuffer;
  103. };
  104. /** Interface implemented by all versions of TTiledDeferredLightingMat<T>. */
  105. class ITiledDeferredLightingMat
  106. {
  107. public:
  108. virtual ~ITiledDeferredLightingMat() {}
  109. /** @copydoc TiledDeferredLighting::execute() */
  110. virtual void execute(const SPtr<RenderTargets>& gbuffer, const SPtr<GpuParamBlockBuffer>& perCamera,
  111. const SPtr<Texture>& preintegratedGF, bool noLighting) = 0;
  112. /** @copydoc TiledDeferredLighting::setLights() */
  113. virtual void setLights(const GPULightData& lightData) = 0;
  114. /** @copydoc TiledDeferredLighting::setReflectionProbes() */
  115. virtual void setReflectionProbes(const GPUReflProbeData& probeData, const SPtr<Texture>& reflectionCubemaps) = 0;
  116. /** @copydoc TiledDeferredLighting::setSkyReflections() */
  117. virtual void setSkyReflections(const SPtr<Texture>& skyReflections) = 0;
  118. };
  119. /** Shader that performs a lighting pass over data stored in the Gbuffer. */
  120. template<int MSAA_COUNT, bool FixedReflColor>
  121. class TTiledDeferredLightingMat : public ITiledDeferredLightingMat, public RendererMaterial<TTiledDeferredLightingMat<MSAA_COUNT, FixedReflColor>>
  122. {
  123. RMAT_DEF("TiledDeferredLighting.bsl");
  124. public:
  125. TTiledDeferredLightingMat();
  126. /** @copydoc ITiledDeferredLightingMat::execute() */
  127. void execute(const SPtr<RenderTargets>& gbuffer, const SPtr<GpuParamBlockBuffer>& perCamera,
  128. const SPtr<Texture>& preintegratedGF, bool noLighting) override;
  129. /** @copydoc ITiledDeferredLightingMat::setLights() */
  130. void setLights(const GPULightData& lightData) override;
  131. /** @copydoc ITiledDeferredLightingMat::setReflectionProbes() */
  132. void setReflectionProbes(const GPUReflProbeData& probeData, const SPtr<Texture>& reflectionCubemaps) override;
  133. /** @copydoc ITiledDeferredLightingMat::setSkyReflections() */
  134. void setSkyReflections(const SPtr<Texture>& skyReflections) override;
  135. private:
  136. TiledDeferredLighting mInternal;
  137. };
  138. /** Contains instances for all types of tile deferred lighting materials. */
  139. class TiledDeferredLightingMaterials
  140. {
  141. public:
  142. TiledDeferredLightingMaterials();
  143. ~TiledDeferredLightingMaterials();
  144. /**
  145. * Returns a version of the tile-deferred lighting material that matches the parameters.
  146. *
  147. * @param[in] msaa Number of samples per pixel.
  148. * @param[in] fixedReflColor If true reflection probes will not be evaluated and instead a fixed color will
  149. * be returned instead. Useful when rendering reflection probes.
  150. */
  151. ITiledDeferredLightingMat* get(UINT32 msaa, bool fixedReflColor);
  152. private:
  153. ITiledDeferredLightingMat* mInstances[8];
  154. };
  155. BS_PARAM_BLOCK_BEGIN(FlatFramebufferToTextureParamDef)
  156. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  157. BS_PARAM_BLOCK_ENTRY(INT32, gSampleCount)
  158. BS_PARAM_BLOCK_END
  159. /** Shader that copies a flattened framebuffer into a multisampled texture. */
  160. class FlatFramebufferToTextureMat : public RendererMaterial<FlatFramebufferToTextureMat>
  161. {
  162. RMAT_DEF("FlatFramebufferToTexture.bsl");
  163. public:
  164. FlatFramebufferToTextureMat();
  165. /** Binds the material for rendering, sets up parameters and executes it. */
  166. void execute(const SPtr<GpuBuffer>& flatFramebuffer, const SPtr<Texture>& target);
  167. private:
  168. GpuParamBuffer mInputParam;
  169. SPtr<GpuParamBlockBuffer> mParamBuffer;
  170. };
  171. /** @} */
  172. }}