]> git.tdb.fi Git - libs/gl.git/blobdiff - source/backends/opengl/program_backend.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / backends / opengl / program_backend.cpp
index d78a5b4206505ee51c9b3ecce1a201579bfe18ee..3ebb7da1292d8de854a09131a3eb3ece18eb127b 100644 (file)
@@ -1,5 +1,6 @@
 #include <cstring>
 #include <msp/core/algorithm.h>
+#include <msp/gl/extensions/arb_compute_shader.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>
@@ -7,11 +8,13 @@
 #include <msp/gl/extensions/arb_separate_shader_objects.h>
 #include <msp/gl/extensions/arb_shader_objects.h>
 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
+#include <msp/gl/extensions/arb_tessellation_shader.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 <msp/gl/extensions/nv_non_square_matrices.h>
 #include <msp/io/print.h>
+#include "device.h"
 #include "error.h"
 #include "program.h"
 #include "program_backend.h"
@@ -45,6 +48,17 @@ OpenGLProgram::OpenGLProgram()
        id = glCreateProgram();
 }
 
+OpenGLProgram::OpenGLProgram(OpenGLProgram &&other):
+       id(other.id),
+       linked(other.linked),
+       uniform_calls(move(other.uniform_calls)),
+       debug_name(move(other.debug_name))
+{
+       move(other.stage_ids, other.stage_ids+MAX_STAGES, stage_ids);
+       other.id = 0;
+       fill(other.stage_ids, other.stage_ids+MAX_STAGES, 0);
+}
+
 OpenGLProgram::~OpenGLProgram()
 {
        for(unsigned i=0; i<MAX_STAGES; ++i)
@@ -67,8 +81,11 @@ unsigned OpenGLProgram::add_stage(Stage type)
        switch(type)
        {
        case VERTEX: { static Require _req(ARB_vertex_shader); gl_type = GL_VERTEX_SHADER; } break;
+       case TESS_CONTROL: { static Require _req(ARB_tessellation_shader); gl_type = GL_TESS_CONTROL_SHADER; } break;
+       case TESS_EVAL: { static Require _req(ARB_tessellation_shader); gl_type = GL_TESS_EVALUATION_SHADER; } break;
        case GEOMETRY: { static Require _req(ARB_geometry_shader4); gl_type = GL_GEOMETRY_SHADER; } break;
        case FRAGMENT: { static Require _req(ARB_fragment_shader); gl_type = GL_FRAGMENT_SHADER; } break;
+       case COMPUTE: { static Require _req(ARB_compute_shader); gl_type = GL_COMPUTE_SHADER; } break;
        default: throw invalid_argument("OpenGLProgram::add_stage");
        }
 
@@ -87,9 +104,9 @@ unsigned OpenGLProgram::add_stage(Stage type)
        return stage_id;
 }
 
-void OpenGLProgram::add_glsl_stages(const GlslModule &mod, const map<string, int> &spec_values, TransientData &transient)
+void OpenGLProgram::add_glsl_stages(const GlslModule &mod, const map<string, int> &spec_values)
 {
-       SL::Compiler compiler;
+       SL::Compiler compiler(Device::get_current().get_info().glsl_features);
        compiler.set_source(mod.get_prepared_source(), "<module>");
        compiler.specialize(spec_values);
        compiler.compile(SL::Compiler::PROGRAM);
@@ -109,8 +126,11 @@ void OpenGLProgram::add_glsl_stages(const GlslModule &mod, const map<string, int
                switch(st)
                {
                case SL::Stage::VERTEX: stage_id = add_stage(VERTEX); break;
+               case SL::Stage::TESS_CONTROL: stage_id = add_stage(TESS_CONTROL); break;
+               case SL::Stage::TESS_EVAL: stage_id = add_stage(TESS_EVAL); break;
                case SL::Stage::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
                case SL::Stage::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
+               case SL::Stage::COMPUTE: stage_id = add_stage(COMPUTE); break;
                default: throw invalid_operation("OpenGLProgram::add_glsl_stages");
                }
 
@@ -134,11 +154,50 @@ void OpenGLProgram::add_glsl_stages(const GlslModule &mod, const map<string, int
                compile_glsl_stage(mod, stage_id);
        }
 
-       transient.textures = compiler.get_texture_bindings();
-       transient.blocks = compiler.get_uniform_block_bindings();
-
        ReflectData &rd = static_cast<Program *>(this)->reflect_data;
        rd.n_clip_distances = compiler.get_n_clip_distances();
+
+       link(mod);
+       query_uniforms();
+       query_attributes();
+       if(is_compute())
+       {
+               int wg_size[3];
+               glGetProgramiv(id, GL_COMPUTE_WORK_GROUP_SIZE, wg_size);
+               rd.compute_wg_size = LinAl::Vector<unsigned, 3>(wg_size[0], wg_size[1], wg_size[2]);
+       }
+
+       const map<string, unsigned> &block_bindings = compiler.get_uniform_block_bindings();
+       if(!block_bindings.empty())
+       {
+               for(unsigned i=0; i<rd.uniform_blocks.size(); ++i)
+               {
+                       auto j = block_bindings.find(rd.uniform_blocks[i].name);
+                       if(j!=block_bindings.end())
+                       {
+                               glUniformBlockBinding(id, i, j->second);
+                               rd.uniform_blocks[i].bind_point = j->second;
+                       }
+               }
+       }
+
+       const map<string, unsigned> &tex_bindings = compiler.get_texture_bindings();
+       if(!tex_bindings.empty())
+       {
+               if(!ARB_separate_shader_objects)
+                       glUseProgram(id);
+               for(const auto &kvp: tex_bindings)
+               {
+                       int location = static_cast<const Program *>(this)->get_uniform_location(kvp.first);
+                       if(location>=0)
+                       {
+                               if(ARB_separate_shader_objects)
+                                       glProgramUniform1i(id, location, kvp.second);
+                               else
+                                       glUniform1i(location, kvp.second);
+                       }
+               }
+       }
 }
 
 void OpenGLProgram::compile_glsl_stage(const GlslModule &mod, unsigned stage_id)
@@ -162,7 +221,7 @@ void OpenGLProgram::compile_glsl_stage(const GlslModule &mod, unsigned stage_id)
 #endif
 }
 
-void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values, TransientData &transient)
+void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values)
 {
        static Require _req(ARB_gl_spirv);
        static Require _req2(ARB_ES2_compatibility);
@@ -175,8 +234,11 @@ void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const map<string, i
                switch(e.stage)
                {
                case SpirVModule::VERTEX: stage_id = add_stage(VERTEX); break;
+               case SpirVModule::TESS_CONTROL: stage_id = add_stage(TESS_CONTROL); break;
+               case SpirVModule::TESS_EVAL: stage_id = add_stage(TESS_EVAL); break;
                case SpirVModule::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
                case SpirVModule::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
+               case SpirVModule::COMPUTE: stage_id = add_stage(COMPUTE); break;
                default: throw invalid_operation("OpenGLProgram::add_spirv_stages");
                }
 
@@ -201,7 +263,6 @@ void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const map<string, i
                {
                        spec_id_array.push_back(c.constant_id);
                        spec_value_array.push_back(i->second);
-                       transient.spec_values[c.constant_id] = i->second;
                }
        }
 
@@ -209,9 +270,11 @@ void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const map<string, i
        for(unsigned i=0; i<MAX_STAGES; ++i)
                if(stage_ids[i])
                        glSpecializeShader(stage_ids[i], j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]);
+
+       link(mod);
 }
 
-void OpenGLProgram::finalize(const Module &mod, TransientData &transient)
+void OpenGLProgram::link(const Module &mod)
 {
        glLinkProgram(id);
        int status = 0;
@@ -232,13 +295,6 @@ void OpenGLProgram::finalize(const Module &mod, TransientData &transient)
        if(!info_log.empty())
                IO::print("Program link info log:\n%s", info_log);
 #endif
-
-       if(mod.get_format()==Module::GLSL)
-       {
-               query_uniforms();
-               query_attributes();
-               apply_bindings(transient);
-       }
 }
 
 void OpenGLProgram::query_uniforms()
@@ -263,7 +319,7 @@ void OpenGLProgram::query_uniforms()
                        if(len>3 && !strcmp(name+len-3, "[0]"))
                                name[len-3] = 0;
 
-                       rd.uniforms.push_back(ReflectData::UniformInfo());
+                       rd.uniforms.emplace_back();
                        ReflectData::UniformInfo &info = rd.uniforms.back();
                        info.name = name;
                        info.tag = name;
@@ -285,7 +341,7 @@ void OpenGLProgram::query_uniforms()
                query_uniform_blocks(uniforms_by_index);
        }
 
-       rd.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
+       rd.uniform_blocks.emplace_back();
        ReflectData::UniformBlockInfo &default_block = rd.uniform_blocks.back();
 
        for(ReflectData::UniformInfo &u: rd.uniforms)
@@ -298,57 +354,8 @@ void OpenGLProgram::query_uniforms()
                                u.matrix_stride = get_type_size(get_matrix_column_type(u.type));
                        default_block.uniforms.push_back(&u);
 
-                       if(u.location>=0)
-                       {
-                               UniformCall::FuncPtr func = 0;
-                               if(is_image(u.type))
-                                       glGetUniformiv(id, u.location, &u.binding);
-                               else 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==UNSIGNED_INT)
-                                       func = &uniform_wrapper<unsigned, glUniform1uiv>;
-                               else if(u.type==UINT_VEC2)
-                                       func = &uniform_wrapper<unsigned, glUniform2uiv>;
-                               else if(u.type==UINT_VEC3)
-                                       func = &uniform_wrapper<unsigned, glUniform3uiv>;
-                               else if(u.type==UINT_VEC4)
-                                       func = &uniform_wrapper<unsigned, glUniform4uiv>;
-                               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>;
-
-                               if(func)
-                                       uniform_calls.push_back(UniformCall(u.location, u.array_size, func));
-                       }
+                       if(is_image(u.type) && u.location>=0)
+                               glGetUniformiv(id, u.location, &u.binding);
                }
 
        default_block.sort_uniforms();
@@ -369,7 +376,7 @@ void OpenGLProgram::query_uniform_blocks(const vector<ReflectData::UniformInfo *
                char name[128];
                int len;
                glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
-               rd.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
+               rd.uniform_blocks.emplace_back();
                ReflectData::UniformBlockInfo &info = rd.uniform_blocks.back();
                info.name = name;
 
@@ -446,7 +453,7 @@ void OpenGLProgram::query_attributes()
                        if(len>3 && !strcmp(name+len-3, "[0]"))
                                name[len-3] = 0;
 
-                       rd.attributes.push_back(ReflectData::AttributeInfo());
+                       rd.attributes.emplace_back();
                        ReflectData::AttributeInfo &info = rd.attributes.back();
                        info.name = name;
                        info.location = glGetAttribLocation(id, name);
@@ -454,33 +461,72 @@ void OpenGLProgram::query_attributes()
                        info.type = from_gl_type(type);
                }
        }
+
+       sort_member(rd.attributes, &ReflectData::AttributeInfo::name);
 }
 
-void OpenGLProgram::apply_bindings(const TransientData &transient)
+void OpenGLProgram::finalize_uniforms()
 {
        ReflectData &rd = static_cast<Program *>(this)->reflect_data;
 
-       for(unsigned i=0; i<rd.uniform_blocks.size(); ++i)
+       auto i = find_if(rd.uniform_blocks, [](const ReflectData::UniformBlockInfo &b){ return b.bind_point<0; });
+       if(i!=rd.uniform_blocks.end() && !i->uniforms.empty())
        {
-               auto j = transient.blocks.find(rd.uniform_blocks[i].name);
-               if(j!=transient.blocks.end())
-               {
-                       glUniformBlockBinding(id, i, j->second);
-                       rd.uniform_blocks[i].bind_point = j->second;
-               }
-       }
+               for(const ReflectData::UniformInfo *u: i->uniforms)
+                       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==UNSIGNED_INT)
+                                       func = &uniform_wrapper<unsigned, glUniform1uiv>;
+                               else if(u->type==UINT_VEC2)
+                                       func = &uniform_wrapper<unsigned, glUniform2uiv>;
+                               else if(u->type==UINT_VEC3)
+                                       func = &uniform_wrapper<unsigned, glUniform3uiv>;
+                               else if(u->type==UINT_VEC4)
+                                       func = &uniform_wrapper<unsigned, glUniform4uiv>;
+                               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>;
 
-       if(!ARB_separate_shader_objects)
-               glUseProgram(id);
-       for(const auto &kvp: transient.textures)
-       {
-               int location = static_cast<const Program *>(this)->get_uniform_location(kvp.first);
-               if(location>=0)
+                               if(func)
+                                       uniform_calls.push_back(UniformCall(u->location, u->array_size, func));
+                       }
+
+               if(i->data_size<=0)
                {
-                       if(ARB_separate_shader_objects)
-                               glProgramUniform1i(id, location, kvp.second);
-                       else
-                               glUniform1i(location, kvp.second);
+                       const ReflectData::UniformInfo &last = *i->uniforms.back();
+                       i->data_size = last.location*16+last.array_size*get_type_size(last.type);
                }
        }
 }
@@ -504,7 +550,7 @@ void OpenGLProgram::set_debug_name(const string &name)
 void OpenGLProgram::set_stage_debug_name(unsigned stage_id, Stage type)
 {
 #ifdef DEBUG
-       static const char *const suffixes[] = { " [VS]", " [GS]", " [FS]" };
+       static const char *const suffixes[] = { " [VS]", " [TCS]", " [TES]", " [GS]", " [FS]", " [CS]" };
        string name = debug_name+suffixes[type];
        glObjectLabel(GL_SHADER, stage_id, name.size(), name.c_str());
 #else