Branimir Karadžić 7 years ago
parent
commit
1eb853512e
3 changed files with 72 additions and 69 deletions
  1. 48 37
      tools/shaderc/shaderc.cpp
  2. 2 0
      tools/shaderc/shaderc.h
  3. 22 32
      tools/shaderc/shaderc_glsl.cpp

+ 48 - 37
tools/shaderc/shaderc.cpp

@@ -879,6 +879,13 @@ namespace bgfx
 			);
 			);
 	}
 	}
 
 
+	bx::StringView nextWord(bx::StringView& _parse)
+	{
+		bx::StringView word = bx::strWord(bx::strLTrimSpace(_parse) );
+		_parse = bx::strLTrimSpace(bx::StringView(word.getTerm(), _parse.getTerm() ) );
+		return word;
+	}
+
 	bool compileShader(const char* _varying, const char* _comment, char* _shader, uint32_t _shaderLen, Options& _options, bx::FileWriter* _writer)
 	bool compileShader(const char* _varying, const char* _comment, char* _shader, uint32_t _shaderLen, Options& _options, bx::FileWriter* _writer)
 	{
 	{
 		uint32_t glsl  = 0;
 		uint32_t glsl  = 0;
@@ -1048,37 +1055,34 @@ namespace bgfx
 		bool compiled = false;
 		bool compiled = false;
 
 
 		VaryingMap varyingMap;
 		VaryingMap varyingMap;
-		const char* parse = _varying;
+		bx::StringView parse(_varying);
 		bx::StringView term(parse);
 		bx::StringView term(parse);
 
 
 		bool usesInterpolationQualifiers = false;
 		bool usesInterpolationQualifiers = false;
 
 
-		while (NULL !=  parse
-		&&     '\0' != *parse)
+		while (!parse.isEmpty() )
 		{
 		{
-			parse = bx::strLTrimSpace(parse).getPtr();
+			parse = bx::strLTrimSpace(parse);
 			bx::StringView eol = bx::strFind(parse, ';');
 			bx::StringView eol = bx::strFind(parse, ';');
 			if (eol.isEmpty() )
 			if (eol.isEmpty() )
 			{
 			{
-				eol = bx::strFindEol(bx::StringView(parse, term.getTerm() ) );
-			}
-			else
-			{
-				eol.set(eol.getPtr(), term.getTerm() );
+				eol = bx::strFindEol(parse);
 			}
 			}
 
 
 			if (!eol.isEmpty() )
 			if (!eol.isEmpty() )
 			{
 			{
-				const char* precision = NULL;
-				const char* interpolation = NULL;
-				const char* typen = parse;
+				eol.set(eol.getPtr() + 1, parse.getTerm() );
+
+				bx::StringView precision;
+				bx::StringView interpolation;
+				bx::StringView typen = nextWord(parse);
 
 
 				if (0 == bx::strCmp(typen, "lowp", 4)
 				if (0 == bx::strCmp(typen, "lowp", 4)
 				||  0 == bx::strCmp(typen, "mediump", 7)
 				||  0 == bx::strCmp(typen, "mediump", 7)
 				||  0 == bx::strCmp(typen, "highp", 5) )
 				||  0 == bx::strCmp(typen, "highp", 5) )
 				{
 				{
 					precision = typen;
 					precision = typen;
-					typen = parse = bx::strLTrimSpace(bx::strSkipWord(parse) ).getPtr();
+					typen = nextWord(parse);
 				}
 				}
 
 
 				if (0 == bx::strCmp(typen, "flat", 4)
 				if (0 == bx::strCmp(typen, "flat", 4)
@@ -1087,36 +1091,45 @@ namespace bgfx
 				||  0 == bx::strCmp(typen, "centroid", 8) )
 				||  0 == bx::strCmp(typen, "centroid", 8) )
 				{
 				{
 					interpolation = typen;
 					interpolation = typen;
-					typen = parse = bx::strLTrimSpace(bx::strSkipWord(parse) ).getPtr();
+					typen = nextWord(parse);
 					usesInterpolationQualifiers = true;
 					usesInterpolationQualifiers = true;
 				}
 				}
 
 
-				const char* name      = parse = bx::strLTrimSpace(bx::strSkipWord(parse) ).getPtr();
-				const char* column    = parse = bx::strLTrimSpace(bx::strSkipWord(parse) ).getPtr();
-				const char* semantics = parse = bx::strLTrimSpace( (*parse == ':' ? ++parse : parse) ).getPtr();
-				const char* assign    = parse = bx::strLTrimSpace(bx::strSkipWord(parse) ).getPtr();
-				const char* init      = parse = bx::strLTrimSpace( (*parse == '=' ? ++parse : parse) ).getPtr();
-
-				if (typen < eol.getPtr()
-				&&  name < eol.getPtr()
-				&&  column < eol.getPtr()
-				&&  ':' == *column
-				&&  semantics < eol.getPtr() )
+				bx::StringView name   = nextWord(parse);
+				bx::StringView column = bx::strSubstr(parse, 0, 1);
+				bx::StringView semantics;
+				if (0 == bx::strCmp(column, ":", 1) )
+				{
+					parse = bx::strLTrimSpace(bx::StringView(parse.getPtr() + 1, parse.getTerm() ) );
+					semantics = nextWord(parse);
+				}
+
+				bx::StringView assign = bx::strSubstr(parse, 0, 1);
+				bx::StringView init;
+				if (0 == bx::strCmp(assign, "=", 1))
+				{
+					parse = bx::strLTrimSpace(bx::StringView(parse.getPtr() + 1, parse.getTerm() ) );
+					init.set(parse.getPtr(), eol.getPtr() );
+				}
+
+				if (!typen.isEmpty()
+				&&  !name.isEmpty()
+				&&  !semantics.isEmpty() )
 				{
 				{
 					Varying var;
 					Varying var;
-					if (NULL != precision)
+					if (!precision.isEmpty() )
 					{
 					{
-						var.m_precision.assign(precision, bx::strSkipWord(precision)-precision);
+						var.m_precision.assign(precision.getPtr(), precision.getTerm() );
 					}
 					}
 
 
-					if (NULL != interpolation)
+					if (!interpolation.isEmpty() )
 					{
 					{
-						var.m_interpolation.assign(interpolation, bx::strSkipWord(interpolation)-interpolation);
+						var.m_interpolation.assign(interpolation.getPtr(), interpolation.getTerm() );
 					}
 					}
 
 
-					var.m_type.assign(typen, bx::strSkipWord(typen)-typen);
-					var.m_name.assign(name, bx::strSkipWord(name)-name);
-					var.m_semantics.assign(semantics, bx::strSkipWord(semantics)-semantics);
+					var.m_type.assign(typen.getPtr(), typen.getTerm() );
+					var.m_name.assign(name.getPtr(), name.getTerm() );
+					var.m_semantics.assign(semantics.getPtr(), semantics.getTerm() );
 
 
 					if (d3d == 9
 					if (d3d == 9
 					&&  var.m_semantics == "BITANGENT")
 					&&  var.m_semantics == "BITANGENT")
@@ -1124,17 +1137,15 @@ namespace bgfx
 						var.m_semantics = "BINORMAL";
 						var.m_semantics = "BINORMAL";
 					}
 					}
 
 
-					if (assign < eol.getPtr()
-					&&  '=' == *assign
-					&&  init < eol.getPtr() )
+					if (!init.isEmpty() )
 					{
 					{
-						var.m_init.assign(init, eol.getPtr()-init);
+						var.m_init.assign(init.getPtr(), init.getTerm() );
 					}
 					}
 
 
 					varyingMap.insert(std::make_pair(var.m_name, var) );
 					varyingMap.insert(std::make_pair(var.m_name, var) );
 				}
 				}
 
 
-				parse = bx::strLTrimSpace(bx::strFindNl(bx::StringView(eol.getPtr(), term.getTerm() ) ) ).getPtr();
+				parse = bx::strLTrimSpace(bx::strFindNl(bx::StringView(eol.getPtr(), term.getTerm() ) ) );
 			}
 			}
 		}
 		}
 
 

+ 2 - 0
tools/shaderc/shaderc.h

@@ -105,6 +105,8 @@ namespace bgfx
 		uint32_t m_size;
 		uint32_t m_size;
 	};
 	};
 
 
+	bx::StringView nextWord(bx::StringView& _parse);
+
 #define BGFX_UNIFORM_FRAGMENTBIT UINT8_C(0x10)
 #define BGFX_UNIFORM_FRAGMENTBIT UINT8_C(0x10)
 #define BGFX_UNIFORM_SAMPLERBIT  UINT8_C(0x20)
 #define BGFX_UNIFORM_SAMPLERBIT  UINT8_C(0x20)
 
 

+ 22 - 32
tools/shaderc/shaderc_glsl.cpp

@@ -104,17 +104,15 @@ namespace bgfx { namespace glsl
 
 
 		if (target != kGlslTargetMetal)
 		if (target != kGlslTargetMetal)
 		{
 		{
-			const char* parse = optimizedShader;
+			bx::StringView parse(optimizedShader);
 
 
-			while (NULL != parse
-				&& *parse != '\0')
+			while (!parse.isEmpty() )
 			{
 			{
-				parse = bx::strLTrimSpace(parse).getPtr();
-				const bx::StringView eol = bx::strFind(parse, ';');
+				parse = bx::strLTrimSpace(parse);
+				bx::StringView eol = bx::strFind(parse, ';');
 				if (!eol.isEmpty() )
 				if (!eol.isEmpty() )
 				{
 				{
-					const char* qualifier = parse;
-					parse = bx::strLTrimSpace(bx::strSkipWord(parse) ).getPtr();
+					bx::StringView qualifier = nextWord(parse);
 
 
 					if (0 == bx::strCmp(qualifier, "attribute", 9)
 					if (0 == bx::strCmp(qualifier, "attribute", 9)
 					||  0 == bx::strCmp(qualifier, "varying",   7)
 					||  0 == bx::strCmp(qualifier, "varying",   7)
@@ -123,39 +121,38 @@ namespace bgfx { namespace glsl
 					   )
 					   )
 					{
 					{
 						// skip attributes and varyings.
 						// skip attributes and varyings.
-						parse = eol.getPtr() + 1;
+						parse.set(eol.getPtr() + 1, parse.getTerm() );
 						continue;
 						continue;
 					}
 					}
 
 
 					if (0 == bx::strCmp(parse, "tmpvar", 6) )
 					if (0 == bx::strCmp(parse, "tmpvar", 6) )
 					{
 					{
 						// skip temporaries
 						// skip temporaries
-						parse = eol.getPtr() + 1;
+						parse.set(eol.getPtr() + 1, parse.getTerm() );
 						continue;
 						continue;
 					}
 					}
 
 
 					if (0 != bx::strCmp(qualifier, "uniform", 7) )
 					if (0 != bx::strCmp(qualifier, "uniform", 7) )
 					{
 					{
 						// end if there is no uniform keyword.
 						// end if there is no uniform keyword.
-						parse = NULL;
+						parse.clear();
 						continue;
 						continue;
 					}
 					}
 
 
-					const char* precision = NULL;
-					const char* typen = parse;
+					bx::StringView precision;
+					bx::StringView typen = nextWord(parse);
 
 
 					if (0 == bx::strCmp(typen, "lowp", 4)
 					if (0 == bx::strCmp(typen, "lowp", 4)
 					||  0 == bx::strCmp(typen, "mediump", 7)
 					||  0 == bx::strCmp(typen, "mediump", 7)
 					||  0 == bx::strCmp(typen, "highp", 5) )
 					||  0 == bx::strCmp(typen, "highp", 5) )
 					{
 					{
 						precision = typen;
 						precision = typen;
-						typen = parse = bx::strLTrimSpace(bx::strSkipWord(parse) ).getPtr();
+						typen = nextWord(parse);
 					}
 					}
 
 
 					BX_UNUSED(precision);
 					BX_UNUSED(precision);
 
 
 					char uniformType[256];
 					char uniformType[256];
-					parse = bx::strSkipWord(parse);
 
 
 					if (0 == bx::strCmp(typen, "sampler", 7) )
 					if (0 == bx::strCmp(typen, "sampler", 7) )
 					{
 					{
@@ -163,45 +160,38 @@ namespace bgfx { namespace glsl
 					}
 					}
 					else
 					else
 					{
 					{
-						bx::strCopy(uniformType, int32_t(parse-typen+1), typen);
+						bx::strCopy(uniformType, BX_COUNTOF(uniformType), typen);
 					}
 					}
 
 
-					const char* name = parse = bx::strLTrimSpace(parse).getPtr();
+					bx::StringView name = nextWord(parse);
 
 
-					char uniformName[256];
 					uint8_t num = 1;
 					uint8_t num = 1;
-					bx::StringView array = bx::strFind(bx::StringView(name, int32_t(eol.getPtr()-parse) ), "[");
-					if (!array.isEmpty() )
+					bx::StringView array = bx::strSubstr(parse, 0, 1);
+					if (0 == bx::strCmp(array, "[", 1) )
 					{
 					{
-						bx::strCopy(uniformName, int32_t(array.getPtr()-name+1), name);
+						parse = bx::strLTrimSpace(bx::StringView(parse.getPtr() + 1, parse.getTerm() ) );
 
 
-						char arraySize[32];
-						bx::StringView end = bx::strFind(bx::StringView(array.getPtr(), int32_t(eol.getPtr()-array.getPtr() ) ), "]");
-						bx::strCopy(arraySize, int32_t(end.getPtr()-array.getPtr() ), array.getPtr()+1);
 						uint32_t tmp;
 						uint32_t tmp;
-						bx::fromString(&tmp, arraySize);
+						bx::fromString(&tmp, parse);
 						num = uint8_t(tmp);
 						num = uint8_t(tmp);
 					}
 					}
-					else
-					{
-						bx::strCopy(uniformName, int32_t(eol.getPtr() -name+1), name);
-					}
 
 
 					Uniform un;
 					Uniform un;
 					un.type = nameToUniformTypeEnum(uniformType);
 					un.type = nameToUniformTypeEnum(uniformType);
 
 
 					if (UniformType::Count != un.type)
 					if (UniformType::Count != un.type)
 					{
 					{
-						BX_TRACE("name: %s (type %d, num %d)", uniformName, un.type, num);
+						un.name.assign(name.getPtr(), name.getTerm());
+
+						BX_TRACE("name: %s (type %d, num %d)", un.name.c_str(), un.type, num);
 
 
-						un.name = uniformName;
 						un.num = num;
 						un.num = num;
 						un.regIndex = 0;
 						un.regIndex = 0;
 						un.regCount = num;
 						un.regCount = num;
 						uniforms.push_back(un);
 						uniforms.push_back(un);
 					}
 					}
 
 
-					parse = eol.getPtr() + 1;
+					parse = bx::strLTrimSpace(bx::strFindNl(bx::StringView(eol.getPtr(), parse.getTerm() ) ) );
 				}
 				}
 			}
 			}
 		}
 		}
@@ -226,7 +216,7 @@ namespace bgfx { namespace glsl
 					const char* typen = parse.getPtr();
 					const char* typen = parse.getPtr();
 
 
 					char uniformType[256];
 					char uniformType[256];
-					parse = bx::strSkipWord(parse.getPtr() );
+					parse = bx::strWord(parse).getPtr();
 					bx::strCopy(uniformType, int32_t(parse.getPtr()-typen+1), typen);
 					bx::strCopy(uniformType, int32_t(parse.getPtr()-typen+1), typen);
 					const char* name = bx::strLTrimSpace(parse).getPtr();
 					const char* name = bx::strLTrimSpace(parse).getPtr();
 					parse.set(name, optShader.getTerm() );
 					parse.set(name, optShader.getTerm() );