StreamingImagePool.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Atom/RPI.Reflect/Base.h>
  9. #include <Atom/RPI.Public/Image/StreamingImage.h>
  10. #include <Atom/RPI.Public/Image/StreamingImagePool.h>
  11. #include <Atom/RPI.Public/Image/StreamingImageController.h>
  12. #include <Atom/RHI/Factory.h>
  13. #include <AtomCore/Instance/InstanceDatabase.h>
  14. namespace AZ
  15. {
  16. namespace RPI
  17. {
  18. Data::Instance<StreamingImagePool> StreamingImagePool::FindOrCreate(const Data::Asset<StreamingImagePoolAsset>& streamingImagePoolAsset)
  19. {
  20. return Data::InstanceDatabase<StreamingImagePool>::Instance().FindOrCreate(
  21. Data::InstanceId::CreateFromAssetId(streamingImagePoolAsset.GetId()),
  22. streamingImagePoolAsset);
  23. }
  24. Data::Instance<StreamingImagePool> StreamingImagePool::CreateInternal(RHI::Device& device, StreamingImagePoolAsset& streamingImagePoolAsset)
  25. {
  26. Data::Instance<StreamingImagePool> streamingImagePool = aznew StreamingImagePool();
  27. const RHI::ResultCode resultCode = streamingImagePool->Init(device, streamingImagePoolAsset);
  28. if (resultCode == RHI::ResultCode::Success)
  29. {
  30. return streamingImagePool;
  31. }
  32. return nullptr;
  33. }
  34. RHI::ResultCode StreamingImagePool::Init(RHI::Device& device, StreamingImagePoolAsset& poolAsset)
  35. {
  36. AZ_PROFILE_FUNCTION(RPI);
  37. if (Validation::IsEnabled())
  38. {
  39. if (m_pool)
  40. {
  41. AZ_Error("StreamingImagePool", false, "Invalid Operation: Attempted to initialize an already initialized pool.");
  42. return RHI::ResultCode::InvalidOperation;
  43. }
  44. }
  45. RHI::Ptr<RHI::StreamingImagePool> pool = RHI::Factory::Get().CreateStreamingImagePool();
  46. const RHI::ResultCode resultCode = pool->Init(device, poolAsset.GetPoolDescriptor());
  47. if (resultCode == RHI::ResultCode::Success)
  48. {
  49. m_controller = StreamingImageController::Create(*pool);
  50. m_pool = AZStd::move(pool);
  51. m_pool->SetName(AZ::Name{ poolAsset.GetPoolName() });
  52. return RHI::ResultCode::Success;
  53. }
  54. AZ_Warning("StreamingImagePoolAsset", false, "Failed to initialize RHI::StreamingImagePool.");
  55. return resultCode;
  56. }
  57. void StreamingImagePool::AttachImage(StreamingImage* image)
  58. {
  59. // Add image to controller only if the image is streamable
  60. if (image->IsStreamable())
  61. {
  62. m_controller->AttachImage(image);
  63. }
  64. }
  65. void StreamingImagePool::DetachImage(StreamingImage* image)
  66. {
  67. if (image->IsStreamable())
  68. {
  69. m_controller->DetachImage(image);
  70. }
  71. }
  72. void StreamingImagePool::Update()
  73. {
  74. m_controller->Update();
  75. }
  76. RHI::StreamingImagePool* StreamingImagePool::GetRHIPool()
  77. {
  78. return m_pool.get();
  79. }
  80. const RHI::StreamingImagePool* StreamingImagePool::GetRHIPool() const
  81. {
  82. return m_pool.get();
  83. }
  84. uint32_t StreamingImagePool::GetImageCount() const
  85. {
  86. return m_pool->GetResourceCount();
  87. }
  88. uint32_t StreamingImagePool::GetStreamableImageCount() const
  89. {
  90. return m_controller->GetStreamableImageCount();
  91. }
  92. bool StreamingImagePool::SetMemoryBudget(size_t newBudgetInBytes)
  93. {
  94. size_t currentBudget = GetMemoryBudget();
  95. bool isSet = m_pool->SetMemoryBudget(newBudgetInBytes);
  96. if (currentBudget > 0)
  97. {
  98. if (newBudgetInBytes == 0 || newBudgetInBytes > currentBudget)
  99. {
  100. if (isSet)
  101. {
  102. m_controller->ResetLowMemoryState();
  103. }
  104. }
  105. }
  106. return isSet;
  107. }
  108. size_t StreamingImagePool::GetMemoryBudget() const
  109. {
  110. return m_pool->GetHeapMemoryUsage(RHI::HeapMemoryLevel::Device).m_budgetInBytes;
  111. }
  112. bool StreamingImagePool::IsMemoryLow() const
  113. {
  114. return m_controller->IsMemoryLow();
  115. }
  116. void StreamingImagePool::SetMipBias(int16_t mipBias)
  117. {
  118. m_controller->SetMipBias(mipBias);
  119. }
  120. int16_t StreamingImagePool::GetMipBias() const
  121. {
  122. return m_controller->GetMipBias();
  123. }
  124. }
  125. }