Browse Source

Add the #pragma anki 16bit that enables 16bit types in DXC

Panagiotis Christopoulos Charitos 2 years ago
parent
commit
d0ce1cf0b7

+ 7 - 3
AnKi/ShaderCompiler/Dxc.cpp

@@ -50,8 +50,8 @@ static CString profile(ShaderType shaderType)
 	return "";
 }
 
-Error compileHlslToSpirv(CString src, ShaderType shaderType, BaseMemoryPool& tmpPool, DynamicArrayRaii<U8>& spirv,
-						 StringRaii& errorMessage)
+Error compileHlslToSpirv(CString src, ShaderType shaderType, Bool compileWith16bitTypes, BaseMemoryPool& tmpPool,
+						 DynamicArrayRaii<U8>& spirv, StringRaii& errorMessage)
 {
 	Array<U64, 3> toHash = {g_nextFileId.fetchAdd(1), getCurrentProcessId(), getRandom() & kMaxU32};
 	const U64 rand = computeHash(&toHash[0], sizeof(toHash));
@@ -83,7 +83,6 @@ Error compileHlslToSpirv(CString src, ShaderType shaderType, BaseMemoryPool& tmp
 	dxcArgs.emplaceBack(&tmpPool, "-Wfatal-errors");
 	dxcArgs.emplaceBack(&tmpPool, "-Wundef");
 	dxcArgs.emplaceBack(&tmpPool, "-Wno-unused-const-variable");
-	// dxcArgs.emplaceBack(&tmpPool, "-enable-16bit-types");
 	dxcArgs.emplaceBack(&tmpPool, "-HV");
 	dxcArgs.emplaceBack(&tmpPool, "2021");
 	dxcArgs.emplaceBack(&tmpPool, "-E");
@@ -94,6 +93,11 @@ Error compileHlslToSpirv(CString src, ShaderType shaderType, BaseMemoryPool& tmp
 	dxcArgs.emplaceBack(&tmpPool, "-fspv-target-env=vulkan1.1spirv1.4");
 	dxcArgs.emplaceBack(&tmpPool, hlslFilename);
 
+	if(compileWith16bitTypes)
+	{
+		dxcArgs.emplaceBack(&tmpPool, "-enable-16bit-types");
+	}
+
 	DynamicArrayRaii<CString> dxcArgs2(&tmpPool, dxcArgs.getSize());
 	for(U32 i = 0; i < dxcArgs.getSize(); ++i)
 	{

+ 2 - 2
AnKi/ShaderCompiler/Dxc.h

@@ -14,8 +14,8 @@ namespace anki {
 /// @{
 
 /// Compile HLSL to SPIR-V.
-Error compileHlslToSpirv(CString src, ShaderType shaderType, BaseMemoryPool& tmpPool, DynamicArrayRaii<U8>& spirv,
-						 StringRaii& errorMessage);
+Error compileHlslToSpirv(CString src, ShaderType shaderType, Bool compileWith16bitTypes, BaseMemoryPool& tmpPool,
+						 DynamicArrayRaii<U8>& spirv, StringRaii& errorMessage);
 /// @}
 
 } // end namespace anki

+ 2 - 2
AnKi/ShaderCompiler/ShaderProgramCompiler.cpp

@@ -188,8 +188,8 @@ static Error compileSpirv(ConstWeakArray<MutatorValue> mutation, const ShaderPro
 		}
 		else
 		{
-			ANKI_CHECK(compileHlslToSpirv(parserVariant.getSource(shaderType), shaderType, tempPool, spirv[shaderType],
-										  errorLog));
+			ANKI_CHECK(compileHlslToSpirv(parserVariant.getSource(shaderType), shaderType,
+										  parser.compileWith16bitTypes(), tempPool, spirv[shaderType], errorLog));
 		}
 		ANKI_ASSERT(spirv[shaderType].getSize() > 0);
 	}

+ 28 - 1
AnKi/ShaderCompiler/ShaderProgramParser.cpp

@@ -536,6 +536,10 @@ Error ShaderProgramParser::parseLine(CString line, CString fname, Bool& foundPra
 			{
 				ANKI_CHECK(parsePragmaHlsl(token + 1, end, line, fname));
 			}
+			else if(*token == "16bit")
+			{
+				ANKI_CHECK(parsePragma16bit(token + 1, end, line, fname));
+			}
 			else
 			{
 				ANKI_PP_ERROR_MALFORMED();
@@ -839,6 +843,21 @@ Error ShaderProgramParser::parsePragmaHlsl(const StringRaii* begin, const String
 	return Error::kNone;
 }
 
+Error ShaderProgramParser::parsePragma16bit(const StringRaii* begin, const StringRaii* end, CString line, CString fname)
+{
+	ANKI_ASSERT(begin && end);
+
+	// Check tokens
+	if(begin != end)
+	{
+		ANKI_PP_ERROR_MALFORMED();
+	}
+
+	m_16bitTypes = true;
+
+	return Error::kNone;
+}
+
 Error ShaderProgramParser::parseFile(CString fname, U32 depth)
 {
 	// First check the depth
@@ -943,7 +962,7 @@ Error ShaderProgramParser::parse()
 
 	if(!m_hlsl)
 	{
-		m_codeLines.pushFront(StringRaii(m_pool, "#extension  GL_GOOGLE_cpp_style_line_directive : enable"));
+		m_codeLines.pushFront(StringRaii(m_pool, "#extension GL_GOOGLE_cpp_style_line_directive : enable"));
 	}
 
 	// Create the code lines
@@ -1016,6 +1035,14 @@ Error ShaderProgramParser::generateVariant(ConstWeakArray<MutatorValue> mutation
 		// Create the final source without the bindings
 		StringRaii finalSource(m_pool);
 		finalSource.append(header);
+		if(m_16bitTypes)
+		{
+			finalSource.append("#define ANKI_SUPPORTS_16BIT_TYPES 1\n");
+		}
+		else
+		{
+			finalSource.append("#define ANKI_SUPPORTS_16BIT_TYPES 0\n");
+		}
 		finalSource.append(mutatorDefines);
 		finalSource.append(m_codeSource);
 

+ 8 - 0
AnKi/ShaderCompiler/ShaderProgramParser.h

@@ -113,6 +113,7 @@ private:
 /// #pragma anki reflect NAME
 /// #pragma anki skip_mutation MUTATOR0 VALUE0 MUTATOR1 VALUE1 [MUTATOR2 VALUE2 ...]
 /// #pragma anki hlsl // By default it's GLSL
+/// #pragma anki 16bit // Works only in HLSL. Gain 16bit types but loose min16xxx types
 ///
 /// #pragma anki struct NAME
 /// #	pragma anki member [ANKI_RP] TYPE NAME [if MUTATOR_NAME is MUTATOR_VALUE]
@@ -182,6 +183,11 @@ public:
 		return m_hlsl;
 	}
 
+	Bool compileWith16bitTypes() const
+	{
+		return m_16bitTypes;
+	}
+
 	/// Generates the common header that will be used by all AnKi shaders.
 	static void generateAnkiShaderHeader(ShaderType shaderType, const ShaderCompilerOptions& compilerOptions,
 										 StringRaii& header);
@@ -228,6 +234,7 @@ private:
 	Bool m_insideStruct = false;
 
 	Bool m_hlsl = false;
+	Bool m_16bitTypes = false;
 
 	Error parseFile(CString fname, U32 depth);
 	Error parseLine(CString line, CString fname, Bool& foundPragmaOnce, U32 depth, U32 lineNumber);
@@ -243,6 +250,7 @@ private:
 	Error parsePragmaStructEnd(const StringRaii* begin, const StringRaii* end, CString line, CString fname);
 	Error parsePragmaMember(const StringRaii* begin, const StringRaii* end, CString line, CString fname);
 	Error parsePragmaHlsl(const StringRaii* begin, const StringRaii* end, CString line, CString fname);
+	Error parsePragma16bit(const StringRaii* begin, const StringRaii* end, CString line, CString fname);
 
 	void tokenizeLine(CString line, DynamicArrayRaii<StringRaii>& tokens) const;
 

+ 0 - 2
AnKi/Shaders/Include/Common.h

@@ -52,8 +52,6 @@ ANKI_END_NAMESPACE
 
 #	define constexpr static const
 
-#	define ANKI_SUPPORTS_16BIT_TYPES 0
-
 template<typename T>
 void maybeUnused(T a)
 {