From: Mikko Rasa Date: Sun, 25 Apr 2021 11:28:17 +0000 (+0300) Subject: Set OpenGL debug labels on various objects loaded from Resources X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=fcde8390ad577fe434dcd4b29e0f410d29f867c9;p=libs%2Fgl.git Set OpenGL debug labels on various objects loaded from Resources --- diff --git a/extensions/khr_debug.glext b/extensions/khr_debug.glext new file mode 100644 index 00000000..f484e716 --- /dev/null +++ b/extensions/khr_debug.glext @@ -0,0 +1 @@ +extension KHR_debug diff --git a/source/core/buffer.cpp b/source/core/buffer.cpp index 17e833d7..00fd9cd3 100644 --- a/source/core/buffer.cpp +++ b/source/core/buffer.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #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 BufferRange::bound_uniform; diff --git a/source/core/buffer.h b/source/core/buffer.h index 47cab8cd..7ef84668 100644 --- a/source/core/buffer.h +++ b/source/core/buffer.h @@ -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 &); }; diff --git a/source/core/mesh.cpp b/source/core/mesh.cpp index d3fd7360..f8135dc8 100644 --- a/source/core/mesh.cpp +++ b/source/core/mesh.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #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(m), diff --git a/source/core/mesh.h b/source/core/mesh.h index c325ade6..acf2910c 100644 --- a/source/core/mesh.h +++ b/source/core/mesh.h @@ -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 diff --git a/source/core/program.cpp b/source/core/program.cpp index d6bd5ee8..33c53cd7 100644 --- a/source/core/program.cpp +++ b/source/core/program.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -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(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), diff --git a/source/core/program.h b/source/core/program.h index 090d8ed5..a2324f23 100644 --- a/source/core/program.h +++ b/source/core/program.h @@ -125,6 +125,7 @@ private: std::vector uniforms; LayoutHash uniform_layout_hash; std::vector 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 diff --git a/source/core/sampler.cpp b/source/core/sampler.cpp index 273f2bb7..93b3b5c9 100644 --- a/source/core/sampler.cpp +++ b/source/core/sampler.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #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(s) diff --git a/source/core/sampler.h b/source/core/sampler.h index 79aea8f8..f68f1a16 100644 --- a/source/core/sampler.h +++ b/source/core/sampler.h @@ -166,6 +166,8 @@ public: static void unbind_from(unsigned); void unload(); + + void set_debug_name(const std::string &); }; diff --git a/source/core/texture.cpp b/source/core/texture.cpp index 354c1b12..967a7fe3 100644 --- a/source/core/texture.cpp +++ b/source/core/texture.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #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(t, 0) diff --git a/source/core/texture.h b/source/core/texture.h index bca0d409..4fa20c76 100644 --- a/source/core/texture.h +++ b/source/core/texture.h @@ -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 diff --git a/source/core/vertexsetup.cpp b/source/core/vertexsetup.cpp index f6f0f58b..4b2e2a5f 100644 --- a/source/core/vertexsetup.cpp +++ b/source/core/vertexsetup.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #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 diff --git a/source/core/vertexsetup.h b/source/core/vertexsetup.h index dceb7987..06422a08 100644 --- a/source/core/vertexsetup.h +++ b/source/core/vertexsetup.h @@ -55,6 +55,8 @@ private: public: void bind() const; static void unbind(); + + void set_debug_name(const std::string &); }; } // namespace GL diff --git a/source/materials/material.cpp b/source/materials/material.cpp index b737c031..ace87e70 100644 --- a/source/materials/material.cpp +++ b/source/materials/material.cpp @@ -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; diff --git a/source/materials/material.h b/source/materials/material.h index 8f387a43..ae38f244 100644 --- a/source/materials/material.h +++ b/source/materials/material.h @@ -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 static void register_type(const std::string &); private: diff --git a/source/materials/renderpass.cpp b/source/materials/renderpass.cpp index 36404a66..dde1fdb4 100644 --- a/source/materials/renderpass.cpp +++ b/source/materials/renderpass.cpp @@ -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; diff --git a/source/materials/renderpass.h b/source/materials/renderpass.h index 46fb7a35..f02303bc 100644 --- a/source/materials/renderpass.h +++ b/source/materials/renderpass.h @@ -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 diff --git a/source/materials/technique.cpp b/source/materials/technique.cpp index a4d18162..b5ed6447 100644 --- a/source/materials/technique.cpp +++ b/source/materials/technique.cpp @@ -101,6 +101,16 @@ bool Technique::has_shaders() const return false; } +void Technique::set_debug_name(const std::string &name) +{ +#ifdef DEBUG + for(map::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; diff --git a/source/materials/technique.h b/source/materials/technique.h index 8e4adf4f..47a307c9 100644 --- a/source/materials/technique.h +++ b/source/materials/technique.h @@ -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 diff --git a/source/render/programdata.cpp b/source/render/programdata.cpp index f3c6108c..4133b9ac 100644 --- a/source/render/programdata.cpp +++ b/source/render/programdata.cpp @@ -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) diff --git a/source/render/programdata.h b/source/render/programdata.h index aeffae45..a05674dd 100644 --- a/source/render/programdata.h +++ b/source/render/programdata.h @@ -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 diff --git a/source/resources/resources.cpp b/source/resources/resources.cpp index 11dc2d32..66f6f082 100644 --- a/source/resources/resources.cpp +++ b/source/resources/resources.cpp @@ -46,21 +46,33 @@ Resources::Resources(): add_type().suffix(".kframe").keyword("keyframe"); add_type().keyword("light"); add_type().suffix(".lightn").keyword("lighting"); - add_type().suffix(".mat").creator(&Resources::create_material); - add_type().keyword("mesh").creator(&Resources::create_mesh); - add_type().suffix(".glsl").suffix(".spv").creator(&Resources::create_module); + add_type().suffix(".mat") + .creator(&Resources::create_material).notify(&Resources::set_debug_name); + add_type().keyword("mesh") + .creator(&Resources::create_mesh).notify(&Resources::set_debug_name); + add_type().suffix(".glsl").suffix(".spv") + .creator(&Resources::create_module); add_type().keyword("object"); add_type().suffix(".seq").keyword("sequence"); add_type().keyword("pose"); - add_type().keyword("shader").creator(&Resources::create_program); - add_type().suffix(".samp").keyword("sampler"); - add_type().suffix(".scene").creator(&Resources::create_scene); - add_type().suffix(".tech").keyword("technique"); - add_type().base().suffix(".tex1d").keyword("texture1d"); - add_type().base().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d); - add_type().base().suffix(".tex3d").keyword("texture3d"); - add_type().base().suffix(".texcb").keyword("texture_cube"); - add_type().base().suffix(".tex2da").keyword("texture2d_array"); + add_type().keyword("shader") + .creator(&Resources::create_program).notify(&Resources::set_debug_name); + add_type().suffix(".samp").keyword("sampler") + .notify(&Resources::set_debug_name); + add_type().suffix(".scene") + .creator(&Resources::create_scene); + add_type().suffix(".tech").keyword("technique") + .notify(&Resources::set_debug_name); + add_type().base().suffix(".tex1d").keyword("texture1d") + .notify(&Resources::set_debug_name); + add_type().base().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d") + .creator(&Resources::create_texture2d).notify(&Resources::set_debug_name); + add_type().base().suffix(".tex3d").keyword("texture3d") + .notify(&Resources::set_debug_name); + add_type().base().suffix(".texcb").keyword("texture_cube") + .notify(&Resources::set_debug_name); + add_type().base().suffix(".tex2da").keyword("texture2d_array") + .notify(&Resources::set_debug_name); add_source(get_builtins()); } @@ -240,6 +252,14 @@ Program *Resources::create_program(const string &name) return 0; } +template +void Resources::set_debug_name(const string &name, T &item) +{ +#ifdef DEBUG + item.set_debug_name(name); +#endif +} + Resources::Loader::Loader(Resources &r): DerivedObjectLoader(r) diff --git a/source/resources/resources.h b/source/resources/resources.h index 5861bbfb..a50fdc0e 100644 --- a/source/resources/resources.h +++ b/source/resources/resources.h @@ -60,6 +60,9 @@ protected: Texture2D *create_texture2d(const std::string &); Module *create_module(const std::string &); Program *create_program(const std::string &); + + template + void set_debug_name(const std::string &, T &); }; } // namespace GL