]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/program.cpp
Rewrite state management
[libs/gl.git] / source / core / program.cpp
index d6bd5ee88d889c4efc02a0b9ee86a8051b8ec9a2..ed2455f86aa315ec79f97bf2860563f7d40cc243 100644 (file)
@@ -13,6 +13,7 @@
 #include <msp/gl/extensions/arb_uniform_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 <msp/gl/extensions/nv_non_square_matrices.h>
 #include <msp/io/print.h>
 #include <msp/strings/format.h>
@@ -123,6 +124,11 @@ unsigned Program::add_stage(Stage type)
        stage_ids[type] = stage_id;
        glAttachShader(id, stage_id);
 
+#ifdef DEBUG
+       if(!debug_name.empty() && KHR_debug)
+               set_stage_debug_name(stage_id, type);
+#endif
+
        return stage_id;
 }
 
@@ -349,7 +355,8 @@ void Program::link()
                                }
                        }
 
-                       Conditional<BindRestore> _bind(!ARB_separate_shader_objects, this);
+                       if(!ARB_separate_shader_objects)
+                               glUseProgram(id);
                        for(map<string, unsigned>::const_iterator i=transient->textures.begin(); i!=transient->textures.end(); ++i)
                        {
                                int location = get_uniform_location(i->first);
@@ -748,23 +755,31 @@ int Program::get_attribute_location(const string &name) const
        return i!=attributes.end() && i->name==name ? i->location : -1;
 }
 
-void Program::bind() const
+void Program::set_debug_name(const string &name)
 {
-       if(!linked)
-               throw invalid_operation("Program::bind");
-
-       if(!set_current(this))
-               return;
-
-       glUseProgram(id);
+#ifdef DEBUG
+       debug_name = name;
+       if(KHR_debug)
+       {
+               glObjectLabel(GL_PROGRAM, id, name.size(), name.c_str());
+               for(unsigned i=0; i<MAX_STAGES; ++i)
+                       if(stage_ids[i])
+                               set_stage_debug_name(stage_ids[i], static_cast<Stage>(i));
+       }
+#else
+       (void)name;
+#endif
 }
 
-void Program::unbind()
+void Program::set_stage_debug_name(unsigned stage_id, Stage type)
 {
-       if(!set_current(0))
-               return;
-
-       glUseProgram(0);
+#ifdef DEBUG
+       static const char *const suffixes[] = { " [VS]", " [GS]", " [FS]" };
+       string name = debug_name+suffixes[type];
+       glObjectLabel(GL_SHADER, stage_id, name.size(), name.c_str());
+#else
+       (void)stage_id; (void)type;
+#endif
 }