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 | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 68 | rx::BufferStorage *Buffer::getStorage() const |
| 69 | { |
| 70 | return mBufferStorage; |
| 71 | } |
| 72 | |
| 73 | unsigned int Buffer::size() |
| 74 | { |
| 75 | return mBufferStorage->getSize(); |
| 76 | } |
| 77 | |
| 78 | GLenum Buffer::usage() const |
| 79 | { |
| 80 | return mUsage; |
| 81 | } |
| 82 | |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 83 | rx::StaticVertexBufferInterface *Buffer::getStaticVertexBuffer() |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 84 | { |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 85 | return mStaticVertexBuffer; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 86 | } |
| 87 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 88 | rx::StaticIndexBufferInterface *Buffer::getStaticIndexBuffer() |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 89 | { |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 90 | return mStaticIndexBuffer; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void Buffer::invalidateStaticData() |
| 94 | { |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 95 | delete mStaticVertexBuffer; |
| 96 | mStaticVertexBuffer = NULL; |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 97 | |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 98 | delete mStaticIndexBuffer; |
| 99 | mStaticIndexBuffer = NULL; |
| 100 | |
| 101 | mUnmodifiedDataUse = 0; |
| 102 | } |
| 103 | |
| 104 | // Creates static buffers if sufficient used data has been left unmodified |
| 105 | void Buffer::promoteStaticUsage(int dataSize) |
| 106 | { |
| 107 | if (!mStaticVertexBuffer && !mStaticIndexBuffer) |
| 108 | { |
| 109 | mUnmodifiedDataUse += dataSize; |
| 110 | |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 111 | if (mUnmodifiedDataUse > 3 * mBufferStorage->getSize()) |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 112 | { |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 113 | mStaticVertexBuffer = new rx::StaticVertexBufferInterface(mRenderer); |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 114 | mStaticIndexBuffer = new rx::StaticIndexBufferInterface(mRenderer); |
daniel@transgaming.com | 78624ca | 2011-04-22 04:17:57 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 117 | } |
| 118 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 119 | } |