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