2016-02-16 20:24:00 +01:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2017-01-22 13:40:32 +01:00
|
|
|
* Copyright (C) 2010-2017 - Hans-Kristian Arntzen
|
2019-02-03 15:49:35 -08:00
|
|
|
*
|
2016-02-16 20:24:00 +01:00
|
|
|
* 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>
|
2016-09-11 15:07:07 +02:00
|
|
|
#include <string.h>
|
2016-02-16 20:24:00 +01:00
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
2016-08-01 18:57:00 +02:00
|
|
|
#include <algorithm>
|
2016-02-16 20:24:00 +01:00
|
|
|
|
2016-09-16 17:33:18 +02:00
|
|
|
#include <retro_miscellaneous.h>
|
2016-09-11 15:07:07 +02:00
|
|
|
#include <file/file_path.h>
|
2019-07-18 12:03:50 +02:00
|
|
|
#include <file/config_file.h>
|
2016-03-20 16:29:14 +01:00
|
|
|
#include <streams/file_stream.h>
|
2016-09-16 17:33:18 +02:00
|
|
|
#include <string/stdstring.h>
|
2016-03-20 14:53:54 +01:00
|
|
|
|
2018-01-06 03:39:53 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2018-02-01 15:07:12 +01:00
|
|
|
#include "glslang_util.h"
|
2019-08-18 18:01:21 +02:00
|
|
|
#include "glslang_util_cxx.h"
|
2018-03-02 02:14:29 +01:00
|
|
|
#if defined(HAVE_GLSLANG)
|
2020-03-02 13:40:22 -08:00
|
|
|
#include "glslang.hpp"
|
2018-01-06 03:39:53 +01:00
|
|
|
#endif
|
2016-09-11 15:07:07 +02:00
|
|
|
#include "../../verbosity.h"
|
2016-02-16 20:24:00 +01:00
|
|
|
|
2019-08-18 14:40:26 +02:00
|
|
|
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];
|
|
|
|
|
2019-09-20 02:03:26 +02:00
|
|
|
expected[0] = '\0';
|
2019-08-18 14:40:26 +02:00
|
|
|
|
2019-09-23 00:18:29 +02:00
|
|
|
strlcpy(expected, "#pragma stage ", sizeof(expected));
|
2019-08-18 14:40:26 +02:00
|
|
|
strlcat(expected, stage, sizeof(expected));
|
|
|
|
|
2020-03-03 10:58:04 +01:00
|
|
|
active = string_is_equal(expected, line);
|
2019-08-18 14:40:26 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 10:58:04 +01:00
|
|
|
else if (
|
|
|
|
!strncmp("#pragma name ", line, STRLEN_CONST("#pragma name ")) ||
|
|
|
|
!strncmp("#pragma format ", line, STRLEN_CONST("#pragma format ")))
|
2019-08-18 14:40:26 +02:00
|
|
|
{
|
|
|
|
/* Ignore */
|
|
|
|
}
|
|
|
|
else if (active)
|
|
|
|
str << line;
|
|
|
|
|
|
|
|
str << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
return str.str();
|
|
|
|
}
|
|
|
|
|
2019-08-12 12:51:11 +01:00
|
|
|
bool glslang_parse_meta(const struct string_list *lines, glslang_meta *meta)
|
2016-03-26 17:59:50 +01:00
|
|
|
{
|
2017-01-12 10:32:57 +01:00
|
|
|
char id[64];
|
|
|
|
char desc[64];
|
2019-08-12 12:51:11 +01:00
|
|
|
size_t i;
|
2017-01-12 10:32:57 +01:00
|
|
|
|
2019-08-12 12:51:11 +01:00
|
|
|
id[0] = '\0';
|
|
|
|
desc[0] = '\0';
|
|
|
|
|
|
|
|
for (i = 0; i < lines->size; i++)
|
2016-03-26 17:59:50 +01:00
|
|
|
{
|
2019-08-12 12:51:11 +01:00
|
|
|
const char *line = lines->elems[i].data;
|
2019-01-19 23:15:48 +01:00
|
|
|
|
2019-08-12 12:51:11 +01:00
|
|
|
/* Check for shader identifier */
|
2020-03-03 10:58:04 +01:00
|
|
|
if (!strncmp("#pragma name ", line,
|
|
|
|
STRLEN_CONST("#pragma name ")))
|
2016-03-26 17:59:50 +01:00
|
|
|
{
|
2017-01-12 10:32:57 +01:00
|
|
|
const char *str = NULL;
|
|
|
|
|
2016-03-26 17:59:50 +01:00
|
|
|
if (!meta->name.empty())
|
|
|
|
{
|
|
|
|
RARCH_ERR("[slang]: Trying to declare multiple names for file.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-12 12:51:11 +01:00
|
|
|
str = line + STRLEN_CONST("#pragma name ");
|
2016-03-26 17:59:50 +01:00
|
|
|
while (*str == ' ')
|
|
|
|
str++;
|
2019-08-12 12:51:11 +01:00
|
|
|
|
2016-03-26 17:59:50 +01:00
|
|
|
meta->name = str;
|
|
|
|
}
|
2019-08-12 12:51:11 +01:00
|
|
|
/* Check for shader parameters */
|
2020-03-03 10:58:04 +01:00
|
|
|
else if (!strncmp("#pragma parameter ", line,
|
|
|
|
STRLEN_CONST("#pragma parameter ")))
|
2016-08-01 15:48:09 +02:00
|
|
|
{
|
|
|
|
float initial, minimum, maximum, step;
|
2019-08-12 12:51:11 +01:00
|
|
|
int ret = sscanf(
|
|
|
|
line, "#pragma parameter %63s \"%63[^\"]\" %f %f %f %f",
|
2016-08-01 15:48:09 +02:00
|
|
|
id, desc, &initial, &minimum, &maximum, &step);
|
|
|
|
|
2016-08-01 18:57:00 +02:00
|
|
|
if (ret == 5)
|
2016-08-01 15:48:09 +02:00
|
|
|
{
|
|
|
|
step = 0.1f * (maximum - minimum);
|
2017-01-12 10:32:57 +01:00
|
|
|
ret = 6;
|
2016-08-01 15:48:09 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 18:57:00 +02:00
|
|
|
if (ret == 6)
|
|
|
|
{
|
2019-08-12 12:51:11 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2016-08-01 18:57:00 +02:00
|
|
|
|
2019-02-03 15:49:35 -08:00
|
|
|
/* Allow duplicate #pragma parameter, but only
|
2016-09-11 15:07:07 +02:00
|
|
|
* if they are exactly the same. */
|
2019-08-12 12:51:11 +01:00
|
|
|
if (parameter_found)
|
2016-08-01 18:57:00 +02:00
|
|
|
{
|
2019-08-18 14:40:26 +02:00
|
|
|
const glslang_parameter *parameter =
|
|
|
|
&meta->parameters[parameter_index];
|
2019-08-12 12:51:11 +01:00
|
|
|
|
|
|
|
if ( parameter->desc != desc ||
|
|
|
|
parameter->initial != initial ||
|
|
|
|
parameter->minimum != minimum ||
|
|
|
|
parameter->maximum != maximum ||
|
|
|
|
parameter->step != step
|
2017-01-12 10:32:57 +01:00
|
|
|
)
|
2016-08-01 18:57:00 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2020-03-03 10:58:04 +01:00
|
|
|
RARCH_ERR("[slang]: Invalid #pragma parameter line: \"%s\".\n",
|
|
|
|
line);
|
2016-08-01 18:57:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-01 15:48:09 +02:00
|
|
|
}
|
2019-08-12 12:51:11 +01:00
|
|
|
/* Check for framebuffer format */
|
2020-03-03 10:58:04 +01:00
|
|
|
else if (!strncmp("#pragma format ", line,
|
|
|
|
STRLEN_CONST("#pragma format ")))
|
2016-03-26 23:49:57 +01:00
|
|
|
{
|
2016-09-11 15:07:07 +02:00
|
|
|
const char *str = NULL;
|
|
|
|
|
2016-03-26 23:49:57 +01:00
|
|
|
if (meta->rt_format != SLANG_FORMAT_UNKNOWN)
|
|
|
|
{
|
|
|
|
RARCH_ERR("[slang]: Trying to declare format multiple times for file.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-12 12:51:11 +01:00
|
|
|
str = line + STRLEN_CONST("#pragma format ");
|
2016-03-26 23:49:57 +01:00
|
|
|
while (*str == ' ')
|
|
|
|
str++;
|
|
|
|
|
|
|
|
meta->rt_format = glslang_find_format(str);
|
2019-08-12 12:51:11 +01:00
|
|
|
|
2016-03-26 23:49:57 +01:00
|
|
|
if (meta->rt_format == SLANG_FORMAT_UNKNOWN)
|
|
|
|
{
|
|
|
|
RARCH_ERR("[slang]: Failed to find format \"%s\".\n", str);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-03-26 17:59:50 +01:00
|
|
|
}
|
2019-08-12 12:51:11 +01:00
|
|
|
|
2016-03-26 17:59:50 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-16 20:24:00 +01:00
|
|
|
bool glslang_compile_shader(const char *shader_path, glslang_output *output)
|
|
|
|
{
|
2019-08-18 14:40:26 +02:00
|
|
|
#if defined(HAVE_GLSLANG)
|
2020-03-03 10:49:47 +01:00
|
|
|
const char *v_src, *f_src;
|
2019-08-12 12:51:11 +01:00
|
|
|
struct string_list *lines = string_list_new();
|
|
|
|
|
|
|
|
if (!lines)
|
|
|
|
return false;
|
2016-03-02 00:07:31 +01:00
|
|
|
|
2016-03-26 17:59:50 +01:00
|
|
|
RARCH_LOG("[slang]: Compiling shader \"%s\".\n", shader_path);
|
2017-01-12 10:32:57 +01:00
|
|
|
|
2019-08-12 12:51:11 +01:00
|
|
|
if (!glslang_read_shader_file(shader_path, lines, true))
|
|
|
|
goto error;
|
2019-08-18 18:05:26 +02:00
|
|
|
output->meta = glslang_meta{};
|
2016-03-26 17:59:50 +01:00
|
|
|
if (!glslang_parse_meta(lines, &output->meta))
|
2019-08-12 12:51:11 +01:00
|
|
|
goto error;
|
2016-03-26 17:59:50 +01:00
|
|
|
|
2020-03-03 10:49:47 +01:00
|
|
|
v_src = build_stage_source(lines, "vertex").c_str();
|
|
|
|
f_src = build_stage_source(lines, "fragment").c_str();
|
|
|
|
|
|
|
|
if (!glslang::compile_spirv(v_src,
|
2016-02-16 20:24:00 +01:00
|
|
|
glslang::StageVertex, &output->vertex))
|
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to compile vertex shader stage.\n");
|
2019-08-12 12:51:11 +01:00
|
|
|
goto error;
|
2016-02-16 20:24:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-03 10:49:47 +01:00
|
|
|
if (!glslang::compile_spirv(f_src,
|
2016-02-16 20:24:00 +01:00
|
|
|
glslang::StageFragment, &output->fragment))
|
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to compile fragment shader stage.\n");
|
2019-08-12 12:51:11 +01:00
|
|
|
goto error;
|
2016-02-16 20:24:00 +01:00
|
|
|
}
|
|
|
|
|
2019-08-12 12:51:11 +01:00
|
|
|
string_list_free(lines);
|
|
|
|
|
2016-02-16 20:24:00 +01:00
|
|
|
return true;
|
2019-08-12 12:51:11 +01:00
|
|
|
|
|
|
|
error:
|
2020-03-03 10:58:04 +01:00
|
|
|
string_list_free(lines);
|
2019-08-18 14:40:26 +02:00
|
|
|
#endif
|
2019-08-12 12:51:11 +01:00
|
|
|
|
|
|
|
return false;
|
2016-02-16 20:24:00 +01:00
|
|
|
}
|