2
0

CmGpuResourceData.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "CmGpuResourceData.h"
  2. #include "CmRenderSystem.h"
  3. #include "CmException.h"
  4. namespace CamelotFramework
  5. {
  6. GpuResourceData::GpuResourceData()
  7. :mData(nullptr), mLocked(false), mOwnsData(false)
  8. {
  9. }
  10. GpuResourceData::GpuResourceData(const GpuResourceData& copy)
  11. {
  12. mData = copy.mData;
  13. mLocked = copy.mLocked; // TODO - This should be shared by all copies pointing to the same data?
  14. mOwnsData = false;
  15. }
  16. GpuResourceData::~GpuResourceData()
  17. {
  18. freeInternalBuffer();
  19. }
  20. UINT8* GpuResourceData::getData() const
  21. {
  22. #if !CM_FORCE_SINGLETHREADED_RENDERING
  23. if(mLocked)
  24. {
  25. if(CM_THREAD_CURRENT_ID != RenderSystem::instance().getRenderThreadId())
  26. CM_EXCEPT(InternalErrorException, "You are not allowed to access buffer data from non-render thread when the buffer is locked.");
  27. }
  28. #endif
  29. return mData;
  30. }
  31. void GpuResourceData::allocateInternalBuffer(UINT32 size)
  32. {
  33. #if !CM_FORCE_SINGLETHREADED_RENDERING
  34. if(mLocked)
  35. {
  36. if(CM_THREAD_CURRENT_ID != RenderSystem::instance().getRenderThreadId())
  37. CM_EXCEPT(InternalErrorException, "You are not allowed to access buffer data from non-render thread when the buffer is locked.");
  38. }
  39. #endif
  40. freeInternalBuffer();
  41. mData = CM_NEW_BYTES(size, ScratchAlloc);
  42. mOwnsData = true;
  43. }
  44. void GpuResourceData::freeInternalBuffer()
  45. {
  46. if(mData == nullptr || !mOwnsData)
  47. return;
  48. #if !CM_FORCE_SINGLETHREADED_RENDERING
  49. if(mLocked)
  50. {
  51. if(CM_THREAD_CURRENT_ID != RenderSystem::instance().getRenderThreadId())
  52. CM_EXCEPT(InternalErrorException, "You are not allowed to access buffer data from non-render thread when the buffer is locked.");
  53. }
  54. #endif
  55. CM_DELETE_BYTES(mData, ScratchAlloc);
  56. mData = nullptr;
  57. }
  58. void GpuResourceData::setExternalBuffer(UINT8* data)
  59. {
  60. #if !CM_FORCE_SINGLETHREADED_RENDERING
  61. if(mLocked)
  62. {
  63. if(CM_THREAD_CURRENT_ID != RenderSystem::instance().getRenderThreadId())
  64. CM_EXCEPT(InternalErrorException, "You are not allowed to access buffer data from non-render thread when the buffer is locked.");
  65. }
  66. #endif
  67. freeInternalBuffer();
  68. mData = data;
  69. mOwnsData = false;
  70. }
  71. void GpuResourceData::lock() const
  72. {
  73. mLocked = true;
  74. }
  75. void GpuResourceData::unlock() const
  76. {
  77. mLocked = false;
  78. }
  79. }