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