|
| 1 | +// glslang-module.cpp |
| 2 | +#include "glslang-module.h" |
| 3 | + |
| 4 | +#include <assert.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <stdlib.h> |
| 7 | + |
| 8 | +#if SLANG_WINDOWS_FAMILY |
| 9 | +#include <windows.h> |
| 10 | +#else |
| 11 | +#include <dlfcn.h> |
| 12 | +#endif |
| 13 | + |
| 14 | +#include "../renderer-shared.h" |
| 15 | + |
| 16 | +namespace gfx |
| 17 | +{ |
| 18 | +using namespace Slang; |
| 19 | + |
| 20 | +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! GlslangModule !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 21 | + |
| 22 | +Slang::Result GlslangModule::init() |
| 23 | +{ |
| 24 | + if (isInitialized()) |
| 25 | + { |
| 26 | + destroy(); |
| 27 | + } |
| 28 | + |
| 29 | + const char* dynamicLibraryName = "Unknown"; |
| 30 | + |
| 31 | +#if SLANG_WINDOWS_FAMILY |
| 32 | + dynamicLibraryName = "slang-glslang.dll"; |
| 33 | + HMODULE module = ::LoadLibraryA(dynamicLibraryName); |
| 34 | + m_module = (void*)module; |
| 35 | +#elif SLANG_APPLE_FAMILY |
| 36 | + dynamicLibraryName = "libslang_glslang.dylib"; |
| 37 | + m_module = dlopen(dynamicLibraryName, RTLD_NOW | RTLD_GLOBAL); |
| 38 | +#else |
| 39 | + dynamicLibraryName = "libslang_glslang.so"; |
| 40 | + m_module = dlopen(dynamicLibraryName, RTLD_NOW); |
| 41 | +#endif |
| 42 | + |
| 43 | + if (!m_module) |
| 44 | + { |
| 45 | + return SLANG_FAIL; |
| 46 | + } |
| 47 | + |
| 48 | + // Load functions |
| 49 | +#if SLANG_WINDOWS_FAMILY |
| 50 | + m_linkSPIRVFunc = (glslang_LinkSPIRVFunc)GetProcAddress((HMODULE)m_module, "glslang_linkSPIRV"); |
| 51 | +#else |
| 52 | + m_linkSPIRVFunc = (glslang_LinkSPIRVFunc)dlsym(m_module, "glslang_linkSPIRV"); |
| 53 | +#endif |
| 54 | + if (!m_linkSPIRVFunc) |
| 55 | + { |
| 56 | + return SLANG_FAIL; |
| 57 | + } |
| 58 | + |
| 59 | + return SLANG_OK; |
| 60 | +} |
| 61 | + |
| 62 | +void GlslangModule::destroy() |
| 63 | +{ |
| 64 | + if (!isInitialized()) |
| 65 | + { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | +#if SLANG_WINDOWS_FAMILY |
| 70 | + ::FreeLibrary((HMODULE)m_module); |
| 71 | +#else |
| 72 | + dlclose(m_module); |
| 73 | +#endif |
| 74 | + m_module = nullptr; |
| 75 | +} |
| 76 | + |
| 77 | +ComPtr<ISlangBlob> GlslangModule::linkSPIRV(List<ComPtr<ISlangBlob>> spirvModules) |
| 78 | +{ |
| 79 | + |
| 80 | + if (!m_linkSPIRVFunc) |
| 81 | + { |
| 82 | + return nullptr; |
| 83 | + } |
| 84 | + |
| 85 | + glslang_LinkRequest request = {}; |
| 86 | + |
| 87 | + std::vector<const uint32_t*> moduleCodePtrs(spirvModules.getCount()); |
| 88 | + std::vector<uint32_t> moduleSizes(spirvModules.getCount()); |
| 89 | + for (Index i = 0; i < spirvModules.getCount(); ++i) |
| 90 | + { |
| 91 | + moduleCodePtrs[i] = (const uint32_t*)spirvModules[i]->getBufferPointer(); |
| 92 | + moduleSizes[i] = spirvModules[i]->getBufferSize() / sizeof(uint32_t); |
| 93 | + SLANG_ASSERT(spirvModules[i]->getBufferSize() % sizeof(uint32_t) == 0); |
| 94 | + } |
| 95 | + request.modules = moduleCodePtrs.data(); |
| 96 | + request.moduleSizes = moduleSizes.data(); |
| 97 | + request.moduleCount = spirvModules.getCount(); |
| 98 | + request.linkResult = nullptr; |
| 99 | + |
| 100 | + m_linkSPIRVFunc(&request); |
| 101 | + |
| 102 | + ComPtr<ISlangBlob> linkedSPIRV; |
| 103 | + linkedSPIRV = RawBlob::create(request.linkResult, request.linkResultSize * sizeof(uint32_t)); |
| 104 | + return linkedSPIRV; |
| 105 | +} |
| 106 | + |
| 107 | +} // namespace gfx |
0 commit comments