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