BsImageBasedLighting.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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, gSkyCubemapNumMips)
  43. BS_PARAM_BLOCK_ENTRY(float, gSkyBrightness)
  44. BS_PARAM_BLOCK_END
  45. extern ReflProbeParamsParamDef gReflProbeParamsParamDef;
  46. /** Renderer information specific to a single reflection probe. */
  47. class RendererReflectionProbe
  48. {
  49. public:
  50. RendererReflectionProbe(ReflectionProbe* probe);
  51. /** Populates the structure with reflection probe parameters. */
  52. void getParameters(ReflProbeData& output) const;
  53. ReflectionProbe* probe;
  54. UINT32 arrayIdx;
  55. SPtr<Texture> texture;
  56. bool customTexture : 1;
  57. bool textureDirty : 1;
  58. bool arrayDirty : 1;
  59. bool errorFlagged : 1;
  60. };
  61. BS_PARAM_BLOCK_BEGIN(TiledImageBasedLightingParamDef)
  62. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  63. BS_PARAM_BLOCK_END
  64. extern TiledImageBasedLightingParamDef gTiledImageBasedLightingParamDef;
  65. /** Functionality common to all versions of TiledDeferredImageBasedLightingMat<T>. */
  66. class TiledDeferredImageBasedLighting
  67. {
  68. public:
  69. TiledDeferredImageBasedLighting(const SPtr<Material>& material, const SPtr<GpuParamsSet>& paramsSet,
  70. UINT32 sampleCount);
  71. /** Binds the material for rendering, sets up parameters and executes it. */
  72. void execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera,
  73. const SPtr<Texture>& preintegratedGF);
  74. /** Binds all the active reflection probes. */
  75. void setReflectionProbes(const GPUReflProbeData& probeData, const SPtr<Texture>& reflectionCubemaps);
  76. /** Binds the sky reflection & irradiance textures. Set textures to null if not available. */
  77. void setSky(const SPtr<Texture>& skyReflections, const SPtr<Texture>& skyIrradiance, float brightness);
  78. /**
  79. * Generates a 2D 2-channel texture containing a pre-integrated G and F factors of the microfactet BRDF. This is an
  80. * approximation used for image based lighting, so we can avoid sampling environment maps for each light. Works in
  81. * tandem with the importance sampled reflection cubemaps.
  82. *
  83. * (u, v) = (NoV, roughness)
  84. * (r, g) = (scale, bias)
  85. */
  86. static SPtr<Texture> generatePreintegratedEnvBRDF();
  87. static const UINT32 TILE_SIZE;
  88. private:
  89. UINT32 mSampleCount;
  90. SPtr<Material> mMaterial;
  91. SPtr<GpuParamsSet> mParamsSet;
  92. GpuParamTexture mGBufferA;
  93. GpuParamTexture mGBufferB;
  94. GpuParamTexture mGBufferC;
  95. GpuParamTexture mGBufferDepth;
  96. GpuParamTexture mInColorTextureParam;
  97. GpuParamBuffer mInColorBufferParam;
  98. GpuParamTexture mSkyReflectionsParam;
  99. GpuParamTexture mSkyIrradianceParam;
  100. GpuParamTexture mReflectionProbeCubemapsParam;
  101. GpuParamTexture mPreintegratedEnvBRDFParam;
  102. GpuParamBuffer mReflectionProbesParam;
  103. GpuParamLoadStoreTexture mOutputTextureParam;
  104. GpuParamBuffer mOutputBufferParam;
  105. SPtr<GpuParamBlockBuffer> mParamBuffer;
  106. SPtr<GpuParamBlockBuffer> mReflectionsParamBuffer;
  107. SPtr<SamplerState> mReflectionSamplerState;
  108. };
  109. /** Interface implemented by all versions of TTiledDeferredImageBasedLightingMat<T>. */
  110. class ITiledDeferredImageBasedLightingMat
  111. {
  112. public:
  113. virtual ~ITiledDeferredImageBasedLightingMat() {}
  114. /** @copydoc ITiledDeferredImageBasedLightingMat::execute() */
  115. virtual void execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera,
  116. const SPtr<Texture>& preintegratedGF) = 0;
  117. /** @copydoc ITiledDeferredImageBasedLightingMat::setReflectionProbes() */
  118. virtual void setReflectionProbes(const GPUReflProbeData& probeData, const SPtr<Texture>& reflectionCubemaps) = 0;
  119. /** @copydoc ITiledDeferredImageBasedLightingMat::setSky() */
  120. virtual void setSky(const SPtr<Texture>& skyReflections, const SPtr<Texture>& skyIrradiance, float brightness) = 0;
  121. };
  122. /** Shader that performs a lighting pass over data stored in the Gbuffer. */
  123. template<int MSAA_COUNT, bool CapturingReflections>
  124. class TTiledDeferredImageBasedLightingMat : public ITiledDeferredImageBasedLightingMat,
  125. public RendererMaterial<TTiledDeferredImageBasedLightingMat<MSAA_COUNT, CapturingReflections>>
  126. {
  127. RMAT_DEF("TiledDeferredImageBasedLighting.bsl");
  128. public:
  129. TTiledDeferredImageBasedLightingMat();
  130. /** @copydoc ITiledDeferredImageBasedLightingMat::execute() */
  131. void execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera,
  132. const SPtr<Texture>& preintegratedGF) override;
  133. /** @copydoc ITiledDeferredImageBasedLightingMat::setReflectionProbes() */
  134. void setReflectionProbes(const GPUReflProbeData& probeData, const SPtr<Texture>& reflectionCubemaps) override;
  135. /** @copydoc ITiledDeferredImageBasedLightingMat::setSky() */
  136. void setSky(const SPtr<Texture>& skyReflections, const SPtr<Texture>& skyIrradiance, float brightness) override;
  137. private:
  138. TiledDeferredImageBasedLighting mInternal;
  139. };
  140. /** Contains instances for all types of tile deferred image based lighting materials. */
  141. class TiledDeferredImageBasedLightingMaterials
  142. {
  143. public:
  144. TiledDeferredImageBasedLightingMaterials();
  145. ~TiledDeferredImageBasedLightingMaterials();
  146. /**
  147. * Returns a version of the tile-deferred image based lighting material that matches the parameters.
  148. *
  149. * @param[in] msaa Number of samples per pixel.
  150. * @param[in] capturingReflections If true reflection probes will not be evaluated and instead the material's
  151. * specular color will be returned instead. Useful when rendering reflection
  152. * probes.
  153. */
  154. ITiledDeferredImageBasedLightingMat* get(UINT32 msaa, bool capturingReflections);
  155. private:
  156. ITiledDeferredImageBasedLightingMat* mInstances[8];
  157. };
  158. /** @} */
  159. }}