Use plain data arrays for Buffer storage

TRAC #12299 Signed-off-by: Andrew Lewycky Signed-off-by: Daniel Koch Author: Nicolas Capens git-svn-id: https://angleproject.googlecode.com/svn/trunk@299 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent 91fd1de6
...@@ -10,71 +10,47 @@ ...@@ -10,71 +10,47 @@
#include "libGLESv2/Buffer.h" #include "libGLESv2/Buffer.h"
#include <cstdlib>
#include <limits>
#include <utility>
#include "common/debug.h"
#include "libGLESv2/geometry/backend.h"
namespace gl namespace gl
{ {
Buffer::Buffer(BufferBackEnd *backEnd) Buffer::Buffer()
: mBackEnd(backEnd)
{ {
mContents = NULL;
mSize = 0;
mUsage = GL_DYNAMIC_DRAW;
} }
GLenum Buffer::bufferData(const void* data, GLsizeiptr size, GLenum usage) Buffer::~Buffer()
{ {
if (size < 0) return GL_INVALID_VALUE; delete[] mContents;
}
const GLubyte* newdata = static_cast<const GLubyte*>(data);
void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage)
{
if (size == 0) if (size == 0)
{ {
mContents.clear(); delete[] mContents;
mContents = NULL;
} }
else if (size != mContents.size()) else if (size != mSize)
{ {
// vector::resize only provides the basic exception guarantee, so use temporaries & swap to get the strong exception guarantee. delete[] mContents;
std::vector<GLubyte> newContents; mContents = new GLubyte[size];
memset(mContents, 0, size);
if (newdata != NULL)
{
newContents.assign(newdata, newdata + size);
}
else
{
newContents.assign(size, 0);
}
// No exceptions allowed after this point.
mContents.swap(newContents);
} }
else if (newdata != NULL)
if (data != NULL && size > 0)
{ {
mContents.assign(newdata, newdata + size); memcpy(mContents, data, size);
} }
mSize = size;
mUsage = usage; mUsage = usage;
return GL_NO_ERROR;
} }
GLenum Buffer::bufferSubData(const void* data, GLsizeiptr size, GLintptr offset) void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset)
{ {
if (size < 0 || offset < 0) return GL_INVALID_VALUE; memcpy(mContents + offset, data, size);
if (std::numeric_limits<GLsizeiptr>::max() - offset < size) return GL_INVALID_VALUE;
if (size + offset > static_cast<GLsizeiptr>(mContents.size())) return GL_INVALID_VALUE;
const GLubyte *newdata = static_cast<const GLubyte*>(data);
memcpy(&mContents[offset], newdata, size);
return GL_NO_ERROR;
} }
} }
...@@ -22,28 +22,26 @@ ...@@ -22,28 +22,26 @@
namespace gl namespace gl
{ {
class BufferBackEnd;
class TranslatedVertexBuffer;
class Buffer class Buffer
{ {
public: public:
explicit Buffer(BufferBackEnd *backEnd); Buffer();
virtual ~Buffer();
GLenum bufferData(const void *data, GLsizeiptr size, GLenum usage); void bufferData(const void *data, GLsizeiptr size, GLenum usage);
GLenum bufferSubData(const void *data, GLsizeiptr size, GLintptr offset); void bufferSubData(const void *data, GLsizeiptr size, GLintptr offset);
void *data() { return &mContents[0]; } void *data() { return mContents; }
GLsizeiptr size() const { return mContents.size(); } size_t size() const { return mSize; }
GLenum usage() const { return mUsage; } GLenum usage() const { return mUsage; }
private: private:
DISALLOW_COPY_AND_ASSIGN(Buffer); DISALLOW_COPY_AND_ASSIGN(Buffer);
std::vector<GLubyte> mContents; GLubyte *mContents;
size_t mSize;
GLenum mUsage; GLenum mUsage;
BufferBackEnd *mBackEnd;
}; };
} }
......
...@@ -929,7 +929,7 @@ void Context::bindArrayBuffer(unsigned int buffer) ...@@ -929,7 +929,7 @@ void Context::bindArrayBuffer(unsigned int buffer)
{ {
if (buffer != 0 && !getBuffer(buffer)) if (buffer != 0 && !getBuffer(buffer))
{ {
mBufferMap[buffer] = new Buffer(mBufferBackEnd); mBufferMap[buffer] = new Buffer();
} }
mState.arrayBuffer = buffer; mState.arrayBuffer = buffer;
...@@ -939,7 +939,7 @@ void Context::bindElementArrayBuffer(unsigned int buffer) ...@@ -939,7 +939,7 @@ void Context::bindElementArrayBuffer(unsigned int buffer)
{ {
if (buffer != 0 && !getBuffer(buffer)) if (buffer != 0 && !getBuffer(buffer))
{ {
mBufferMap[buffer] = new Buffer(mBufferBackEnd); mBufferMap[buffer] = new Buffer();
} }
mState.elementArrayBuffer = buffer; mState.elementArrayBuffer = buffer;
......
...@@ -506,7 +506,7 @@ void __stdcall glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, ...@@ -506,7 +506,7 @@ void __stdcall glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size,
try try
{ {
if (size < 0) if (size < 0 || offset < 0)
{ {
return error(GL_INVALID_VALUE); return error(GL_INVALID_VALUE);
} }
...@@ -539,12 +539,12 @@ void __stdcall glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, ...@@ -539,12 +539,12 @@ void __stdcall glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size,
return error(GL_INVALID_OPERATION); return error(GL_INVALID_OPERATION);
} }
GLenum err = buffer->bufferSubData(data, size, offset); if ((size_t)size + offset > buffer->size())
if (err != GL_NO_ERROR)
{ {
return error(err); return error(GL_INVALID_VALUE);
} }
buffer->bufferSubData(data, size, offset);
} }
} }
catch(std::bad_alloc&) catch(std::bad_alloc&)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment