(glslang_util_cxx.cpp) Use string_starts_with

This commit is contained in:
twinaphex 2020-06-28 21:46:08 +02:00
parent c600fbc6ff
commit fe8a191012

View File

@ -43,9 +43,9 @@ static std::string build_stage_source(
* there is nothing to be gained from trying to replace * there is nothing to be gained from trying to replace
* this ostringstream with a C-based alternative * this ostringstream with a C-based alternative
* (would require a rewrite of deps/glslang/glslang.cpp) */ * (would require a rewrite of deps/glslang/glslang.cpp) */
size_t i;
std::ostringstream str; std::ostringstream str;
bool active = true; bool active = true;
size_t i;
if (!lines) if (!lines)
return ""; return "";
@ -61,26 +61,33 @@ static std::string build_stage_source(
{ {
const char *line = lines->elems[i].data; const char *line = lines->elems[i].data;
/* Identify 'stage' (fragment/vertex) */ if (string_starts_with(line, "#pragma"))
if (!strncmp("#pragma stage ", line, STRLEN_CONST("#pragma stage ")))
{ {
if (!string_is_empty(stage)) /* Identify 'stage' (fragment/vertex) */
if (!strncmp("#pragma stage ", line, STRLEN_CONST("#pragma stage ")))
{ {
char expected[128]; if (!string_is_empty(stage))
{
char expected[128];
expected[0] = '\0'; expected[0] = '\0';
strlcpy(expected, "#pragma stage ", sizeof(expected)); strlcpy(expected, "#pragma stage ", sizeof(expected));
strlcat(expected, stage, sizeof(expected)); strlcat(expected, stage, sizeof(expected));
active = string_is_equal(expected, line); active = string_is_equal(expected, line);
}
} }
} else if (
else if ( !strncmp("#pragma name ", line,
!strncmp("#pragma name ", line, STRLEN_CONST("#pragma name ")) || STRLEN_CONST("#pragma name ")) ||
!strncmp("#pragma format ", line, STRLEN_CONST("#pragma format "))) !strncmp("#pragma format ", line,
{ STRLEN_CONST("#pragma format ")))
/* Ignore */ {
/* Ignore */
}
else if (active)
str << line;
} }
else if (active) else if (active)
str << line; str << line;
@ -104,107 +111,110 @@ bool glslang_parse_meta(const struct string_list *lines, glslang_meta *meta)
{ {
const char *line = lines->elems[i].data; const char *line = lines->elems[i].data;
/* Check for shader identifier */ if (string_starts_with(line, "#pragma"))
if (!strncmp("#pragma name ", line,
STRLEN_CONST("#pragma name ")))
{ {
const char *str = NULL; /* Check for shader identifier */
if (!strncmp("#pragma name ", line,
if (!meta->name.empty()) STRLEN_CONST("#pragma name ")))
{ {
RARCH_ERR("[slang]: Trying to declare multiple names for file.\n"); const char *str = NULL;
return false;
}
str = line + STRLEN_CONST("#pragma name "); if (!meta->name.empty())
while (*str == ' ')
str++;
meta->name = str;
}
/* Check for shader parameters */
else if (!strncmp("#pragma parameter ", line,
STRLEN_CONST("#pragma parameter ")))
{
float initial, minimum, maximum, step;
int ret = sscanf(
line, "#pragma parameter %63s \"%63[^\"]\" %f %f %f %f",
id, desc, &initial, &minimum, &maximum, &step);
if (ret == 5)
{
step = 0.1f * (maximum - minimum);
ret = 6;
}
if (ret == 6)
{
bool parameter_found = false;
size_t parameter_index = 0;
size_t j;
for (j = 0; j < meta->parameters.size(); j++)
{ {
/* Note: LHS is a std:string, RHS is a C string. RARCH_ERR("[slang]: Trying to declare multiple names for file.\n");
* (the glslang_meta stuff has to be C++) */ return false;
if (meta->parameters[j].id == id)
{
parameter_found = true;
parameter_index = j;
break;
}
} }
/* Allow duplicate #pragma parameter, but only str = line + STRLEN_CONST("#pragma name ");
* if they are exactly the same. */ while (*str == ' ')
if (parameter_found) str++;
{
const glslang_parameter *parameter =
&meta->parameters[parameter_index];
if ( parameter->desc != desc || meta->name = str;
parameter->initial != initial || }
parameter->minimum != minimum || /* Check for shader parameters */
parameter->maximum != maximum || else if (!strncmp("#pragma parameter ", line,
parameter->step != step STRLEN_CONST("#pragma parameter ")))
) {
float initial, minimum, maximum, step;
int ret = sscanf(
line, "#pragma parameter %63s \"%63[^\"]\" %f %f %f %f",
id, desc, &initial, &minimum, &maximum, &step);
if (ret == 5)
{
step = 0.1f * (maximum - minimum);
ret = 6;
}
if (ret == 6)
{
bool parameter_found = false;
size_t parameter_index = 0;
size_t j;
for (j = 0; j < meta->parameters.size(); j++)
{ {
RARCH_ERR("[slang]: Duplicate parameters found for \"%s\", but arguments do not match.\n", id); /* Note: LHS is a std:string, RHS is a C string.
return false; * (the glslang_meta stuff has to be C++) */
if (meta->parameters[j].id == id)
{
parameter_found = true;
parameter_index = j;
break;
}
} }
/* Allow duplicate #pragma parameter, but only
* if they are exactly the same. */
if (parameter_found)
{
const glslang_parameter *parameter =
&meta->parameters[parameter_index];
if ( parameter->desc != desc ||
parameter->initial != initial ||
parameter->minimum != minimum ||
parameter->maximum != maximum ||
parameter->step != step
)
{
RARCH_ERR("[slang]: Duplicate parameters found for \"%s\", but arguments do not match.\n", id);
return false;
}
}
else
meta->parameters.push_back({ id, desc, initial, minimum, maximum, step });
} }
else else
meta->parameters.push_back({ id, desc, initial, minimum, maximum, step }); {
RARCH_ERR("[slang]: Invalid #pragma parameter line: \"%s\".\n",
line);
return false;
}
} }
else /* Check for framebuffer format */
else if (!strncmp("#pragma format ", line,
STRLEN_CONST("#pragma format ")))
{ {
RARCH_ERR("[slang]: Invalid #pragma parameter line: \"%s\".\n", const char *str = NULL;
line);
return false;
}
}
/* Check for framebuffer format */
else if (!strncmp("#pragma format ", line,
STRLEN_CONST("#pragma format ")))
{
const char *str = NULL;
if (meta->rt_format != SLANG_FORMAT_UNKNOWN) if (meta->rt_format != SLANG_FORMAT_UNKNOWN)
{ {
RARCH_ERR("[slang]: Trying to declare format multiple times for file.\n"); RARCH_ERR("[slang]: Trying to declare format multiple times for file.\n");
return false; return false;
} }
str = line + STRLEN_CONST("#pragma format "); str = line + STRLEN_CONST("#pragma format ");
while (*str == ' ') while (*str == ' ')
str++; str++;
meta->rt_format = glslang_find_format(str); meta->rt_format = glslang_find_format(str);
if (meta->rt_format == SLANG_FORMAT_UNKNOWN) if (meta->rt_format == SLANG_FORMAT_UNKNOWN)
{ {
RARCH_ERR("[slang]: Failed to find format \"%s\".\n", str); RARCH_ERR("[slang]: Failed to find format \"%s\".\n", str);
return false; return false;
}
} }
} }
} }