summaryrefslogtreecommitdiff
path: root/source/compiler-core/slang-fxc-compiler.cpp
diff options
context:
space:
mode:
authorEllie Hermaszewska <ellieh@nvidia.com>2024-10-29 14:49:26 +0800
committerGitHub <noreply@github.com>2024-10-29 14:49:26 +0800
commitf65d756bff8d4c5cbc15bd0322a2ae8e6b896a21 (patch)
treeea1d61342cd29368e19135000ec2948813096205 /source/compiler-core/slang-fxc-compiler.cpp
parenta729c15e9dce9f5116a38afc66329ab2ca4cea54 (diff)
format
* format * Minor test fixes * enable checking cpp format in ci
Diffstat (limited to 'source/compiler-core/slang-fxc-compiler.cpp')
-rw-r--r--source/compiler-core/slang-fxc-compiler.cpp167
1 files changed, 100 insertions, 67 deletions
diff --git a/source/compiler-core/slang-fxc-compiler.cpp b/source/compiler-core/slang-fxc-compiler.cpp
index e4a9f25c7..0c571c10b 100644
--- a/source/compiler-core/slang-fxc-compiler.cpp
+++ b/source/compiler-core/slang-fxc-compiler.cpp
@@ -3,43 +3,36 @@
#if SLANG_ENABLE_DXBC_SUPPORT
-#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 "slang-include-system.h"
-#include "slang-source-loc.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-artifact-diagnostic-util.h"
-
-#include "../core/slang-shared-library.h"
+#include "slang-com-helper.h"
+#include "slang-include-system.h"
+#include "slang-source-loc.h"
// Enable calling through to `fxc` or `dxc` to
// generate code on Windows.
#ifdef _WIN32
-# include <windows.h>
-# include <d3dcompiler.h>
+#include <d3dcompiler.h>
+#include <windows.h>
#endif
// Some of the `D3DCOMPILE_*` constants aren't available in all
// versions of `d3dcompiler.h`, so we define them here just in case
#ifndef D3DCOMPILE_ENABLE_UNBOUNDED_DESCRIPTOR_TABLES
-# define D3DCOMPILE_ENABLE_UNBOUNDED_DESCRIPTOR_TABLES (1 << 20)
+#define D3DCOMPILE_ENABLE_UNBOUNDED_DESCRIPTOR_TABLES (1 << 20)
#endif
#ifndef D3DCOMPILE_ALL_RESOURCES_BOUND
-# define D3DCOMPILE_ALL_RESOURCES_BOUND (1 << 21)
+#define D3DCOMPILE_ALL_RESOURCES_BOUND (1 << 21)
#endif
#endif // SLANG_ENABLE_DXBC_SUPPORT
@@ -49,25 +42,35 @@ namespace Slang
#if SLANG_ENABLE_DXBC_SUPPORT
-static UnownedStringSlice _getSlice(ID3DBlob* blob) { return StringUtil::getSlice((ISlangBlob*)blob); }
+static UnownedStringSlice _getSlice(ID3DBlob* blob)
+{
+ return StringUtil::getSlice((ISlangBlob*)blob);
+}
struct FxcIncludeHandler : ID3DInclude
{
- STDMETHOD(Open)(D3D_INCLUDE_TYPE includeType, LPCSTR fileName, LPCVOID parentData, LPCVOID* outData, UINT* outSize) override
+ STDMETHOD(Open)
+ (D3D_INCLUDE_TYPE includeType,
+ LPCSTR fileName,
+ LPCVOID parentData,
+ LPCVOID* outData,
+ UINT* outSize) override
{
SLANG_UNUSED(includeType);
// NOTE! The pParentData means the *text* of any previous include.
- // In order to work out what *path* that came from, we need to seach which source file it came from, and
- // use it's path
+ // In order to work out what *path* that came from, we need to seach which source file it
+ // came from, and use it's path
- // Assume the root pathInfo initially
+ // Assume the root pathInfo initially
PathInfo includedFromPathInfo = m_rootPathInfo;
// Lets try and find the parent source if there is any
if (parentData)
{
- SourceFile* foundSourceFile = m_system.getSourceManager()->findSourceFileByContentRecursively((const char*)parentData);
+ SourceFile* foundSourceFile =
+ m_system.getSourceManager()->findSourceFileByContentRecursively(
+ (const char*)parentData);
if (foundSourceFile)
{
includedFromPathInfo = foundSourceFile->getPathInfo();
@@ -78,7 +81,8 @@ struct FxcIncludeHandler : ID3DInclude
PathInfo pathInfo;
ComPtr<ISlangBlob> blob;
- SLANG_RETURN_ON_FAIL(m_system.findAndLoadFile(path, includedFromPathInfo.foundPath, pathInfo, blob));
+ SLANG_RETURN_ON_FAIL(
+ m_system.findAndLoadFile(path, includedFromPathInfo.foundPath, pathInfo, blob));
// Return the data
*outData = blob->getBufferPointer();
@@ -92,8 +96,11 @@ struct FxcIncludeHandler : ID3DInclude
SLANG_UNUSED(pData);
return S_OK;
}
- FxcIncludeHandler(SearchDirectoryList* searchDirectories, ISlangFileSystemExt* fileSystemExt, SourceManager* sourceManager) :
- m_system(searchDirectories, fileSystemExt, sourceManager)
+ FxcIncludeHandler(
+ SearchDirectoryList* searchDirectories,
+ ISlangFileSystemExt* fileSystemExt,
+ SourceManager* sourceManager)
+ : m_system(searchDirectories, fileSystemExt, sourceManager)
{
}
@@ -107,22 +114,24 @@ public:
typedef DownstreamCompilerBase Super;
// IDownstreamCompiler
- virtual SLANG_NO_THROW SlangResult SLANG_MCALL compile(const CompileOptions& options, IArtifact** outArtifact) 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** outArtifact) 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; }
- /// Must be called before use
+ /// Must be called before use
SlangResult init(ISlangSharedLibrary* library);
FXCDownstreamCompiler() {}
-
-protected:
+protected:
pD3DCompile m_compile = nullptr;
pD3DDisassemble m_disassemble = nullptr;
- ComPtr<ISlangSharedLibrary> m_sharedLibrary;
+ ComPtr<ISlangSharedLibrary> m_sharedLibrary;
};
SlangResult FXCDownstreamCompiler::init(ISlangSharedLibrary* library)
@@ -143,21 +152,29 @@ SlangResult FXCDownstreamCompiler::init(ISlangSharedLibrary* library)
return SLANG_OK;
}
-static SlangResult _parseDiagnosticLine(SliceAllocator& allocator, const UnownedStringSlice& line, List<UnownedStringSlice>& lineSlices, ArtifactDiagnostic& outDiagnostic)
+static SlangResult _parseDiagnosticLine(
+ SliceAllocator& allocator,
+ const UnownedStringSlice& line,
+ List<UnownedStringSlice>& lineSlices,
+ ArtifactDiagnostic& outDiagnostic)
{
- /* tests/diagnostics/syntax-error-intrinsic.slang(14,2): error X3000: syntax error: unexpected token '@' */
+ /* tests/diagnostics/syntax-error-intrinsic.slang(14,2): error X3000: syntax error: unexpected
+ * token '@' */
if (lineSlices.getCount() < 3)
{
return SLANG_FAIL;
}
- SLANG_RETURN_ON_FAIL(ArtifactDiagnosticUtil::splitPathLocation(allocator, lineSlices[0], outDiagnostic));
+ SLANG_RETURN_ON_FAIL(
+ ArtifactDiagnosticUtil::splitPathLocation(allocator, lineSlices[0], outDiagnostic));
{
const UnownedStringSlice severityAndCodeSlice = lineSlices[1].trim();
- const UnownedStringSlice severitySlice = StringUtil::getAtInSplit(severityAndCodeSlice, ' ', 0);
+ const UnownedStringSlice severitySlice =
+ StringUtil::getAtInSplit(severityAndCodeSlice, ' ', 0);
- outDiagnostic.code = allocator.allocate(StringUtil::getAtInSplit(severityAndCodeSlice, ' ', 1));
+ outDiagnostic.code =
+ allocator.allocate(StringUtil::getAtInSplit(severityAndCodeSlice, ' ', 1));
outDiagnostic.severity = ArtifactDiagnostic::Severity::Error;
if (severitySlice == "warning")
@@ -212,10 +229,13 @@ SlangResult FXCDownstreamCompiler::compile(const CompileOptions& inOptions, IArt
// Use the default fileSystemExt is not set
ID3DInclude* includeHandler = nullptr;
- FxcIncludeHandler fxcIncludeHandlerStorage(&searchDirectories, options.fileSystemExt, options.sourceManager);
+ FxcIncludeHandler fxcIncludeHandlerStorage(
+ &searchDirectories,
+ options.fileSystemExt,
+ options.sourceManager);
if (options.fileSystemExt)
{
-
+
if (sourcePath.getLength() > 0)
{
fxcIncludeHandlerStorage.m_rootPathInfo = PathInfo::makePath(sourcePath);
@@ -235,7 +255,7 @@ SlangResult FXCDownstreamCompiler::compile(const CompileOptions& inOptions, IArt
dxMacro.Definition = define.value;
dxMacrosStorage.add(dxMacro);
}
- D3D_SHADER_MACRO nullTerminator = { 0, 0 };
+ D3D_SHADER_MACRO nullTerminator = {0, 0};
dxMacrosStorage.add(nullTerminator);
dxMacros = dxMacrosStorage.getBuffer();
@@ -245,12 +265,9 @@ SlangResult FXCDownstreamCompiler::compile(const CompileOptions& inOptions, IArt
switch (options.floatingPointMode)
{
- default:
- break;
+ default: break;
- case FloatingPointMode::Precise:
- flags |= D3DCOMPILE_IEEE_STRICTNESS;
- break;
+ case FloatingPointMode::Precise: flags |= D3DCOMPILE_IEEE_STRICTNESS; break;
}
flags |= D3DCOMPILE_ENABLE_STRICTNESS;
@@ -258,23 +275,19 @@ SlangResult FXCDownstreamCompiler::compile(const CompileOptions& inOptions, IArt
switch (options.optimizationLevel)
{
- default:
- break;
+ default: break;
- case OptimizationLevel::None: flags |= D3DCOMPILE_OPTIMIZATION_LEVEL0; break;
- case OptimizationLevel::Default: flags |= D3DCOMPILE_OPTIMIZATION_LEVEL1; break;
- case OptimizationLevel::High: flags |= D3DCOMPILE_OPTIMIZATION_LEVEL2; break;
- case OptimizationLevel::Maximal: flags |= D3DCOMPILE_OPTIMIZATION_LEVEL3; break;
+ case OptimizationLevel::None: flags |= D3DCOMPILE_OPTIMIZATION_LEVEL0; break;
+ case OptimizationLevel::Default: flags |= D3DCOMPILE_OPTIMIZATION_LEVEL1; break;
+ case OptimizationLevel::High: flags |= D3DCOMPILE_OPTIMIZATION_LEVEL2; break;
+ case OptimizationLevel::Maximal: flags |= D3DCOMPILE_OPTIMIZATION_LEVEL3; break;
}
switch (options.debugInfoType)
{
- case DebugInfoType::None:
- break;
+ case DebugInfoType::None: break;
- default:
- flags |= D3DCOMPILE_DEBUG;
- break;
+ default: flags |= D3DCOMPILE_DEBUG; break;
}
ComPtr<ISlangBlob> sourceBlob;
@@ -307,7 +320,12 @@ SlangResult FXCDownstreamCompiler::compile(const CompileOptions& inOptions, IArt
UnownedStringSlice diagnosticText = _getSlice(diagnosticsBlob);
diagnostics->setRaw(asCharSlice(diagnosticText));
- SlangResult diagnosticParseRes = ArtifactDiagnosticUtil::parseColonDelimitedDiagnostics(allocator, diagnosticText, 0, _parseDiagnosticLine, diagnostics);
+ SlangResult diagnosticParseRes = ArtifactDiagnosticUtil::parseColonDelimitedDiagnostics(
+ allocator,
+ diagnosticText,
+ 0,
+ _parseDiagnosticLine,
+ diagnostics);
SLANG_UNUSED(diagnosticParseRes);
SLANG_ASSERT(SLANG_SUCCEEDED(diagnosticParseRes));
}
@@ -338,7 +356,10 @@ bool FXCDownstreamCompiler::canConvert(const ArtifactDesc& from, const ArtifactD
return ArtifactDescUtil::isDisassembly(from, to) && from.payload == ArtifactPayload::DXBC;
}
-SlangResult FXCDownstreamCompiler::convert(IArtifact* from, const ArtifactDesc& to, IArtifact** outArtifact)
+SlangResult FXCDownstreamCompiler::convert(
+ IArtifact* from,
+ const ArtifactDesc& to,
+ IArtifact** outArtifact)
{
if (!canConvert(from->getDesc(), to))
{
@@ -349,22 +370,31 @@ SlangResult FXCDownstreamCompiler::convert(IArtifact* from, const ArtifactDesc&
SLANG_RETURN_ON_FAIL(from->loadBlob(ArtifactKeep::No, dxbcBlob.writeRef()));
ComPtr<ID3DBlob> disassemblyBlob;
- SLANG_RETURN_ON_FAIL(m_disassemble(dxbcBlob->getBufferPointer(), dxbcBlob->getBufferSize(), 0, nullptr, disassemblyBlob.writeRef()));
+ SLANG_RETURN_ON_FAIL(m_disassemble(
+ dxbcBlob->getBufferPointer(),
+ dxbcBlob->getBufferSize(),
+ 0,
+ nullptr,
+ disassemblyBlob.writeRef()));
auto artifact = ArtifactUtil::createArtifact(to);
// ISlangBlob is compatible with ID3DBlob
artifact->addRepresentationUnknown((ISlangBlob*)disassemblyBlob.get());
- *outArtifact= artifact.detach();
+ *outArtifact = artifact.detach();
return SLANG_OK;
}
-/* static */SlangResult FXCDownstreamCompilerUtil::locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set)
+/* static */ SlangResult FXCDownstreamCompilerUtil::locateCompilers(
+ const String& path,
+ ISlangSharedLibraryLoader* loader,
+ DownstreamCompilerSet* set)
{
ComPtr<ISlangSharedLibrary> library;
const char* const libName = "d3dcompiler_47";
- SLANG_RETURN_ON_FAIL(DownstreamCompilerUtil::loadSharedLibrary(path, loader, nullptr, libName, library));
+ SLANG_RETURN_ON_FAIL(
+ DownstreamCompilerUtil::loadSharedLibrary(path, loader, nullptr, libName, library));
SLANG_ASSERT(library);
if (!library)
@@ -383,7 +413,10 @@ SlangResult FXCDownstreamCompiler::convert(IArtifact* from, const ArtifactDesc&
#else // SLANG_ENABLE_DXBC_SUPPORT
-/* static */SlangResult FXCDownstreamCompilerUtil::locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set)
+/* static */ SlangResult FXCDownstreamCompilerUtil::locateCompilers(
+ const String& path,
+ ISlangSharedLibraryLoader* loader,
+ DownstreamCompilerSet* set)
{
SLANG_UNUSED(path);
SLANG_UNUSED(loader);
@@ -393,4 +426,4 @@ SlangResult FXCDownstreamCompiler::convert(IArtifact* from, const ArtifactDesc&
#endif // else SLANG_ENABLE_DXBC_SUPPORT
-}
+} // namespace Slang