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.h: Defines 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 #ifndef LIBGLESV2_BUFFER_H_
20 #define LIBGLESV2_BUFFER_H_
21 
22 #include "common/Object.hpp"
23 #include "Common/Resource.hpp"
24 
25 #include <GLES2/gl2.h>
26 
27 #include <cstddef>
28 #include <vector>
29 
30 namespace es2
31 {
32 class Buffer : public gl::NamedObject
33 {
34 public:
35 	explicit Buffer(GLuint name);
36 
37 	virtual ~Buffer();
38 
39 	void bufferData(const void *data, GLsizeiptr size, GLenum usage);
40 	void bufferSubData(const void *data, GLsizeiptr size, GLintptr offset);
41 
data()42 	const void *data() const { return mContents ? mContents->data() : 0; }
size()43 	size_t size() const { return mSize; }
usage()44 	GLenum usage() const { return mUsage; }
isMapped()45 	bool isMapped() const { return mIsMapped; }
offset()46 	GLintptr offset() const { return mOffset; }
length()47 	GLsizeiptr length() const { return mLength; }
access()48 	GLbitfield access() const { return mAccess; }
49 
50 	void* mapRange(GLintptr offset, GLsizeiptr length, GLbitfield access);
51 	bool unmap();
flushMappedRange(GLintptr offset,GLsizeiptr length)52 	void flushMappedRange(GLintptr offset, GLsizeiptr length) {}
53 
54 	sw::Resource *getResource();
55 
56 private:
57 	sw::Resource *mContents;
58 	size_t mSize;
59 	GLenum mUsage;
60 	bool mIsMapped;
61 	GLintptr mOffset;
62 	GLsizeiptr mLength;
63 	GLbitfield mAccess;
64 };
65 
66 class BufferBinding
67 {
68 public:
BufferBinding()69 	BufferBinding() : offset(0), size(0) { }
70 
71 	void set(Buffer *newBuffer, int newOffset = 0, int newSize = 0)
72 	{
73 		buffer = newBuffer;
74 		offset = newOffset;
75 		size = newSize;
76 	}
77 
getOffset()78 	int getOffset() const { return offset; }
getSize()79 	int getSize() const { return size; }
get()80 	const gl::BindingPointer<Buffer>& get() const { return buffer; }
81 
82 private:
83 	gl::BindingPointer<Buffer> buffer;
84 	int offset;
85 	int size;
86 };
87 
88 }
89 
90 #endif   // LIBGLESV2_BUFFER_H_
91