2
0

BsImageBasedLighting.cpp 11 KB

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