3
0

BufferAssetCreator.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/Buffer/BufferAssetCreator.h>
  9. #include <AzCore/Asset/AssetManager.h>
  10. namespace AZ
  11. {
  12. namespace RPI
  13. {
  14. void BufferAssetCreator::Begin(const Data::AssetId& assetId)
  15. {
  16. BeginCommon(assetId);
  17. }
  18. void BufferAssetCreator::SetBuffer(const void* initialData, const size_t initialDataSize, const RHI::BufferDescriptor& descriptor)
  19. {
  20. if (!ValidateIsReady())
  21. {
  22. return;
  23. }
  24. // buffer is allowed to be nullptr as a BufferAsset may describe a Read/Write buffer
  25. if (descriptor.m_byteCount == 0)
  26. {
  27. ReportError("Size of the buffer in the descriptor was 0");
  28. return;
  29. }
  30. if (initialDataSize > descriptor.m_byteCount)
  31. {
  32. ReportError("initialSize is larger than the total size in the descriptor.");
  33. return;
  34. }
  35. if (initialData == nullptr && initialDataSize > 0)
  36. {
  37. ReportError("Initial buffer data was not provided but the initial size was non-zero.");
  38. return;
  39. }
  40. if (initialData != nullptr && initialDataSize == 0)
  41. {
  42. ReportError("Initial buffer data was not null but the initial size was zero.");
  43. return;
  44. }
  45. BufferAsset* bufferAsset = m_asset.Get();
  46. // Only allocate buffer if initial data is not empty
  47. if (initialData != nullptr && initialDataSize > 0)
  48. {
  49. bufferAsset->m_buffer.resize_no_construct(descriptor.m_byteCount);
  50. memcpy(bufferAsset->m_buffer.data(), initialData, initialDataSize);
  51. }
  52. bufferAsset->m_bufferDescriptor = descriptor;
  53. }
  54. void BufferAssetCreator::SetBufferViewDescriptor(const RHI::BufferViewDescriptor& viewDescriptor)
  55. {
  56. if (!ValidateIsReady())
  57. {
  58. return;
  59. }
  60. if (viewDescriptor.m_elementCount == 0)
  61. {
  62. ReportError("BufferAssetCreator::SetBufferViewDescriptor was given a view descriptor with an element count of 0.");
  63. return;
  64. }
  65. if (viewDescriptor.m_elementSize == 0)
  66. {
  67. ReportError("BufferAssetCreator::SetBufferViewDescriptor was given a view descriptor with an element size of 0.");
  68. return;
  69. }
  70. BufferAsset* bufferAsset = m_asset.Get();
  71. bufferAsset->m_bufferViewDescriptor = viewDescriptor;
  72. }
  73. void BufferAssetCreator::SetPoolAsset(const Data::Asset<ResourcePoolAsset>& poolAsset)
  74. {
  75. if (ValidateIsReady())
  76. {
  77. m_asset->m_poolAsset = poolAsset;
  78. }
  79. }
  80. void BufferAssetCreator::SetUseCommonPool(CommonBufferPoolType poolType)
  81. {
  82. if (ValidateIsReady())
  83. {
  84. m_asset->m_poolType = poolType;
  85. }
  86. }
  87. bool BufferAssetCreator::End(Data::Asset<BufferAsset>& result)
  88. {
  89. if (!ValidateIsReady() || !ValidateBuffer())
  90. {
  91. return false;
  92. }
  93. m_asset->SetReady();
  94. return EndCommon(result);
  95. }
  96. bool BufferAssetCreator::ValidateBuffer()
  97. {
  98. RPI::BufferAsset* bufferAsset = m_asset.Get();
  99. if (!bufferAsset->m_poolAsset.GetId().IsValid() && bufferAsset->m_poolType == CommonBufferPoolType::Invalid)
  100. {
  101. ReportError("BufferAssetCreator::ValidateBuffer Failed; need valid pool asset or select a valid common pool.");
  102. return false;
  103. }
  104. if (bufferAsset->m_bufferDescriptor.m_byteCount == 0)
  105. {
  106. ReportError("BufferAssetCreator::ValidateBuffer Failed; buffer descriptor has a byte count of 0.");
  107. return false;
  108. }
  109. if (bufferAsset->m_bufferViewDescriptor.m_elementCount == 0)
  110. {
  111. ReportError("BufferAssetCreator::ValidateBuffer Failed; buffer view descirptor has an element count of 0.");
  112. return false;
  113. }
  114. if (bufferAsset->m_bufferViewDescriptor.m_elementSize == 0)
  115. {
  116. ReportError("BufferAssetCreator::ValidateBuffer Failed; buffer view descriptor has an element size of 0.");
  117. return false;
  118. }
  119. return true;
  120. }
  121. void BufferAssetCreator::SetBufferName(AZStd::string_view name)
  122. {
  123. if (ValidateIsReady())
  124. {
  125. m_asset->m_name = name;
  126. }
  127. }
  128. bool BufferAssetCreator::Clone(const Data::Asset<BufferAsset>& sourceAsset, Data::Asset<BufferAsset>& clonedResult, Data::AssetId& inOutLastCreatedAssetId)
  129. {
  130. BufferAssetCreator creator;
  131. inOutLastCreatedAssetId.m_subId = inOutLastCreatedAssetId.m_subId + 1;
  132. creator.Begin(inOutLastCreatedAssetId);
  133. creator.SetBufferName(sourceAsset.GetHint());
  134. creator.SetUseCommonPool(sourceAsset->GetCommonPoolType());
  135. creator.SetPoolAsset(sourceAsset->GetPoolAsset());
  136. creator.SetBufferViewDescriptor(sourceAsset->GetBufferViewDescriptor());
  137. const AZStd::span<const uint8_t> sourceBuffer = sourceAsset->GetBuffer();
  138. creator.SetBuffer(sourceBuffer.data(), sourceBuffer.size(), sourceAsset->GetBufferDescriptor());
  139. return creator.End(clonedResult);
  140. }
  141. } // namespace RPI
  142. } // namespace AZ