Skip to content

Commit e3b2bc7

Browse files
committed
Add missing files
1 parent 0bfc2cc commit e3b2bc7

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

tools/gfx/vulkan/glslang-module.cpp

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

tools/gfx/vulkan/glslang-module.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// glslang-module.h
2+
#pragma once
3+
4+
#include "slang-com-ptr.h"
5+
#include "slang-com-helper.h"
6+
#include "core/slang-list.h"
7+
#include "slang.h"
8+
#include "slang-glslang/slang-glslang.h"
9+
#include "external/spirv-tools/include/spirv-tools/linker.hpp"
10+
11+
namespace gfx
12+
{
13+
14+
struct GlslangModule
15+
{
16+
/// true if has been initialized
17+
SLANG_FORCE_INLINE bool isInitialized() const { return m_module != nullptr; }
18+
19+
/// Initialize
20+
Slang::Result init();
21+
22+
/// Destroy
23+
void destroy();
24+
25+
/// Dtor
26+
~GlslangModule() { destroy(); }
27+
28+
Slang::ComPtr<ISlangBlob> linkSPIRV(Slang::List<Slang::ComPtr<ISlangBlob>> spirvModules);
29+
30+
protected:
31+
void* m_module = nullptr;
32+
33+
glslang_LinkSPIRVFunc m_linkSPIRVFunc = nullptr;
34+
};
35+
36+
} // namespace gfx

0 commit comments

Comments
 (0)