|
| 1 | +/* |
| 2 | + * GLP OpenGL Wrappers - Copyright (c) 2012 Jakob Progsch |
| 3 | + * |
| 4 | + * This software is provided 'as-is', without any express or implied |
| 5 | + * warranty. In no event will the authors be held liable for any damages |
| 6 | + * arising from the use of this software. |
| 7 | + * |
| 8 | + * Permission is granted to anyone to use this software for any purpose, |
| 9 | + * including commercial applications, and to alter it and redistribute it |
| 10 | + * freely, subject to the following restrictions: |
| 11 | + * |
| 12 | + * 1. The origin of this software must not be misrepresented; you must not |
| 13 | + * claim that you wrote the original software. If you use this software |
| 14 | + * in a product, an acknowledgment in the product documentation would be |
| 15 | + * appreciated but is not required. |
| 16 | + * |
| 17 | + * 2. Altered source versions must be plainly marked as such, and must not be |
| 18 | + * misrepresented as being the original software. |
| 19 | + * |
| 20 | + * 3. This notice may not be removed or altered from any source |
| 21 | + * distribution. |
| 22 | + */ |
| 23 | + |
| 24 | +#ifndef GL_BUFFER_H |
| 25 | +#define GL_BUFFER_H |
| 26 | + |
| 27 | +#include <stdexcept> |
| 28 | +#include <boost/utility.hpp> |
| 29 | + |
| 30 | +#define GL_GLEXT_PROTOTYPES |
| 31 | +#include <GL/gl.h> |
| 32 | + |
| 33 | +#include "GLCheckError.h" |
| 34 | + |
| 35 | +namespace glp { |
| 36 | + |
| 37 | +template<class T, GLenum TARGET> |
| 38 | +class Buffer : boost::noncopyable { |
| 39 | +public: |
| 40 | + typedef T value_type; |
| 41 | + typedef T& reference; |
| 42 | + typedef const T& const_reference; |
| 43 | + typedef T* iterator; |
| 44 | + typedef const T* const_iterator; |
| 45 | + typedef const ptrdiff_t difference_type; |
| 46 | + typedef size_t size_type; |
| 47 | + |
| 48 | + |
| 49 | + Buffer(size_type s, GLenum usage) |
| 50 | + : size_(s), host_ptr(0) |
| 51 | + { |
| 52 | + GLP_CHECKED_CALL(glGenBuffers(1, &buffer);) |
| 53 | + GLP_CHECKED_CALL(glBindBuffer(TARGET, buffer);) |
| 54 | + GLP_CHECKED_CALL(glBufferData(TARGET, size_*sizeof(value_type), 0, usage);) |
| 55 | + GLP_CHECKED_CALL(glBindBuffer(TARGET, 0);) |
| 56 | + } |
| 57 | + |
| 58 | + void map(GLbitfield access) |
| 59 | + { |
| 60 | + if(host_ptr) |
| 61 | + return; |
| 62 | + GLP_CHECKED_CALL(glBindBuffer(TARGET, buffer);) |
| 63 | + GLP_CHECKED_CALL( |
| 64 | + host_ptr = reinterpret_cast<value_type*>( |
| 65 | + glMapBufferRange(TARGET, 0, |
| 66 | + size_*sizeof(value_type), |
| 67 | + access) |
| 68 | + ); |
| 69 | + ) |
| 70 | + GLP_CHECKED_CALL(glBindBuffer(TARGET, 0);) |
| 71 | + } |
| 72 | + |
| 73 | + bool isMapped() const { return host_ptr != 0; } |
| 74 | + |
| 75 | + inline reference operator[](size_t i) |
| 76 | + { |
| 77 | + check_mapped(); |
| 78 | + return host_ptr[i]; |
| 79 | + } |
| 80 | + |
| 81 | + inline const_reference operator[](size_t i) const |
| 82 | + { |
| 83 | + check_mapped(); |
| 84 | + return host_ptr[i]; |
| 85 | + } |
| 86 | + |
| 87 | + inline value_type* data() { check_mapped(); return host_ptr; } |
| 88 | + inline const value_type* data() const { check_mapped(); return host_ptr; } |
| 89 | + inline iterator begin() { check_mapped(); return host_ptr; } |
| 90 | + inline const_iterator begin() const { check_mapped(); return host_ptr; } |
| 91 | + inline iterator end() { check_mapped(); return host_ptr+size_; } |
| 92 | + inline const_iterator end() const { check_mapped(); return host_ptr+size_; } |
| 93 | + |
| 94 | + inline size_type size() const { return size_; } |
| 95 | + |
| 96 | + inline void bind() |
| 97 | + { |
| 98 | + check_unmapped(); |
| 99 | + GLP_CHECKED_CALL(glBindBuffer(TARGET, buffer);) |
| 100 | + } |
| 101 | + |
| 102 | + inline void unbind() |
| 103 | + { |
| 104 | + GLP_CHECKED_CALL(glBindBuffer(TARGET, 0);) |
| 105 | + } |
| 106 | + |
| 107 | + void unmap() |
| 108 | + { |
| 109 | + if(!host_ptr) |
| 110 | + return; |
| 111 | + GLP_CHECKED_CALL(glBindBuffer(TARGET, buffer);) |
| 112 | + GLP_CHECKED_CALL(glUnmapBuffer(TARGET);) |
| 113 | + GLP_CHECKED_CALL(glBindBuffer(TARGET, 0);) |
| 114 | + host_ptr = 0; |
| 115 | + } |
| 116 | + |
| 117 | + operator GLuint() const { return buffer; } |
| 118 | + GLuint getBuffer() const { return buffer; } |
| 119 | + |
| 120 | + virtual ~Buffer() |
| 121 | + { |
| 122 | + GLP_CHECKED_CALL(glDeleteBuffers(1, &buffer);) |
| 123 | + } |
| 124 | +protected: |
| 125 | + inline void check_mapped() const |
| 126 | + { |
| 127 | + if(!host_ptr) |
| 128 | + throw exception("Buffer not mapped"); |
| 129 | + } |
| 130 | + |
| 131 | + inline void check_unmapped() const |
| 132 | + { |
| 133 | + if(host_ptr) |
| 134 | + throw exception("Buffer mapped"); |
| 135 | + } |
| 136 | + |
| 137 | + GLuint buffer; |
| 138 | + size_type size_; |
| 139 | + value_type *host_ptr; |
| 140 | +}; |
| 141 | + |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +#endif |
0 commit comments