GlobalIlluminationProbeComponent.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (C) 2009-2023, 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/Scene/Components/GlobalIlluminationProbeComponent.h>
  6. #include <AnKi/Scene/Components/MoveComponent.h>
  7. #include <AnKi/Scene/SceneNode.h>
  8. #include <AnKi/Scene/SceneGraph.h>
  9. #include <AnKi/Core/CVarSet.h>
  10. #include <AnKi/Resource/ResourceManager.h>
  11. #include <AnKi/Gr/Texture.h>
  12. #include <AnKi/Gr/CommandBuffer.h>
  13. namespace anki {
  14. GlobalIlluminationProbeComponent::GlobalIlluminationProbeComponent(SceneNode* node)
  15. : SceneComponent(node, kClassType)
  16. {
  17. m_gpuSceneProbe.allocate();
  18. const Error err = ResourceManager::getSingleton().loadResource("ShaderBinaries/ClearTextureCompute.ankiprogbin", m_clearTextureProg);
  19. if(err)
  20. {
  21. ANKI_LOGF("Failed to load shader");
  22. }
  23. }
  24. GlobalIlluminationProbeComponent::~GlobalIlluminationProbeComponent()
  25. {
  26. }
  27. Error GlobalIlluminationProbeComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  28. {
  29. const Bool moved = info.m_node->movedThisFrame();
  30. updated = moved || m_shapeDirty || m_refreshDirty;
  31. if(moved || m_shapeDirty)
  32. {
  33. m_cellsRefreshedCount = 0;
  34. }
  35. // (re-)create the volume texture
  36. if(m_shapeDirty) [[unlikely]]
  37. {
  38. TextureInitInfo texInit("GiProbe");
  39. texInit.m_format = (GrManager::getSingleton().getDeviceCapabilities().m_unalignedBbpTextureFormats) ? Format::kR16G16B16_Sfloat
  40. : Format::kR16G16B16A16_Sfloat;
  41. texInit.m_width = m_cellCounts.x() * 6;
  42. texInit.m_height = m_cellCounts.y();
  43. texInit.m_depth = m_cellCounts.z();
  44. texInit.m_type = TextureType::k3D;
  45. texInit.m_usage = TextureUsageBit::kAllSampled | TextureUsageBit::kUavComputeWrite | TextureUsageBit::kUavComputeRead;
  46. m_volTex = GrManager::getSingleton().newTexture(texInit);
  47. TextureViewInitInfo viewInit(m_volTex.get(), "GiProbe");
  48. m_volView = GrManager::getSingleton().newTextureView(viewInit);
  49. m_volTexBindlessIdx = m_volView->getOrCreateBindlessTextureIndex();
  50. // Zero the texture
  51. const ShaderProgramResourceVariant* variant;
  52. ShaderProgramResourceVariantInitInfo variantInit(m_clearTextureProg);
  53. variantInit.addMutation("TEXTURE_DIMENSIONS", 3);
  54. variantInit.addMutation("COMPONENT_TYPE", 0);
  55. m_clearTextureProg->getOrCreateVariant(variantInit, variant);
  56. CommandBufferInitInfo cmdbInit("ClearGIVol");
  57. cmdbInit.m_flags = CommandBufferFlag::kSmallBatch | CommandBufferFlag::kGeneralWork;
  58. CommandBufferPtr cmdb = GrManager::getSingleton().newCommandBuffer(cmdbInit);
  59. TextureBarrierInfo texBarrier;
  60. texBarrier.m_previousUsage = TextureUsageBit::kNone;
  61. texBarrier.m_nextUsage = TextureUsageBit::kUavComputeWrite;
  62. texBarrier.m_texture = m_volTex.get();
  63. cmdb->setPipelineBarrier({&texBarrier, 1}, {}, {});
  64. cmdb->bindShaderProgram(&variant->getProgram());
  65. cmdb->bindUavTexture(0, 0, m_volView.get());
  66. const Vec4 clearColor(0.0f);
  67. cmdb->setPushConstants(&clearColor, sizeof(clearColor));
  68. UVec3 wgSize;
  69. wgSize.x() = (8 - 1 + m_volTex->getWidth()) / 8;
  70. wgSize.y() = (8 - 1 + m_volTex->getHeight()) / 8;
  71. wgSize.z() = (8 - 1 + m_volTex->getDepth()) / 8;
  72. cmdb->dispatchCompute(wgSize.x(), wgSize.y(), wgSize.z());
  73. texBarrier.m_previousUsage = TextureUsageBit::kUavComputeWrite;
  74. texBarrier.m_nextUsage = m_volTex->getTextureUsage();
  75. cmdb->setPipelineBarrier({&texBarrier, 1}, {}, {});
  76. cmdb->flush();
  77. }
  78. // Any update
  79. if(updated) [[unlikely]]
  80. {
  81. m_worldPos = info.m_node->getWorldTransform().getOrigin().xyz();
  82. // Change the UUID
  83. if(m_cellsRefreshedCount == 0)
  84. {
  85. // Refresh starts over, get a new UUID
  86. m_uuid = SceneGraph::getSingleton().getNewUuid();
  87. }
  88. else if(m_cellsRefreshedCount < m_totalCellCount)
  89. {
  90. // In the middle of the refresh process
  91. ANKI_ASSERT(m_uuid != 0);
  92. }
  93. else
  94. {
  95. // Refresh it done
  96. m_uuid = 0;
  97. }
  98. // Upload to the GPU scene
  99. GpuSceneGlobalIlluminationProbe gpuProbe = {};
  100. const Aabb aabb(-m_halfSize + m_worldPos, m_halfSize + m_worldPos);
  101. gpuProbe.m_aabbMin = aabb.getMin().xyz();
  102. gpuProbe.m_aabbMax = aabb.getMax().xyz();
  103. gpuProbe.m_volumeTexture = m_volTexBindlessIdx;
  104. gpuProbe.m_halfTexelSizeU = 1.0f / (F32(m_cellCounts.y()) * 6.0f) / 2.0f;
  105. gpuProbe.m_fadeDistance = m_fadeDistance;
  106. gpuProbe.m_uuid = m_uuid;
  107. gpuProbe.m_componentArrayIndex = getArrayIndex();
  108. m_gpuSceneProbe.uploadToGpuScene(gpuProbe);
  109. }
  110. m_shapeDirty = false;
  111. m_refreshDirty = false;
  112. return Error::kNone;
  113. }
  114. F32 GlobalIlluminationProbeComponent::getRenderRadius() const
  115. {
  116. F32 effectiveDistance = max(m_halfSize.x(), m_halfSize.y());
  117. effectiveDistance = max(effectiveDistance, m_halfSize.z());
  118. effectiveDistance = max(effectiveDistance, g_probeEffectiveDistanceCVar.get());
  119. return effectiveDistance;
  120. }
  121. F32 GlobalIlluminationProbeComponent::getShadowsRenderRadius() const
  122. {
  123. return min(getRenderRadius(), g_probeShadowEffectiveDistanceCVar.get());
  124. }
  125. } // end namespace anki