BsImageBasedLighting.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsImageBasedLighting.h"
  4. #include "BsMaterial.h"
  5. #include "BsShader.h"
  6. #include "BsGpuBuffer.h"
  7. #include "BsGpuParamsSet.h"
  8. #include "BsReflectionProbe.h"
  9. #include "BsGpuParamsSet.h"
  10. #include "BsRenderBeast.h"
  11. #include "BsRendererUtility.h"
  12. #include "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. if(gridIndices)
  101. {
  102. if (!optional || params->hasBuffer(programType, "gReflectionProbeIndices"))
  103. params->getBufferParam(programType, "gReflectionProbeIndices", reflectionProbeIndicesParam);
  104. }
  105. reflProbeParamsBindingIdx = paramsSet->getParamBlockBufferIndex("ReflProbeParams");
  106. }
  107. ReflProbeParamBuffer::ReflProbeParamBuffer()
  108. {
  109. buffer = gReflProbeParamsParamDef.createBuffer();
  110. }
  111. void ReflProbeParamBuffer::populate(const Skybox* sky, const VisibleReflProbeData& probeData,
  112. const SPtr<Texture>& reflectionCubemaps, bool capturingReflections)
  113. {
  114. float brightness = 1.0f;
  115. UINT32 skyReflectionsAvailable = 0;
  116. UINT32 numSkyMips = 0;
  117. if(sky != nullptr)
  118. {
  119. SPtr<Texture> filteredReflections = sky->getFilteredRadiance();
  120. if (filteredReflections)
  121. {
  122. numSkyMips = filteredReflections->getProperties().getNumMipmaps() + 1;
  123. skyReflectionsAvailable = 1;
  124. }
  125. brightness = sky->getBrightness();
  126. }
  127. gReflProbeParamsParamDef.gSkyCubemapNumMips.set(buffer, numSkyMips);
  128. gReflProbeParamsParamDef.gSkyCubemapAvailable.set(buffer, skyReflectionsAvailable);
  129. gReflProbeParamsParamDef.gNumProbes.set(buffer, probeData.getNumProbes());
  130. UINT32 numReflProbeMips = 0;
  131. if (reflectionCubemaps != nullptr)
  132. numReflProbeMips = reflectionCubemaps->getProperties().getNumMipmaps() + 1;
  133. gReflProbeParamsParamDef.gReflCubemapNumMips.set(buffer, numReflProbeMips);
  134. gReflProbeParamsParamDef.gUseReflectionMaps.set(buffer, capturingReflections ? 0 : 1);
  135. }
  136. ShaderVariation TiledDeferredImageBasedLightingMat::VAR_1MSAA = ShaderVariation({
  137. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  138. ShaderVariation::Param("MSAA_COUNT", 1)
  139. });
  140. ShaderVariation TiledDeferredImageBasedLightingMat::VAR_2MSAA = ShaderVariation({
  141. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  142. ShaderVariation::Param("MSAA_COUNT", 2)
  143. });
  144. ShaderVariation TiledDeferredImageBasedLightingMat::VAR_4MSAA = ShaderVariation({
  145. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  146. ShaderVariation::Param("MSAA_COUNT", 4)
  147. });
  148. ShaderVariation TiledDeferredImageBasedLightingMat::VAR_8MSAA = ShaderVariation({
  149. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  150. ShaderVariation::Param("MSAA_COUNT", 8)
  151. });
  152. // Note: Using larger tiles than in tiled deferred lighting since we use AABB for intersections, which is more
  153. // expensive to compute than frustums. This way we amortize the cost even though other parts of the shader might suffer
  154. // due to increased thread group load.
  155. const UINT32 TiledDeferredImageBasedLightingMat::TILE_SIZE = 32;
  156. TiledDeferredImageBasedLightingMat::TiledDeferredImageBasedLightingMat()
  157. {
  158. mSampleCount = mVariation.getUInt("MSAA_COUNT");
  159. SPtr<GpuParams> params = mParamsSet->getGpuParams();
  160. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferATex", mGBufferA);
  161. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferBTex", mGBufferB);
  162. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferCTex", mGBufferC);
  163. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gDepthBufferTex", mGBufferDepth);
  164. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gInColor", mInColorTextureParam);
  165. if (mSampleCount > 1)
  166. params->getBufferParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputBufferParam);
  167. else
  168. params->getLoadStoreTextureParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputTextureParam);
  169. mParamBuffer = gTiledImageBasedLightingParamDef.createBuffer();
  170. mParamsSet->setParamBlockBuffer("Params", mParamBuffer, true);
  171. mImageBasedParams.populate(mParamsSet, GPT_COMPUTE_PROGRAM, false, false);
  172. mParamsSet->setParamBlockBuffer("ReflProbeParams", mReflProbeParamBuffer.buffer);
  173. }
  174. void TiledDeferredImageBasedLightingMat::_initVariations(ShaderVariations& variations)
  175. {
  176. variations.add(VAR_1MSAA);
  177. variations.add(VAR_2MSAA);
  178. variations.add(VAR_4MSAA);
  179. variations.add(VAR_8MSAA);
  180. }
  181. void TiledDeferredImageBasedLightingMat::execute(const RendererView& view, const SceneInfo& sceneInfo,
  182. const VisibleReflProbeData& probeData, const Inputs& inputs)
  183. {
  184. const RendererViewProperties& viewProps = view.getProperties();
  185. UINT32 width = viewProps.viewRect.width;
  186. UINT32 height = viewProps.viewRect.height;
  187. Vector2I framebufferSize;
  188. framebufferSize[0] = width;
  189. framebufferSize[1] = height;
  190. gTiledImageBasedLightingParamDef.gFramebufferSize.set(mParamBuffer, framebufferSize);
  191. mReflProbeParamBuffer.populate(sceneInfo.skybox, probeData, sceneInfo.reflProbeCubemapsTex,
  192. viewProps.renderingReflections);
  193. mParamBuffer->flushToGPU();
  194. mReflProbeParamBuffer.buffer->flushToGPU();
  195. mGBufferA.set(inputs.gbuffer.albedo);
  196. mGBufferB.set(inputs.gbuffer.normals);
  197. mGBufferC.set(inputs.gbuffer.roughMetal);
  198. mGBufferDepth.set(inputs.gbuffer.depth);
  199. SPtr<Texture> skyFilteredRadiance;
  200. SPtr<Texture> skyIrradiance;
  201. if(sceneInfo.skybox)
  202. {
  203. skyFilteredRadiance = sceneInfo.skybox->getFilteredRadiance();
  204. skyIrradiance = sceneInfo.skybox->getIrradiance();
  205. }
  206. mImageBasedParams.preintegratedEnvBRDFParam.set(inputs.preIntegratedGF);
  207. mImageBasedParams.reflectionProbesParam.set(probeData.getProbeBuffer());
  208. mImageBasedParams.reflectionProbeCubemapsTexParam.set(sceneInfo.reflProbeCubemapsTex);
  209. mImageBasedParams.skyReflectionsTexParam.set(skyFilteredRadiance);
  210. mImageBasedParams.ambientOcclusionTexParam.set(inputs.ambientOcclusion);
  211. mParamsSet->setParamBlockBuffer("PerCamera", view.getPerViewBuffer(), true);
  212. mInColorTextureParam.set(inputs.lightAccumulation);
  213. if (mSampleCount > 1)
  214. mOutputBufferParam.set(inputs.sceneColorBuffer);
  215. else
  216. mOutputTextureParam.set(inputs.sceneColorTex);
  217. UINT32 numTilesX = (UINT32)Math::ceilToInt(width / (float)TILE_SIZE);
  218. UINT32 numTilesY = (UINT32)Math::ceilToInt(height / (float)TILE_SIZE);
  219. gRendererUtility().setComputePass(mMaterial, 0);
  220. gRendererUtility().setPassParams(mParamsSet);
  221. RenderAPI::instance().dispatchCompute(numTilesX, numTilesY);
  222. }
  223. TiledDeferredImageBasedLightingMat* TiledDeferredImageBasedLightingMat::getVariation(UINT32 msaaCount)
  224. {
  225. switch(msaaCount)
  226. {
  227. case 1:
  228. return get(VAR_1MSAA);
  229. case 2:
  230. return get(VAR_2MSAA);
  231. case 4:
  232. return get(VAR_4MSAA);
  233. case 8:
  234. default:
  235. return get(VAR_8MSAA);
  236. }
  237. }
  238. }}