GlobalIlluminationProbeComponent.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (C) 2009-present, 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::kAllSrv | TextureUsageBit::kUavCompute;
  46. m_volTex = GrManager::getSingleton().newTexture(texInit);
  47. m_volTexBindlessIdx = m_volTex->getOrCreateBindlessTextureIndex(TextureSubresourceDesc::all());
  48. // Zero the texture
  49. const ShaderProgramResourceVariant* variant;
  50. ShaderProgramResourceVariantInitInfo variantInit(m_clearTextureProg);
  51. variantInit.addMutation("TEXTURE_DIMENSIONS", 3);
  52. variantInit.addMutation("COMPONENT_TYPE", 0);
  53. m_clearTextureProg->getOrCreateVariant(variantInit, variant);
  54. CommandBufferInitInfo cmdbInit("ClearGIVol");
  55. cmdbInit.m_flags = CommandBufferFlag::kSmallBatch | CommandBufferFlag::kGeneralWork;
  56. CommandBufferPtr cmdb = GrManager::getSingleton().newCommandBuffer(cmdbInit);
  57. TextureBarrierInfo texBarrier;
  58. texBarrier.m_previousUsage = TextureUsageBit::kNone;
  59. texBarrier.m_nextUsage = TextureUsageBit::kUavCompute;
  60. texBarrier.m_textureView = TextureView(m_volTex.get(), TextureSubresourceDesc::all());
  61. cmdb->setPipelineBarrier({&texBarrier, 1}, {}, {});
  62. cmdb->bindShaderProgram(&variant->getProgram());
  63. cmdb->bindUav(0, 0, TextureView(m_volTex.get(), TextureSubresourceDesc::all()));
  64. const Vec4 clearColor(0.0f);
  65. cmdb->setFastConstants(&clearColor, sizeof(clearColor));
  66. UVec3 wgSize;
  67. wgSize.x() = (8 - 1 + m_volTex->getWidth()) / 8;
  68. wgSize.y() = (8 - 1 + m_volTex->getHeight()) / 8;
  69. wgSize.z() = (8 - 1 + m_volTex->getDepth()) / 8;
  70. cmdb->dispatchCompute(wgSize.x(), wgSize.y(), wgSize.z());
  71. texBarrier.m_previousUsage = TextureUsageBit::kUavCompute;
  72. texBarrier.m_nextUsage = TextureUsageBit::kAllSrv; // Put something random, the renderer will start from kNone
  73. cmdb->setPipelineBarrier({&texBarrier, 1}, {}, {});
  74. cmdb->endRecording();
  75. GrManager::getSingleton().submit(cmdb.get());
  76. }
  77. // Any update
  78. if(updated) [[unlikely]]
  79. {
  80. m_worldPos = info.m_node->getWorldTransform().getOrigin().xyz();
  81. // Change the UUID
  82. if(m_cellsRefreshedCount == 0)
  83. {
  84. // Refresh starts over, get a new UUID
  85. m_uuid = SceneGraph::getSingleton().getNewUuid();
  86. }
  87. else if(m_cellsRefreshedCount < m_totalCellCount)
  88. {
  89. // In the middle of the refresh process
  90. ANKI_ASSERT(m_uuid != 0);
  91. }
  92. else
  93. {
  94. // Refresh it done
  95. m_uuid = 0;
  96. }
  97. // Upload to the GPU scene
  98. GpuSceneGlobalIlluminationProbe gpuProbe = {};
  99. const Aabb aabb(-m_halfSize + m_worldPos, m_halfSize + m_worldPos);
  100. gpuProbe.m_aabbMin = aabb.getMin().xyz();
  101. gpuProbe.m_aabbMax = aabb.getMax().xyz();
  102. gpuProbe.m_volumeTexture = m_volTexBindlessIdx;
  103. gpuProbe.m_halfTexelSizeU = 1.0f / (F32(m_cellCounts.y()) * 6.0f) / 2.0f;
  104. gpuProbe.m_fadeDistance = m_fadeDistance;
  105. gpuProbe.m_uuid = m_uuid;
  106. gpuProbe.m_componentArrayIndex = getArrayIndex();
  107. m_gpuSceneProbe.uploadToGpuScene(gpuProbe);
  108. }
  109. m_shapeDirty = false;
  110. m_refreshDirty = false;
  111. return Error::kNone;
  112. }
  113. F32 GlobalIlluminationProbeComponent::getRenderRadius() const
  114. {
  115. F32 effectiveDistance = max(m_halfSize.x(), m_halfSize.y());
  116. effectiveDistance = max(effectiveDistance, m_halfSize.z());
  117. effectiveDistance = max(effectiveDistance, g_probeEffectiveDistanceCVar.get());
  118. return effectiveDistance;
  119. }
  120. F32 GlobalIlluminationProbeComponent::getShadowsRenderRadius() const
  121. {
  122. return min(getRenderRadius(), g_probeShadowEffectiveDistanceCVar.get());
  123. }
  124. } // end namespace anki