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>
11 #include "vertexarray.h"
12 #include "vertexformat.h"
13 #include "vertexsetup.h"
14 #include "vertexsetup_backend.h"
21 OpenGLVertexSetup::OpenGLVertexSetup()
23 static Require req(ARB_vertex_array_object);
24 if(ARB_direct_state_access)
25 glCreateVertexArrays(1, &id);
27 glGenVertexArrays(1, &id);
30 OpenGLVertexSetup::~OpenGLVertexSetup()
32 glDeleteVertexArrays(1, &id);
35 void OpenGLVertexSetup::require_format(const VertexFormat &fmt, bool instanced)
37 if(any_of(fmt.begin(), fmt.end(), is_integer_attribute))
38 static Require _req(EXT_gpu_shader4);
40 static Require req(ARB_instanced_arrays);
43 void OpenGLVertexSetup::update(unsigned mask) const
45 static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
47 if(mask&VertexSetup::VERTEX_ARRAY)
48 update_vertex_array(*static_cast<const VertexSetup *>(this)->vertex_array, 0, 0, direct);
50 if(mask&VertexSetup::INSTANCE_ARRAY)
51 update_vertex_array(*static_cast<const VertexSetup *>(this)->inst_array, 1, 1, direct);
53 if(mask&VertexSetup::INDEX_BUFFER)
55 unsigned buf_id = static_cast<const VertexSetup *>(this)->index_buffer->id;
57 glVertexArrayElementBuffer(id, buf_id);
59 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buf_id);
63 void OpenGLVertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
67 OpenGLBuffer::unbind_scratch();
68 glBindBuffer(GL_ARRAY_BUFFER, array.get_buffer()->id);
71 const VertexFormat &fmt = array.get_format();
72 unsigned stride = fmt.stride();
75 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->id, 0, stride);
76 glVertexArrayBindingDivisor(id, binding, divisor);
80 for(VertexAttribute a: fmt)
82 unsigned sem = get_attribute_semantic(a);
83 bool integer = is_integer_attribute(a);
84 GLenum type = get_gl_type(get_attribute_source_type(a));
85 unsigned cc = get_attribute_component_count(a);
89 glVertexArrayAttribIFormat(id, sem, cc, type, offset);
91 glVertexArrayAttribFormat(id, sem, cc, type, true, offset);
92 glVertexArrayAttribBinding(id, sem, binding);
93 glEnableVertexArrayAttrib(id, sem);
98 glVertexAttribIPointer(sem, cc, type, stride, reinterpret_cast<void *>(offset));
100 glVertexAttribPointer(sem, cc, type, true, stride, reinterpret_cast<void *>(offset));
101 if(ARB_instanced_arrays)
102 glVertexAttribDivisor(sem, divisor);
103 glEnableVertexAttribArray(sem);
105 offset += get_attribute_size(a);
109 glBindBuffer(GL_ARRAY_BUFFER, 0);
112 void OpenGLVertexSetup::unload()
114 if(ARB_direct_state_access)
116 glVertexArrayVertexBuffer(id, 0, 0, 0, 0);
117 glVertexArrayVertexBuffer(id, 1, 0, 0, 0);
118 glVertexArrayElementBuffer(id, 0);
122 glBindVertexArray(id);
123 glBindBuffer(GL_ARRAY_BUFFER, 0);
125 for(VertexAttribute a: static_cast<const VertexSetup *>(this)->vertex_format)
127 unsigned sem = get_attribute_semantic(a);
128 glDisableVertexAttribArray(sem);
129 glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
131 for(VertexAttribute a: static_cast<const VertexSetup *>(this)->inst_format)
133 unsigned sem = get_attribute_semantic(a);
134 glDisableVertexAttribArray(sem);
135 glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
138 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
142 void OpenGLVertexSetup::set_debug_name(const string &name)
146 glObjectLabel(GL_VERTEX_ARRAY, id, name.size(), name.c_str());