shannon.woods@transgaming.com | bdf2d80 | 2013-02-28 23:16:20 +0000 | [diff] [blame] | 1 | #include "precompiled.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2 | // |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 3 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 4 | // Use of this source code is governed by a BSD-style license that can be |
| 5 | // found in the LICENSE file. |
| 6 | // |
| 7 | |
| 8 | // Buffer.cpp: Implements the gl::Buffer class, representing storage of vertex and/or |
| 9 | // index data. Implements GL buffer objects and related functionality. |
| 10 | // [OpenGL ES 2.0.24] section 2.9 page 21. |
| 11 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 12 | #include "libGLESv2/Buffer.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 13 | |
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" |
shannon.woods@transgaming.com | 486d9e9 | 2013-02-28 23:15:41 +0000 | [diff] [blame] | 17 | #include "libGLESv2/renderer/Renderer.h" |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 18 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 19 | namespace gl |
| 20 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 21 | |
daniel@transgaming.com | 70062c9 | 2012-11-28 19:32:30 +0000 | [diff] [blame] | 22 | Buffer::Buffer(rx::Renderer *renderer, GLuint id) : RefCountObject(id) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 23 | { |
daniel@transgaming.com | b9c64a8 | 2013-01-11 04:09:04 +0000 | [diff] [blame] | 24 | mRenderer = renderer; |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 25 | mUsage = GL_DYNAMIC_DRAW; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 26 | |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 27 | mBufferStorage = renderer->createBufferStorage(); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 28 | mStaticVertexBuffer = NULL; |
| 29 | mStaticIndexBuffer = NULL; |
| 30 | mUnmodifiedDataUse = 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 31 | } |
| 32 | |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 33 | Buffer::~Buffer() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 34 | { |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 35 | delete mBufferStorage; |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 36 | delete mStaticVertexBuffer; |
| 37 | delete mStaticIndexBuffer; |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 38 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 39 | |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 40 | void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage) |
| 41 | { |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 42 | mBufferStorage->clear(); |
| 43 | mBufferStorage->setData(data, size, 0); |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 44 | |
daniel@transgaming.com | aa0ccbd | 2010-04-15 20:45:05 +0000 | [diff] [blame] | 45 | mUsage = usage; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 46 | |
| 47 | invalidateStaticData(); |
| 48 | |
| 49 | if (usage == GL_STATIC_DRAW) |
| 50 | { |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 51 | mStaticVertexBuffer = new rx::StaticVertexBufferInterface(mRenderer); |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 52 | mStaticIndexBuffer = new rx::StaticIndexBufferInterface(mRenderer); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 53 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 54 | } |
| 55 | |
daniel@transgaming.com | defa1c3 | 2010-05-18 18:51:45 +0000 | [diff] [blame] | 56 | void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 57 | { |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 58 | mBufferStorage->setData(data, size, offset); |
| 59 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 60 | if ((mStaticVertexBuffer && mStaticVertexBuffer->getBufferSize() != 0) || (mStaticIndexBuffer && mStaticIndexBuffer->getBufferSize() != 0)) |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 61 | { |
| 62 | invalidateStaticData(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 63 | } |
jbauman@chromium.org | aa9c5ca | 2011-09-26 21:10:13 +0000 | [diff] [blame] | 64 | |
| 65 | mUnmodifiedDataUse = 0; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 66 | } |
| 67 | |
shannon.woods%transgaming.com@gtempaccount.com | 614c07f | 2013-04-13 03:39:32 +0000 | [diff] [blame^] | 68 | void Buffer::copyBufferSubData(Buffer* source, GLintptr sourceOffset, GLintptr destOffset, GLsizeiptr size) |
| 69 | { |
| 70 | mBufferStorage->copyData(source->mBufferStorage, size, sourceOffset, destOffset); |
| 71 | |
| 72 | if ((mStaticVertexBuffer && mStaticVertexBuffer->getBufferSize() != 0) || (mStaticIndexBuffer && mStaticIndexBuffer->getBufferSize() != 0)) |
| 73 | { |
| 74 | invalidateStaticData(); |
| 75 | } |
| 76 | |
| 77 | mUnmodifiedDataUse = 0; |
| 78 | } |
| 79 | |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 80 | rx::BufferStorage *Buffer::getStorage() const |
| 81 | { |
| 82 | return mBufferStorage; |
| 83 | } |
| 84 | |
| 85 | unsigned int Buffer::size() |
| 86 | { |
| 87 | return mBufferStorage->getSize(); |
| 88 | } |
| 89 | |
| 90 | GLenum Buffer::usage() const |
| 91 | { |
| 92 | return mUsage; |
| 93 | } |
| 94 | |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 95 | rx::StaticVertexBufferInterface *Buffer::getStaticVertexBuffer() |
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 | return mStaticVertexBuffer; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 98 | } |
| 99 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 100 | rx::StaticIndexBufferInterface *Buffer::getStaticIndexBuffer() |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 101 | { |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 102 | return mStaticIndexBuffer; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void Buffer::invalidateStaticData() |
| 106 | { |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 107 | delete mStaticVertexBuffer; |
| 108 | mStaticVertexBuffer = NULL; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 109 | |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 110 | delete mStaticIndexBuffer; |
| 111 | mStaticIndexBuffer = NULL; |
| 112 | |
| 113 | mUnmodifiedDataUse = 0; |
| 114 | } |
| 115 | |
| 116 | // Creates static buffers if sufficient used data has been left unmodified |
| 117 | void Buffer::promoteStaticUsage(int dataSize) |
| 118 | { |
| 119 | if (!mStaticVertexBuffer && !mStaticIndexBuffer) |
| 120 | { |
| 121 | mUnmodifiedDataUse += dataSize; |
| 122 | |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 123 | if (mUnmodifiedDataUse > 3 * mBufferStorage->getSize()) |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 124 | { |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 125 | mStaticVertexBuffer = new rx::StaticVertexBufferInterface(mRenderer); |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 126 | mStaticIndexBuffer = new rx::StaticIndexBufferInterface(mRenderer); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 129 | } |
| 130 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 131 | } |