ImageMipChainAssetCreator.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/Image/ImageMipChainAssetCreator.h>
  9. #include <AzCore/Asset/AssetManager.h>
  10. namespace AZ
  11. {
  12. namespace RPI
  13. {
  14. bool ImageMipChainAssetCreator::IsBuildingMip() const
  15. {
  16. return m_mipLevelsPending != m_mipLevelsCompleted;
  17. }
  18. bool ImageMipChainAssetCreator::ValidateIsBuildingMip()
  19. {
  20. if (!ValidateIsReady())
  21. {
  22. return false;
  23. }
  24. if (!IsBuildingMip())
  25. {
  26. AZ_Assert(false, "BeginMip() was not called");
  27. return false;
  28. }
  29. return true;
  30. }
  31. void ImageMipChainAssetCreator::Begin(const Data::AssetId& assetId, uint16_t mipLevels, uint16_t arraySize)
  32. {
  33. BeginCommon(assetId);
  34. m_mipLevelsPending = 0;
  35. m_mipLevelsCompleted = 0;
  36. m_subImageOffset = 0;
  37. if (ValidateIsReady())
  38. {
  39. // An extra value is added at the end to avoid branching in GetSubImageData()
  40. m_asset->m_subImageDataOffsets.resize(mipLevels * arraySize + 1);
  41. m_asset->m_mipLevels = mipLevels;
  42. m_asset->m_arraySize = arraySize;
  43. }
  44. }
  45. void ImageMipChainAssetCreator::BeginMip(const RHI::DeviceImageSubresourceLayout& layout)
  46. {
  47. if (!ValidateIsReady())
  48. {
  49. return;
  50. }
  51. if (IsBuildingMip())
  52. {
  53. AZ_Assert(false, "Already building a mip. You must call EndMip() first.");
  54. return;
  55. }
  56. if (m_mipLevelsCompleted == m_asset->m_mipLevels)
  57. {
  58. ReportError("Reached the maximum number of declared mip levels.");
  59. return;
  60. }
  61. m_asset->m_mipToSubImageOffset[m_mipLevelsPending] = m_subImageOffset;
  62. m_asset->m_subImageLayouts[m_mipLevelsPending] = layout;
  63. ++m_mipLevelsPending;
  64. }
  65. void ImageMipChainAssetCreator::AddSubImage(const void* data, size_t dataSize)
  66. {
  67. if (!ValidateIsBuildingMip())
  68. {
  69. return;
  70. }
  71. if (!data || !dataSize)
  72. {
  73. ReportError("You must supply a valid data payload.");
  74. return;
  75. }
  76. if (m_arraySlicesCompleted == m_asset->m_arraySize)
  77. {
  78. ReportError("Exceeded the %d array slices declared in Begin().", m_asset->m_arraySize);
  79. return;
  80. }
  81. // Resize the payload and copy in the new layout.
  82. auto& flatData = m_asset->m_imageData;
  83. const size_t dataOffset = flatData.size();
  84. flatData.resize(dataOffset + dataSize);
  85. memcpy(&flatData[dataOffset], data, dataSize);
  86. m_asset->m_subImageDataOffsets[m_subImageOffset] = dataOffset;
  87. ++m_arraySlicesCompleted;
  88. ++m_subImageOffset;
  89. }
  90. void ImageMipChainAssetCreator::EndMip()
  91. {
  92. if (!ValidateIsBuildingMip())
  93. {
  94. return;
  95. }
  96. if (m_arraySlicesCompleted != m_asset->m_arraySize)
  97. {
  98. ReportError("Expected %d sub-images in mip, but got %d.", m_asset->m_arraySize, m_arraySlicesCompleted);
  99. return;
  100. }
  101. ++m_mipLevelsCompleted;
  102. m_arraySlicesCompleted = 0;
  103. }
  104. bool ImageMipChainAssetCreator::End(Data::Asset<ImageMipChainAsset>& result)
  105. {
  106. if (!ValidateIsReady())
  107. {
  108. return false;
  109. }
  110. if (IsBuildingMip())
  111. {
  112. ReportError("You must call EndMip() before calling End().");
  113. return false;
  114. }
  115. if (m_mipLevelsCompleted != m_asset->m_mipLevels)
  116. {
  117. ReportError("The number of completed mip levels (%d) does not match the number of declared mip levels (%d).",
  118. m_mipLevelsCompleted, m_asset->m_mipLevels);
  119. return false;
  120. }
  121. // Assign the last offset (the sentinel value) to be the size of the full image data.
  122. m_asset->m_subImageDataOffsets.back() = m_asset->m_imageData.size();
  123. m_asset->Init();
  124. m_asset->SetReady();
  125. return EndCommon(result);
  126. }
  127. }
  128. }