BsImageBasedLighting.cpp 11 KB

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