CpuGLVB: reading data back from the GPU each time model is changed is not very efficient.

Model the GL VB after D3D11 one, where there are no data read backs, however this means
an extra memory copy of the buffer. 4th level uniform subdiv on Car, glGetBufferSubData
was taking 50% of CPU time before (actual subdiv 22%), now that is gone. Full CPU Draw
62ms -> 54ms, looks like most of overhead now is just waiting on GL queries).
This commit is contained in:
Aras Pranckevicius 2013-02-03 20:42:18 +02:00
parent 687cedfb68
commit a4a8cd122d
2 changed files with 10 additions and 46 deletions

View File

@ -85,8 +85,7 @@ OsdCpuGLVertexBuffer::OsdCpuGLVertexBuffer(int numElements, int numVertices)
OsdCpuGLVertexBuffer::~OsdCpuGLVertexBuffer() {
if (_cpuBuffer)
delete[] _cpuBuffer;
delete[] _cpuBuffer;
glDeleteBuffers(1, &_vbo);
}
@ -102,8 +101,7 @@ OsdCpuGLVertexBuffer::Create(int numElements, int numVertices) {
void
OsdCpuGLVertexBuffer::UpdateData(const float *src, int numVertices) {
map();
memcpy(_cpuBuffer, src, GetNumElements() * numVertices * sizeof(float));
memcpy(_cpuBuffer, src, _numElements * numVertices * sizeof(float));
}
int
@ -120,20 +118,25 @@ OsdCpuGLVertexBuffer::GetNumVertices() const {
float*
OsdCpuGLVertexBuffer::BindCpuBuffer() {
map();
return _cpuBuffer;
}
GLuint
OsdCpuGLVertexBuffer::BindVBO() {
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);
unmap();
return _vbo;
}
bool
OsdCpuGLVertexBuffer::allocate() {
_cpuBuffer = new float[_numElements * _numVertices];
int size = _numElements * _numVertices * sizeof(float);
GLint prev = 0;
@ -147,39 +150,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,12 +117,6 @@ 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;