BsLightRendering.cpp 13 KB

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