BsImageBasedLighting.h 6.1 KB

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