CmGpuResourceData.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "CmGpuResourceData.h"
  2. #include "CmGpuResourceDataRTTI.h"
  3. #include "CmRenderSystem.h"
  4. #include "CmException.h"
  5. namespace CamelotFramework
  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. UINT8* GpuResourceData::getData() const
  22. {
  23. #if !CM_FORCE_SINGLETHREADED_RENDERING
  24. if(mLocked)
  25. {
  26. if(CM_THREAD_CURRENT_ID != RenderSystem::instance().getRenderThreadId())
  27. CM_EXCEPT(InternalErrorException, "You are not allowed to access buffer data from non-render thread when the buffer is locked.");
  28. }
  29. #endif
  30. return mData;
  31. }
  32. void GpuResourceData::allocateInternalBuffer()
  33. {
  34. allocateInternalBuffer(getInternalBufferSize());
  35. }
  36. void GpuResourceData::allocateInternalBuffer(UINT32 size)
  37. {
  38. #if !CM_FORCE_SINGLETHREADED_RENDERING
  39. if(mLocked)
  40. {
  41. if(CM_THREAD_CURRENT_ID != RenderSystem::instance().getRenderThreadId())
  42. CM_EXCEPT(InternalErrorException, "You are not allowed to access buffer data from non-render thread when the buffer is locked.");
  43. }
  44. #endif
  45. freeInternalBuffer();
  46. mData = CM_NEW_BYTES(size, ScratchAlloc);
  47. mOwnsData = true;
  48. }
  49. void GpuResourceData::freeInternalBuffer()
  50. {
  51. if(mData == nullptr || !mOwnsData)
  52. return;
  53. #if !CM_FORCE_SINGLETHREADED_RENDERING
  54. if(mLocked)
  55. {
  56. if(CM_THREAD_CURRENT_ID != RenderSystem::instance().getRenderThreadId())
  57. CM_EXCEPT(InternalErrorException, "You are not allowed to access buffer data from non-render thread when the buffer is locked.");
  58. }
  59. #endif
  60. CM_DELETE_BYTES(mData, ScratchAlloc);
  61. mData = nullptr;
  62. }
  63. void GpuResourceData::setExternalBuffer(UINT8* data)
  64. {
  65. #if !CM_FORCE_SINGLETHREADED_RENDERING
  66. if(mLocked)
  67. {
  68. if(CM_THREAD_CURRENT_ID != RenderSystem::instance().getRenderThreadId())
  69. CM_EXCEPT(InternalErrorException, "You are not allowed to access buffer data from non-render thread when the buffer is locked.");
  70. }
  71. #endif
  72. freeInternalBuffer();
  73. mData = data;
  74. mOwnsData = false;
  75. }
  76. void GpuResourceData::lock() const
  77. {
  78. mLocked = true;
  79. }
  80. void GpuResourceData::unlock() const
  81. {
  82. mLocked = false;
  83. }
  84. /************************************************************************/
  85. /* SERIALIZATION */
  86. /************************************************************************/
  87. RTTITypeBase* GpuResourceData::getRTTIStatic()
  88. {
  89. return GpuResourceDataRTTI::instance();
  90. }
  91. RTTITypeBase* GpuResourceData::getRTTI() const
  92. {
  93. return GpuResourceData::getRTTIStatic();
  94. }
  95. }