BsImageBasedLighting.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. 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<GpuParamsSet>& paramsSet, GpuProgramType programType, bool optional,
  86. bool gridIndices)
  87. {
  88. SPtr<GpuParams> params = paramsSet->getGpuParams();
  89. // Sky
  90. if (!optional || params->hasTexture(programType, "gSkyReflectionTex"))
  91. params->getTextureParam(programType, "gSkyReflectionTex", skyReflectionsTexParam);
  92. // Reflections
  93. if (!optional || params->hasTexture(programType, "gReflProbeCubemaps"))
  94. {
  95. params->getTextureParam(programType, "gReflProbeCubemaps", reflectionProbeCubemapsTexParam);
  96. params->getBufferParam(programType, "gReflectionProbes", reflectionProbesParam);
  97. params->getTextureParam(programType, "gPreintegratedEnvBRDF", preintegratedEnvBRDFParam);
  98. }
  99. // AO
  100. if (params->hasTexture(programType, "gAmbientOcclusionTex"))
  101. {
  102. params->getTextureParam(programType, "gAmbientOcclusionTex", ambientOcclusionTexParam);
  103. if(params->hasParam(programType, "gAmbientOcclusionSamp"))
  104. params->getSamplerStateParam(programType, "gAmbientOcclusionSamp", ambientOcclusionSampParam);
  105. else
  106. params->getSamplerStateParam(programType, "gAmbientOcclusionTex", ssrSampParam);
  107. }
  108. // SSR
  109. if (params->hasTexture(programType, "gSSRTex"))
  110. {
  111. params->getTextureParam(programType, "gSSRTex", ssrTexParam);
  112. if(params->hasParam(programType, "gSSRSamp"))
  113. params->getSamplerStateParam(programType, "gSSRSamp", ssrSampParam);
  114. else
  115. params->getSamplerStateParam(programType, "gSSRTex", ssrSampParam);
  116. }
  117. if(gridIndices)
  118. {
  119. if (!optional || params->hasBuffer(programType, "gReflectionProbeIndices"))
  120. params->getBufferParam(programType, "gReflectionProbeIndices", reflectionProbeIndicesParam);
  121. }
  122. reflProbeParamsBindingIdx = paramsSet->getParamBlockBufferIndex("ReflProbeParams");
  123. }
  124. ReflProbeParamBuffer::ReflProbeParamBuffer()
  125. {
  126. buffer = gReflProbeParamsParamDef.createBuffer();
  127. }
  128. void ReflProbeParamBuffer::populate(const Skybox* sky, const VisibleReflProbeData& probeData,
  129. const SPtr<Texture>& reflectionCubemaps, bool capturingReflections)
  130. {
  131. float brightness = 1.0f;
  132. UINT32 skyReflectionsAvailable = 0;
  133. UINT32 numSkyMips = 0;
  134. if(sky != nullptr)
  135. {
  136. SPtr<Texture> filteredReflections = sky->getFilteredRadiance();
  137. if (filteredReflections)
  138. {
  139. numSkyMips = filteredReflections->getProperties().getNumMipmaps() + 1;
  140. skyReflectionsAvailable = 1;
  141. }
  142. brightness = sky->getBrightness();
  143. }
  144. gReflProbeParamsParamDef.gSkyCubemapNumMips.set(buffer, numSkyMips);
  145. gReflProbeParamsParamDef.gSkyCubemapAvailable.set(buffer, skyReflectionsAvailable);
  146. gReflProbeParamsParamDef.gNumProbes.set(buffer, probeData.getNumProbes());
  147. UINT32 numReflProbeMips = 0;
  148. if (reflectionCubemaps != nullptr)
  149. numReflProbeMips = reflectionCubemaps->getProperties().getNumMipmaps() + 1;
  150. gReflProbeParamsParamDef.gReflCubemapNumMips.set(buffer, numReflProbeMips);
  151. gReflProbeParamsParamDef.gUseReflectionMaps.set(buffer, capturingReflections ? 0 : 1);
  152. gReflProbeParamsParamDef.gSkyBrightness.set(buffer, brightness);
  153. }
  154. ShaderVariation TiledDeferredImageBasedLightingMat::VAR_1MSAA = ShaderVariation({
  155. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  156. ShaderVariation::Param("MSAA_COUNT", 1)
  157. });
  158. ShaderVariation TiledDeferredImageBasedLightingMat::VAR_2MSAA = ShaderVariation({
  159. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  160. ShaderVariation::Param("MSAA_COUNT", 2)
  161. });
  162. ShaderVariation TiledDeferredImageBasedLightingMat::VAR_4MSAA = ShaderVariation({
  163. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  164. ShaderVariation::Param("MSAA_COUNT", 4)
  165. });
  166. ShaderVariation TiledDeferredImageBasedLightingMat::VAR_8MSAA = ShaderVariation({
  167. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  168. ShaderVariation::Param("MSAA_COUNT", 8)
  169. });
  170. // Note: Using larger tiles than in tiled deferred lighting since we use AABB for intersections, which is more
  171. // expensive to compute than frustums. This way we amortize the cost even though other parts of the shader might suffer
  172. // due to increased thread group load.
  173. const UINT32 TiledDeferredImageBasedLightingMat::TILE_SIZE = 32;
  174. TiledDeferredImageBasedLightingMat::TiledDeferredImageBasedLightingMat()
  175. {
  176. mSampleCount = mVariation.getUInt("MSAA_COUNT");
  177. SPtr<GpuParams> params = mParamsSet->getGpuParams();
  178. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferATex", mGBufferA);
  179. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferBTex", mGBufferB);
  180. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferCTex", mGBufferC);
  181. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gDepthBufferTex", mGBufferDepth);
  182. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gInColor", mInColorTextureParam);
  183. if (mSampleCount > 1)
  184. {
  185. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gMSAACoverage", mMSAACoverageTexParam);
  186. params->getBufferParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputBufferParam);
  187. }
  188. else
  189. params->getLoadStoreTextureParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputTextureParam);
  190. mParamBuffer = gTiledImageBasedLightingParamDef.createBuffer();
  191. params->setParamBlockBuffer("Params", mParamBuffer);
  192. mImageBasedParams.populate(mParamsSet, GPT_COMPUTE_PROGRAM, false, false);
  193. SAMPLER_STATE_DESC desc;
  194. desc.minFilter = FO_POINT;
  195. desc.magFilter = FO_POINT;
  196. desc.mipFilter = FO_POINT;
  197. desc.addressMode.u = TAM_CLAMP;
  198. desc.addressMode.v = TAM_CLAMP;
  199. desc.addressMode.w = TAM_CLAMP;
  200. SPtr<SamplerState> samplerState = SamplerState::create(desc);
  201. mImageBasedParams.ssrSampParam.set(samplerState);
  202. mImageBasedParams.ambientOcclusionSampParam.set(samplerState);
  203. params->setParamBlockBuffer("ReflProbeParams", mReflProbeParamBuffer.buffer);
  204. }
  205. void TiledDeferredImageBasedLightingMat::_initVariations(ShaderVariations& variations)
  206. {
  207. variations.add(VAR_1MSAA);
  208. variations.add(VAR_2MSAA);
  209. variations.add(VAR_4MSAA);
  210. variations.add(VAR_8MSAA);
  211. }
  212. void TiledDeferredImageBasedLightingMat::execute(const RendererView& view, const SceneInfo& sceneInfo,
  213. const VisibleReflProbeData& probeData, const Inputs& inputs)
  214. {
  215. const RendererViewProperties& viewProps = view.getProperties();
  216. UINT32 width = viewProps.viewRect.width;
  217. UINT32 height = viewProps.viewRect.height;
  218. Vector2I framebufferSize;
  219. framebufferSize[0] = width;
  220. framebufferSize[1] = height;
  221. gTiledImageBasedLightingParamDef.gFramebufferSize.set(mParamBuffer, framebufferSize);
  222. mReflProbeParamBuffer.populate(sceneInfo.skybox, probeData, sceneInfo.reflProbeCubemapsTex,
  223. viewProps.renderingReflections);
  224. mParamBuffer->flushToGPU();
  225. mReflProbeParamBuffer.buffer->flushToGPU();
  226. mGBufferA.set(inputs.gbuffer.albedo);
  227. mGBufferB.set(inputs.gbuffer.normals);
  228. mGBufferC.set(inputs.gbuffer.roughMetal);
  229. mGBufferDepth.set(inputs.gbuffer.depth);
  230. SPtr<Texture> skyFilteredRadiance;
  231. if(sceneInfo.skybox)
  232. skyFilteredRadiance = sceneInfo.skybox->getFilteredRadiance();
  233. mImageBasedParams.preintegratedEnvBRDFParam.set(inputs.preIntegratedGF);
  234. mImageBasedParams.reflectionProbesParam.set(probeData.getProbeBuffer());
  235. mImageBasedParams.reflectionProbeCubemapsTexParam.set(sceneInfo.reflProbeCubemapsTex);
  236. mImageBasedParams.skyReflectionsTexParam.set(skyFilteredRadiance);
  237. mImageBasedParams.ambientOcclusionTexParam.set(inputs.ambientOcclusion);
  238. mImageBasedParams.ssrTexParam.set(inputs.ssr);
  239. mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", view.getPerViewBuffer());
  240. mInColorTextureParam.set(inputs.lightAccumulation);
  241. if (mSampleCount > 1)
  242. {
  243. mOutputBufferParam.set(inputs.sceneColorBuffer);
  244. mMSAACoverageTexParam.set(inputs.msaaCoverage);
  245. }
  246. else
  247. mOutputTextureParam.set(inputs.sceneColorTex);
  248. UINT32 numTilesX = (UINT32)Math::ceilToInt(width / (float)TILE_SIZE);
  249. UINT32 numTilesY = (UINT32)Math::ceilToInt(height / (float)TILE_SIZE);
  250. gRendererUtility().setComputePass(mMaterial, 0);
  251. gRendererUtility().setPassParams(mParamsSet);
  252. RenderAPI::instance().dispatchCompute(numTilesX, numTilesY);
  253. }
  254. TiledDeferredImageBasedLightingMat* TiledDeferredImageBasedLightingMat::getVariation(UINT32 msaaCount)
  255. {
  256. switch(msaaCount)
  257. {
  258. case 1:
  259. return get(VAR_1MSAA);
  260. case 2:
  261. return get(VAR_2MSAA);
  262. case 4:
  263. return get(VAR_4MSAA);
  264. case 8:
  265. default:
  266. return get(VAR_8MSAA);
  267. }
  268. }
  269. }}