From 1185bd464092f372430cbfaa15a7be4dcaa90752 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Tue, 6 Nov 2018 14:28:25 -0500 Subject: Feature/shared library refactor (#712) * * Added ISlangSharedLibraryLoader and ISlangSharedLibrary * Implemented default implementations * Added slang API function to get/set the ISlangSharedLibraryLoader on the session * Put function caching onto the Session - so that if the loader is chaged, its easy to reset the shared libraries, and functions * Run premake. * Fix problem with setting null, would cause an unnecessary function/shared lib flush. * * Unload SharedLibrary when DefaultSharedLibrary is deleted. * Make SharedLibrary handle unload safely if already unloaded. * Refactor SharedLibrary, such that it becomes a utility class - simplifying it's semantics. * Simplified ISlangSharedLibrary such that doesn't have unload and isLoaded so easier to implement. Use updated SharedLibrary impl. * Disable aarch64 on windows * Premake windows files without aarch64 build. * Moved slang-shared-library to core (so can be used in code outside of main slang) Fixed problem in premake5 where on windows projects were incorrectly constructed * Allowed RefObject to base class of com types Added ConfigurableSharedLibraryLoader Added -dxc-path -fxc-path -glslang-path Fix problem with dxc-path not honoring it's path when loading dxil * Added documentation for command line control of dll loading paths. * Remove some tabbing issues. * Change name of include guard. --- source/core/slang-shared-library.cpp | 115 +++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 source/core/slang-shared-library.cpp (limited to 'source/core/slang-shared-library.cpp') diff --git a/source/core/slang-shared-library.cpp b/source/core/slang-shared-library.cpp new file mode 100644 index 000000000..716f570c4 --- /dev/null +++ b/source/core/slang-shared-library.cpp @@ -0,0 +1,115 @@ +#include "slang-shared-library.h" + +#include "../../slang-com-ptr.h" +#include "../core/slang-io.h" +#include "../core/slang-string-util.h" + +namespace Slang +{ + +// Allocate static const storage for the various interface IDs that the Slang API needs to expose +static const Guid IID_ISlangUnknown = SLANG_UUID_ISlangUnknown; +static const Guid IID_ISlangSharedLibrary = SLANG_UUID_ISlangSharedLibrary; +static const Guid IID_ISlangSharedLibraryLoader = SLANG_UUID_ISlangSharedLibraryLoader; + +/* !!!!!!!!!!!!!!!!!!!!!!!!!! DefaultSharedLibraryLoader !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ + +/* static */const char* DefaultSharedLibraryLoader::s_libraryNames[int(SharedLibraryType::CountOf)] = +{ + nullptr, // SharedLibraryType::Unknown + "dxcompiler", // SharedLibraryType::Dxc + "d3dcompiler_47", // SharedLibraryType::Fxc + "slang-glslang", // SharedLibraryType::Glslang + "dxil", // SharedLibraryType::Dxil +}; + +/* static */DefaultSharedLibraryLoader DefaultSharedLibraryLoader::s_singleton; + +/* static */SharedLibraryType DefaultSharedLibraryLoader::getSharedLibraryTypeFromName(const UnownedStringSlice& name) +{ + // Start from 1 to skip Unknown + for (int i = 1; i < SLANG_COUNT_OF(s_libraryNames); ++i) + { + if (name == s_libraryNames[i]) + { + return SharedLibraryType(i); + } + } + return SharedLibraryType::Unknown; +} + +ISlangUnknown* DefaultSharedLibraryLoader::getInterface(const Guid& guid) +{ + return (guid == IID_ISlangUnknown || guid == IID_ISlangSharedLibraryLoader) ? static_cast(this) : nullptr; +} + +SlangResult DefaultSharedLibraryLoader::loadSharedLibrary(const char* path, ISlangSharedLibrary** sharedLibraryOut) +{ + *sharedLibraryOut = nullptr; + // Try loading + SharedLibrary::Handle handle; + SLANG_RETURN_ON_FAIL(SharedLibrary::load(path, handle)); + *sharedLibraryOut = ComPtr(new DefaultSharedLibrary(handle)).detach(); + return SLANG_OK; +} + +/* !!!!!!!!!!!!!!!!!!!!!!!!!! DefaultSharedLibrary !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ + +ISlangUnknown* DefaultSharedLibrary::getInterface(const Guid& guid) +{ + return (guid == IID_ISlangUnknown || guid == IID_ISlangSharedLibrary) ? static_cast(this) : nullptr; +} + +DefaultSharedLibrary::~DefaultSharedLibrary() +{ + SharedLibrary::unload(m_sharedLibraryHandle); +} + +SlangFuncPtr DefaultSharedLibrary::findFuncByName(char const* name) +{ + return SharedLibrary::findFuncByName(m_sharedLibraryHandle, name); +} + +/* !!!!!!!!!!!!!!!!!!!!!!!!!! ConfigurableSharedLibraryLoader !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ + +ISlangUnknown* ConfigurableSharedLibraryLoader::getInterface(const Guid& guid) +{ + return (guid == IID_ISlangUnknown || guid == IID_ISlangSharedLibraryLoader) ? static_cast(this) : nullptr; +} + +SlangResult ConfigurableSharedLibraryLoader::loadSharedLibrary(const char* path, ISlangSharedLibrary** sharedLibraryOut) +{ + Entry* entry = m_entryMap.TryGetValue(String(path)); + if (entry) + { + SharedLibrary::Handle handle; + SLANG_RETURN_ON_FAIL(entry->func(path, entry->entryString, handle)); + SLANG_ASSERT(handle); + + ComPtr sharedLib(new DefaultSharedLibrary(handle)); + *sharedLibraryOut = sharedLib.detach(); + return SLANG_OK; + } + + return DefaultSharedLibraryLoader::getSingleton()->loadSharedLibrary(path, sharedLibraryOut); +} + +/* static */Result ConfigurableSharedLibraryLoader::replace(const char* pathIn, const String& entryString, SharedLibrary::Handle& handleOut) +{ + SLANG_UNUSED(pathIn); + // The replacement is the *whole* string + return SharedLibrary::loadWithPlatformFilename(entryString.begin(), handleOut); +} + +/* static */Result ConfigurableSharedLibraryLoader::changePath(const char* pathIn, const String& entryString, SharedLibrary::Handle& handleOut ) +{ + // Okay we need to reconstruct the name and insert the path + StringBuilder builder; + SharedLibrary::appendPlatformFileName(UnownedStringSlice(pathIn), builder); + String path = Path::Combine(entryString, builder); + + return SharedLibrary::loadWithPlatformFilename(path.begin(), handleOut); +} + + +} \ No newline at end of file -- cgit v1.2.3