BsImageBasedLighting.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_END
  55. extern ReflProbeParamsParamDef gReflProbeParamsParamDef;
  56. /** Renderer information specific to a single reflection probe. */
  57. class RendererReflectionProbe
  58. {
  59. public:
  60. RendererReflectionProbe(ReflectionProbe* probe);
  61. /** Populates the structure with reflection probe parameters. */
  62. void getParameters(ReflProbeData& output) const;
  63. ReflectionProbe* probe;
  64. UINT32 arrayIdx;
  65. bool arrayDirty : 1;
  66. mutable bool errorFlagged : 1;
  67. };
  68. BS_PARAM_BLOCK_BEGIN(TiledImageBasedLightingParamDef)
  69. BS_PARAM_BLOCK_ENTRY(Vector2I, gFramebufferSize)
  70. BS_PARAM_BLOCK_END
  71. extern TiledImageBasedLightingParamDef gTiledImageBasedLightingParamDef;
  72. /** Helper struct containing all parameters for binding image lighting related data to the GPU programs using them .*/
  73. struct ImageBasedLightingParams
  74. {
  75. /**
  76. * Initializes the parameters from the provided parameters.
  77. *
  78. * @param[in] paramsSet GPU parameters object to look for the parameters in.
  79. * @param[in] programType Type of the GPU program to look up the parameters for.
  80. * @param[in] optional If true no warnings will be thrown if some or all of the parameters will be found.
  81. * @param[in] gridIndices Set to true if grid indices (used by light grid) parameter is required.
  82. */
  83. void populate(const SPtr<GpuParamsSet>& paramsSet, GpuProgramType programType, bool optional, bool gridIndices);
  84. GpuParamTexture skyReflectionsTexParam;
  85. GpuParamTexture reflectionProbeCubemapsTexParam;
  86. GpuParamTexture preintegratedEnvBRDFParam;
  87. GpuParamBuffer reflectionProbesParam;
  88. GpuParamBuffer reflectionProbeIndicesParam;
  89. UINT32 reflProbeParamsBindingIdx;
  90. };
  91. /** Parameter buffer containing information about reflection probes. */
  92. struct ReflProbeParamBuffer
  93. {
  94. ReflProbeParamBuffer();
  95. /** Updates the parameter buffer contents with require refl. probe data. */
  96. void populate(const Skybox* sky, const VisibleReflProbeData& probeData,
  97. const SPtr<Texture>& reflectionCubemaps, bool capturingReflections);
  98. SPtr<GpuParamBlockBuffer> buffer;
  99. };
  100. /** Shader that performs a lighting pass over data stored in the Gbuffer. */
  101. class TiledDeferredImageBasedLightingMat : public RendererMaterial<TiledDeferredImageBasedLightingMat>
  102. {
  103. RMAT_DEF("TiledDeferredImageBasedLighting.bsl");
  104. public:
  105. /** Container for parameters to be passed to the execute() method. */
  106. struct Inputs
  107. {
  108. GBufferTextures gbuffer;
  109. SPtr<Texture> lightAccumulation;
  110. SPtr<Texture> sceneColorTex;
  111. SPtr<GpuBuffer> sceneColorBuffer;
  112. SPtr<Texture> preIntegratedGF;
  113. };
  114. TiledDeferredImageBasedLightingMat();
  115. /** Binds the material for rendering, sets up parameters and executes it. */
  116. void execute(const RendererView& view, const SceneInfo& sceneInfo, const VisibleReflProbeData& probeData,
  117. const Inputs& inputs);
  118. /** Returns the material variation matching the provided parameters. */
  119. static TiledDeferredImageBasedLightingMat* getVariation(UINT32 msaaCount);
  120. private:
  121. UINT32 mSampleCount;
  122. GpuParamTexture mGBufferA;
  123. GpuParamTexture mGBufferB;
  124. GpuParamTexture mGBufferC;
  125. GpuParamTexture mGBufferDepth;
  126. GpuParamTexture mInColorTextureParam;
  127. ImageBasedLightingParams mImageBasedParams;
  128. GpuParamLoadStoreTexture mOutputTextureParam;
  129. GpuParamBuffer mOutputBufferParam;
  130. SPtr<GpuParamBlockBuffer> mParamBuffer;
  131. ReflProbeParamBuffer mReflProbeParamBuffer;
  132. static const UINT32 TILE_SIZE;
  133. static ShaderVariation VAR_1MSAA;
  134. static ShaderVariation VAR_2MSAA;
  135. static ShaderVariation VAR_4MSAA;
  136. static ShaderVariation VAR_8MSAA;
  137. };
  138. /** @} */
  139. }}