RetroArch/gfx/drivers_shader/glslang_util.cpp

511 lines
13 KiB
C++
Raw Normal View History

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>
#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>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "glslang_util.h"
#if defined(HAVE_GLSLANG)
2018-03-02 18:34:29 +01:00
#include <glslang.hpp>
#endif
2016-09-11 15:07:07 +02:00
#include "../../verbosity.h"
2016-02-16 20:24:00 +01:00
static void get_include_file(
const char *line, char *include_file, size_t len)
{
char *end = NULL;
2019-08-12 19:33:50 +02:00
char *start = (char*)strchr(line, '\"');
if (!start)
return;
start++;
end = (char*)strchr(start, '\"');
if (!end)
return;
*end = '\0';
strlcpy(include_file, start, len);
}
2019-08-18 14:40:26 +02:00
bool glslang_read_shader_file(const char *path,
struct string_list *output, bool root_file)
2016-02-16 20:24:00 +01:00
{
2016-10-21 05:57:40 +02:00
char tmp[PATH_MAX_LENGTH];
union string_list_elem_attr attr;
size_t i;
const char *basename = NULL;
uint8_t *buf = NULL;
int64_t buf_len = 0;
struct string_list *lines = NULL;
tmp[0] = '\0';
attr.i = 0;
/* Sanity check */
if (string_is_empty(path) || !output)
return false;
2016-03-02 00:07:31 +01:00
basename = path_basename(path);
2016-10-21 05:57:40 +02:00
if (string_is_empty(basename))
return false;
/* Read file contents */
if (!filestream_read_file(path, (void**)&buf, &buf_len))
2016-02-16 20:24:00 +01:00
{
RARCH_ERR("Failed to open shader file: \"%s\".\n", path);
2016-02-16 20:24:00 +01:00
return false;
}
if (buf_len > 0)
{
/* Remove Windows '\r' chars if we encounter them */
string_remove_all_chars((char*)buf, '\r');
2017-01-12 10:32:57 +01:00
/* Split into lines
* (Blank lines must be included) */
lines = string_separate((char*)buf, "\n");
}
2016-03-02 00:07:31 +01:00
/* Buffer is no longer required - clean up */
if (buf)
free(buf);
2017-01-12 10:32:57 +01:00
/* Sanity check */
if (!lines)
return false;
if (lines->size < 1)
2017-01-12 10:32:57 +01:00
goto error;
2016-02-16 20:24:00 +01:00
/* If this is the 'parent' shader file, ensure that first
* line is a 'VERSION' string */
if (root_file)
2016-02-16 20:24:00 +01:00
{
const char *line = lines->elems[0].data;
if (strncmp("#version ", line, STRLEN_CONST("#version ")))
{
RARCH_ERR("First line of the shader must contain a valid #version string.\n");
goto error;
}
if (!string_list_append(output, line, attr))
goto error;
2016-09-11 15:07:07 +02:00
/* Allows us to use #line to make dealing with shader errors easier.
* This is supported by glslang, but since we always use glslang statically,
* this is fine. */
if (!string_list_append(output, "#extension GL_GOOGLE_cpp_style_line_directive : require", attr))
goto error;
2016-02-16 20:24:00 +01:00
}
2019-02-03 15:49:35 -08:00
/* At least VIM treats the first line as line #1,
2016-09-11 15:07:07 +02:00
* so offset everything by one. */
snprintf(tmp, sizeof(tmp), "#line %u \"%s\"", root_file ? 2 : 1, basename);
if (!string_list_append(output, tmp, attr))
goto error;
/* Loop through lines of file */
for (i = root_file ? 1 : 0; i < lines->size; i++)
{
2019-08-08 17:04:45 +02:00
unsigned push_line = 0;
const char *line = lines->elems[i].data;
2016-09-11 15:07:07 +02:00
/* Check for 'include' statements */
if (!strncmp("#include ", line, STRLEN_CONST("#include ")))
{
char include_file[PATH_MAX_LENGTH];
char include_path[PATH_MAX_LENGTH];
2017-01-12 10:32:57 +01:00
include_file[0] = '\0';
include_path[0] = '\0';
2016-09-11 15:07:07 +02:00
/* Build include file path */
get_include_file(line, include_file, sizeof(include_file));
2016-09-11 15:07:07 +02:00
if (string_is_empty(include_file))
{
RARCH_ERR("Invalid include statement \"%s\".\n", line);
2017-01-12 10:32:57 +01:00
goto error;
}
2016-09-11 15:07:07 +02:00
fill_pathname_resolve_relative(
include_path, path, include_file, sizeof(include_path));
/* Parse include file */
if (!glslang_read_shader_file(include_path, output, false))
2017-01-12 10:32:57 +01:00
goto error;
2019-02-03 15:49:35 -08:00
/* After including a file, use line directive
2016-09-11 15:07:07 +02:00
* to pull it back to current file. */
2019-08-08 17:04:45 +02:00
push_line = 1;
}
else if (!strncmp("#endif", line, STRLEN_CONST("#endif")) ||
!strncmp("#pragma", line, STRLEN_CONST("#pragma")))
{
2016-09-11 15:07:07 +02:00
/* #line seems to be ignored if preprocessor tests fail,
* so we should reapply #line after each #endif.
2019-02-03 15:49:35 -08:00
* Add extra offset here since we're setting #line
2016-09-11 15:07:07 +02:00
* for the line after this one.
*/
2019-08-08 17:04:45 +02:00
push_line = 2;
if (!string_list_append(output, line, attr))
goto error;
}
else
if (!string_list_append(output, line, attr))
goto error;
2019-08-08 17:04:45 +02:00
if (push_line != 0)
{
2019-08-18 14:40:26 +02:00
snprintf(tmp, sizeof(tmp), "#line %u \"%s\"",
unsigned(i + push_line), basename);
if (!string_list_append(output, tmp, attr))
goto error;
2019-08-08 17:04:45 +02:00
}
}
2016-02-16 20:24:00 +01:00
string_list_free(lines);
2016-02-16 20:24:00 +01:00
return true;
2017-01-12 10:32:57 +01:00
error:
if (lines)
string_list_free(lines);
2017-01-12 10:32:57 +01:00
return false;
2016-02-16 20:24:00 +01:00
}
static const char *glslang_formats[] = {
"UNKNOWN",
"R8_UNORM",
"R8_UINT",
"R8_SINT",
"R8G8_UNORM",
"R8G8_UINT",
"R8G8_SINT",
"R8G8B8A8_UNORM",
"R8G8B8A8_UINT",
"R8G8B8A8_SINT",
"R8G8B8A8_SRGB",
"A2B10G10R10_UNORM_PACK32",
"A2B10G10R10_UINT_PACK32",
"R16_UINT",
"R16_SINT",
"R16_SFLOAT",
"R16G16_UINT",
"R16G16_SINT",
"R16G16_SFLOAT",
"R16G16B16A16_UINT",
"R16G16B16A16_SINT",
"R16G16B16A16_SFLOAT",
"R32_UINT",
"R32_SINT",
"R32_SFLOAT",
"R32G32_UINT",
"R32G32_SINT",
"R32G32_SFLOAT",
"R32G32B32A32_UINT",
"R32G32B32A32_SINT",
"R32G32B32A32_SFLOAT",
};
const char *glslang_format_to_string(enum glslang_format fmt)
{
return glslang_formats[fmt];
}
static glslang_format glslang_find_format(const char *fmt)
{
#undef FMT
#define FMT(x) if (string_is_equal(fmt, #x)) return SLANG_FORMAT_ ## x
FMT(R8_UNORM);
FMT(R8_UINT);
FMT(R8_SINT);
FMT(R8G8_UNORM);
FMT(R8G8_UINT);
FMT(R8G8_SINT);
FMT(R8G8B8A8_UNORM);
FMT(R8G8B8A8_UINT);
FMT(R8G8B8A8_SINT);
FMT(R8G8B8A8_SRGB);
FMT(A2B10G10R10_UNORM_PACK32);
FMT(A2B10G10R10_UINT_PACK32);
FMT(R16_UINT);
FMT(R16_SINT);
FMT(R16_SFLOAT);
FMT(R16G16_UINT);
FMT(R16G16_SINT);
FMT(R16G16_SFLOAT);
FMT(R16G16B16A16_UINT);
FMT(R16G16B16A16_SINT);
FMT(R16G16B16A16_SFLOAT);
FMT(R32_UINT);
FMT(R32_SINT);
FMT(R32_SFLOAT);
FMT(R32G32_UINT);
FMT(R32G32_SINT);
FMT(R32G32_SFLOAT);
FMT(R32G32B32A32_UINT);
FMT(R32G32B32A32_SINT);
FMT(R32G32B32A32_SFLOAT);
return SLANG_FORMAT_UNKNOWN;
}
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];
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)
{
2017-01-12 10:32:57 +01:00
char id[64];
char desc[64];
size_t i;
2017-01-12 10:32:57 +01:00
id[0] = '\0';
desc[0] = '\0';
if (!lines)
return false;
2016-08-01 15:48:09 +02:00
*meta = glslang_meta{};
2016-09-11 15:07:07 +02:00
for (i = 0; i < lines->size; i++)
{
const char *line = lines->elems[i].data;
2019-01-19 23:15:48 +01:00
/* Check for shader identifier */
if (!strncmp("#pragma name ", line, STRLEN_CONST("#pragma name ")))
{
2017-01-12 10:32:57 +01:00
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 ")))
2016-08-01 15:48:09 +02:00
{
float initial, minimum, maximum, step;
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)
{
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. */
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];
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
{
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
}
/* Check for framebuffer format */
else if (!strncmp("#pragma format ", line, STRLEN_CONST("#pragma format ")))
{
2016-09-11 15:07:07 +02:00
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;
}
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)
struct string_list *lines = string_list_new();
if (!lines)
return false;
2016-03-02 00:07:31 +01:00
RARCH_LOG("[slang]: Compiling shader \"%s\".\n", shader_path);
2017-01-12 10:32:57 +01:00
if (!glslang_read_shader_file(shader_path, lines, true))
goto error;
2016-02-16 20:24:00 +01:00
if (!glslang_parse_meta(lines, &output->meta))
goto error;
2017-01-12 10:32:57 +01:00
if ( !glslang::compile_spirv(build_stage_source(lines, "vertex"),
2016-02-16 20:24:00 +01:00
glslang::StageVertex, &output->vertex))
{
RARCH_ERR("Failed to compile vertex shader stage.\n");
goto error;
2016-02-16 20:24:00 +01:00
}
2017-01-12 10:32:57 +01:00
if ( !glslang::compile_spirv(build_stage_source(lines, "fragment"),
2016-02-16 20:24:00 +01:00
glslang::StageFragment, &output->fragment))
{
RARCH_ERR("Failed to compile fragment shader stage.\n");
goto error;
2016-02-16 20:24:00 +01:00
}
string_list_free(lines);
2016-02-16 20:24:00 +01:00
return true;
error:
if (lines)
string_list_free(lines);
2019-08-18 14:40:26 +02:00
#endif
return false;
2016-02-16 20:24:00 +01:00
}