daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
| 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. |
| 5 | // |
| 6 | |
| 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. |
| 10 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 11 | #include "libGLESv2/Buffer.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 12 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 13 | #include "libGLESv2/main.h" |
daniel@transgaming.com | 8fd34bd | 2011-02-18 02:52:14 +0000 | [diff] [blame] | 14 | #include "libGLESv2/VertexDataManager.h" |
| 15 | #include "libGLESv2/IndexDataManager.h" |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 16 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 17 | namespace gl |
| 18 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 19 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 20 | Buffer::Buffer(GLuint id) : RefCountObject(id) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 21 | { |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 22 | mContents = NULL; |
| 23 | mSize = 0; |
| 24 | mUsage = GL_DYNAMIC_DRAW; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 25 | |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 26 | mStaticVertexBuffer = NULL; |
| 27 | mStaticIndexBuffer = NULL; |
| 28 | mUnmodifiedDataUse = 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 29 | } |
| 30 | |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 31 | Buffer::~Buffer() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 32 | { |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 33 | delete[] mContents; |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 34 | delete mStaticVertexBuffer; |
| 35 | delete mStaticIndexBuffer; |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 36 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 37 | |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 38 | void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage) |
| 39 | { |
daniel@transgaming.com | c103b60 | 2010-04-23 18:34:58 +0000 | [diff] [blame] | 40 | if (size == 0) |
| 41 | { |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 42 | delete[] mContents; |
| 43 | mContents = NULL; |
daniel@transgaming.com | c103b60 | 2010-04-23 18:34:58 +0000 | [diff] [blame] | 44 | } |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 45 | else if (size != mSize) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 46 | { |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 47 | delete[] mContents; |
| 48 | mContents = new GLubyte[size]; |
| 49 | memset(mContents, 0, size); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 50 | } |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 51 | |
| 52 | if (data != NULL && size > 0) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 53 | { |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 54 | memcpy(mContents, data, size); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 55 | } |
| 56 | |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 57 | mSize = size; |
daniel@transgaming.com | aa0ccbd | 2010-04-15 20:45:05 +0000 | [diff] [blame] | 58 | mUsage = usage; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 59 | |
| 60 | invalidateStaticData(); |
| 61 | |
| 62 | if (usage == GL_STATIC_DRAW) |
| 63 | { |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 64 | mStaticVertexBuffer = new StaticVertexBuffer(getDevice()); |
| 65 | mStaticIndexBuffer = new StaticIndexBuffer(getDevice()); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 66 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 67 | } |
| 68 | |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 69 | void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 70 | { |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 71 | memcpy(mContents + offset, data, size); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 72 | |
| 73 | if ((mStaticVertexBuffer && mStaticVertexBuffer->size() != 0) || (mStaticIndexBuffer && mStaticIndexBuffer->size() != 0)) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 74 | { |
| 75 | invalidateStaticData(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 76 | } |
jbauman@chromium.org | aa9c5ca | 2011-09-26 21:10:13 +0000 | [diff] [blame^] | 77 | |
| 78 | mUnmodifiedDataUse = 0; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 79 | } |
| 80 | |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 81 | StaticVertexBuffer *Buffer::getStaticVertexBuffer() |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 82 | { |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 83 | return mStaticVertexBuffer; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 84 | } |
| 85 | |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 86 | StaticIndexBuffer *Buffer::getStaticIndexBuffer() |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 87 | { |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 88 | return mStaticIndexBuffer; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void Buffer::invalidateStaticData() |
| 92 | { |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 93 | delete mStaticVertexBuffer; |
| 94 | mStaticVertexBuffer = NULL; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 95 | |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 96 | delete mStaticIndexBuffer; |
| 97 | mStaticIndexBuffer = NULL; |
| 98 | |
| 99 | mUnmodifiedDataUse = 0; |
| 100 | } |
| 101 | |
| 102 | // Creates static buffers if sufficient used data has been left unmodified |
| 103 | void Buffer::promoteStaticUsage(int dataSize) |
| 104 | { |
| 105 | if (!mStaticVertexBuffer && !mStaticIndexBuffer) |
| 106 | { |
| 107 | mUnmodifiedDataUse += dataSize; |
| 108 | |
| 109 | if (mUnmodifiedDataUse > 3 * mSize) |
| 110 | { |
| 111 | mStaticVertexBuffer = new StaticVertexBuffer(getDevice()); |
| 112 | mStaticIndexBuffer = new StaticIndexBuffer(getDevice()); |
| 113 | } |
| 114 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 115 | } |
| 116 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 117 | } |