]> git.tdb.fi Git - libs/gl.git/blob - source/core/vertexsetup.cpp
da79e7f6135f9b3d06badf30e024903e7d5b280a
[libs/gl.git] / source / core / vertexsetup.cpp
1 #include <msp/core/raii.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_instanced_arrays.h>
4 #include <msp/gl/extensions/arb_vertex_array_object.h>
5 #include <msp/gl/extensions/arb_vertex_attrib_binding.h>
6 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
7 #include <msp/gl/extensions/arb_vertex_shader.h>
8 #include <msp/gl/extensions/khr_debug.h>
9 #include "buffer.h"
10 #include "error.h"
11 #include "gl.h"
12 #include "misc.h"
13 #include "vertexarray.h"
14 #include "vertexsetup.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GL {
20
21 VertexSetup::VertexSetup():
22         dirty(0),
23         vertex_array(0),
24         inst_array(0),
25         index_buffer(0)
26 {
27         static Require req(ARB_vertex_array_object);
28         if(ARB_direct_state_access)
29                 glCreateVertexArrays(1, &id);
30         else
31                 glGenVertexArrays(1, &id);
32 }
33
34 VertexSetup::~VertexSetup()
35 {
36         if(current()==this)
37                 unbind();
38         glDeleteVertexArrays(1, &id);
39 }
40
41 void VertexSetup::set_format(const VertexFormat &vfmt)
42 {
43         if(!verify_format(vfmt))
44                 throw invalid_argument("VertexSetup::set_format");
45         if(!vertex_format.empty())
46                 throw invalid_operation("VertexSetup::set_format");
47
48         vertex_format = vfmt;
49 }
50
51 void VertexSetup::set_format_instanced(const VertexFormat &vfmt, const VertexFormat &ifmt)
52 {
53         if(!verify_format(vfmt) || !verify_format(ifmt))
54                 throw invalid_argument("VertexSetup::set_format");
55         if(!vertex_format.empty())
56                 throw invalid_operation("VertexSetup::set_format");
57
58         vertex_format = vfmt;
59         inst_format = ifmt;
60 }
61
62 void VertexSetup::set_vertex_array(const VertexArray &a)
63 {
64         if(vertex_format.empty())
65                 throw invalid_operation("VertexSetup::set_vertex_array");
66         if(a.get_format()!=vertex_format)
67                 throw incompatible_data("VertexSetup::set_vertex_array");
68         if(!a.get_buffer())
69                 throw invalid_argument("VertexSetup::set_vertex_array");
70
71         vertex_array = &a;
72         update(VERTEX_ARRAY);
73 }
74
75 void VertexSetup::set_instance_array(const VertexArray &a)
76 {
77         if(inst_format.empty())
78                 throw invalid_operation("VertexSetup::set_instance_array");
79         if(a.get_format()!=inst_format)
80                 throw incompatible_data("VertexSetup::set_instance_array");
81         if(!a.get_buffer())
82                 throw invalid_argument("VertexSetup::set_instance_array");
83
84         static Require req(ARB_instanced_arrays);
85
86         inst_array = &a;
87         update(INSTANCE_ARRAY);
88 }
89
90 void VertexSetup::set_index_buffer(const Buffer &ibuf)
91 {
92         index_buffer = &ibuf;
93         update(INDEX_BUFFER);
94 }
95
96 bool VertexSetup::verify_format(const VertexFormat &fmt)
97 {
98         if(fmt.empty())
99                 return false;
100
101         static int max_attribs = -1;
102         if(max_attribs<0)
103                 max_attribs = get_i(GL_MAX_VERTEX_ATTRIBS);
104
105         for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
106                 if(static_cast<int>(get_attribute_semantic(*a))>=max_attribs)
107                         return false;
108
109         return true;
110 }
111
112 void VertexSetup::update(unsigned mask) const
113 {
114         static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
115         if(!direct && current()!=this)
116         {
117                 dirty |= mask;
118                 return;
119         }
120
121         if(mask&VERTEX_ARRAY)
122                 update_vertex_array(*vertex_array, 0, 0, direct);
123
124         if((mask&INSTANCE_ARRAY) && inst_array)
125                 update_vertex_array(*inst_array, 1, 1, direct);
126
127         if(mask&INDEX_BUFFER)
128         {
129                 if(direct)
130                         glVertexArrayElementBuffer(id, index_buffer->get_id());
131                 else
132                         glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
133         }
134 }
135
136 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
137 {
138         Conditional<Bind> bind_vbuf(!direct, array.get_buffer(), ARRAY_BUFFER);
139
140         const VertexFormat &fmt = array.get_format();
141         unsigned stride = fmt.stride()*sizeof(float);
142         if(direct)
143         {
144                 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
145                 glVertexArrayBindingDivisor(id, binding, divisor);
146         }
147
148         unsigned offset = 0;
149         for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
150         {
151                 unsigned sem = get_attribute_semantic(*a);
152                 unsigned sz = get_attribute_size(*a);
153                 if(direct)
154                 {
155                         if(*a==COLOR4_UBYTE)
156                                 glVertexArrayAttribFormat(id, sem, 4, GL_UNSIGNED_BYTE, true, offset);
157                         else
158                                 glVertexArrayAttribFormat(id, sem, sz, GL_FLOAT, false, offset);
159                         glVertexArrayAttribBinding(id, sem, binding);
160                         glEnableVertexArrayAttrib(id, sem);
161                 }
162                 else
163                 {
164                         if(*a==COLOR4_UBYTE)
165                                 glVertexAttribPointer(sem, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
166                         else
167                                 glVertexAttribPointer(sem, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
168                         if(ARB_instanced_arrays)
169                                 glVertexAttribDivisor(sem, divisor);
170                         glEnableVertexAttribArray(sem);
171                 }
172                 offset += sz*sizeof(float);
173         }
174 }
175
176 void VertexSetup::bind() const
177 {
178         if(!vertex_array || !index_buffer)
179                 throw invalid_operation("VertexSetup::bind");
180
181         if(set_current(this))
182         {
183                 vertex_array->refresh();
184                 if(inst_array)
185                         inst_array->refresh();
186                 glBindVertexArray(id);
187                 if(dirty)
188                 {
189                         update(dirty);
190                         dirty = 0;
191                 }
192         }
193 }
194
195 void VertexSetup::unbind()
196 {
197         if(set_current(0))
198                 glBindVertexArray(0);
199 }
200
201 void VertexSetup::unload()
202 {
203         if(ARB_direct_state_access)
204         {
205                 glVertexArrayVertexBuffer(id, 0, 0, 0, 0);
206                 glVertexArrayVertexBuffer(id, 1, 0, 0, 0);
207                 glVertexArrayElementBuffer(id, 0);
208         }
209         else
210         {
211                 BindRestore _bind(*this);
212                 Buffer::unbind_from(ARRAY_BUFFER);
213
214                 for(const unsigned char *a=vertex_format.begin(); a!=vertex_format.end(); ++a)
215                 {
216                         unsigned sem = get_attribute_semantic(*a);
217                         glDisableVertexAttribArray(sem);
218                         glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
219                 }
220                 for(const unsigned char *a=inst_format.begin(); a!=inst_format.end(); ++a)
221                 {
222                         unsigned sem = get_attribute_semantic(*a);
223                         glDisableVertexAttribArray(sem);
224                         glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
225                 }
226
227                 glBindBuffer(ELEMENT_ARRAY_BUFFER, 0);
228         }
229
230         vertex_array = 0;
231         vertex_format = VertexFormat();
232         inst_array = 0;
233         inst_format = VertexFormat();
234         index_buffer = 0;
235 }
236
237 void VertexSetup::set_debug_name(const string &name)
238 {
239 #ifdef DEBUG
240         if(KHR_debug)
241                 glObjectLabel(GL_VERTEX_ARRAY, id, name.size(), name.c_str());
242 #else
243         (void)name;
244 #endif
245 }
246
247 } // namespace GL
248 } // namespace Msp