Browse Source

Preprocess GLSL because mesa can't

Panagiotis Christopoulos Charitos 7 years ago
parent
commit
d485b454d5
2 changed files with 30 additions and 8 deletions
  1. 2 6
      shaders/Pack.glsl
  2. 28 2
      src/anki/gr/ShaderCompiler.cpp

+ 2 - 6
shaders/Pack.glsl

@@ -59,10 +59,8 @@ Vec3 signedOctDecode(Vec3 n)
 	return outn;
 	return outn;
 }
 }
 
 
-#if GL_ES || __VERSION__ < 400
-
 // Vectorized version. See clean one at <= r1048
 // Vectorized version. See clean one at <= r1048
-U32 packUnorm4x8(in Vec4 v)
+U32 newPackUnorm4x8(Vec4 v)
 {
 {
 	Vec4 a = clamp(v, 0.0, 1.0) * 255.0;
 	Vec4 a = clamp(v, 0.0, 1.0) * 255.0;
 	UVec4 b = UVec4(a) << UVec4(0U, 8U, 16U, 24U);
 	UVec4 b = UVec4(a) << UVec4(0U, 8U, 16U, 24U);
@@ -71,15 +69,13 @@ U32 packUnorm4x8(in Vec4 v)
 }
 }
 
 
 // Vectorized version. See clean one at <= r1048
 // Vectorized version. See clean one at <= r1048
-Vec4 unpackUnorm4x8(in highp U32 u)
+Vec4 newUnpackUnorm4x8(highp U32 u)
 {
 {
 	UVec4 a = UVec4(u) >> UVec4(0U, 8U, 16U, 24U);
 	UVec4 a = UVec4(u) >> UVec4(0U, 8U, 16U, 24U);
 	UVec4 b = a & UVec4(0xFFU);
 	UVec4 b = a & UVec4(0xFFU);
 	Vec4 c = Vec4(b);
 	Vec4 c = Vec4(b);
-
 	return c * (1.0 / 255.0);
 	return c * (1.0 / 255.0);
 }
 }
-#endif
 
 
 // Convert from RGB to YCbCr.
 // Convert from RGB to YCbCr.
 // The RGB should be in [0, 1] and the output YCbCr will be in [0, 1] as well.
 // The RGB should be in [0, 1] and the output YCbCr will be in [0, 1] as well.

+ 28 - 2
src/anki/gr/ShaderCompiler.cpp

@@ -11,6 +11,7 @@
 #include <anki/core/Trace.h>
 #include <anki/core/Trace.h>
 #include <glslang/Public/ShaderLang.h>
 #include <glslang/Public/ShaderLang.h>
 #include <glslang/SPIRV/GlslangToSpv.h>
 #include <glslang/SPIRV/GlslangToSpv.h>
+#include <glslang/StandAlone/DirStackFileIncluder.h>
 #include <SPIRV-Cross/spirv_glsl.hpp>
 #include <SPIRV-Cross/spirv_glsl.hpp>
 
 
 namespace anki
 namespace anki
@@ -276,6 +277,26 @@ static ANKI_USE_RESULT Error genSpirv(const ShaderCompiler::BuildContext& ctx, s
 	return Error::NONE;
 	return Error::NONE;
 }
 }
 
 
+/// Just run the preprocessor.
+static ANKI_USE_RESULT Error preprocess(const ShaderCompiler::BuildContext& ctx, std::string& src)
+{
+	const EShLanguage stage = ankiToGlslangShaderType(ctx.m_options.m_shaderType);
+
+	glslang::TShader shader(stage);
+	Array<const char*, 1> csrc = {{&ctx.m_src[0]}};
+	shader.setStrings(&csrc[0], 1);
+
+	DirStackFileIncluder includer;
+	EShMessages messages = EShMsgDefault;
+	if(!shader.preprocess(&GLSLANG_LIMITS, 450, ENoProfile, false, false, messages, &src, includer))
+	{
+		ShaderCompiler::logShaderErrorCode(shader.getInfoLog(), ctx.m_src, ctx.m_alloc);
+		return Error::USER_DATA;
+	}
+
+	return Error::NONE;
+}
+
 I32 ShaderCompiler::m_refcount = {0};
 I32 ShaderCompiler::m_refcount = {0};
 Mutex ShaderCompiler::m_refcountMtx;
 Mutex ShaderCompiler::m_refcountMtx;
 
 
@@ -358,8 +379,13 @@ Error ShaderCompiler::compile(CString source, const ShaderCompilerOptions& optio
 			memcpy(&bin[0], &newSrc[0], bin.getSize());
 			memcpy(&bin[0], &newSrc[0], bin.getSize());
 		}
 		}
 #else
 #else
-		bin.resize(fullSrc.getLength() + 1);
-		memcpy(&bin[0], &fullSrc[0], bin.getSize());
+		// Preprocess the source because MESA sucks and can't do it
+		std::string preprocessedSrc;
+		ANKI_CHECK(preprocess(ctx, preprocessedSrc));
+		ANKI_ASSERT(preprocessedSrc.length() > 0);
+
+		bin.resize(preprocessedSrc.length() + 1);
+		memcpy(&bin[0], &preprocessedSrc[0], bin.getSize());
 #endif
 #endif
 	}
 	}
 	else
 	else