BsImageBasedLighting.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. bool supportsStructuredBuffers = gRenderBeast()->getFeatureSet() == RenderBeastFeatureSet::Desktop;
  44. if(supportsStructuredBuffers)
  45. {
  46. UINT32 size = mNumProbes * sizeof(ReflProbeData);
  47. UINT32 curBufferSize;
  48. if (mProbeBuffer != nullptr)
  49. curBufferSize = mProbeBuffer->getSize();
  50. else
  51. curBufferSize = 0;
  52. if (size > curBufferSize || curBufferSize == 0)
  53. {
  54. // Allocate at least one block even if no probes, to avoid issues with null buffers
  55. UINT32 bufferSize = std::max(1, Math::ceilToInt(size / (float) BUFFER_INCREMENT)) * BUFFER_INCREMENT;
  56. GPU_BUFFER_DESC bufferDesc;
  57. bufferDesc.type = GBT_STRUCTURED;
  58. bufferDesc.elementCount = bufferSize / sizeof(ReflProbeData);
  59. bufferDesc.elementSize = sizeof(ReflProbeData);
  60. bufferDesc.format = BF_UNKNOWN;
  61. mProbeBuffer = GpuBuffer::create(bufferDesc);
  62. }
  63. if (size > 0)
  64. mProbeBuffer->writeData(0, size, mReflProbeData.data(), BWT_DISCARD);
  65. }
  66. }
  67. RendererReflectionProbe::RendererReflectionProbe(ReflectionProbe* probe)
  68. :probe(probe)
  69. {
  70. arrayIdx = -1;
  71. arrayDirty = true;
  72. errorFlagged = false;
  73. }
  74. void RendererReflectionProbe::getParameters(ReflProbeData& output) const
  75. {
  76. output.type = probe->getType() == ReflectionProbeType::Sphere ? 0
  77. : probe->getType() == ReflectionProbeType::Box ? 1 : 2;
  78. const Transform& tfrm = probe->getTransform();
  79. output.position = tfrm.getPosition();
  80. output.boxExtents = probe->getExtents();
  81. if (probe->getType() == ReflectionProbeType::Sphere)
  82. output.radius = probe->getRadius();
  83. else
  84. output.radius = output.boxExtents.length();
  85. output.transitionDistance = probe->getTransitionDistance();
  86. output.cubemapIdx = arrayIdx;
  87. output.invBoxTransform.setInverseTRS(output.position, tfrm.getRotation(), output.boxExtents);
  88. }
  89. void ImageBasedLightingParams::populate(const SPtr<GpuParams>& params, GpuProgramType programType, bool optional,
  90. bool gridIndices, bool probeArray)
  91. {
  92. // Sky
  93. if (!optional || params->hasTexture(programType, "gSkyReflectionTex"))
  94. params->getTextureParam(programType, "gSkyReflectionTex", skyReflectionsTexParam);
  95. // Reflections
  96. if (!optional || params->hasTexture(programType, "gReflProbeCubemaps"))
  97. {
  98. params->getTextureParam(programType, "gReflProbeCubemaps", reflectionProbeCubemapsTexParam);
  99. if(probeArray)
  100. params->getBufferParam(programType, "gReflectionProbes", reflectionProbesParam);
  101. }
  102. if (!optional || params->hasTexture(programType, "gPreintegratedEnvBRDF"))
  103. params->getTextureParam(programType, "gPreintegratedEnvBRDF", preintegratedEnvBRDFParam);
  104. // AO
  105. if (params->hasTexture(programType, "gAmbientOcclusionTex"))
  106. params->getTextureParam(programType, "gAmbientOcclusionTex", ambientOcclusionTexParam);
  107. // SSR
  108. if (params->hasTexture(programType, "gSSRTex"))
  109. params->getTextureParam(programType, "gSSRTex", ssrTexParam);
  110. if(gridIndices)
  111. {
  112. if (!optional || params->hasBuffer(programType, "gReflectionProbeIndices"))
  113. params->getBufferParam(programType, "gReflectionProbeIndices", reflectionProbeIndicesParam);
  114. }
  115. params->getParamInfo()->getBinding(
  116. programType,
  117. GpuPipelineParamInfoBase::ParamType::ParamBlock,
  118. "ReflProbeParams",
  119. reflProbeParamBindings
  120. );
  121. params->getParamInfo()->getBinding(
  122. programType,
  123. GpuPipelineParamInfoBase::ParamType::ParamBlock,
  124. "ReflectionProbes",
  125. reflProbesBinding
  126. );
  127. }
  128. ReflProbeParamBuffer::ReflProbeParamBuffer()
  129. {
  130. buffer = gReflProbeParamsParamDef.createBuffer();
  131. }
  132. void ReflProbeParamBuffer::populate(const Skybox* sky, UINT32 numProbes, const SPtr<Texture>& reflectionCubemaps,
  133. bool capturingReflections)
  134. {
  135. float brightness = 1.0f;
  136. UINT32 skyReflectionsAvailable = 0;
  137. UINT32 numSkyMips = 0;
  138. if(sky != nullptr)
  139. {
  140. SPtr<Texture> filteredReflections = sky->getFilteredRadiance();
  141. if (filteredReflections)
  142. {
  143. numSkyMips = filteredReflections->getProperties().getNumMipmaps() + 1;
  144. skyReflectionsAvailable = 1;
  145. }
  146. brightness = sky->getBrightness();
  147. }
  148. gReflProbeParamsParamDef.gSkyCubemapNumMips.set(buffer, numSkyMips);
  149. gReflProbeParamsParamDef.gSkyCubemapAvailable.set(buffer, skyReflectionsAvailable);
  150. gReflProbeParamsParamDef.gNumProbes.set(buffer, numProbes);
  151. UINT32 numReflProbeMips = 0;
  152. if (reflectionCubemaps != nullptr)
  153. numReflProbeMips = reflectionCubemaps->getProperties().getNumMipmaps() + 1;
  154. gReflProbeParamsParamDef.gReflCubemapNumMips.set(buffer, numReflProbeMips);
  155. gReflProbeParamsParamDef.gUseReflectionMaps.set(buffer, capturingReflections ? 0 : 1);
  156. gReflProbeParamsParamDef.gSkyBrightness.set(buffer, brightness);
  157. }
  158. // Note: Using larger tiles than in tiled deferred lighting since we use AABB for intersections, which is more
  159. // expensive to compute than frustums. This way we amortize the cost even though other parts of the shader might suffer
  160. // due to increased thread group load.
  161. const UINT32 TiledDeferredImageBasedLightingMat::TILE_SIZE = 32;
  162. TiledDeferredImageBasedLightingMat::TiledDeferredImageBasedLightingMat()
  163. {
  164. mSampleCount = mVariation.getUInt("MSAA_COUNT");
  165. mParams->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferATex", mGBufferA);
  166. mParams->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferBTex", mGBufferB);
  167. mParams->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferCTex", mGBufferC);
  168. mParams->getTextureParam(GPT_COMPUTE_PROGRAM, "gDepthBufferTex", mGBufferDepth);
  169. mParams->getTextureParam(GPT_COMPUTE_PROGRAM, "gInColor", mInColorTextureParam);
  170. if (mSampleCount > 1)
  171. {
  172. mParams->getTextureParam(GPT_COMPUTE_PROGRAM, "gMSAACoverage", mMSAACoverageTexParam);
  173. mParams->getBufferParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputBufferParam);
  174. }
  175. else
  176. mParams->getLoadStoreTextureParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputTextureParam);
  177. mParamBuffer = gTiledImageBasedLightingParamDef.createBuffer();
  178. mParams->setParamBlockBuffer("Params", mParamBuffer);
  179. mImageBasedParams.populate(mParams, GPT_COMPUTE_PROGRAM, false, false, true);
  180. mParams->setParamBlockBuffer("ReflProbeParams", mReflProbeParamBuffer.buffer);
  181. }
  182. void TiledDeferredImageBasedLightingMat::_initDefines(ShaderDefines& defines)
  183. {
  184. defines.set("TILE_SIZE", TILE_SIZE);
  185. }
  186. void TiledDeferredImageBasedLightingMat::execute(const RendererView& view, const SceneInfo& sceneInfo,
  187. const VisibleReflProbeData& probeData, const Inputs& inputs)
  188. {
  189. const RendererViewProperties& viewProps = view.getProperties();
  190. UINT32 width = viewProps.viewRect.width;
  191. UINT32 height = viewProps.viewRect.height;
  192. Vector2I framebufferSize;
  193. framebufferSize[0] = width;
  194. framebufferSize[1] = height;
  195. gTiledImageBasedLightingParamDef.gFramebufferSize.set(mParamBuffer, framebufferSize);
  196. mReflProbeParamBuffer.populate(sceneInfo.skybox, probeData.getNumProbes(), sceneInfo.reflProbeCubemapsTex,
  197. viewProps.capturingReflections);
  198. mParamBuffer->flushToGPU();
  199. mReflProbeParamBuffer.buffer->flushToGPU();
  200. mGBufferA.set(inputs.gbuffer.albedo);
  201. mGBufferB.set(inputs.gbuffer.normals);
  202. mGBufferC.set(inputs.gbuffer.roughMetal);
  203. mGBufferDepth.set(inputs.gbuffer.depth);
  204. SPtr<Texture> skyFilteredRadiance;
  205. if(sceneInfo.skybox)
  206. skyFilteredRadiance = sceneInfo.skybox->getFilteredRadiance();
  207. mImageBasedParams.preintegratedEnvBRDFParam.set(inputs.preIntegratedGF);
  208. mImageBasedParams.reflectionProbesParam.set(probeData.getProbeBuffer());
  209. mImageBasedParams.reflectionProbeCubemapsTexParam.set(sceneInfo.reflProbeCubemapsTex);
  210. mImageBasedParams.skyReflectionsTexParam.set(skyFilteredRadiance);
  211. mImageBasedParams.ambientOcclusionTexParam.set(inputs.ambientOcclusion);
  212. mImageBasedParams.ssrTexParam.set(inputs.ssr);
  213. mParams->setParamBlockBuffer("PerCamera", view.getPerViewBuffer());
  214. mInColorTextureParam.set(inputs.lightAccumulation);
  215. if (mSampleCount > 1)
  216. {
  217. mOutputBufferParam.set(inputs.sceneColorBuffer);
  218. mMSAACoverageTexParam.set(inputs.msaaCoverage);
  219. }
  220. else
  221. mOutputTextureParam.set(inputs.sceneColorTex);
  222. UINT32 numTilesX = (UINT32)Math::ceilToInt(width / (float)TILE_SIZE);
  223. UINT32 numTilesY = (UINT32)Math::ceilToInt(height / (float)TILE_SIZE);
  224. bind();
  225. RenderAPI::instance().dispatchCompute(numTilesX, numTilesY);
  226. }
  227. TiledDeferredImageBasedLightingMat* TiledDeferredImageBasedLightingMat::getVariation(UINT32 msaaCount)
  228. {
  229. switch(msaaCount)
  230. {
  231. case 1:
  232. return get(getVariation<1>());
  233. case 2:
  234. return get(getVariation<2>());
  235. case 4:
  236. return get(getVariation<4>());
  237. case 8:
  238. default:
  239. return get(getVariation<8>());
  240. }
  241. }
  242. ReflProbesParamDef gReflProbesParamDef;
  243. }}