]> git.tdb.fi Git - libs/gl.git/blob - source/core/vertexsetup.cpp
Store index type in 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 <msp/gl/extensions/khr_debug.h>
9 #include "buffer.h"
10 #include "deviceinfo.h"
11 #include "error.h"
12 #include "gl.h"
13 #include "misc.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         vertex_format = vfmt;
49 }
50
51 void VertexSetup::set_format_instanced(const VertexFormat &vfmt, const VertexFormat &ifmt)
52 {
53         if(!verify_format(vfmt) || !verify_format(ifmt))
54                 throw invalid_argument("VertexSetup::set_format");
55         if(!vertex_format.empty())
56                 throw invalid_operation("VertexSetup::set_format");
57
58         vertex_format = vfmt;
59         inst_format = ifmt;
60 }
61
62 void VertexSetup::set_vertex_array(const VertexArray &a)
63 {
64         if(vertex_format.empty())
65                 throw invalid_operation("VertexSetup::set_vertex_array");
66         if(a.get_format()!=vertex_format)
67                 throw incompatible_data("VertexSetup::set_vertex_array");
68         if(!a.get_buffer())
69                 throw invalid_argument("VertexSetup::set_vertex_array");
70
71         vertex_array = &a;
72         dirty |= VERTEX_ARRAY;
73 }
74
75 void VertexSetup::set_instance_array(const VertexArray &a)
76 {
77         if(inst_format.empty())
78                 throw invalid_operation("VertexSetup::set_instance_array");
79         if(a.get_format()!=inst_format)
80                 throw incompatible_data("VertexSetup::set_instance_array");
81         if(!a.get_buffer())
82                 throw invalid_argument("VertexSetup::set_instance_array");
83
84         static Require req(ARB_instanced_arrays);
85
86         inst_array = &a;
87         dirty |= INSTANCE_ARRAY;
88 }
89
90 void VertexSetup::set_index_buffer(const Buffer &ibuf, DataType itype)
91 {
92         index_buffer = &ibuf;
93         index_type = itype;
94         dirty |= INDEX_BUFFER;
95 }
96
97 bool VertexSetup::verify_format(const VertexFormat &fmt)
98 {
99         if(fmt.empty())
100                 return false;
101
102         unsigned max_attribs = Limits::get_global().max_vertex_attributes;
103
104         for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
105                 if(get_attribute_semantic(*a)>=max_attribs)
106                         return false;
107
108         return true;
109 }
110
111 void VertexSetup::update() const
112 {
113         static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
114
115         if(dirty&VERTEX_ARRAY)
116                 update_vertex_array(*vertex_array, 0, 0, direct);
117
118         if(dirty&INSTANCE_ARRAY)
119                 update_vertex_array(*inst_array, 1, 1, direct);
120
121         if(dirty&INDEX_BUFFER)
122         {
123                 if(direct)
124                         glVertexArrayElementBuffer(id, index_buffer->get_id());
125                 else
126                         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
127         }
128
129         dirty = 0;
130 }
131
132 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
133 {
134         if(!direct)
135                 glBindBuffer(GL_ARRAY_BUFFER, array.get_buffer()->get_id());
136
137         const VertexFormat &fmt = array.get_format();
138         unsigned stride = fmt.stride()*sizeof(float);
139         if(direct)
140         {
141                 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
142                 glVertexArrayBindingDivisor(id, binding, divisor);
143         }
144
145         unsigned offset = 0;
146         for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
147         {
148                 unsigned sem = get_attribute_semantic(*a);
149                 unsigned sz = get_attribute_size(*a);
150                 if(direct)
151                 {
152                         if(*a==COLOR4_UBYTE)
153                                 glVertexArrayAttribFormat(id, sem, 4, GL_UNSIGNED_BYTE, true, offset);
154                         else
155                                 glVertexArrayAttribFormat(id, sem, sz, GL_FLOAT, false, offset);
156                         glVertexArrayAttribBinding(id, sem, binding);
157                         glEnableVertexArrayAttrib(id, sem);
158                 }
159                 else
160                 {
161                         if(*a==COLOR4_UBYTE)
162                                 glVertexAttribPointer(sem, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
163                         else
164                                 glVertexAttribPointer(sem, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
165                         if(ARB_instanced_arrays)
166                                 glVertexAttribDivisor(sem, divisor);
167                         glEnableVertexAttribArray(sem);
168                 }
169                 offset += sz*sizeof(float);
170         }
171
172         if(!direct)
173                 glBindBuffer(GL_ARRAY_BUFFER, 0);
174 }
175
176 void VertexSetup::unload()
177 {
178         if(ARB_direct_state_access)
179         {
180                 glVertexArrayVertexBuffer(id, 0, 0, 0, 0);
181                 glVertexArrayVertexBuffer(id, 1, 0, 0, 0);
182                 glVertexArrayElementBuffer(id, 0);
183         }
184         else
185         {
186                 glBindVertexArray(id);
187                 glBindBuffer(GL_ARRAY_BUFFER, 0);
188
189                 for(const unsigned char *a=vertex_format.begin(); a!=vertex_format.end(); ++a)
190                 {
191                         unsigned sem = get_attribute_semantic(*a);
192                         glDisableVertexAttribArray(sem);
193                         glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
194                 }
195                 for(const unsigned char *a=inst_format.begin(); a!=inst_format.end(); ++a)
196                 {
197                         unsigned sem = get_attribute_semantic(*a);
198                         glDisableVertexAttribArray(sem);
199                         glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
200                 }
201
202                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
203         }
204
205         vertex_array = 0;
206         vertex_format = VertexFormat();
207         inst_array = 0;
208         inst_format = VertexFormat();
209         index_buffer = 0;
210 }
211
212 void VertexSetup::set_debug_name(const string &name)
213 {
214 #ifdef DEBUG
215         if(KHR_debug)
216                 glObjectLabel(GL_VERTEX_ARRAY, id, name.size(), name.c_str());
217 #else
218         (void)name;
219 #endif
220 }
221
222 } // namespace GL
223 } // namespace Msp