RetroArch/gfx/drivers_shader/glslang_util.cpp

395 lines
10 KiB
C++
Raw Normal View History

2016-02-16 19:24:00 +00:00
/* RetroArch - A frontend for libretro.
2017-01-22 12:40:32 +00:00
* Copyright (C) 2010-2017 - Hans-Kristian Arntzen
2016-02-16 19:24:00 +00: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 13:07:07 +00:00
#include <string.h>
2016-02-16 19:24:00 +00:00
#include <string>
#include <sstream>
2016-08-01 16:57:00 +00:00
#include <algorithm>
2016-02-16 19:24:00 +00:00
2016-09-16 15:33:18 +00:00
#include <retro_miscellaneous.h>
2016-09-11 13:07:07 +00:00
#include <file/file_path.h>
2016-03-20 15:29:14 +00:00
#include <streams/file_stream.h>
#include <lists/string_list.h>
2016-09-16 15:33:18 +00:00
#include <string/stdstring.h>
2016-02-16 19:24:00 +00:00
#include "glslang_util.hpp"
#include "glslang.hpp"
2016-09-11 13:07:07 +00:00
#include "../../verbosity.h"
2016-02-16 19:24:00 +00:00
using namespace std;
bool glslang_read_shader_file(const char *path, vector<string> *output, bool root_file)
2016-02-16 19:24:00 +00:00
{
vector<const char *> lines;
2016-10-21 03:57:40 +00:00
char include_path[PATH_MAX_LENGTH];
char tmp[PATH_MAX_LENGTH];
2016-09-11 13:07:07 +00:00
char *ptr = NULL;
char *buf = nullptr;
ssize_t len = 0;
const char *basename = path_basename(path);
2016-03-01 23:07:31 +00:00
2016-10-21 03:57:40 +00:00
include_path[0] = tmp[0] = '\0';
if (!filestream_read_file(path, (void**)&buf, &len))
2016-02-16 19:24:00 +00:00
{
RARCH_ERR("Failed to open shader file: \"%s\".\n", path);
return false;
}
2016-09-11 13:07:07 +00:00
/* Cannot use string_split since it removes blank lines (strtok). */
ptr = buf;
while (ptr && *ptr)
{
2017-01-12 09:32:57 +00:00
char *next_ptr = NULL;
lines.push_back(ptr);
2016-03-01 23:07:31 +00:00
2017-01-12 09:32:57 +00:00
next_ptr = strchr(ptr, '\n');
if (next_ptr)
{
ptr = next_ptr + 1;
*next_ptr = '\0';
}
else
ptr = nullptr;
}
if (lines.empty())
2017-01-12 09:32:57 +00:00
goto error;
2016-02-16 19:24:00 +00:00
if (root_file)
2016-02-16 19:24:00 +00:00
{
if (strstr(lines[0], "#version ") != lines[0])
{
RARCH_ERR("First line of the shader must contain a valid #version string.\n");
return false;
}
output->push_back(lines[0]);
2016-09-11 13:07:07 +00: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. */
output->push_back("#extension GL_GOOGLE_cpp_style_line_directive : require");
2016-02-16 19:24:00 +00:00
}
2016-09-11 13:07:07 +00:00
/* At least VIM treats the first line as line #1,
* so offset everything by one. */
snprintf(tmp, sizeof(tmp), "#line %u \"%s\"", root_file ? 2 : 1, basename);
output->push_back(tmp);
for (size_t i = root_file ? 1 : 0; i < lines.size(); i++)
{
const char *line = lines[i];
if (strstr(line, "#include ") == line)
{
2016-09-11 13:07:07 +00:00
char *closing = NULL;
char *c = (char*)strchr(line, '"');
if (!c)
{
RARCH_ERR("Invalid include statement \"%s\".\n", line);
2017-01-12 09:32:57 +00:00
goto error;
}
2017-01-12 09:32:57 +00:00
c++;
2016-09-11 13:07:07 +00:00
closing = (char*)strchr(c, '"');
if (!closing)
{
RARCH_ERR("Invalid include statement \"%s\".\n", line);
2017-01-12 09:32:57 +00:00
goto error;
}
2016-09-11 13:07:07 +00:00
*closing = '\0';
2016-09-11 13:07:07 +00:00
fill_pathname_resolve_relative(include_path, path, c, sizeof(include_path));
if (!glslang_read_shader_file(include_path, output, false))
2017-01-12 09:32:57 +00:00
goto error;
2016-09-11 13:07:07 +00:00
/* After including a file, use line directive
* to pull it back to current file. */
snprintf(tmp, sizeof(tmp), "#line %u \"%s\"", unsigned(i + 1), basename);
output->push_back(tmp);
}
else if (strstr(line, "#endif") || strstr(line, "#pragma"))
{
2016-09-11 13:07:07 +00:00
/* #line seems to be ignored if preprocessor tests fail,
* so we should reapply #line after each #endif.
* Add extra offset here since we're setting #line
* for the line after this one.
*/
snprintf(tmp, sizeof(tmp), "#line %u \"%s\"", unsigned(i + 2), basename);
output->push_back(line);
output->push_back(tmp);
}
else
output->push_back(line);
}
2016-02-16 19:24:00 +00:00
free(buf);
2016-02-16 19:24:00 +00:00
return true;
2017-01-12 09:32:57 +00:00
error:
free(buf);
return false;
2016-02-16 19:24:00 +00:00
}
static string build_stage_source(const vector<string> &lines, const char *stage)
2016-02-16 19:24:00 +00:00
{
ostringstream str;
2016-03-01 23:07:31 +00:00
bool active = true;
2016-02-16 19:24:00 +00:00
2016-09-11 13:07:07 +00:00
/* Version header. */
2016-02-16 19:24:00 +00:00
str << lines.front();
str << '\n';
for (auto itr = begin(lines) + 1; itr != end(lines); ++itr)
{
if (itr->find("#pragma stage ") == 0)
2016-02-16 19:24:00 +00:00
{
if (stage)
{
auto expected = string("#pragma stage ") + stage;
active = itr->find(expected) != string::npos;
}
2016-02-16 19:24:00 +00:00
}
else if (itr->find("#pragma name ") == 0 ||
itr->find("#pragma format ") == 0)
2016-03-26 17:07:59 +00:00
{
2016-09-11 13:07:07 +00:00
/* Ignore */
2016-03-26 17:07:59 +00:00
}
2016-02-16 19:24:00 +00:00
else if (active)
str << *itr;
str << '\n';
}
return str.str();
}
static const char *glslang_formats[] = {
"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",
"UNKNOWN",
};
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
2016-09-16 15:33:18 +00:00
#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;
}
bool glslang_parse_meta(const vector<string> &lines, glslang_meta *meta)
{
2017-01-12 09:32:57 +00:00
char id[64];
char desc[64];
id[0] = desc[0] = '\0';
2016-08-01 13:48:09 +00:00
2017-01-12 09:32:57 +00:00
*meta = glslang_meta{};
2016-09-11 13:07:07 +00:00
for (auto &line : lines)
{
if (line.find("#pragma name ") == 0)
{
2017-01-12 09:32:57 +00:00
const char *str = NULL;
if (!meta->name.empty())
{
RARCH_ERR("[slang]: Trying to declare multiple names for file.\n");
return false;
}
2017-01-12 09:32:57 +00:00
str = line.c_str() + strlen("#pragma name ");
while (*str == ' ')
str++;
meta->name = str;
}
2016-08-01 13:48:09 +00:00
else if (line.find("#pragma parameter ") == 0)
{
float initial, minimum, maximum, step;
int ret = sscanf(line.c_str(), "#pragma parameter %63s \"%63[^\"]\" %f %f %f %f",
id, desc, &initial, &minimum, &maximum, &step);
2016-08-01 16:57:00 +00:00
if (ret == 5)
2016-08-01 13:48:09 +00:00
{
step = 0.1f * (maximum - minimum);
2017-01-12 09:32:57 +00:00
ret = 6;
2016-08-01 13:48:09 +00:00
}
2016-08-01 16:57:00 +00:00
if (ret == 6)
{
auto itr = find_if(begin(meta->parameters), end(meta->parameters), [&](const glslang_parameter &param) {
return param.id == id;
});
2016-09-11 13:07:07 +00:00
/* Allow duplicate #pragma parameter, but only
* if they are exactly the same. */
2016-08-01 16:57:00 +00:00
if (itr != end(meta->parameters))
{
2017-01-12 09:32:57 +00:00
if ( itr->desc != desc ||
itr->initial != initial ||
itr->minimum != minimum ||
itr->maximum != maximum ||
itr->step != step
)
2016-08-01 16:57:00 +00: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.c_str());
return false;
}
2016-08-01 13:48:09 +00:00
}
else if (line.find("#pragma format ") == 0)
{
2016-09-11 13:07:07 +00: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;
}
2016-09-11 13:07:07 +00:00
str = line.c_str() + strlen("#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 19:24:00 +00:00
bool glslang_compile_shader(const char *shader_path, glslang_output *output)
{
vector<string> lines;
2016-03-01 23:07:31 +00:00
RARCH_LOG("[slang]: Compiling shader \"%s\".\n", shader_path);
2017-01-12 09:32:57 +00:00
if (!glslang_read_shader_file(shader_path, &lines, true))
2016-02-16 19:24:00 +00:00
return false;
if (!glslang_parse_meta(lines, &output->meta))
return false;
2017-01-12 09:32:57 +00:00
if ( !glslang::compile_spirv(build_stage_source(lines, "vertex"),
2016-02-16 19:24:00 +00:00
glslang::StageVertex, &output->vertex))
{
RARCH_ERR("Failed to compile vertex shader stage.\n");
return false;
}
2017-01-12 09:32:57 +00:00
if ( !glslang::compile_spirv(build_stage_source(lines, "fragment"),
2016-02-16 19:24:00 +00:00
glslang::StageFragment, &output->fragment))
{
RARCH_ERR("Failed to compile fragment shader stage.\n");
return false;
}
return true;
}