BsLightRendering.cpp 14 KB

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