BsStandardDeferredLighting.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 "Utility/BsModule.h"
  6. #include "Renderer/BsRendererMaterial.h"
  7. #include "BsLightRendering.h"
  8. #include "BsImageBasedLighting.h"
  9. namespace bs { namespace ct {
  10. class RendererLight;
  11. BS_PARAM_BLOCK_BEGIN(PerLightParamDef)
  12. BS_PARAM_BLOCK_ENTRY(Vector4, gLightPositionAndSrcRadius)
  13. BS_PARAM_BLOCK_ENTRY(Vector4, gLightColorAndLuminance)
  14. BS_PARAM_BLOCK_ENTRY(Vector4, gLightSpotAnglesAndSqrdInvAttRadius)
  15. BS_PARAM_BLOCK_ENTRY(Vector4, gLightDirectionAndAttRadius)
  16. BS_PARAM_BLOCK_ENTRY(Vector4, gShiftedLightPositionAndType)
  17. BS_PARAM_BLOCK_ENTRY(Vector4, gLightGeometry)
  18. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatConeTransform)
  19. BS_PARAM_BLOCK_END
  20. extern PerLightParamDef gPerLightParamDef;
  21. /** Shader that renders directional light sources during deferred rendering light pass. */
  22. class DeferredDirectionalLightMat : public RendererMaterial<DeferredDirectionalLightMat>
  23. {
  24. RMAT_DEF("DeferredDirectionalLight.bsl");
  25. /** Helper method used for initializing variations of this material. */
  26. template<bool msaa, bool singleSampleMSAA>
  27. static const ShaderVariation& getVariation()
  28. {
  29. static ShaderVariation variation = ShaderVariation({
  30. ShaderVariation::Param("MSAA", msaa),
  31. ShaderVariation::Param("MSAA_RESOLVE_0TH", singleSampleMSAA)
  32. });
  33. return variation;
  34. }
  35. public:
  36. DeferredDirectionalLightMat();
  37. /** Binds the material for rendering and sets up any parameters. */
  38. void bind(const GBufferTextures& gBufferInput, const SPtr<Texture>& lightOcclusion,
  39. const SPtr<GpuParamBlockBuffer>& perCamera, const SPtr<GpuParamBlockBuffer>& perLight);
  40. /**
  41. * Returns the material variation matching the provided parameters.
  42. *
  43. * @param[in] msaa True if the shader will operate on a multisampled surface.
  44. * @param[in] singleSampleMSAA Only relevant of @p msaa is true. When enabled only the first sample will be
  45. * evaluated. Otherwise all samples will be evaluated.
  46. * @return Requested variation of the material.
  47. */
  48. static DeferredDirectionalLightMat* getVariation(bool msaa, bool singleSampleMSAA = false);
  49. private:
  50. GBufferParams mGBufferParams;
  51. GpuParamTexture mLightOcclusionTexParam;
  52. };
  53. /** Shader that renders point (radial & spot) light sources during deferred rendering light pass. */
  54. class DeferredPointLightMat : public RendererMaterial<DeferredPointLightMat>
  55. {
  56. RMAT_DEF("DeferredPointLight.bsl");
  57. /** Helper method used for initializing variations of this material. */
  58. template<bool inside, bool msaa, bool singleSampleMSAA>
  59. static const ShaderVariation& getVariation()
  60. {
  61. static ShaderVariation variation = ShaderVariation({
  62. ShaderVariation::Param("MSAA", msaa),
  63. ShaderVariation::Param("INSIDE_GEOMETRY", inside),
  64. ShaderVariation::Param("MSAA_RESOLVE_0TH", singleSampleMSAA)
  65. });
  66. return variation;
  67. }
  68. public:
  69. DeferredPointLightMat();
  70. /** Binds the material for rendering and sets up any parameters. */
  71. void bind(const GBufferTextures& gBufferInput, const SPtr<Texture>& lightOcclusion,
  72. const SPtr<GpuParamBlockBuffer>& perCamera, const SPtr<GpuParamBlockBuffer>& perLight);
  73. /**
  74. * Returns the material variation matching the provided parameters.
  75. *
  76. * @param[in] inside Set to true if viewer is inside the light's stencil geometry.
  77. * @param[in] msaa True if the shader will operate on a multisampled surface.
  78. * @param[in] singleSampleMSAA Only relevant of @p msaa is true. When enabled only the first sample will be
  79. * evaluated. Otherwise all samples will be evaluated.
  80. * @return Requested variation of the material.
  81. */
  82. static DeferredPointLightMat* getVariation(bool inside, bool msaa, bool singleSampleMSAA = false);
  83. private:
  84. GBufferParams mGBufferParams;
  85. GpuParamTexture mLightOcclusionTexParam;
  86. };
  87. BS_PARAM_BLOCK_BEGIN(PerProbeParamDef)
  88. BS_PARAM_BLOCK_ENTRY(Vector3, gPosition)
  89. BS_PARAM_BLOCK_ENTRY(Vector3, gExtents)
  90. BS_PARAM_BLOCK_ENTRY(float, gTransitionDistance)
  91. BS_PARAM_BLOCK_ENTRY(Matrix4, gInvBoxTransform)
  92. BS_PARAM_BLOCK_ENTRY(INT32, gCubemapIdx)
  93. BS_PARAM_BLOCK_ENTRY(INT32, gType)
  94. BS_PARAM_BLOCK_END
  95. extern PerProbeParamDef gPerProbeParamDef;
  96. /**
  97. * Shader that prepares the surface for image based lighting.
  98. *
  99. * This is an alternative to TiledDeferredImageBasedLighting for cases when compute shaders are not usable or suitable.
  100. * Needs to be followed by execution of all other DeferredIBL* materials.
  101. */
  102. class DeferredIBLSetupMat : public RendererMaterial<DeferredIBLSetupMat>
  103. {
  104. RMAT_DEF("DeferredIBLSetup.bsl");
  105. /** Helper method used for initializing variations of this material. */
  106. template<bool msaa, bool singleSampleMSAA>
  107. static const ShaderVariation& getVariation()
  108. {
  109. static ShaderVariation variation = ShaderVariation({
  110. ShaderVariation::Param("MSAA", msaa),
  111. ShaderVariation::Param("MSAA_RESOLVE_0TH", singleSampleMSAA)
  112. });
  113. return variation;
  114. }
  115. public:
  116. DeferredIBLSetupMat();
  117. /** Binds the material for rendering and sets up any parameters. */
  118. void bind(const GBufferTextures& gBufferInput, const SPtr<GpuParamBlockBuffer>& perCamera,
  119. const SPtr<Texture>& ssr, const SPtr<Texture>& ao, const SPtr<GpuParamBlockBuffer>& reflProbeParams);
  120. /**
  121. * Returns the material variation matching the provided parameters.
  122. *
  123. * @param[in] msaa True if the shader will operate on a multisampled surface.
  124. * @param[in] singleSampleMSAA Only relevant of @p msaa is true. When enabled only the first sample will be
  125. * evaluated. Otherwise all samples will be evaluated.
  126. * @return Requested variation of the material.
  127. */
  128. static DeferredIBLSetupMat* getVariation(bool msaa, bool singleSampleMSAA = false);
  129. private:
  130. GBufferParams mGBufferParams;
  131. ImageBasedLightingParams mIBLParams;
  132. };
  133. /**
  134. * Shader that renders an individual reflection probe for image based lighting.
  135. *
  136. * This is an alternative to TiledDeferredImageBasedLighting for cases when compute shaders are not usable or suitable.
  137. * Must be preceeded by DeferredIBLSetupMat and followed by DeferredIBLSkyMat and DeferredIBLFinalizeMat.
  138. */
  139. class DeferredIBLProbeMat : public RendererMaterial<DeferredIBLProbeMat>
  140. {
  141. RMAT_DEF("DeferredIBLProbe.bsl");
  142. /** Helper method used for initializing variations of this material. */
  143. template<bool inside, bool msaa, bool singleSampleMSAA>
  144. static const ShaderVariation& getVariation()
  145. {
  146. static ShaderVariation variation = ShaderVariation({
  147. ShaderVariation::Param("MSAA", msaa),
  148. ShaderVariation::Param("INSIDE_GEOMETRY", inside),
  149. ShaderVariation::Param("MSAA_RESOLVE_0TH", singleSampleMSAA)
  150. });
  151. return variation;
  152. }
  153. public:
  154. DeferredIBLProbeMat();
  155. /** Binds the material for rendering and sets up any parameters. */
  156. void bind(const GBufferTextures& gBufferInput, const SPtr<GpuParamBlockBuffer>& perCamera,
  157. const SceneInfo& sceneInfo, const ReflProbeData& probeData, const SPtr<GpuParamBlockBuffer>& reflProbeParams);
  158. /**
  159. * Returns the material variation matching the provided parameters.
  160. *
  161. * @param[in] inside Set to true if viewer is inside the probe's stencil geometry.
  162. * @param[in] msaa True if the shader will operate on a multisampled surface.
  163. * @param[in] singleSampleMSAA Only relevant of @p msaa is true. When enabled only the first sample will be
  164. * evaluated. Otherwise all samples will be evaluated.
  165. * @return Requested variation of the material.
  166. */
  167. static DeferredIBLProbeMat* getVariation(bool inside, bool msaa, bool singleSampleMSAA = false);
  168. private:
  169. SPtr<GpuParamBlockBuffer> mParamBuffer;
  170. GBufferParams mGBufferParams;
  171. ImageBasedLightingParams mIBLParams;
  172. };
  173. /**
  174. * Shader that renders the sky reflections. The results are additively blended with the currently bound render target.
  175. *
  176. * This is an alternative to TiledDeferredImageBasedLighting for cases when compute shaders are not usable or suitable.
  177. * Must be preceeded by DeferredIBLSetupMat and followed by DeferredIBLFinalizeMat.
  178. */
  179. class DeferredIBLSkyMat : public RendererMaterial<DeferredIBLSkyMat>
  180. {
  181. RMAT_DEF("DeferredIBLSky.bsl");
  182. /** Helper method used for initializing variations of this material. */
  183. template<bool msaa, bool singleSampleMSAA>
  184. static const ShaderVariation& getVariation()
  185. {
  186. static ShaderVariation variation = ShaderVariation({
  187. ShaderVariation::Param("MSAA", msaa),
  188. ShaderVariation::Param("MSAA_RESOLVE_0TH", singleSampleMSAA)
  189. });
  190. return variation;
  191. }
  192. public:
  193. DeferredIBLSkyMat();
  194. /** Binds the material for rendering and sets up any parameters. */
  195. void bind(const GBufferTextures& gBufferInput, const SPtr<GpuParamBlockBuffer>& perCamera,
  196. const Skybox* skybox, const SPtr<GpuParamBlockBuffer>& reflProbeParams);
  197. /**
  198. * Returns the material variation matching the provided parameters.
  199. *
  200. * @param[in] msaa True if the shader will operate on a multisampled surface.
  201. * @param[in] singleSampleMSAA Only relevant of @p msaa is true. When enabled only the first sample will be
  202. * evaluated. Otherwise all samples will be evaluated.
  203. * @return Requested variation of the material.
  204. */
  205. static DeferredIBLSkyMat* getVariation(bool msaa, bool singleSampleMSAA = false);
  206. private:
  207. GBufferParams mGBufferParams;
  208. ImageBasedLightingParams mIBLParams;
  209. };
  210. /**
  211. * Material that finalizes the rendering of reflections. As input it takes the texture output by previous DeferredIBL*
  212. * materials, and the resulting output is blended additively with the current render target.
  213. *
  214. * This is an alternative to TiledDeferredImageBasedLighting for cases when compute shaders are not usable or suitable.
  215. */
  216. class DeferredIBLFinalizeMat : public RendererMaterial<DeferredIBLFinalizeMat>
  217. {
  218. RMAT_DEF("DeferredIBLFinalize.bsl");
  219. /** Helper method used for initializing variations of this material. */
  220. template<bool msaa, bool singleSampleMSAA>
  221. static const ShaderVariation& getVariation()
  222. {
  223. static ShaderVariation variation = ShaderVariation({
  224. ShaderVariation::Param("MSAA", msaa),
  225. ShaderVariation::Param("MSAA_RESOLVE_0TH", singleSampleMSAA)
  226. });
  227. return variation;
  228. }
  229. public:
  230. DeferredIBLFinalizeMat();
  231. /** Binds the material for rendering and sets up any parameters. */
  232. void bind(const GBufferTextures& gBufferInput, const SPtr<GpuParamBlockBuffer>& perCamera,
  233. const SPtr<Texture>& iblRadiance, const SPtr<Texture>& preintegratedBrdf,
  234. const SPtr<GpuParamBlockBuffer>& reflProbeParams);
  235. /**
  236. * Returns the material variation matching the provided parameters.
  237. *
  238. * @param[in] msaa True if the shader will operate on a multisampled surface.
  239. * @param[in] singleSampleMSAA Only relevant of @p msaa is true. When enabled only the first sample will be
  240. * evaluated. Otherwise all samples will be evaluated.
  241. * @return Requested variation of the material.
  242. */
  243. static DeferredIBLFinalizeMat* getVariation(bool msaa, bool singleSampleMSAA = false);
  244. private:
  245. GBufferParams mGBufferParams;
  246. ImageBasedLightingParams mIBLParams;
  247. GpuParamTexture mIBLRadiance;
  248. };
  249. /** Provides functionality for standard (non-tiled) deferred rendering. */
  250. class StandardDeferred : public Module<StandardDeferred>
  251. {
  252. public:
  253. StandardDeferred();
  254. /** Calculates lighting for the specified light, using the standard deferred renderer. */
  255. void renderLight(LightType type, const RendererLight& light, const RendererView& view,
  256. const GBufferTextures& gBufferInput, const SPtr<Texture>& lightOcclusion);
  257. /**
  258. * Evaluates filtered radiance from a single reflection probe and blends it into the current render target.
  259. * Alpha value of the render target is used for determining the contribution and will be updated with new
  260. * contibution after blending.
  261. */
  262. void renderReflProbe(const ReflProbeData& probeData, const RendererView& view,
  263. const GBufferTextures& gBufferInput, const SceneInfo& sceneInfo,
  264. const SPtr<GpuParamBlockBuffer>& reflProbeParams);
  265. private:
  266. SPtr<GpuParamBlockBuffer> mPerLightBuffer;
  267. };
  268. }}