From 02706dfc5f0526f4f8ca337be16d7b00640ba168 Mon Sep 17 00:00:00 2001 From: cheneym2 Date: Wed, 26 Feb 2025 21:20:29 -0500 Subject: Fix precompiledTargetModule tests (#6455) * Fix precompiledTargetModule tests Add SPIRV-Tool linker support to gfx unit tests and use the linker in precompileModule tests that use precompiled modules to reconstitute SPIRV shaders that were modularly compiled. Fix a Slang reference count bug in the precompile service. * Use sm_6_6 New DXC requires higher version for linkability. * Rename helper function, pass by reference * Link through slang-glslang * Add missing files * Fix metal * format code --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> Co-authored-by: Yong He --- source/slang-glslang/slang-glslang.cpp | 65 ++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'source/slang-glslang/slang-glslang.cpp') diff --git a/source/slang-glslang/slang-glslang.cpp b/source/slang-glslang/slang-glslang.cpp index 8d17afc79..bbb3f6afc 100644 --- a/source/slang-glslang/slang-glslang.cpp +++ b/source/slang-glslang/slang-glslang.cpp @@ -6,6 +6,7 @@ #include "glslang/Public/ShaderLang.h" #include "slang.h" #include "spirv-tools/libspirv.h" +#include "spirv-tools/linker.hpp" #include "spirv-tools/optimizer.hpp" #ifdef _WIN32 @@ -979,3 +980,67 @@ extern "C" request.set(*inRequest); return glslang_compile_1_1(&request); } + +extern "C" +#ifdef _MSC_VER + _declspec(dllexport) +#else + __attribute__((__visibility__("default"))) +#endif + int glslang_linkSPIRV(glslang_LinkRequest* request) +{ + if (!request || !request->modules || request->linkResult) + return false; + + try + { + spvtools::Context context(SPV_ENV_UNIVERSAL_1_5); + spvtools::LinkerOptions options = {}; + + spvtools::MessageConsumer consumer = [](spv_message_level_t level, + const char* source, + const spv_position_t& position, + const char* message) + { + printf("SPIRV-TOOLS: %s\n", message); + printf("SPIRV-TOOLS: %s\n", source); + printf("SPIRV-TOOLS: %zu:%zu\n", position.index, position.column); + }; + context.SetMessageConsumer(consumer); + + std::vector> moduleVecs(request->moduleCount); + std::vector moduleData(request->moduleCount); + std::vector moduleSizes(request->moduleCount); + + for (size_t i = 0; i < request->moduleCount; ++i) + { + moduleData[i] = request->modules[i]; + moduleSizes[i] = request->moduleSizes[i]; + } + + std::vector linkedBinary; + spv_result_t success = spvtools::Link( + context, + moduleData.data(), + moduleSizes.data(), + request->moduleCount, + &linkedBinary, + options); + + if (success == SPV_SUCCESS) + { + request->linkResult = new uint32_t[linkedBinary.size()]; + memcpy( + (void*)request->linkResult, + linkedBinary.data(), + linkedBinary.size() * sizeof(uint32_t)); + request->linkResultSize = linkedBinary.size(); + } + + return success; + } + catch (...) + { + return false; + } +} -- cgit v1.2.3