BsStandardDeferredLighting.h 12 KB

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