]> git.tdb.fi Git - libs/gl.git/blob - source/core/vertexsetup.cpp
Move checking of vertex attribute indices to VertexSetup
[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 "buffer.h"
9 #include "error.h"
10 #include "gl.h"
11 #include "misc.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 {
26         static Require req(ARB_vertex_array_object);
27         if(ARB_direct_state_access)
28                 glCreateVertexArrays(1, &id);
29         else
30                 glGenVertexArrays(1, &id);
31 }
32
33 VertexSetup::~VertexSetup()
34 {
35         if(current()==this)
36                 unbind();
37         glDeleteVertexArrays(1, &id);
38 }
39
40 void VertexSetup::set_vertex_array(const VertexArray &a)
41 {
42         if(!verify_array(a))
43                 throw invalid_argument("VertexSetup::set_vertex_array");
44
45         vertex_array = &a;
46         update(get_update_mask(VERTEX_ARRAY, vertex_format, *vertex_array));
47         vertex_format = vertex_array->get_format();
48 }
49
50 void VertexSetup::set_instance_array(const VertexArray *a)
51 {
52         if(a)
53         {
54                 if(!verify_array(*a))
55                         throw invalid_argument("VertexSetup::set_instance_array");
56
57                 static Require req(ARB_instanced_arrays);
58         }
59
60         inst_array = a;
61         update(get_update_mask(INSTANCE_ARRAY, inst_format, *inst_array));
62         inst_format = inst_array->get_format();
63 }
64
65 void VertexSetup::set_index_buffer(const Buffer &ibuf)
66 {
67         index_buffer = &ibuf;
68         update(INDEX_BUFFER);
69 }
70
71 void VertexSetup::refresh()
72 {
73         if(vertex_array && vertex_array->get_format()!=vertex_format)
74                 set_vertex_array(*vertex_array);
75
76         if(inst_array && inst_array->get_format()!=inst_format)
77                 set_instance_array(inst_array);
78 }
79
80 bool VertexSetup::verify_array(const VertexArray &array)
81 {
82         if(!array.get_buffer())
83                 return false;
84
85         static int max_attribs = -1;
86         if(max_attribs<0)
87                 max_attribs = get_i(GL_MAX_VERTEX_ATTRIBS);
88
89         const VertexFormat &fmt = array.get_format();
90         for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
91                 if(static_cast<int>(get_attribute_semantic(*a))>=max_attribs)
92                         return false;
93
94         return true;
95 }
96
97 unsigned VertexSetup::get_attribs(const VertexFormat &fmt)
98 {
99         unsigned mask = 0;
100         for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
101                 mask |= 1<<get_attribute_semantic(*a);
102         return mask;
103 }
104
105 unsigned VertexSetup::get_update_mask(unsigned base, const VertexFormat &cur_fmt, const VertexArray &new_array)
106 {
107         unsigned unused = get_attribs(cur_fmt)&~get_attribs(new_array.get_format());
108         return base | (unused ? UNUSED_ATTRIBS | (unused<<ATTRIB_SHIFT) : 0);
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&UNUSED_ATTRIBS)
121         {
122                 for(unsigned i=0, am=mask>>ATTRIB_SHIFT; am; ++i, am>>=1)
123                         if(am&1)
124                         {
125                                 if(direct)
126                                         glDisableVertexArrayAttrib(id, i);
127                                 else
128                                         glDisableVertexAttribArray(i);
129                         }
130         }
131
132         if(mask&VERTEX_ARRAY)
133                 update_vertex_array(*vertex_array, 0, 0, direct);
134
135         if((mask&INSTANCE_ARRAY) && inst_array)
136                 update_vertex_array(*inst_array, 1, 1, direct);
137
138         if(mask&INDEX_BUFFER)
139         {
140                 if(direct)
141                         glVertexArrayElementBuffer(id, index_buffer->get_id());
142                 else
143                         glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
144         }
145 }
146
147 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
148 {
149         Conditional<Bind> bind_vbuf(!direct, array.get_buffer(), ARRAY_BUFFER);
150
151         const VertexFormat &fmt = array.get_format();
152         unsigned stride = fmt.stride()*sizeof(float);
153         if(direct)
154         {
155                 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
156                 glVertexArrayBindingDivisor(id, binding, divisor);
157         }
158
159         unsigned offset = 0;
160         for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
161         {
162                 unsigned sem = get_attribute_semantic(*a);
163                 unsigned sz = get_attribute_size(*a);
164                 if(direct)
165                 {
166                         if(*a==COLOR4_UBYTE)
167                                 glVertexArrayAttribFormat(id, sem, 4, GL_UNSIGNED_BYTE, true, offset);
168                         else
169                                 glVertexArrayAttribFormat(id, sem, sz, GL_FLOAT, false, offset);
170                         glVertexArrayAttribBinding(id, sem, binding);
171                         glEnableVertexArrayAttrib(id, sem);
172                 }
173                 else
174                 {
175                         if(*a==COLOR4_UBYTE)
176                                 glVertexAttribPointer(sem, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
177                         else
178                                 glVertexAttribPointer(sem, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
179                         if(ARB_instanced_arrays)
180                                 glVertexAttribDivisor(sem, divisor);
181                         glEnableVertexAttribArray(sem);
182                 }
183                 offset += sz*sizeof(float);
184         }
185 }
186
187 void VertexSetup::bind() const
188 {
189         if(!vertex_array || !index_buffer)
190                 throw invalid_operation("VertexSetup::bind");
191
192         if(set_current(this))
193         {
194                 vertex_array->refresh();
195                 if(inst_array)
196                         inst_array->refresh();
197                 glBindVertexArray(id);
198                 if(dirty)
199                 {
200                         update(dirty);
201                         dirty = 0;
202                 }
203         }
204 }
205
206 void VertexSetup::unbind()
207 {
208         if(set_current(0))
209                 glBindVertexArray(0);
210 }
211
212 } // namespace GL
213 } // namespace Msp