BsImageBasedLighting.h 5.9 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;
  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. private:
  44. SPtr<GpuBuffer> mProbeBuffer;
  45. UINT32 mNumProbes;
  46. // Helper to avoid memory allocations
  47. Vector<ReflProbeData> mReflProbeDataTemp;
  48. };
  49. BS_PARAM_BLOCK_BEGIN(ReflProbeParamsParamDef)
  50. BS_PARAM_BLOCK_ENTRY(INT32, gReflCubemapNumMips)
  51. BS_PARAM_BLOCK_ENTRY(INT32, gNumProbes)
  52. BS_PARAM_BLOCK_ENTRY(INT32, gSkyCubemapAvailable)
  53. BS_PARAM_BLOCK_ENTRY(INT32, gUseReflectionMaps)
  54. BS_PARAM_BLOCK_ENTRY(INT32, gSkyCubemapNumMips)
  55. BS_PARAM_BLOCK_ENTRY(float, gSkyBrightness)
  56. BS_PARAM_BLOCK_END
  57. extern ReflProbeParamsParamDef gReflProbeParamsParamDef;
  58. /** Renderer information specific to a single reflection probe. */
  59. class RendererReflectionProbe
  60. {
  61. public:
  62. RendererReflectionProbe(ReflectionProbe* probe);
  63. /** Populates the structure with reflection probe parameters. */
  64. void getParameters(ReflProbeData& output) const;
  65. ReflectionProbe* probe;
  66. UINT32 arrayIdx;
  67. bool arrayDirty : 1;
  68. mutable bool errorFlagged : 1;
  69. };
  70. BS_PARAM_BLOCK_BEGIN(TiledImageBasedLightingParamDef)
  71. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  72. BS_PARAM_BLOCK_END
  73. extern TiledImageBasedLightingParamDef gTiledImageBasedLightingParamDef;
  74. /** Helper struct containing all parameters for binding image lighting related data to the GPU programs using them .*/
  75. struct ImageBasedLightingParams
  76. {
  77. /**
  78. * Initializes the parameters from the provided parameters.
  79. *
  80. * @param[in] params GPU parameters object to look for the parameters in.
  81. * @param[in] programType Type of the GPU program to look up the parameters for.
  82. * @param[in] optional If true no warnings will be thrown if some or all of the parameters will be found.
  83. * @param[in] gridIndices Set to true if grid indices (used by light grid) parameter is required.
  84. */
  85. void populate(const SPtr<GpuParams>& params, GpuProgramType programType, bool optional, bool gridIndices);
  86. GpuParamTexture skyReflectionsTexParam;
  87. GpuParamTexture ambientOcclusionTexParam;
  88. GpuParamTexture ssrTexParam;
  89. GpuParamTexture reflectionProbeCubemapsTexParam;
  90. GpuParamTexture preintegratedEnvBRDFParam;
  91. GpuParamBuffer reflectionProbesParam;
  92. GpuParamSampState ambientOcclusionSampParam;
  93. GpuParamSampState ssrSampParam;
  94. GpuParamBuffer reflectionProbeIndicesParam;
  95. GpuParamBinding reflProbeParamBindings;
  96. };
  97. /** Parameter buffer containing information about reflection probes. */
  98. struct ReflProbeParamBuffer
  99. {
  100. ReflProbeParamBuffer();
  101. /** Updates the parameter buffer contents with require refl. probe data. */
  102. void populate(const Skybox* sky, const VisibleReflProbeData& probeData,
  103. const SPtr<Texture>& reflectionCubemaps, bool capturingReflections);
  104. SPtr<GpuParamBlockBuffer> buffer;
  105. };
  106. /** Shader that performs a lighting pass over data stored in the Gbuffer. */
  107. class TiledDeferredImageBasedLightingMat : public RendererMaterial<TiledDeferredImageBasedLightingMat>
  108. {
  109. RMAT_DEF_CUSTOMIZED("TiledDeferredImageBasedLighting.bsl");
  110. /** Helper method used for initializing variations of this material. */
  111. template<UINT32 msaa>
  112. static const ShaderVariation& getVariation()
  113. {
  114. static ShaderVariation variation = ShaderVariation({
  115. ShaderVariation::Param("MSAA_COUNT", msaa)
  116. });
  117. return variation;
  118. }
  119. public:
  120. /** Container for parameters to be passed to the execute() method. */
  121. struct Inputs
  122. {
  123. GBufferTextures gbuffer;
  124. SPtr<Texture> lightAccumulation;
  125. SPtr<Texture> sceneColorTex;
  126. SPtr<GpuBuffer> sceneColorBuffer;
  127. SPtr<Texture> preIntegratedGF;
  128. SPtr<Texture> ambientOcclusion;
  129. SPtr<Texture> ssr;
  130. SPtr<Texture> msaaCoverage;
  131. };
  132. TiledDeferredImageBasedLightingMat();
  133. /** Binds the material for rendering, sets up parameters and executes it. */
  134. void execute(const RendererView& view, const SceneInfo& sceneInfo, const VisibleReflProbeData& probeData,
  135. const Inputs& inputs);
  136. /** Returns the material variation matching the provided parameters. */
  137. static TiledDeferredImageBasedLightingMat* getVariation(UINT32 msaaCount);
  138. private:
  139. UINT32 mSampleCount;
  140. GpuParamTexture mGBufferA;
  141. GpuParamTexture mGBufferB;
  142. GpuParamTexture mGBufferC;
  143. GpuParamTexture mGBufferDepth;
  144. GpuParamTexture mInColorTextureParam;
  145. GpuParamTexture mMSAACoverageTexParam;
  146. ImageBasedLightingParams mImageBasedParams;
  147. GpuParamLoadStoreTexture mOutputTextureParam;
  148. GpuParamBuffer mOutputBufferParam;
  149. SPtr<GpuParamBlockBuffer> mParamBuffer;
  150. ReflProbeParamBuffer mReflProbeParamBuffer;
  151. static const UINT32 TILE_SIZE;
  152. };
  153. /** @} */
  154. }}