diff options
Diffstat (limited to 'source/slang/slang-dxc-support.cpp')
| -rwxr-xr-x | source/slang/slang-dxc-support.cpp | 80 |
1 files changed, 79 insertions, 1 deletions
diff --git a/source/slang/slang-dxc-support.cpp b/source/slang/slang-dxc-support.cpp index 6f8151921..b0938ed12 100755 --- a/source/slang/slang-dxc-support.cpp +++ b/source/slang/slang-dxc-support.cpp @@ -45,6 +45,82 @@ namespace Slang return UnownedStringSlice(); } + // IDxcIncludeHandler + // 7f61fc7d-950d-467f-b3e3-3c02fb49187c + static const Guid IID_IDxcIncludeHandler = { 0x7f61fc7d, 0x950d, 0x467f, { 0x3c, 0x02, 0xfb, 0x49, 0x18, 0x7c } }; + static const Guid IID_IUnknown = SLANG_UUID_ISlangUnknown; + + class DxcIncludeHandler : public IDxcIncludeHandler, public RefObject + { + public: + // Implement IUnknown + SLANG_NO_THROW HRESULT SLANG_MCALL QueryInterface(const IID& uuid, void** out) + { + ISlangUnknown* intf = getInterface(reinterpret_cast<const Guid&>(uuid)); + if (intf) + { + addReference(); + *out = intf; + return SLANG_OK; + } + return SLANG_E_NO_INTERFACE; + } + SLANG_NO_THROW ULONG SLANG_MCALL AddRef() SLANG_OVERRIDE { return (uint32_t)addReference(); } + SLANG_NO_THROW ULONG SLANG_MCALL Release() SLANG_OVERRIDE { return (uint32_t)releaseReference(); } + + // Implement IDxcIncludeHandler + virtual HRESULT SLANG_MCALL LoadSource(LPCWSTR inFilename, IDxcBlob** outSource) SLANG_OVERRIDE + { + // Hmm DXC does something a bit odd - when it sees a path, it just passes that in with ./ in front!! + // NOTE! It doesn't make any difference if it is "" or <> quoted. + + // So we just do a work around where we strip if we see a path starting with ./ + String filePath = String::fromWString(inFilename); + + // If it starts with ./ then attempt to strip it + if (filePath.startsWith("./")) + { + String remaining(filePath.subString(2, filePath.getLength() - 2)); + + // Okay if we strip ./ and what we have is absolute, then it's the absolute path that we care about, + // otherwise we just leave as is. + if (Path::isAbsolute(remaining)) + { + filePath = remaining; + } + } + + ComPtr<ISlangBlob> blob; + PathInfo pathInfo; + SlangResult res = m_system.findAndLoadFile(filePath, String(), pathInfo, blob); + + // NOTE! This only works because ISlangBlob is *binary compatible* with IDxcBlob, if either + // change things could go boom + *outSource = (IDxcBlob*)blob.detach(); + return res; + } + + DxcIncludeHandler(SearchDirectoryList* searchDirectories, ISlangFileSystemExt* fileSystemExt, SourceManager* sourceManager = nullptr) : + m_system(searchDirectories, fileSystemExt, sourceManager) + { + } + + protected: + + // Used by QueryInterface for casting + ISlangUnknown* getInterface(const Guid& guid) + { + if (guid == IID_IUnknown || guid == IID_IDxcIncludeHandler) + { + return (ISlangUnknown*)(static_cast<IDxcIncludeHandler*>(this)); + } + return nullptr; + } + + IncludeSystem m_system; + }; + + SlangResult emitDXILForEntryPointUsingDXC( ComponentType* program, BackEndCompileRequest* compileRequest, @@ -194,6 +270,8 @@ namespace Slang const String sourcePath = calcSourcePathForEntryPoint(endToEndReq, entryPointIndex); + ComPtr<DxcIncludeHandler> includeHandler(new DxcIncludeHandler(&linkage->searchDirectories, linkage->getFileSystemExt())); + ComPtr<IDxcOperationResult> dxcResult; SLANG_RETURN_ON_FAIL(dxcCompiler->Compile(dxcSourceBlob, sourcePath.toWString().begin(), @@ -203,7 +281,7 @@ namespace Slang argCount, nullptr, // `#define`s 0, // `#define` count - nullptr, // `#include` handler + includeHandler, // `#include` handler dxcResult.writeRef())); // Retrieve result. |
