Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 2 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 6 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 8 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 14 | |
| 15 | // Buffer.cpp: Implements the Buffer class, representing storage of vertex and/or |
| 16 | // index data. Implements GL buffer objects and related functionality. |
| 17 | // [OpenGL ES 2.0.24] section 2.9 page 21. |
| 18 | |
| 19 | #include "Buffer.h" |
| 20 | |
| 21 | #include "main.h" |
| 22 | #include "VertexDataManager.h" |
| 23 | #include "IndexDataManager.h" |
| 24 | |
Nicolas Capens | 14ee762 | 2014-10-28 23:48:41 -0400 | [diff] [blame] | 25 | namespace es1 |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 26 | { |
| 27 | |
Nicolas Capens | e826ef0 | 2015-04-02 14:43:13 -0400 | [diff] [blame] | 28 | Buffer::Buffer(GLuint name) : NamedObject(name) |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 29 | { |
| 30 | mContents = 0; |
| 31 | mSize = 0; |
| 32 | mUsage = GL_DYNAMIC_DRAW; |
| 33 | } |
| 34 | |
| 35 | Buffer::~Buffer() |
| 36 | { |
| 37 | if(mContents) |
| 38 | { |
| 39 | mContents->destruct(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage) |
| 44 | { |
| 45 | if(mContents) |
| 46 | { |
| 47 | mContents->destruct(); |
| 48 | mContents = 0; |
| 49 | } |
| 50 | |
| 51 | mSize = size; |
| 52 | mUsage = usage; |
| 53 | |
| 54 | if(size > 0) |
| 55 | { |
| 56 | const int padding = 1024; // For SIMD processing of vertices |
| 57 | mContents = new sw::Resource(size + padding); |
| 58 | |
| 59 | if(!mContents) |
| 60 | { |
| 61 | return error(GL_OUT_OF_MEMORY); |
| 62 | } |
| 63 | |
| 64 | if(data) |
| 65 | { |
Nicolas Capens | 53fae3e | 2014-12-03 11:09:40 -0500 | [diff] [blame] | 66 | memcpy((void*)mContents->data(), data, size); |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset) |
| 72 | { |
| 73 | if(mContents) |
| 74 | { |
| 75 | char *buffer = (char*)mContents->lock(sw::PUBLIC); |
| 76 | memcpy(buffer + offset, data, size); |
| 77 | mContents->unlock(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | sw::Resource *Buffer::getResource() |
| 82 | { |
| 83 | return mContents; |
| 84 | } |
| 85 | |
| 86 | } |