diff options
| author | cheneym2 <acheney@nvidia.com> | 2025-02-26 21:20:29 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-26 18:20:29 -0800 |
| commit | 02706dfc5f0526f4f8ca337be16d7b00640ba168 (patch) | |
| tree | 08a7b1a94aaa048a07864bf7dec68ab23fb00edb /tools/gfx/vulkan/glslang-module.cpp | |
| parent | 6e862bb370c1f64abf0e7f9efa73dec38a76555e (diff) | |
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 <yonghe@outlook.com>
Diffstat (limited to 'tools/gfx/vulkan/glslang-module.cpp')
| -rw-r--r-- | tools/gfx/vulkan/glslang-module.cpp | 108 |
1 files changed, 108 insertions, 0 deletions
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 <assert.h> +#include <stdio.h> +#include <stdlib.h> + +#if SLANG_WINDOWS_FAMILY +#include <windows.h> +#else +#include <dlfcn.h> +#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<ISlangBlob> GlslangModule::linkSPIRV(List<ComPtr<ISlangBlob>> spirvModules) +{ + + if (!m_linkSPIRVFunc) + { + return nullptr; + } + + glslang_LinkRequest request = {}; + + std::vector<const uint32_t*> moduleCodePtrs(spirvModules.getCount()); + std::vector<uint32_t> 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<ISlangBlob> linkedSPIRV; + linkedSPIRV = RawBlob::create(request.linkResult, request.linkResultSize * sizeof(uint32_t)); + return linkedSPIRV; +} + +} // namespace gfx |
