]> git.tdb.fi Git - libs/gl.git/commitdiff
Set OpenGL debug labels on various objects loaded from Resources
authorMikko Rasa <tdb@tdb.fi>
Sun, 25 Apr 2021 11:28:17 +0000 (14:28 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 25 Apr 2021 14:32:42 +0000 (17:32 +0300)
23 files changed:
extensions/khr_debug.glext [new file with mode: 0644]
source/core/buffer.cpp
source/core/buffer.h
source/core/mesh.cpp
source/core/mesh.h
source/core/program.cpp
source/core/program.h
source/core/sampler.cpp
source/core/sampler.h
source/core/texture.cpp
source/core/texture.h
source/core/vertexsetup.cpp
source/core/vertexsetup.h
source/materials/material.cpp
source/materials/material.h
source/materials/renderpass.cpp
source/materials/renderpass.h
source/materials/technique.cpp
source/materials/technique.h
source/render/programdata.cpp
source/render/programdata.h
source/resources/resources.cpp
source/resources/resources.h

diff --git a/extensions/khr_debug.glext b/extensions/khr_debug.glext
new file mode 100644 (file)
index 0000000..f484e71
--- /dev/null
@@ -0,0 +1 @@
+extension KHR_debug
index 17e833d71dfee347e7f3b46c885bc674aa0de864..00fd9cd3d42ac0597c255413b983f8d704b73bb4 100644 (file)
@@ -2,6 +2,7 @@
 #include <msp/gl/extensions/arb_buffer_storage.h>
 #include <msp/gl/extensions/arb_direct_state_access.h>
 #include <msp/gl/extensions/arb_map_buffer_range.h>
+#include <msp/gl/extensions/khr_debug.h>
 #include <msp/strings/format.h>
 #include "buffer.h"
 #include "error.h"
@@ -237,6 +238,16 @@ bool Buffer::set_current(BufferType type, const Buffer *buf)
        return true;
 }
 
+void Buffer::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+       if(KHR_debug)
+               glObjectLabel(GL_BUFFER, id, name.size(), name.c_str());
+#else
+       (void)name;
+#endif
+}
+
 
 vector<const BufferRange *> BufferRange::bound_uniform;
 
index 47cab8cd5646e2c3842f641bd31dcb4c418aaf6f..7ef846681a8d487a693ac59e177fee3e68cec970 100644 (file)
@@ -130,6 +130,9 @@ public:
 private:
        static const Buffer *&binding(BufferType);
        static bool set_current(BufferType, const Buffer *);
+
+public:
+       void set_debug_name(const std::string &);
 };
 
 
index d3fd7360f756680ef198c44916b9fe71ed7042ac..f8135dc8c2ce5dcb7baee86238128aa3b78c8a34 100644 (file)
@@ -1,6 +1,7 @@
 #include <msp/gl/extensions/arb_vertex_array_object.h>
 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
 #include <msp/gl/extensions/arb_vertex_shader.h>
+#include <msp/gl/extensions/khr_debug.h>
 #include "buffer.h"
 #include "error.h"
 #include "mesh.h"
@@ -63,6 +64,11 @@ void Mesh::check_buffers(unsigned mask)
                        vertices.use_buffer(vbuf);
                        vtx_setup.set_vertex_array(vertices);
                        dirty |= VERTEX_BUFFER;
+
+#ifdef DEBUG
+                       if(!debug_name.empty())
+                               vbuf->set_debug_name(debug_name+" [VBO]");
+#endif
                }
        }
 
@@ -77,6 +83,11 @@ void Mesh::check_buffers(unsigned mask)
                                batches.front().change_buffer(ibuf);
                        vtx_setup.set_index_buffer(*ibuf);
                        dirty |= INDEX_BUFFER;
+
+#ifdef DEBUG
+                       if(!debug_name.empty())
+                               vbuf->set_debug_name(debug_name+" [IBO]");
+#endif
                }
        }
 }
@@ -211,6 +222,20 @@ void Mesh::unload()
        ibuf = 0;
 }
 
+void Mesh::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+       debug_name = name;
+       if(vbuf)
+               vbuf->set_debug_name(name+" [VBO]");
+       if(ibuf)
+               ibuf->set_debug_name(name+" [IBO]");
+       vtx_setup.set_debug_name(name+" [VAO]");
+#else
+       (void)name;
+#endif
+}
+
 
 Mesh::Loader::Loader(Mesh &m, bool g):
        DataFile::ObjectLoader<Mesh>(m),
index c325ade6111c9f4a981f41d80b228db65decb057..acf2910cb7ea208f3ce2fbab9c74bc182b781928 100644 (file)
@@ -69,6 +69,7 @@ private:
        mutable unsigned short dirty;
        bool disallow_rendering;
        const WindingTest *winding;
+       std::string debug_name;
 
 public:
        Mesh(ResourceManager * = 0);
@@ -105,6 +106,8 @@ public:
        virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
        virtual UInt64 get_data_size() const;
        virtual void unload();
+
+       void set_debug_name(const std::string &);
 };
 
 } // namespace GL
index d6bd5ee88d889c4efc02a0b9ee86a8051b8ec9a2..33c53cd7755fe4f0e4d7b1bd293c2f6af30c8748 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;
 }
 
@@ -767,6 +773,33 @@ void Program::unbind()
        glUseProgram(0);
 }
 
+void Program::set_debug_name(const string &name)
+{
+#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::set_stage_debug_name(unsigned stage_id, Stage type)
+{
+#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
+}
+
 
 Program::UniformInfo::UniformInfo():
        block(0),
index 090d8ed5ebc5c4d191ab24d13f7cef402502cdc3..a2324f2329cc0fbaa3c8a4a2ea733384d02e6aeb 100644 (file)
@@ -125,6 +125,7 @@ private:
        std::vector<UniformInfo> uniforms;
        LayoutHash uniform_layout_hash;
        std::vector<AttributeInfo> attributes;
+       std::string debug_name;
 
 public:
        /// Constructs an empty Program with no shader stages attached.
@@ -192,6 +193,10 @@ public:
 
        void bind() const;
        static void unbind();
+
+       void set_debug_name(const std::string &);
+private:
+       void set_stage_debug_name(unsigned, Stage);
 };
 
 } // namespace GL
index 273f2bb712cf96a1acb0ee93f7afb6e0343f86c5..93b3b5c9f702e69e091ffbd480c438d7fe8f695c 100644 (file)
@@ -3,6 +3,7 @@
 #include <msp/gl/extensions/arb_shadow.h>
 #include <msp/gl/extensions/ext_texture_filter_anisotropic.h>
 #include <msp/gl/extensions/ext_texture3d.h>
+#include <msp/gl/extensions/khr_debug.h>
 #include <msp/strings/format.h>
 #include "error.h"
 #include "sampler.h"
@@ -255,6 +256,16 @@ void Sampler::unload()
        }
 }
 
+void Sampler::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+       if(id && KHR_debug)
+               glObjectLabel(GL_SAMPLER, id, name.size(), name.c_str());
+#else
+       (void)name;
+#endif
+}
+
 
 Sampler::Loader::Loader(Sampler &s):
        DataFile::ObjectLoader<Sampler>(s)
index 79aea8f8b76621f6ed9f5e5f44d0df08bf84aa0d..f68f1a16183545c315a224017bd7b1ddd292adae 100644 (file)
@@ -166,6 +166,8 @@ public:
        static void unbind_from(unsigned);
 
        void unload();
+
+       void set_debug_name(const std::string &);
 };
 
 
index 354c1b12d5307b53113c0ae7080f2635c3e0f3a9..967a7fe31562d8509619b3bef3d060c3bd75d714 100644 (file)
@@ -1,6 +1,7 @@
 #include <msp/gl/extensions/arb_direct_state_access.h>
 #include <msp/gl/extensions/arb_texture_swizzle.h>
 #include <msp/gl/extensions/ext_framebuffer_object.h>
+#include <msp/gl/extensions/khr_debug.h>
 #include <msp/io/memory.h>
 #include "bindable.h"
 #include "error.h"
@@ -55,6 +56,11 @@ void Texture::generate_id()
                glCreateTextures(target, 1, &id);
        else
                glGenTextures(1, &id);
+
+#ifdef DEBUG
+       if(!debug_name.empty() && KHR_debug)
+               glObjectLabel(GL_TEXTURE, id, debug_name.size(), debug_name.c_str());
+#endif
 }
 
 void Texture::set_format(PixelFormat fmt)
@@ -277,6 +283,17 @@ void Texture::unbind_from(unsigned i)
        }
 }
 
+void Texture::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+       debug_name = name;
+       if(id && KHR_debug)
+               glObjectLabel(GL_TEXTURE, id, name.size(), name.c_str());
+#else
+       (void)name;
+#endif
+}
+
 
 Texture::Loader::Loader(Texture &t):
        DataFile::CollectionObjectLoader<Texture>(t, 0)
index bca0d409b105d9177ea7a108efc498e25c180769..4fa20c76483490c724b4f8b6eb72d6d05cd25707 100644 (file)
@@ -80,6 +80,7 @@ protected:
        bool use_srgb_format;
        bool auto_gen_mipmap;
        Sampler default_sampler;
+       std::string debug_name;
 
        static int swizzle_orders[];
 
@@ -160,6 +161,8 @@ public:
        static void unbind_from(unsigned);
 
        virtual UInt64 get_data_size() const { return 0; }
+
+       void set_debug_name(const std::string &);
 };
 
 } // namespace GL
index f6f0f58b65397cabdba6f9cbdc42bf027503689d..4b2e2a5f0e5b8d3ccfb1bb435772f58557c15efb 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/khr_debug.h>
 #include "buffer.h"
 #include "error.h"
 #include "gl.h"
@@ -209,5 +210,15 @@ void VertexSetup::unbind()
                glBindVertexArray(0);
 }
 
+void VertexSetup::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+       if(KHR_debug)
+               glObjectLabel(GL_VERTEX_ARRAY, id, name.size(), name.c_str());
+#else
+       (void)name;
+#endif
+}
+
 } // namespace GL
 } // namespace Msp
index dceb79876910cdf60d76990ecba25e73992d82d9..06422a08c006b7953136a17ed4cbe218350d04ae 100644 (file)
@@ -55,6 +55,8 @@ private:
 public:
        void bind() const;
        static void unbind();
+
+       void set_debug_name(const std::string &);
 };
 
 } // namespace GL
index b737c03183b504b82592f5959cfd70e80ec10f77..ace87e70954847d299061205826d9dacf9029919 100644 (file)
@@ -69,6 +69,15 @@ void Material::attach_texture_to(const Texture *tex, Texturing &texturing, Progr
 }
 #pragma GCC diagnostic pop
 
+void Material::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+       shdata.set_debug_name(name+" [UBO]");
+#else
+       (void)name;
+#endif
+}
+
 Material::MaterialRegistry &Material::get_material_registry()
 {
        static MaterialRegistry registry;
index 8f387a43bfacfbdc4b2b5629d1a5a718f653a5cd..ae38f2444afdadbd3fc260e142bf1c83113660bf 100644 (file)
@@ -114,6 +114,8 @@ public:
        virtual const Texture *get_texture(Tag) const = 0;
        const Sampler *get_sampler() const { return sampler; }
 
+       void set_debug_name(const std::string &);
+
        template<typename T>
        static void register_type(const std::string &);
 private:
index 36404a6620b7d209713fb6302b970af9027aad72..dde1fdb4f9847b39178cc412f8c6cdcde3576a6e 100644 (file)
@@ -149,6 +149,16 @@ void RenderPass::apply(Renderer &renderer) const
        renderer.set_reverse_winding(back_faces);
 }
 
+void RenderPass::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+       if(shdata.refcount()==1)
+               shdata->set_debug_name(name+" [UBO]");
+#else
+       (void)name;
+#endif
+}
+
 
 DataFile::Loader::ActionMap RenderPass::Loader::shared_actions;
 
index 46fb7a3535a0bb51a2417e190e3cf3f3dd5a809e..f02303bc8865f91632723bfe160143dd201dabf8 100644 (file)
@@ -107,6 +107,8 @@ public:
        bool get_receive_shadows() const { return receive_shadows; }
 
        void apply(Renderer &) const;
+
+       void set_debug_name(const std::string &);
 };
 
 } // namespace GL
index a4d181629a122a802d8bd66509f3f01617784a88..b5ed6447c314c3e69be3bfe0fcb0c08be67cfc07 100644 (file)
@@ -101,6 +101,16 @@ bool Technique::has_shaders() const
        return false;
 }
 
+void Technique::set_debug_name(const std::string &name)
+{
+#ifdef DEBUG
+       for(map<Tag, RenderPass>::iterator i=passes.begin(); i!=passes.end(); ++i)
+               i->second.set_debug_name(format("%s [pass:%s]", name, i->first.str()));
+#else
+       (void)name;
+#endif
+}
+
 
 DataFile::Loader::ActionMap Technique::Loader::shared_actions;
 
index 8e4adf4f1ebaad993e2fd0efbd1d6806b38599e3..47a307c9af318b111ef2266b93fc9442ece058e1 100644 (file)
@@ -60,6 +60,8 @@ public:
        bool replace_material(const std::string &, const Material &);
        bool replace_uniforms(const ProgramData &);
        bool has_shaders() const;
+
+       void set_debug_name(const std::string &);
 };
 
 } // namespace GL
index f3c6108c767284a9e158ee88df7fdd9a5986873d..4133b9acaabb207fbd63d077ca9bc8da7bcca2c4 100644 (file)
@@ -565,8 +565,15 @@ void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program
                if(info.bind_point>=0)
                {
                        if(!buffer)
+                       {
                                buffer = new Buffer(UNIFORM_BUFFER);
 
+#ifdef DEBUG
+                               if(!debug_name.empty())
+                                       buffer->set_debug_name(debug_name);
+#endif
+                       }
+
                        BufferBackedUniformBlock *bb_block = new BufferBackedUniformBlock(info.data_size);
                        block.block = bb_block;
                        bb_block->use_buffer(buffer, last_buffer_block);
@@ -665,6 +672,11 @@ void ProgramData::apply() const
                                        delete buffer;
                                        buffer = new Buffer(UNIFORM_BUFFER);
                                        last_buffer_block->change_buffer(buffer);
+
+#ifdef DEBUG
+                                       if(!debug_name.empty())
+                                               buffer->set_debug_name(debug_name);
+#endif
                                }
 
                                buffer->storage(required_size);
@@ -677,6 +689,17 @@ void ProgramData::apply() const
                        i->block->apply(i->bind_point);
 }
 
+void ProgramData::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+       debug_name = name;
+       if(buffer)
+               buffer->set_debug_name(name);
+#else
+       (void)name;
+#endif
+}
+
 
 ProgramData::TaggedUniform::TaggedUniform():
        value(0)
index aeffae45700726f9bc5bd9c7fbc350477486eb90..a05674dd21927304d3755aa5f0118870ef81729e 100644 (file)
@@ -158,6 +158,7 @@ private:
        mutable BufferBackedUniformBlock *last_buffer_block;
        mutable Buffer *buffer;
        mutable Mask dirty;
+       std::string debug_name;
 
 public:
        ProgramData(const Program * = 0);
@@ -252,6 +253,8 @@ public:
        /** Applies uniform blocks for the currently bound program, creating them
        if needed. */
        void apply() const;
+
+       void set_debug_name(const std::string &);
 };
 
 template<typename T, unsigned N>
index 11dc2d329dd29cfca2ec9385b72361f6260e54b8..66f6f082b927d2d6cdd5499a859f23bde6eadff2 100644 (file)
@@ -46,21 +46,33 @@ Resources::Resources():
        add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
        add_type<Light>().keyword("light");
        add_type<Lighting>().suffix(".lightn").keyword("lighting");
-       add_type<Material>().suffix(".mat").creator(&Resources::create_material);
-       add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
-       add_type<Module>().suffix(".glsl").suffix(".spv").creator(&Resources::create_module);
+       add_type<Material>().suffix(".mat")
+               .creator(&Resources::create_material).notify(&Resources::set_debug_name<Material>);
+       add_type<Mesh>().keyword("mesh")
+               .creator(&Resources::create_mesh).notify(&Resources::set_debug_name<Mesh>);
+       add_type<Module>().suffix(".glsl").suffix(".spv")
+               .creator(&Resources::create_module);
        add_type<Object>().keyword("object");
        add_type<SequenceTemplate>().suffix(".seq").keyword("sequence");
        add_type<Pose>().keyword("pose");
-       add_type<Program>().keyword("shader").creator(&Resources::create_program);
-       add_type<Sampler>().suffix(".samp").keyword("sampler");
-       add_type<Scene>().suffix(".scene").creator(&Resources::create_scene);
-       add_type<Technique>().suffix(".tech").keyword("technique");
-       add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
-       add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
-       add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
-       add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
-       add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array");
+       add_type<Program>().keyword("shader")
+               .creator(&Resources::create_program).notify(&Resources::set_debug_name<Program>);
+       add_type<Sampler>().suffix(".samp").keyword("sampler")
+               .notify(&Resources::set_debug_name<Sampler>);
+       add_type<Scene>().suffix(".scene")
+               .creator(&Resources::create_scene);
+       add_type<Technique>().suffix(".tech").keyword("technique")
+               .notify(&Resources::set_debug_name<Technique>);
+       add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d")
+               .notify(&Resources::set_debug_name<Texture1D>);
+       add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d")
+               .creator(&Resources::create_texture2d).notify(&Resources::set_debug_name<Texture2D>);
+       add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d")
+               .notify(&Resources::set_debug_name<Texture3D>);
+       add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube")
+               .notify(&Resources::set_debug_name<TextureCube>);
+       add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array")
+               .notify(&Resources::set_debug_name<Texture2DArray>);
 
        add_source(get_builtins());
 }
@@ -240,6 +252,14 @@ Program *Resources::create_program(const string &name)
        return 0;
 }
 
+template<typename T>
+void Resources::set_debug_name(const string &name, T &item)
+{
+#ifdef DEBUG
+       item.set_debug_name(name);
+#endif
+}
+
 
 Resources::Loader::Loader(Resources &r):
        DerivedObjectLoader<Resources, Collection::Loader>(r)
index 5861bbfb41259a8a910e87f55a2ad78498706c81..a50fdc0e022081602abcaea69c44b4e7d3b2ee9d 100644 (file)
@@ -60,6 +60,9 @@ protected:
        Texture2D *create_texture2d(const std::string &);
        Module *create_module(const std::string &);
        Program *create_program(const std::string &);
+
+       template<typename T>
+       void set_debug_name(const std::string &, T &);
 };
 
 } // namespace GL