BsGpuResourceData.cpp 3.3 KB

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