]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/program.cpp
Add an abstraction for queries
[libs/gl.git] / source / core / program.cpp
index 4e09038074613d64f0d5559127e51df305a156c2..b1516e2dc329c95d6d26f7cc1ce6d575f990ef45 100644 (file)
@@ -1,8 +1,5 @@
 #include <cstring>
-#include <set>
 #include <msp/core/algorithm.h>
-#include <msp/core/maputils.h>
-#include <msp/core/raii.h>
 #include <msp/gl/extensions/arb_es2_compatibility.h>
 #include <msp/gl/extensions/arb_fragment_shader.h>
 #include <msp/gl/extensions/arb_gl_spirv.h>
 #include <msp/gl/extensions/khr_debug.h>
 #include <msp/gl/extensions/nv_non_square_matrices.h>
 #include <msp/io/print.h>
-#include "buffer.h"
 #include "error.h"
 #include "program.h"
-#include "resources.h"
 #include "glsl/compiler.h"
 
 using namespace std;
 
+namespace {
+
+template<typename T, void (*&func)(GLint, GLsizei, const T *)>
+void uniform_wrapper(unsigned index, unsigned count, const void *data)
+{
+       func(index, count, static_cast<const T *>(data));
+}
+
+template<typename T, void (*&func)(GLint, GLsizei, GLboolean, const T *)>
+void uniform_matrix_wrapper(unsigned index, unsigned count, const void *data)
+{
+       func(index, count, false, static_cast<const T *>(data));
+}
+
+}
+
 namespace Msp {
 namespace GL {
 
@@ -344,12 +355,62 @@ void Program::query_uniforms()
                {
                        u.location = glGetUniformLocation(id, u.name.c_str());
                        u.block = &default_block;
+                       u.array_stride = get_type_size(u.type);
+                       if(is_matrix(u.type))
+                               u.matrix_stride = get_type_size(get_matrix_column_type(u.type));
                        default_block.uniforms.push_back(&u);
 
-                       if(is_image(u.type) && u.location>=0)
-                               glGetUniformiv(id, u.location, &u.binding);
+                       if(u.location>=0)
+                       {
+                               UniformCall::FuncPtr func = 0;
+                               if(u.type==FLOAT)
+                                       func = &uniform_wrapper<float, glUniform1fv>;
+                               else if(u.type==FLOAT_VEC2)
+                                       func = &uniform_wrapper<float, glUniform2fv>;
+                               else if(u.type==FLOAT_VEC3)
+                                       func = &uniform_wrapper<float, glUniform3fv>;
+                               else if(u.type==FLOAT_VEC4)
+                                       func = &uniform_wrapper<float, glUniform4fv>;
+                               else if(u.type==INT)
+                                       func = &uniform_wrapper<int, glUniform1iv>;
+                               else if(u.type==INT_VEC2)
+                                       func = &uniform_wrapper<int, glUniform2iv>;
+                               else if(u.type==INT_VEC3)
+                                       func = &uniform_wrapper<int, glUniform3iv>;
+                               else if(u.type==INT_VEC4)
+                                       func = &uniform_wrapper<int, glUniform4iv>;
+                               else if(u.type==FLOAT_MAT2)
+                                       func = &uniform_matrix_wrapper<float, glUniformMatrix2fv>;
+                               else if(u.type==FLOAT_MAT3)
+                                       func = &uniform_matrix_wrapper<float, glUniformMatrix3fv>;
+                               else if(u.type==FLOAT_MAT4)
+                                       func = &uniform_matrix_wrapper<float, glUniformMatrix4fv>;
+                               else if(u.type==FLOAT_MAT2x3)
+                                       func = &uniform_matrix_wrapper<float, glUniformMatrix2x3fv>;
+                               else if(u.type==FLOAT_MAT3x2)
+                                       func = &uniform_matrix_wrapper<float, glUniformMatrix3x2fv>;
+                               else if(u.type==FLOAT_MAT2x4)
+                                       func = &uniform_matrix_wrapper<float, glUniformMatrix2x4fv>;
+                               else if(u.type==FLOAT_MAT4x2)
+                                       func = &uniform_matrix_wrapper<float, glUniformMatrix4x2fv>;
+                               else if(u.type==FLOAT_MAT3x4)
+                                       func = &uniform_matrix_wrapper<float, glUniformMatrix3x4fv>;
+                               else if(u.type==FLOAT_MAT4x3)
+                                       func = &uniform_matrix_wrapper<float, glUniformMatrix4x3fv>;
+                               else if(is_image(u.type))
+                                       glGetUniformiv(id, u.location, &u.binding);
+
+                               if(func)
+                                       uniform_calls.push_back(UniformCall(u.location, u.array_size, func));
+                       }
                }
 
+       default_block.sort_uniforms();
+       if(!default_block.uniforms.empty())
+       {
+               const ReflectData::UniformInfo &uni = *default_block.uniforms.back();
+               default_block.data_size = uni.location*16+uni.array_size*get_type_size(uni.type);
+       }
        default_block.update_layout_hash();
        reflect_data.update_layout_hash();
 }