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