1
0
mirror of https://github.com/libretro/RetroArch synced 2024-07-03 00:38:44 +00:00

Move C code over to glslang_util.c

This commit is contained in:
twinaphex 2019-08-18 18:01:21 +02:00
parent db043a7fd7
commit aaeff6c888
10 changed files with 336 additions and 273 deletions

View File

@ -1489,6 +1489,7 @@ ifeq ($(HAVE_SLANG),1)
OBJ += gfx/drivers_shader/slang_process.o
OBJ += gfx/drivers_shader/slang_preprocess.o
OBJ += gfx/drivers_shader/glslang_util.o
OBJ += gfx/drivers_shader/glslang_util_cxx.o
OBJ += gfx/drivers_shader/slang_reflection.o
endif

View File

@ -15,9 +15,6 @@
#include <stdio.h>
#include <string.h>
#include <string>
#include <sstream>
#include <algorithm>
#include <retro_miscellaneous.h>
#include <file/file_path.h>
@ -30,9 +27,6 @@
#endif
#include "glslang_util.h"
#if defined(HAVE_GLSLANG)
#include <glslang.hpp>
#endif
#include "../../verbosity.h"
static void get_include_file(
@ -188,7 +182,7 @@ bool glslang_read_shader_file(const char *path,
if (push_line != 0)
{
snprintf(tmp, sizeof(tmp), "#line %u \"%s\"",
unsigned(i + push_line), basename);
(unsigned)(i + push_line), basename);
if (!string_list_append(output, tmp, attr))
goto error;
}
@ -249,7 +243,7 @@ const char *glslang_format_to_string(enum glslang_format fmt)
return glslang_formats[fmt];
}
static glslang_format glslang_find_format(const char *fmt)
enum glslang_format glslang_find_format(const char *fmt)
{
#undef FMT
#define FMT(x) if (string_is_equal(fmt, #x)) return SLANG_FORMAT_ ## x
@ -289,222 +283,3 @@ static glslang_format glslang_find_format(const char *fmt)
return SLANG_FORMAT_UNKNOWN;
}
static std::string build_stage_source(
const struct string_list *lines, const char *stage)
{
/* Note: since we have to return a std::string anyway,
* there is nothing to be gained from trying to replace
* this ostringstream with a C-based alternative
* (would require a rewrite of deps/glslang/glslang.cpp) */
std::ostringstream str;
bool active = true;
size_t i;
if (!lines)
return "";
if (lines->size < 1)
return "";
/* Version header. */
str << lines->elems[0].data;;
str << '\n';
for (i = 1; i < lines->size; i++)
{
const char *line = lines->elems[i].data;
/* Identify 'stage' (fragment/vertex) */
if (!strncmp("#pragma stage ", line, STRLEN_CONST("#pragma stage ")))
{
if (!string_is_empty(stage))
{
char expected[128];
expected[0] = '\0';
strlcpy(expected, "#pragma stage ", sizeof(expected));
strlcat(expected, stage, sizeof(expected));
active = strcmp(expected, line) == 0;
}
}
else if (!strncmp("#pragma name ", line, STRLEN_CONST("#pragma name ")) ||
!strncmp("#pragma format ", line, STRLEN_CONST("#pragma format ")))
{
/* Ignore */
}
else if (active)
str << line;
str << '\n';
}
return str.str();
}
bool glslang_parse_meta(const struct string_list *lines, glslang_meta *meta)
{
char id[64];
char desc[64];
size_t i;
id[0] = '\0';
desc[0] = '\0';
if (!lines)
return false;
*meta = glslang_meta{};
for (i = 0; i < lines->size; i++)
{
const char *line = lines->elems[i].data;
/* Check for shader identifier */
if (!strncmp("#pragma name ", line, STRLEN_CONST("#pragma name ")))
{
const char *str = NULL;
if (!meta->name.empty())
{
RARCH_ERR("[slang]: Trying to declare multiple names for file.\n");
return false;
}
str = line + STRLEN_CONST("#pragma name ");
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.
* (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
{
RARCH_ERR("[slang]: Invalid #pragma parameter line: \"%s\".\n", 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)
{
RARCH_ERR("[slang]: Trying to declare format multiple times for file.\n");
return false;
}
str = line + STRLEN_CONST("#pragma format ");
while (*str == ' ')
str++;
meta->rt_format = glslang_find_format(str);
if (meta->rt_format == SLANG_FORMAT_UNKNOWN)
{
RARCH_ERR("[slang]: Failed to find format \"%s\".\n", str);
return false;
}
}
}
return true;
}
bool glslang_compile_shader(const char *shader_path, glslang_output *output)
{
#if defined(HAVE_GLSLANG)
struct string_list *lines = string_list_new();
if (!lines)
return false;
RARCH_LOG("[slang]: Compiling shader \"%s\".\n", shader_path);
if (!glslang_read_shader_file(shader_path, lines, true))
goto error;
if (!glslang_parse_meta(lines, &output->meta))
goto error;
if ( !glslang::compile_spirv(build_stage_source(lines, "vertex"),
glslang::StageVertex, &output->vertex))
{
RARCH_ERR("Failed to compile vertex shader stage.\n");
goto error;
}
if ( !glslang::compile_spirv(build_stage_source(lines, "fragment"),
glslang::StageFragment, &output->fragment))
{
RARCH_ERR("Failed to compile fragment shader stage.\n");
goto error;
}
string_list_free(lines);
return true;
error:
if (lines)
string_list_free(lines);
#endif
return false;
}

View File

@ -13,8 +13,8 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GLSLANG_UTIL_HPP
#define GLSLANG_UTIL_HPP
#ifndef GLSLANG_UTIL_H
#define GLSLANG_UTIL_H
#include <stdint.h>
#include <retro_common_api.h>
@ -70,48 +70,11 @@ RETRO_BEGIN_DECLS
const char *glslang_format_to_string(glslang_format fmt);
enum glslang_format glslang_find_format(const char *fmt);
bool glslang_read_shader_file(const char *path,
struct string_list *output, bool root_file);
RETRO_END_DECLS
#ifdef __cplusplus
#include <vector>
#include <string>
struct glslang_parameter
{
std::string id;
std::string desc;
float initial;
float minimum;
float maximum;
float step;
};
struct glslang_meta
{
std::vector<glslang_parameter> parameters;
std::string name;
glslang_format rt_format;
glslang_meta()
{
rt_format = SLANG_FORMAT_UNKNOWN;
}
};
struct glslang_output
{
std::vector<uint32_t> vertex;
std::vector<uint32_t> fragment;
glslang_meta meta;
};
bool glslang_compile_shader(const char *shader_path, glslang_output *output);
/* Helpers for internal use. */
bool glslang_read_shader_file(const char *path, struct string_list *output, bool root_file);
bool glslang_parse_meta(const struct string_list *lines, glslang_meta *meta);
#endif
void *config_file_new_wrapper(const char *path);
#endif

View File

@ -0,0 +1,256 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2017 - Hans-Kristian Arntzen
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <string>
#include <sstream>
#include <algorithm>
#include <retro_miscellaneous.h>
#include <file/file_path.h>
#include <file/config_file.h>
#include <streams/file_stream.h>
#include <string/stdstring.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "glslang_util.h"
#include "glslang_util_cxx.h"
#if defined(HAVE_GLSLANG)
#include <glslang.hpp>
#endif
#include "../../verbosity.h"
static std::string build_stage_source(
const struct string_list *lines, const char *stage)
{
/* Note: since we have to return a std::string anyway,
* there is nothing to be gained from trying to replace
* this ostringstream with a C-based alternative
* (would require a rewrite of deps/glslang/glslang.cpp) */
std::ostringstream str;
bool active = true;
size_t i;
if (!lines)
return "";
if (lines->size < 1)
return "";
/* Version header. */
str << lines->elems[0].data;;
str << '\n';
for (i = 1; i < lines->size; i++)
{
const char *line = lines->elems[i].data;
/* Identify 'stage' (fragment/vertex) */
if (!strncmp("#pragma stage ", line, STRLEN_CONST("#pragma stage ")))
{
if (!string_is_empty(stage))
{
char expected[128];
expected[0] = '\0';
strlcpy(expected, "#pragma stage ", sizeof(expected));
strlcat(expected, stage, sizeof(expected));
active = strcmp(expected, line) == 0;
}
}
else if (!strncmp("#pragma name ", line, STRLEN_CONST("#pragma name ")) ||
!strncmp("#pragma format ", line, STRLEN_CONST("#pragma format ")))
{
/* Ignore */
}
else if (active)
str << line;
str << '\n';
}
return str.str();
}
bool glslang_parse_meta(const struct string_list *lines, glslang_meta *meta)
{
char id[64];
char desc[64];
size_t i;
id[0] = '\0';
desc[0] = '\0';
if (!lines)
return false;
*meta = glslang_meta{};
for (i = 0; i < lines->size; i++)
{
const char *line = lines->elems[i].data;
/* Check for shader identifier */
if (!strncmp("#pragma name ", line, STRLEN_CONST("#pragma name ")))
{
const char *str = NULL;
if (!meta->name.empty())
{
RARCH_ERR("[slang]: Trying to declare multiple names for file.\n");
return false;
}
str = line + STRLEN_CONST("#pragma name ");
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.
* (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
{
RARCH_ERR("[slang]: Invalid #pragma parameter line: \"%s\".\n", 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)
{
RARCH_ERR("[slang]: Trying to declare format multiple times for file.\n");
return false;
}
str = line + STRLEN_CONST("#pragma format ");
while (*str == ' ')
str++;
meta->rt_format = glslang_find_format(str);
if (meta->rt_format == SLANG_FORMAT_UNKNOWN)
{
RARCH_ERR("[slang]: Failed to find format \"%s\".\n", str);
return false;
}
}
}
return true;
}
bool glslang_compile_shader(const char *shader_path, glslang_output *output)
{
#if defined(HAVE_GLSLANG)
struct string_list *lines = string_list_new();
if (!lines)
return false;
RARCH_LOG("[slang]: Compiling shader \"%s\".\n", shader_path);
if (!glslang_read_shader_file(shader_path, lines, true))
goto error;
if (!glslang_parse_meta(lines, &output->meta))
goto error;
if ( !glslang::compile_spirv(build_stage_source(lines, "vertex"),
glslang::StageVertex, &output->vertex))
{
RARCH_ERR("Failed to compile vertex shader stage.\n");
goto error;
}
if ( !glslang::compile_spirv(build_stage_source(lines, "fragment"),
glslang::StageFragment, &output->fragment))
{
RARCH_ERR("Failed to compile fragment shader stage.\n");
goto error;
}
string_list_free(lines);
return true;
error:
if (lines)
string_list_free(lines);
#endif
return false;
}

View File

@ -0,0 +1,61 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2017 - Hans-Kristian Arntzen
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GLSLANG_UTIL_HPP
#define GLSLANG_UTIL_HPP
#include <stdint.h>
#include <retro_common_api.h>
#include <lists/string_list.h>
#include <vector>
#include <string>
struct glslang_parameter
{
std::string id;
std::string desc;
float initial;
float minimum;
float maximum;
float step;
};
struct glslang_meta
{
std::vector<glslang_parameter> parameters;
std::string name;
glslang_format rt_format;
glslang_meta()
{
rt_format = SLANG_FORMAT_UNKNOWN;
}
};
struct glslang_output
{
std::vector<uint32_t> vertex;
std::vector<uint32_t> fragment;
glslang_meta meta;
};
bool glslang_compile_shader(const char *shader_path, glslang_output *output);
/* Helpers for internal use. */
bool glslang_parse_meta(const struct string_list *lines, glslang_meta *meta);
#endif

View File

@ -15,6 +15,7 @@
#include "shader_gl_core.h"
#include "glslang_util.h"
#include "glslang_util_cxx.h"
#include <vector>
#include <memory>

View File

@ -16,6 +16,7 @@
#include "shader_vulkan.h"
#include "glslang_util.h"
#include "glslang_util_cxx.h"
#include <vector>
#include <memory>
#include <functional>

View File

@ -20,6 +20,7 @@
#include <retro_common_api.h>
#include "../../retroarch.h"
#include "glslang_util.h"
RETRO_BEGIN_DECLS
@ -33,7 +34,7 @@ RETRO_END_DECLS
#ifdef __cplusplus
#include "glslang_util.h"
#include "glslang_util_cxx.h"
bool slang_preprocess_parse_parameters(glslang_meta& meta,
struct video_shader *shader);

View File

@ -301,6 +301,10 @@ VIDEO SHADERS
#include "../gfx/video_shader_parse.c"
#endif
#ifdef HAVE_SLANG
#include "../gfx/drivers_shader/glslang_util.c"
#endif
#ifdef HAVE_CG
#ifdef HAVE_OPENGL
#include "../gfx/drivers_shader/shader_gl_cg.c"

View File

@ -115,7 +115,7 @@ VIDEO DRIVER
#include "../deps/SPIRV-Cross/spirv_parser.cpp"
#include "../deps/SPIRV-Cross/spirv_cross_parsed_ir.cpp"
#ifdef HAVE_SLANG
#include "../gfx/drivers_shader/glslang_util.cpp"
#include "../gfx/drivers_shader/glslang_util_cxx.cpp"
#include "../gfx/drivers_shader/slang_preprocess.cpp"
#include "../gfx/drivers_shader/slang_process.cpp"
#include "../gfx/drivers_shader/slang_reflection.cpp"