Add support for integer vertex attributes
[libs/gl.git] / source / core / vertexsetup.cpp
index 8498e043dde57d48c758e4a83957caf2f77e384b..81d2261e6aa78b55b3240e231ea72b25232bea60 100644 (file)
@@ -5,6 +5,7 @@
 #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"
@@ -45,6 +46,8 @@ void VertexSetup::set_format(const VertexFormat &vfmt)
        if(!vertex_format.empty())
                throw invalid_operation("VertexSetup::set_format");
 
+       require_format(vfmt);
+
        vertex_format = vfmt;
 }
 
@@ -55,6 +58,9 @@ void VertexSetup::set_format_instanced(const VertexFormat &vfmt, const VertexFor
        if(!vertex_format.empty())
                throw invalid_operation("VertexSetup::set_format");
 
+       require_format(vfmt);
+       require_format(ifmt);
+
        vertex_format = vfmt;
        inst_format = ifmt;
 }
@@ -108,6 +114,16 @@ bool VertexSetup::verify_format(const VertexFormat &fmt)
        return true;
 }
 
+void VertexSetup::require_format(const VertexFormat &fmt)
+{
+       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() const
 {
        static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
@@ -146,17 +162,24 @@ void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding
        for(const UInt16 *a=fmt.begin(); a!=fmt.end(); ++a)
        {
                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)
                {
-                       glVertexArrayAttribFormat(id, sem, cc, type, true, offset);
+                       if(integer)
+                               glVertexArrayAttribIFormat(id, sem, cc, type, offset);
+                       else
+                               glVertexArrayAttribFormat(id, sem, cc, type, true, offset);
                        glVertexArrayAttribBinding(id, sem, binding);
                        glEnableVertexArrayAttrib(id, sem);
                }
                else
                {
-                       glVertexAttribPointer(sem, cc, type, true, stride, reinterpret_cast<void *>(offset));
+                       if(integer)
+                               glVertexAttribIPointer(sem, cc, type, stride, reinterpret_cast<void *>(offset));
+                       else
+                               glVertexAttribPointer(sem, cc, type, true, stride, reinterpret_cast<void *>(offset));
                        if(ARB_instanced_arrays)
                                glVertexAttribDivisor(sem, divisor);
                        glEnableVertexAttribArray(sem);