TraditionalDeferredShading.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Renderer/TraditionalDeferredShading.h>
  6. #include <AnKi/Renderer/Renderer.h>
  7. #include <AnKi/Renderer/RenderQueue.h>
  8. #include <AnKi/Resource/ResourceManager.h>
  9. #include <AnKi/Resource/MeshResource.h>
  10. #include <AnKi/Shaders/Include/TraditionalDeferredShadingTypes.h>
  11. namespace anki {
  12. TraditionalDeferredLightShading::TraditionalDeferredLightShading(Renderer* r)
  13. : RendererObject(r)
  14. {
  15. }
  16. TraditionalDeferredLightShading::~TraditionalDeferredLightShading()
  17. {
  18. }
  19. Error TraditionalDeferredLightShading::init()
  20. {
  21. // Init progs
  22. {
  23. ANKI_CHECK(
  24. getResourceManager().loadResource("ShaderBinaries/TraditionalDeferredShading.ankiprogbin", m_lightProg));
  25. for(U32 specular = 0; specular <= 1; ++specular)
  26. {
  27. ShaderProgramResourceVariantInitInfo variantInitInfo(m_lightProg);
  28. variantInitInfo.addMutation("LIGHT_TYPE", 0);
  29. variantInitInfo.addMutation("SPECULAR", specular);
  30. const ShaderProgramResourceVariant* variant;
  31. m_lightProg->getOrCreateVariant(variantInitInfo, variant);
  32. m_plightGrProg[specular] = variant->getProgram();
  33. variantInitInfo.addMutation("LIGHT_TYPE", 1);
  34. m_lightProg->getOrCreateVariant(variantInitInfo, variant);
  35. m_slightGrProg[specular] = variant->getProgram();
  36. variantInitInfo.addMutation("LIGHT_TYPE", 2);
  37. m_lightProg->getOrCreateVariant(variantInitInfo, variant);
  38. m_dirLightGrProg[specular] = variant->getProgram();
  39. }
  40. }
  41. // Init meshes
  42. ANKI_CHECK(getResourceManager().loadResource("EngineAssets/Plight.ankimesh", m_plightMesh, false));
  43. ANKI_CHECK(getResourceManager().loadResource("EngineAssets/Slight.ankimesh", m_slightMesh, false));
  44. // Shadow sampler
  45. {
  46. SamplerInitInfo inf;
  47. inf.m_compareOperation = CompareOperation::kLessEqual;
  48. inf.m_addressing = SamplingAddressing::kClamp;
  49. inf.m_mipmapFilter = SamplingFilter::kBase;
  50. inf.m_minMagFilter = SamplingFilter::kLinear;
  51. m_shadowSampler = getGrManager().newSampler(inf);
  52. }
  53. // Skybox
  54. {
  55. ANKI_CHECK(getResourceManager().loadResource("ShaderBinaries/TraditionalDeferredShadingSkybox.ankiprogbin",
  56. m_skyboxProg));
  57. for(U32 i = 0; i < m_skyboxGrProgs.getSize(); ++i)
  58. {
  59. ShaderProgramResourceVariantInitInfo variantInitInfo(m_skyboxProg);
  60. variantInitInfo.addMutation("METHOD", i);
  61. const ShaderProgramResourceVariant* variant;
  62. m_skyboxProg->getOrCreateVariant(variantInitInfo, variant);
  63. m_skyboxGrProgs[i] = variant->getProgram();
  64. }
  65. }
  66. return Error::NONE;
  67. }
  68. void TraditionalDeferredLightShading::bindVertexIndexBuffers(MeshResourcePtr& mesh, CommandBufferPtr& cmdb,
  69. U32& indexCount)
  70. {
  71. // Attrib
  72. U32 bufferBinding;
  73. Format fmt;
  74. U32 relativeOffset;
  75. mesh->getVertexAttributeInfo(VertexAttributeId::POSITION, bufferBinding, fmt, relativeOffset);
  76. cmdb->setVertexAttribute(0, 0, fmt, relativeOffset);
  77. // Vert buff
  78. BufferPtr buff;
  79. PtrSize offset, stride;
  80. mesh->getVertexBufferInfo(bufferBinding, buff, offset, stride);
  81. cmdb->bindVertexBuffer(0, buff, offset, stride);
  82. // Idx buff
  83. IndexType idxType;
  84. mesh->getIndexBufferInfo(buff, offset, indexCount, idxType);
  85. cmdb->bindIndexBuffer(buff, offset, idxType);
  86. }
  87. void TraditionalDeferredLightShading::drawLights(TraditionalDeferredLightShadingDrawInfo& info)
  88. {
  89. CommandBufferPtr& cmdb = info.m_commandBuffer;
  90. RenderPassWorkContext& rgraphCtx = *info.m_renderpassContext;
  91. // Set common state for all
  92. cmdb->setViewport(info.m_viewport.x(), info.m_viewport.y(), info.m_viewport.z(), info.m_viewport.w());
  93. // Skybox first
  94. {
  95. const Bool isSolidColor = info.m_skybox->m_skyboxTexture == nullptr;
  96. cmdb->bindShaderProgram(m_skyboxGrProgs[!isSolidColor]);
  97. cmdb->bindSampler(0, 0, m_r->getSamplers().m_nearestNearestClamp);
  98. rgraphCtx.bindTexture(0, 1, info.m_gbufferDepthRenderTarget,
  99. TextureSubresourceInfo(DepthStencilAspectBit::kDepth));
  100. if(!isSolidColor)
  101. {
  102. cmdb->bindSampler(0, 2, m_r->getSamplers().m_trilinearRepeatAniso);
  103. cmdb->bindTexture(0, 3, TextureViewPtr(const_cast<TextureView*>(info.m_skybox->m_skyboxTexture)));
  104. }
  105. DeferredSkyboxUniforms unis;
  106. unis.m_solidColor = info.m_skybox->m_solidColor;
  107. unis.m_inputTexUvBias = info.m_gbufferTexCoordsBias;
  108. unis.m_inputTexUvScale = info.m_gbufferTexCoordsScale;
  109. unis.m_invertedViewProjectionMat = info.m_invViewProjectionMatrix;
  110. unis.m_cameraPos = info.m_cameraPosWSpace.xyz();
  111. cmdb->setPushConstants(&unis, sizeof(unis));
  112. drawQuad(cmdb);
  113. }
  114. // Set common state for all light drawcalls
  115. {
  116. cmdb->setBlendFactors(0, BlendFactor::kOne, BlendFactor::kOne);
  117. // NOTE: Use nearest sampler because we don't want the result to sample the near tiles
  118. cmdb->bindSampler(0, 2, m_r->getSamplers().m_nearestNearestClamp);
  119. rgraphCtx.bindColorTexture(0, 3, info.m_gbufferRenderTargets[0]);
  120. rgraphCtx.bindColorTexture(0, 4, info.m_gbufferRenderTargets[1]);
  121. rgraphCtx.bindColorTexture(0, 5, info.m_gbufferRenderTargets[2]);
  122. rgraphCtx.bindTexture(0, 6, info.m_gbufferDepthRenderTarget,
  123. TextureSubresourceInfo(DepthStencilAspectBit::kDepth));
  124. // Set shadowmap resources
  125. cmdb->bindSampler(0, 7, m_shadowSampler);
  126. if(info.m_directionalLight && info.m_directionalLight->hasShadow())
  127. {
  128. ANKI_ASSERT(info.m_directionalLightShadowmapRenderTarget.isValid());
  129. rgraphCtx.bindTexture(0, 8, info.m_directionalLightShadowmapRenderTarget,
  130. TextureSubresourceInfo(DepthStencilAspectBit::kDepth));
  131. }
  132. else
  133. {
  134. // No shadows for the dir light, bind something random
  135. rgraphCtx.bindColorTexture(0, 8, info.m_gbufferRenderTargets[0]);
  136. }
  137. }
  138. // Dir light
  139. if(info.m_directionalLight)
  140. {
  141. ANKI_ASSERT(info.m_directionalLight->m_uuid && info.m_directionalLight->m_shadowCascadeCount == 1);
  142. cmdb->bindShaderProgram(m_dirLightGrProg[info.m_computeSpecular]);
  143. DeferredDirectionalLightUniforms* unis = allocateAndBindUniforms<DeferredDirectionalLightUniforms*>(
  144. sizeof(DeferredDirectionalLightUniforms), cmdb, 0, 1);
  145. unis->m_inputTexUvScale = info.m_gbufferTexCoordsScale;
  146. unis->m_inputTexUvBias = info.m_gbufferTexCoordsBias;
  147. unis->m_fbUvScale = info.m_lightbufferTexCoordsScale;
  148. unis->m_fbUvBias = info.m_lightbufferTexCoordsBias;
  149. unis->m_invViewProjMat = info.m_invViewProjectionMatrix;
  150. unis->m_camPos = info.m_cameraPosWSpace.xyz();
  151. unis->m_diffuseColor = info.m_directionalLight->m_diffuseColor;
  152. unis->m_lightDir = info.m_directionalLight->m_direction;
  153. unis->m_lightMatrix = info.m_directionalLight->m_textureMatrices[0];
  154. unis->m_near = info.m_cameraNear;
  155. unis->m_far = info.m_cameraFar;
  156. if(info.m_directionalLight->m_shadowCascadeCount > 0)
  157. {
  158. unis->m_effectiveShadowDistance =
  159. info.m_directionalLight->m_shadowRenderQueues[0]->m_effectiveShadowDistance;
  160. }
  161. else
  162. {
  163. unis->m_effectiveShadowDistance = 0.0f;
  164. }
  165. drawQuad(cmdb);
  166. }
  167. // Set other light state
  168. cmdb->setCullMode(FaceSelectionBit::kFront);
  169. // Do point lights
  170. U32 indexCount;
  171. bindVertexIndexBuffers(m_plightMesh, cmdb, indexCount);
  172. cmdb->bindShaderProgram(m_plightGrProg[info.m_computeSpecular]);
  173. for(const PointLightQueueElement& plightEl : info.m_pointLights)
  174. {
  175. // Update uniforms
  176. DeferredVertexUniforms* vert =
  177. allocateAndBindUniforms<DeferredVertexUniforms*>(sizeof(DeferredVertexUniforms), cmdb, 0, 0);
  178. Mat4 modelM(plightEl.m_worldPosition.xyz1(), Mat3::getIdentity(), plightEl.m_radius);
  179. vert->m_mvp = info.m_viewProjectionMatrix * modelM;
  180. DeferredPointLightUniforms* light =
  181. allocateAndBindUniforms<DeferredPointLightUniforms*>(sizeof(DeferredPointLightUniforms), cmdb, 0, 1);
  182. light->m_inputTexUvScale = info.m_gbufferTexCoordsScale;
  183. light->m_inputTexUvBias = info.m_gbufferTexCoordsBias;
  184. light->m_fbUvScale = info.m_lightbufferTexCoordsScale;
  185. light->m_fbUvBias = info.m_lightbufferTexCoordsBias;
  186. light->m_invViewProjMat = info.m_invViewProjectionMatrix;
  187. light->m_camPos = info.m_cameraPosWSpace.xyz();
  188. light->m_position = plightEl.m_worldPosition;
  189. light->m_oneOverSquareRadius = 1.0f / (plightEl.m_radius * plightEl.m_radius);
  190. light->m_diffuseColor = plightEl.m_diffuseColor;
  191. // Draw
  192. cmdb->drawElements(PrimitiveTopology::kTriangles, indexCount);
  193. }
  194. // Do spot lights
  195. bindVertexIndexBuffers(m_slightMesh, cmdb, indexCount);
  196. cmdb->bindShaderProgram(m_slightGrProg[info.m_computeSpecular]);
  197. for(const SpotLightQueueElement& splightEl : info.m_spotLights)
  198. {
  199. // Compute the model matrix
  200. //
  201. Mat4 modelM(splightEl.m_worldTransform.getTranslationPart().xyz1(),
  202. splightEl.m_worldTransform.getRotationPart(), 1.0f);
  203. // Calc the scale of the cone
  204. Mat4 scaleM(Mat4::getIdentity());
  205. scaleM(0, 0) = tan(splightEl.m_outerAngle / 2.0f) * splightEl.m_distance;
  206. scaleM(1, 1) = scaleM(0, 0);
  207. scaleM(2, 2) = splightEl.m_distance;
  208. modelM = modelM * scaleM;
  209. // Update vertex uniforms
  210. DeferredVertexUniforms* vert =
  211. allocateAndBindUniforms<DeferredVertexUniforms*>(sizeof(DeferredVertexUniforms), cmdb, 0, 0);
  212. vert->m_mvp = info.m_viewProjectionMatrix * modelM;
  213. // Update fragment uniforms
  214. DeferredSpotLightUniforms* light =
  215. allocateAndBindUniforms<DeferredSpotLightUniforms*>(sizeof(DeferredSpotLightUniforms), cmdb, 0, 1);
  216. light->m_inputTexUvScale = info.m_gbufferTexCoordsScale;
  217. light->m_inputTexUvBias = info.m_gbufferTexCoordsBias;
  218. light->m_fbUvScale = info.m_lightbufferTexCoordsScale;
  219. light->m_fbUvBias = info.m_lightbufferTexCoordsBias;
  220. light->m_invViewProjMat = info.m_invViewProjectionMatrix;
  221. light->m_camPos = info.m_cameraPosWSpace.xyz();
  222. light->m_position = splightEl.m_worldTransform.getTranslationPart().xyz();
  223. light->m_oneOverSquareRadius = 1.0f / (splightEl.m_distance * splightEl.m_distance);
  224. light->m_diffuseColor = splightEl.m_diffuseColor;
  225. light->m_outerCos = cos(splightEl.m_outerAngle / 2.0f);
  226. light->m_lightDir = -splightEl.m_worldTransform.getZAxis().xyz();
  227. light->m_innerCos = cos(splightEl.m_innerAngle / 2.0f);
  228. // Draw
  229. cmdb->drawElements(PrimitiveTopology::kTriangles, indexCount);
  230. }
  231. // Restore state
  232. cmdb->setBlendFactors(0, BlendFactor::kOne, BlendFactor::kZero);
  233. cmdb->setCullMode(FaceSelectionBit::kBack);
  234. }
  235. } // end namespace anki