yum-mirror/slang

Making it easier to work with shaders

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

Ellie Hermaszewskaformatf65d756bf

master
1.8 KiB85 linesraw
1// module.cpp
2#include "vk-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// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! VulkanModule !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21
22Slang::Result VulkanModule::init(bool useSoftwareImpl)
23{
24    if (isInitialized())
25    {
26        destroy();
27    }
28
29    const char* dynamicLibraryName = "Unknown";
30    m_isSoftware = useSoftwareImpl;
31
32#if SLANG_WINDOWS_FAMILY
33    dynamicLibraryName = useSoftwareImpl ? "vk_swiftshader.dll" : "vulkan-1.dll";
34    HMODULE module = ::LoadLibraryA(dynamicLibraryName);
35    m_module = (void*)module;
36#elif SLANG_APPLE_FAMILY
37    dynamicLibraryName = useSoftwareImpl ? "libvk_swiftshader.dylib" : "libvulkan.1.dylib";
38    m_module = dlopen(dynamicLibraryName, RTLD_NOW | RTLD_GLOBAL);
39#else
40    dynamicLibraryName = useSoftwareImpl ? "libvk_swiftshader.so" : "libvulkan.so.1";
41    if (useSoftwareImpl)
42    {
43        dlopen("libpthread.so.0", RTLD_NOW | RTLD_GLOBAL);
44    }
45    m_module = dlopen(dynamicLibraryName, RTLD_NOW);
46#endif
47
48    if (!m_module)
49    {
50        return SLANG_FAIL;
51    }
52
53    return SLANG_OK;
54}
55
56PFN_vkVoidFunction VulkanModule::getFunction(const char* name) const
57{
58    assert(m_module);
59    if (!m_module)
60    {
61        return nullptr;
62    }
63#if SLANG_WINDOWS_FAMILY
64    return (PFN_vkVoidFunction)::GetProcAddress((HMODULE)m_module, name);
65#else
66    return (PFN_vkVoidFunction)dlsym(m_module, name);
67#endif
68}
69
70void VulkanModule::destroy()
71{
72    if (!isInitialized())
73    {
74        return;
75    }
76
77#if SLANG_WINDOWS_FAMILY
78    ::FreeLibrary((HMODULE)m_module);
79#else
80    dlclose(m_module);
81#endif
82    m_module = nullptr;
83}
84
85} // namespace gfx