daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 14 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 15 | namespace gl |
| 16 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 17 | |
daniel@transgaming.com | 70062c9 | 2012-11-28 19:32:30 +0000 | [diff] [blame] | 18 | Buffer::Buffer(rx::Renderer *renderer, GLuint id) : RefCountObject(id) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 19 | { |
daniel@transgaming.com | b9c64a8 | 2013-01-11 04:09:04 +0000 | [diff] [blame] | 20 | mRenderer = renderer; |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 21 | mUsage = GL_DYNAMIC_DRAW; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 22 | |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 23 | mBufferStorage = renderer->createBufferStorage(); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 24 | mStaticVertexBuffer = NULL; |
| 25 | mStaticIndexBuffer = NULL; |
| 26 | mUnmodifiedDataUse = 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 27 | } |
| 28 | |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 29 | Buffer::~Buffer() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 30 | { |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 31 | delete mBufferStorage; |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 32 | delete mStaticVertexBuffer; |
| 33 | delete mStaticIndexBuffer; |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 34 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 35 | |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 36 | void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage) |
| 37 | { |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 38 | mBufferStorage->clear(); |
| 39 | mBufferStorage->setData(data, size, 0); |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 40 | |
daniel@transgaming.com | aa0ccbd | 2010-04-15 20:45:05 +0000 | [diff] [blame] | 41 | mUsage = usage; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 42 | |
| 43 | invalidateStaticData(); |
| 44 | |
| 45 | if (usage == GL_STATIC_DRAW) |
| 46 | { |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 47 | mStaticVertexBuffer = new rx::StaticVertexBufferInterface(mRenderer); |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 48 | mStaticIndexBuffer = new rx::StaticIndexBufferInterface(mRenderer); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 49 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 50 | } |
| 51 | |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 52 | void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 53 | { |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 54 | mBufferStorage->setData(data, size, offset); |
| 55 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 56 | if ((mStaticVertexBuffer && mStaticVertexBuffer->getBufferSize() != 0) || (mStaticIndexBuffer && mStaticIndexBuffer->getBufferSize() != 0)) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 57 | { |
| 58 | invalidateStaticData(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 59 | } |
jbauman@chromium.org | aa9c5ca | 2011-09-26 21:10:13 +0000 | [diff] [blame] | 60 | |
| 61 | mUnmodifiedDataUse = 0; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 62 | } |
| 63 | |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 64 | rx::BufferStorage *Buffer::getStorage() const |
| 65 | { |
| 66 | return mBufferStorage; |
| 67 | } |
| 68 | |
| 69 | unsigned int Buffer::size() |
| 70 | { |
| 71 | return mBufferStorage->getSize(); |
| 72 | } |
| 73 | |
| 74 | GLenum Buffer::usage() const |
| 75 | { |
| 76 | return mUsage; |
| 77 | } |
| 78 | |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 79 | rx::StaticVertexBufferInterface *Buffer::getStaticVertexBuffer() |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 80 | { |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 81 | return mStaticVertexBuffer; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 82 | } |
| 83 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 84 | rx::StaticIndexBufferInterface *Buffer::getStaticIndexBuffer() |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 85 | { |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 86 | return mStaticIndexBuffer; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void Buffer::invalidateStaticData() |
| 90 | { |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 91 | delete mStaticVertexBuffer; |
| 92 | mStaticVertexBuffer = NULL; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 93 | |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 94 | delete mStaticIndexBuffer; |
| 95 | mStaticIndexBuffer = NULL; |
| 96 | |
| 97 | mUnmodifiedDataUse = 0; |
| 98 | } |
| 99 | |
| 100 | // Creates static buffers if sufficient used data has been left unmodified |
| 101 | void Buffer::promoteStaticUsage(int dataSize) |
| 102 | { |
| 103 | if (!mStaticVertexBuffer && !mStaticIndexBuffer) |
| 104 | { |
| 105 | mUnmodifiedDataUse += dataSize; |
| 106 | |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 107 | if (mUnmodifiedDataUse > 3 * mBufferStorage->getSize()) |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 108 | { |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 109 | mStaticVertexBuffer = new rx::StaticVertexBufferInterface(mRenderer); |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 110 | mStaticIndexBuffer = new rx::StaticIndexBufferInterface(mRenderer); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 113 | } |
| 114 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 115 | } |