BsImageBasedLighting.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. GpuParamSampState ambientOcclusionSampParam;
  96. GpuParamSampState ssrSampParam;
  97. GpuParamBuffer reflectionProbeIndicesParam;
  98. GpuParamBinding reflProbeParamBindings;
  99. };
  100. /** Parameter buffer containing information about reflection probes. */
  101. struct ReflProbeParamBuffer
  102. {
  103. ReflProbeParamBuffer();
  104. /** Updates the parameter buffer contents with required refl. probe data. */
  105. void populate(const Skybox* sky, UINT32 numProbes, const SPtr<Texture>& reflectionCubemaps,
  106. bool capturingReflections);
  107. SPtr<GpuParamBlockBuffer> buffer;
  108. };
  109. /** Shader that performs a lighting pass over data stored in the Gbuffer. */
  110. class TiledDeferredImageBasedLightingMat : public RendererMaterial<TiledDeferredImageBasedLightingMat>
  111. {
  112. RMAT_DEF_CUSTOMIZED("TiledDeferredImageBasedLighting.bsl");
  113. /** Helper method used for initializing variations of this material. */
  114. template<UINT32 msaa>
  115. static const ShaderVariation& getVariation()
  116. {
  117. static ShaderVariation variation = ShaderVariation({
  118. ShaderVariation::Param("MSAA_COUNT", msaa)
  119. });
  120. return variation;
  121. }
  122. public:
  123. /** Container for parameters to be passed to the execute() method. */
  124. struct Inputs
  125. {
  126. GBufferTextures gbuffer;
  127. SPtr<Texture> lightAccumulation;
  128. SPtr<Texture> sceneColorTex;
  129. SPtr<GpuBuffer> sceneColorBuffer;
  130. SPtr<Texture> preIntegratedGF;
  131. SPtr<Texture> ambientOcclusion;
  132. SPtr<Texture> ssr;
  133. SPtr<Texture> msaaCoverage;
  134. };
  135. TiledDeferredImageBasedLightingMat();
  136. /** Binds the material for rendering, sets up parameters and executes it. */
  137. void execute(const RendererView& view, const SceneInfo& sceneInfo, const VisibleReflProbeData& probeData,
  138. const Inputs& inputs);
  139. /** Returns the material variation matching the provided parameters. */
  140. static TiledDeferredImageBasedLightingMat* getVariation(UINT32 msaaCount);
  141. private:
  142. UINT32 mSampleCount;
  143. GpuParamTexture mGBufferA;
  144. GpuParamTexture mGBufferB;
  145. GpuParamTexture mGBufferC;
  146. GpuParamTexture mGBufferDepth;
  147. GpuParamTexture mInColorTextureParam;
  148. GpuParamTexture mMSAACoverageTexParam;
  149. ImageBasedLightingParams mImageBasedParams;
  150. GpuParamLoadStoreTexture mOutputTextureParam;
  151. GpuParamBuffer mOutputBufferParam;
  152. SPtr<GpuParamBlockBuffer> mParamBuffer;
  153. ReflProbeParamBuffer mReflProbeParamBuffer;
  154. static const UINT32 TILE_SIZE;
  155. };
  156. /** @} */
  157. }}