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