1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
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
18 #include "Buffer.h"
19
20 #include "main.h"
21 #include "VertexDataManager.h"
22 #include "IndexDataManager.h"
23
24 namespace gl
25 {
26
Buffer(GLuint name)27 Buffer::Buffer(GLuint name) : NamedObject(name)
28 {
29 mContents = 0;
30 mSize = 0;
31 mUsage = GL_DYNAMIC_DRAW;
32 }
33
~Buffer()34 Buffer::~Buffer()
35 {
36 if(mContents)
37 {
38 mContents->destruct();
39 }
40 }
41
bufferData(const void * data,GLsizeiptr size,GLenum usage)42 void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage)
43 {
44 if(mContents)
45 {
46 mContents->destruct();
47 mContents = 0;
48 }
49
50 mSize = size;
51 mUsage = usage;
52
53 if(size > 0)
54 {
55 const int padding = 1024; // For SIMD processing of vertices
56 mContents = new sw::Resource(size + padding);
57
58 if(!mContents)
59 {
60 return error(GL_OUT_OF_MEMORY);
61 }
62
63 if(data)
64 {
65 memcpy((void*)mContents->data(), data, size);
66 }
67 }
68 }
69
bufferSubData(const void * data,GLsizeiptr size,GLintptr offset)70 void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset)
71 {
72 if(mContents)
73 {
74 char *buffer = (char*)mContents->lock(sw::PUBLIC);
75 memcpy(buffer + offset, data, size);
76 mContents->unlock();
77 }
78 }
79
getResource()80 sw::Resource *Buffer::getResource()
81 {
82 return mContents;
83 }
84
85 }
86