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