BsImageBasedLighting.h 8.7 KB

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