BsGpuResourceData.cpp 2.9 KB

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