BsLightRendering.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. UINT32 numUnshadowed = 0;
  143. int first = -1;
  144. for (UINT32 i = 0; i < (UINT32)entries.size(); ++i)
  145. {
  146. if(entries[i]->internal->getCastsShadow())
  147. {
  148. first = i;
  149. break;
  150. }
  151. else
  152. ++numUnshadowed;
  153. }
  154. if(first != -1)
  155. {
  156. for(UINT32 i = first + 1; i < (UINT32)entries.size(); ++i)
  157. {
  158. if(!entries[i]->internal->getCastsShadow())
  159. {
  160. std::swap(entries[i], entries[first]);
  161. ++numUnshadowed;
  162. }
  163. }
  164. }
  165. return numUnshadowed;
  166. };
  167. for (UINT32 i = 0; i < (UINT32)LightType::Count; i++)
  168. mNumShadowedLights[i] = mNumLights[i] - partition(mVisibleLights[i]);
  169. // Generate light data to initialize the GPU buffer with
  170. for(auto& lightsPerType : mVisibleLights)
  171. {
  172. for(auto& entry : lightsPerType)
  173. {
  174. mLightDataTemp.push_back(LightData());
  175. entry->getParameters(mLightDataTemp.back());
  176. }
  177. }
  178. UINT32 size = (UINT32)mLightDataTemp.size() * sizeof(LightData);
  179. UINT32 curBufferSize;
  180. if (mLightBuffer != nullptr)
  181. curBufferSize = mLightBuffer->getSize();
  182. else
  183. curBufferSize = 0;
  184. if (size > curBufferSize || curBufferSize == 0)
  185. {
  186. // Allocate at least one block even if no lights, to avoid issues with null buffers
  187. UINT32 bufferSize = std::max(1, Math::ceilToInt(size / (float)BUFFER_INCREMENT)) * BUFFER_INCREMENT;
  188. GPU_BUFFER_DESC bufferDesc;
  189. bufferDesc.type = GBT_STRUCTURED;
  190. bufferDesc.elementCount = bufferSize / sizeof(LightData);
  191. bufferDesc.elementSize = sizeof(LightData);
  192. bufferDesc.format = BF_UNKNOWN;
  193. mLightBuffer = GpuBuffer::create(bufferDesc);
  194. }
  195. if (size > 0)
  196. mLightBuffer->writeData(0, size, mLightDataTemp.data(), BWT_DISCARD);
  197. mLightDataTemp.clear();
  198. }
  199. const UINT32 TiledDeferredLightingMat::TILE_SIZE = 16;
  200. ShaderVariation TiledDeferredLightingMat::VAR_1MSAA = ShaderVariation({
  201. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  202. ShaderVariation::Param("MSAA_COUNT", 1)
  203. });
  204. ShaderVariation TiledDeferredLightingMat::VAR_2MSAA = ShaderVariation({
  205. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  206. ShaderVariation::Param("MSAA_COUNT", 2)
  207. });
  208. ShaderVariation TiledDeferredLightingMat::VAR_4MSAA = ShaderVariation({
  209. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  210. ShaderVariation::Param("MSAA_COUNT", 4)
  211. });
  212. ShaderVariation TiledDeferredLightingMat::VAR_8MSAA = ShaderVariation({
  213. ShaderVariation::Param("TILE_SIZE", TILE_SIZE),
  214. ShaderVariation::Param("MSAA_COUNT", 8)
  215. });
  216. TiledDeferredLightingMat::TiledDeferredLightingMat()
  217. :mGBufferParams(mMaterial, mParamsSet)
  218. {
  219. mSampleCount = mVariation.getUInt("MSAA_COUNT");
  220. SPtr<GpuParams> params = mParamsSet->getGpuParams();
  221. params->getBufferParam(GPT_COMPUTE_PROGRAM, "gLights", mLightBufferParam);
  222. if (params->hasLoadStoreTexture(GPT_COMPUTE_PROGRAM, "gOutput"))
  223. params->getLoadStoreTextureParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputTextureParam);
  224. if (params->hasBuffer(GPT_COMPUTE_PROGRAM, "gOutput"))
  225. params->getBufferParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputBufferParam);
  226. if (mSampleCount > 1)
  227. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gMSAACoverage", mMSAACoverageTexParam);
  228. mParamBuffer = gTiledLightingParamDef.createBuffer();
  229. params->setParamBlockBuffer("Params", mParamBuffer);
  230. }
  231. void TiledDeferredLightingMat::_initVariations(ShaderVariations& variations)
  232. {
  233. variations.add(VAR_1MSAA);
  234. variations.add(VAR_2MSAA);
  235. variations.add(VAR_4MSAA);
  236. variations.add(VAR_8MSAA);
  237. }
  238. void TiledDeferredLightingMat::execute(const RendererView& view, const VisibleLightData& lightData,
  239. const GBufferTextures& gbuffer, const SPtr<Texture>& lightAccumTex, const SPtr<GpuBuffer>& lightAccumBuffer,
  240. const SPtr<Texture>& msaaCoverage)
  241. {
  242. const RendererViewProperties& viewProps = view.getProperties();
  243. const RenderSettings& settings = view.getRenderSettings();
  244. mLightBufferParam.set(lightData.getLightBuffer());
  245. UINT32 width = viewProps.viewRect.width;
  246. UINT32 height = viewProps.viewRect.height;
  247. Vector2I framebufferSize;
  248. framebufferSize[0] = width;
  249. framebufferSize[1] = height;
  250. gTiledLightingParamDef.gFramebufferSize.set(mParamBuffer, framebufferSize);
  251. if (!settings.enableLighting)
  252. {
  253. Vector4I lightCounts;
  254. lightCounts[0] = 0;
  255. lightCounts[1] = 0;
  256. lightCounts[2] = 0;
  257. lightCounts[3] = 0;
  258. Vector2I lightStrides;
  259. lightStrides[0] = 0;
  260. lightStrides[1] = 0;
  261. gTiledLightingParamDef.gLightCounts.set(mParamBuffer, lightCounts);
  262. gTiledLightingParamDef.gLightStrides.set(mParamBuffer, lightStrides);
  263. }
  264. else
  265. {
  266. Vector4I unshadowedLightCounts;
  267. unshadowedLightCounts[0] = lightData.getNumUnshadowedLights(LightType::Directional);
  268. unshadowedLightCounts[1] = lightData.getNumUnshadowedLights(LightType::Radial);
  269. unshadowedLightCounts[2] = lightData.getNumUnshadowedLights(LightType::Spot);
  270. unshadowedLightCounts[3] = unshadowedLightCounts[0] + unshadowedLightCounts[1] + unshadowedLightCounts[2];
  271. Vector4I lightCounts;
  272. lightCounts[0] = lightData.getNumLights(LightType::Directional);
  273. lightCounts[1] = lightData.getNumLights(LightType::Radial);
  274. lightCounts[2] = lightData.getNumLights(LightType::Spot);
  275. lightCounts[3] = lightCounts[0] + lightCounts[1] + lightCounts[2];
  276. Vector2I lightStrides;
  277. lightStrides[0] = lightCounts[0];
  278. lightStrides[1] = lightStrides[0] + lightCounts[1];
  279. if(!settings.enableShadows)
  280. gTiledLightingParamDef.gLightCounts.set(mParamBuffer, lightCounts);
  281. else
  282. gTiledLightingParamDef.gLightCounts.set(mParamBuffer, unshadowedLightCounts);
  283. gTiledLightingParamDef.gLightStrides.set(mParamBuffer, lightStrides);
  284. }
  285. mParamBuffer->flushToGPU();
  286. mGBufferParams.bind(gbuffer);
  287. mParamsSet->getGpuParams()->setParamBlockBuffer("PerCamera", view.getPerViewBuffer());
  288. if (mSampleCount > 1)
  289. {
  290. mOutputBufferParam.set(lightAccumBuffer);
  291. mMSAACoverageTexParam.set(msaaCoverage);
  292. }
  293. else
  294. mOutputTextureParam.set(lightAccumTex);
  295. UINT32 numTilesX = (UINT32)Math::ceilToInt(width / (float)TILE_SIZE);
  296. UINT32 numTilesY = (UINT32)Math::ceilToInt(height / (float)TILE_SIZE);
  297. gRendererUtility().setComputePass(mMaterial, 0);
  298. gRendererUtility().setPassParams(mParamsSet);
  299. RenderAPI::instance().dispatchCompute(numTilesX, numTilesY);
  300. }
  301. TiledDeferredLightingMat* TiledDeferredLightingMat::getVariation(UINT32 msaaCount)
  302. {
  303. switch(msaaCount)
  304. {
  305. case 1:
  306. return get(VAR_1MSAA);
  307. case 2:
  308. return get(VAR_2MSAA);
  309. case 4:
  310. return get(VAR_4MSAA);
  311. case 8:
  312. default:
  313. return get(VAR_8MSAA);
  314. }
  315. }
  316. FlatFramebufferToTextureParamDef gFlatFramebufferToTextureParamDef;
  317. FlatFramebufferToTextureMat::FlatFramebufferToTextureMat()
  318. {
  319. SPtr<GpuParams> params = mParamsSet->getGpuParams();
  320. params->getBufferParam(GPT_FRAGMENT_PROGRAM, "gInput", mInputParam);
  321. mParamBuffer = gTiledLightingParamDef.createBuffer();
  322. params->setParamBlockBuffer("Params", mParamBuffer);
  323. }
  324. void FlatFramebufferToTextureMat::_initVariations(ShaderVariations& variations)
  325. {
  326. // Do nothing
  327. }
  328. void FlatFramebufferToTextureMat::execute(const SPtr<GpuBuffer>& flatFramebuffer, const SPtr<Texture>& target)
  329. {
  330. const TextureProperties& props = target->getProperties();
  331. Vector2I framebufferSize;
  332. framebufferSize[0] = props.getWidth();
  333. framebufferSize[1] = props.getHeight();
  334. gFlatFramebufferToTextureParamDef.gFramebufferSize.set(mParamBuffer, framebufferSize);
  335. gFlatFramebufferToTextureParamDef.gSampleCount.set(mParamBuffer, props.getNumSamples());
  336. mParamBuffer->flushToGPU();
  337. mInputParam.set(flatFramebuffer);
  338. gRendererUtility().setPass(mMaterial, 0);
  339. gRendererUtility().setPassParams(mParamsSet);
  340. Rect2 area(0.0f, 0.0f, (float)props.getWidth(), (float)props.getHeight());
  341. gRendererUtility().drawScreenQuad(area);
  342. }
  343. }}