DeviceBufferPoolBase.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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/RHI/DeviceBufferPoolBase.h>
  9. #include <Atom/RHI/RHIUtils.h>
  10. namespace AZ::RHI
  11. {
  12. ResultCode DeviceBufferPoolBase::InitBuffer(DeviceBuffer* buffer, const BufferDescriptor& descriptor, PlatformMethod platformInitResourceMethod)
  13. {
  14. // The descriptor is assigned regardless of whether initialization succeeds. Descriptors are considered
  15. // undefined for uninitialized resources. This makes the buffer descriptor well defined at initialization
  16. // time rather than leftover data from the previous usage.
  17. buffer->SetDescriptor(descriptor);
  18. return DeviceResourcePool::InitResource(buffer, platformInitResourceMethod);
  19. }
  20. void DeviceBufferPoolBase::ValidateBufferMap(DeviceBuffer& buffer, bool isDataValid)
  21. {
  22. if (Validation::IsEnabled())
  23. {
  24. // No need for validation for a null rhi
  25. if (IsNullRHI())
  26. {
  27. return;
  28. }
  29. if (!isDataValid)
  30. {
  31. AZ_Error("DeviceBufferPoolBase", false, "Failed to map buffer '%s'.", buffer.GetName().GetCStr());
  32. }
  33. ++buffer.m_mapRefCount;
  34. ++m_mapRefCount;
  35. }
  36. }
  37. bool DeviceBufferPoolBase::ValidateBufferUnmap(DeviceBuffer& buffer)
  38. {
  39. if (Validation::IsEnabled())
  40. {
  41. // No need for validation for a null rhi
  42. if (IsNullRHI())
  43. {
  44. return true;
  45. }
  46. if (--buffer.m_mapRefCount == -1)
  47. {
  48. AZ_Error("DeviceBufferPoolBase", false, "DeviceBuffer '%s' was unmapped more times than it was mapped.", buffer.GetName().GetCStr());
  49. // Undo the ref-count to keep the validation state sane.
  50. ++buffer.m_mapRefCount;
  51. return false;
  52. }
  53. else
  54. {
  55. --m_mapRefCount;
  56. }
  57. }
  58. return true;
  59. }
  60. uint32_t DeviceBufferPoolBase::GetMapRefCount() const
  61. {
  62. return m_mapRefCount;
  63. }
  64. }