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 --- tools/gfx/vulkan/glslang-module.cpp | 108 ++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 tools/gfx/vulkan/glslang-module.cpp (limited to 'tools/gfx/vulkan/glslang-module.cpp') diff --git a/tools/gfx/vulkan/glslang-module.cpp b/tools/gfx/vulkan/glslang-module.cpp new file mode 100644 index 000000000..03afef794 --- /dev/null +++ b/tools/gfx/vulkan/glslang-module.cpp @@ -0,0 +1,108 @@ +// glslang-module.cpp +#include "glslang-module.h" + +#include +#include +#include + +#if SLANG_WINDOWS_FAMILY +#include +#else +#include +#endif + +#include "../renderer-shared.h" + +namespace gfx +{ +using namespace Slang; + +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! GlslangModule +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +Slang::Result GlslangModule::init() +{ + if (isInitialized()) + { + destroy(); + } + + const char* dynamicLibraryName = "Unknown"; + +#if SLANG_WINDOWS_FAMILY + dynamicLibraryName = "slang-glslang.dll"; + HMODULE module = ::LoadLibraryA(dynamicLibraryName); + m_module = (void*)module; +#elif SLANG_APPLE_FAMILY + dynamicLibraryName = "libslang_glslang.dylib"; + m_module = dlopen(dynamicLibraryName, RTLD_NOW | RTLD_GLOBAL); +#else + dynamicLibraryName = "libslang_glslang.so"; + m_module = dlopen(dynamicLibraryName, RTLD_NOW); +#endif + + if (!m_module) + { + return SLANG_FAIL; + } + + // Load functions +#if SLANG_WINDOWS_FAMILY + m_linkSPIRVFunc = (glslang_LinkSPIRVFunc)GetProcAddress((HMODULE)m_module, "glslang_linkSPIRV"); +#else + m_linkSPIRVFunc = (glslang_LinkSPIRVFunc)dlsym(m_module, "glslang_linkSPIRV"); +#endif + if (!m_linkSPIRVFunc) + { + return SLANG_FAIL; + } + + return SLANG_OK; +} + +void GlslangModule::destroy() +{ + if (!isInitialized()) + { + return; + } + +#if SLANG_WINDOWS_FAMILY + ::FreeLibrary((HMODULE)m_module); +#else + dlclose(m_module); +#endif + m_module = nullptr; +} + +ComPtr GlslangModule::linkSPIRV(List> spirvModules) +{ + + if (!m_linkSPIRVFunc) + { + return nullptr; + } + + glslang_LinkRequest request = {}; + + std::vector moduleCodePtrs(spirvModules.getCount()); + std::vector moduleSizes(spirvModules.getCount()); + for (Index i = 0; i < spirvModules.getCount(); ++i) + { + moduleCodePtrs[i] = (const uint32_t*)spirvModules[i]->getBufferPointer(); + moduleSizes[i] = spirvModules[i]->getBufferSize() / sizeof(uint32_t); + SLANG_ASSERT(spirvModules[i]->getBufferSize() % sizeof(uint32_t) == 0); + } + request.modules = moduleCodePtrs.data(); + request.moduleSizes = moduleSizes.data(); + request.moduleCount = spirvModules.getCount(); + request.linkResult = nullptr; + + m_linkSPIRVFunc(&request); + + ComPtr linkedSPIRV; + linkedSPIRV = RawBlob::create(request.linkResult, request.linkResultSize * sizeof(uint32_t)); + return linkedSPIRV; +} + +} // namespace gfx -- cgit v1.2.3