BsLightRendering.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsLightRendering.h"
  4. #include "BsMaterial.h"
  5. #include "BsShader.h"
  6. #include "BsRenderBeast.h"
  7. #include "BsRenderTargets.h"
  8. #include "BsGpuParams.h"
  9. #include "BsGpuParamsSet.h"
  10. #include "BsGpuBuffer.h"
  11. #include "BsLight.h"
  12. #include "BsRendererUtility.h"
  13. namespace bs { namespace ct
  14. {
  15. static const UINT32 BUFFER_INCREMENT = 16 * sizeof(LightData);
  16. TiledLightingParamDef gTiledLightingParamDef;
  17. PerLightParamDef gPerLightParamDef;
  18. RendererLight::RendererLight(Light* light)
  19. :internal(light)
  20. { }
  21. void RendererLight::getParameters(LightData& output) const
  22. {
  23. Radian spotAngle = Math::clamp(internal->getSpotAngle() * 0.5f, Degree(0), Degree(89));
  24. Radian spotFalloffAngle = Math::clamp(internal->getSpotFalloffAngle() * 0.5f, Degree(0), (Degree)spotAngle);
  25. Color color = internal->getColor();
  26. output.position = internal->getPosition();
  27. output.attRadius = internal->getBounds().getRadius();
  28. output.srcRadius = internal->getSourceRadius();
  29. output.direction = internal->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. // Create position for fake attenuation for area spot lights (with disc center)
  40. if (internal->getType() == LightType::Spot)
  41. output.shiftedLightPosition = output.position - output.direction * (output.srcRadius / Math::tan(spotAngle * 0.5f));
  42. else
  43. output.shiftedLightPosition = output.position;
  44. }
  45. void RendererLight::getParameters(SPtr<GpuParamBlockBuffer>& buffer) const
  46. {
  47. LightData lightData;
  48. getParameters(lightData);
  49. float type = 0.0f;
  50. switch (internal->getType())
  51. {
  52. case LightType::Directional:
  53. type = 0;
  54. break;
  55. case LightType::Radial:
  56. type = 0.3f;
  57. break;
  58. case LightType::Spot:
  59. type = 0.8f;
  60. break;
  61. }
  62. gPerLightParamDef.gLightPositionAndSrcRadius.set(buffer, Vector4(lightData.position, lightData.srcRadius));
  63. gPerLightParamDef.gLightColorAndLuminance.set(buffer, Vector4(lightData.color, lightData.luminance));
  64. gPerLightParamDef.gLightSpotAnglesAndSqrdInvAttRadius.set(buffer, Vector4(lightData.spotAngles, lightData.attRadiusSqrdInv));
  65. gPerLightParamDef.gLightDirectionAndAttRadius.set(buffer, Vector4(lightData.direction, lightData.attRadius));
  66. gPerLightParamDef.gShiftedLightPositionAndType.set(buffer, Vector4(lightData.shiftedLightPosition, type));
  67. Vector4 lightGeometry;
  68. lightGeometry.x = internal->getType() == LightType::Spot ? (float)Light::LIGHT_CONE_NUM_SIDES : 0;
  69. lightGeometry.y = (float)Light::LIGHT_CONE_NUM_SLICES;
  70. lightGeometry.z = internal->getBounds().getRadius();
  71. float coneRadius = Math::sin(lightData.spotAngles.x) * internal->getAttenuationRadius();
  72. lightGeometry.w = coneRadius;
  73. gPerLightParamDef.gLightGeometry.set(buffer, lightGeometry);
  74. Matrix4 transform = Matrix4::TRS(internal->getPosition(), internal->getRotation(), Vector3::ONE);
  75. gPerLightParamDef.gMatConeTransform.set(buffer, transform);
  76. }
  77. GBufferParams::GBufferParams(const SPtr<Material>& material, const SPtr<GpuParamsSet>& paramsSet)
  78. {
  79. SPtr<GpuParams> params = paramsSet->getGpuParams();
  80. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferATex", mGBufferA);
  81. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferBTex", mGBufferB);
  82. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gGBufferCTex", mGBufferC);
  83. params->getTextureParam(GPT_COMPUTE_PROGRAM, "gDepthBufferTex", mGBufferDepth);
  84. }
  85. void GBufferParams::bind(const SPtr<RenderTargets>& renderTargets)
  86. {
  87. mGBufferA.set(renderTargets->getGBufferA());
  88. mGBufferB.set(renderTargets->getGBufferB());
  89. mGBufferC.set(renderTargets->getGBufferC());
  90. mGBufferDepth.set(renderTargets->getSceneDepth());
  91. }
  92. template<bool MSAA>
  93. DirectionalLightMat<MSAA>::DirectionalLightMat()
  94. :mGBufferParams(mMaterial, mParamsSet)
  95. {
  96. }
  97. template<bool MSAA>
  98. void DirectionalLightMat<MSAA>::_initDefines(ShaderDefines& defines)
  99. {
  100. if (MSAA)
  101. defines.set("MSAA_COUNT", 2); // Actual count doesn't matter, as long as it's greater than one
  102. else
  103. defines.set("MSAA_COUNT", 1);
  104. }
  105. template<bool MSAA>
  106. void DirectionalLightMat<MSAA>::bind(const SPtr<RenderTargets>& gbuffer, const SPtr<GpuParamBlockBuffer>& perCamera)
  107. {
  108. RendererUtility::instance().setPass(mMaterial, 0);
  109. mGBufferParams.bind(gbuffer);
  110. mParamsSet->setParamBlockBuffer("PerCamera", perCamera, true);
  111. }
  112. template<bool MSAA>
  113. void DirectionalLightMat<MSAA>::setPerLightParams(const SPtr<GpuParamBlockBuffer>& perLight)
  114. {
  115. mParamsSet->setParamBlockBuffer("PerLight", perLight, true);
  116. gRendererUtility().setPassParams(mParamsSet);
  117. }
  118. template class DirectionalLightMat<true>;
  119. template class DirectionalLightMat<false>;
  120. template<bool MSAA, bool InsideGeometry>
  121. PointLightMat<MSAA, InsideGeometry>::PointLightMat()
  122. :mGBufferParams(mMaterial, mParamsSet)
  123. {
  124. }
  125. template<bool MSAA, bool InsideGeometry>
  126. void PointLightMat<MSAA, InsideGeometry>::_initDefines(ShaderDefines& defines)
  127. {
  128. if (MSAA)
  129. defines.set("MSAA_COUNT", 2); // Actual count doesn't matter, as long as it's greater than one
  130. else
  131. defines.set("MSAA_COUNT", 1);
  132. if (InsideGeometry)
  133. defines.set("INSIDE_GEOMETRY", 1);
  134. }
  135. template<bool MSAA, bool InsideGeometry>
  136. void PointLightMat<MSAA, InsideGeometry>::bind(const SPtr<RenderTargets>& gbuffer,
  137. const SPtr<GpuParamBlockBuffer>& perCamera)
  138. {
  139. RendererUtility::instance().setPass(mMaterial, 0);
  140. mGBufferParams.bind(gbuffer);
  141. mParamsSet->setParamBlockBuffer("PerCamera", perCamera, true);
  142. }
  143. template<bool MSAA, bool InsideGeometry>
  144. void PointLightMat<MSAA, InsideGeometry>::setPerLightParams(const SPtr<GpuParamBlockBuffer>& perLight)
  145. {
  146. mParamsSet->setParamBlockBuffer("PerLight", perLight, true);
  147. gRendererUtility().setPassParams(mParamsSet);
  148. }
  149. template class PointLightMat<false, false>;
  150. template class PointLightMat<false, true>;
  151. template class PointLightMat<true, false>;
  152. template class PointLightMat<true, true>;
  153. GPULightData::GPULightData()
  154. :mNumLights{}
  155. { }
  156. void GPULightData::setLights(const Vector<LightData>& lightData, UINT32 numDirLights, UINT32 numRadialLights,
  157. UINT32 numSpotLights)
  158. {
  159. mNumLights[0] = numDirLights;
  160. mNumLights[1] = numRadialLights;
  161. mNumLights[2] = numSpotLights;
  162. Vector3I lightOffsets;
  163. lightOffsets[0] = numDirLights;
  164. lightOffsets[1] = lightOffsets[0] + numRadialLights;
  165. lightOffsets[2] = lightOffsets[1] + numSpotLights;
  166. UINT32 totalNumLights = (UINT32)lightOffsets[2];
  167. UINT32 size = totalNumLights * 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, lightData.data(), BWT_DISCARD);
  186. }
  187. const UINT32 TiledDeferredLighting::TILE_SIZE = 16;
  188. TiledDeferredLighting::TiledDeferredLighting(const SPtr<Material>& material, const SPtr<GpuParamsSet>& paramsSet,
  189. UINT32 sampleCount)
  190. : mSampleCount(sampleCount), mMaterial(material), mParamsSet(paramsSet), mGBufferParams(material, paramsSet)
  191. , mLightOffsets()
  192. {
  193. SPtr<GpuParams> params = mParamsSet->getGpuParams();
  194. params->getBufferParam(GPT_COMPUTE_PROGRAM, "gLights", mLightBufferParam);
  195. if (params->hasLoadStoreTexture(GPT_COMPUTE_PROGRAM, "gOutput"))
  196. params->getLoadStoreTextureParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputTextureParam);
  197. if (params->hasBuffer(GPT_COMPUTE_PROGRAM, "gOutput"))
  198. params->getBufferParam(GPT_COMPUTE_PROGRAM, "gOutput", mOutputBufferParam);
  199. mParamBuffer = gTiledLightingParamDef.createBuffer();
  200. mParamsSet->setParamBlockBuffer("Params", mParamBuffer, true);
  201. }
  202. void TiledDeferredLighting::execute(const SPtr<RenderTargets>& renderTargets, const SPtr<GpuParamBlockBuffer>& perCamera,
  203. bool noLighting)
  204. {
  205. Vector2I framebufferSize;
  206. framebufferSize[0] = renderTargets->getWidth();
  207. framebufferSize[1] = renderTargets->getHeight();
  208. gTiledLightingParamDef.gFramebufferSize.set(mParamBuffer, framebufferSize);
  209. if (noLighting)
  210. {
  211. Vector3I lightOffsets;
  212. lightOffsets[0] = 0;
  213. lightOffsets[1] = 0;
  214. lightOffsets[2] = 0;
  215. gTiledLightingParamDef.gLightOffsets.set(mParamBuffer, lightOffsets);
  216. }
  217. else
  218. {
  219. gTiledLightingParamDef.gLightOffsets.set(mParamBuffer, mLightOffsets);
  220. }
  221. mParamBuffer->flushToGPU();
  222. mGBufferParams.bind(renderTargets);
  223. mParamsSet->setParamBlockBuffer("PerCamera", perCamera, true);
  224. if (mSampleCount > 1)
  225. {
  226. SPtr<GpuBuffer> lightAccumulation = renderTargets->getLightAccumulationBuffer();
  227. mOutputBufferParam.set(lightAccumulation);
  228. }
  229. else
  230. {
  231. SPtr<Texture> lightAccumulation = renderTargets->getLightAccumulation();
  232. mOutputTextureParam.set(lightAccumulation);
  233. }
  234. UINT32 width = renderTargets->getWidth();
  235. UINT32 height = renderTargets->getHeight();
  236. UINT32 numTilesX = (UINT32)Math::ceilToInt(width / (float)TILE_SIZE);
  237. UINT32 numTilesY = (UINT32)Math::ceilToInt(height / (float)TILE_SIZE);
  238. gRendererUtility().setComputePass(mMaterial, 0);
  239. gRendererUtility().setPassParams(mParamsSet);
  240. RenderAPI::instance().dispatchCompute(numTilesX, numTilesY);
  241. }
  242. void TiledDeferredLighting::setLights(const GPULightData& lightData)
  243. {
  244. mLightBufferParam.set(lightData.getLightBuffer());
  245. mLightOffsets[0] = lightData.getNumDirLights();
  246. mLightOffsets[1] = mLightOffsets[0] + lightData.getNumRadialLights();
  247. mLightOffsets[2] = mLightOffsets[1] + lightData.getNumSpotLights();
  248. }
  249. template<int MSAA_COUNT>
  250. TTiledDeferredLightingMat<MSAA_COUNT>::TTiledDeferredLightingMat()
  251. :mInternal(mMaterial, mParamsSet, MSAA_COUNT)
  252. {
  253. }
  254. template<int MSAA_COUNT>
  255. void TTiledDeferredLightingMat<MSAA_COUNT>::_initDefines(ShaderDefines& defines)
  256. {
  257. defines.set("TILE_SIZE", TiledDeferredLighting::TILE_SIZE);
  258. defines.set("MSAA_COUNT", MSAA_COUNT);
  259. }
  260. template<int MSAA_COUNT>
  261. void TTiledDeferredLightingMat<MSAA_COUNT>::execute(const SPtr<RenderTargets>& gbuffer,
  262. const SPtr<GpuParamBlockBuffer>& perCamera, bool noLighting)
  263. {
  264. mInternal.execute(gbuffer, perCamera, noLighting);
  265. }
  266. template<int MSAA_COUNT>
  267. void TTiledDeferredLightingMat<MSAA_COUNT>::setLights(const GPULightData& lightData)
  268. {
  269. mInternal.setLights(lightData);
  270. }
  271. TiledDeferredLightingMaterials::TiledDeferredLightingMaterials()
  272. {
  273. mInstances[0] = bs_new<TTiledDeferredLightingMat<1>>();
  274. mInstances[1] = bs_new<TTiledDeferredLightingMat<2>>();
  275. mInstances[2] = bs_new<TTiledDeferredLightingMat<4>>();
  276. mInstances[3] = bs_new<TTiledDeferredLightingMat<8>>();
  277. }
  278. TiledDeferredLightingMaterials::~TiledDeferredLightingMaterials()
  279. {
  280. for (UINT32 i = 0; i < 4; i++)
  281. bs_delete(mInstances[i]);
  282. }
  283. ITiledDeferredLightingMat* TiledDeferredLightingMaterials::get(UINT32 msaa)
  284. {
  285. if (msaa == 1)
  286. return mInstances[0];
  287. else if (msaa == 2)
  288. return mInstances[1];
  289. else if (msaa == 4)
  290. return mInstances[2];
  291. else
  292. return mInstances[3];
  293. }
  294. FlatFramebufferToTextureParamDef gFlatFramebufferToTextureParamDef;
  295. FlatFramebufferToTextureMat::FlatFramebufferToTextureMat()
  296. {
  297. SPtr<GpuParams> params = mParamsSet->getGpuParams();
  298. params->getBufferParam(GPT_FRAGMENT_PROGRAM, "gInput", mInputParam);
  299. mParamBuffer = gTiledLightingParamDef.createBuffer();
  300. mParamsSet->setParamBlockBuffer("Params", mParamBuffer, true);
  301. }
  302. void FlatFramebufferToTextureMat::_initDefines(ShaderDefines& defines)
  303. {
  304. // Do nothing
  305. }
  306. void FlatFramebufferToTextureMat::execute(const SPtr<GpuBuffer>& flatFramebuffer, const SPtr<Texture>& target)
  307. {
  308. const TextureProperties& props = target->getProperties();
  309. Vector2I framebufferSize;
  310. framebufferSize[0] = props.getWidth();
  311. framebufferSize[1] = props.getHeight();
  312. gFlatFramebufferToTextureParamDef.gFramebufferSize.set(mParamBuffer, framebufferSize);
  313. gFlatFramebufferToTextureParamDef.gSampleCount.set(mParamBuffer, props.getNumSamples());
  314. mParamBuffer->flushToGPU();
  315. mInputParam.set(flatFramebuffer);
  316. gRendererUtility().setPass(mMaterial, 0);
  317. gRendererUtility().setPassParams(mParamsSet);
  318. Rect2 area(0.0f, 0.0f, (float)props.getWidth(), (float)props.getHeight());
  319. gRendererUtility().drawScreenQuad(area);
  320. }
  321. }}