Browse code

Add implementation

Robert Cranston authored on 29/12/2021 16:30:02
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,153 @@
1
+#ifndef GLTEXTUREBUFFER_HPP_
2
+#define GLTEXTUREBUFFER_HPP_
3
+
4
+
5
+#include <string>
6
+#include <utility>
7
+
8
+#include <gltexturend.hpp>
9
+
10
+
11
+/// Class
12
+
13
+template<typename Data>
14
+class GLTextureBuffer : public GLTextureND<1>
15
+{
16
+public:
17
+
18
+    /// Special member functions
19
+
20
+    explicit GLTextureBuffer(
21
+        std::string object_label,
22
+        Size        size,
23
+        GLenum      usage           = GL_STATIC_DRAW,
24
+        GLenum      internal_format = DataTraits<Data>::internal_format
25
+    );
26
+    GLTextureBuffer(GLTextureBuffer && other) noexcept;
27
+    virtual ~GLTextureBuffer();
28
+
29
+    /// Core
30
+
31
+    GLTextureBuffer static const & empty();
32
+
33
+    GLOBJECT_GET(GLuint, buffer)
34
+
35
+protected:
36
+
37
+    /// Core
38
+
39
+    void data_(
40
+        void const * data,
41
+        GLenum       target,
42
+        GLenum       format,
43
+        GLenum       type
44
+    ) override;
45
+
46
+    GLsizeiptr buffer_size_() const;
47
+
48
+private:
49
+
50
+    /// Core
51
+
52
+    GLuint buffer_;
53
+};
54
+
55
+
56
+/// Special member functions
57
+
58
+template<typename Data>
59
+inline GLTextureBuffer<Data>::GLTextureBuffer(
60
+    std::string object_label,
61
+    Size        size,
62
+    GLenum      usage,
63
+    GLenum      internal_format
64
+)
65
+:
66
+    GLTextureND(
67
+        std::move(object_label),
68
+        GL_TEXTURE_BUFFER,
69
+        GL_TEXTURE_BINDING_BUFFER,
70
+        GL_MAX_TEXTURE_BUFFER_SIZE,
71
+        size,
72
+        internal_format
73
+    ),
74
+    buffer_{0}
75
+{
76
+    try
77
+    {
78
+        check_supported_({3, 1}, "GL_ARB_texture_buffer_object");
79
+        switch (internal_format)
80
+        {
81
+            case GL_RGB32F:
82
+            case GL_RGB32I:
83
+            case GL_RGB32UI:
84
+                check_supported_({4, 0}, "GL_ARB_texture_buffer_object_rgb32");
85
+        }
86
+        glGenBuffers(1, &buffer_);
87
+        glBindBuffer(GL_TEXTURE_BUFFER, buffer_);
88
+        glBufferData(
89
+            GL_TEXTURE_BUFFER,
90
+            buffer_size_(),
91
+            nullptr,
92
+            usage
93
+        );
94
+        glTexBuffer(target_, internal_format, buffer_);
95
+        check_error_(glGetError());
96
+    }
97
+    catch (...)
98
+    {
99
+        glDeleteBuffers(1, &buffer_);
100
+        fail_action_("create");
101
+    }
102
+}
103
+
104
+template<typename Data>
105
+inline GLTextureBuffer<Data>::GLTextureBuffer(
106
+    GLTextureBuffer && other
107
+) noexcept
108
+:
109
+    buffer_{other.buffer_}
110
+{
111
+    other.buffer_ = 0;
112
+}
113
+
114
+template<typename Data>
115
+inline GLTextureBuffer<Data>::~GLTextureBuffer()
116
+{
117
+    glDeleteBuffers(1, &buffer_);
118
+}
119
+
120
+/// Core
121
+
122
+template<typename Data>
123
+inline GLTextureBuffer<Data> const & GLTextureBuffer<Data>::empty()
124
+{
125
+    return empty_<GLTextureBuffer>();
126
+}
127
+
128
+template<typename Data>
129
+inline void GLTextureBuffer<Data>::data_(
130
+    void const * data,
131
+    GLenum       target,
132
+    GLenum       format,
133
+    GLenum       type
134
+)
135
+{
136
+    check_format_(format, DataTraits<Data>::format);
137
+    check_type_  (type,   DataTraits<Data>::type);
138
+    glBufferSubData(
139
+        target,
140
+        0,
141
+        buffer_size_(),
142
+        data
143
+    );
144
+}
145
+
146
+template<typename Data>
147
+inline GLsizeiptr GLTextureBuffer<Data>::buffer_size_() const
148
+{
149
+    return (GLsizeiptr)(data_size_() * sizeof(Data));
150
+}
151
+
152
+
153
+#endif // GLTEXTUREBUFFER_HPP_