API Documentation
Loading...
Searching...
No Matches
GLBuffer.h
Go to the documentation of this file.
1#pragma once
7#include <QOpenGLFunctions>
8namespace NDEVR
9{
10 /**--------------------------------------------------------------------------------------------------
11 \brief Buffer for storing data in the OpenGL engine, usually for rendering
12 **/
14 {
15 public:
18 , m_context(device)
19 {
20 //m_context->glGenBuffers(1, &m_buffer);
21 }
22 OpenGLBuffer(const TableColumn& reference_data, const DynamicPointer<GLESGraphicsDevice>& device)
23 : GraphicsBuffer(reference_data.label())
24 , m_context(device)
25 {
26 copyData(reference_data, true);
27 }
28 OpenGLBuffer(OpenGLBuffer&& other) noexcept
29 : GraphicsBuffer(std::move(other))
30 , m_buffer(other.m_buffer)
31 , m_context(other.m_context)
32 {
33 other.m_buffer = Constant<uint04>::Invalid;
34 }
36 {
37 cleanup();
38 }
39
40 void* mapMemory(size_t offset, size_t size) const final override
41 {
42 lib_assert(size > 0, "Bad size map");
43 UNUSED(size);
44 if (!m_is_accessable)
45 throw GraphicsException(_t("OpenGL Buffer Error"), _td("opengl_buffer_memory_cannot_map", "Memory [label] Could not be mapped.").replace("label", m_label));
46
47 lib_assert(offset + size <= m_size * type().total_size, "Bad size update");
48 return m_temp_buffer.begin(cast<uint04>(offset));
49 }
50 void unmapMemory() const final override
51 {
52 //m_context->glBindBuffer(bindLocation(), bufferID());
53 //m_context->glFlushMappedBufferRange(bindLocation(), m_mapped_start, m_mapped_size);
54 //m_context->glUnmapBuffer(bindLocation());
55 }
56 void cleanup() final override
57 {
58 if (m_is_memory_owner && !IsInvalid(m_buffer))
59 {
60 m_context->glDeleteBuffers(1, &m_buffer);
62 }
64 m_size = 0;
65 }
66 void getFromVideoCard(bool) final override
67 {
68 if (!m_is_accessable)
69 {
70 uint04 old_size = size();
71 cleanup();
72 m_allocated_size = old_size;
73 m_temp_buffer.setSize(old_size);
74 m_size = old_size;
75 m_is_accessable = true;
76 }
77 };
78 void sendToVideoCard(bool) final override
79 {
81 {
82 uint04 alloc_size = size();
83 m_buffer = createBuffer(alloc_size, alloc_size, type().total_size, GL_STATIC_DRAW, m_temp_buffer.begin());
84 m_temp_buffer.clear();
85 m_temp_buffer.ensureCapacity(0, true, true);
86 m_is_accessable = false;
87 }
88 };
89 GLuint getFormat(uint01) const
90 {
91 if (type().is_number)
92 {
93 switch (type().byte_size)
94 {
95 case 1:
96 if (type().is_unsigned) return GL_UNSIGNED_BYTE;
97 else return GL_BYTE;
98 case 2:
99 if (type().is_float) return GL_SHORT;
100 if (type().is_unsigned) return GL_UNSIGNED_SHORT;
101 else return GL_SHORT;
102 case 4:
103 if (type().is_float) return GL_FLOAT;
104 if (type().is_unsigned) return GL_UNSIGNED_INT;
105 else return GL_INT;
106 #ifdef GL_DOUBLE
107 case 8:
108 if (type().is_float) return GL_DOUBLE;
109 break;
110 #endif
111 case 16:
112 if (type().is_float) return GL_FLOAT;
113 break;
114 default:
115 break;
116 }
117 }
118 else if (type().is_color)
119 {
120 return GL_UNSIGNED_INT;
121 }
122 lib_assert(false, "unknown vulkanbuffer format");
123 return GL_INT;
124 };
126 {
127 if (type().is_color)
128 return 4;
129 switch (type().byte_size)
130 {
131 case 1: return 1;
132 case 2: return 2;
133 case 4: return 4;
134 case 8: return 8;
135 case 16: return 4;
136 default:
137 lib_assert(false, "unknown vulkanbuffer format");
138 break;
139 }
140 return 1;
141 };
142 GLint vectorSize() const
143 {
144 if (type().is_color)
145 return 1;
146 if (type().byte_size == 16)
147 return 4;//we are a 4x4 matrix
148 return cast<GLint>(getMax<uint02>(1U, type().vector_size));
149 };
150 GLuint getFormat() const
151 {
152 return getFormat(0);
153 }
154 void insertRows(uint04, uint04) final override
155 {
156 lib_assert(false, "Not yet implemented");
157 };
158 void setSize(uint04 buffer_size) final override
159 {
160 lib_assert(m_is_accessable, "Should only be called when accessable");
161 m_temp_buffer.setSize(buffer_size * type().total_size);
162 m_size = buffer_size;
163 m_allocated_size = buffer_size;
164 }
165 void updateRegion(const TableColumn& reference_data, uint04 offset, uint04 size) final override
166 {
167 lib_assert(offset == 0, "Bad offset?");
168 void* data = mapMemory(cast<size_t>(offset) * type().total_size, size * type().total_size);
169 if (reference_data.type() == type())
170 {
171 memcpy((uint01*)data, (uint01*)reference_data.begin() + (cast<size_t>(offset) * type().total_size), size * type().total_size);
172 }
173 else
174 {
175 lib_assert(false, "Not yet implimented");
176 }
177 }
178 void copyData(const TableColumn& reference_data, bool set_type) final override
179 {
180
181 if (set_type && m_type != reference_data.type())
182 {
183 cleanup();
184 setType(reference_data.type());
185 }
186 setSize(reference_data.size());
187 updateRegion(reference_data, 0, reference_data.size());
188 m_modified_time = reference_data.modifiedTime();
189 }
190 GLuint bufferID() const
191 {
192 lib_assert(!IsInvalid(m_buffer), "Bad buffer");
193 return m_buffer;
194 }
195 void bind()
196 {
197 m_context->glBindBuffer(bindLocation(), bufferID());
198 }
199
200 void setUniformAttribute(const GLESShader& shader, const char* location)
201 {
202 lib_assert(!m_is_accessable, "Buffer not setup");
203 bind();
204 GLint idx = m_context->glGetUniformBlockIndex(shader.id(), location);
205 m_context->glUniformBlockBinding(shader.id(), idx, 0);
206 m_context->glBindBufferRange(bindLocation(), 0, bufferID(), 0, type().total_size);
207 }
208 void setAttribute(GLint attribute, uint04 offset = 0, uint04 stride = Constant<uint04>::Invalid)
209 {
210 if (IsInvalid(stride))
211 stride = vectorSize();
212 size_t loc = offset * typeStride();
213 GLsizei gl_stride = cast<GLsizei>(stride * typeStride());
214 setRawAttribute(attribute, vectorSize(), getFormat(), gl_stride, loc);
215 }
216 void setRawAttribute(GLint attribute, uint04 vector_size, GLuint format, GLsizei stride, size_t offset)
217 {
218 lib_assert(!m_is_accessable, "Buffer not setup");
219 bind();
220 m_context->glEnableVertexAttribArray(attribute);
221 switch (format)
222 {
223 case GL_FLOAT:
224#ifdef GL_DOUBLE
225 case GL_DOUBLE:
226#endif
227 m_context->glVertexAttribPointer(attribute, vector_size, format, GL_FALSE, stride, (void*)offset);
228 break;
229 default:
230 m_context->glVertexAttribIPointer(attribute, vector_size, format, stride, (void*)offset);
231 break;
232 }
234 m_context->glVertexAttribDivisor(attribute, 1);
235 release();
236 }
237 GLuint bindLocation() const
238 {
239 switch (m_internal_type)
240 {
241 case Type::INDEX: return GL_ELEMENT_ARRAY_BUFFER; break;
242 case Type::INSTANCE: return GL_ARRAY_BUFFER; break;
243 case Type::VERTEX: return GL_ARRAY_BUFFER; break;
244 #ifdef GL_UNIFORM_BUFFER
245 case Type::UNIFORM: return GL_UNIFORM_BUFFER; break;
246 #endif
247 default: lib_assert(false, "unknown type"); break;
248 }
249 return GL_ARRAY_BUFFER;
250 }
251 void release()
252 {
253 m_context->glBindBuffer(bindLocation(), 0);
254 }
255 private:
256 GLuint createBuffer(uint04& allocation_size, uint04 buffer_size, uint04 object_size, GLuint usage, const void* data)
257 {
258 lib_assert(buffer_size > 0, "Bad buffer size");
259 lib_assert(IsInvalid(m_buffer), "Bad buffer generation");
260 GLuint buffer = 0;
261 m_context->glGenBuffers(1, &buffer);
262 lib_assert(buffer != 0, "Bad buffer allocation");
263 m_context->glBindBuffer(bindLocation(), buffer);
264 allocation_size = buffer_size;
265 m_context->glBufferData(bindLocation(), buffer_size * object_size, data, usage);
266 return buffer;
267 }
268 GLuint m_buffer = Constant<uint04>::Invalid;
269 DynamicPointer<GLESGraphicsDevice> m_context;
270 Buffer<uint01> m_temp_buffer;
271 // Other member variables as needed...
272 };
273}
#define UNUSED(expr)
Definition BaseValues.hpp:409
#define lib_assert(expression, message)
Definition LibAssert.h:61
#define _t(english_string)
Definition Translator.h:90
#define _td(def, english_string)
Definition Translator.h:94
void setSize(t_index_type new_size)
Definition Buffer.hpp:803
void ensureCapacity(t_index_type new_capacity, bool ensure_not_greater=false, bool ensure_not_less=true)
Definition Buffer.hpp:519
decltype(auto) begin()
Definition Buffer.hpp:402
void clear()
Definition Buffer.hpp:422
Provides a modifiable pointer that has shared ownership of a dynamically allocated object.
Definition Pointer.hpp:320
A Shader for rendering default OpenGL logic to the graphics card.
Definition GLESShader.h:16
uint04 id() const
A Special abstract TableColumn responsible for interfacing a Buffer of data with the video card....
Definition GraphicsBuffer.h:45
@ VERTEX
Definition GraphicsBuffer.h:49
@ INDEX
Definition GraphicsBuffer.h:51
@ UNIFORM
Definition GraphicsBuffer.h:50
@ INSTANCE
Definition GraphicsBuffer.h:52
void setType(const TypeInfo &type)
GraphicsBuffer(const String &label)
uint04 m_size
Definition GraphicsBuffer.h:509
uint04 size() const final override
Definition GraphicsBuffer.h:382
bool m_is_memory_owner
Definition GraphicsBuffer.h:514
Type m_internal_type
Definition GraphicsBuffer.h:511
uint04 m_allocated_size
Definition GraphicsBuffer.h:510
bool m_is_accessable
Definition GraphicsBuffer.h:512
A common exeption thrown when an issue occurs within the NDEVR Graphics Engine.
Definition GraphicsException.h:44
Buffer for storing data in the OpenGL engine, usually for rendering.
Definition GLBuffer.h:14
void release()
Definition GLBuffer.h:251
uint04 typeStride() const
Definition GLBuffer.h:125
void sendToVideoCard(bool) final override
Definition GLBuffer.h:78
OpenGLBuffer(const String &label, const DynamicPointer< GLESGraphicsDevice > &device)
Definition GLBuffer.h:16
GLuint bufferID() const
Definition GLBuffer.h:190
void setSize(uint04 buffer_size) final override
Definition GLBuffer.h:158
OpenGLBuffer(const TableColumn &reference_data, const DynamicPointer< GLESGraphicsDevice > &device)
Definition GLBuffer.h:22
void bind()
Definition GLBuffer.h:195
void getFromVideoCard(bool) final override
Definition GLBuffer.h:66
void setRawAttribute(GLint attribute, uint04 vector_size, GLuint format, GLsizei stride, size_t offset)
Definition GLBuffer.h:216
~OpenGLBuffer()
Definition GLBuffer.h:35
OpenGLBuffer(OpenGLBuffer &&other) noexcept
Definition GLBuffer.h:28
void updateRegion(const TableColumn &reference_data, uint04 offset, uint04 size) final override
Definition GLBuffer.h:165
GLuint getFormat(uint01) const
Definition GLBuffer.h:89
void * mapMemory(size_t offset, size_t size) const final override
Definition GLBuffer.h:40
void setAttribute(GLint attribute, uint04 offset=0, uint04 stride=Constant< uint04 >::Invalid)
Definition GLBuffer.h:208
GLuint bindLocation() const
Definition GLBuffer.h:237
void copyData(const TableColumn &reference_data, bool set_type) final override
Definition GLBuffer.h:178
void cleanup() final override
Definition GLBuffer.h:56
void insertRows(uint04, uint04) final override
Definition GLBuffer.h:154
void setUniformAttribute(const GLESShader &shader, const char *location)
Definition GLBuffer.h:200
void unmapMemory() const final override
Definition GLBuffer.h:50
GLuint getFormat() const
Definition GLBuffer.h:150
GLint vectorSize() const
Definition GLBuffer.h:142
The core String class for the NDEVR API.
Definition String.h:69
A virtual storage type that is used with Table class to store data where the actual mechanism for sto...
Definition TableColumn.h:76
String m_label
Definition TableColumn.h:616
TypeInfo m_type
Definition TableColumn.h:617
Time m_modified_time
Definition TableColumn.h:615
const String & label() const
virtual TypeInfo type() const
Definition TableColumn.h:86
Definition ACIColor.h:37
constexpr bool IsInvalid(const t_type &value)
Query if 'value' is valid or invalid. Invalid values should return invalid if used for calculations o...
Definition BaseFunctions.hpp:170
constexpr t_type getMax(const t_type &left, const t_type &right)
Finds the max of the given arguments using the > operator The only requirement is that t_type have > ...
Definition BaseFunctions.hpp:94
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:80
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:375
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233
static const t_type Invalid
Definition BaseValues.hpp:234