BsImageBasedLighting.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. const VisibilityInfo& visibility = viewGroup.getVisibilityInfo();
  24. // Generate refl. probe data for the visible ones
  25. UINT32 numProbes = (UINT32)sceneInfo.reflProbes.size();
  26. for(UINT32 i = 0; i < numProbes; i++)
  27. {
  28. if (!visibility.reflProbes[i])
  29. continue;
  30. mReflProbeDataTemp.push_back(ReflProbeData());
  31. sceneInfo.reflProbes[i].getParameters(mReflProbeDataTemp.back());
  32. }
  33. // Sort probes so bigger ones get accessed first, this way we overlay smaller ones on top of biggers ones when
  34. // rendering
  35. auto sorter = [](const ReflProbeData& lhs, const ReflProbeData& rhs)
  36. {
  37. return rhs.radius < lhs.radius;
  38. };
  39. std::sort(mReflProbeDataTemp.begin(), mReflProbeDataTemp.end(), sorter);
  40. mNumProbes = (UINT32)mReflProbeDataTemp.size();
  41. // Move refl. probe data into a GPU buffer
  42. UINT32 size = mNumProbes * sizeof(ReflProbeData);
  43. UINT32 curBufferSize;
  44. if (mProbeBuffer != nullptr)
  45. curBufferSize = mProbeBuffer->getSize();
  46. else
  47. curBufferSize = 0;
  48. if (size > curBufferSize || curBufferSize == 0)
  49. {
  50. // Allocate at least one block even if no probes, to avoid issues with null buffers
  51. UINT32 bufferSize = std::max(1, Math::ceilToInt(size / (float)BUFFER_INCREMENT)) * BUFFER_INCREMENT;
  52. GPU_BUFFER_DESC bufferDesc;
  53. bufferDesc.type = GBT_STRUCTURED;
  54. bufferDesc.elementCount = bufferSize / sizeof(ReflProbeData);
  55. bufferDesc.elementSize = sizeof(ReflProbeData);
  56. bufferDesc.format = BF_UNKNOWN;
  57. mProbeBuffer = GpuBuffer::create(bufferDesc);
  58. }
  59. if (size > 0)
  60. mProbeBuffer->writeData(0, size, mReflProbeDataTemp.data(), BWT_DISCARD);
  61. mReflProbeDataTemp.clear();
  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. output.position = probe->getPosition();
  75. output.boxExtents = probe->getExtents();
  76. if (probe->getType() == ReflectionProbeType::Sphere)
  77. output.radius = probe->getRadius();
  78. else
  79. output.radius = output.boxExtents.length();
  80. output.transitionDistance = probe->getTransitionDistance();
  81. output.cubemapIdx = arrayIdx;
  82. output.invBoxTransform.setInverseTRS(output.position, probe->getRotation(), output.boxExtents);
  83. }
  84. void ImageBasedLightingParams::populate(const SPtr<GpuParamsSet>& paramsSet, GpuProgramType programType, bool optional,
  85. bool gridIndices)
  86. {
  87. SPtr<GpuParams> params = paramsSet->getGpuParams();
  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. params->getBufferParam(programType, "gReflectionProbes", reflectionProbesParam);
  96. params->getTextureParam(programType, "gPreintegratedEnvBRDF", preintegratedEnvBRDFParam);
  97. }
  98. // AO
  99. params->getTextureParam(programType, "gAmbientOcclusionTex", ambientOcclusionTexParam);
  100. // SSR
  101. params->getTextureParam(programType, "gSSRTex", ssrTexParam);
  102. if(gridIndices)
  103. {
  104. if (!optional || params->hasBuffer(programType, "gReflectionProbeIndices"))
  105. params->getBufferParam(programType, "gReflectionProbeIndices", reflectionProbeIndicesParam);
  106. }
  107. reflProbeParamsBindingIdx = paramsSet->getParamBlockBufferIndex("ReflProbeParams");
  108. }
  109. ReflProbeParamBuffer::ReflProbeParamBuffer()
  110. {
  111. buffer = gReflProbeParamsParamDef.createBuffer();
  112. }
  113. void ReflProbeParamBuffer::populate(const Skybox* sky, const VisibleReflProbeData& probeData,
  114. const SPtr<Texture>& reflectionCubemaps, bool capturingReflections)
  115. {
  116. float brightness = 1.0f;
  117. UINT32 skyReflectionsAvailable = 0;
  118. UINT32 numSkyMips = 0;
  119. if(sky != nullptr)
  120. {
  121. SPtr<Texture> filteredReflections = sky->getFilteredRadiance();
  122. if (filteredReflections)
  123. {
  124. numSkyMips = filteredReflections->getProperties().getNumMipmaps() + 1;
  125. skyReflectionsAvailable = 1;
  126. }
  127. brightness = sky->getBrightness();
  128. }
  129. gReflProbeParamsParamDef.gSkyCubemapNumMips.set(buffer, numSkyMips);
  130. gReflProbeParamsParamDef.gSkyCubemapAvailable.set(buffer, skyReflectionsAvailable);
  131. gReflProbeParamsParamDef.gNumProbes.set(buffer, probeData.getNumProbes());
  132. UINT32 numReflProbeMips = 0;
  133. if (reflectionCubemaps != nullptr)
  134. numReflProbeMips = reflectionCubemaps->getProperties().getNumMipmaps() + 1;
  135. gReflProbeParamsParamDef.gReflCubemapNumMips.set(buffer, numReflProbeMips);
  136. gReflProbeParamsParamDef.gUseReflectionMaps.set(buffer, capturingReflections ? 0 : 1);
  137. gReflProbeParamsParamDef.gSkyBrightness.set(buffer, brightness);
  138. }
  139. ShaderVariation TiledDeferredImageBasedLightingMat::VAR_1MSAA = ShaderVariation({
  140. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  141. ShaderVariation::Param("MSAA_COUNT", 1)
  142. });
  143. ShaderVariation TiledDeferredImageBasedLightingMat::VAR_2MSAA = ShaderVariation({
  144. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  145. ShaderVariation::Param("MSAA_COUNT", 2)
  146. });
  147. ShaderVariation TiledDeferredImageBasedLightingMat::VAR_4MSAA = ShaderVariation({
  148. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  149. ShaderVariation::Param("MSAA_COUNT", 4)
  150. });
  151. ShaderVariation TiledDeferredImageBasedLightingMat::VAR_8MSAA = ShaderVariation({
  152. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  153. ShaderVariation::Param("MSAA_COUNT", 8)
  154. });
  155. // Note: Using larger tiles than in tiled deferred lighting since we use AABB for intersections, which is more
  156. // expensive to compute than frustums. This way we amortize the cost even though other parts of the shader might suffer
  157. // due to increased thread group load.
  158. const UINT32 TiledDeferredImageBasedLightingMat::TILE_SIZE = 32;
  159. TiledDeferredImageBasedLightingMat::TiledDeferredImageBasedLightingMat()
  160. {
  161. mSampleCount = mVariation.getUInt("MSAA_COUNT");
  162. SPtr<GpuParams> params = mParamsSet->getGpuParams();
  163. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferATex", mGBufferA);
  164. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferBTex", mGBufferB);
  165. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferCTex", mGBufferC);
  166. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gDepthBufferTex", mGBufferDepth);
  167. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gInColor", mInColorTextureParam);
  168. if (mSampleCount > 1)
  169. {
  170. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gMSAACoverage", mMSAACoverageTexParam);
  171. params->getBufferParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputBufferParam);
  172. }
  173. else
  174. params->getLoadStoreTextureParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputTextureParam);
  175. mParamBuffer = gTiledImageBasedLightingParamDef.createBuffer();
  176. mParamsSet->setParamBlockBuffer("Params", mParamBuffer, true);
  177. mImageBasedParams.populate(mParamsSet, GPT_COMPUTE_PROGRAM, false, false);
  178. mParamsSet->setParamBlockBuffer("ReflProbeParams", mReflProbeParamBuffer.buffer);
  179. }
  180. void TiledDeferredImageBasedLightingMat::_initVariations(ShaderVariations& variations)
  181. {
  182. variations.add(VAR_1MSAA);
  183. variations.add(VAR_2MSAA);
  184. variations.add(VAR_4MSAA);
  185. variations.add(VAR_8MSAA);
  186. }
  187. void TiledDeferredImageBasedLightingMat::execute(const RendererView& view, const SceneInfo& sceneInfo,
  188. const VisibleReflProbeData& probeData, const Inputs& inputs)
  189. {
  190. const RendererViewProperties& viewProps = view.getProperties();
  191. UINT32 width = viewProps.viewRect.width;
  192. UINT32 height = viewProps.viewRect.height;
  193. Vector2I framebufferSize;
  194. framebufferSize[0] = width;
  195. framebufferSize[1] = height;
  196. gTiledImageBasedLightingParamDef.gFramebufferSize.set(mParamBuffer, framebufferSize);
  197. mReflProbeParamBuffer.populate(sceneInfo.skybox, probeData, sceneInfo.reflProbeCubemapsTex,
  198. viewProps.renderingReflections);
  199. mParamBuffer->flushToGPU();
  200. mReflProbeParamBuffer.buffer->flushToGPU();
  201. mGBufferA.set(inputs.gbuffer.albedo);
  202. mGBufferB.set(inputs.gbuffer.normals);
  203. mGBufferC.set(inputs.gbuffer.roughMetal);
  204. mGBufferDepth.set(inputs.gbuffer.depth);
  205. SPtr<Texture> skyFilteredRadiance;
  206. if(sceneInfo.skybox)
  207. skyFilteredRadiance = sceneInfo.skybox->getFilteredRadiance();
  208. mImageBasedParams.preintegratedEnvBRDFParam.set(inputs.preIntegratedGF);
  209. mImageBasedParams.reflectionProbesParam.set(probeData.getProbeBuffer());
  210. mImageBasedParams.reflectionProbeCubemapsTexParam.set(sceneInfo.reflProbeCubemapsTex);
  211. mImageBasedParams.skyReflectionsTexParam.set(skyFilteredRadiance);
  212. mImageBasedParams.ambientOcclusionTexParam.set(inputs.ambientOcclusion);
  213. mImageBasedParams.ssrTexParam.set(inputs.ssr);
  214. mParamsSet->setParamBlockBuffer("PerCamera", view.getPerViewBuffer(), true);
  215. mInColorTextureParam.set(inputs.lightAccumulation);
  216. if (mSampleCount > 1)
  217. {
  218. mOutputBufferParam.set(inputs.sceneColorBuffer);
  219. mMSAACoverageTexParam.set(inputs.msaaCoverage);
  220. }
  221. else
  222. mOutputTextureParam.set(inputs.sceneColorTex);
  223. UINT32 numTilesX = (UINT32)Math::ceilToInt(width / (float)TILE_SIZE);
  224. UINT32 numTilesY = (UINT32)Math::ceilToInt(height / (float)TILE_SIZE);
  225. gRendererUtility().setComputePass(mMaterial, 0);
  226. gRendererUtility().setPassParams(mParamsSet);
  227. RenderAPI::instance().dispatchCompute(numTilesX, numTilesY);
  228. }
  229. TiledDeferredImageBasedLightingMat* TiledDeferredImageBasedLightingMat::getVariation(UINT32 msaaCount)
  230. {
  231. switch(msaaCount)
  232. {
  233. case 1:
  234. return get(VAR_1MSAA);
  235. case 2:
  236. return get(VAR_2MSAA);
  237. case 4:
  238. return get(VAR_4MSAA);
  239. case 8:
  240. default:
  241. return get(VAR_8MSAA);
  242. }
  243. }
  244. }}