BsImageBasedLighting.h 8.7 KB

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