From 812c09ec9b5f8ff3d5f92dc5311a89a2d7e6dda4 Mon Sep 17 00:00:00 2001 From: Kai Angulo Date: Mon, 3 Jun 2024 01:52:25 -0700 Subject: [PATCH] Added dissasembler and extra options --- SPIRV/CInterface/spirv_c_interface.cpp | 20 ++++++++++++++++++++ build.zig | 21 +++++++++++++++++++-- glslang/Include/glslang_c_interface.h | 2 ++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/SPIRV/CInterface/spirv_c_interface.cpp b/SPIRV/CInterface/spirv_c_interface.cpp index 2ba819b8..220ffb52 100644 --- a/SPIRV/CInterface/spirv_c_interface.cpp +++ b/SPIRV/CInterface/spirv_c_interface.cpp @@ -29,12 +29,15 @@ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. **/ +#include +#include #include "glslang/Include/glslang_c_interface.h" #include "SPIRV/GlslangToSpv.h" #include "SPIRV/Logger.h" #include "SPIRV/SpvTools.h" +#include "SPIRV/disassemble.h" static_assert(sizeof(glslang_spv_options_t) == sizeof(glslang::SpvOptions), ""); @@ -118,3 +121,20 @@ GLSLANG_EXPORT const char* glslang_program_SPIRV_get_messages(glslang_program_t* { return program->loggerMessages.empty() ? nullptr : program->loggerMessages.c_str(); } + +GLSLANG_EXPORT char* glslang_SPIRV_disassemble(const unsigned int* spv_words, size_t spv_words_len) +{ + std::stringstream string_stream; + std::vector words_vector; + words_vector.assign(spv_words, spv_words + spv_words_len); + + spv::Disassemble(string_stream, words_vector); + + std::string string = string_stream.str(); + + const size_t size = string.size(); + char* buffer = new char[size + 1]; + memcpy(buffer, string.c_str(), size + 1); + + return buffer; +} diff --git a/build.zig b/build.zig index 494c8cea..ebef792d 100644 --- a/build.zig +++ b/build.zig @@ -26,17 +26,28 @@ pub fn build(b: *Build) !void { const standalone_glslang = b.option(bool, "standalone", "Build glslang.exe standalone command-line compiler.") orelse false; const standalone_spvremap = b.option(bool, "standalone-remap", "Build spirv-remap.exe standalone command-line remapper.") orelse false; + if (shared and (standalone_glslang or standalone_spvremap)) { + log.err("Cannot build standalone sources with shared glslang. Recompile without `-Dshared` or `-Dstandalone/-Dstandalone-remap`", .{}); + std.process.exit(1); + } + const tools_libs: spvtools.SPVLibs = spvtools.buildSpirv(b, optimize, target, shared_tools, debug, spirv_header_name) catch |err| { log.err("Error building SPIRV-Tools: {s}", .{ @errorName(err) }); std.process.exit(1); }; + const tag = target.result.os.tag; + var cppflags = std.ArrayList([]const u8).init(b.allocator); if (!debug) { try cppflags.append("-g0"); } + if (tag == .windows and shared) { + try cppflags.append("-rdynamic"); + } + try cppflags.append("-std=c++17"); const base_flags = &.{ @@ -94,8 +105,6 @@ pub fn build(b: *Build) !void { .flags = cppflags.items, }); - const tag = target.result.os.tag; - if (tag == .windows) { glslang_lib.addCSourceFiles(.{ .files = &sources_win, @@ -159,6 +168,10 @@ pub fn build(b: *Build) !void { .target = target, }); + if (shared) { + glslang_exe.defineCMacro("GLSLANG_IS_SHARED_LIBRARY", ""); + } + const install_glslang_step = b.step("glslang-standalone", "Build and install glslang.exe"); install_glslang_step.dependOn(&b.addInstallArtifact(glslang_exe, .{}).step); glslang_exe.addCSourceFiles(.{ @@ -197,6 +210,10 @@ pub fn build(b: *Build) !void { .target = target, }); + if (shared) { + spirv_remap.defineCMacro("GLSLANG_IS_SHARED_LIBRARY", ""); + } + const install_remap_step = b.step("spirv-remap", "Build and install spirv-remap.exe"); install_remap_step.dependOn(&b.addInstallArtifact(spirv_remap, .{}).step); spirv_remap.addCSourceFiles(.{ diff --git a/glslang/Include/glslang_c_interface.h b/glslang/Include/glslang_c_interface.h index 3e546258..d78c3ae1 100644 --- a/glslang/Include/glslang_c_interface.h +++ b/glslang/Include/glslang_c_interface.h @@ -284,6 +284,8 @@ GLSLANG_EXPORT const char* glslang_program_SPIRV_get_messages(glslang_program_t* GLSLANG_EXPORT const char* glslang_program_get_info_log(glslang_program_t* program); GLSLANG_EXPORT const char* glslang_program_get_info_debug_log(glslang_program_t* program); +GLSLANG_EXPORT char* glslang_SPIRV_disassemble(const unsigned int* spv_words, size_t spv_words_len); + #ifdef __cplusplus } #endif