2
// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3
// Use of this source code is governed by a BSD-style license that can be
4
// found in the LICENSE file.
7
// Buffer.cpp: Implements the gl::Buffer class, representing storage of vertex and/or
8
// index data. Implements GL buffer objects and related functionality.
9
// [OpenGL ES 2.0.24] section 2.9 page 21.
11
#include "libGLESv2/Buffer.h"
13
#include "libGLESv2/main.h"
14
#include "libGLESv2/VertexDataManager.h"
15
#include "libGLESv2/IndexDataManager.h"
20
Buffer::Buffer(GLuint id) : RefCountObject(id)
24
mUsage = GL_DYNAMIC_DRAW;
26
mStaticVertexBuffer = NULL;
27
mStaticIndexBuffer = NULL;
28
mUnmodifiedDataUse = 0;
34
delete mStaticVertexBuffer;
35
delete mStaticIndexBuffer;
38
void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage)
45
else if (size != mSize)
48
mContents = new GLubyte[size];
49
memset(mContents, 0, size);
52
if (data != NULL && size > 0)
54
memcpy(mContents, data, size);
60
invalidateStaticData();
62
if (usage == GL_STATIC_DRAW)
64
mStaticVertexBuffer = new StaticVertexBuffer(getDevice());
65
mStaticIndexBuffer = new StaticIndexBuffer(getDevice());
69
void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset)
71
memcpy(mContents + offset, data, size);
73
if ((mStaticVertexBuffer && mStaticVertexBuffer->size() != 0) || (mStaticIndexBuffer && mStaticIndexBuffer->size() != 0))
75
invalidateStaticData();
78
mUnmodifiedDataUse = 0;
81
StaticVertexBuffer *Buffer::getStaticVertexBuffer()
83
return mStaticVertexBuffer;
86
StaticIndexBuffer *Buffer::getStaticIndexBuffer()
88
return mStaticIndexBuffer;
91
void Buffer::invalidateStaticData()
93
delete mStaticVertexBuffer;
94
mStaticVertexBuffer = NULL;
96
delete mStaticIndexBuffer;
97
mStaticIndexBuffer = NULL;
99
mUnmodifiedDataUse = 0;
102
// Creates static buffers if sufficient used data has been left unmodified
103
void Buffer::promoteStaticUsage(int dataSize)
105
if (!mStaticVertexBuffer && !mStaticIndexBuffer)
107
mUnmodifiedDataUse += dataSize;
109
if (mUnmodifiedDataUse > 3 * mSize)
111
mStaticVertexBuffer = new StaticVertexBuffer(getDevice());
112
mStaticIndexBuffer = new StaticIndexBuffer(getDevice());