BsImageBasedLighting.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsImageBasedLighting.h"
  4. #include "Material/BsMaterial.h"
  5. #include "Material/BsShader.h"
  6. #include "RenderAPI/BsGpuBuffer.h"
  7. #include "Material/BsGpuParamsSet.h"
  8. #include "Renderer/BsReflectionProbe.h"
  9. #include "Material/BsGpuParamsSet.h"
  10. #include "BsRenderBeast.h"
  11. #include "Renderer/BsRendererUtility.h"
  12. #include "Renderer/BsSkybox.h"
  13. namespace bs { namespace ct
  14. {
  15. static const UINT32 BUFFER_INCREMENT = 16 * sizeof(ReflProbeData);
  16. ReflProbeParamsParamDef gReflProbeParamsParamDef;
  17. TiledImageBasedLightingParamDef gTiledImageBasedLightingParamDef;
  18. VisibleReflProbeData::VisibleReflProbeData()
  19. :mNumProbes(0)
  20. { }
  21. void VisibleReflProbeData::update(const SceneInfo& sceneInfo, const RendererViewGroup& viewGroup)
  22. {
  23. mReflProbeData.clear();
  24. const VisibilityInfo& visibility = viewGroup.getVisibilityInfo();
  25. // Generate refl. probe data for the visible ones
  26. UINT32 numProbes = (UINT32)sceneInfo.reflProbes.size();
  27. for(UINT32 i = 0; i < numProbes; i++)
  28. {
  29. if (!visibility.reflProbes[i])
  30. continue;
  31. mReflProbeData.push_back(ReflProbeData());
  32. sceneInfo.reflProbes[i].getParameters(mReflProbeData.back());
  33. }
  34. // Sort probes so bigger ones get accessed first, this way we overlay smaller ones on top of biggers ones when
  35. // rendering
  36. auto sorter = [](const ReflProbeData& lhs, const ReflProbeData& rhs)
  37. {
  38. return rhs.radius < lhs.radius;
  39. };
  40. std::sort(mReflProbeData.begin(), mReflProbeData.end(), sorter);
  41. mNumProbes = (UINT32)mReflProbeData.size();
  42. // Move refl. probe data into a GPU buffer
  43. UINT32 size = mNumProbes * sizeof(ReflProbeData);
  44. UINT32 curBufferSize;
  45. if (mProbeBuffer != nullptr)
  46. curBufferSize = mProbeBuffer->getSize();
  47. else
  48. curBufferSize = 0;
  49. if (size > curBufferSize || curBufferSize == 0)
  50. {
  51. // Allocate at least one block even if no probes, to avoid issues with null buffers
  52. UINT32 bufferSize = std::max(1, Math::ceilToInt(size / (float)BUFFER_INCREMENT)) * BUFFER_INCREMENT;
  53. GPU_BUFFER_DESC bufferDesc;
  54. bufferDesc.type = GBT_STRUCTURED;
  55. bufferDesc.elementCount = bufferSize / sizeof(ReflProbeData);
  56. bufferDesc.elementSize = sizeof(ReflProbeData);
  57. bufferDesc.format = BF_UNKNOWN;
  58. mProbeBuffer = GpuBuffer::create(bufferDesc);
  59. }
  60. if (size > 0)
  61. mProbeBuffer->writeData(0, size, mReflProbeData.data(), BWT_DISCARD);
  62. }
  63. RendererReflectionProbe::RendererReflectionProbe(ReflectionProbe* probe)
  64. :probe(probe)
  65. {
  66. arrayIdx = -1;
  67. arrayDirty = true;
  68. errorFlagged = false;
  69. }
  70. void RendererReflectionProbe::getParameters(ReflProbeData& output) const
  71. {
  72. output.type = probe->getType() == ReflectionProbeType::Sphere ? 0
  73. : probe->getType() == ReflectionProbeType::Box ? 1 : 2;
  74. const Transform& tfrm = probe->getTransform();
  75. output.position = tfrm.getPosition();
  76. output.boxExtents = probe->getExtents();
  77. if (probe->getType() == ReflectionProbeType::Sphere)
  78. output.radius = probe->getRadius();
  79. else
  80. output.radius = output.boxExtents.length();
  81. output.transitionDistance = probe->getTransitionDistance();
  82. output.cubemapIdx = arrayIdx;
  83. output.invBoxTransform.setInverseTRS(output.position, tfrm.getRotation(), output.boxExtents);
  84. }
  85. void ImageBasedLightingParams::populate(const SPtr<GpuParams>& params, GpuProgramType programType, bool optional,
  86. bool gridIndices, bool probeArray)
  87. {
  88. // Sky
  89. if (!optional || params->hasTexture(programType, "gSkyReflectionTex"))
  90. params->getTextureParam(programType, "gSkyReflectionTex", skyReflectionsTexParam);
  91. // Reflections
  92. if (!optional || params->hasTexture(programType, "gReflProbeCubemaps"))
  93. {
  94. params->getTextureParam(programType, "gReflProbeCubemaps", reflectionProbeCubemapsTexParam);
  95. if(probeArray)
  96. params->getBufferParam(programType, "gReflectionProbes", reflectionProbesParam);
  97. }
  98. if (!optional || params->hasTexture(programType, "gPreintegratedEnvBRDF"))
  99. params->getTextureParam(programType, "gPreintegratedEnvBRDF", preintegratedEnvBRDFParam);
  100. // AO
  101. if (params->hasTexture(programType, "gAmbientOcclusionTex"))
  102. params->getTextureParam(programType, "gAmbientOcclusionTex", ambientOcclusionTexParam);
  103. // SSR
  104. if (params->hasTexture(programType, "gSSRTex"))
  105. params->getTextureParam(programType, "gSSRTex", ssrTexParam);
  106. if(gridIndices)
  107. {
  108. if (!optional || params->hasBuffer(programType, "gReflectionProbeIndices"))
  109. params->getBufferParam(programType, "gReflectionProbeIndices", reflectionProbeIndicesParam);
  110. }
  111. params->getParamInfo()->getBinding(
  112. programType,
  113. GpuPipelineParamInfoBase::ParamType::ParamBlock,
  114. "ReflProbeParams",
  115. reflProbeParamBindings
  116. );
  117. }
  118. ReflProbeParamBuffer::ReflProbeParamBuffer()
  119. {
  120. buffer = gReflProbeParamsParamDef.createBuffer();
  121. }
  122. void ReflProbeParamBuffer::populate(const Skybox* sky, UINT32 numProbes, const SPtr<Texture>& reflectionCubemaps,
  123. bool capturingReflections)
  124. {
  125. float brightness = 1.0f;
  126. UINT32 skyReflectionsAvailable = 0;
  127. UINT32 numSkyMips = 0;
  128. if(sky != nullptr)
  129. {
  130. SPtr<Texture> filteredReflections = sky->getFilteredRadiance();
  131. if (filteredReflections)
  132. {
  133. numSkyMips = filteredReflections->getProperties().getNumMipmaps() + 1;
  134. skyReflectionsAvailable = 1;
  135. }
  136. brightness = sky->getBrightness();
  137. }
  138. gReflProbeParamsParamDef.gSkyCubemapNumMips.set(buffer, numSkyMips);
  139. gReflProbeParamsParamDef.gSkyCubemapAvailable.set(buffer, skyReflectionsAvailable);
  140. gReflProbeParamsParamDef.gNumProbes.set(buffer, numProbes);
  141. UINT32 numReflProbeMips = 0;
  142. if (reflectionCubemaps != nullptr)
  143. numReflProbeMips = reflectionCubemaps->getProperties().getNumMipmaps() + 1;
  144. gReflProbeParamsParamDef.gReflCubemapNumMips.set(buffer, numReflProbeMips);
  145. gReflProbeParamsParamDef.gUseReflectionMaps.set(buffer, capturingReflections ? 0 : 1);
  146. gReflProbeParamsParamDef.gSkyBrightness.set(buffer, brightness);
  147. }
  148. // Note: Using larger tiles than in tiled deferred lighting since we use AABB for intersections, which is more
  149. // expensive to compute than frustums. This way we amortize the cost even though other parts of the shader might suffer
  150. // due to increased thread group load.
  151. const UINT32 TiledDeferredImageBasedLightingMat::TILE_SIZE = 32;
  152. TiledDeferredImageBasedLightingMat::TiledDeferredImageBasedLightingMat()
  153. {
  154. mSampleCount = mVariation.getUInt("MSAA_COUNT");
  155. mParams->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferATex", mGBufferA);
  156. mParams->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferBTex", mGBufferB);
  157. mParams->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferCTex", mGBufferC);
  158. mParams->getTextureParam(GPT_COMPUTE_PROGRAM, "gDepthBufferTex", mGBufferDepth);
  159. mParams->getTextureParam(GPT_COMPUTE_PROGRAM, "gInColor", mInColorTextureParam);
  160. if (mSampleCount > 1)
  161. {
  162. mParams->getTextureParam(GPT_COMPUTE_PROGRAM, "gMSAACoverage", mMSAACoverageTexParam);
  163. mParams->getBufferParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputBufferParam);
  164. }
  165. else
  166. mParams->getLoadStoreTextureParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputTextureParam);
  167. mParamBuffer = gTiledImageBasedLightingParamDef.createBuffer();
  168. mParams->setParamBlockBuffer("Params", mParamBuffer);
  169. mImageBasedParams.populate(mParams, GPT_COMPUTE_PROGRAM, false, false, true);
  170. mParams->setParamBlockBuffer("ReflProbeParams", mReflProbeParamBuffer.buffer);
  171. }
  172. void TiledDeferredImageBasedLightingMat::_initDefines(ShaderDefines& defines)
  173. {
  174. defines.set("TILE_SIZE", TILE_SIZE);
  175. }
  176. void TiledDeferredImageBasedLightingMat::execute(const RendererView& view, const SceneInfo& sceneInfo,
  177. const VisibleReflProbeData& probeData, const Inputs& inputs)
  178. {
  179. const RendererViewProperties& viewProps = view.getProperties();
  180. UINT32 width = viewProps.viewRect.width;
  181. UINT32 height = viewProps.viewRect.height;
  182. Vector2I framebufferSize;
  183. framebufferSize[0] = width;
  184. framebufferSize[1] = height;
  185. gTiledImageBasedLightingParamDef.gFramebufferSize.set(mParamBuffer, framebufferSize);
  186. mReflProbeParamBuffer.populate(sceneInfo.skybox, probeData.getNumProbes(), sceneInfo.reflProbeCubemapsTex,
  187. viewProps.capturingReflections);
  188. mParamBuffer->flushToGPU();
  189. mReflProbeParamBuffer.buffer->flushToGPU();
  190. mGBufferA.set(inputs.gbuffer.albedo);
  191. mGBufferB.set(inputs.gbuffer.normals);
  192. mGBufferC.set(inputs.gbuffer.roughMetal);
  193. mGBufferDepth.set(inputs.gbuffer.depth);
  194. SPtr<Texture> skyFilteredRadiance;
  195. if(sceneInfo.skybox)
  196. skyFilteredRadiance = sceneInfo.skybox->getFilteredRadiance();
  197. mImageBasedParams.preintegratedEnvBRDFParam.set(inputs.preIntegratedGF);
  198. mImageBasedParams.reflectionProbesParam.set(probeData.getProbeBuffer());
  199. mImageBasedParams.reflectionProbeCubemapsTexParam.set(sceneInfo.reflProbeCubemapsTex);
  200. mImageBasedParams.skyReflectionsTexParam.set(skyFilteredRadiance);
  201. mImageBasedParams.ambientOcclusionTexParam.set(inputs.ambientOcclusion);
  202. mImageBasedParams.ssrTexParam.set(inputs.ssr);
  203. mParams->setParamBlockBuffer("PerCamera", view.getPerViewBuffer());
  204. mInColorTextureParam.set(inputs.lightAccumulation);
  205. if (mSampleCount > 1)
  206. {
  207. mOutputBufferParam.set(inputs.sceneColorBuffer);
  208. mMSAACoverageTexParam.set(inputs.msaaCoverage);
  209. }
  210. else
  211. mOutputTextureParam.set(inputs.sceneColorTex);
  212. UINT32 numTilesX = (UINT32)Math::ceilToInt(width / (float)TILE_SIZE);
  213. UINT32 numTilesY = (UINT32)Math::ceilToInt(height / (float)TILE_SIZE);
  214. bind();
  215. RenderAPI::instance().dispatchCompute(numTilesX, numTilesY);
  216. }
  217. TiledDeferredImageBasedLightingMat* TiledDeferredImageBasedLightingMat::getVariation(UINT32 msaaCount)
  218. {
  219. switch(msaaCount)
  220. {
  221. case 1:
  222. return get(getVariation<1>());
  223. case 2:
  224. return get(getVariation<2>());
  225. case 4:
  226. return get(getVariation<4>());
  227. case 8:
  228. default:
  229. return get(getVariation<8>());
  230. }
  231. }
  232. }}