Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -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 |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -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 |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -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. |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -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 es2 |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 26 | { |
| 27 | |
Nicolas Capens | e826ef0 | 2015-04-02 14:43:13 -0400 | [diff] [blame] | 28 | Buffer::Buffer(GLuint name) : NamedObject(name) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 29 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 30 | mContents = 0; |
| 31 | mSize = 0; |
| 32 | mUsage = GL_STATIC_DRAW; |
Alexis Hetu | ff7b886 | 2015-04-01 13:12:10 -0400 | [diff] [blame] | 33 | mIsMapped = false; |
| 34 | mOffset = 0; |
| 35 | mLength = 0; |
| 36 | mAccess = 0; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | Buffer::~Buffer() |
| 40 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 41 | if(mContents) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 42 | { |
| 43 | mContents->destruct(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage) |
| 48 | { |
| 49 | if(mContents) |
| 50 | { |
| 51 | mContents->destruct(); |
| 52 | mContents = 0; |
| 53 | } |
| 54 | |
| 55 | mSize = size; |
| 56 | mUsage = usage; |
| 57 | |
| 58 | if(size > 0) |
| 59 | { |
| 60 | const int padding = 1024; // For SIMD processing of vertices |
| 61 | mContents = new sw::Resource(size + padding); |
| 62 | |
| 63 | if(!mContents) |
| 64 | { |
| 65 | return error(GL_OUT_OF_MEMORY); |
| 66 | } |
| 67 | |
| 68 | if(data) |
| 69 | { |
Alexis Hetu | c2632d2 | 2015-05-01 14:16:47 -0400 | [diff] [blame] | 70 | char *buffer = (char*)mContents->data(); |
| 71 | memcpy(buffer + mOffset, data, size); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset) |
| 77 | { |
Alexis Hetu | dafae03 | 2015-06-29 17:55:33 -0400 | [diff] [blame] | 78 | if(mContents && data) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 79 | { |
| 80 | char *buffer = (char*)mContents->lock(sw::PUBLIC); |
| 81 | memcpy(buffer + offset, data, size); |
| 82 | mContents->unlock(); |
| 83 | } |
| 84 | } |
| 85 | |
Alexis Hetu | ff7b886 | 2015-04-01 13:12:10 -0400 | [diff] [blame] | 86 | void* Buffer::mapRange(GLintptr offset, GLsizeiptr length, GLbitfield access) |
| 87 | { |
| 88 | if(mContents) |
| 89 | { |
| 90 | char* buffer = (char*)mContents->lock(sw::PUBLIC); |
| 91 | mIsMapped = true; |
| 92 | mOffset = offset; |
| 93 | mLength = length; |
| 94 | mAccess = access; |
| 95 | return buffer + offset; |
| 96 | } |
| 97 | return nullptr; |
| 98 | } |
| 99 | |
| 100 | bool Buffer::unmap() |
| 101 | { |
| 102 | if(mContents) |
| 103 | { |
| 104 | mContents->unlock(); |
| 105 | } |
| 106 | mIsMapped = false; |
| 107 | mOffset = 0; |
| 108 | mLength = 0; |
| 109 | mAccess = 0; |
| 110 | return true; |
| 111 | } |
| 112 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 113 | sw::Resource *Buffer::getResource() |
| 114 | { |
| 115 | return mContents; |
| 116 | } |
| 117 | |
| 118 | } |