Merge pull request #109 from aras-p/glvbo-fixes

CpuGLVertexBuffer performance fix + glViewer fixes

reviewed, approved & much appreciated : thank you

takahito + manuelk
This commit is contained in:
Manuel Kraemer 2013-02-06 18:27:48 -08:00
commit 6d4565e6b8
3 changed files with 31 additions and 59 deletions

View File

@ -975,21 +975,23 @@ EffectDrawRegistry::_CreateDrawConfig(
ConfigType * config = BaseRegistry::_CreateDrawConfig(desc.first, sconfig);
assert(config);
GLuint uboIndex;
// XXXdyu can use layout(binding=) with GLSL 4.20 and beyond
g_transformBinding = 0;
glUniformBlockBinding(config->program,
glGetUniformBlockIndex(config->program, "Transform"),
g_transformBinding);
uboIndex = glGetUniformBlockIndex(config->program, "Transform");
if (uboIndex != GL_INVALID_INDEX)
glUniformBlockBinding(config->program, uboIndex, g_transformBinding);
g_tessellationBinding = 1;
glUniformBlockBinding(config->program,
glGetUniformBlockIndex(config->program, "Tessellation"),
g_tessellationBinding);
uboIndex = glGetUniformBlockIndex(config->program, "Tessellation");
if (uboIndex != GL_INVALID_INDEX)
glUniformBlockBinding(config->program, uboIndex, g_tessellationBinding);
g_lightingBinding = 2;
glUniformBlockBinding(config->program,
glGetUniformBlockIndex(config->program, "Lighting"),
g_lightingBinding);
uboIndex = glGetUniformBlockIndex(config->program, "Lighting");
if (uboIndex != GL_INVALID_INDEX)
glUniformBlockBinding(config->program, uboIndex, g_lightingBinding);
GLint loc;
#if not defined(GL_ARB_separate_shader_objects) || defined(GL_VERSION_4_1)
@ -1835,9 +1837,7 @@ int main(int argc, char ** argv)
initGL();
linkDefaultProgram();
#ifdef WIN32
wglSwapIntervalEXT(0);
#endif
glfwSwapInterval(0);
initHUD();
callbackModel(g_currentShape);

View File

@ -80,13 +80,12 @@ namespace OPENSUBDIV_VERSION {
OsdCpuGLVertexBuffer::OsdCpuGLVertexBuffer(int numElements, int numVertices)
: _numElements(numElements), _numVertices(numVertices),
_vbo(0), _cpuBuffer(0) {
_vbo(0), _cpuBuffer(0), _dataDirty(true) {
}
OsdCpuGLVertexBuffer::~OsdCpuGLVertexBuffer() {
if (_cpuBuffer)
delete[] _cpuBuffer;
delete[] _cpuBuffer;
glDeleteBuffers(1, &_vbo);
}
@ -102,8 +101,8 @@ OsdCpuGLVertexBuffer::Create(int numElements, int numVertices) {
void
OsdCpuGLVertexBuffer::UpdateData(const float *src, int numVertices) {
map();
memcpy(_cpuBuffer, src, GetNumElements() * numVertices * sizeof(float));
_dataDirty = true;
}
int
@ -120,21 +119,32 @@ OsdCpuGLVertexBuffer::GetNumVertices() const {
float*
OsdCpuGLVertexBuffer::BindCpuBuffer() {
map();
_dataDirty = true; // caller might modify data
return _cpuBuffer;
}
GLuint
OsdCpuGLVertexBuffer::BindVBO() {
if (not _dataDirty)
return _vbo;
unmap();
int size = GetNumElements() * GetNumVertices() * sizeof(float);
GLint prev = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &prev);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, size, _cpuBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, prev);
_dataDirty = false;
return _vbo;
}
bool
OsdCpuGLVertexBuffer::allocate() {
int size = _numElements * _numVertices * sizeof(float);
_cpuBuffer = new float[GetNumElements() * GetNumVertices()];
_dataDirty = true;
int size = GetNumElements() * GetNumVertices() * sizeof(float);
GLint prev = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &prev);
@ -147,39 +157,6 @@ OsdCpuGLVertexBuffer::allocate() {
return false;
}
void
OsdCpuGLVertexBuffer::map() {
if (_cpuBuffer) return;
int size = _numElements * _numVertices;
GLint prev = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &prev);
_cpuBuffer = new float[size];
#if defined(GL_VERSION_2_1)
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glGetBufferSubData(GL_ARRAY_BUFFER, 0, size * sizeof(float), _cpuBuffer);
glBindBuffer(GL_ARRAY_BUFFER, prev);
#endif
}
void
OsdCpuGLVertexBuffer::unmap() {
if (_cpuBuffer == NULL) return;
int size = _numElements * _numVertices * sizeof(float);
GLint prev = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &prev);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, size, _cpuBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, prev);
delete[] _cpuBuffer;
_cpuBuffer = NULL;
}
} // end namespace OPENSUBDIV_VERSION
} // end namespace OpenSubdiv

View File

@ -117,17 +117,12 @@ protected:
/// Allocates VBO for this buffer. Returns true if success.
bool allocate();
/// Acquires cpu memory buffer from GL.
void map();
/// Releases cpu memory and copy them back to GL.
void unmap();
private:
int _numElements;
int _numVertices;
GLuint _vbo;
float *_cpuBuffer;
bool _dataDirty;
};
} // end namespace OPENSUBDIV_VERSION