]> git.tdb.fi Git - libs/gl.git/blob - source/vertexsetup.cpp
Check the flat qualifier from the correct member
[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 "gl.h"
10 #include "vertexarray.h"
11 #include "vertexsetup.h"
12
13 namespace Msp {
14 namespace GL {
15
16 VertexSetup::VertexSetup():
17         dirty(0),
18         vertex_array(0),
19         inst_array(0),
20         index_buffer(0)
21 {
22         static Require req(ARB_vertex_array_object);
23         if(ARB_direct_state_access)
24                 glCreateVertexArrays(1, &id);
25         else
26                 glGenVertexArrays(1, &id);
27 }
28
29 VertexSetup::~VertexSetup()
30 {
31         if(current()==this)
32                 unbind();
33         glDeleteVertexArrays(1, &id);
34 }
35
36 void VertexSetup::set_vertex_array(const VertexArray &a)
37 {
38         vertex_array = &a;
39         update(VERTEX_ARRAY);
40 }
41
42 void VertexSetup::set_instance_array(const VertexArray *a)
43 {
44         if(a)
45                 static Require req(ARB_instanced_arrays);
46
47         inst_array = a;
48         update(INSTANCE_ARRAY);
49 }
50
51 void VertexSetup::set_index_buffer(const Buffer &ibuf)
52 {
53         index_buffer = &ibuf;
54         update(INDEX_BUFFER);
55 }
56
57 void VertexSetup::update(unsigned mask) const
58 {
59         static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
60         if(!direct && current()!=this)
61         {
62                 dirty |= mask;
63                 return;
64         }
65
66         if(mask&VERTEX_ARRAY)
67                 update_vertex_array(*vertex_array, 0, 0, direct);
68
69         if(mask&INSTANCE_ARRAY)
70         {
71                 if(inst_array)
72                         update_vertex_array(*inst_array, 1, 1, direct);
73         }
74
75         if(mask&INDEX_BUFFER)
76         {
77                 if(direct)
78                         glVertexArrayElementBuffer(id, index_buffer->get_id());
79                 else
80                         glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
81         }
82 }
83
84 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
85 {
86         Conditional<Bind> bind_vbuf(!direct, array.get_buffer(), ARRAY_BUFFER);
87
88         const VertexFormat &fmt = array.get_format();
89         unsigned stride = get_stride(fmt)*sizeof(float);
90         if(direct)
91         {
92                 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
93                 glVertexArrayBindingDivisor(id, binding, divisor);
94         }
95
96         unsigned offset = 0;
97         for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
98         {
99                 unsigned t = get_component_type(*c);
100                 if(t>=get_component_type(ATTRIB1))
101                         t -= get_component_type(ATTRIB1);
102                 unsigned sz = get_component_size(*c);
103                 if(direct)
104                 {
105                         if(*c==COLOR4_UBYTE)
106                                 glVertexArrayAttribFormat(id, t, 4, GL_UNSIGNED_BYTE, true, offset);
107                         else
108                                 glVertexArrayAttribFormat(id, t, sz, GL_FLOAT, false, offset);
109                         glVertexArrayAttribBinding(id, t, binding);
110                         glEnableVertexArrayAttrib(id, t);
111                 }
112                 else
113                 {
114                         if(*c==COLOR4_UBYTE)
115                                 glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
116                         else
117                                 glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
118                         glVertexAttribDivisor(t, divisor);
119                         glEnableVertexAttribArray(t);
120                 }
121                 offset += sz*sizeof(float);
122         }
123 }
124
125 void VertexSetup::bind() const
126 {
127         if(set_current(this))
128         {
129                 glBindVertexArray(id);
130                 if(dirty)
131                 {
132                         update(dirty);
133                         dirty = 0;
134                 }
135         }
136 }
137
138 void VertexSetup::unbind()
139 {
140         if(set_current(0))
141                 glBindVertexArray(0);
142 }
143
144 } // namespace GL
145 } // namespace Msp