GlobalIlluminationProbeComponent.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/Util/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. void GlobalIlluminationProbeComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  28. {
  29. const Bool moved = info.m_node->movedThisFrame();
  30. if(!m_dirty && !moved) [[likely]]
  31. {
  32. return;
  33. }
  34. updated = true;
  35. const Vec3 halfSize = info.m_node->getWorldTransform().getScale().xyz;
  36. UVec3 newCellCounts = UVec3(2.0f * halfSize / m_cellSize);
  37. newCellCounts = newCellCounts.max(UVec3(1));
  38. const Bool shapeDirty = m_cellCounts != newCellCounts;
  39. if(shapeDirty)
  40. {
  41. m_volTex.reset(nullptr);
  42. m_cellCounts = newCellCounts;
  43. m_totalCellCount = m_cellCounts.x * m_cellCounts.y * m_cellCounts.z;
  44. }
  45. if(moved)
  46. {
  47. m_worldPos = info.m_node->getWorldTransform().getOrigin().xyz;
  48. m_halfSize = halfSize;
  49. }
  50. if(moved || shapeDirty)
  51. {
  52. m_cellsRefreshedCount = 0;
  53. }
  54. // (re-)create the volume texture
  55. if(!m_volTex)
  56. {
  57. TextureInitInfo texInit("GiProbe");
  58. texInit.m_format = (GrManager::getSingleton().getDeviceCapabilities().m_unalignedBbpTextureFormats) ? Format::kR16G16B16_Sfloat
  59. : Format::kR16G16B16A16_Sfloat;
  60. texInit.m_width = m_cellCounts.x * 6;
  61. texInit.m_height = m_cellCounts.y;
  62. texInit.m_depth = m_cellCounts.z;
  63. texInit.m_type = TextureType::k3D;
  64. texInit.m_usage = TextureUsageBit::kAllSrv | TextureUsageBit::kUavCompute;
  65. m_volTex = GrManager::getSingleton().newTexture(texInit);
  66. m_volTexBindlessIdx = m_volTex->getOrCreateBindlessTextureIndex(TextureSubresourceDesc::all());
  67. // Zero the texture
  68. const ShaderProgramResourceVariant* variant;
  69. ShaderProgramResourceVariantInitInfo variantInit(m_clearTextureProg);
  70. variantInit.addMutation("TEXTURE_DIMENSIONS", 3);
  71. variantInit.addMutation("COMPONENT_TYPE", 0);
  72. m_clearTextureProg->getOrCreateVariant(variantInit, variant);
  73. CommandBufferInitInfo cmdbInit("ClearGIVol");
  74. cmdbInit.m_flags = CommandBufferFlag::kSmallBatch | CommandBufferFlag::kGeneralWork;
  75. CommandBufferPtr cmdb = GrManager::getSingleton().newCommandBuffer(cmdbInit);
  76. TextureBarrierInfo texBarrier;
  77. texBarrier.m_previousUsage = TextureUsageBit::kNone;
  78. texBarrier.m_nextUsage = TextureUsageBit::kUavCompute;
  79. texBarrier.m_textureView = TextureView(m_volTex.get(), TextureSubresourceDesc::all());
  80. cmdb->setPipelineBarrier({&texBarrier, 1}, {}, {});
  81. cmdb->bindShaderProgram(&variant->getProgram());
  82. cmdb->bindUav(0, 0, TextureView(m_volTex.get(), TextureSubresourceDesc::all()));
  83. const Vec4 clearColor(0.0f);
  84. cmdb->setFastConstants(&clearColor, sizeof(clearColor));
  85. UVec3 wgSize;
  86. wgSize.x = (8 - 1 + m_volTex->getWidth()) / 8;
  87. wgSize.y = (8 - 1 + m_volTex->getHeight()) / 8;
  88. wgSize.z = (8 - 1 + m_volTex->getDepth()) / 8;
  89. cmdb->dispatchCompute(wgSize.x, wgSize.y, wgSize.z);
  90. texBarrier.m_previousUsage = TextureUsageBit::kUavCompute;
  91. texBarrier.m_nextUsage = TextureUsageBit::kAllSrv; // Put something random, the renderer will start from kNone
  92. cmdb->setPipelineBarrier({&texBarrier, 1}, {}, {});
  93. cmdb->endRecording();
  94. GrManager::getSingleton().submit(cmdb.get());
  95. }
  96. // Upload to GPU scene
  97. if(moved || shapeDirty || m_dirty)
  98. {
  99. Bool cpuFeedback = false;
  100. if(m_cellsRefreshedCount == 0)
  101. {
  102. // Refresh starts over
  103. cpuFeedback = true;
  104. }
  105. else if(m_cellsRefreshedCount < m_totalCellCount)
  106. {
  107. // In the middle of the refresh process
  108. cpuFeedback = true;
  109. }
  110. else
  111. {
  112. // Refresh it done
  113. cpuFeedback = false;
  114. }
  115. // Upload to the GPU scene
  116. GpuSceneGlobalIlluminationProbe gpuProbe = {};
  117. const Aabb aabb(-m_halfSize + m_worldPos, m_halfSize + m_worldPos);
  118. gpuProbe.m_aabbMin = aabb.getMin().xyz;
  119. gpuProbe.m_aabbMax = aabb.getMax().xyz;
  120. gpuProbe.m_volumeTexture = m_volTexBindlessIdx;
  121. gpuProbe.m_halfTexelSizeU = 1.0f / (F32(m_cellCounts.y) * 6.0f) / 2.0f;
  122. gpuProbe.m_fadeDistance = m_fadeDistance;
  123. gpuProbe.m_uuid = getUuid();
  124. gpuProbe.m_componentArrayIndex = getArrayIndex();
  125. gpuProbe.m_cpuFeedback = cpuFeedback;
  126. m_gpuSceneProbe.uploadToGpuScene(gpuProbe);
  127. }
  128. m_dirty = false;
  129. }
  130. F32 GlobalIlluminationProbeComponent::getRenderRadius() const
  131. {
  132. F32 effectiveDistance = max(m_halfSize.x, m_halfSize.y);
  133. effectiveDistance = max(effectiveDistance, m_halfSize.z);
  134. effectiveDistance = max<F32>(effectiveDistance, g_cvarSceneProbeEffectiveDistance);
  135. return effectiveDistance;
  136. }
  137. F32 GlobalIlluminationProbeComponent::getShadowsRenderRadius() const
  138. {
  139. return min<F32>(getRenderRadius(), g_cvarSceneProbeShadowEffectiveDistance);
  140. }
  141. Error GlobalIlluminationProbeComponent::serialize(SceneSerializer& serializer)
  142. {
  143. ANKI_SERIALIZE(m_cellSize, 1);
  144. ANKI_SERIALIZE(m_fadeDistance, 1);
  145. ANKI_SERIALIZE(m_halfSize, 1);
  146. return Error::kNone;
  147. }
  148. } // end namespace anki