BsImageBasedLighting.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. namespace bs { namespace ct
  8. {
  9. struct SceneInfo;
  10. class RendererViewGroup;
  11. /** @addtogroup RenderBeast
  12. * @{
  13. */
  14. /** Information about a single reflection probe, as seen by the lighting shader. */
  15. struct ReflProbeData
  16. {
  17. Vector3 position;
  18. float radius;
  19. Vector3 boxExtents;
  20. float transitionDistance;
  21. Matrix4 invBoxTransform;
  22. UINT32 cubemapIdx;
  23. UINT32 type;
  24. Vector2 padding;
  25. };
  26. /** Contains GPU buffers used by the renderer to manipulate reflection probes. */
  27. class VisibleReflProbeData
  28. {
  29. public:
  30. VisibleReflProbeData();
  31. /**
  32. * Updates the internal buffers with a new set of refl. probes. Before calling make sure that probe visibility has
  33. * been calculated for the provided view group.
  34. */
  35. void update(const SceneInfo& sceneInfo, const RendererViewGroup& viewGroup);
  36. /** Returns a GPU bindable buffer containing information about every reflection probe. */
  37. SPtr<GpuBuffer> getProbeBuffer() const { return mProbeBuffer; }
  38. /** Returns the number of reflection probes in the probe buffer. */
  39. UINT32 getNumProbes() const { return mNumProbes; }
  40. private:
  41. SPtr<GpuBuffer> mProbeBuffer;
  42. UINT32 mNumProbes;
  43. // Helper to avoid memory allocations
  44. Vector<ReflProbeData> mReflProbeDataTemp;
  45. };
  46. BS_PARAM_BLOCK_BEGIN(ReflProbeParamsParamDef)
  47. BS_PARAM_BLOCK_ENTRY(INT32, gReflCubemapNumMips)
  48. BS_PARAM_BLOCK_ENTRY(INT32, gNumProbes)
  49. BS_PARAM_BLOCK_ENTRY(INT32, gSkyCubemapAvailable)
  50. BS_PARAM_BLOCK_ENTRY(INT32, gUseReflectionMaps)
  51. BS_PARAM_BLOCK_ENTRY(INT32, gSkyCubemapNumMips)
  52. BS_PARAM_BLOCK_ENTRY(float, gSkyBrightness)
  53. BS_PARAM_BLOCK_END
  54. extern ReflProbeParamsParamDef gReflProbeParamsParamDef;
  55. /** Renderer information specific to a single reflection probe. */
  56. class RendererReflectionProbe
  57. {
  58. public:
  59. RendererReflectionProbe(ReflectionProbe* probe);
  60. /** Populates the structure with reflection probe parameters. */
  61. void getParameters(ReflProbeData& output) const;
  62. ReflectionProbe* probe;
  63. UINT32 arrayIdx;
  64. SPtr<Texture> texture;
  65. bool customTexture : 1;
  66. bool textureDirty : 1;
  67. bool arrayDirty : 1;
  68. mutable bool errorFlagged : 1;
  69. };
  70. BS_PARAM_BLOCK_BEGIN(TiledImageBasedLightingParamDef)
  71. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  72. BS_PARAM_BLOCK_END
  73. extern TiledImageBasedLightingParamDef gTiledImageBasedLightingParamDef;
  74. /** Helper struct containing all parameters for binding image lighting related data to the GPU programs using them .*/
  75. struct ImageBasedLightingParams
  76. {
  77. /**
  78. * Initializes the parameters from the provided @p params object.
  79. *
  80. * @param[in] paramsSet GPU parameters object to look for the parameters in.
  81. * @param[in] programType Type of the GPU program to look up the parameters for.
  82. * @param[in] optional If true no warnings will be thrown if some or all of the parameters will be found.
  83. * @param[in] gridIndices Set to true if grid indices (used by light grid) parameter is required.
  84. */
  85. void populate(const SPtr<GpuParamsSet>& paramsSet, GpuProgramType programType, bool optional, bool gridIndices);
  86. GpuParamTexture skyReflectionsTexParam;
  87. GpuParamSampState skyReflectionsSampParam;
  88. GpuParamTexture skyIrradianceTexParam;
  89. GpuParamTexture reflectionProbeCubemapsTexParam;
  90. GpuParamSampState reflectionProbeCubemapsSampParam;
  91. GpuParamTexture preintegratedEnvBRDFParam;
  92. GpuParamBuffer reflectionProbesParam;
  93. GpuParamBuffer reflectionProbeIndicesParam;
  94. UINT32 reflProbeParamsBindingIdx;
  95. };
  96. /** Functionality common to all versions of TiledDeferredImageBasedLightingMat<T>. */
  97. class TiledDeferredImageBasedLighting
  98. {
  99. public:
  100. TiledDeferredImageBasedLighting(const SPtr<Material>& material, const SPtr<GpuParamsSet>& paramsSet,
  101. UINT32 sampleCount);
  102. /** Binds the material for rendering, sets up parameters and executes it. */
  103. void execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera,
  104. const SPtr<Texture>& preintegratedGF);
  105. /** Binds all the active reflection probes. */
  106. void setReflectionProbes(const VisibleReflProbeData& probeData, const SPtr<Texture>& reflectionCubemaps,
  107. bool capturingReflections);
  108. /** Binds the sky reflection & irradiance textures. Set textures to null if not available. */
  109. void setSky(const SPtr<Texture>& skyReflections, const SPtr<Texture>& skyIrradiance, float brightness);
  110. /** Returns a param buffer containing information required to evaluate reflection probe data. */
  111. SPtr<GpuParamBlockBuffer> getReflectionsParamBuffer() const { return mReflectionsParamBuffer; }
  112. /** Returns a sampler state object that should be used for evaluating reflection maps. */
  113. SPtr<SamplerState> getReflectionsSamplerState() const { return mReflectionSamplerState; }
  114. /**
  115. * Generates a 2D 2-channel texture containing a pre-integrated G and F factors of the microfactet BRDF. This is an
  116. * approximation used for image based lighting, so we can avoid sampling environment maps for each light. Works in
  117. * tandem with the importance sampled reflection cubemaps.
  118. *
  119. * (u, v) = (NoV, roughness)
  120. * (r, g) = (scale, bias)
  121. */
  122. static SPtr<Texture> generatePreintegratedEnvBRDF();
  123. static const UINT32 TILE_SIZE;
  124. private:
  125. UINT32 mSampleCount;
  126. SPtr<Material> mMaterial;
  127. SPtr<GpuParamsSet> mParamsSet;
  128. GpuParamTexture mGBufferA;
  129. GpuParamTexture mGBufferB;
  130. GpuParamTexture mGBufferC;
  131. GpuParamTexture mGBufferDepth;
  132. GpuParamTexture mInColorTextureParam;
  133. ImageBasedLightingParams mImageBasedParams;
  134. GpuParamLoadStoreTexture mOutputTextureParam;
  135. GpuParamBuffer mOutputBufferParam;
  136. SPtr<GpuParamBlockBuffer> mParamBuffer;
  137. SPtr<GpuParamBlockBuffer> mReflectionsParamBuffer;
  138. SPtr<SamplerState> mReflectionSamplerState;
  139. };
  140. /** Interface implemented by all versions of TTiledDeferredImageBasedLightingMat<T>. */
  141. class ITiledDeferredImageBasedLightingMat
  142. {
  143. public:
  144. virtual ~ITiledDeferredImageBasedLightingMat() {}
  145. /** @copydoc TiledDeferredImageBasedLighting::execute() */
  146. virtual void execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera,
  147. const SPtr<Texture>& preintegratedGF) = 0;
  148. /** @copydoc TiledDeferredImageBasedLighting::setReflectionProbes() */
  149. virtual void setReflectionProbes(const VisibleReflProbeData& probeData, const SPtr<Texture>& reflectionCubemaps,
  150. bool capturingReflections) = 0;
  151. /** @copydoc TiledDeferredImageBasedLighting::setSky() */
  152. virtual void setSky(const SPtr<Texture>& skyReflections, const SPtr<Texture>& skyIrradiance, float brightness) = 0;
  153. /** @copydoc TiledDeferredImageBasedLighting::getReflectionsParamBuffer() */
  154. virtual SPtr<GpuParamBlockBuffer> getReflectionsParamBuffer() const = 0;
  155. /** @copydoc TiledDeferredImageBasedLighting::getReflectionsSamplerState() */
  156. virtual SPtr<SamplerState> getReflectionsSamplerState() const = 0;
  157. };
  158. /** Shader that performs a lighting pass over data stored in the Gbuffer. */
  159. template<int MSAA_COUNT>
  160. class TTiledDeferredImageBasedLightingMat : public ITiledDeferredImageBasedLightingMat,
  161. public RendererMaterial<TTiledDeferredImageBasedLightingMat<MSAA_COUNT>>
  162. {
  163. RMAT_DEF("TiledDeferredImageBasedLighting.bsl");
  164. public:
  165. TTiledDeferredImageBasedLightingMat();
  166. /** @copydoc ITiledDeferredImageBasedLightingMat::execute() */
  167. void execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera,
  168. const SPtr<Texture>& preintegratedGF) override;
  169. /** @copydoc ITiledDeferredImageBasedLightingMat::setReflectionProbes() */
  170. void setReflectionProbes(const VisibleReflProbeData& probeData, const SPtr<Texture>& reflectionCubemaps,
  171. bool capturingReflections) override;
  172. /** @copydoc ITiledDeferredImageBasedLightingMat::setSky() */
  173. void setSky(const SPtr<Texture>& skyReflections, const SPtr<Texture>& skyIrradiance, float brightness) override;
  174. /** @copydoc ITiledDeferredImageBasedLightingMat::getReflectionsParamBuffer() */
  175. SPtr<GpuParamBlockBuffer> getReflectionsParamBuffer() const override;
  176. /** @copydoc ITiledDeferredImageBasedLightingMat::getReflectionsSamplerState() */
  177. SPtr<SamplerState> getReflectionsSamplerState() const override;
  178. private:
  179. TiledDeferredImageBasedLighting mInternal;
  180. };
  181. /** Contains instances for all types of tile deferred image based lighting materials. */
  182. class TiledDeferredImageBasedLightingMaterials
  183. {
  184. public:
  185. TiledDeferredImageBasedLightingMaterials();
  186. ~TiledDeferredImageBasedLightingMaterials();
  187. /**
  188. * Returns a version of the tile-deferred image based lighting material that matches the parameters.
  189. *
  190. * @param[in] msaa Number of samples per pixel.
  191. */
  192. ITiledDeferredImageBasedLightingMat* get(UINT32 msaa);
  193. private:
  194. ITiledDeferredImageBasedLightingMat* mInstances[4];
  195. };
  196. /** @} */
  197. }}