summaryrefslogtreecommitdiff
path: root/include/llmr/geometry/buffer.hpp
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2014-02-10 15:18:24 +0100
committerKonstantin Käfer <mail@kkaefer.com>2014-02-10 15:18:24 +0100
commit4c6dc54352db2c5ef616eb88984845bb11ee2dbe (patch)
tree647f8ef2af3cffa4b814d282133ad488c3f89ab2 /include/llmr/geometry/buffer.hpp
parentf4996046a2436886a5f1fc17a72a8cec21ad423d (diff)
downloadqtlocation-mapboxgl-4c6dc54352db2c5ef616eb88984845bb11ee2dbe.tar.gz
add missing buffer.hpp
Diffstat (limited to 'include/llmr/geometry/buffer.hpp')
-rw-r--r--include/llmr/geometry/buffer.hpp73
1 files changed, 73 insertions, 0 deletions
diff --git a/include/llmr/geometry/buffer.hpp b/include/llmr/geometry/buffer.hpp
new file mode 100644
index 0000000000..02f03d0105
--- /dev/null
+++ b/include/llmr/geometry/buffer.hpp
@@ -0,0 +1,73 @@
+#ifndef LLMR_GEOMETRY_BUFFER
+#define LLMR_GEOMETRY_BUFFER
+
+#include <cstdlib>
+#include <cassert>
+#include <llmr/platform/gl.hpp>
+
+namespace llmr {
+
+template <
+ size_t itemSize,
+ int bufferType = GL_ARRAY_BUFFER,
+ size_t defaultLength = 8192
+>
+class Buffer {
+public:
+ ~Buffer() {
+ if (array) {
+ free(array);
+ array = nullptr;
+ }
+ if (buffer != 0) {
+ glDeleteBuffers(1, &buffer);
+ buffer = 0;
+ }
+ }
+
+ inline size_t index() const {
+ return pos / itemSize;
+ }
+
+ void bind() {
+ if (buffer == 0) {
+ glGenBuffers(1, &buffer);
+ glBindBuffer(bufferType, buffer);
+ glBufferData(bufferType, pos, array, GL_STATIC_DRAW);
+ free(array);
+ array = nullptr;
+ } else {
+ glBindBuffer(bufferType, buffer);
+ }
+ }
+
+protected:
+ // increase the buffer size by at least /required/ bytes.
+ void *addElement() {
+ assert("Buffer is already bound to GPU" && buffer == 0);
+ if (length < pos + itemSize) {
+ while (length < pos + itemSize) length += defaultLength;
+ array = realloc(array, length);
+ assert("Buffer reallocation failed" && array != nullptr);
+ }
+ pos += itemSize;
+ return static_cast<char *>(array) + (pos - itemSize);
+ }
+
+private:
+ // CPU buffer
+ void *array = nullptr;
+
+ // Byte position where we are writing.
+ size_t pos = 0;
+
+ // Number of bytes that are valid in this buffer.
+ size_t length = 0;
+
+ // GL buffer ID
+ uint32_t buffer = 0;
+};
+
+}
+
+#endif