Browse Source

Minor additions to the shader compiler

Panagiotis Christopoulos Charitos 1 year ago
parent
commit
ad4e66311f

+ 13 - 8
AnKi/ShaderCompiler/Dxc.cpp

@@ -58,8 +58,8 @@ static CString profile(ShaderType shaderType)
 	return "";
 }
 
-static Error compileHlsl(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool debugInfo, Bool spirv,
-						 ShaderCompilerDynamicArray<U8>& bin, ShaderCompilerString& errorMessage)
+static Error compileHlsl(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool debugInfo, ConstWeakArray<CString> compilerArgs,
+						 Bool spirv, ShaderCompilerDynamicArray<U8>& bin, ShaderCompilerString& errorMessage)
 {
 	Array<U64, 3> toHash = {g_nextFileId.fetchAdd(1), getCurrentProcessId(), getRandom() & kMaxU32};
 	const U64 rand = computeHash(&toHash[0], sizeof(toHash));
@@ -149,6 +149,11 @@ static Error compileHlsl(CString src, ShaderType shaderType, Bool compileWith16b
 		dxcArgs.emplaceBack("-enable-16bit-types");
 	}
 
+	for(CString extraArg : compilerArgs)
+	{
+		dxcArgs.emplaceBack(extraArg);
+	}
+
 	ShaderCompilerDynamicArray<CString> dxcArgs2;
 	dxcArgs2.resize(U32(dxcArgs.getSize()));
 	U32 count = 0;
@@ -222,16 +227,16 @@ static Error compileHlsl(CString src, ShaderType shaderType, Bool compileWith16b
 	return Error::kNone;
 }
 
-Error compileHlslToSpirv(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool debugInfo, ShaderCompilerDynamicArray<U8>& spirv,
-						 ShaderCompilerString& errorMessage)
+Error compileHlslToSpirv(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool debugInfo, ConstWeakArray<CString> compilerArgs,
+						 ShaderCompilerDynamicArray<U8>& spirv, ShaderCompilerString& errorMessage)
 {
-	return compileHlsl(src, shaderType, compileWith16bitTypes, debugInfo, true, spirv, errorMessage);
+	return compileHlsl(src, shaderType, compileWith16bitTypes, debugInfo, compilerArgs, true, spirv, errorMessage);
 }
 
-Error compileHlslToDxil(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool debugInfo, ShaderCompilerDynamicArray<U8>& dxil,
-						ShaderCompilerString& errorMessage)
+Error compileHlslToDxil(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool debugInfo, ConstWeakArray<CString> compilerArgs,
+						ShaderCompilerDynamicArray<U8>& dxil, ShaderCompilerString& errorMessage)
 {
-	return compileHlsl(src, shaderType, compileWith16bitTypes, debugInfo, false, dxil, errorMessage);
+	return compileHlsl(src, shaderType, compileWith16bitTypes, debugInfo, compilerArgs, false, dxil, errorMessage);
 }
 
 } // end namespace anki

+ 5 - 4
AnKi/ShaderCompiler/Dxc.h

@@ -7,6 +7,7 @@
 
 #include <AnKi/ShaderCompiler/Common.h>
 #include <AnKi/Util/String.h>
+#include <AnKi/Util/WeakArray.h>
 
 namespace anki {
 
@@ -20,12 +21,12 @@ inline constexpr Array2d<U32, kMaxDescriptorSets, U32(HlslResourceType::kCount)>
 inline constexpr U32 kDxcVkBindlessRegisterSpace = 1000000;
 
 /// Compile HLSL to SPIR-V.
-Error compileHlslToSpirv(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool debugInfo, ShaderCompilerDynamicArray<U8>& spirv,
-						 ShaderCompilerString& errorMessage);
+Error compileHlslToSpirv(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool debugInfo, ConstWeakArray<CString> compilerArgs,
+						 ShaderCompilerDynamicArray<U8>& spirv, ShaderCompilerString& errorMessage);
 
 /// Compile HLSL to DXIL.
-Error compileHlslToDxil(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool debugInfo, ShaderCompilerDynamicArray<U8>& dxil,
-						ShaderCompilerString& errorMessage);
+Error compileHlslToDxil(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool debugInfo, ConstWeakArray<CString> compilerArgs,
+						ShaderCompilerDynamicArray<U8>& dxil, ShaderCompilerString& errorMessage);
 /// @}
 
 } // end namespace anki

+ 4 - 2
AnKi/ShaderCompiler/ShaderCompiler.cpp

@@ -762,11 +762,13 @@ static void compileVariantAsync(const ShaderParser& parser, Bool spirv, Bool deb
 				ShaderCompilerDynamicArray<U8> il;
 				if(ctx.m_spirv)
 				{
-					err = compileHlslToSpirv(source, shaderType, ctx.m_parser->compileWith16bitTypes(), ctx.m_debugInfo, il, compilerErrorLog);
+					err = compileHlslToSpirv(source, shaderType, ctx.m_parser->compileWith16bitTypes(), ctx.m_debugInfo,
+											 ctx.m_parser->getExtraCompilerArgs(), il, compilerErrorLog);
 				}
 				else
 				{
-					err = compileHlslToDxil(source, shaderType, ctx.m_parser->compileWith16bitTypes(), ctx.m_debugInfo, il, compilerErrorLog);
+					err = compileHlslToDxil(source, shaderType, ctx.m_parser->compileWith16bitTypes(), ctx.m_debugInfo,
+											ctx.m_parser->getExtraCompilerArgs(), il, compilerErrorLog);
 				}
 
 				if(err)

+ 37 - 0
AnKi/ShaderCompiler/ShaderParser.cpp

@@ -567,6 +567,10 @@ Error ShaderParser::parseLine(CString line, CString fname, Bool& foundPragmaOnce
 			{
 				ANKI_CHECK(parsePragma16bit(token + 1, end, line, fname));
 			}
+			else if(*token == "extra_compiler_args")
+			{
+				ANKI_CHECK(parseExtraCompilerArgs(token + 1, end, line, fname));
+			}
 			else
 			{
 				ANKI_PP_ERROR_MALFORMED();
@@ -725,6 +729,28 @@ Error ShaderParser::parsePragma16bit(const ShaderCompilerString* begin, const Sh
 	return Error::kNone;
 }
 
+Error ShaderParser::parseExtraCompilerArgs(const ShaderCompilerString* begin, const ShaderCompilerString* end, CString line, CString fname)
+{
+	ANKI_ASSERT(begin && end);
+
+	if(begin >= end)
+	{
+		ANKI_PP_ERROR_MALFORMED();
+	}
+
+	for(; begin < end; ++begin)
+	{
+		if(tokenIsComment(begin->toCString()))
+		{
+			break;
+		}
+
+		m_extraCompilerArgs.emplaceBack(*begin);
+	}
+
+	return Error::kNone;
+}
+
 Error ShaderParser::parseFile(CString fname, U32 depth)
 {
 	// First check the depth
@@ -830,6 +856,17 @@ Error ShaderParser::parse()
 		}
 	}
 
+	// Copy the extra compiler args to a better structure
+	if(m_extraCompilerArgs.getSize() > 0)
+	{
+		m_extraCompilerArgsCString.resize(m_extraCompilerArgs.getSize());
+
+		for(U32 i = 0; i < m_extraCompilerArgs.getSize(); ++i)
+		{
+			m_extraCompilerArgsCString[i] = m_extraCompilerArgs[i];
+		}
+	}
+
 	m_commonSourceLines.destroy(); // Free mem
 
 	return Error::kNone;

+ 10 - 0
AnKi/ShaderCompiler/ShaderParser.h

@@ -63,6 +63,7 @@ public:
 /// #pragma anki 16bit // Works only in HLSL. Gain 16bit types but loose min16xxx types
 /// #pragma anki technique_start STAGE [NAME] [uses_mutators [USES_MUTATOR1 [USES_MUTATOR2 ...]]]
 /// #pragma anki technique_end STAGE [NAME]
+/// #pragma anki extra_compiler_args ARG0 [ARG1 [ARG2...]]
 ///
 /// #pragma anki struct NAME
 /// #	pragma anki member TYPE NAME
@@ -117,6 +118,11 @@ public:
 		return m_16bitTypes;
 	}
 
+	ConstWeakArray<CString> getExtraCompilerArgs() const
+	{
+		return m_extraCompilerArgsCString;
+	}
+
 	/// Generates the common header that will be used by all AnKi shaders.
 	static void generateAnkiShaderHeader(ShaderType shaderType, ShaderCompilerString& header);
 
@@ -164,6 +170,9 @@ private:
 
 	Bool m_16bitTypes = false;
 
+	ShaderCompilerDynamicArray<ShaderCompilerString> m_extraCompilerArgs;
+	ShaderCompilerDynamicArray<CString> m_extraCompilerArgsCString;
+
 	ShaderCompilerStringList& getAppendSourceList()
 	{
 		return (insideTechnique()) ? m_techniqueExtras[m_insideTechniqueIdx].m_sourceLines[m_insideTechniqueShaderType] : m_commonSourceLines;
@@ -185,6 +194,7 @@ private:
 	Error parsePragmaStructEnd(const ShaderCompilerString* begin, const ShaderCompilerString* end, CString line, CString fname);
 	Error parsePragmaMember(const ShaderCompilerString* begin, const ShaderCompilerString* end, CString line, CString fname);
 	Error parsePragma16bit(const ShaderCompilerString* begin, const ShaderCompilerString* end, CString line, CString fname);
+	Error parseExtraCompilerArgs(const ShaderCompilerString* begin, const ShaderCompilerString* end, CString line, CString fname);
 
 	void tokenizeLine(CString line, ShaderCompilerDynamicArray<ShaderCompilerString>& tokens) const;
 

+ 10 - 1
AnKi/Util/File.cpp

@@ -441,7 +441,16 @@ PtrSize File::tell()
 	else
 #endif
 	{
-		return ftell(ANKI_CFILE);
+#if ANKI_OS_WINDOWS
+		const auto offset = _ftelli64(ANKI_CFILE);
+#else
+		const auto offset = ftell(ANKI_CFILE);
+#endif
+		if(offset < 0)
+		{
+			ANKI_UTIL_LOGF("ftell overflow");
+		}
+		return offset;
 	}
 }
 

+ 2 - 2
Tests/Gr/GrCommon.h

@@ -20,9 +20,9 @@ inline ShaderPtr createShader(CString src, ShaderType type)
 	ShaderCompilerString errorLog;
 
 #if ANKI_GR_BACKEND_VULKAN
-	Error err = compileHlslToSpirv(header, type, false, true, bin, errorLog);
+	Error err = compileHlslToSpirv(header, type, false, true, {}, bin, errorLog);
 #else
-	Error err = compileHlslToDxil(header, type, false, true, bin, errorLog);
+	Error err = compileHlslToDxil(header, type, false, true, {}, bin, errorLog);
 #endif
 	if(err)
 	{