]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/vertexsetup_backend.cpp
Add support for padding in vertex formats
[libs/gl.git] / source / backends / opengl / vertexsetup_backend.cpp
1 #include <algorithm>
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/ext_gpu_shader4.h>
9 #include <msp/gl/extensions/khr_debug.h>
10 #include "buffer.h"
11 #include "vertexarray.h"
12 #include "vertexformat.h"
13 #include "vertexsetup.h"
14 #include "vertexsetup_backend.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GL {
20
21 OpenGLVertexSetup::OpenGLVertexSetup()
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 OpenGLVertexSetup::OpenGLVertexSetup(OpenGLVertexSetup &&other):
31         id(other.id)
32 {
33         other.id = 0;
34 }
35
36 OpenGLVertexSetup::~OpenGLVertexSetup()
37 {
38         if(id)
39                 glDeleteVertexArrays(1, &id);
40 }
41
42 void OpenGLVertexSetup::require_format(const VertexFormat &fmt, bool instanced)
43 {
44         if(any_of(fmt.begin(), fmt.end(), is_integer_attribute))
45                 static Require _req(EXT_gpu_shader4);
46         if(instanced)
47                 static Require req(ARB_instanced_arrays);
48 }
49
50 void OpenGLVertexSetup::update(unsigned mask) const
51 {
52         static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
53
54         if(mask&VertexSetup::VERTEX_ARRAY)
55                 update_vertex_array(*static_cast<const VertexSetup *>(this)->vertex_array, 0, 0, direct);
56
57         if(mask&VertexSetup::INSTANCE_ARRAY)
58                 update_vertex_array(*static_cast<const VertexSetup *>(this)->inst_array, 1, 1, direct);
59
60         if(mask&VertexSetup::INDEX_BUFFER)
61         {
62                 unsigned buf_id = static_cast<const VertexSetup *>(this)->index_buffer->id;
63                 if(direct)
64                         glVertexArrayElementBuffer(id, buf_id);
65                 else
66                         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buf_id);
67         }
68 }
69
70 void OpenGLVertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
71 {
72         if(!direct)
73         {
74                 OpenGLBuffer::unbind_scratch();
75                 glBindBuffer(GL_ARRAY_BUFFER, array.get_buffer()->id);
76         }
77
78         const VertexFormat &fmt = array.get_format();
79         unsigned stride = fmt.stride();
80         if(direct)
81         {
82                 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->id, 0, stride);
83                 glVertexArrayBindingDivisor(id, binding, divisor);
84         }
85
86         unsigned offset = 0;
87         for(VertexAttribute a: fmt)
88         {
89                 unsigned sem = get_attribute_semantic(a);
90                 if(!is_padding(a))
91                 {
92                         bool integer = is_integer_attribute(a);
93                         GLenum type = get_gl_type(get_attribute_source_type(a));
94                         unsigned cc = get_attribute_component_count(a);
95                         if(direct)
96                         {
97                                 if(integer)
98                                         glVertexArrayAttribIFormat(id, sem, cc, type, offset);
99                                 else
100                                         glVertexArrayAttribFormat(id, sem, cc, type, true, offset);
101                                 glVertexArrayAttribBinding(id, sem, binding);
102                                 glEnableVertexArrayAttrib(id, sem);
103                         }
104                         else
105                         {
106                                 if(integer)
107                                         glVertexAttribIPointer(sem, cc, type, stride, reinterpret_cast<void *>(offset));
108                                 else
109                                         glVertexAttribPointer(sem, cc, type, true, stride, reinterpret_cast<void *>(offset));
110                                 if(ARB_instanced_arrays)
111                                         glVertexAttribDivisor(sem, divisor);
112                                 glEnableVertexAttribArray(sem);
113                         }
114                 }
115                 offset += get_attribute_size(a);
116         }
117
118         if(!direct)
119                 glBindBuffer(GL_ARRAY_BUFFER, 0);
120 }
121
122 void OpenGLVertexSetup::unload()
123 {
124         if(ARB_direct_state_access)
125         {
126                 glVertexArrayVertexBuffer(id, 0, 0, 0, 0);
127                 glVertexArrayVertexBuffer(id, 1, 0, 0, 0);
128                 glVertexArrayElementBuffer(id, 0);
129         }
130         else
131         {
132                 glBindVertexArray(id);
133                 glBindBuffer(GL_ARRAY_BUFFER, 0);
134
135                 for(VertexAttribute a: static_cast<const VertexSetup *>(this)->vertex_format)
136                         if(!is_padding(a))
137                         {
138                                 unsigned sem = get_attribute_semantic(a);
139                                 glDisableVertexAttribArray(sem);
140                                 glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
141                         }
142                 for(VertexAttribute a: static_cast<const VertexSetup *>(this)->inst_format)
143                         if(!is_padding(a))
144                         {
145                                 unsigned sem = get_attribute_semantic(a);
146                                 glDisableVertexAttribArray(sem);
147                                 glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
148                         }
149
150                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
151         }
152 }
153
154 void OpenGLVertexSetup::set_debug_name(const string &name)
155 {
156 #ifdef DEBUG
157         if(KHR_debug)
158                 glObjectLabel(GL_VERTEX_ARRAY, id, name.size(), name.c_str());
159 #else
160         (void)name;
161 #endif
162 }
163
164 } // namespace GL
165 } // namespace Msp