Browse Source

Remove some specialization from the shader compiler

Panagiotis Christopoulos Charitos 4 years ago
parent
commit
37d8faedde

+ 4 - 5
AnKi/Resource/ShaderProgramResourceSystem.cpp

@@ -65,10 +65,9 @@ Error ShaderProgramResourceSystem::compileAllShaders(CString cacheDir, GrManager
 	ThreadHive threadHive(getCpuCoresCount(), alloc, false);
 
 	// Compute hash for both
-	const GpuDeviceCapabilities caps = gr.getDeviceCapabilities();
-	const BindlessLimits limits = gr.getBindlessLimits();
-	U64 gpuHash = computeHash(&caps, sizeof(caps));
-	gpuHash = appendHash(&limits, sizeof(limits), gpuHash);
+	ShaderCompilerOptions compilerOptions;
+	compilerOptions.m_bindlessLimits = gr.getBindlessLimits();
+	U64 gpuHash = computeHash(&compilerOptions, sizeof(compilerOptions));
 	gpuHash = appendHash(&SHADER_BINARY_VERSION, sizeof(SHADER_BINARY_VERSION), gpuHash);
 
 	ANKI_CHECK(fs.iterateAllFilenames([&](CString fname) -> Error {
@@ -201,7 +200,7 @@ Error ShaderProgramResourceSystem::compileAllShaders(CString cacheDir, GrManager
 
 		// Compile
 		ShaderProgramBinaryWrapper binary(alloc);
-		ANKI_CHECK(compileShaderProgram(fname, fsystem, &skip, &taskManager, alloc, caps, limits, binary));
+		ANKI_CHECK(compileShaderProgram(fname, fsystem, &skip, &taskManager, alloc, compilerOptions, binary));
 
 		const Bool cachedBinIsUpToDate = metafileHash == skip.m_newHash;
 		if(!cachedBinIsUpToDate)

+ 8 - 0
AnKi/ShaderCompiler/Common.h

@@ -8,6 +8,7 @@
 #include <AnKi/Util/Logger.h>
 #include <AnKi/Util/String.h>
 #include <AnKi/Util/BitSet.h>
+#include <AnKi/Gr/Common.h>
 
 namespace anki
 {
@@ -46,6 +47,13 @@ public:
 
 	virtual ANKI_USE_RESULT Error joinTasks() = 0;
 };
+
+/// Options to be passed to the compiler.
+class ShaderCompilerOptions
+{
+public:
+	BindlessLimits m_bindlessLimits;
+};
 /// @}
 
 } // end namespace anki

+ 4 - 6
AnKi/ShaderCompiler/ShaderProgramCompiler.cpp

@@ -767,8 +767,7 @@ Error compileShaderProgramInternal(CString fname, ShaderProgramFilesystemInterfa
 								   ShaderProgramPostParseInterface* postParseCallback,
 								   ShaderProgramAsyncTaskInterface* taskManager_,
 								   GenericMemoryPoolAllocator<U8> tempAllocator,
-								   const GpuDeviceCapabilities& gpuCapabilities, const BindlessLimits& bindlessLimits,
-								   ShaderProgramBinaryWrapper& binaryW)
+								   const ShaderCompilerOptions& compilerOptions, ShaderProgramBinaryWrapper& binaryW)
 {
 	// Initialize the binary
 	binaryW.cleanup();
@@ -780,7 +779,7 @@ Error compileShaderProgramInternal(CString fname, ShaderProgramFilesystemInterfa
 	memcpy(&binary.m_magic[0], SHADER_BINARY_MAGIC, 8);
 
 	// Parse source
-	ShaderProgramParser parser(fname, &fsystem, tempAllocator, gpuCapabilities, bindlessLimits);
+	ShaderProgramParser parser(fname, &fsystem, tempAllocator, compilerOptions);
 	ANKI_CHECK(parser.parse());
 
 	if(postParseCallback && postParseCallback->skipCompilation(parser.getHash()))
@@ -1008,11 +1007,10 @@ Error compileShaderProgramInternal(CString fname, ShaderProgramFilesystemInterfa
 Error compileShaderProgram(CString fname, ShaderProgramFilesystemInterface& fsystem,
 						   ShaderProgramPostParseInterface* postParseCallback,
 						   ShaderProgramAsyncTaskInterface* taskManager, GenericMemoryPoolAllocator<U8> tempAllocator,
-						   const GpuDeviceCapabilities& gpuCapabilities, const BindlessLimits& bindlessLimits,
-						   ShaderProgramBinaryWrapper& binaryW)
+						   const ShaderCompilerOptions& compilerOptions, ShaderProgramBinaryWrapper& binaryW)
 {
 	const Error err = compileShaderProgramInternal(fname, fsystem, postParseCallback, taskManager, tempAllocator,
-												   gpuCapabilities, bindlessLimits, binaryW);
+												   compilerOptions, binaryW);
 	if(err)
 	{
 		ANKI_SHADER_COMPILER_LOGE("Failed to compile: %s", fname.cstr());

+ 4 - 4
AnKi/ShaderCompiler/ShaderProgramCompiler.h

@@ -25,8 +25,8 @@ class ShaderProgramBinaryWrapper : public NonCopyable
 											  ShaderProgramPostParseInterface* postParseCallback,
 											  ShaderProgramAsyncTaskInterface* taskManager,
 											  GenericMemoryPoolAllocator<U8> tempAllocator,
-											  const GpuDeviceCapabilities& gpuCapabilities,
-											  const BindlessLimits& bindlessLimits, ShaderProgramBinaryWrapper& binary);
+											  const ShaderCompilerOptions& compilerOptions,
+											  ShaderProgramBinaryWrapper& binary);
 
 public:
 	ShaderProgramBinaryWrapper(GenericMemoryPoolAllocator<U8> alloc)
@@ -62,8 +62,8 @@ ANKI_USE_RESULT Error compileShaderProgram(CString fname, ShaderProgramFilesyste
 										   ShaderProgramPostParseInterface* postParseCallback,
 										   ShaderProgramAsyncTaskInterface* taskManager,
 										   GenericMemoryPoolAllocator<U8> tempAllocator,
-										   const GpuDeviceCapabilities& gpuCapabilities,
-										   const BindlessLimits& bindlessLimits, ShaderProgramBinaryWrapper& binary);
+										   const ShaderCompilerOptions& compilerOptions,
+										   ShaderProgramBinaryWrapper& binary);
 /// @}
 
 } // end namespace anki

+ 9 - 14
AnKi/ShaderCompiler/ShaderProgramParser.cpp

@@ -21,9 +21,6 @@ static const Array<CString, U32(ShaderType::COUNT)> SHADER_STAGE_NAMES = {
 	 "ANY_HIT", "CLOSEST_HIT", "MISS", "INTERSECTION", "CALLABLE"}};
 
 static const char* SHADER_HEADER = R"(#version 460 core
-#define ANKI_BACKEND_MINOR %u
-#define ANKI_BACKEND_MAJOR %u
-#define ANKI_VENDOR_%s 1
 #define ANKI_%s_SHADER 1
 
 #define gl_VertexID gl_VertexIndex
@@ -63,7 +60,7 @@ static const char* SHADER_HEADER = R"(#version 460 core
 #define ANKI_MAX_BINDLESS_TEXTURES %u
 #define ANKI_MAX_BINDLESS_IMAGES %u
 
-#if %u || defined(ANKI_RAY_GEN_SHADER) || defined(ANKI_ANY_HIT_SHADER) || defined(ANKI_CLOSEST_HIT_SHADER) || defined(ANKI_MISS_SHADER) || defined(ANKI_INTERSECTION_SHADER) || defined(ANKI_CALLABLE_SHADER)
+#if defined(ANKI_RAY_GEN_SHADER) || defined(ANKI_ANY_HIT_SHADER) || defined(ANKI_CLOSEST_HIT_SHADER) || defined(ANKI_MISS_SHADER) || defined(ANKI_INTERSECTION_SHADER) || defined(ANKI_CALLABLE_SHADER)
 #	extension GL_EXT_ray_tracing : enable
 #endif
 
@@ -236,13 +233,11 @@ static const U64 SHADER_HEADER_HASH = computeHash(SHADER_HEADER, sizeof(SHADER_H
 
 ShaderProgramParser::ShaderProgramParser(CString fname, ShaderProgramFilesystemInterface* fsystem,
 										 GenericMemoryPoolAllocator<U8> alloc,
-										 const GpuDeviceCapabilities& gpuCapabilities,
-										 const BindlessLimits& bindlessLimits)
+										 const ShaderCompilerOptions& compilerOptions)
 	: m_alloc(alloc)
 	, m_fname(alloc, fname)
 	, m_fsystem(fsystem)
-	, m_gpuCapabilities(gpuCapabilities)
-	, m_bindlessLimits(bindlessLimits)
+	, m_compilerOptions(compilerOptions)
 {
 }
 
@@ -906,12 +901,12 @@ Error ShaderProgramParser::parse()
 	return Error::NONE;
 }
 
-void ShaderProgramParser::generateAnkiShaderHeader(ShaderType shaderType, const GpuDeviceCapabilities& caps,
-												   const BindlessLimits& limits, StringAuto& header)
+void ShaderProgramParser::generateAnkiShaderHeader(ShaderType shaderType, const ShaderCompilerOptions& compilerOptions,
+												   StringAuto& header)
 {
-	header.sprintf(SHADER_HEADER, caps.m_minorApiVersion, caps.m_majorApiVersion,
-				   GPU_VENDOR_STR[caps.m_gpuVendor].cstr(), SHADER_STAGE_NAMES[shaderType].cstr(),
-				   limits.m_bindlessTextureCount, limits.m_bindlessImageCount, U(caps.m_rayTracingEnabled));
+	header.sprintf(SHADER_HEADER, SHADER_STAGE_NAMES[shaderType].cstr(),
+				   compilerOptions.m_bindlessLimits.m_bindlessTextureCount,
+				   compilerOptions.m_bindlessLimits.m_bindlessImageCount);
 }
 
 Error ShaderProgramParser::generateVariant(ConstWeakArray<MutatorValue> mutation,
@@ -946,7 +941,7 @@ Error ShaderProgramParser::generateVariant(ConstWeakArray<MutatorValue> mutation
 
 		// Create the header
 		StringAuto header(m_alloc);
-		generateAnkiShaderHeader(shaderType, m_gpuCapabilities, m_bindlessLimits, header);
+		generateAnkiShaderHeader(shaderType, m_compilerOptions, header);
 
 		// Create the final source without the bindings
 		StringAuto finalSource(m_alloc);

+ 4 - 5
AnKi/ShaderCompiler/ShaderProgramParser.h

@@ -91,7 +91,7 @@ class ShaderProgramParser : public NonCopyable
 {
 public:
 	ShaderProgramParser(CString fname, ShaderProgramFilesystemInterface* fsystem, GenericMemoryPoolAllocator<U8> alloc,
-						const GpuDeviceCapabilities& gpuCapabilities, const BindlessLimits& bindlessLimits);
+						const ShaderCompilerOptions& compilerOptions);
 
 	~ShaderProgramParser();
 
@@ -133,8 +133,8 @@ public:
 	}
 
 	/// Generates the common header that will be used by all AnKi shaders.
-	static void generateAnkiShaderHeader(ShaderType shaderType, const GpuDeviceCapabilities& caps,
-										 const BindlessLimits& limits, StringAuto& header);
+	static void generateAnkiShaderHeader(ShaderType shaderType, const ShaderCompilerOptions& compilerOptions,
+										 StringAuto& header);
 
 private:
 	using Mutator = ShaderProgramParserMutator;
@@ -179,8 +179,7 @@ private:
 
 	ShaderTypeBit m_shaderTypes = ShaderTypeBit::NONE;
 	Bool m_insideShader = false;
-	GpuDeviceCapabilities m_gpuCapabilities;
-	BindlessLimits m_bindlessLimits;
+	ShaderCompilerOptions m_compilerOptions;
 
 	StringAuto m_libName = {m_alloc};
 	U32 m_rayType = MAX_U32;

+ 1 - 37
AnKi/Shaders/Common.glsl

@@ -10,38 +10,13 @@
 #include <AnKi/Shaders/Include/Common.h>
 #include <AnKi/Shaders/TextureFunctions.glsl>
 
-// WORKAROUNDS
-#if defined(ANKI_VENDOR_NVIDIA)
-#	define NVIDIA_LINK_ERROR_WORKAROUND 1
-#else
-#	define NVIDIA_LINK_ERROR_WORKAROUND 0
-#endif
-
-#if defined(ANKI_VENDOR_AMD) && defined(ANKI_BACKEND_VULKAN)
-#	define AMD_VK_READ_FIRST_INVOCATION_COMPILER_CRASH 1
-#else
-#	define AMD_VK_READ_FIRST_INVOCATION_COMPILER_CRASH 0
-#endif
-
-// Default precision
-#ifndef DEFAULT_FLOAT_PRECISION
-#	define DEFAULT_FLOAT_PRECISION highp
-#endif
-
-#ifndef DEFAULT_INT_PRECISION
-#	define DEFAULT_INT_PRECISION highp
-#endif
-
 // Constants
-precision DEFAULT_FLOAT_PRECISION F32;
-precision DEFAULT_INT_PRECISION I32;
-
 const F32 EPSILON = 0.000001;
 const F32 FLT_MAX = 3.402823e+38;
 const U32 MAX_U32 = 0xFFFFFFFFu;
 
 const F32 PI = 3.14159265358979323846;
-const U32 UBO_MAX_SIZE = 16384u;
+const U32 MAX_UBO_SIZE = 16384u;
 const U32 MAX_SHARED_MEMORY = 32u * 1024u;
 
 // Macros
@@ -68,14 +43,3 @@ const U32 MAX_SHARED_MEMORY = 32u * 1024u;
 #define PASS_FS 1
 #define PASS_SM 2
 #define PASS_EZ 3
-
-// Other
-#if defined(ANKI_BACKEND_VULKAN) && ANKI_BACKEND_MAJOR >= 1 && ANKI_BACKEND_MINOR >= 1
-#	define UNIFORM(x_) subgroupBroadcastFirst(x_)
-#else
-#	define UNIFORM(x_) x_
-#endif
-
-#define CALC_BITANGENT_IN_VERT 1
-
-#define ANKI_RANDOM_BLOCK_NAME ANKI_CONCATENATE(b_rand, __LINE__)

+ 1 - 1
Tests/Gr/Gr.cpp

@@ -364,7 +364,7 @@ static ShaderPtr createShader(CString src, ShaderType type, GrManager& gr,
 {
 	HeapAllocator<U8> alloc(allocAligned, nullptr);
 	StringAuto header(alloc);
-	ShaderProgramParser::generateAnkiShaderHeader(type, gr.getDeviceCapabilities(), gr.getBindlessLimits(), header);
+	ShaderProgramParser::generateAnkiShaderHeader(type, ShaderCompilerOptions(), header);
 	header.append(src);
 	DynamicArrayAuto<U8> spirv(alloc);
 	ANKI_TEST_EXPECT_NO_ERR(compilerGlslToSpirv(header, type, alloc, spirv));

+ 5 - 8
Tests/ShaderCompiler/ShaderProgramCompiler.cpp

@@ -137,10 +137,9 @@ void main()
 	taskManager.m_alloc = alloc;
 
 	ShaderProgramBinaryWrapper binary(alloc);
-	BindlessLimits bindlessLimits;
-	GpuDeviceCapabilities gpuCapabilities;
-	ANKI_TEST_EXPECT_NO_ERR(compileShaderProgram("test.glslp", fsystem, nullptr, &taskManager, alloc, gpuCapabilities,
-												 bindlessLimits, binary));
+	ShaderCompilerOptions compilerOptions;
+	ANKI_TEST_EXPECT_NO_ERR(
+		compileShaderProgram("test.glslp", fsystem, nullptr, &taskManager, alloc, compilerOptions, binary));
 
 #if 1
 	StringAuto dis(alloc);
@@ -334,10 +333,8 @@ void main()
 	taskManager.m_alloc = alloc;
 
 	ShaderProgramBinaryWrapper binary(alloc);
-	BindlessLimits bindlessLimits;
-	GpuDeviceCapabilities gpuCapabilities;
-	ANKI_TEST_EXPECT_NO_ERR(compileShaderProgram("test.glslp", fsystem, nullptr, &taskManager, alloc, gpuCapabilities,
-												 bindlessLimits, binary));
+	ANKI_TEST_EXPECT_NO_ERR(
+		compileShaderProgram("test.glslp", fsystem, nullptr, &taskManager, alloc, ShaderCompilerOptions(), binary));
 
 #if 1
 	StringAuto dis(alloc);

+ 1 - 3
Tests/ShaderCompiler/ShaderProgramParser.cpp

@@ -45,9 +45,7 @@ ANKI_TEST(ShaderCompiler, ShaderCompilerParser)
 		}
 	} interface;
 
-	BindlessLimits bindlessLimits;
-	GpuDeviceCapabilities gpuCapabilities;
-	ShaderProgramParser parser("filename0", &interface, alloc, gpuCapabilities, bindlessLimits);
+	ShaderProgramParser parser("filename0", &interface, alloc, ShaderCompilerOptions());
 	ANKI_TEST_EXPECT_NO_ERR(parser.parse());
 
 	// Test a variant

+ 5 - 10
Tools/Shader/ShaderProgramCompilerMain.cpp

@@ -183,20 +183,15 @@ static Error work(const CmdLineArgs& info)
 		(info.m_threadCount) ? alloc.newInstance<ThreadHive>(info.m_threadCount, alloc, true) : nullptr;
 	taskManager.m_alloc = alloc;
 
-	// Some dummy caps
-	GpuDeviceCapabilities caps;
-	caps.m_gpuVendor = GpuVendor::AMD;
-	caps.m_minorApiVersion = 1;
-	caps.m_majorApiVersion = 1;
-
-	BindlessLimits limits;
-	limits.m_bindlessImageCount = 16;
-	limits.m_bindlessTextureCount = 16;
+	// Compiler options
+	ShaderCompilerOptions compilerOptions;
+	compilerOptions.m_bindlessLimits.m_bindlessImageCount = 16;
+	compilerOptions.m_bindlessLimits.m_bindlessTextureCount = 16;
 
 	// Compile
 	ShaderProgramBinaryWrapper binary(alloc);
 	ANKI_CHECK(compileShaderProgram(info.m_inputFname, fsystem, nullptr, (info.m_threadCount) ? &taskManager : nullptr,
-									alloc, caps, limits, binary));
+									alloc, compilerOptions, binary));
 
 	// Store the binary
 	ANKI_CHECK(binary.serializeToFile(info.m_outFname));