BsImageBasedLighting.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "BsRendererMaterial.h"
  6. #include "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. SPtr<Texture> texture;
  67. bool customTexture : 1;
  68. bool textureDirty : 1;
  69. bool arrayDirty : 1;
  70. mutable bool errorFlagged : 1;
  71. };
  72. BS_PARAM_BLOCK_BEGIN(TiledImageBasedLightingParamDef)
  73. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  74. BS_PARAM_BLOCK_END
  75. extern TiledImageBasedLightingParamDef gTiledImageBasedLightingParamDef;
  76. /** Helper struct containing all parameters for binding image lighting related data to the GPU programs using them .*/
  77. struct ImageBasedLightingParams
  78. {
  79. /**
  80. * Initializes the parameters from the provided parameters.
  81. *
  82. * @param[in] paramsSet GPU parameters object to look for the parameters in.
  83. * @param[in] programType Type of the GPU program to look up the parameters for.
  84. * @param[in] optional If true no warnings will be thrown if some or all of the parameters will be found.
  85. * @param[in] gridIndices Set to true if grid indices (used by light grid) parameter is required.
  86. */
  87. void populate(const SPtr<GpuParamsSet>& paramsSet, GpuProgramType programType, bool optional, bool gridIndices);
  88. GpuParamTexture skyReflectionsTexParam;
  89. GpuParamTexture skyIrradianceTexParam;
  90. GpuParamTexture reflectionProbeCubemapsTexParam;
  91. GpuParamTexture preintegratedEnvBRDFParam;
  92. GpuParamBuffer reflectionProbesParam;
  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 SkyInfo& 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. GBufferInput gbuffer;
  114. SPtr<Texture> lightAccumulation;
  115. SPtr<Texture> sceneColorTex;
  116. SPtr<GpuBuffer> sceneColorBuffer;
  117. SPtr<Texture> preIntegratedGF;
  118. };
  119. TiledDeferredImageBasedLightingMat();
  120. /** Binds the material for rendering, sets up parameters and executes it. */
  121. void execute(const RendererView& view, const SceneInfo& sceneInfo, const VisibleReflProbeData& probeData,
  122. const Inputs& inputs);
  123. /** Returns the material variation matching the provided parameters. */
  124. static TiledDeferredImageBasedLightingMat* getVariation(UINT32 msaaCount);
  125. private:
  126. UINT32 mSampleCount;
  127. GpuParamTexture mGBufferA;
  128. GpuParamTexture mGBufferB;
  129. GpuParamTexture mGBufferC;
  130. GpuParamTexture mGBufferDepth;
  131. GpuParamTexture mInColorTextureParam;
  132. ImageBasedLightingParams mImageBasedParams;
  133. GpuParamLoadStoreTexture mOutputTextureParam;
  134. GpuParamBuffer mOutputBufferParam;
  135. SPtr<GpuParamBlockBuffer> mParamBuffer;
  136. ReflProbeParamBuffer mReflProbeParamBuffer;
  137. static const UINT32 TILE_SIZE;
  138. static ShaderVariation VAR_1MSAA;
  139. static ShaderVariation VAR_2MSAA;
  140. static ShaderVariation VAR_4MSAA;
  141. static ShaderVariation VAR_8MSAA;
  142. };
  143. /** @} */
  144. }}