yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

cheneym2Fix precompiledTargetModule tests (#6455)02706dfc5

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