BsLightRendering.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsLightRendering.h"
  4. #include "Material/BsMaterial.h"
  5. #include "Material/BsShader.h"
  6. #include "BsRenderBeast.h"
  7. #include "RenderAPI/BsGpuParams.h"
  8. #include "Material/BsGpuParamsSet.h"
  9. #include "RenderAPI/BsGpuBuffer.h"
  10. #include "Renderer/BsLight.h"
  11. #include "Renderer/BsRendererUtility.h"
  12. #include "BsStandardDeferredLighting.h"
  13. namespace bs { namespace ct
  14. {
  15. static const UINT32 BUFFER_INCREMENT = 16 * sizeof(LightData);
  16. TiledLightingParamDef gTiledLightingParamDef;
  17. RendererLight::RendererLight(Light* light)
  18. :internal(light)
  19. { }
  20. void RendererLight::getParameters(LightData& output) const
  21. {
  22. Radian spotAngle = Math::clamp(internal->getSpotAngle() * 0.5f, Degree(0), Degree(89));
  23. Radian spotFalloffAngle = Math::clamp(internal->getSpotFalloffAngle() * 0.5f, Degree(0), (Degree)spotAngle);
  24. Color color = internal->getColor();
  25. const Transform& tfrm = internal->getTransform();
  26. output.position = tfrm.getPosition();
  27. output.attRadius = internal->getBounds().getRadius();
  28. output.srcRadius = internal->getSourceRadius();
  29. output.direction = -tfrm.getRotation().zAxis();
  30. output.luminance = internal->getLuminance();
  31. output.spotAngles.x = spotAngle.valueRadians();
  32. output.spotAngles.y = Math::cos(output.spotAngles.x);
  33. output.spotAngles.z = 1.0f / std::max(Math::cos(spotFalloffAngle) - output.spotAngles.y, 0.001f);
  34. output.attRadiusSqrdInv = 1.0f / (output.attRadius * output.attRadius);
  35. output.color = Vector3(color.r, color.g, color.b);
  36. // If directional lights, convert angular radius in degrees to radians
  37. if (internal->getType() == LightType::Directional)
  38. output.srcRadius *= Math::DEG2RAD;
  39. output.shiftedLightPosition = getShiftedLightPosition();
  40. }
  41. void RendererLight::getParameters(SPtr<GpuParamBlockBuffer>& buffer) const
  42. {
  43. LightData lightData;
  44. getParameters(lightData);
  45. float type = 0.0f;
  46. switch (internal->getType())
  47. {
  48. case LightType::Directional:
  49. type = 0;
  50. break;
  51. case LightType::Radial:
  52. type = 0.3f;
  53. break;
  54. case LightType::Spot:
  55. type = 0.8f;
  56. break;
  57. default:
  58. break;
  59. }
  60. gPerLightParamDef.gLightPositionAndSrcRadius.set(buffer, Vector4(lightData.position, lightData.srcRadius));
  61. gPerLightParamDef.gLightColorAndLuminance.set(buffer, Vector4(lightData.color, lightData.luminance));
  62. gPerLightParamDef.gLightSpotAnglesAndSqrdInvAttRadius.set(buffer, Vector4(lightData.spotAngles, lightData.attRadiusSqrdInv));
  63. gPerLightParamDef.gLightDirectionAndAttRadius.set(buffer, Vector4(lightData.direction, lightData.attRadius));
  64. gPerLightParamDef.gShiftedLightPositionAndType.set(buffer, Vector4(lightData.shiftedLightPosition, type));
  65. Vector4 lightGeometry;
  66. lightGeometry.x = internal->getType() == LightType::Spot ? (float)Light::LIGHT_CONE_NUM_SIDES : 0;
  67. lightGeometry.y = (float)Light::LIGHT_CONE_NUM_SLICES;
  68. lightGeometry.z = internal->getBounds().getRadius();
  69. float extraRadius = lightData.srcRadius / Math::tan(lightData.spotAngles.x * 0.5f);
  70. float coneRadius = Math::sin(lightData.spotAngles.x) * (internal->getAttenuationRadius() + extraRadius);
  71. lightGeometry.w = coneRadius;
  72. gPerLightParamDef.gLightGeometry.set(buffer, lightGeometry);
  73. const Transform& tfrm = internal->getTransform();
  74. Quaternion lightRotation(BsIdentity);
  75. lightRotation.lookRotation(-tfrm.getRotation().zAxis());
  76. Matrix4 transform = Matrix4::TRS(lightData.shiftedLightPosition, lightRotation, Vector3::ONE);
  77. gPerLightParamDef.gMatConeTransform.set(buffer, transform);
  78. }
  79. Vector3 RendererLight::getShiftedLightPosition() const
  80. {
  81. const Transform& tfrm = internal->getTransform();
  82. Vector3 direction = -tfrm.getRotation().zAxis();
  83. // Create position for fake attenuation for area spot lights (with disc center)
  84. if (internal->getType() == LightType::Spot)
  85. return tfrm.getPosition() - direction * (internal->getSourceRadius() / Math::tan(internal->getSpotAngle() * 0.5f));
  86. else
  87. return tfrm.getPosition();
  88. }
  89. GBufferParams::GBufferParams(const SPtr<Material>& material, const SPtr<GpuParamsSet>& paramsSet)
  90. : mMaterial(material), mParamsSet(paramsSet)
  91. {
  92. mGBufferA = material->getParamTexture("gGBufferATex");
  93. mGBufferB = material->getParamTexture("gGBufferBTex");
  94. mGBufferC = material->getParamTexture("gGBufferCTex");
  95. mGBufferDepth = material->getParamTexture("gDepthBufferTex");
  96. SAMPLER_STATE_DESC desc;
  97. desc.minFilter = FO_POINT;
  98. desc.magFilter = FO_POINT;
  99. desc.mipFilter = FO_POINT;
  100. SPtr<SamplerState> ss = SamplerState::create(desc);
  101. material->setSamplerState("gDepthBufferSamp", ss);
  102. }
  103. void GBufferParams::bind(const GBufferTextures& gbuffer)
  104. {
  105. mGBufferA.set(gbuffer.albedo);
  106. mGBufferB.set(gbuffer.normals);
  107. mGBufferC.set(gbuffer.roughMetal);
  108. mGBufferDepth.set(gbuffer.depth);
  109. mMaterial->updateParamsSet(mParamsSet);
  110. }
  111. VisibleLightData::VisibleLightData()
  112. :mNumLights{}, mNumShadowedLights{}
  113. { }
  114. void VisibleLightData::update(const SceneInfo& sceneInfo, const RendererViewGroup& viewGroup)
  115. {
  116. const VisibilityInfo& visibility = viewGroup.getVisibilityInfo();
  117. for (UINT32 i = 0; i < (UINT32)LightType::Count; i++)
  118. mVisibleLights[i].clear();
  119. // Generate a list of lights and their GPU buffers
  120. UINT32 numDirLights = (UINT32)sceneInfo.directionalLights.size();
  121. for (UINT32 i = 0; i < numDirLights; i++)
  122. mVisibleLights[(UINT32)LightType::Directional].push_back(&sceneInfo.directionalLights[i]);
  123. UINT32 numRadialLights = (UINT32)sceneInfo.radialLights.size();
  124. for(UINT32 i = 0; i < numRadialLights; i++)
  125. {
  126. if (!visibility.radialLights[i])
  127. continue;
  128. mVisibleLights[(UINT32)LightType::Radial].push_back(&sceneInfo.radialLights[i]);
  129. }
  130. UINT32 numSpotLights = (UINT32)sceneInfo.spotLights.size();
  131. for (UINT32 i = 0; i < numSpotLights; i++)
  132. {
  133. if (!visibility.spotLights[i])
  134. continue;
  135. mVisibleLights[(UINT32)LightType::Spot].push_back(&sceneInfo.spotLights[i]);
  136. }
  137. for (UINT32 i = 0; i < (UINT32)LightType::Count; i++)
  138. mNumLights[i] = (UINT32)mVisibleLights[i].size();
  139. // Partition all visible lights so that unshadowed ones come first
  140. auto partition = [](Vector<const RendererLight*>& entries)
  141. {
  142. int first = 0;
  143. for (UINT32 i = 0; i < (UINT32)entries.size(); ++i)
  144. {
  145. if(entries[i]->internal->getCastsShadow())
  146. {
  147. first = i;
  148. break;
  149. }
  150. }
  151. for(UINT32 i = first + 1; i < (UINT32)entries.size(); ++i)
  152. {
  153. if(!entries[i]->internal->getCastsShadow())
  154. {
  155. std::swap(entries[i], entries[first]);
  156. ++first;
  157. }
  158. }
  159. return first;
  160. };
  161. for (UINT32 i = 0; i < (UINT32)LightType::Count; i++)
  162. mNumShadowedLights[i] = mNumLights[i] - partition(mVisibleLights[i]);
  163. // Generate light data to initialize the GPU buffer with
  164. for(auto& lightsPerType : mVisibleLights)
  165. {
  166. for(auto& entry : lightsPerType)
  167. {
  168. mLightDataTemp.push_back(LightData());
  169. entry->getParameters(mLightDataTemp.back());
  170. }
  171. }
  172. UINT32 size = (UINT32)mLightDataTemp.size() * sizeof(LightData);
  173. UINT32 curBufferSize;
  174. if (mLightBuffer != nullptr)
  175. curBufferSize = mLightBuffer->getSize();
  176. else
  177. curBufferSize = 0;
  178. if (size > curBufferSize || curBufferSize == 0)
  179. {
  180. // Allocate at least one block even if no lights, to avoid issues with null buffers
  181. UINT32 bufferSize = std::max(1, Math::ceilToInt(size / (float)BUFFER_INCREMENT)) * BUFFER_INCREMENT;
  182. GPU_BUFFER_DESC bufferDesc;
  183. bufferDesc.type = GBT_STRUCTURED;
  184. bufferDesc.elementCount = bufferSize / sizeof(LightData);
  185. bufferDesc.elementSize = sizeof(LightData);
  186. bufferDesc.format = BF_UNKNOWN;
  187. mLightBuffer = GpuBuffer::create(bufferDesc);
  188. }
  189. if (size > 0)
  190. mLightBuffer->writeData(0, size, mLightDataTemp.data(), BWT_DISCARD);
  191. mLightDataTemp.clear();
  192. }
  193. const UINT32 TiledDeferredLightingMat::TILE_SIZE = 16;
  194. ShaderVariation TiledDeferredLightingMat::VAR_1MSAA = ShaderVariation({
  195. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  196. ShaderVariation::Param("MSAA_COUNT", 1)
  197. });
  198. ShaderVariation TiledDeferredLightingMat::VAR_2MSAA = ShaderVariation({
  199. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  200. ShaderVariation::Param("MSAA_COUNT", 2)
  201. });
  202. ShaderVariation TiledDeferredLightingMat::VAR_4MSAA = ShaderVariation({
  203. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  204. ShaderVariation::Param("MSAA_COUNT", 4)
  205. });
  206. ShaderVariation TiledDeferredLightingMat::VAR_8MSAA = ShaderVariation({
  207. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  208. ShaderVariation::Param("MSAA_COUNT", 8)
  209. });
  210. TiledDeferredLightingMat::TiledDeferredLightingMat()
  211. :mGBufferParams(mMaterial, mParamsSet)
  212. {
  213. mSampleCount = mVariation.getUInt("MSAA_COUNT");
  214. SPtr<GpuParams> params = mParamsSet->getGpuParams();
  215. params->getBufferParam(GPT_COMPUTE_PROGRAM, "gLights", mLightBufferParam);
  216. if (params->hasLoadStoreTexture(GPT_COMPUTE_PROGRAM, "gOutput"))
  217. params->getLoadStoreTextureParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputTextureParam);
  218. if (params->hasBuffer(GPT_COMPUTE_PROGRAM, "gOutput"))
  219. params->getBufferParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputBufferParam);
  220. if (mSampleCount > 1)
  221. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gMSAACoverage", mMSAACoverageTexParam);
  222. mParamBuffer = gTiledLightingParamDef.createBuffer();
  223. mParamsSet->setParamBlockBuffer("Params", mParamBuffer, true);
  224. }
  225. void TiledDeferredLightingMat::_initVariations(ShaderVariations& variations)
  226. {
  227. variations.add(VAR_1MSAA);
  228. variations.add(VAR_2MSAA);
  229. variations.add(VAR_4MSAA);
  230. variations.add(VAR_8MSAA);
  231. }
  232. void TiledDeferredLightingMat::execute(const RendererView& view, const VisibleLightData& lightData,
  233. const GBufferTextures& gbuffer, const SPtr<Texture>& lightAccumTex, const SPtr<GpuBuffer>& lightAccumBuffer,
  234. const SPtr<Texture>& msaaCoverage)
  235. {
  236. const RendererViewProperties& viewProps = view.getProperties();
  237. const RenderSettings& settings = view.getRenderSettings();
  238. mLightBufferParam.set(lightData.getLightBuffer());
  239. UINT32 width = viewProps.viewRect.width;
  240. UINT32 height = viewProps.viewRect.height;
  241. Vector2I framebufferSize;
  242. framebufferSize[0] = width;
  243. framebufferSize[1] = height;
  244. gTiledLightingParamDef.gFramebufferSize.set(mParamBuffer, framebufferSize);
  245. if (!settings.enableLighting)
  246. {
  247. Vector4I lightCounts;
  248. lightCounts[0] = 0;
  249. lightCounts[1] = 0;
  250. lightCounts[2] = 0;
  251. lightCounts[3] = 0;
  252. Vector2I lightStrides;
  253. lightStrides[0] = 0;
  254. lightStrides[1] = 0;
  255. gTiledLightingParamDef.gLightCounts.set(mParamBuffer, lightCounts);
  256. gTiledLightingParamDef.gLightStrides.set(mParamBuffer, lightStrides);
  257. }
  258. else
  259. {
  260. Vector4I unshadowedLightCounts;
  261. unshadowedLightCounts[0] = lightData.getNumUnshadowedLights(LightType::Directional);
  262. unshadowedLightCounts[1] = lightData.getNumUnshadowedLights(LightType::Radial);
  263. unshadowedLightCounts[2] = lightData.getNumUnshadowedLights(LightType::Spot);
  264. unshadowedLightCounts[3] = unshadowedLightCounts[0] + unshadowedLightCounts[1] + unshadowedLightCounts[2];
  265. Vector4I lightCounts;
  266. lightCounts[0] = lightData.getNumLights(LightType::Directional);
  267. lightCounts[1] = lightData.getNumLights(LightType::Radial);
  268. lightCounts[2] = lightData.getNumLights(LightType::Spot);
  269. lightCounts[3] = lightCounts[0] + lightCounts[1] + lightCounts[2];
  270. Vector2I lightStrides;
  271. lightStrides[0] = lightCounts[0];
  272. lightStrides[1] = lightStrides[0] + lightCounts[1];
  273. if(!settings.enableShadows)
  274. gTiledLightingParamDef.gLightCounts.set(mParamBuffer, lightCounts);
  275. else
  276. gTiledLightingParamDef.gLightCounts.set(mParamBuffer, unshadowedLightCounts);
  277. gTiledLightingParamDef.gLightStrides.set(mParamBuffer, lightStrides);
  278. }
  279. mParamBuffer->flushToGPU();
  280. mGBufferParams.bind(gbuffer);
  281. mParamsSet->setParamBlockBuffer("PerCamera", view.getPerViewBuffer(), true);
  282. if (mSampleCount > 1)
  283. {
  284. mOutputBufferParam.set(lightAccumBuffer);
  285. mMSAACoverageTexParam.set(msaaCoverage);
  286. }
  287. else
  288. mOutputTextureParam.set(lightAccumTex);
  289. UINT32 numTilesX = (UINT32)Math::ceilToInt(width / (float)TILE_SIZE);
  290. UINT32 numTilesY = (UINT32)Math::ceilToInt(height / (float)TILE_SIZE);
  291. gRendererUtility().setComputePass(mMaterial, 0);
  292. gRendererUtility().setPassParams(mParamsSet);
  293. RenderAPI::instance().dispatchCompute(numTilesX, numTilesY);
  294. }
  295. TiledDeferredLightingMat* TiledDeferredLightingMat::getVariation(UINT32 msaaCount)
  296. {
  297. switch(msaaCount)
  298. {
  299. case 1:
  300. return get(VAR_1MSAA);
  301. case 2:
  302. return get(VAR_2MSAA);
  303. case 4:
  304. return get(VAR_4MSAA);
  305. case 8:
  306. default:
  307. return get(VAR_8MSAA);
  308. }
  309. }
  310. FlatFramebufferToTextureParamDef gFlatFramebufferToTextureParamDef;
  311. FlatFramebufferToTextureMat::FlatFramebufferToTextureMat()
  312. {
  313. SPtr<GpuParams> params = mParamsSet->getGpuParams();
  314. params->getBufferParam(GPT_FRAGMENT_PROGRAM, "gInput", mInputParam);
  315. mParamBuffer = gTiledLightingParamDef.createBuffer();
  316. mParamsSet->setParamBlockBuffer("Params", mParamBuffer, true);
  317. }
  318. void FlatFramebufferToTextureMat::_initVariations(ShaderVariations& variations)
  319. {
  320. // Do nothing
  321. }
  322. void FlatFramebufferToTextureMat::execute(const SPtr<GpuBuffer>& flatFramebuffer, const SPtr<Texture>& target)
  323. {
  324. const TextureProperties& props = target->getProperties();
  325. Vector2I framebufferSize;
  326. framebufferSize[0] = props.getWidth();
  327. framebufferSize[1] = props.getHeight();
  328. gFlatFramebufferToTextureParamDef.gFramebufferSize.set(mParamBuffer, framebufferSize);
  329. gFlatFramebufferToTextureParamDef.gSampleCount.set(mParamBuffer, props.getNumSamples());
  330. mParamBuffer->flushToGPU();
  331. mInputParam.set(flatFramebuffer);
  332. gRendererUtility().setPass(mMaterial, 0);
  333. gRendererUtility().setPassParams(mParamsSet);
  334. Rect2 area(0.0f, 0.0f, (float)props.getWidth(), (float)props.getHeight());
  335. gRendererUtility().drawScreenQuad(area);
  336. }
  337. }}