BsStandardDeferredLighting.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. SAMPLER_STATE_DESC desc;
  83. desc.minFilter = FO_POINT;
  84. desc.magFilter = FO_POINT;
  85. desc.mipFilter = FO_POINT;
  86. desc.addressMode.u = TAM_CLAMP;
  87. desc.addressMode.v = TAM_CLAMP;
  88. desc.addressMode.w = TAM_CLAMP;
  89. SPtr<SamplerState> samplerState = SamplerState::create(desc);
  90. mIBLParams.ssrSampParam.set(samplerState);
  91. mIBLParams.ambientOcclusionSampParam.set(samplerState);
  92. }
  93. void DeferredIBLSetupMat::bind(const GBufferTextures& gBufferInput, const SPtr<GpuParamBlockBuffer>& perCamera,
  94. const SPtr<Texture>& ssr, const SPtr<Texture>& ao, const SPtr<GpuParamBlockBuffer>& reflProbeParams)
  95. {
  96. mGBufferParams.bind(gBufferInput);
  97. mParams->setParamBlockBuffer("PerCamera", perCamera);
  98. mParams->setParamBlockBuffer("ReflProbeParams", reflProbeParams);
  99. mIBLParams.ambientOcclusionTexParam.set(ao);
  100. mIBLParams.ssrTexParam.set(ssr);
  101. RendererMaterial::bind();
  102. }
  103. DeferredIBLSetupMat* DeferredIBLSetupMat::getVariation(bool msaa, bool singleSampleMSAA)
  104. {
  105. if(msaa)
  106. {
  107. if (singleSampleMSAA)
  108. return get(getVariation<true, true>());
  109. return get(getVariation<true, false>());
  110. }
  111. else
  112. {
  113. return get(getVariation<false, false>());
  114. }
  115. }
  116. DeferredIBLProbeMat::DeferredIBLProbeMat()
  117. :mGBufferParams(GPT_FRAGMENT_PROGRAM, mParams)
  118. {
  119. mIBLParams.populate(mParams, GPT_FRAGMENT_PROGRAM, true, false, false);
  120. mParamBuffer = gPerProbeParamDef.createBuffer();
  121. mParams->setParamBlockBuffer("PerProbe", mParamBuffer);
  122. }
  123. void DeferredIBLProbeMat::bind(const GBufferTextures& gBufferInput, const SPtr<GpuParamBlockBuffer>& perCamera,
  124. const SceneInfo& sceneInfo, const ReflProbeData& probeData, const SPtr<GpuParamBlockBuffer>& reflProbeParams)
  125. {
  126. mGBufferParams.bind(gBufferInput);
  127. mParams->setParamBlockBuffer("PerCamera", perCamera);
  128. mParams->setParamBlockBuffer("ReflProbeParams", reflProbeParams);
  129. gPerProbeParamDef.gPosition.set(mParamBuffer, probeData.position);
  130. if(probeData.type == 1)
  131. gPerProbeParamDef.gExtents.set(mParamBuffer, probeData.boxExtents);
  132. else
  133. {
  134. Vector3 extents(probeData.radius, probeData.radius, probeData.radius);
  135. gPerProbeParamDef.gExtents.set(mParamBuffer, extents);
  136. }
  137. gPerProbeParamDef.gTransitionDistance.set(mParamBuffer, probeData.transitionDistance);
  138. gPerProbeParamDef.gInvBoxTransform.set(mParamBuffer, probeData.invBoxTransform);
  139. gPerProbeParamDef.gCubemapIdx.set(mParamBuffer, probeData.cubemapIdx);
  140. gPerProbeParamDef.gType.set(mParamBuffer, probeData.type);
  141. mIBLParams.reflectionProbeCubemapsTexParam.set(sceneInfo.reflProbeCubemapsTex);
  142. RendererMaterial::bind();
  143. }
  144. DeferredIBLProbeMat* DeferredIBLProbeMat::getVariation(bool inside, bool msaa, bool singleSampleMSAA)
  145. {
  146. if(msaa)
  147. {
  148. if (inside)
  149. {
  150. if (singleSampleMSAA)
  151. return get(getVariation<true, true, true>());
  152. return get(getVariation<true, true, false>());
  153. }
  154. else
  155. {
  156. if (singleSampleMSAA)
  157. return get(getVariation<false, true, true>());
  158. return get(getVariation<false, true, false>());
  159. }
  160. }
  161. else
  162. {
  163. if (inside)
  164. return get(getVariation<true, false, false>());
  165. else
  166. return get(getVariation<false, false, false>());
  167. }
  168. }
  169. DeferredIBLSkyMat::DeferredIBLSkyMat()
  170. :mGBufferParams(GPT_FRAGMENT_PROGRAM, mParams)
  171. {
  172. mIBLParams.populate(mParams, GPT_FRAGMENT_PROGRAM, true, false, false);
  173. SAMPLER_STATE_DESC desc;
  174. desc.minFilter = FO_POINT;
  175. desc.magFilter = FO_POINT;
  176. desc.mipFilter = FO_POINT;
  177. desc.addressMode.u = TAM_CLAMP;
  178. desc.addressMode.v = TAM_CLAMP;
  179. desc.addressMode.w = TAM_CLAMP;
  180. SPtr<SamplerState> samplerState = SamplerState::create(desc);
  181. mIBLParams.ssrSampParam.set(samplerState);
  182. mIBLParams.ambientOcclusionSampParam.set(samplerState);
  183. }
  184. void DeferredIBLSkyMat::bind(const GBufferTextures& gBufferInput, const SPtr<GpuParamBlockBuffer>& perCamera,
  185. const Skybox* skybox, const SPtr<GpuParamBlockBuffer>& reflProbeParams)
  186. {
  187. mGBufferParams.bind(gBufferInput);
  188. mParams->setParamBlockBuffer("PerCamera", perCamera);
  189. mParams->setParamBlockBuffer("ReflProbeParams", reflProbeParams);
  190. if(skybox != nullptr)
  191. mIBLParams.skyReflectionsTexParam.set(skybox->getFilteredRadiance());
  192. RendererMaterial::bind();
  193. }
  194. DeferredIBLSkyMat* DeferredIBLSkyMat::getVariation(bool msaa, bool singleSampleMSAA)
  195. {
  196. if(msaa)
  197. {
  198. if (singleSampleMSAA)
  199. return get(getVariation<true, true>());
  200. return get(getVariation<true, false>());
  201. }
  202. else
  203. {
  204. return get(getVariation<false, false>());
  205. }
  206. }
  207. DeferredIBLFinalizeMat::DeferredIBLFinalizeMat()
  208. :mGBufferParams(GPT_FRAGMENT_PROGRAM, mParams)
  209. {
  210. mParams->getTextureParam(GPT_FRAGMENT_PROGRAM, "gIBLRadianceTex", mIBLRadiance);
  211. mIBLParams.populate(mParams, GPT_FRAGMENT_PROGRAM, true, false, false);
  212. }
  213. void DeferredIBLFinalizeMat::bind(const GBufferTextures& gBufferInput, const SPtr<GpuParamBlockBuffer>& perCamera,
  214. const SPtr<Texture>& iblRadiance, const SPtr<Texture>& preintegratedBrdf,
  215. const SPtr<GpuParamBlockBuffer>& reflProbeParams)
  216. {
  217. mGBufferParams.bind(gBufferInput);
  218. mParams->setParamBlockBuffer("PerCamera", perCamera);
  219. mParams->setParamBlockBuffer("ReflProbeParams", reflProbeParams);
  220. mIBLParams.preintegratedEnvBRDFParam.set(preintegratedBrdf);
  221. mIBLRadiance.set(iblRadiance);
  222. RendererMaterial::bind();
  223. }
  224. DeferredIBLFinalizeMat* DeferredIBLFinalizeMat::getVariation(bool msaa, bool singleSampleMSAA)
  225. {
  226. if(msaa)
  227. {
  228. if (singleSampleMSAA)
  229. return get(getVariation<true, true>());
  230. return get(getVariation<true, false>());
  231. }
  232. else
  233. {
  234. return get(getVariation<false, false>());
  235. }
  236. }
  237. StandardDeferred::StandardDeferred()
  238. {
  239. mPerLightBuffer = gPerLightParamDef.createBuffer();
  240. }
  241. void StandardDeferred::renderLight(LightType lightType, const RendererLight& light, const RendererView& view,
  242. const GBufferTextures& gBufferInput, const SPtr<Texture>& lightOcclusion)
  243. {
  244. const auto& viewProps = view.getProperties();
  245. bool isMSAA = view.getProperties().numSamples > 1;
  246. SPtr<GpuParamBlockBuffer> perViewBuffer = view.getPerViewBuffer();
  247. light.getParameters(mPerLightBuffer);
  248. if (lightType == LightType::Directional)
  249. {
  250. DeferredDirectionalLightMat* material = DeferredDirectionalLightMat::getVariation(isMSAA, true);
  251. material->bind(gBufferInput, lightOcclusion, perViewBuffer, mPerLightBuffer);
  252. gRendererUtility().drawScreenQuad();
  253. // Draw pixels requiring per-sample evaluation
  254. if(isMSAA)
  255. {
  256. DeferredDirectionalLightMat* msaaMaterial = DeferredDirectionalLightMat::getVariation(true, false);
  257. msaaMaterial->bind(gBufferInput, lightOcclusion, perViewBuffer, mPerLightBuffer);
  258. gRendererUtility().drawScreenQuad();
  259. }
  260. }
  261. else // Radial or spot
  262. {
  263. // Check if viewer is inside the light volume
  264. float distSqrd = (light.internal->getBounds().getCenter() - viewProps.viewOrigin).squaredLength();
  265. // Extend the bounds slighty to cover the case when the viewer is outside, but the near plane is intersecting
  266. // the light bounds. We need to be conservative since the material for rendering outside will not properly
  267. // render the inside of the light volume.
  268. float boundRadius = light.internal->getBounds().getRadius() + viewProps.nearPlane * 3.0f;
  269. bool isInside = distSqrd < (boundRadius * boundRadius);
  270. SPtr<Mesh> stencilMesh;
  271. if(lightType == LightType::Radial)
  272. stencilMesh = RendererUtility::instance().getSphereStencil();
  273. else // Spot
  274. stencilMesh = RendererUtility::instance().getSpotLightStencil();
  275. DeferredPointLightMat* material = DeferredPointLightMat::getVariation(isInside, isMSAA, true);
  276. material->bind(gBufferInput, lightOcclusion, perViewBuffer, mPerLightBuffer);
  277. // Note: If MSAA is enabled this will be rendered multisampled (on polygon edges), see if this can be avoided
  278. gRendererUtility().draw(stencilMesh);
  279. // Draw pixels requiring per-sample evaluation
  280. if(isMSAA)
  281. {
  282. DeferredPointLightMat* msaaMaterial = DeferredPointLightMat::getVariation(isInside, true, false);
  283. msaaMaterial->bind(gBufferInput, lightOcclusion, perViewBuffer, mPerLightBuffer);
  284. gRendererUtility().draw(stencilMesh);
  285. }
  286. }
  287. }
  288. void StandardDeferred::renderReflProbe(const ReflProbeData& probeData, const RendererView& view,
  289. const GBufferTextures& gBufferInput, const SceneInfo& sceneInfo, const SPtr<GpuParamBlockBuffer>& reflProbeParams)
  290. {
  291. const auto& viewProps = view.getProperties();
  292. bool isMSAA = viewProps.numSamples > 1;
  293. SPtr<GpuParamBlockBuffer> perViewBuffer = view.getPerViewBuffer();
  294. // When checking if viewer is inside the volume extend the bounds slighty to cover the case when the viewer is
  295. // outside, but the near plane is intersecting the bounds. We need to be conservative since the material for
  296. // rendering outside will not properly render the inside of the volume.
  297. float radiusBuffer = viewProps.nearPlane * 3.0f;
  298. SPtr<Mesh> stencilMesh;
  299. bool isInside;
  300. if(probeData.type == 0) // Sphere
  301. {
  302. // Check if viewer is inside the light volume
  303. float distSqrd = (probeData.position - viewProps.viewOrigin).squaredLength();
  304. float boundRadius = probeData.radius + radiusBuffer;
  305. isInside = distSqrd < (boundRadius * boundRadius);
  306. stencilMesh = RendererUtility::instance().getSphereStencil();
  307. }
  308. else // Box
  309. {
  310. Vector3 extents = probeData.boxExtents + radiusBuffer;
  311. AABox box(probeData.position - extents, probeData.position + extents);
  312. isInside = box.contains(viewProps.viewOrigin);
  313. stencilMesh = RendererUtility::instance().getBoxStencil();
  314. }
  315. DeferredIBLProbeMat* material = DeferredIBLProbeMat::getVariation(isInside, isMSAA, true);
  316. material->bind(gBufferInput, perViewBuffer, sceneInfo, probeData, reflProbeParams);
  317. // Note: If MSAA is enabled this will be rendered multisampled (on polygon edges), see if this can be avoided
  318. gRendererUtility().draw(stencilMesh);
  319. // Draw pixels requiring per-sample evaluation
  320. if (isMSAA)
  321. {
  322. DeferredIBLProbeMat* msaaMaterial = DeferredIBLProbeMat::getVariation(isInside, true, false);
  323. msaaMaterial->bind(gBufferInput, perViewBuffer, sceneInfo, probeData, reflProbeParams);
  324. gRendererUtility().draw(stencilMesh);
  325. }
  326. }
  327. }}