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 | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 13 | #include <cstdlib> |
| 14 | #include <limits> |
| 15 | #include <utility> |
| 16 | |
alokp@chromium.org | ea0e1af | 2010-03-22 19:33:14 +0000 | [diff] [blame] | 17 | #include "common/debug.h" |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 18 | |
| 19 | #include "libGLESv2/geometry/backend.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 20 | |
| 21 | namespace gl |
| 22 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 23 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 24 | Buffer::Buffer(BufferBackEnd *backEnd) |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 25 | : mBackEnd(backEnd) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 26 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 27 | } |
| 28 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 29 | GLenum Buffer::bufferData(const void* data, GLsizeiptr size, GLenum usage) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 30 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 31 | if (size < 0) return GL_INVALID_VALUE; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 32 | |
daniel@transgaming.com | d4620a3 | 2010-03-21 04:31:28 +0000 | [diff] [blame] | 33 | const GLubyte* newdata = static_cast<const GLubyte*>(data); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 34 | |
daniel@transgaming.com | c103b60 | 2010-04-23 18:34:58 +0000 | [diff] [blame] | 35 | if (size == 0) |
| 36 | { |
| 37 | mContents.clear(); |
daniel@transgaming.com | c103b60 | 2010-04-23 18:34:58 +0000 | [diff] [blame] | 38 | } |
| 39 | else if (size != mContents.size()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 40 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 41 | // vector::resize only provides the basic exception guarantee, so use temporaries & swap to get the strong exception guarantee. |
daniel@transgaming.com | d4620a3 | 2010-03-21 04:31:28 +0000 | [diff] [blame] | 42 | std::vector<GLubyte> newContents; |
| 43 | |
| 44 | if (newdata != NULL) |
| 45 | { |
| 46 | newContents.assign(newdata, newdata + size); |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | newContents.assign(size, 0); |
| 51 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 52 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 53 | // No exceptions allowed after this point. |
| 54 | |
| 55 | mContents.swap(newContents); |
| 56 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 57 | } |
daniel@transgaming.com | d4620a3 | 2010-03-21 04:31:28 +0000 | [diff] [blame] | 58 | else if (newdata != NULL) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 59 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 60 | mContents.assign(newdata, newdata + size); |
| 61 | } |
| 62 | |
daniel@transgaming.com | aa0ccbd | 2010-04-15 20:45:05 +0000 | [diff] [blame] | 63 | mUsage = usage; |
| 64 | |
daniel@transgaming.com | 9a0606c | 2010-05-12 03:42:00 +0000 | [diff] [blame] | 65 | return GL_NO_ERROR; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 66 | } |
| 67 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 68 | GLenum Buffer::bufferSubData(const void* data, GLsizeiptr size, GLintptr offset) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 69 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 70 | if (size < 0 || offset < 0) return GL_INVALID_VALUE; |
| 71 | if (std::numeric_limits<GLsizeiptr>::max() - offset < size) return GL_INVALID_VALUE; |
| 72 | if (size + offset > static_cast<GLsizeiptr>(mContents.size())) return GL_INVALID_VALUE; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 73 | |
daniel@transgaming.com | d4620a3 | 2010-03-21 04:31:28 +0000 | [diff] [blame] | 74 | const GLubyte *newdata = static_cast<const GLubyte*>(data); |
daniel@transgaming.com | 2b7ebdb94 | 2010-05-14 17:31:24 +0000 | [diff] [blame] | 75 | memcpy(&mContents[offset], newdata, size); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 76 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 77 | return GL_NO_ERROR; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 78 | } |
| 79 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 80 | } |