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