Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 826 vs 827 for /trunk/Source Code/Platform/Video/OpenGL/Buffers/glBufferImpl.cpp

Diff revisions: vs.
  @@ -5,6 +5,8 @@
5 5 namespace Divide {
6 6 namespace {
7 7
8 + const size_t g_persistentMapSizeThreshold = 512 * 1024; //512Kb
9 +
8 10 typedef std::array<vec3<size_t>, to_const_uint(ShaderBufferLocation::COUNT)> BindConfig;
9 11 BindConfig g_currentBindConfig;
10 12
  @@ -17,7 +19,8 @@
17 19
18 20 if (crtConfig.x != static_cast<size_t>(UBOid) ||
19 21 crtConfig.y != offset ||
20 - crtConfig.z != range) {
22 + crtConfig.z != range ||
23 + true) {
21 24 crtConfig.set(static_cast<size_t>(UBOid), offset, range);
22 25 return true;
23 26 }
  @@ -26,161 +29,116 @@
26 29 }
27 30 };
28 31
29 - glBufferImpl::glBufferImpl(GLenum target)
30 - : _target(target),
32 + glBufferImpl::glBufferImpl(const BufferImplParams& params)
33 + : _target(params._target),
31 34 _handle(0),
32 - _alignedSize(0),
33 - _updateFrequency(BufferUpdateFrequency::ONCE)
35 + _alignedSize(params._dataSizeInBytes),
36 + _updateFrequency(params._frequency),
37 + _mappedBuffer(nullptr),
38 + _lockManager(MemoryManager_NEW glBufferLockManager())
34 39 {
35 - }
36 40
37 - glBufferImpl::~glBufferImpl()
38 - {
39 - assert(_handle == 0);
40 - }
41 + _usage = _target == GL_TRANSFORM_FEEDBACK
42 + ? _updateFrequency == BufferUpdateFrequency::ONCE
43 + ? GL_STATIC_COPY
44 + : _updateFrequency == BufferUpdateFrequency::OCASSIONAL
45 + ? GL_DYNAMIC_COPY
46 + : GL_STREAM_COPY
47 + : _updateFrequency == BufferUpdateFrequency::ONCE
48 + ? GL_STATIC_DRAW
49 + : _updateFrequency == BufferUpdateFrequency::OCASSIONAL
50 + ? GL_DYNAMIC_DRAW
51 + : GL_STREAM_DRAW;
41 52
42 - GLuint glBufferImpl::bufferID() const {
43 - return _handle;
44 - }
53 + bool usePersistentMapping = !Config::Profile::DISABLE_PERSISTENT_BUFFER &&
54 + _alignedSize > g_persistentMapSizeThreshold;
45 55
46 - void glBufferImpl::create(BufferUpdateFrequency frequency, size_t size, const char* name)
47 - {
48 - assert(_handle == 0 && "BufferImpl::Create error: Tried to double create current UBO");
49 - _updateFrequency = frequency;
50 - _alignedSize = size;
51 - }
52 -
53 - bool glBufferImpl::bindRange(GLuint bindIndex, size_t offset, size_t range) {
54 - assert(_handle != 0 && "BufferImpl error: Tried to bind an uninitialized UBO");
55 -
56 - bool success = false;
57 - if (setIfDifferentBindRange(_handle, bindIndex, offset, range))
58 - {
59 - glBindBufferRange(_target, bindIndex, _handle, offset, range);
60 - success = true;
61 - }
56 + if (!usePersistentMapping) {
57 + GLUtil::createAndAllocBuffer(_alignedSize, _usage, _handle, params._initialData, params._name);
58 + } else {
59 + gl::BufferStorageMask storageMask = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
60 + if (_updateFrequency != BufferUpdateFrequency::ONCE) {
61 + //storageMask |= GL_DYNAMIC_STORAGE_BIT;
62 + }
62 63
63 - return success;
64 - }
64 + gl::BufferAccessMask accessMask = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
65 65
66 - void glBufferImpl::lockRange(size_t offset, size_t range) {
67 - }
66 + _mappedBuffer = GLUtil::createAndAllocPersistentBuffer(_alignedSize, storageMask, accessMask, _handle, params._initialData, params._name);
68 67
69 - glRegularBuffer::glRegularBuffer(GLenum target)
70 - : glBufferImpl(target),
71 - _usage(GL_NONE)
72 - {
68 + assert(_mappedBuffer != nullptr && "PersistentBuffer::Create error: Can't mapped persistent buffer!");
69 + }
73 70 }
74 71
75 - glRegularBuffer::~glRegularBuffer()
72 + glBufferImpl::~glBufferImpl()
76 73 {
77 74 if (_handle > 0) {
78 - glInvalidateBufferData(_handle);
79 - GLUtil::freeBuffer(_handle, nullptr);
75 + waitRange(0, _alignedSize, false);
76 + if (_mappedBuffer == nullptr) {
77 + glInvalidateBufferData(_handle);
78 + }
79 +
80 + GLUtil::freeBuffer(_handle, _mappedBuffer);
80 81 }
81 - }
82 82
83 - void glRegularBuffer::create(BufferUpdateFrequency frequency, size_t size, const char* name)
84 - {
85 - glBufferImpl::create(frequency, size, name);
86 - _usage =
87 - _target == GL_TRANSFORM_FEEDBACK
88 - ? frequency == BufferUpdateFrequency::ONCE
89 - ? GL_STATIC_COPY
90 - : frequency == BufferUpdateFrequency::OCASSIONAL
91 - ? GL_DYNAMIC_COPY
92 - : GL_STREAM_COPY
93 - : frequency == BufferUpdateFrequency::ONCE
94 - ? GL_STATIC_DRAW
95 - : frequency == BufferUpdateFrequency::OCASSIONAL
96 - ? GL_DYNAMIC_DRAW
97 - : GL_STREAM_DRAW;
98 - GLUtil::createAndAllocBuffer(size, _usage, _handle, NULL, name);
83 + MemoryManager::DELETE(_lockManager);
99 84 }
100 85
101 - void glRegularBuffer::updateData(size_t offset, size_t range, const bufferPtr data)
102 - {
103 - if (offset == 0 && range == _alignedSize) {
104 - glInvalidateBufferData(_handle);
105 - glNamedBufferData(_handle, _alignedSize, data, _usage);
106 - } else {
107 - glInvalidateBufferSubData(_handle, offset, range);
108 - glNamedBufferSubData(_handle, offset, range, data);
109 - }
110 -
86 + void glBufferImpl::waitRange(size_t offset, size_t range, bool blockClient) {
87 + _lockManager->WaitForLockedRange(offset, range, blockClient);
111 88 }
112 89
113 - void glRegularBuffer::readData(size_t offset, size_t range, const bufferPtr data)
114 - {
115 - glGetNamedBufferSubData(_handle, offset, range, data);
90 + void glBufferImpl::lockRange(size_t offset, size_t range) {
91 + _lockManager->LockRange(offset, range);
116 92 }
117 93
118 - glPersistentBuffer::glPersistentBuffer(GLenum target)
119 - : glBufferImpl(target),
120 - _mappedBuffer(nullptr),
121 - _lockManager(MemoryManager_NEW glBufferLockManager())
122 - {
94 + GLuint glBufferImpl::bufferID() const {
95 + return _handle;
123 96 }
124 97
125 - glPersistentBuffer::~glPersistentBuffer()
126 - {
127 - if (_handle > 0) {
128 - waitRange(0, _alignedSize, false);
129 - GLUtil::freeBuffer(_handle, _mappedBuffer);
130 - }
131 - MemoryManager::DELETE(_lockManager);
132 - }
98 + bool glBufferImpl::bindRange(GLuint bindIndex, size_t offset, size_t range) {
99 + assert(_handle != 0 && "BufferImpl error: Tried to bind an uninitialized UBO");
133 100
134 - void glPersistentBuffer::create(BufferUpdateFrequency frequency, size_t size, const char* name)
135 - {
136 - glBufferImpl::create(frequency, size, name);
137 -
138 - gl::BufferStorageMask storageMask = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
139 - if (frequency != BufferUpdateFrequency::ONCE) {
140 - storageMask |= GL_DYNAMIC_STORAGE_BIT;
101 + bool success = false;
102 + if (setIfDifferentBindRange(_handle, bindIndex, offset, range))
103 + {
104 + glBindBufferRange(_target, bindIndex, _handle, offset, range);
105 + success = true;
106 + }
107 + if (_mappedBuffer) {
108 + lockRange(offset, range);
141 109 }
142 110
143 - gl::BufferAccessMask accessMask = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
144 -
145 - _mappedBuffer = GLUtil::createAndAllocPersistentBuffer(size, storageMask, accessMask, _handle, NULL, name);
146 -
147 - assert(_mappedBuffer != nullptr && "PersistentBuffer::Create error: Can't mapped persistent buffer!");
111 + return success;
148 112 }
149 113
150 - void glPersistentBuffer::updateData(size_t offset, size_t range, const bufferPtr data)
114 + void glBufferImpl::updateData(size_t offset, size_t range, const bufferPtr data)
151 115 {
152 - waitRange(offset, range, true);
153 - assert(_mappedBuffer != nullptr && "PersistentBuffer::UpdateData error: was called for an unmapped buffer!");
154 - void* dst = (U8*)_mappedBuffer + offset;
155 - if (data) {
156 - memcpy(dst, data, range);
116 + if (_mappedBuffer) {
117 + waitRange(offset, range, true);
118 + assert(_mappedBuffer != nullptr && "PersistentBuffer::UpdateData error: was called for an unmapped buffer!");
119 + if (data) {
120 + memcpy(((U8*)_mappedBuffer) + offset, data, range);
121 + } else {
122 + memset(((U8*)_mappedBuffer) + offset, 0, range);
123 + }
157 124 } else {
158 - memset(dst, 0, range);
125 + if (offset == 0 && range == _alignedSize) {
126 + glInvalidateBufferData(_handle);
127 + glNamedBufferData(_handle, _alignedSize, data, _usage);
128 + } else {
129 + glInvalidateBufferSubData(_handle, offset, range);
130 + glNamedBufferSubData(_handle, offset, range, data);
131 + }
159 132 }
160 133 }
161 134
162 - void glPersistentBuffer::readData(size_t offset, size_t range, const bufferPtr data)
135 + void glBufferImpl::readData(size_t offset, size_t range, const bufferPtr data)
163 136 {
164 - memcpy(data, ((U8*)(_mappedBuffer)+offset), range);
165 - }
166 -
167 - bool glPersistentBuffer::bindRange(GLuint bindIndex, size_t offset, size_t range)
168 - {
169 - if (range > 0) {
170 - bool ret = glBufferImpl::bindRange(bindIndex, offset, range);
171 - lockRange(offset, range);
172 - return ret;
137 + if (_mappedBuffer) {
138 + memcpy(data, ((U8*)(_mappedBuffer)+offset), range);
139 + } else {
140 + glGetNamedBufferSubData(_handle, offset, range, data);
173 141 }
174 -
175 - return false;
176 - }
177 -
178 - void glPersistentBuffer::waitRange(size_t offset, size_t range, bool blockClient) {
179 - _lockManager->WaitForLockedRange(offset, range, blockClient);
180 - }
181 -
182 - void glPersistentBuffer::lockRange(size_t offset, size_t range) {
183 - _lockManager->LockRange(offset, range);
184 142 }
185 143
186 144 }; //namespace Divide