]> git.tdb.fi Git - libs/gl.git/blob - source/core/vertexsetup.cpp
Refactor VertexSetup format checking
[libs/gl.git] / source / core / vertexsetup.cpp
1 #include <msp/gl/extensions/arb_direct_state_access.h>
2 #include <msp/gl/extensions/arb_instanced_arrays.h>
3 #include <msp/gl/extensions/arb_vertex_array_object.h>
4 #include <msp/gl/extensions/arb_vertex_attrib_binding.h>
5 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
6 #include <msp/gl/extensions/arb_vertex_shader.h>
7 #include <msp/gl/extensions/ext_gpu_shader4.h>
8 #include <msp/gl/extensions/khr_debug.h>
9 #include "buffer.h"
10 #include "deviceinfo.h"
11 #include "error.h"
12 #include "vertexarray.h"
13 #include "vertexsetup.h"
14
15 using namespace std;
16
17 namespace Msp {
18 namespace GL {
19
20 VertexSetup::VertexSetup():
21         dirty(0),
22         vertex_array(0),
23         inst_array(0),
24         index_buffer(0),
25         index_type(UNSIGNED_SHORT)
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         glDeleteVertexArrays(1, &id);
37 }
38
39 void VertexSetup::set_format(const VertexFormat &vfmt)
40 {
41         if(!verify_format(vfmt))
42                 throw invalid_argument("VertexSetup::set_format");
43         if(!vertex_format.empty())
44                 throw invalid_operation("VertexSetup::set_format");
45
46         require_format(vfmt, false);
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         require_format(vfmt, false);
59         require_format(ifmt, true);
60
61         vertex_format = vfmt;
62         inst_format = ifmt;
63 }
64
65 void VertexSetup::set_vertex_array(const VertexArray &a)
66 {
67         if(vertex_format.empty())
68                 throw invalid_operation("VertexSetup::set_vertex_array");
69         if(a.get_format()!=vertex_format)
70                 throw incompatible_data("VertexSetup::set_vertex_array");
71         if(!a.get_buffer())
72                 throw invalid_argument("VertexSetup::set_vertex_array");
73
74         vertex_array = &a;
75         dirty |= VERTEX_ARRAY;
76 }
77
78 void VertexSetup::set_instance_array(const VertexArray &a)
79 {
80         if(inst_format.empty())
81                 throw invalid_operation("VertexSetup::set_instance_array");
82         if(a.get_format()!=inst_format)
83                 throw incompatible_data("VertexSetup::set_instance_array");
84         if(!a.get_buffer())
85                 throw invalid_argument("VertexSetup::set_instance_array");
86
87         inst_array = &a;
88         dirty |= INSTANCE_ARRAY;
89 }
90
91 void VertexSetup::set_index_buffer(const Buffer &ibuf, DataType itype)
92 {
93         index_buffer = &ibuf;
94         index_type = itype;
95         dirty |= INDEX_BUFFER;
96 }
97
98 bool VertexSetup::verify_format(const VertexFormat &fmt)
99 {
100         if(fmt.empty())
101                 return false;
102
103         static unsigned max_attribs = DeviceInfo::get_global().limits.max_vertex_attributes;
104         return all_of(fmt.begin(), fmt.end(), [](VertexAttribute a){ return get_attribute_semantic(a)<max_attribs; });
105 }
106
107 void VertexSetup::require_format(const VertexFormat &fmt, bool instanced)
108 {
109         if(any_of(fmt.begin(), fmt.end(), is_integer_attribute))
110                 static Require _req(EXT_gpu_shader4);
111         if(instanced)
112                 static Require req(ARB_instanced_arrays);
113 }
114
115 void VertexSetup::update() const
116 {
117         static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
118
119         if(dirty&VERTEX_ARRAY)
120                 update_vertex_array(*vertex_array, 0, 0, direct);
121
122         if(dirty&INSTANCE_ARRAY)
123                 update_vertex_array(*inst_array, 1, 1, direct);
124
125         if(dirty&INDEX_BUFFER)
126         {
127                 if(direct)
128                         glVertexArrayElementBuffer(id, index_buffer->id);
129                 else
130                         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer->id);
131         }
132
133         dirty = 0;
134 }
135
136 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
137 {
138         if(!direct)
139         {
140                 Buffer::unbind_scratch();
141                 glBindBuffer(GL_ARRAY_BUFFER, array.get_buffer()->id);
142         }
143
144         const VertexFormat &fmt = array.get_format();
145         unsigned stride = fmt.stride();
146         if(direct)
147         {
148                 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->id, 0, stride);
149                 glVertexArrayBindingDivisor(id, binding, divisor);
150         }
151
152         unsigned offset = 0;
153         for(VertexAttribute a: fmt)
154         {
155                 unsigned sem = get_attribute_semantic(a);
156                 bool integer = is_integer_attribute(a);
157                 GLenum type = get_gl_type(get_attribute_source_type(a));
158                 unsigned cc = get_attribute_component_count(a);
159                 if(direct)
160                 {
161                         if(integer)
162                                 glVertexArrayAttribIFormat(id, sem, cc, type, offset);
163                         else
164                                 glVertexArrayAttribFormat(id, sem, cc, type, true, offset);
165                         glVertexArrayAttribBinding(id, sem, binding);
166                         glEnableVertexArrayAttrib(id, sem);
167                 }
168                 else
169                 {
170                         if(integer)
171                                 glVertexAttribIPointer(sem, cc, type, stride, reinterpret_cast<void *>(offset));
172                         else
173                                 glVertexAttribPointer(sem, cc, type, true, stride, reinterpret_cast<void *>(offset));
174                         if(ARB_instanced_arrays)
175                                 glVertexAttribDivisor(sem, divisor);
176                         glEnableVertexAttribArray(sem);
177                 }
178                 offset += get_attribute_size(a);
179         }
180
181         if(!direct)
182                 glBindBuffer(GL_ARRAY_BUFFER, 0);
183 }
184
185 void VertexSetup::unload()
186 {
187         if(ARB_direct_state_access)
188         {
189                 glVertexArrayVertexBuffer(id, 0, 0, 0, 0);
190                 glVertexArrayVertexBuffer(id, 1, 0, 0, 0);
191                 glVertexArrayElementBuffer(id, 0);
192         }
193         else
194         {
195                 glBindVertexArray(id);
196                 glBindBuffer(GL_ARRAY_BUFFER, 0);
197
198                 for(VertexAttribute a: vertex_format)
199                 {
200                         unsigned sem = get_attribute_semantic(a);
201                         glDisableVertexAttribArray(sem);
202                         glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
203                 }
204                 for(VertexAttribute a: inst_format)
205                 {
206                         unsigned sem = get_attribute_semantic(a);
207                         glDisableVertexAttribArray(sem);
208                         glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
209                 }
210
211                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
212         }
213
214         vertex_array = 0;
215         vertex_format = VertexFormat();
216         inst_array = 0;
217         inst_format = VertexFormat();
218         index_buffer = 0;
219 }
220
221 void VertexSetup::set_debug_name(const string &name)
222 {
223 #ifdef DEBUG
224         if(KHR_debug)
225                 glObjectLabel(GL_VERTEX_ARRAY, id, name.size(), name.c_str());
226 #else
227         (void)name;
228 #endif
229 }
230
231 } // namespace GL
232 } // namespace Msp