File: PBO.cpp

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (179 lines) | stat: -rwxr-xr-x 3,287 bytes parent folder | download
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

/**
 * @brief EXT_pixel_buffer_object implementation
 * EXT_pixel_buffer_object class implementation
 */

#include <assert.h>
#include <vector>
#include "System/mmgr.h"

#include "PBO.h"

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

CONFIG(bool, UsePBO).defaultValue(true).safemodeValue(false);

/**
 * Returns if the current gpu drivers support Pixelbuffer Objects
 */
bool PBO::IsSupported()
{
	return (GLEW_EXT_pixel_buffer_object && GLEW_ARB_map_buffer_range && configHandler->GetBool("UsePBO"));
}


PBO::PBO() : pboId(0)
{
	bound = false;
	mapped = false;
	data = NULL;
	size = 0;

	if (IsSupported()) {
		glGenBuffers(1, &pboId);
		PBOused = true;
	} else {
		PBOused = false;
	}
}


PBO::~PBO()
{
	if (PBOused) {
		glDeleteBuffers(1, &pboId);
	} else {
		delete[] data;
		data = NULL;
	}
}


void PBO::Bind()
{
	assert(!bound);

	bound = true;
	if (PBOused) {
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboId);
	}
}


void PBO::Unbind(bool discard)
{
	assert(bound);

	if (PBOused) {
		if (discard) {
			if (!globalRendering->atiHacks) {
				glBufferData(GL_PIXEL_UNPACK_BUFFER, 0, 0, GL_STREAM_DRAW);
			} else {
				glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
				glDeleteBuffers(1, &pboId);
				glGenBuffers(1, &pboId);
			}
		}
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
	} else {
		if (discard) {
			delete[] data;
			data = NULL;
		}
	}

	bound = false;
	if (discard) {
		size = 0;
	}
}


void PBO::Resize(GLsizeiptr _size, GLenum usage)
{
	assert(bound);

	size = _size;
	if (PBOused) {
		glBufferData(GL_PIXEL_UNPACK_BUFFER, size, 0, usage);
	} else {
		delete[] data;
		data = NULL; // to prevent a dead-pointer in case of an out-of-memory exception on the next line
		data = new GLubyte[size];
	}
}


GLubyte* PBO::MapBuffer(GLbitfield access)
{
	assert(!mapped);

	//! we don't use glMapBuffer, because glMapBufferRange seems to be a small
	//! step faster than it due to GL_MAP_UNSYNCHRONIZED_BIT
	/*mapped = true;
	if (PBOused) {
		return (GLubyte*)glMapBuffer(GL_PIXEL_UNPACK_BUFFER, access);
	} else {
		assert(data);
		return data;
	}*/

	return MapBuffer(0, size, access);
}


GLubyte* PBO::MapBuffer(GLintptr offset, GLsizeiptr _size, GLbitfield access)
{
	assert(!mapped);

	//! 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 | GL_MAP_UNSYNCHRONIZED_BIT;
			break;
		case GL_READ_WRITE:
			access = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT;
			break;
		case GL_READ_ONLY:
			access = GL_MAP_READ_BIT | GL_MAP_UNSYNCHRONIZED_BIT;
			break;
	}

	mapped = true;
	if (PBOused) {
		return (GLubyte*)glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, offset, _size, access);
	} else {
		assert(data);
		return data+offset;
	}
}


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

	if (PBOused) {
		glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
	} else {
	}
	mapped = false;
}


const GLvoid* PBO::GetPtr(GLintptr offset)
{
	assert(bound);

	if (PBOused) {
		return (GLvoid*)((char*)NULL + (offset));
	} else {
		assert(data);
		return (GLvoid*)(data + offset);
	}
}