BsStandardDeferredLighting.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsStandardDeferredLighting.h"
  4. #include "Renderer/BsRendererUtility.h"
  5. #include "BsRendererView.h"
  6. #include "Material/BsGpuParamsSet.h"
  7. #include "Mesh/BsMesh.h"
  8. #include "Renderer/BsSkybox.h"
  9. #include "BsRendererScene.h"
  10. #include "Renderer/BsReflectionProbe.h"
  11. namespace bs { namespace ct {
  12. PerLightParamDef gPerLightParamDef;
  13. DeferredDirectionalLightMat::DeferredDirectionalLightMat()
  14. :mGBufferParams(GPT_FRAGMENT_PROGRAM, mParams)
  15. {
  16. mParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gLightOcclusionTex", mLightOcclusionTexParam);
  17. }
  18. void DeferredDirectionalLightMat::bind(const GBufferTextures& gBufferInput, const SPtr<Texture>& lightOcclusion,
  19. const SPtr<GpuParamBlockBuffer>& perCamera, const SPtr<GpuParamBlockBuffer>& perLight)
  20. {
  21. mGBufferParams.bind(gBufferInput);
  22. mLightOcclusionTexParam.set(lightOcclusion);
  23. mParams->setParamBlockBuffer("PerCamera", perCamera);
  24. mParams->setParamBlockBuffer("PerLight", perLight);
  25. RendererMaterial::bind();
  26. }
  27. DeferredDirectionalLightMat* DeferredDirectionalLightMat::getVariation(bool msaa, bool singleSampleMSAA)
  28. {
  29. if (msaa)
  30. {
  31. if (singleSampleMSAA)
  32. return get(getVariation<true, true>());
  33. else
  34. return get(getVariation<true, false>());
  35. }
  36. return get(getVariation<false, false>());
  37. }
  38. DeferredPointLightMat::DeferredPointLightMat()
  39. :mGBufferParams(GPT_FRAGMENT_PROGRAM, mParams)
  40. {
  41. mParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gLightOcclusionTex", mLightOcclusionTexParam);
  42. }
  43. void DeferredPointLightMat::bind(const GBufferTextures& gBufferInput, const SPtr<Texture>& lightOcclusion,
  44. const SPtr<GpuParamBlockBuffer>& perCamera, const SPtr<GpuParamBlockBuffer>& perLight)
  45. {
  46. mGBufferParams.bind(gBufferInput);
  47. mLightOcclusionTexParam.set(lightOcclusion);
  48. mParams->setParamBlockBuffer("PerCamera", perCamera);
  49. mParams->setParamBlockBuffer("PerLight", perLight);
  50. RendererMaterial::bind();
  51. }
  52. DeferredPointLightMat* DeferredPointLightMat::getVariation(bool inside, bool msaa, bool singleSampleMSAA)
  53. {
  54. if(msaa)
  55. {
  56. if (inside)
  57. {
  58. if (singleSampleMSAA)
  59. return get(getVariation<true, true, true>());
  60. return get(getVariation<true, true, false>());
  61. }
  62. else
  63. {
  64. if (singleSampleMSAA)
  65. return get(getVariation<false, true, true>());
  66. return get(getVariation<false, true, false>());
  67. }
  68. }
  69. else
  70. {
  71. if (inside)
  72. return get(getVariation<true, false, false>());
  73. else
  74. return get(getVariation<false, false, false>());
  75. }
  76. }
  77. PerProbeParamDef gPerProbeParamDef;
  78. DeferredIBLSetupMat::DeferredIBLSetupMat()
  79. :mGBufferParams(GPT_FRAGMENT_PROGRAM, mParams)
  80. {
  81. mIBLParams.populate(mParams, GPT_FRAGMENT_PROGRAM, true, false, false);
  82. }
  83. void DeferredIBLSetupMat::bind(const GBufferTextures& gBufferInput, const SPtr<GpuParamBlockBuffer>& perCamera,
  84. const SPtr<Texture>& ssr, const SPtr<Texture>& ao, const SPtr<GpuParamBlockBuffer>& reflProbeParams)
  85. {
  86. mGBufferParams.bind(gBufferInput);
  87. mParams->setParamBlockBuffer("PerCamera", perCamera);
  88. mParams->setParamBlockBuffer("ReflProbeParams", reflProbeParams);
  89. mIBLParams.ambientOcclusionTexParam.set(ao);
  90. mIBLParams.ssrTexParam.set(ssr);
  91. RendererMaterial::bind();
  92. }
  93. DeferredIBLSetupMat* DeferredIBLSetupMat::getVariation(bool msaa, bool singleSampleMSAA)
  94. {
  95. if(msaa)
  96. {
  97. if (singleSampleMSAA)
  98. return get(getVariation<true, true>());
  99. return get(getVariation<true, false>());
  100. }
  101. else
  102. {
  103. return get(getVariation<false, false>());
  104. }
  105. }
  106. DeferredIBLProbeMat::DeferredIBLProbeMat()
  107. :mGBufferParams(GPT_FRAGMENT_PROGRAM, mParams)
  108. {
  109. mIBLParams.populate(mParams, GPT_FRAGMENT_PROGRAM, true, false, false);
  110. mParamBuffer = gPerProbeParamDef.createBuffer();
  111. mParams->setParamBlockBuffer("PerProbe", mParamBuffer);
  112. }
  113. void DeferredIBLProbeMat::bind(const GBufferTextures& gBufferInput, const SPtr<GpuParamBlockBuffer>& perCamera,
  114. const SceneInfo& sceneInfo, const ReflProbeData& probeData, const SPtr<GpuParamBlockBuffer>& reflProbeParams)
  115. {
  116. mGBufferParams.bind(gBufferInput);
  117. mParams->setParamBlockBuffer("PerCamera", perCamera);
  118. mParams->setParamBlockBuffer("ReflProbeParams", reflProbeParams);
  119. gPerProbeParamDef.gPosition.set(mParamBuffer, probeData.position);
  120. if(probeData.type == 1)
  121. gPerProbeParamDef.gExtents.set(mParamBuffer, probeData.boxExtents);
  122. else
  123. {
  124. Vector3 extents(probeData.radius, probeData.radius, probeData.radius);
  125. gPerProbeParamDef.gExtents.set(mParamBuffer, extents);
  126. }
  127. gPerProbeParamDef.gTransitionDistance.set(mParamBuffer, probeData.transitionDistance);
  128. gPerProbeParamDef.gInvBoxTransform.set(mParamBuffer, probeData.invBoxTransform);
  129. gPerProbeParamDef.gCubemapIdx.set(mParamBuffer, probeData.cubemapIdx);
  130. gPerProbeParamDef.gType.set(mParamBuffer, probeData.type);
  131. mIBLParams.reflectionProbeCubemapsTexParam.set(sceneInfo.reflProbeCubemapsTex);
  132. RendererMaterial::bind();
  133. }
  134. DeferredIBLProbeMat* DeferredIBLProbeMat::getVariation(bool inside, bool msaa, bool singleSampleMSAA)
  135. {
  136. if(msaa)
  137. {
  138. if (inside)
  139. {
  140. if (singleSampleMSAA)
  141. return get(getVariation<true, true, true>());
  142. return get(getVariation<true, true, false>());
  143. }
  144. else
  145. {
  146. if (singleSampleMSAA)
  147. return get(getVariation<false, true, true>());
  148. return get(getVariation<false, true, false>());
  149. }
  150. }
  151. else
  152. {
  153. if (inside)
  154. return get(getVariation<true, false, false>());
  155. else
  156. return get(getVariation<false, false, false>());
  157. }
  158. }
  159. DeferredIBLSkyMat::DeferredIBLSkyMat()
  160. :mGBufferParams(GPT_FRAGMENT_PROGRAM, mParams)
  161. {
  162. mIBLParams.populate(mParams, GPT_FRAGMENT_PROGRAM, true, false, false);
  163. }
  164. void DeferredIBLSkyMat::bind(const GBufferTextures& gBufferInput, const SPtr<GpuParamBlockBuffer>& perCamera,
  165. const Skybox* skybox, const SPtr<GpuParamBlockBuffer>& reflProbeParams)
  166. {
  167. mGBufferParams.bind(gBufferInput);
  168. mParams->setParamBlockBuffer("PerCamera", perCamera);
  169. mParams->setParamBlockBuffer("ReflProbeParams", reflProbeParams);
  170. if(skybox != nullptr)
  171. mIBLParams.skyReflectionsTexParam.set(skybox->getFilteredRadiance());
  172. RendererMaterial::bind();
  173. }
  174. DeferredIBLSkyMat* DeferredIBLSkyMat::getVariation(bool msaa, bool singleSampleMSAA)
  175. {
  176. if(msaa)
  177. {
  178. if (singleSampleMSAA)
  179. return get(getVariation<true, true>());
  180. return get(getVariation<true, false>());
  181. }
  182. else
  183. {
  184. return get(getVariation<false, false>());
  185. }
  186. }
  187. DeferredIBLFinalizeMat::DeferredIBLFinalizeMat()
  188. :mGBufferParams(GPT_FRAGMENT_PROGRAM, mParams)
  189. {
  190. mParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gIBLRadianceTex", mIBLRadiance);
  191. mIBLParams.populate(mParams, GPT_FRAGMENT_PROGRAM, true, false, false);
  192. }
  193. void DeferredIBLFinalizeMat::bind(const GBufferTextures& gBufferInput, const SPtr<GpuParamBlockBuffer>& perCamera,
  194. const SPtr<Texture>& iblRadiance, const SPtr<Texture>& preintegratedBrdf,
  195. const SPtr<GpuParamBlockBuffer>& reflProbeParams)
  196. {
  197. mGBufferParams.bind(gBufferInput);
  198. mParams->setParamBlockBuffer("PerCamera", perCamera);
  199. mParams->setParamBlockBuffer("ReflProbeParams", reflProbeParams);
  200. mIBLParams.preintegratedEnvBRDFParam.set(preintegratedBrdf);
  201. mIBLRadiance.set(iblRadiance);
  202. RendererMaterial::bind();
  203. }
  204. DeferredIBLFinalizeMat* DeferredIBLFinalizeMat::getVariation(bool msaa, bool singleSampleMSAA)
  205. {
  206. if(msaa)
  207. {
  208. if (singleSampleMSAA)
  209. return get(getVariation<true, true>());
  210. return get(getVariation<true, false>());
  211. }
  212. else
  213. {
  214. return get(getVariation<false, false>());
  215. }
  216. }
  217. StandardDeferred::StandardDeferred()
  218. {
  219. mPerLightBuffer = gPerLightParamDef.createBuffer();
  220. }
  221. void StandardDeferred::renderLight(LightType lightType, const RendererLight& light, const RendererView& view,
  222. const GBufferTextures& gBufferInput, const SPtr<Texture>& lightOcclusion)
  223. {
  224. const auto& viewProps = view.getProperties();
  225. bool isMSAA = view.getProperties().numSamples > 1;
  226. SPtr<GpuParamBlockBuffer> perViewBuffer = view.getPerViewBuffer();
  227. light.getParameters(mPerLightBuffer);
  228. if (lightType == LightType::Directional)
  229. {
  230. DeferredDirectionalLightMat* material = DeferredDirectionalLightMat::getVariation(isMSAA, true);
  231. material->bind(gBufferInput, lightOcclusion, perViewBuffer, mPerLightBuffer);
  232. gRendererUtility().drawScreenQuad();
  233. // Draw pixels requiring per-sample evaluation
  234. if(isMSAA)
  235. {
  236. DeferredDirectionalLightMat* msaaMaterial = DeferredDirectionalLightMat::getVariation(true, false);
  237. msaaMaterial->bind(gBufferInput, lightOcclusion, perViewBuffer, mPerLightBuffer);
  238. gRendererUtility().drawScreenQuad();
  239. }
  240. }
  241. else // Radial or spot
  242. {
  243. // Check if viewer is inside the light volume
  244. float distSqrd = (light.internal->getBounds().getCenter() - viewProps.viewOrigin).squaredLength();
  245. // Extend the bounds slighty to cover the case when the viewer is outside, but the near plane is intersecting
  246. // the light bounds. We need to be conservative since the material for rendering outside will not properly
  247. // render the inside of the light volume.
  248. float boundRadius = light.internal->getBounds().getRadius() + viewProps.nearPlane * 3.0f;
  249. bool isInside = distSqrd < (boundRadius * boundRadius);
  250. SPtr<Mesh> stencilMesh;
  251. if(lightType == LightType::Radial)
  252. stencilMesh = RendererUtility::instance().getSphereStencil();
  253. else // Spot
  254. stencilMesh = RendererUtility::instance().getSpotLightStencil();
  255. DeferredPointLightMat* material = DeferredPointLightMat::getVariation(isInside, isMSAA, true);
  256. material->bind(gBufferInput, lightOcclusion, perViewBuffer, mPerLightBuffer);
  257. // Note: If MSAA is enabled this will be rendered multisampled (on polygon edges), see if this can be avoided
  258. gRendererUtility().draw(stencilMesh);
  259. // Draw pixels requiring per-sample evaluation
  260. if(isMSAA)
  261. {
  262. DeferredPointLightMat* msaaMaterial = DeferredPointLightMat::getVariation(isInside, true, false);
  263. msaaMaterial->bind(gBufferInput, lightOcclusion, perViewBuffer, mPerLightBuffer);
  264. gRendererUtility().draw(stencilMesh);
  265. }
  266. }
  267. }
  268. void StandardDeferred::renderReflProbe(const ReflProbeData& probeData, const RendererView& view,
  269. const GBufferTextures& gBufferInput, const SceneInfo& sceneInfo, const SPtr<GpuParamBlockBuffer>& reflProbeParams)
  270. {
  271. const auto& viewProps = view.getProperties();
  272. bool isMSAA = viewProps.numSamples > 1;
  273. SPtr<GpuParamBlockBuffer> perViewBuffer = view.getPerViewBuffer();
  274. // When checking if viewer is inside the volume extend the bounds slighty to cover the case when the viewer is
  275. // outside, but the near plane is intersecting the bounds. We need to be conservative since the material for
  276. // rendering outside will not properly render the inside of the volume.
  277. float radiusBuffer = viewProps.nearPlane * 3.0f;
  278. SPtr<Mesh> stencilMesh;
  279. bool isInside;
  280. if(probeData.type == 0) // Sphere
  281. {
  282. // Check if viewer is inside the light volume
  283. float distSqrd = (probeData.position - viewProps.viewOrigin).squaredLength();
  284. float boundRadius = probeData.radius + radiusBuffer;
  285. isInside = distSqrd < (boundRadius * boundRadius);
  286. stencilMesh = RendererUtility::instance().getSphereStencil();
  287. }
  288. else // Box
  289. {
  290. Vector3 extents = probeData.boxExtents + radiusBuffer;
  291. AABox box(probeData.position - extents, probeData.position + extents);
  292. isInside = box.contains(viewProps.viewOrigin);
  293. stencilMesh = RendererUtility::instance().getBoxStencil();
  294. }
  295. DeferredIBLProbeMat* material = DeferredIBLProbeMat::getVariation(isInside, isMSAA, true);
  296. material->bind(gBufferInput, perViewBuffer, sceneInfo, probeData, reflProbeParams);
  297. // Note: If MSAA is enabled this will be rendered multisampled (on polygon edges), see if this can be avoided
  298. gRendererUtility().draw(stencilMesh);
  299. // Draw pixels requiring per-sample evaluation
  300. if (isMSAA)
  301. {
  302. DeferredIBLProbeMat* msaaMaterial = DeferredIBLProbeMat::getVariation(isInside, true, false);
  303. msaaMaterial->bind(gBufferInput, perViewBuffer, sceneInfo, probeData, reflProbeParams);
  304. gRendererUtility().draw(stencilMesh);
  305. }
  306. }
  307. }}