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