BsImageBasedLighting.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "Renderer/BsRendererMaterial.h"
  6. #include "Renderer/BsParamBlocks.h"
  7. #include "BsLightRendering.h"
  8. #include "RenderAPI/BsGpuPipelineParamInfo.h"
  9. namespace bs { namespace ct
  10. {
  11. struct SkyInfo;
  12. struct SceneInfo;
  13. class RendererViewGroup;
  14. /** @addtogroup RenderBeast
  15. * @{
  16. */
  17. /** Maximum number of refl. probes that can influence an object when basic forward rendering is used. */
  18. static constexpr UINT32 STANDARD_FORWARD_MAX_NUM_PROBES = 8;
  19. /** Information about a single reflection probe, as seen by the lighting shader. */
  20. struct ReflProbeData
  21. {
  22. Vector3 position;
  23. float radius;
  24. Vector3 boxExtents;
  25. float transitionDistance;
  26. Matrix4 invBoxTransform;
  27. UINT32 cubemapIdx;
  28. UINT32 type; // 0 - Sphere, 1 - Box
  29. Vector2 padding;
  30. };
  31. /** Contains GPU buffers used by the renderer to manipulate reflection probes. */
  32. class VisibleReflProbeData
  33. {
  34. public:
  35. VisibleReflProbeData();
  36. /**
  37. * Updates the internal buffers with a new set of refl. probes. Before calling make sure that probe visibility has
  38. * been calculated for the provided view group.
  39. */
  40. void update(const SceneInfo& sceneInfo, const RendererViewGroup& viewGroup);
  41. /** Returns a GPU bindable buffer containing information about every reflection probe. */
  42. SPtr<GpuBuffer> getProbeBuffer() const { return mProbeBuffer; }
  43. /** Returns the number of reflection probes in the probe buffer. */
  44. UINT32 getNumProbes() const { return mNumProbes; }
  45. /** Returns information about a probe at the specified index. */
  46. const ReflProbeData& getProbeData(UINT32 idx) const { return mReflProbeData[idx]; }
  47. private:
  48. Vector<ReflProbeData> mReflProbeData;
  49. SPtr<GpuBuffer> mProbeBuffer;
  50. UINT32 mNumProbes;
  51. };
  52. BS_PARAM_BLOCK_BEGIN(ReflProbeParamsParamDef)
  53. BS_PARAM_BLOCK_ENTRY(INT32, gReflCubemapNumMips)
  54. BS_PARAM_BLOCK_ENTRY(INT32, gNumProbes)
  55. BS_PARAM_BLOCK_ENTRY(INT32, gSkyCubemapAvailable)
  56. BS_PARAM_BLOCK_ENTRY(INT32, gUseReflectionMaps)
  57. BS_PARAM_BLOCK_ENTRY(INT32, gSkyCubemapNumMips)
  58. BS_PARAM_BLOCK_ENTRY(float, gSkyBrightness)
  59. BS_PARAM_BLOCK_END
  60. extern ReflProbeParamsParamDef gReflProbeParamsParamDef;
  61. /** Renderer information specific to a single reflection probe. */
  62. class RendererReflectionProbe
  63. {
  64. public:
  65. RendererReflectionProbe(ReflectionProbe* probe);
  66. /** Populates the structure with reflection probe parameters. */
  67. void getParameters(ReflProbeData& output) const;
  68. ReflectionProbe* probe;
  69. UINT32 arrayIdx;
  70. bool arrayDirty : 1;
  71. mutable bool errorFlagged : 1;
  72. };
  73. BS_PARAM_BLOCK_BEGIN(TiledImageBasedLightingParamDef)
  74. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  75. BS_PARAM_BLOCK_END
  76. extern TiledImageBasedLightingParamDef gTiledImageBasedLightingParamDef;
  77. /** Helper struct containing all parameters for binding image lighting related data to the GPU programs using them .*/
  78. struct ImageBasedLightingParams
  79. {
  80. /**
  81. * Initializes the parameters from the provided parameters.
  82. *
  83. * @param[in] params GPU parameters object to look for the parameters in.
  84. * @param[in] programType Type of the GPU program to look up the parameters for.
  85. * @param[in] optional If true no warnings will be thrown if some or all of the parameters will be found.
  86. * @param[in] gridIndices Set to true if grid indices (used by light grid) parameter is required.
  87. * @param[in] probeArray True if the refl. probe data is to be provided in a structured buffer.
  88. */
  89. void populate(const SPtr<GpuParams>& params, GpuProgramType programType, bool optional, bool gridIndices,
  90. bool probeArray);
  91. GpuParamTexture skyReflectionsTexParam;
  92. GpuParamTexture ambientOcclusionTexParam;
  93. GpuParamTexture ssrTexParam;
  94. GpuParamTexture reflectionProbeCubemapsTexParam;
  95. GpuParamTexture preintegratedEnvBRDFParam;
  96. GpuParamBuffer reflectionProbesParam;
  97. GpuParamBuffer reflectionProbeIndicesParam;
  98. GpuParamBinding reflProbeParamBindings;
  99. // Only utilized when standard forward rendering is used
  100. GpuParamBinding reflProbesBinding;
  101. };
  102. /** Parameter buffer containing information about reflection probes. */
  103. struct ReflProbeParamBuffer
  104. {
  105. ReflProbeParamBuffer();
  106. /** Updates the parameter buffer contents with required refl. probe data. */
  107. void populate(const Skybox* sky, UINT32 numProbes, const SPtr<Texture>& reflectionCubemaps,
  108. bool capturingReflections);
  109. SPtr<GpuParamBlockBuffer> buffer;
  110. };
  111. /** Shader that performs a lighting pass over data stored in the Gbuffer. */
  112. class TiledDeferredImageBasedLightingMat : public RendererMaterial<TiledDeferredImageBasedLightingMat>
  113. {
  114. RMAT_DEF_CUSTOMIZED("TiledDeferredImageBasedLighting.bsl");
  115. /** Helper method used for initializing variations of this material. */
  116. template<UINT32 msaa>
  117. static const ShaderVariation& getVariation()
  118. {
  119. static ShaderVariation variation = ShaderVariation({
  120. ShaderVariation::Param("MSAA_COUNT", msaa)
  121. });
  122. return variation;
  123. }
  124. public:
  125. /** Container for parameters to be passed to the execute() method. */
  126. struct Inputs
  127. {
  128. GBufferTextures gbuffer;
  129. SPtr<Texture> lightAccumulation;
  130. SPtr<Texture> sceneColorTex;
  131. SPtr<GpuBuffer> sceneColorBuffer;
  132. SPtr<Texture> preIntegratedGF;
  133. SPtr<Texture> ambientOcclusion;
  134. SPtr<Texture> ssr;
  135. SPtr<Texture> msaaCoverage;
  136. };
  137. TiledDeferredImageBasedLightingMat();
  138. /** Binds the material for rendering, sets up parameters and executes it. */
  139. void execute(const RendererView& view, const SceneInfo& sceneInfo, const VisibleReflProbeData& probeData,
  140. const Inputs& inputs);
  141. /** Returns the material variation matching the provided parameters. */
  142. static TiledDeferredImageBasedLightingMat* getVariation(UINT32 msaaCount);
  143. private:
  144. UINT32 mSampleCount;
  145. GpuParamTexture mGBufferA;
  146. GpuParamTexture mGBufferB;
  147. GpuParamTexture mGBufferC;
  148. GpuParamTexture mGBufferDepth;
  149. GpuParamTexture mInColorTextureParam;
  150. GpuParamTexture mMSAACoverageTexParam;
  151. ImageBasedLightingParams mImageBasedParams;
  152. GpuParamLoadStoreTexture mOutputTextureParam;
  153. GpuParamBuffer mOutputBufferParam;
  154. SPtr<GpuParamBlockBuffer> mParamBuffer;
  155. ReflProbeParamBuffer mReflProbeParamBuffer;
  156. static const UINT32 TILE_SIZE;
  157. };
  158. BS_PARAM_BLOCK_BEGIN(ReflProbesParamDef)
  159. BS_PARAM_BLOCK_ENTRY_ARRAY(ReflProbeData, gReflectionProbes, STANDARD_FORWARD_MAX_NUM_PROBES)
  160. BS_PARAM_BLOCK_END
  161. extern ReflProbesParamDef gReflProbesParamDef;
  162. /** @} */
  163. }}