BsStandardDeferredLighting.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. namespace bs { namespace ct {
  9. PerLightParamDef gPerLightParamDef;
  10. ShaderVariation DirectionalLightMat::VAR_FullMSAA = ShaderVariation({
  11. ShaderVariation::Param("MSAA_COUNT", 2),
  12. ShaderVariation::Param("MSAA", true)
  13. });
  14. ShaderVariation DirectionalLightMat::VAR_SingleMSAA = ShaderVariation({
  15. ShaderVariation::Param("MSAA_COUNT", 2),
  16. ShaderVariation::Param("MSAA", true),
  17. ShaderVariation::Param("MSAA_RESOLVE_0TH", true)
  18. });
  19. ShaderVariation DirectionalLightMat::VAR_NoMSAA = ShaderVariation({
  20. ShaderVariation::Param("MSAA_COUNT", 1)
  21. });
  22. DirectionalLightMat::DirectionalLightMat()
  23. :mGBufferParams(mMaterial, mParamsSet)
  24. {
  25. const GpuParams& gpuParams = *mParamsSet->getGpuParams();
  26. gpuParams.getTextureParam(GPT_FRAGMENT_PROGRAM, "gLightOcclusionTex", mLightOcclusionTexParam);
  27. }
  28. void DirectionalLightMat::_initVariations(ShaderVariations& variations)
  29. {
  30. variations.add(VAR_FullMSAA);
  31. variations.add(VAR_SingleMSAA);
  32. variations.add(VAR_NoMSAA);
  33. }
  34. void DirectionalLightMat::bind(const GBufferTextures& gBufferInput, const SPtr<Texture>& lightOcclusion,
  35. const SPtr<GpuParamBlockBuffer>& perCamera)
  36. {
  37. RendererUtility::instance().setPass(mMaterial, 0);
  38. mGBufferParams.bind(gBufferInput);
  39. mLightOcclusionTexParam.set(lightOcclusion);
  40. mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perCamera);
  41. }
  42. void DirectionalLightMat::setPerLightParams(const SPtr<GpuParamBlockBuffer>& perLight)
  43. {
  44. mParamsSet->getGpuParams()->setParamBlockBuffer("PerLight", perLight);
  45. gRendererUtility().setPassParams(mParamsSet);
  46. }
  47. DirectionalLightMat* DirectionalLightMat::getVariation(bool msaa, bool singleSampleMSAA)
  48. {
  49. if (msaa)
  50. {
  51. if (singleSampleMSAA)
  52. return get(VAR_SingleMSAA);
  53. else
  54. return get(VAR_FullMSAA);
  55. }
  56. return get(VAR_NoMSAA);
  57. }
  58. ShaderVariation PointLightMat::VAR_FullMSAA_Inside = ShaderVariation({
  59. ShaderVariation::Param("MSAA_COUNT", 2),
  60. ShaderVariation::Param("MSAA", true),
  61. ShaderVariation::Param("INSIDE_GEOMETRY", true)
  62. });
  63. ShaderVariation PointLightMat::VAR_SingleMSAA_Inside = ShaderVariation({
  64. ShaderVariation::Param("MSAA_COUNT", 2),
  65. ShaderVariation::Param("MSAA", true),
  66. ShaderVariation::Param("INSIDE_GEOMETRY", true),
  67. ShaderVariation::Param("MSAA_RESOLVE_0TH", true)
  68. });
  69. ShaderVariation PointLightMat::VAR_FullMSAA_Outside = ShaderVariation({
  70. ShaderVariation::Param("MSAA_COUNT", 2),
  71. ShaderVariation::Param("MSAA", true)
  72. });
  73. ShaderVariation PointLightMat::VAR_SingleMSAA_Outside = ShaderVariation({
  74. ShaderVariation::Param("MSAA_COUNT", 2),
  75. ShaderVariation::Param("MSAA", true),
  76. ShaderVariation::Param("MSAA_RESOLVE_0TH", true)
  77. });
  78. ShaderVariation PointLightMat::VAR_NoMSAA_Inside = ShaderVariation({
  79. ShaderVariation::Param("MSAA_COUNT", 1),
  80. ShaderVariation::Param("INSIDE_GEOMETRY", true)
  81. });
  82. ShaderVariation PointLightMat::VAR_NoMSAA_Outside = ShaderVariation({
  83. ShaderVariation::Param("MSAA_COUNT", 1)
  84. });
  85. PointLightMat::PointLightMat()
  86. :mGBufferParams(mMaterial, mParamsSet)
  87. {
  88. const GpuParams& gpuParams = *mParamsSet->getGpuParams();
  89. gpuParams.getTextureParam(GPT_FRAGMENT_PROGRAM, "gLightOcclusionTex", mLightOcclusionTexParam);
  90. }
  91. void PointLightMat::_initVariations(ShaderVariations& variations)
  92. {
  93. variations.add(VAR_FullMSAA_Inside);
  94. variations.add(VAR_SingleMSAA_Inside);
  95. variations.add(VAR_FullMSAA_Outside);
  96. variations.add(VAR_SingleMSAA_Outside);
  97. variations.add(VAR_NoMSAA_Inside);
  98. variations.add(VAR_NoMSAA_Outside);
  99. }
  100. void PointLightMat::bind(const GBufferTextures& gBufferInput, const SPtr<Texture>& lightOcclusion,
  101. const SPtr<GpuParamBlockBuffer>& perCamera)
  102. {
  103. RendererUtility::instance().setPass(mMaterial, 0);
  104. mGBufferParams.bind(gBufferInput);
  105. mLightOcclusionTexParam.set(lightOcclusion);
  106. mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", perCamera);
  107. }
  108. void PointLightMat::setPerLightParams(const SPtr<GpuParamBlockBuffer>& perLight)
  109. {
  110. mParamsSet->getGpuParams()->setParamBlockBuffer("PerLight", perLight);
  111. gRendererUtility().setPassParams(mParamsSet);
  112. }
  113. PointLightMat* PointLightMat::getVariation(bool inside, bool msaa, bool singleSampleMSAA)
  114. {
  115. if(msaa)
  116. {
  117. if (inside)
  118. {
  119. if (singleSampleMSAA)
  120. return get(VAR_SingleMSAA_Inside);
  121. return get(VAR_FullMSAA_Inside);
  122. }
  123. else
  124. {
  125. if (singleSampleMSAA)
  126. return get(VAR_SingleMSAA_Outside);
  127. return get(VAR_FullMSAA_Outside);
  128. }
  129. }
  130. else
  131. {
  132. if (inside)
  133. return get(VAR_NoMSAA_Inside);
  134. else
  135. return get(VAR_NoMSAA_Outside);
  136. }
  137. }
  138. StandardDeferred::StandardDeferred()
  139. {
  140. mPerLightBuffer = gPerLightParamDef.createBuffer();
  141. }
  142. void StandardDeferred::renderLight(LightType lightType, const RendererLight& light, const RendererView& view,
  143. const GBufferTextures& gBufferInput, const SPtr<Texture>& lightOcclusion)
  144. {
  145. const auto& viewProps = view.getProperties();
  146. bool isMSAA = view.getProperties().numSamples > 1;
  147. SPtr<GpuParamBlockBuffer> perViewBuffer = view.getPerViewBuffer();
  148. light.getParameters(mPerLightBuffer);
  149. if (lightType == LightType::Directional)
  150. {
  151. DirectionalLightMat* material = DirectionalLightMat::getVariation(isMSAA, true);
  152. material->bind(gBufferInput, lightOcclusion, perViewBuffer);
  153. material->setPerLightParams(mPerLightBuffer);
  154. gRendererUtility().drawScreenQuad();
  155. // Draw pixels requiring per-sample evaluation
  156. if(isMSAA)
  157. {
  158. DirectionalLightMat* msaaMaterial = DirectionalLightMat::getVariation(true, false);
  159. msaaMaterial->bind(gBufferInput, lightOcclusion, perViewBuffer);
  160. msaaMaterial->setPerLightParams(mPerLightBuffer);
  161. gRendererUtility().drawScreenQuad();
  162. }
  163. }
  164. else // Radial or spot
  165. {
  166. // Check if viewer is inside the light volume
  167. float distSqrd = (light.internal->getBounds().getCenter() - viewProps.viewOrigin).squaredLength();
  168. // Extend the bounds slighty to cover the case when the viewer is outside, but the near plane is intersecting
  169. // the light bounds. We need to be conservative since the material for rendering outside will not properly
  170. // render the inside of the light volume.
  171. float boundRadius = light.internal->getBounds().getRadius() + viewProps.nearPlane * 3.0f;
  172. bool isInside = distSqrd < (boundRadius * boundRadius);
  173. SPtr<Mesh> stencilMesh;
  174. if(lightType == LightType::Radial)
  175. stencilMesh = RendererUtility::instance().getRadialLightStencil();
  176. else // Spot
  177. stencilMesh = RendererUtility::instance().getSpotLightStencil();
  178. PointLightMat* material = PointLightMat::getVariation(isInside, isMSAA, true);
  179. material->bind(gBufferInput, lightOcclusion, perViewBuffer);
  180. material->setPerLightParams(mPerLightBuffer);
  181. // Note: If MSAA is enabled this will be rendered multisampled (on polygon edges), see if this can be avoided
  182. gRendererUtility().draw(stencilMesh);
  183. // Draw pixels requiring per-sample evaluation
  184. if(isMSAA)
  185. {
  186. PointLightMat* msaaMaterial = PointLightMat::getVariation(isInside, true, false);
  187. msaaMaterial->bind(gBufferInput, lightOcclusion, perViewBuffer);
  188. msaaMaterial->setPerLightParams(mPerLightBuffer);
  189. gRendererUtility().draw(stencilMesh);
  190. }
  191. }
  192. }
  193. }}