1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "Resources/BsGpuResourceData.h"
4#include "Private/RTTI/BsGpuResourceDataRTTI.h"
5#include "CoreThread/BsCoreThread.h"
6#include "Error/BsException.h"
7
8namespace bs
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
17 GpuResourceData::~GpuResourceData()
18 {
19 freeInternalBuffer();
20 }
21
22 GpuResourceData& GpuResourceData::operator=(const GpuResourceData& rhs)
23 {
24 mData = rhs.mData;
25 mLocked = rhs.mLocked; // TODO - This should be shared by all copies pointing to the same data?
26 mOwnsData = false;
27
28 return *this;
29 }
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
41 return mData;
42 }
43
44 void GpuResourceData::allocateInternalBuffer()
45 {
46 allocateInternalBuffer(getInternalBufferSize());
47 }
48
49 void GpuResourceData::allocateInternalBuffer(UINT32 size)
50 {
51#if !BS_FORCE_SINGLETHREADED_RENDERING
52 if(mLocked)
53 {
54 if(BS_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId())
55 BS_EXCEPT(InternalErrorException, "You are not allowed to access buffer data from non-core thread when the buffer is locked.");
56 }
57#endif
58
59 freeInternalBuffer();
60
61 mData = (UINT8*)bs_alloc(size);
62 mOwnsData = true;
63 }
64
65 void GpuResourceData::freeInternalBuffer()
66 {
67 if(mData == nullptr || !mOwnsData)
68 return;
69
70#if !BS_FORCE_SINGLETHREADED_RENDERING
71 if(mLocked)
72 {
73 if(BS_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId())
74 BS_EXCEPT(InternalErrorException, "You are not allowed to access buffer data from non-core thread when the buffer is locked.");
75 }
76#endif
77
78 bs_free(mData);
79 mData = nullptr;
80 }
81
82 void GpuResourceData::setExternalBuffer(UINT8* data)
83 {
84#if !BS_FORCE_SINGLETHREADED_RENDERING
85 if(mLocked)
86 {
87 if(BS_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId())
88 BS_EXCEPT(InternalErrorException, "You are not allowed to access buffer data from non-core thread when the buffer is locked.");
89 }
90#endif
91
92 freeInternalBuffer();
93
94 mData = data;
95 mOwnsData = false;
96 }
97
98 void GpuResourceData::_lock() const
99 {
100 mLocked = true;
101 }
102
103 void GpuResourceData::_unlock() const
104 {
105 mLocked = false;
106 }
107
108 /************************************************************************/
109 /* SERIALIZATION */
110 /************************************************************************/
111
112 RTTITypeBase* GpuResourceData::getRTTIStatic()
113 {
114 return GpuResourceDataRTTI::instance();
115 }
116
117 RTTITypeBase* GpuResourceData::getRTTI() const
118 {
119 return GpuResourceData::getRTTIStatic();
120 }
121}