From 1eb853512e396aaec2dfd74a31bd5a631394133a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 25 Oct 2018 22:16:10 -0700 Subject: [PATCH] Cleanup. --- tools/shaderc/shaderc.cpp | 83 +++++++++++++++++++--------------- tools/shaderc/shaderc.h | 2 + tools/shaderc/shaderc_glsl.cpp | 54 +++++++++------------- 3 files changed, 71 insertions(+), 68 deletions(-) diff --git a/tools/shaderc/shaderc.cpp b/tools/shaderc/shaderc.cpp index 53623b796..c0f08b0fe 100644 --- a/tools/shaderc/shaderc.cpp +++ b/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) { uint32_t glsl = 0; @@ -1048,37 +1055,34 @@ namespace bgfx bool compiled = false; VaryingMap varyingMap; - const char* parse = _varying; + bx::StringView parse(_varying); bx::StringView term(parse); 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, ';'); 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() ) { - 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) || 0 == bx::strCmp(typen, "mediump", 7) || 0 == bx::strCmp(typen, "highp", 5) ) { precision = typen; - typen = parse = bx::strLTrimSpace(bx::strSkipWord(parse) ).getPtr(); + typen = nextWord(parse); } if (0 == bx::strCmp(typen, "flat", 4) @@ -1087,36 +1091,45 @@ namespace bgfx || 0 == bx::strCmp(typen, "centroid", 8) ) { interpolation = typen; - typen = parse = bx::strLTrimSpace(bx::strSkipWord(parse) ).getPtr(); + typen = nextWord(parse); 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(); + 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); + } - if (typen < eol.getPtr() - && name < eol.getPtr() - && column < eol.getPtr() - && ':' == *column - && semantics < eol.getPtr() ) + 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; - 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 && var.m_semantics == "BITANGENT") @@ -1124,17 +1137,15 @@ namespace bgfx 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) ); } - parse = bx::strLTrimSpace(bx::strFindNl(bx::StringView(eol.getPtr(), term.getTerm() ) ) ).getPtr(); + parse = bx::strLTrimSpace(bx::strFindNl(bx::StringView(eol.getPtr(), term.getTerm() ) ) ); } } diff --git a/tools/shaderc/shaderc.h b/tools/shaderc/shaderc.h index 1de0addb5..1a2c78f12 100644 --- a/tools/shaderc/shaderc.h +++ b/tools/shaderc/shaderc.h @@ -105,6 +105,8 @@ namespace bgfx uint32_t m_size; }; + bx::StringView nextWord(bx::StringView& _parse); + #define BGFX_UNIFORM_FRAGMENTBIT UINT8_C(0x10) #define BGFX_UNIFORM_SAMPLERBIT UINT8_C(0x20) diff --git a/tools/shaderc/shaderc_glsl.cpp b/tools/shaderc/shaderc_glsl.cpp index 5d18fbd0c..1547b3bea 100644 --- a/tools/shaderc/shaderc_glsl.cpp +++ b/tools/shaderc/shaderc_glsl.cpp @@ -104,17 +104,15 @@ namespace bgfx { namespace glsl 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() ) { - const char* qualifier = parse; - parse = bx::strLTrimSpace(bx::strSkipWord(parse) ).getPtr(); + bx::StringView qualifier = nextWord(parse); if (0 == bx::strCmp(qualifier, "attribute", 9) || 0 == bx::strCmp(qualifier, "varying", 7) @@ -123,39 +121,38 @@ namespace bgfx { namespace glsl ) { // skip attributes and varyings. - parse = eol.getPtr() + 1; + parse.set(eol.getPtr() + 1, parse.getTerm() ); continue; } if (0 == bx::strCmp(parse, "tmpvar", 6) ) { // skip temporaries - parse = eol.getPtr() + 1; + parse.set(eol.getPtr() + 1, parse.getTerm() ); continue; } if (0 != bx::strCmp(qualifier, "uniform", 7) ) { // end if there is no uniform keyword. - parse = NULL; + parse.clear(); continue; } - const char* precision = NULL; - const char* typen = parse; + bx::StringView precision; + bx::StringView typen = nextWord(parse); if (0 == bx::strCmp(typen, "lowp", 4) || 0 == bx::strCmp(typen, "mediump", 7) || 0 == bx::strCmp(typen, "highp", 5) ) { precision = typen; - typen = parse = bx::strLTrimSpace(bx::strSkipWord(parse) ).getPtr(); + typen = nextWord(parse); } BX_UNUSED(precision); char uniformType[256]; - parse = bx::strSkipWord(parse); if (0 == bx::strCmp(typen, "sampler", 7) ) { @@ -163,45 +160,38 @@ namespace bgfx { namespace glsl } 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; - 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; - bx::fromString(&tmp, arraySize); + bx::fromString(&tmp, parse); num = uint8_t(tmp); } - else - { - bx::strCopy(uniformName, int32_t(eol.getPtr() -name+1), name); - } Uniform un; un.type = nameToUniformTypeEnum(uniformType); 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.regIndex = 0; un.regCount = num; 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(); char uniformType[256]; - parse = bx::strSkipWord(parse.getPtr() ); + parse = bx::strWord(parse).getPtr(); bx::strCopy(uniformType, int32_t(parse.getPtr()-typen+1), typen); const char* name = bx::strLTrimSpace(parse).getPtr(); parse.set(name, optShader.getTerm() );