From f65d756bff8d4c5cbc15bd0322a2ae8e6b896a21 Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Tue, 29 Oct 2024 14:49:26 +0800 Subject: format * format * Minor test fixes * enable checking cpp format in ci --- source/compiler-core/slang-glslang-compiler.cpp | 149 ++++++++++++++---------- 1 file changed, 86 insertions(+), 63 deletions(-) (limited to 'source/compiler-core/slang-glslang-compiler.cpp') diff --git a/source/compiler-core/slang-glslang-compiler.cpp b/source/compiler-core/slang-glslang-compiler.cpp index 54635873a..fd80c7581 100644 --- a/source/compiler-core/slang-glslang-compiler.cpp +++ b/source/compiler-core/slang-glslang-compiler.cpp @@ -1,35 +1,28 @@ // slang-glslang-compiler.cpp #include "slang-glslang-compiler.h" -#include "../core/slang-common.h" -#include "slang-com-helper.h" - #include "../core/slang-blob.h" - -#include "../core/slang-string-util.h" -#include "../core/slang-string-slice-pool.h" - +#include "../core/slang-char-util.h" +#include "../core/slang-common.h" #include "../core/slang-io.h" -#include "../core/slang-shared-library.h" #include "../core/slang-semantic-version.h" -#include "../core/slang-char-util.h" - +#include "../core/slang-shared-library.h" +#include "../core/slang-string-slice-pool.h" +#include "../core/slang-string-util.h" #include "slang-artifact-associated-impl.h" #include "slang-artifact-desc-util.h" - +#include "slang-com-helper.h" #include "slang-include-system.h" #include "slang-source-loc.h" -#include "../core/slang-shared-library.h" - // Enable calling through to `glslang` on // all platforms. #ifndef SLANG_ENABLE_GLSLANG_SUPPORT -# define SLANG_ENABLE_GLSLANG_SUPPORT 1 +#define SLANG_ENABLE_GLSLANG_SUPPORT 1 #endif #if SLANG_ENABLE_GLSLANG_SUPPORT -# include "../slang-glslang/slang-glslang.h" +#include "../slang-glslang/slang-glslang.h" #endif namespace Slang @@ -43,24 +36,31 @@ public: typedef DownstreamCompilerBase Super; // 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; - virtual SLANG_NO_THROW SlangResult SLANG_MCALL convert(IArtifact* from, const ArtifactDesc& to, IArtifact** outArtifact) SLANG_OVERRIDE; + 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; + virtual SLANG_NO_THROW SlangResult SLANG_MCALL + convert(IArtifact* from, const ArtifactDesc& to, IArtifact** outArtifact) SLANG_OVERRIDE; virtual SLANG_NO_THROW bool SLANG_MCALL isFileBased() SLANG_OVERRIDE { return false; } - virtual SLANG_NO_THROW SlangResult SLANG_MCALL getVersionString(slang::IBlob** outVersionString) SLANG_OVERRIDE; - virtual SLANG_NO_THROW SlangResult SLANG_MCALL validate(const uint32_t* contents, int contentsSize) SLANG_OVERRIDE; + virtual SLANG_NO_THROW SlangResult SLANG_MCALL getVersionString(slang::IBlob** outVersionString) + SLANG_OVERRIDE; + virtual SLANG_NO_THROW SlangResult SLANG_MCALL + validate(const uint32_t* contents, int contentsSize) SLANG_OVERRIDE; - /// Must be called before use + /// Must be called before use SlangResult init(ISlangSharedLibrary* library); - GlslangDownstreamCompiler(SlangPassThrough compilerType) : m_compilerType(compilerType) {} - -protected: + GlslangDownstreamCompiler(SlangPassThrough compilerType) + : m_compilerType(compilerType) + { + } +protected: SlangResult _invoke(glslang_CompileRequest_1_2& request); - glslang_CompileFunc_1_0 m_compile_1_0 = nullptr; - glslang_CompileFunc_1_1 m_compile_1_1 = nullptr; + glslang_CompileFunc_1_0 m_compile_1_0 = nullptr; + glslang_CompileFunc_1_1 m_compile_1_1 = nullptr; glslang_CompileFunc_1_2 m_compile_1_2 = nullptr; glslang_ValidateSPIRVFunc m_validate = nullptr; @@ -135,7 +135,11 @@ SlangResult GlslangDownstreamCompiler::_invoke(glslang_CompileRequest_1_2& reque return err ? SLANG_FAIL : SLANG_OK; } -static SlangResult _parseDiagnosticLine(SliceAllocator& allocator, const UnownedStringSlice& line, List& lineSlices, ArtifactDiagnostic& outDiagnostic) +static SlangResult _parseDiagnosticLine( + SliceAllocator& allocator, + const UnownedStringSlice& line, + List& lineSlices, + ArtifactDiagnostic& outDiagnostic) { /* ERROR: tests/diagnostics/syntax-error-intrinsic.slang:13: '@' : unexpected token */ @@ -160,7 +164,9 @@ static SlangResult _parseDiagnosticLine(SliceAllocator& allocator, const Unowned return SLANG_OK; } -SlangResult GlslangDownstreamCompiler::compile(const CompileOptions& inOptions, IArtifact** outArtifact) +SlangResult GlslangDownstreamCompiler::compile( + const CompileOptions& inOptions, + IArtifact** outArtifact) { if (!isVersionCompatible(inOptions)) { @@ -186,14 +192,10 @@ SlangResult GlslangDownstreamCompiler::compile(const CompileOptions& inOptions, StringBuilder diagnosticOutput; auto diagnosticOutputFunc = [](void const* data, size_t size, void* userData) - { - (*(StringBuilder*)userData).append((char const*)data, (char const*)data + size); - }; + { (*(StringBuilder*)userData).append((char const*)data, (char const*)data + size); }; List spirv; auto outputFunc = [](void const* data, size_t size, void* userData) - { - ((List*)userData)->addRange((uint8_t*)data, size); - }; + { ((List*)userData)->addRange((uint8_t*)data, size); }; ComPtr sourceBlob; SLANG_RETURN_ON_FAIL(sourceArtifact->loadBlob(ArtifactKeep::Yes, sourceBlob.writeRef())); @@ -206,15 +208,9 @@ SlangResult GlslangDownstreamCompiler::compile(const CompileOptions& inOptions, switch (options.sourceLanguage) { - case SLANG_SOURCE_LANGUAGE_GLSL: - request.action = GLSLANG_ACTION_COMPILE_GLSL_TO_SPIRV; - break; - case SLANG_SOURCE_LANGUAGE_SPIRV: - request.action = GLSLANG_ACTION_OPTIMIZE_SPIRV; - break; - default: - SLANG_ASSERT(!"Can only handle GLSL or SPIR-V as input."); - return SLANG_FAIL; + case SLANG_SOURCE_LANGUAGE_GLSL: request.action = GLSLANG_ACTION_COMPILE_GLSL_TO_SPIRV; break; + case SLANG_SOURCE_LANGUAGE_SPIRV: request.action = GLSLANG_ACTION_OPTIMIZE_SPIRV; break; + default: SLANG_ASSERT(!"Can only handle GLSL or SPIR-V as input."); return SLANG_FAIL; } request.sourcePath = sourcePath.getBuffer(); @@ -270,7 +266,12 @@ SlangResult GlslangDownstreamCompiler::compile(const CompileOptions& inOptions, SliceAllocator allocator; - SlangResult diagnosticParseRes = ArtifactDiagnosticUtil::parseColonDelimitedDiagnostics(allocator, diagnosticOutput.getUnownedSlice(), 1, _parseDiagnosticLine, diagnostics); + SlangResult diagnosticParseRes = ArtifactDiagnosticUtil::parseColonDelimitedDiagnostics( + allocator, + diagnosticOutput.getUnownedSlice(), + 1, + _parseDiagnosticLine, + diagnostics); SLANG_UNUSED(diagnosticParseRes); diagnostics->requireErrorDiagnostic(); @@ -301,12 +302,15 @@ SlangResult GlslangDownstreamCompiler::validate(const uint32_t* contents, int co bool GlslangDownstreamCompiler::canConvert(const ArtifactDesc& from, const ArtifactDesc& to) { // Can only disassemble blobs that are SPIR-V - return ArtifactDescUtil::isDisassembly(from, to) && ( - (from.payload == ArtifactPayload::SPIRV) || - (from.payload == ArtifactPayload::WGSL_SPIRV)); + return ArtifactDescUtil::isDisassembly(from, to) && + ((from.payload == ArtifactPayload::SPIRV) || + (from.payload == ArtifactPayload::WGSL_SPIRV)); } -SlangResult GlslangDownstreamCompiler::convert(IArtifact* from, const ArtifactDesc& to, IArtifact** outArtifact) +SlangResult GlslangDownstreamCompiler::convert( + IArtifact* from, + const ArtifactDesc& to, + IArtifact** outArtifact) { if (!canConvert(from->getDesc(), to)) { @@ -317,11 +321,9 @@ SlangResult GlslangDownstreamCompiler::convert(IArtifact* from, const ArtifactDe SLANG_RETURN_ON_FAIL(from->loadBlob(ArtifactKeep::No, blob.writeRef())); StringBuilder builder; - + auto outputFunc = [](void const* data, size_t size, void* userData) - { - (*(StringBuilder*)userData).append((char const*)data, (char const*)data + size); - }; + { (*(StringBuilder*)userData).append((char const*)data, (char const*)data + size); }; glslang_CompileRequest_1_2 request; memset(&request, 0, sizeof(request)); @@ -366,14 +368,18 @@ SlangResult GlslangDownstreamCompiler::getVersionString(slang::IBlob** outVersio { return SLANG_FAIL; } - + auto timestampString = String(timestamp); ComPtr version = StringBlob::create(timestampString.getBuffer()); *outVersionString = version.detach(); return SLANG_OK; } -static SlangResult locateGlslangSpirvDownstreamCompiler(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set, SlangPassThrough compilerType) +static SlangResult locateGlslangSpirvDownstreamCompiler( + const String& path, + ISlangSharedLibraryLoader* loader, + DownstreamCompilerSet* set, + SlangPassThrough compilerType) { ComPtr library; @@ -381,18 +387,23 @@ static SlangResult locateGlslangSpirvDownstreamCompiler(const String& path, ISla // On unix systems we need to ensure pthread is loaded first. // TODO(JS): // There is an argument that this should be performed through the loader.... - // NOTE! We don't currently load through a dependent library, as it is *assumed* something as core as 'ptheads' - // isn't going to be distributed with the shader compiler. + // NOTE! We don't currently load through a dependent library, as it is *assumed* something as + // core as 'ptheads' isn't going to be distributed with the shader compiler. ComPtr pthreadLibrary; DefaultSharedLibraryLoader::load(loader, path, "pthread", pthreadLibrary.writeRef()); if (!pthreadLibrary.get()) { - DefaultSharedLibraryLoader::load(loader, path, "libpthread.so.0", pthreadLibrary.writeRef()); + DefaultSharedLibraryLoader::load( + loader, + path, + "libpthread.so.0", + pthreadLibrary.writeRef()); } #endif - SLANG_RETURN_ON_FAIL(DownstreamCompilerUtil::loadSharedLibrary(path, loader, nullptr, "slang-glslang", library)); + SLANG_RETURN_ON_FAIL( + DownstreamCompilerUtil::loadSharedLibrary(path, loader, nullptr, "slang-glslang", library)); SLANG_ASSERT(library); if (!library) @@ -408,24 +419,36 @@ static SlangResult locateGlslangSpirvDownstreamCompiler(const String& path, ISla return SLANG_OK; } -SlangResult GlslangDownstreamCompilerUtil::locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set) +SlangResult GlslangDownstreamCompilerUtil::locateCompilers( + const String& path, + ISlangSharedLibraryLoader* loader, + DownstreamCompilerSet* set) { return locateGlslangSpirvDownstreamCompiler(path, loader, set, SLANG_PASS_THROUGH_GLSLANG); } -SlangResult SpirvOptDownstreamCompilerUtil::locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set) +SlangResult SpirvOptDownstreamCompilerUtil::locateCompilers( + const String& path, + ISlangSharedLibraryLoader* loader, + DownstreamCompilerSet* set) { return locateGlslangSpirvDownstreamCompiler(path, loader, set, SLANG_PASS_THROUGH_SPIRV_OPT); } -SlangResult SpirvDisDownstreamCompilerUtil::locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set) +SlangResult SpirvDisDownstreamCompilerUtil::locateCompilers( + const String& path, + ISlangSharedLibraryLoader* loader, + DownstreamCompilerSet* set) { return locateGlslangSpirvDownstreamCompiler(path, loader, set, SLANG_PASS_THROUGH_SPIRV_DIS); } #else // SLANG_ENABLE_GLSLANG_SUPPORT -/* static */SlangResult GlslangDownstreamCompilerUtil::locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set) +/* static */ SlangResult GlslangDownstreamCompilerUtil::locateCompilers( + const String& path, + ISlangSharedLibraryLoader* loader, + DownstreamCompilerSet* set) { SLANG_UNUSED(path); SLANG_UNUSED(loader); @@ -435,4 +458,4 @@ SlangResult SpirvDisDownstreamCompilerUtil::locateCompilers(const String& path, #endif // SLANG_ENABLE_GLSLANG_SUPPORT -} +} // namespace Slang -- cgit v1.2.3