diff options
| author | Ellie Hermaszewska <ellieh@nvidia.com> | 2024-10-29 14:49:26 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-29 14:49:26 +0800 |
| commit | f65d756bff8d4c5cbc15bd0322a2ae8e6b896a21 (patch) | |
| tree | ea1d61342cd29368e19135000ec2948813096205 /source/compiler-core/slang-tint-compiler.cpp | |
| parent | a729c15e9dce9f5116a38afc66329ab2ca4cea54 (diff) | |
format
* format
* Minor test fixes
* enable checking cpp format in ci
Diffstat (limited to 'source/compiler-core/slang-tint-compiler.cpp')
| -rw-r--r-- | source/compiler-core/slang-tint-compiler.cpp | 250 |
1 files changed, 119 insertions, 131 deletions
diff --git a/source/compiler-core/slang-tint-compiler.cpp b/source/compiler-core/slang-tint-compiler.cpp index 6319ecf9f..c656f9ba1 100644 --- a/source/compiler-core/slang-tint-compiler.cpp +++ b/source/compiler-core/slang-tint-compiler.cpp @@ -1,164 +1,152 @@ #include "slang-tint-compiler.h" -#include "slang-artifact-associated-impl.h" - #include "../../external/slang-tint-headers/slang-tint.h" +#include "slang-artifact-associated-impl.h" namespace Slang { - class TintDownstreamCompiler : public DownstreamCompilerBase - { - - public: - - // IDownstreamCompiler - virtual SLANG_NO_THROW SlangResult SLANG_MCALL compile( - const CompileOptions& options, IArtifact** outResult) SLANG_OVERRIDE; - - virtual SLANG_NO_THROW bool SLANG_MCALL canConvert( - const ArtifactDesc& from, const ArtifactDesc& to) SLANG_OVERRIDE; +class TintDownstreamCompiler : public DownstreamCompilerBase +{ - virtual SLANG_NO_THROW SlangResult SLANG_MCALL convert( - IArtifact* from, const ArtifactDesc& to, IArtifact** outArtifact) - SLANG_OVERRIDE; +public: + // IDownstreamCompiler + virtual SLANG_NO_THROW SlangResult SLANG_MCALL + compile(const CompileOptions& options, IArtifact** outResult) SLANG_OVERRIDE; - virtual SLANG_NO_THROW bool SLANG_MCALL isFileBased() SLANG_OVERRIDE - { - return false; - } + virtual SLANG_NO_THROW bool SLANG_MCALL + canConvert(const ArtifactDesc& from, const ArtifactDesc& to) SLANG_OVERRIDE; - virtual SLANG_NO_THROW SlangResult SLANG_MCALL getVersionString( - slang::IBlob** outVersionString) SLANG_OVERRIDE; + virtual SLANG_NO_THROW SlangResult SLANG_MCALL + convert(IArtifact* from, const ArtifactDesc& to, IArtifact** outArtifact) SLANG_OVERRIDE; - SlangResult compile(IArtifact *const sourceArtifact, IArtifact** outArtifact); + virtual SLANG_NO_THROW bool SLANG_MCALL isFileBased() SLANG_OVERRIDE { return false; } - SlangResult init(ISlangSharedLibrary* library); + virtual SLANG_NO_THROW SlangResult SLANG_MCALL getVersionString(slang::IBlob** outVersionString) + SLANG_OVERRIDE; - protected: + SlangResult compile(IArtifact* const sourceArtifact, IArtifact** outArtifact); - ComPtr<ISlangSharedLibrary> m_sharedLibrary; + SlangResult init(ISlangSharedLibrary* library); - private: +protected: + ComPtr<ISlangSharedLibrary> m_sharedLibrary; - tint_CompileFunc m_compile; - tint_FreeResultFunc m_freeResult; - }; +private: + tint_CompileFunc m_compile; + tint_FreeResultFunc m_freeResult; +}; - SlangResult TintDownstreamCompiler::init(ISlangSharedLibrary* library) +SlangResult TintDownstreamCompiler::init(ISlangSharedLibrary* library) +{ + tint_CompileFunc compile = (tint_CompileFunc)library->findFuncByName("tint_compile"); + if (compile == nullptr) { - tint_CompileFunc compile = - (tint_CompileFunc)library->findFuncByName("tint_compile"); - if (compile == nullptr) - { - return SLANG_FAIL; - } - - tint_FreeResultFunc freeResult = - (tint_FreeResultFunc)library->findFuncByName("tint_free_result"); - if (freeResult == nullptr) - { - return SLANG_FAIL; - } - - m_sharedLibrary = library; - m_desc = Desc(SLANG_PASS_THROUGH_TINT); - m_compile = compile; - m_freeResult = freeResult; - return SLANG_OK; + return SLANG_FAIL; } - SlangResult TintDownstreamCompilerUtil::locateCompilers( - const String& path, - ISlangSharedLibraryLoader* loader, - DownstreamCompilerSet* set) + tint_FreeResultFunc freeResult = + (tint_FreeResultFunc)library->findFuncByName("tint_free_result"); + if (freeResult == nullptr) { - ComPtr<ISlangSharedLibrary> library; - SLANG_RETURN_ON_FAIL(DownstreamCompilerUtil::loadSharedLibrary( - path, loader, nullptr, "slang-tint", library)); - SLANG_ASSERT(library); - - ComPtr<IDownstreamCompiler> compiler = ComPtr<IDownstreamCompiler>( - new TintDownstreamCompiler()); - SLANG_RETURN_ON_FAIL(static_cast<TintDownstreamCompiler*>( - compiler.get())->init(library)); - - set->addCompiler(compiler); - return SLANG_OK; + return SLANG_FAIL; } - SlangResult TintDownstreamCompiler::compile( - const CompileOptions& options, IArtifact** outArtifact) - { - IArtifact * sourceArtifact = options.sourceArtifacts[0]; - return compile(sourceArtifact, outArtifact); - } + m_sharedLibrary = library; + m_desc = Desc(SLANG_PASS_THROUGH_TINT); + m_compile = compile; + m_freeResult = freeResult; + return SLANG_OK; +} - SlangResult TintDownstreamCompiler::compile( - IArtifact *const sourceArtifact, IArtifact** outArtifact) - { - tint_CompileRequest req = {}; - - if (sourceArtifact == nullptr) - return SLANG_FAIL; - - ComPtr<ISlangBlob> sourceBlob; - SLANG_RETURN_FALSE_ON_FAIL(sourceArtifact->loadBlob( - ArtifactKeep::Yes, sourceBlob.writeRef())); - - String wgslCode( - (char*)sourceBlob->getBufferPointer(), - (char*)sourceBlob->getBufferPointer() + sourceBlob->getBufferSize()); - req.wgslCode = wgslCode.begin(); - req.wgslCodeLength = wgslCode.getLength(); - - tint_CompileResult result = {}; - SLANG_DEFER(m_freeResult(&result)); - bool compileSucceeded = m_compile(&req, &result) == 0; - - ComPtr<ISlangBlob> spirvBlob = RawBlob::create(result.buffer, result.bufferSize); - result.buffer = nullptr; - - ComPtr<IArtifact> resultArtifact = ArtifactUtil::createArtifactForCompileTarget( - SlangCompileTarget::SLANG_WGSL_SPIRV); - auto diagnostics = ArtifactDiagnostics::create(); - diagnostics->setResult(compileSucceeded ? SLANG_OK : SLANG_FAIL); - ArtifactUtil::addAssociated(resultArtifact, diagnostics); - if (compileSucceeded) - { - resultArtifact->addRepresentationUnknown(spirvBlob); - } - else - { - diagnostics->setRaw(CharSlice(result.error)); - diagnostics->requireErrorDiagnostic(); - } - - *outArtifact = resultArtifact.detach(); - return SLANG_OK; - } +SlangResult TintDownstreamCompilerUtil::locateCompilers( + const String& path, + ISlangSharedLibraryLoader* loader, + DownstreamCompilerSet* set) +{ + ComPtr<ISlangSharedLibrary> library; + SLANG_RETURN_ON_FAIL( + DownstreamCompilerUtil::loadSharedLibrary(path, loader, nullptr, "slang-tint", library)); + SLANG_ASSERT(library); - bool TintDownstreamCompiler::canConvert( - const ArtifactDesc& from, const ArtifactDesc& to) + ComPtr<IDownstreamCompiler> compiler = + ComPtr<IDownstreamCompiler>(new TintDownstreamCompiler()); + SLANG_RETURN_ON_FAIL(static_cast<TintDownstreamCompiler*>(compiler.get())->init(library)); + + set->addCompiler(compiler); + return SLANG_OK; +} + +SlangResult TintDownstreamCompiler::compile(const CompileOptions& options, IArtifact** outArtifact) +{ + IArtifact* sourceArtifact = options.sourceArtifacts[0]; + return compile(sourceArtifact, outArtifact); +} + +SlangResult TintDownstreamCompiler::compile( + IArtifact* const sourceArtifact, + IArtifact** outArtifact) +{ + tint_CompileRequest req = {}; + + if (sourceArtifact == nullptr) + return SLANG_FAIL; + + ComPtr<ISlangBlob> sourceBlob; + SLANG_RETURN_FALSE_ON_FAIL(sourceArtifact->loadBlob(ArtifactKeep::Yes, sourceBlob.writeRef())); + + String wgslCode( + (char*)sourceBlob->getBufferPointer(), + (char*)sourceBlob->getBufferPointer() + sourceBlob->getBufferSize()); + req.wgslCode = wgslCode.begin(); + req.wgslCodeLength = wgslCode.getLength(); + + tint_CompileResult result = {}; + SLANG_DEFER(m_freeResult(&result)); + bool compileSucceeded = m_compile(&req, &result) == 0; + + ComPtr<ISlangBlob> spirvBlob = RawBlob::create(result.buffer, result.bufferSize); + result.buffer = nullptr; + + ComPtr<IArtifact> resultArtifact = + ArtifactUtil::createArtifactForCompileTarget(SlangCompileTarget::SLANG_WGSL_SPIRV); + auto diagnostics = ArtifactDiagnostics::create(); + diagnostics->setResult(compileSucceeded ? SLANG_OK : SLANG_FAIL); + ArtifactUtil::addAssociated(resultArtifact, diagnostics); + if (compileSucceeded) { - return (from.payload == ArtifactPayload::WGSL) && - (to.payload == ArtifactPayload::SPIRV); + resultArtifact->addRepresentationUnknown(spirvBlob); } - - SlangResult TintDownstreamCompiler::convert( - IArtifact* from, const ArtifactDesc& to, IArtifact** outArtifact) + else { - if (!canConvert(from->getDesc(), to)) - return SLANG_FAIL; - return compile(from, outArtifact); + diagnostics->setRaw(CharSlice(result.error)); + diagnostics->requireErrorDiagnostic(); } - SlangResult TintDownstreamCompiler::getVersionString( - slang::IBlob** /* outVersionString */) - { - // We just use Tint at whatever version is in our Dawn fork, so nobody should - // depend on the particular version at the moment. + *outArtifact = resultArtifact.detach(); + return SLANG_OK; +} + +bool TintDownstreamCompiler::canConvert(const ArtifactDesc& from, const ArtifactDesc& to) +{ + return (from.payload == ArtifactPayload::WGSL) && (to.payload == ArtifactPayload::SPIRV); +} + +SlangResult TintDownstreamCompiler::convert( + IArtifact* from, + const ArtifactDesc& to, + IArtifact** outArtifact) +{ + if (!canConvert(from->getDesc(), to)) return SLANG_FAIL; - } + return compile(from, outArtifact); +} +SlangResult TintDownstreamCompiler::getVersionString(slang::IBlob** /* outVersionString */) +{ + // We just use Tint at whatever version is in our Dawn fork, so nobody should + // depend on the particular version at the moment. + return SLANG_FAIL; } + +} // namespace Slang |
