]> git.tdb.fi Git - libs/gl.git/blob - source/core/vertexsetup.cpp
Add a visitor to dump the AST for debugging purposes
[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 *c=fmt.begin(); c!=fmt.end(); ++c)
83         {
84                 unsigned t = get_component_type(*c);
85                 if(t>=get_component_type(ATTRIB1))
86                         t -= get_component_type(ATTRIB1);
87                 mask |= 1<<t;
88         }
89         return mask;
90 }
91
92 unsigned VertexSetup::get_update_mask(unsigned base, const VertexFormat &cur_fmt, const VertexArray &new_array)
93 {
94         unsigned unused = get_attribs(cur_fmt)&~get_attribs(new_array.get_format());
95         return base | (unused ? UNUSED_ATTRIBS | (unused<<ATTRIB_SHIFT) : 0);
96 }
97
98 void VertexSetup::update(unsigned mask) const
99 {
100         static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
101         if(!direct && current()!=this)
102         {
103                 dirty |= mask;
104                 return;
105         }
106
107         if(mask&UNUSED_ATTRIBS)
108         {
109                 for(unsigned i=0, am=mask>>ATTRIB_SHIFT; am; ++i, am>>=1)
110                         if(am&1)
111                         {
112                                 if(direct)
113                                         glDisableVertexArrayAttrib(id, i);
114                                 else
115                                         glDisableVertexAttribArray(i);
116                         }
117         }
118
119         if(mask&VERTEX_ARRAY)
120                 update_vertex_array(*vertex_array, 0, 0, direct);
121
122         if((mask&INSTANCE_ARRAY) && inst_array)
123                 update_vertex_array(*inst_array, 1, 1, direct);
124
125         if(mask&INDEX_BUFFER)
126         {
127                 if(direct)
128                         glVertexArrayElementBuffer(id, index_buffer->get_id());
129                 else
130                         glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
131         }
132 }
133
134 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
135 {
136         Conditional<Bind> bind_vbuf(!direct, array.get_buffer(), ARRAY_BUFFER);
137
138         const VertexFormat &fmt = array.get_format();
139         unsigned stride = get_stride(fmt)*sizeof(float);
140         if(direct)
141         {
142                 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
143                 glVertexArrayBindingDivisor(id, binding, divisor);
144         }
145
146         unsigned offset = 0;
147         for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
148         {
149                 unsigned t = get_component_type(*c);
150                 if(t>=get_component_type(ATTRIB1))
151                         t -= get_component_type(ATTRIB1);
152                 unsigned sz = get_component_size(*c);
153                 if(direct)
154                 {
155                         if(*c==COLOR4_UBYTE)
156                                 glVertexArrayAttribFormat(id, t, 4, GL_UNSIGNED_BYTE, true, offset);
157                         else
158                                 glVertexArrayAttribFormat(id, t, sz, GL_FLOAT, false, offset);
159                         glVertexArrayAttribBinding(id, t, binding);
160                         glEnableVertexArrayAttrib(id, t);
161                 }
162                 else
163                 {
164                         if(*c==COLOR4_UBYTE)
165                                 glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
166                         else
167                                 glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
168                         if(ARB_instanced_arrays)
169                                 glVertexAttribDivisor(t, divisor);
170                         glEnableVertexAttribArray(t);
171                 }
172                 offset += sz*sizeof(float);
173         }
174 }
175
176 void VertexSetup::bind() const
177 {
178         if(!vertex_array || !index_buffer)
179                 throw invalid_operation("VertexSetup::bind");
180
181         if(set_current(this))
182         {
183                 vertex_array->refresh();
184                 if(inst_array)
185                         inst_array->refresh();
186                 glBindVertexArray(id);
187                 if(dirty)
188                 {
189                         update(dirty);
190                         dirty = 0;
191                 }
192         }
193 }
194
195 void VertexSetup::unbind()
196 {
197         if(set_current(0))
198                 glBindVertexArray(0);
199 }
200
201 } // namespace GL
202 } // namespace Msp