File: VBO.cpp

package info (click to toggle)
spring 105.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,860 kB
  • sloc: cpp: 467,785; ansic: 302,607; python: 12,925; java: 12,201; awk: 5,889; sh: 2,371; xml: 655; perl: 405; php: 276; objc: 194; makefile: 75; sed: 2
file content (256 lines) | stat: -rw-r--r-- 6,312 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

/**
 * @brief ARB_vertex_buffer_object implementation
 * ARB_vertex_buffer_object class implementation
 */

#include <cassert>
#include <vector>

#include "VBO.h"

#include "Rendering/GlobalRendering.h"
#include "System/Config/ConfigHandler.h"
#include "System/Log/ILog.h"

VBO::VBO(GLenum _defTarget, const bool storage, bool readable)
{
	curBoundTarget = _defTarget;
	defTarget = _defTarget;

	immutableStorage = storage;
	readableStorage = readable;
}

VBO& VBO::operator=(VBO&& other) noexcept
{
	std::swap(vboId, other.vboId);
	std::swap(bound, other.bound);
	std::swap(mapped, other.mapped);
	std::swap(nullSizeMapped, other.nullSizeMapped);

	std::swap(bufSize, other.bufSize);
	std::swap(memSize, other.memSize);

	std::swap(curBoundTarget, other.curBoundTarget);
	std::swap(defTarget, other.defTarget);
	std::swap(usage, other.usage);
	std::swap(mapUnsyncedBit, other.mapUnsyncedBit);

	std::swap(immutableStorage, other.immutableStorage);
	std::swap(readableStorage, other.readableStorage);
	return *this;
}


void VBO::Generate() const { glGenBuffers(1, &vboId); }
void VBO::Delete() const { glDeleteBuffers(1, &vboId); vboId = 0; }

void VBO::Bind(GLenum target) const
{
	assert(!bound);
	bound = true;

	glBindBuffer(curBoundTarget = target, GetId());
}

void VBO::Unbind() const
{
	assert(bound);
	bound = false;

	glBindBuffer(curBoundTarget, 0);
}


bool VBO::Resize(GLsizeiptr newSize, GLenum newUsage)
{
	assert(bound);
	assert(!mapped);

	// no size change -> nothing to do
	if (newSize == bufSize && newUsage == usage)
		return true;

	// first call: no *BO exists yet to copy old data from, so use ::New() (faster)
	if (bufSize == 0)
		return New(newSize, newUsage, nullptr);

	const size_t oldSize = bufSize;
	const GLenum oldBoundTarget = curBoundTarget;

	glClearErrors("VBO", __func__, globalRendering->glDebugErrors);

	{
		VBO vbo(GL_COPY_WRITE_BUFFER, immutableStorage);

		vbo.Bind(GL_COPY_WRITE_BUFFER);
		vbo.New(newSize, GL_STREAM_DRAW);

		// gpu internal copy (fast)
		if (oldSize > 0)
			glCopyBufferSubData(curBoundTarget, GL_COPY_WRITE_BUFFER, 0, 0, oldSize);

		vbo.Unbind();
		Unbind();
		*this = std::move(vbo);
		Bind(oldBoundTarget);
	}

	const GLenum err = glGetError();

	if (err == GL_NO_ERROR) {
		bufSize = newSize;
		usage = newUsage;
		return true;
	}

	LOG_L(L_ERROR, "[VBO::%s(size=%lu,usage=%u)] id=%u tgt=0x%x err=0x%x", __func__, (unsigned long) bufSize, usage, vboId, curBoundTarget, err);
	Unbind();
	return false;
}


bool VBO::New(GLsizeiptr newSize, GLenum newUsage, const void* newData)
{
	assert(bound);
	assert(!mapped || (newData == nullptr && newSize == bufSize && newUsage == usage));

	// ATI interprets unsynchronized access differently; (un)mapping does not sync
	mapUnsyncedBit = GL_MAP_UNSYNCHRONIZED_BIT * (1 - globalRendering->haveATI);

	// no-op new, allows e.g. repeated Bind+New with persistent buffers
	if (newData == nullptr && newSize == bufSize && newUsage == usage)
		return true;

	if (immutableStorage && bufSize != 0) {
		LOG_L(L_ERROR, "[VBO::%s({cur,new}size={" _STPF_ "," _STPF_ "},{cur,new}usage={0x%x,0x%x},data=%p)] cannot recreate persistent storage buffer", __func__, bufSize, newSize, usage, newUsage, newData);
		return false;
	}


	glClearErrors("VBO", __func__, globalRendering->glDebugErrors);

	#ifdef GLEW_ARB_buffer_storage
	if (immutableStorage) {
		glBufferStorage(curBoundTarget, newSize, newData, /*newUsage =*/ (GL_MAP_READ_BIT * readableStorage) | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_DYNAMIC_STORAGE_BIT);
	} else
	#endif
	{
		glBufferData(curBoundTarget, newSize, newData, newUsage);
	}

	const GLenum err = glGetError();

	if (err == GL_NO_ERROR) {
		bufSize = newSize;
		usage = newUsage;
		return true;
	}

	LOG_L(L_ERROR, "[VBO::%s(size=%lu,usage=0x%x)] id=%u tgt=0x%x err=0x%x", __func__, (unsigned long) bufSize, usage, vboId, curBoundTarget, err);
	Unbind();
	return false;
}


GLubyte* VBO::MapBuffer(GLbitfield access)
{
	assert(!mapped);
	return MapBuffer(0, bufSize, access);
}


GLubyte* VBO::MapBuffer(GLintptr offset, GLsizeiptr size, GLbitfield access)
{
	assert(!mapped);
	assert((offset + size) <= bufSize);
	mapped = true;

	// glMapBuffer & glMapBufferRange use different flags for their access argument
	// for easier handling convert the glMapBuffer ones here
	switch (access) {
		case GL_WRITE_ONLY: {
			access = GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | mapUnsyncedBit;

			#ifdef GLEW_ARB_buffer_storage
			access &= ~((GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT) * immutableStorage);
			access |=  ((GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT            ) * immutableStorage);
			#endif
		} break;
		case GL_READ_WRITE: {
			access = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | mapUnsyncedBit;

			#ifdef GLEW_ARB_buffer_storage
			access &= ~(GL_MAP_UNSYNCHRONIZED_BIT * immutableStorage);
			access |=  (GL_MAP_PERSISTENT_BIT     * immutableStorage);
			#endif
		} break;
		case GL_READ_ONLY: {
			access = GL_MAP_READ_BIT | mapUnsyncedBit;

			#ifdef GLEW_ARB_buffer_storage
			access &= ~(GL_MAP_UNSYNCHRONIZED_BIT * immutableStorage);
			access |=  (GL_MAP_PERSISTENT_BIT     * immutableStorage);
			#endif
		} break;

		default: break;
	}

	if (size == 0) {
		// nvidia incorrectly returns GL_INVALID_VALUE when trying to call glMapBufferRange with size zero
		// so catch it ourselves
		nullSizeMapped = true;
		return nullptr;
	}


	GLubyte* ptr = (GLubyte*) glMapBufferRange(curBoundTarget, offset, size, access);
	#ifndef HEADLESS
	assert(ptr != nullptr);
	#else
	assert(ptr == nullptr);
	#endif

	return ptr;
}


void VBO::UnmapBuffer()
{
	assert(mapped);

	if (!nullSizeMapped)
		glUnmapBuffer(curBoundTarget);

	mapped = false;
	nullSizeMapped = false;
}


void VBO::Invalidate()
{
	assert(bound);
	assert(immutableStorage || !mapped);

#ifdef GLEW_ARB_invalidate_subdata
	// OpenGL4 way
	if (GLEW_ARB_invalidate_subdata) {
		glInvalidateBufferData(GetId());
		return;
	}
#endif

	// note: allocating memory doesn't actually block the memory it just makes room in _virtual_ memory space
	New(bufSize, usage, nullptr);
}


const GLvoid* VBO::GetPtr(GLintptr offset) const
{
	assert(bound);
	return (GLvoid*)((char*)nullptr + (offset));
}