summaryrefslogtreecommitdiffstats
path: root/tools/gfx/vulkan/glslang-module.cpp
blob: 03afef794712e837683b7bac81ccef07cd0986bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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