]> git.tdb.fi Git - libs/gl.git/blob - source/vertexsetup.cpp
Require vertex arrays to have a buffer when adding them to VertexSetup
[libs/gl.git] / source / 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(VERTEX_ARRAY);
46 }
47
48 void VertexSetup::set_instance_array(const VertexArray *a)
49 {
50         if(a)
51         {
52                 if(!a->get_buffer())
53                         throw invalid_argument("VertexSetup::set_instance_array");
54
55                 static Require req(ARB_instanced_arrays);
56         }
57
58         inst_array = a;
59         update(INSTANCE_ARRAY);
60 }
61
62 void VertexSetup::set_index_buffer(const Buffer &ibuf)
63 {
64         index_buffer = &ibuf;
65         update(INDEX_BUFFER);
66 }
67
68 void VertexSetup::update(unsigned mask) const
69 {
70         static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
71         if(!direct && current()!=this)
72         {
73                 dirty |= mask;
74                 return;
75         }
76
77         if(mask&VERTEX_ARRAY)
78                 update_vertex_array(*vertex_array, 0, 0, direct);
79
80         if(mask&INSTANCE_ARRAY)
81         {
82                 if(inst_array)
83                         update_vertex_array(*inst_array, 1, 1, direct);
84         }
85
86         if(mask&INDEX_BUFFER)
87         {
88                 if(direct)
89                         glVertexArrayElementBuffer(id, index_buffer->get_id());
90                 else
91                         glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
92         }
93 }
94
95 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
96 {
97         Conditional<Bind> bind_vbuf(!direct, array.get_buffer(), ARRAY_BUFFER);
98
99         const VertexFormat &fmt = array.get_format();
100         unsigned stride = get_stride(fmt)*sizeof(float);
101         if(direct)
102         {
103                 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
104                 glVertexArrayBindingDivisor(id, binding, divisor);
105         }
106
107         unsigned offset = 0;
108         for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
109         {
110                 unsigned t = get_component_type(*c);
111                 if(t>=get_component_type(ATTRIB1))
112                         t -= get_component_type(ATTRIB1);
113                 unsigned sz = get_component_size(*c);
114                 if(direct)
115                 {
116                         if(*c==COLOR4_UBYTE)
117                                 glVertexArrayAttribFormat(id, t, 4, GL_UNSIGNED_BYTE, true, offset);
118                         else
119                                 glVertexArrayAttribFormat(id, t, sz, GL_FLOAT, false, offset);
120                         glVertexArrayAttribBinding(id, t, binding);
121                         glEnableVertexArrayAttrib(id, t);
122                 }
123                 else
124                 {
125                         if(*c==COLOR4_UBYTE)
126                                 glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
127                         else
128                                 glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
129                         glVertexAttribDivisor(t, divisor);
130                         glEnableVertexAttribArray(t);
131                 }
132                 offset += sz*sizeof(float);
133         }
134 }
135
136 void VertexSetup::bind() const
137 {
138         if(!vertex_array || !index_buffer)
139                 throw invalid_operation("VertexSetup::bind");
140
141         if(set_current(this))
142         {
143                 vertex_array->refresh();
144                 if(inst_array)
145                         inst_array->refresh();
146                 glBindVertexArray(id);
147                 if(dirty)
148                 {
149                         update(dirty);
150                         dirty = 0;
151                 }
152         }
153 }
154
155 void VertexSetup::unbind()
156 {
157         if(set_current(0))
158                 glBindVertexArray(0);
159 }
160
161 } // namespace GL
162 } // namespace Msp