]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/vertexsetup.cpp
Add support for integer vertex attributes
[libs/gl.git] / source / core / vertexsetup.cpp
index d50406a1ab70ce44e7caf645d46009a308c8e86f..81d2261e6aa78b55b3240e231ea72b25232bea60 100644 (file)
@@ -5,9 +5,13 @@
 #include <msp/gl/extensions/arb_vertex_attrib_binding.h>
 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
 #include <msp/gl/extensions/arb_vertex_shader.h>
+#include <msp/gl/extensions/ext_gpu_shader4.h>
+#include <msp/gl/extensions/khr_debug.h>
 #include "buffer.h"
+#include "deviceinfo.h"
 #include "error.h"
 #include "gl.h"
+#include "misc.h"
 #include "vertexarray.h"
 #include "vertexsetup.h"
 
@@ -20,7 +24,8 @@ VertexSetup::VertexSetup():
        dirty(0),
        vertex_array(0),
        inst_array(0),
-       index_buffer(0)
+       index_buffer(0),
+       index_type(UNSIGNED_SHORT)
 {
        static Require req(ARB_vertex_array_object);
        if(ARB_direct_state_access)
@@ -31,112 +36,122 @@ VertexSetup::VertexSetup():
 
 VertexSetup::~VertexSetup()
 {
-       if(current()==this)
-               unbind();
        glDeleteVertexArrays(1, &id);
 }
 
+void VertexSetup::set_format(const VertexFormat &vfmt)
+{
+       if(!verify_format(vfmt))
+               throw invalid_argument("VertexSetup::set_format");
+       if(!vertex_format.empty())
+               throw invalid_operation("VertexSetup::set_format");
+
+       require_format(vfmt);
+
+       vertex_format = vfmt;
+}
+
+void VertexSetup::set_format_instanced(const VertexFormat &vfmt, const VertexFormat &ifmt)
+{
+       if(!verify_format(vfmt) || !verify_format(ifmt))
+               throw invalid_argument("VertexSetup::set_format");
+       if(!vertex_format.empty())
+               throw invalid_operation("VertexSetup::set_format");
+
+       require_format(vfmt);
+       require_format(ifmt);
+
+       vertex_format = vfmt;
+       inst_format = ifmt;
+}
+
 void VertexSetup::set_vertex_array(const VertexArray &a)
 {
+       if(vertex_format.empty())
+               throw invalid_operation("VertexSetup::set_vertex_array");
+       if(a.get_format()!=vertex_format)
+               throw incompatible_data("VertexSetup::set_vertex_array");
        if(!a.get_buffer())
                throw invalid_argument("VertexSetup::set_vertex_array");
 
        vertex_array = &a;
-       update(get_update_mask(VERTEX_ARRAY, vertex_format, *vertex_array));
-       vertex_format = vertex_array->get_format();
+       dirty |= VERTEX_ARRAY;
 }
 
-void VertexSetup::set_instance_array(const VertexArray *a)
+void VertexSetup::set_instance_array(const VertexArray &a)
 {
-       if(a)
-       {
-               if(!a->get_buffer())
-                       throw invalid_argument("VertexSetup::set_instance_array");
+       if(inst_format.empty())
+               throw invalid_operation("VertexSetup::set_instance_array");
+       if(a.get_format()!=inst_format)
+               throw incompatible_data("VertexSetup::set_instance_array");
+       if(!a.get_buffer())
+               throw invalid_argument("VertexSetup::set_instance_array");
 
-               static Require req(ARB_instanced_arrays);
-       }
+       static Require req(ARB_instanced_arrays);
 
-       inst_array = a;
-       update(get_update_mask(INSTANCE_ARRAY, inst_format, *inst_array));
-       inst_format = inst_array->get_format();
+       inst_array = &a;
+       dirty |= INSTANCE_ARRAY;
 }
 
-void VertexSetup::set_index_buffer(const Buffer &ibuf)
+void VertexSetup::set_index_buffer(const Buffer &ibuf, DataType itype)
 {
        index_buffer = &ibuf;
-       update(INDEX_BUFFER);
+       index_type = itype;
+       dirty |= INDEX_BUFFER;
 }
 
-void VertexSetup::refresh()
+bool VertexSetup::verify_format(const VertexFormat &fmt)
 {
-       if(vertex_array && vertex_array->get_format()!=vertex_format)
-               set_vertex_array(*vertex_array);
+       if(fmt.empty())
+               return false;
 
-       if(inst_array && inst_array->get_format()!=inst_format)
-               set_instance_array(inst_array);
-}
+       unsigned max_attribs = Limits::get_global().max_vertex_attributes;
 
-unsigned VertexSetup::get_attribs(const VertexFormat &fmt)
-{
-       unsigned mask = 0;
-       for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
-       {
-               unsigned t = get_component_type(*c);
-               if(t>=get_component_type(ATTRIB1))
-                       t -= get_component_type(ATTRIB1);
-               mask |= 1<<t;
-       }
-       return mask;
+       for(const UInt16 *a=fmt.begin(); a!=fmt.end(); ++a)
+               if(get_attribute_semantic(*a)>=max_attribs)
+                       return false;
+
+       return true;
 }
 
-unsigned VertexSetup::get_update_mask(unsigned base, const VertexFormat &cur_fmt, const VertexArray &new_array)
+void VertexSetup::require_format(const VertexFormat &fmt)
 {
-       unsigned unused = get_attribs(cur_fmt)&~get_attribs(new_array.get_format());
-       return base | (unused ? UNUSED_ATTRIBS | (unused<<ATTRIB_SHIFT) : 0);
+       bool has_int = false;
+       for(const UInt16 *a=fmt.begin(); a!=fmt.end(); ++a)
+               has_int = has_int | is_integer_attribute(*a);
+
+       if(has_int)
+               static Require _req(EXT_gpu_shader4);
 }
 
-void VertexSetup::update(unsigned mask) const
+void VertexSetup::update() const
 {
        static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
-       if(!direct && current()!=this)
-       {
-               dirty |= mask;
-               return;
-       }
 
-       if(mask&UNUSED_ATTRIBS)
-       {
-               for(unsigned i=0, am=mask>>ATTRIB_SHIFT; am; ++i, am>>=1)
-                       if(am&1)
-                       {
-                               if(direct)
-                                       glDisableVertexArrayAttrib(id, i);
-                               else
-                                       glDisableVertexAttribArray(i);
-                       }
-       }
-
-       if(mask&VERTEX_ARRAY)
+       if(dirty&VERTEX_ARRAY)
                update_vertex_array(*vertex_array, 0, 0, direct);
 
-       if((mask&INSTANCE_ARRAY) && inst_array)
+       if(dirty&INSTANCE_ARRAY)
                update_vertex_array(*inst_array, 1, 1, direct);
 
-       if(mask&INDEX_BUFFER)
+       if(dirty&INDEX_BUFFER)
        {
                if(direct)
                        glVertexArrayElementBuffer(id, index_buffer->get_id());
                else
-                       glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
+                       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
        }
+
+       dirty = 0;
 }
 
 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
 {
-       Conditional<Bind> bind_vbuf(!direct, array.get_buffer(), ARRAY_BUFFER);
+       if(!direct)
+               glBindBuffer(GL_ARRAY_BUFFER, array.get_buffer()->get_id());
 
        const VertexFormat &fmt = array.get_format();
-       unsigned stride = get_stride(fmt)*sizeof(float);
+       unsigned stride = fmt.stride();
        if(direct)
        {
                glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
@@ -144,58 +159,82 @@ void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding
        }
 
        unsigned offset = 0;
-       for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
+       for(const UInt16 *a=fmt.begin(); a!=fmt.end(); ++a)
        {
-               unsigned t = get_component_type(*c);
-               if(t>=get_component_type(ATTRIB1))
-                       t -= get_component_type(ATTRIB1);
-               unsigned sz = get_component_size(*c);
+               unsigned sem = get_attribute_semantic(*a);
+               bool integer = is_integer_attribute(*a);
+               GLenum type = get_gl_type(get_attribute_source_type(*a));
+               unsigned cc = get_attribute_component_count(*a);
                if(direct)
                {
-                       if(*c==COLOR4_UBYTE)
-                               glVertexArrayAttribFormat(id, t, 4, GL_UNSIGNED_BYTE, true, offset);
+                       if(integer)
+                               glVertexArrayAttribIFormat(id, sem, cc, type, offset);
                        else
-                               glVertexArrayAttribFormat(id, t, sz, GL_FLOAT, false, offset);
-                       glVertexArrayAttribBinding(id, t, binding);
-                       glEnableVertexArrayAttrib(id, t);
+                               glVertexArrayAttribFormat(id, sem, cc, type, true, offset);
+                       glVertexArrayAttribBinding(id, sem, binding);
+                       glEnableVertexArrayAttrib(id, sem);
                }
                else
                {
-                       if(*c==COLOR4_UBYTE)
-                               glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
+                       if(integer)
+                               glVertexAttribIPointer(sem, cc, type, stride, reinterpret_cast<void *>(offset));
                        else
-                               glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
+                               glVertexAttribPointer(sem, cc, type, true, stride, reinterpret_cast<void *>(offset));
                        if(ARB_instanced_arrays)
-                               glVertexAttribDivisor(t, divisor);
-                       glEnableVertexAttribArray(t);
+                               glVertexAttribDivisor(sem, divisor);
+                       glEnableVertexAttribArray(sem);
                }
-               offset += sz*sizeof(float);
+               offset += get_attribute_size(*a);
        }
+
+       if(!direct)
+               glBindBuffer(GL_ARRAY_BUFFER, 0);
 }
 
-void VertexSetup::bind() const
+void VertexSetup::unload()
 {
-       if(!vertex_array || !index_buffer)
-               throw invalid_operation("VertexSetup::bind");
-
-       if(set_current(this))
+       if(ARB_direct_state_access)
+       {
+               glVertexArrayVertexBuffer(id, 0, 0, 0, 0);
+               glVertexArrayVertexBuffer(id, 1, 0, 0, 0);
+               glVertexArrayElementBuffer(id, 0);
+       }
+       else
        {
-               vertex_array->refresh();
-               if(inst_array)
-                       inst_array->refresh();
                glBindVertexArray(id);
-               if(dirty)
+               glBindBuffer(GL_ARRAY_BUFFER, 0);
+
+               for(const UInt16 *a=vertex_format.begin(); a!=vertex_format.end(); ++a)
                {
-                       update(dirty);
-                       dirty = 0;
+                       unsigned sem = get_attribute_semantic(*a);
+                       glDisableVertexAttribArray(sem);
+                       glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
                }
+               for(const UInt16 *a=inst_format.begin(); a!=inst_format.end(); ++a)
+               {
+                       unsigned sem = get_attribute_semantic(*a);
+                       glDisableVertexAttribArray(sem);
+                       glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
+               }
+
+               glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        }
+
+       vertex_array = 0;
+       vertex_format = VertexFormat();
+       inst_array = 0;
+       inst_format = VertexFormat();
+       index_buffer = 0;
 }
 
-void VertexSetup::unbind()
+void VertexSetup::set_debug_name(const string &name)
 {
-       if(set_current(0))
-               glBindVertexArray(0);
+#ifdef DEBUG
+       if(KHR_debug)
+               glObjectLabel(GL_VERTEX_ARRAY, id, name.size(), name.c_str());
+#else
+       (void)name;
+#endif
 }
 
 } // namespace GL