BsGpuResourceData.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Resources/BsGpuResourceData.h"
  4. #include "RTTI/BsGpuResourceDataRTTI.h"
  5. #include "CoreThread/BsCoreThread.h"
  6. #include "Error/BsException.h"
  7. namespace bs
  8. {
  9. GpuResourceData::GpuResourceData()
  10. :mData(nullptr), mOwnsData(false), mLocked(false)
  11. {
  12. }
  13. GpuResourceData::GpuResourceData(const GpuResourceData& copy)
  14. {
  15. mData = copy.mData;
  16. mLocked = copy.mLocked; // TODO - This should be shared by all copies pointing to the same data?
  17. mOwnsData = false;
  18. }
  19. GpuResourceData::~GpuResourceData()
  20. {
  21. freeInternalBuffer();
  22. }
  23. GpuResourceData& GpuResourceData::operator=(const GpuResourceData& rhs)
  24. {
  25. mData = rhs.mData;
  26. mLocked = rhs.mLocked; // TODO - This should be shared by all copies pointing to the same data?
  27. mOwnsData = false;
  28. return *this;
  29. }
  30. UINT8* GpuResourceData::getData() const
  31. {
  32. #if !BS_FORCE_SINGLETHREADED_RENDERING
  33. if(mLocked)
  34. {
  35. if(BS_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId())
  36. BS_EXCEPT(InternalErrorException, "You are not allowed to access buffer data from non-core thread when the buffer is locked.");
  37. }
  38. #endif
  39. return mData;
  40. }
  41. void GpuResourceData::allocateInternalBuffer()
  42. {
  43. allocateInternalBuffer(getInternalBufferSize());
  44. }
  45. void GpuResourceData::allocateInternalBuffer(UINT32 size)
  46. {
  47. #if !BS_FORCE_SINGLETHREADED_RENDERING
  48. if(mLocked)
  49. {
  50. if(BS_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId())
  51. BS_EXCEPT(InternalErrorException, "You are not allowed to access buffer data from non-core thread when the buffer is locked.");
  52. }
  53. #endif
  54. freeInternalBuffer();
  55. mData = (UINT8*)bs_alloc(size);
  56. mOwnsData = true;
  57. }
  58. void GpuResourceData::freeInternalBuffer()
  59. {
  60. if(mData == nullptr || !mOwnsData)
  61. return;
  62. #if !BS_FORCE_SINGLETHREADED_RENDERING
  63. if(mLocked)
  64. {
  65. if(BS_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId())
  66. BS_EXCEPT(InternalErrorException, "You are not allowed to access buffer data from non-core thread when the buffer is locked.");
  67. }
  68. #endif
  69. bs_free(mData);
  70. mData = nullptr;
  71. }
  72. void GpuResourceData::setExternalBuffer(UINT8* data)
  73. {
  74. #if !BS_FORCE_SINGLETHREADED_RENDERING
  75. if(mLocked)
  76. {
  77. if(BS_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId())
  78. BS_EXCEPT(InternalErrorException, "You are not allowed to access buffer data from non-core thread when the buffer is locked.");
  79. }
  80. #endif
  81. freeInternalBuffer();
  82. mData = data;
  83. mOwnsData = false;
  84. }
  85. void GpuResourceData::_lock() const
  86. {
  87. mLocked = true;
  88. }
  89. void GpuResourceData::_unlock() const
  90. {
  91. mLocked = false;
  92. }
  93. /************************************************************************/
  94. /* SERIALIZATION */
  95. /************************************************************************/
  96. RTTITypeBase* GpuResourceData::getRTTIStatic()
  97. {
  98. return GpuResourceDataRTTI::instance();
  99. }
  100. RTTITypeBase* GpuResourceData::getRTTI() const
  101. {
  102. return GpuResourceData::getRTTIStatic();
  103. }
  104. }