From 5677c90619dc446e24d5fe315d69c3279106471a Mon Sep 17 00:00:00 2001 From: nyorain Date: Mon, 29 Dec 2025 01:25:54 +0100 Subject: [PATCH 1/2] Add optional shader disassembly via spriv-tools --- meson.build | 7 ++++++ src/gui/resources.cpp | 35 ++++++++++++++++++++++++++++-- src/gui/resources.hpp | 3 +++ src/handle.cpp | 3 +++ src/imgui/textedit.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++ src/imgui/textedit.h | 1 + src/shader.cpp | 2 ++ 7 files changed, 97 insertions(+), 2 deletions(-) diff --git a/meson.build b/meson.build index 4e84a6fd..9a010f68 100644 --- a/meson.build +++ b/meson.build @@ -164,6 +164,8 @@ dep_nytl = subproject('nytl', default_options: dopts).get_variable('nytl_dep') dep_imgio = subproject('imgio', default_options: dopts).get_variable('imgio_dep') dep_spc = subproject('spc', default_options: dopts).get_variable('spc_dep') +dep_spirv_tools = dependency('SPIRV-Tools', required: false) + layer_args += '-DDLG_DEFAULT_TAGS="vil"' if disable_dlg @@ -414,6 +416,11 @@ if with_window or with_x11_hook or with_win32 or with_example endif endif +if dep_spirv_tools.found() + deps += [dep_spirv_tools] + layer_args += '-DVIL_WITH_SPIRV_TOOLS=1' +endif + with_unit_tests = get_option('unit-tests') if with_unit_tests src += files( diff --git a/src/gui/resources.cpp b/src/gui/resources.cpp index 866fce9b..40d6f54b 100644 --- a/src/gui/resources.cpp +++ b/src/gui/resources.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -30,8 +31,13 @@ #include #include #include +#include #include +#ifdef VIL_WITH_SPIRV_TOOLS +#include +#endif // VIL_WITH_SPIRV_TOOLS + namespace vil { // NOTE: use something like this? But this actively hides information, @@ -1175,8 +1181,32 @@ void ResourceGui::drawDesc(Draw&, ImageView& view) { } } -void ResourceGui::drawDesc(Draw&, ShaderModule&) { - ImGui::Text("TODO"); +void ResourceGui::drawDesc(Draw&, ShaderModule& mod) { +#ifdef VIL_WITH_SPIRV_TOOLS + if (spirvDisassembly_.empty()) { + auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_6); + auto& spirv = mod.compiled->get_ir().spirv; + spv_text text; + spv_diagnostic diagnostic; + auto opts = SPV_BINARY_TO_TEXT_OPTION_INDENT | + SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES; + spvBinaryToText(context, spirv.data(), spirv.size(), + opts, &text, &diagnostic); + + dlg_assert(!diagnostic); + spirvDisassembly_ = {text->str, text->length}; + + spirvEdit_.SetReadOnly(true); + spirvEdit_.SetText(spirvDisassembly_); + spirvEdit_.SetShowWhitespaces(false); + spirvEdit_.SetTabSize(4); + spirvEdit_.SetLanguageDefinition(igt::TextEditor::LanguageDefinition::SPIRV()); + } + + ImGui::PushFont(gui_->monoFont); + spirvEdit_.Render("SPIR-V"); + ImGui::PopFont(); +#endif // VIL_WITH_SPIRV_TOOLS } void ResourceGui::drawDesc(Draw&, Framebuffer& fb) { @@ -1651,6 +1681,7 @@ void ResourceGui::clearSelection() { ds_.selected = {}; image_.object = {}; buffer_.handle = {}; + spirvDisassembly_ = {}; } void ResourceGui::draw(Draw& draw) { diff --git a/src/gui/resources.hpp b/src/gui/resources.hpp index e36ac2fc..2a491169 100644 --- a/src/gui/resources.hpp +++ b/src/gui/resources.hpp @@ -83,6 +83,9 @@ class ResourceGui { Handle* handle_ {}; bool editName_ {false}; + std::string spirvDisassembly_; + igt::TextEditor spirvEdit_; + struct { Image* object {}; ImageViewer viewer {}; diff --git a/src/handle.cpp b/src/handle.cpp index e4766e84..a4150601 100644 --- a/src/handle.cpp +++ b/src/handle.cpp @@ -48,6 +48,9 @@ const char* name(VkObjectType objectType) { case VK_OBJECT_TYPE_FRAMEBUFFER: return "Framebuffer"; case VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE: return "DescriptorUpdateTemplate"; case VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_KHR: return "AccelerationStructure"; + case VK_OBJECT_TYPE_SHADER_EXT: return "Shader"; + case VK_OBJECT_TYPE_INDIRECT_COMMANDS_LAYOUT_EXT: return "IndirectCommandsLayout"; + case VK_OBJECT_TYPE_INDIRECT_EXECUTION_SET_EXT: return "IndirectExecutionSet"; default: return "?"; } } diff --git a/src/imgui/textedit.cpp b/src/imgui/textedit.cpp index 59d02d78..f4bcb9a1 100644 --- a/src/imgui/textedit.cpp +++ b/src/imgui/textedit.cpp @@ -2907,6 +2907,54 @@ const TextEditor::LanguageDefinition& TextEditor::LanguageDefinition::HLSL() return langDef; } +const TextEditor::LanguageDefinition& TextEditor::LanguageDefinition::SPIRV() +{ + // From SHADERed + // Copyright (c) 2018 - 2021 dfranx + // + // Permission is hereby granted, free of charge, to any person obtaining a copy + // of this software and associated documentation files (the "Software"), to deal + // in the Software without restriction, including without limitation the rights + // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + // copies of the Software, and to permit persons to whom the Software is + // furnished to do so, subject to the following conditions: + // + // The above copyright notice and this permission notice shall be included in all + // copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + // SOFTWARE. + + static bool inited = false; + static LanguageDefinition langDef; + if (!inited) { + langDef.mTokenRegexStrings.push_back(std::make_pair("L?\\\"(\\\\.|[^\\\"])*\\\"", PaletteIndex::String)); + langDef.mTokenRegexStrings.push_back(std::make_pair("[ =\\t]Op[a-zA-Z]*", PaletteIndex::Keyword)); + langDef.mTokenRegexStrings.push_back(std::make_pair("%[_a-zA-Z0-9]*", PaletteIndex::Identifier)); + langDef.mTokenRegexStrings.push_back(std::make_pair("[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?[fF]?", PaletteIndex::Number)); + langDef.mTokenRegexStrings.push_back(std::make_pair("[+-]?[0-9]+[Uu]?[lL]?[lL]?", PaletteIndex::Number)); + langDef.mTokenRegexStrings.push_back(std::make_pair("0[0-7]+[Uu]?[lL]?[lL]?", PaletteIndex::Number)); + langDef.mTokenRegexStrings.push_back(std::make_pair("0[xX][0-9a-fA-F]+[uU]?[lL]?[lL]?", PaletteIndex::Number)); + + langDef.mCommentStart = "/*"; + langDef.mCommentEnd = "*/"; + langDef.mSingleLineComment = ";"; + + langDef.mCaseSensitive = true; + langDef.mAutoIndentation = false; + + langDef.mName = "SPIR-V"; + + inited = true; + } + return langDef; +} + const TextEditor::LanguageDefinition& TextEditor::LanguageDefinition::GLSLFast() { static bool inited = false; diff --git a/src/imgui/textedit.h b/src/imgui/textedit.h index 4a527227..63523d60 100644 --- a/src/imgui/textedit.h +++ b/src/imgui/textedit.h @@ -205,6 +205,7 @@ class TextEditor static const LanguageDefinition& CPlusPlus(); static const LanguageDefinition& HLSL(); static const LanguageDefinition& GLSLFast(); + static const LanguageDefinition& SPIRV(); static const LanguageDefinition& GLSL(); static const LanguageDefinition& C(); static const LanguageDefinition& SQL(); diff --git a/src/shader.cpp b/src/shader.cpp index 114939a7..fee32e80 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -805,6 +805,8 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateShadersEXT( mod.handle = pShaders[i]; mod.dsLayouts = std::move(layoutVecs[i]); + // TODO: store code! + pShaders[i] = castDispatch(mod); dev.shaderObjects.mustEmplace(pShaders[i], std::move(modPtr)); } From 4aa11359bdced14d17ec1dbb215566ed7e90dc83 Mon Sep 17 00:00:00 2001 From: nyorain Date: Sun, 4 Jan 2026 15:56:32 +0100 Subject: [PATCH 2/2] Fix standlone creation --- .gitignore | 1 + .launch | 2 -- src/device.cpp | 2 ++ src/gui/resources.cpp | 1 + 4 files changed, 4 insertions(+), 2 deletions(-) delete mode 100644 .launch diff --git a/.gitignore b/.gitignore index 4eb740d4..146b016b 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ docs/traces/ .vimspector.json subprojects/.wraplock subprojects/pml.wrap +.launch diff --git a/.launch b/.launch deleted file mode 100644 index 545ecac2..00000000 --- a/.launch +++ /dev/null @@ -1,2 +0,0 @@ -/home/jan/code/iro/build/npt ---vil /home/jan/art/assets/sponza/Sponza.gltf --no-validation diff --git a/src/device.cpp b/src/device.cpp index a5fc978c..3d02c5e4 100644 --- a/src/device.cpp +++ b/src/device.cpp @@ -251,6 +251,7 @@ std::unique_ptr tryCreateWindow(Instance& ini, // swa whether it should use x11 or wayland; but also have to tell // it whether to use xcb or xlib for window creation). if(standaloneMode) { + window->ini = &ini; if(!window->doCreateDisplay()) { return nullptr; } @@ -1251,6 +1252,7 @@ VkResult doCreateDevice( if(standaloneMode) { dev.window->dev = &dev; + dev.window->doCreateWindow(); dev.window->doInitSwapchain(); } } diff --git a/src/gui/resources.cpp b/src/gui/resources.cpp index 40d6f54b..be06aa48 100644 --- a/src/gui/resources.cpp +++ b/src/gui/resources.cpp @@ -1182,6 +1182,7 @@ void ResourceGui::drawDesc(Draw&, ImageView& view) { } void ResourceGui::drawDesc(Draw&, ShaderModule& mod) { + (void) mod; #ifdef VIL_WITH_SPIRV_TOOLS if (spirvDisassembly_.empty()) { auto context = spvContextCreate(SPV_ENV_UNIVERSAL_1_6);