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