BsImageBasedLighting.h 5.8 KB

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