RetroArch/gfx/video_shader_parse.c

1048 lines
29 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-01-22 12:40:32 +00:00
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* 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/>.
*/
2014-06-17 18:42:38 +00:00
#include <stdlib.h>
#include <string.h>
2015-10-31 19:53:08 +00:00
2018-03-02 00:41:05 +00:00
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
2017-12-14 23:31:37 +00:00
#include <libretro.h>
2014-10-21 05:58:58 +00:00
#include <compat/posix_string.h>
#include <compat/msvc.h>
#include <compat/strl.h>
2014-10-21 22:23:06 +00:00
#include <file/file_path.h>
2015-06-14 17:02:53 +00:00
#include <rhash.h>
2016-01-20 03:07:24 +00:00
#include <string/stdstring.h>
#include <streams/file_stream.h>
#include <lists/string_list.h>
2015-06-14 17:02:53 +00:00
#include "../configuration.h"
#include "../retroarch.h"
#include "../verbosity.h"
#include "../frontend/frontend_driver.h"
#include "../command.h"
#include "../file_path_special.h"
2015-06-14 17:02:53 +00:00
#include "video_shader_parse.h"
#if defined(HAVE_SLANG) && defined(HAVE_SPIRV_CROSS)
#include "drivers_shader/slang_preprocess.h"
#endif
static path_change_data_t *file_change_data = NULL;
2015-01-11 18:59:59 +00:00
/**
* wrap_mode_to_str:
* @type : Wrap type.
*
* Translates wrap mode to human-readable string identifier.
*
* Returns: human-readable string identifier of wrap mode.
**/
static const char *wrap_mode_to_str(enum gfx_wrap_type type)
{
switch (type)
{
case RARCH_WRAP_BORDER:
return "clamp_to_border";
case RARCH_WRAP_EDGE:
return "clamp_to_edge";
case RARCH_WRAP_REPEAT:
return "repeat";
case RARCH_WRAP_MIRRORED_REPEAT:
return "mirrored_repeat";
default:
2015-10-11 07:31:04 +00:00
break;
}
2015-10-11 07:31:04 +00:00
return "???";
}
/**
2015-01-11 18:59:59 +00:00
* wrap_str_to_mode:
* @type : Wrap type in human-readable string format.
*
* Translates wrap mode from human-readable string to enum mode value.
*
* Returns: enum mode value of wrap type.
**/
static enum gfx_wrap_type wrap_str_to_mode(const char *wrap_mode)
{
if (string_is_equal(wrap_mode, "clamp_to_border"))
2018-02-25 00:07:14 +00:00
return RARCH_WRAP_BORDER;
else if (string_is_equal(wrap_mode, "clamp_to_edge"))
2018-02-25 00:07:14 +00:00
return RARCH_WRAP_EDGE;
else if (string_is_equal(wrap_mode, "repeat"))
2018-02-25 00:07:14 +00:00
return RARCH_WRAP_REPEAT;
else if (string_is_equal(wrap_mode, "mirrored_repeat"))
2018-02-25 00:07:14 +00:00
return RARCH_WRAP_MIRRORED_REPEAT;
2014-09-05 19:06:41 +00:00
2018-02-26 14:12:19 +00:00
RARCH_WARN("Invalid wrapping type %s. Valid ones are: clamp_to_border"
" (default), clamp_to_edge, repeat and mirrored_repeat. Falling back to default.\n",
2014-09-05 19:06:41 +00:00
wrap_mode);
return RARCH_WRAP_DEFAULT;
}
/**
* video_shader_parse_pass:
2015-01-11 18:59:59 +00:00
* @conf : Preset file to read from.
* @pass : Shader passes handle.
* @i : Index of shader pass.
*
* Parses shader pass from preset file.
*
* Returns: true (1) if successful, otherwise false (0).
**/
2016-02-06 20:51:37 +00:00
static bool video_shader_parse_pass(config_file_t *conf,
struct video_shader_pass *pass, unsigned i)
{
2016-10-21 04:10:58 +00:00
char shader_name[64];
char filter_name_buf[64];
char wrap_name_buf[64];
char wrap_mode[64];
char frame_count_mod_buf[64];
char srgb_output_buf[64];
2016-12-19 18:17:23 +00:00
char fp_fbo_buf[64];
char mipmap_buf[64];
char alias_buf[64];
char scale_name_buf[64];
char attr_name_buf[64];
char scale_type[64];
char scale_type_x[64];
char scale_type_y[64];
char frame_count_mod[64];
size_t path_size = PATH_MAX_LENGTH;
char *tmp_path = (char*)malloc(path_size);
2015-06-12 23:18:13 +00:00
struct gfx_fbo_scale *scale = NULL;
2016-05-24 20:48:15 +00:00
bool tmp_bool = false;
2015-06-12 23:18:13 +00:00
float fattr = 0.0f;
int iattr = 0;
if (!tmp_path)
return false;
fp_fbo_buf[0] = mipmap_buf[0] = alias_buf[0] =
scale_name_buf[0] = attr_name_buf[0] = scale_type[0] =
scale_type_x[0] = scale_type_y[0] = frame_count_mod[0] =
shader_name[0] = filter_name_buf[0] = wrap_name_buf[0] =
wrap_mode[0] = frame_count_mod_buf[0] = srgb_output_buf[0] = '\0';
2016-10-21 04:10:58 +00:00
2014-09-09 03:56:12 +00:00
/* Source */
2014-10-26 02:04:35 +00:00
snprintf(shader_name, sizeof(shader_name), "shader%u", i);
if (!config_get_path(conf, shader_name, tmp_path, path_size))
{
RARCH_ERR("Couldn't parse shader source (%s).\n", shader_name);
free(tmp_path);
return false;
}
fill_pathname_resolve_relative(pass->source.path,
conf->path, tmp_path, sizeof(pass->source.path));
free(tmp_path);
2014-09-09 03:56:12 +00:00
/* Smooth */
2014-10-26 02:04:35 +00:00
snprintf(filter_name_buf, sizeof(filter_name_buf), "filter_linear%u", i);
2016-05-24 20:48:15 +00:00
if (config_get_bool(conf, filter_name_buf, &tmp_bool))
{
bool smooth = tmp_bool;
pass->filter = smooth ? RARCH_FILTER_LINEAR : RARCH_FILTER_NEAREST;
2016-05-24 20:48:15 +00:00
}
else
pass->filter = RARCH_FILTER_UNSPEC;
2014-09-09 03:56:12 +00:00
/* Wrapping mode */
2014-10-26 02:04:35 +00:00
snprintf(wrap_name_buf, sizeof(wrap_name_buf), "wrap_mode%u", i);
if (config_get_array(conf, wrap_name_buf, wrap_mode, sizeof(wrap_mode)))
pass->wrap = wrap_str_to_mode(wrap_mode);
2014-09-09 03:56:12 +00:00
/* Frame count mod */
2014-10-26 02:04:35 +00:00
snprintf(frame_count_mod_buf, sizeof(frame_count_mod_buf), "frame_count_mod%u", i);
2014-09-08 15:57:18 +00:00
if (config_get_array(conf, frame_count_mod_buf,
frame_count_mod, sizeof(frame_count_mod)))
pass->frame_count_mod = (unsigned)strtoul(frame_count_mod, NULL, 0);
2014-09-09 03:56:12 +00:00
/* FBO types and mipmapping */
2014-10-26 02:04:35 +00:00
snprintf(srgb_output_buf, sizeof(srgb_output_buf), "srgb_framebuffer%u", i);
2016-05-24 20:48:15 +00:00
if (config_get_bool(conf, srgb_output_buf, &tmp_bool))
pass->fbo.srgb_fbo = tmp_bool;
2014-05-11 17:35:54 +00:00
2014-10-26 02:04:35 +00:00
snprintf(fp_fbo_buf, sizeof(fp_fbo_buf), "float_framebuffer%u", i);
2016-05-24 20:48:15 +00:00
if (config_get_bool(conf, fp_fbo_buf, &tmp_bool))
pass->fbo.fp_fbo = tmp_bool;
2014-05-11 11:13:38 +00:00
2014-10-26 02:04:35 +00:00
snprintf(mipmap_buf, sizeof(mipmap_buf), "mipmap_input%u", i);
2016-05-24 20:48:15 +00:00
if (config_get_bool(conf, mipmap_buf, &tmp_bool))
pass->mipmap = tmp_bool;
2014-05-11 11:13:38 +00:00
2014-10-26 02:04:35 +00:00
snprintf(alias_buf, sizeof(alias_buf), "alias%u", i);
if (!config_get_array(conf, alias_buf, pass->alias, sizeof(pass->alias)))
*pass->alias = '\0';
2014-09-09 03:56:12 +00:00
/* Scale */
scale = &pass->fbo;
2014-10-26 02:04:35 +00:00
snprintf(scale_name_buf, sizeof(scale_name_buf), "scale_type%u", i);
config_get_array(conf, scale_name_buf, scale_type, sizeof(scale_type));
2014-10-26 02:04:35 +00:00
snprintf(scale_name_buf, sizeof(scale_name_buf), "scale_type_x%u", i);
config_get_array(conf, scale_name_buf, scale_type_x, sizeof(scale_type_x));
2014-10-26 02:04:35 +00:00
snprintf(scale_name_buf, sizeof(scale_name_buf), "scale_type_y%u", i);
config_get_array(conf, scale_name_buf, scale_type_y, sizeof(scale_type_y));
if (!*scale_type && !*scale_type_x && !*scale_type_y)
return true;
if (*scale_type)
{
strlcpy(scale_type_x, scale_type, sizeof(scale_type_x));
strlcpy(scale_type_y, scale_type, sizeof(scale_type_y));
}
2015-06-14 17:02:53 +00:00
scale->valid = true;
scale->type_x = RARCH_SCALE_INPUT;
scale->type_y = RARCH_SCALE_INPUT;
scale->scale_x = 1.0;
scale->scale_y = 1.0;
if (*scale_type_x)
{
if (string_is_equal(scale_type_x, "source"))
2018-02-25 00:07:14 +00:00
scale->type_x = RARCH_SCALE_INPUT;
else if (string_is_equal(scale_type_x, "viewport"))
2018-02-25 00:07:14 +00:00
scale->type_x = RARCH_SCALE_VIEWPORT;
else if (string_is_equal(scale_type_x, "absolute"))
2018-02-25 00:07:14 +00:00
scale->type_x = RARCH_SCALE_ABSOLUTE;
else
{
2018-02-25 00:07:14 +00:00
RARCH_ERR("Invalid attribute.\n");
return false;
}
}
if (*scale_type_y)
{
if (string_is_equal(scale_type_y, "source"))
2018-02-25 00:07:14 +00:00
scale->type_y = RARCH_SCALE_INPUT;
else if (string_is_equal(scale_type_y, "viewport"))
2018-02-25 00:07:14 +00:00
scale->type_y = RARCH_SCALE_VIEWPORT;
else if (string_is_equal(scale_type_y, "absolute"))
2018-02-25 00:07:14 +00:00
scale->type_y = RARCH_SCALE_ABSOLUTE;
else
{
2018-02-25 00:07:14 +00:00
RARCH_ERR("Invalid attribute.\n");
return false;
}
}
2014-10-26 02:04:35 +00:00
snprintf(attr_name_buf, sizeof(attr_name_buf), "scale%u", i);
2018-08-14 22:44:19 +00:00
if (scale->type_x == RARCH_SCALE_ABSOLUTE)
{
if (config_get_int(conf, attr_name_buf, &iattr))
scale->abs_x = iattr;
else
{
2014-10-26 02:04:35 +00:00
snprintf(attr_name_buf, sizeof(attr_name_buf), "scale_x%u", i);
if (config_get_int(conf, attr_name_buf, &iattr))
scale->abs_x = iattr;
}
}
else
{
if (config_get_float(conf, attr_name_buf, &fattr))
scale->scale_x = fattr;
else
{
2014-10-26 02:04:35 +00:00
snprintf(attr_name_buf, sizeof(attr_name_buf), "scale_x%u", i);
if (config_get_float(conf, attr_name_buf, &fattr))
scale->scale_x = fattr;
}
}
2014-10-26 02:04:35 +00:00
snprintf(attr_name_buf, sizeof(attr_name_buf), "scale%u", i);
2018-08-14 22:44:19 +00:00
if (scale->type_y == RARCH_SCALE_ABSOLUTE)
{
if (config_get_int(conf, attr_name_buf, &iattr))
scale->abs_y = iattr;
else
{
2014-10-26 02:04:35 +00:00
snprintf(attr_name_buf, sizeof(attr_name_buf), "scale_y%u", i);
if (config_get_int(conf, attr_name_buf, &iattr))
scale->abs_y = iattr;
}
}
else
{
if (config_get_float(conf, attr_name_buf, &fattr))
scale->scale_y = fattr;
else
{
2014-10-26 02:04:35 +00:00
snprintf(attr_name_buf, sizeof(attr_name_buf), "scale_y%u", i);
if (config_get_float(conf, attr_name_buf, &fattr))
scale->scale_y = fattr;
}
}
return true;
}
/**
* video_shader_parse_textures:
2015-01-11 18:59:59 +00:00
* @conf : Preset file to read from.
* @shader : Shader pass handle.
*
* Parses shader textures.
*
* Returns: true (1) if successful, otherwise false (0).
**/
static bool video_shader_parse_textures(config_file_t *conf,
struct video_shader *shader)
{
size_t path_size = PATH_MAX_LENGTH;
2015-06-12 23:18:13 +00:00
const char *id = NULL;
char *save = NULL;
char *textures = (char*)malloc(1024 + path_size);
char *tmp_path = textures + 1024;
if (!textures)
return false;
2016-10-21 04:10:58 +00:00
textures[0] = '\0';
if (!config_get_array(conf, "textures", textures, 1024))
{
free(textures);
return true;
}
for (id = strtok_r(textures, ";", &save);
id && shader->luts < GFX_MAX_TEXTURES;
shader->luts++, id = strtok_r(NULL, ";", &save))
{
2016-10-21 04:10:58 +00:00
char id_filter[64];
char id_wrap[64];
char wrap_mode[64];
char id_mipmap[64];
2015-06-12 23:18:13 +00:00
bool mipmap = false;
bool smooth = false;
2016-10-21 04:10:58 +00:00
id_filter[0] = id_wrap[0] = wrap_mode[0] = id_mipmap[0] = '\0';
if (!config_get_array(conf, id, tmp_path, path_size))
{
RARCH_ERR("Cannot find path to texture \"%s\" ...\n", id);
free(textures);
return false;
}
fill_pathname_resolve_relative(shader->lut[shader->luts].path,
conf->path, tmp_path, sizeof(shader->lut[shader->luts].path));
2014-09-08 15:57:18 +00:00
strlcpy(shader->lut[shader->luts].id, id,
sizeof(shader->lut[shader->luts].id));
2014-10-26 02:04:35 +00:00
snprintf(id_filter, sizeof(id_filter), "%s_linear", id);
if (config_get_bool(conf, id_filter, &smooth))
shader->lut[shader->luts].filter = smooth ?
2014-09-08 15:57:18 +00:00
RARCH_FILTER_LINEAR : RARCH_FILTER_NEAREST;
else
shader->lut[shader->luts].filter = RARCH_FILTER_UNSPEC;
2014-10-26 02:04:35 +00:00
snprintf(id_wrap, sizeof(id_wrap), "%s_wrap_mode", id);
if (config_get_array(conf, id_wrap, wrap_mode, sizeof(wrap_mode)))
shader->lut[shader->luts].wrap = wrap_str_to_mode(wrap_mode);
2014-10-26 02:04:35 +00:00
snprintf(id_mipmap, sizeof(id_mipmap), "%s_mipmap", id);
if (config_get_bool(conf, id_mipmap, &mipmap))
shader->lut[shader->luts].mipmap = mipmap;
else
shader->lut[shader->luts].mipmap = false;
}
free(textures);
return true;
}
/**
* video_shader_parse_find_parameter:
2015-01-11 18:59:59 +00:00
* @params : Shader parameter handle.
* @num_params : Number of shader params in @params.
* @id : Identifier to search for.
*
* Finds a shader parameter with identifier @id in @params..
*
* Returns: handle to shader parameter if successful, otherwise NULL.
**/
2015-01-19 20:24:08 +00:00
static struct video_shader_parameter *video_shader_parse_find_parameter(
2016-02-06 20:51:37 +00:00
struct video_shader_parameter *params,
unsigned num_params, const char *id)
2014-05-22 20:02:43 +00:00
{
unsigned i;
2014-05-22 20:02:43 +00:00
for (i = 0; i < num_params; i++)
{
2016-01-20 03:07:24 +00:00
if (string_is_equal(params[i].id, id))
2014-05-22 20:02:43 +00:00
return &params[i];
}
2015-01-11 18:59:59 +00:00
2014-05-22 20:02:43 +00:00
return NULL;
}
/**
* video_shader_set_current_parameters:
* @conf : Preset file to read from.
* @shader : Shader passes handle.
*
* Reads the current value for all parameters from config file.
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool video_shader_resolve_current_parameters(config_file_t *conf,
struct video_shader *shader)
{
size_t param_size = 4096 * sizeof(char);
const char *id = NULL;
char *parameters = NULL;
char *save = NULL;
2006-01-17 05:11:55 +00:00
if (!conf)
return false;
parameters = (char*)malloc(param_size);
if (!parameters)
return false;
parameters[0] = '\0';
2016-10-21 04:10:58 +00:00
2006-01-17 05:11:55 +00:00
/* Read in parameters which override the defaults. */
if (!config_get_array(conf, "parameters",
parameters, param_size))
{
free(parameters);
return true;
}
for (id = strtok_r(parameters, ";", &save); id;
id = strtok_r(NULL, ";", &save))
{
struct video_shader_parameter *parameter =
(struct video_shader_parameter*)
video_shader_parse_find_parameter(
shader->parameters, shader->num_parameters, id);
if (!parameter)
{
2018-02-26 14:12:19 +00:00
RARCH_WARN("[CGP/GLSLP]: Parameter %s is set in the preset,"
" but no shader uses this parameter, ignoring.\n", id);
continue;
}
if (!config_get_float(conf, id, &parameter->current))
RARCH_WARN("[CGP/GLSLP]: Parameter %s is not set in preset.\n", id);
}
free(parameters);
2016-08-01 16:57:00 +00:00
return true;
}
/**
* video_shader_resolve_parameters:
2015-01-11 18:59:59 +00:00
* @conf : Preset file to read from.
* @shader : Shader passes handle.
*
* Resolves all shader parameters belonging to shaders.
2015-01-11 18:59:59 +00:00
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool video_shader_resolve_parameters(config_file_t *conf,
2015-01-19 20:24:08 +00:00
struct video_shader *shader)
2014-05-22 19:24:52 +00:00
{
2014-05-22 20:02:43 +00:00
unsigned i;
2016-08-01 13:43:34 +00:00
struct video_shader_parameter *param = &shader->parameters[0];
2014-05-22 19:24:52 +00:00
shader->num_parameters = 0;
2014-09-08 15:57:18 +00:00
/* Find all parameters in our shaders. */
2015-01-11 18:59:59 +00:00
2014-05-22 19:24:52 +00:00
for (i = 0; i < shader->passes; i++)
{
const char *path = shader->pass[i].source.path;
uint8_t *buf = NULL;
int64_t buf_len = 0;
struct string_list *lines = NULL;
size_t line_index = 0;
if (string_is_empty(path))
continue;
if (!path_is_valid(path))
continue;
2017-10-22 03:24:57 +00:00
/* Read file contents */
if (!filestream_read_file(path, (void**)&buf, &buf_len))
2014-05-22 19:24:52 +00:00
continue;
/* Split into lines */
if (buf_len > 0)
lines = string_split((const char*)buf, "\n");
/* Buffer is no longer required - clean up */
if ((void*)buf)
free((void*)buf);
if (!lines)
continue;
2016-10-21 04:10:58 +00:00
#if defined(HAVE_SLANG) && defined(HAVE_SPIRV_CROSS)
/* First try to use the more robust slang
* implementation to support #includes. */
/* FIXME: The check for slang can be removed
* if it's sufficiently tested for
* GLSL/Cg as well, it should be the same implementation. */
if (string_is_equal(path_get_extension(path), "slang") &&
slang_preprocess_parse_parameters(lines, shader))
continue;
/* If that doesn't work, fallback to the old path.
* Ideally, we'd get rid of this path sooner or later. */
#endif
/* even though the pass is set in the loop too, not all
* passes have parameters */
2018-08-18 04:33:52 +00:00
param->pass = i;
while ((shader->num_parameters < ARRAY_SIZE(shader->parameters)) &&
(line_index < lines->size))
2014-05-22 19:24:52 +00:00
{
int ret;
const char *line = lines->elems[line_index].data;
line_index++;
/* Check if this is a '#pragma parameter' line */
2019-08-08 12:57:28 +00:00
if (strncmp("#pragma parameter", line,
STRLEN_CONST("#pragma parameter")))
continue;
/* Parse line */
ret = sscanf(line, "#pragma parameter %63s \"%63[^\"]\" %f %f %f %f",
2018-02-25 22:49:00 +00:00
param->id, param->desc, &param->initial,
2014-09-08 15:57:18 +00:00
&param->minimum, &param->maximum, &param->step);
2014-05-22 20:02:43 +00:00
2015-01-11 18:59:59 +00:00
if (ret < 5)
continue;
2015-10-11 07:31:04 +00:00
param->id[63] = '\0';
2015-01-11 18:59:59 +00:00
param->desc[63] = '\0';
2014-05-22 20:02:43 +00:00
2015-01-11 18:59:59 +00:00
if (ret == 5)
param->step = 0.1f * (param->maximum - param->minimum);
2014-05-22 20:02:43 +00:00
param->pass = i;
RARCH_LOG("Found #pragma parameter %s (%s) %f %f %f %f in pass %d\n",
2018-02-25 22:49:00 +00:00
param->desc, param->id, param->initial,
param->minimum, param->maximum, param->step, param->pass);
2015-01-11 18:59:59 +00:00
param->current = param->initial;
2014-05-22 19:24:52 +00:00
2015-01-11 18:59:59 +00:00
shader->num_parameters++;
param++;
2014-05-22 19:24:52 +00:00
}
string_list_free(lines);
2014-05-22 19:24:52 +00:00
}
2019-08-08 12:57:28 +00:00
return video_shader_resolve_current_parameters(conf, shader);
2014-05-22 19:24:52 +00:00
}
/**
* video_shader_read_conf_preset:
2015-01-11 19:17:45 +00:00
* @conf : Preset file to read from.
* @shader : Shader passes handle.
*
* Loads preset file and all associated state (passes,
* textures, imports, etc).
2015-01-11 19:17:45 +00:00
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool video_shader_read_conf_preset(config_file_t *conf,
struct video_shader *shader)
{
2017-09-08 14:43:34 +00:00
unsigned i;
2018-02-26 14:12:19 +00:00
union string_list_elem_attr attr;
2018-02-25 22:54:46 +00:00
unsigned shaders = 0;
settings_t *settings = config_get_ptr();
struct string_list *file_list = NULL;
bool watch_files = settings->bools.video_shader_watch_files;
memset(shader, 0, sizeof(*shader));
if (!config_get_uint(conf, "shaders", &shaders))
{
RARCH_ERR("Cannot find \"shaders\" param.\n");
return false;
}
if (!shaders)
{
RARCH_ERR("Need to define at least 1 shader.\n");
return false;
}
2018-02-25 22:54:46 +00:00
if (!config_get_int(conf, "feedback_pass",
&shader->feedback_pass))
shader->feedback_pass = -1;
2016-03-01 23:07:31 +00:00
shader->passes = MIN(shaders, GFX_MAX_SHADERS);
2018-02-26 14:12:19 +00:00
attr.i = 0;
2016-03-01 23:07:31 +00:00
strlcpy(shader->path, conf->path, sizeof(shader->path));
if (watch_files)
{
if (file_change_data)
2018-02-25 22:54:46 +00:00
frontend_driver_watch_path_for_changes(NULL,
0, &file_change_data);
file_change_data = NULL;
2018-02-25 22:54:46 +00:00
file_list = string_list_new();
string_list_append(file_list, conf->path, attr);
}
for (i = 0; i < shader->passes; i++)
{
if (!video_shader_parse_pass(conf, &shader->pass[i], i))
2018-01-26 05:14:59 +00:00
{
if (file_list)
2018-08-14 22:44:19 +00:00
{
2018-01-26 05:14:59 +00:00
string_list_free(file_list);
2018-08-14 22:44:19 +00:00
file_list = NULL;
}
return false;
2018-01-26 05:14:59 +00:00
}
if (watch_files && file_list)
2018-02-25 22:54:46 +00:00
string_list_append(file_list,
shader->pass[i].source.path, attr);
}
if (watch_files)
{
2018-02-25 22:54:46 +00:00
int flags = PATH_CHANGE_TYPE_MODIFIED |
PATH_CHANGE_TYPE_WRITE_FILE_CLOSED |
2018-02-25 22:54:46 +00:00
PATH_CHANGE_TYPE_FILE_MOVED |
PATH_CHANGE_TYPE_FILE_DELETED;
2018-02-25 22:54:46 +00:00
frontend_driver_watch_path_for_changes(file_list,
flags, &file_change_data);
2018-08-14 22:44:19 +00:00
if (file_list)
string_list_free(file_list);
}
command_event(CMD_EVENT_SHADER_PRESET_LOADED, NULL);
if (!video_shader_parse_textures(conf, shader))
return false;
return true;
}
2014-09-08 15:57:18 +00:00
/* CGP store */
static const char *scale_type_to_str(enum gfx_scale_type type)
{
switch (type)
{
case RARCH_SCALE_INPUT:
return "source";
case RARCH_SCALE_VIEWPORT:
return "viewport";
case RARCH_SCALE_ABSOLUTE:
return "absolute";
default:
2015-10-11 07:31:04 +00:00
break;
}
2015-10-11 07:31:04 +00:00
return "?";
}
2018-02-25 22:54:46 +00:00
static void shader_write_scale_dim(config_file_t *conf,
const char *dim,
enum gfx_scale_type type, float scale,
unsigned absolute, unsigned i)
{
2016-10-21 04:10:58 +00:00
char key[64];
key[0] = '\0';
2014-10-26 02:04:35 +00:00
snprintf(key, sizeof(key), "scale_type_%s%u", dim, i);
config_set_string(conf, key, scale_type_to_str(type));
2014-10-26 02:04:35 +00:00
snprintf(key, sizeof(key), "scale_%s%u", dim, i);
if (type == RARCH_SCALE_ABSOLUTE)
config_set_int(conf, key, absolute);
else
config_set_float(conf, key, scale);
}
2014-09-08 15:57:18 +00:00
static void shader_write_fbo(config_file_t *conf,
const struct gfx_fbo_scale *fbo, unsigned i)
{
2016-10-21 04:10:58 +00:00
char key[64];
key[0] = '\0';
2014-10-26 02:04:35 +00:00
snprintf(key, sizeof(key), "float_framebuffer%u", i);
config_set_bool(conf, key, fbo->fp_fbo);
2014-10-26 02:04:35 +00:00
snprintf(key, sizeof(key), "srgb_framebuffer%u", i);
2014-05-11 17:35:54 +00:00
config_set_bool(conf, key, fbo->srgb_fbo);
if (!fbo->valid)
return;
2018-02-25 22:54:46 +00:00
shader_write_scale_dim(conf, "x", fbo->type_x,
fbo->scale_x, fbo->abs_x, i);
shader_write_scale_dim(conf, "y", fbo->type_y,
fbo->scale_y, fbo->abs_y, i);
}
static void make_relative_path_portable(char *path)
{
#ifdef _WIN32
/* use '/' instead of '\' for maximum portability */
if (!path_is_absolute(path))
{
char *p;
for (p = path; *p; p++)
if (*p == '\\')
*p = '/';
}
#endif
}
/**
* video_shader_write_conf_preset:
* @conf : Preset file to write to.
2015-01-11 19:17:45 +00:00
* @shader : Shader passes handle.
* @preset_path : Optional path to where the preset will be written.
2015-01-11 19:17:45 +00:00
*
* Writes preset and all associated state (passes,
* textures, imports, etc) into @conf.
* If @preset_path is not NULL, shader paths are saved
* relative to it.
2015-01-11 19:17:45 +00:00
**/
void video_shader_write_conf_preset(config_file_t *conf,
struct video_shader *shader, const char *preset_path)
{
unsigned i;
char key[64];
size_t tmp_size = PATH_MAX_LENGTH;
char *tmp = (char*)malloc(3*tmp_size);
char *tmp_rel = tmp + tmp_size;
char *tmp_base = tmp + 2*tmp_size;
if (!tmp)
return;
config_set_int(conf, "shaders", shader->passes);
if (shader->feedback_pass >= 0)
config_set_int(conf, "feedback_pass", shader->feedback_pass);
if (preset_path)
{
strlcpy(tmp_base, preset_path, tmp_size);
/* ensure we use a clean base like the shader passes and texture paths do */
path_resolve_realpath(tmp_base, tmp_size, false);
path_basedir(tmp_base);
}
for (i = 0; i < shader->passes; i++)
{
2015-01-19 20:24:08 +00:00
const struct video_shader_pass *pass = &shader->pass[i];
snprintf(key, sizeof(key), "shader%u", i);
if (preset_path)
{
strlcpy(tmp, pass->source.path, tmp_size);
path_relative_to(tmp_rel, tmp, tmp_base, tmp_size);
make_relative_path_portable(tmp_rel);
2016-12-04 06:12:57 +00:00
config_set_path(conf, key, tmp_rel);
}
else
config_set_path(conf, key, pass->source.path);
if (pass->filter != RARCH_FILTER_UNSPEC)
{
snprintf(key, sizeof(key), "filter_linear%u", i);
config_set_bool(conf, key, pass->filter == RARCH_FILTER_LINEAR);
}
snprintf(key, sizeof(key), "wrap_mode%u", i);
config_set_string(conf, key, wrap_mode_to_str(pass->wrap));
if (pass->frame_count_mod)
{
snprintf(key, sizeof(key), "frame_count_mod%u", i);
config_set_int(conf, key, pass->frame_count_mod);
}
snprintf(key, sizeof(key), "mipmap_input%u", i);
config_set_bool(conf, key, pass->mipmap);
2014-05-11 11:13:38 +00:00
snprintf(key, sizeof(key), "alias%u", i);
config_set_string(conf, key, pass->alias);
shader_write_fbo(conf, &pass->fbo, i);
}
if (shader->num_parameters)
{
size_t param_size = 4096 * sizeof(char);
char *parameters = (char*)malloc(param_size);
2016-10-21 04:10:58 +00:00
if (parameters)
{
parameters[0] = '\0';
strlcpy(parameters, shader->parameters[0].id, param_size);
for (i = 1; i < shader->num_parameters; i++)
{
/* O(n^2), but number of parameters is very limited. */
2019-05-10 16:05:58 +00:00
strlcat(parameters, ";", param_size);
strlcat(parameters, shader->parameters[i].id, param_size);
}
config_set_string(conf, "parameters", parameters);
for (i = 0; i < shader->num_parameters; i++)
config_set_float(conf, shader->parameters[i].id,
shader->parameters[i].current);
free(parameters);
}
}
if (shader->luts)
{
size_t tex_size = 4096 * sizeof(char);
char *textures = (char*)malloc(tex_size);
2016-10-21 04:10:58 +00:00
if (textures)
{
textures[0] = '\0';
strlcpy(textures, shader->lut[0].id, tex_size);
for (i = 1; i < shader->luts; i++)
{
/* O(n^2), but number of textures is very limited. */
2019-05-10 16:05:58 +00:00
strlcat(textures, ";", tex_size);
strlcat(textures, shader->lut[i].id, tex_size);
}
2016-10-21 04:10:58 +00:00
config_set_string(conf, "textures", textures);
free(textures);
for (i = 0; i < shader->luts; i++)
{
char key[128];
key[0] = '\0';
if (preset_path)
{
strlcpy(tmp, shader->lut[i].path, tmp_size);
path_relative_to(tmp_rel, tmp, tmp_base, tmp_size);
make_relative_path_portable(tmp_rel);
config_set_path(conf, shader->lut[i].id, tmp_rel);
}
else
config_set_path(conf, shader->lut[i].id, shader->lut[i].path);
if (shader->lut[i].filter != RARCH_FILTER_UNSPEC)
{
snprintf(key, sizeof(key), "%s_linear", shader->lut[i].id);
config_set_bool(conf, key,
shader->lut[i].filter == RARCH_FILTER_LINEAR);
}
snprintf(key, sizeof(key),
"%s_wrap_mode", shader->lut[i].id);
config_set_string(conf, key,
wrap_mode_to_str(shader->lut[i].wrap));
snprintf(key, sizeof(key),
"%s_mipmap", shader->lut[i].id);
config_set_bool(conf, key,
shader->lut[i].mipmap);
}
}
}
free(tmp);
}
const char *video_shader_to_str(enum rarch_shader_type type)
{
switch (type)
{
case RARCH_SHADER_CG:
return "Cg";
case RARCH_SHADER_HLSL:
return "HLSL";
case RARCH_SHADER_GLSL:
return "GLSL";
case RARCH_SHADER_SLANG:
return "Slang";
case RARCH_SHADER_METAL:
return "Metal";
case RARCH_SHADER_NONE:
return "none";
default:
break;
}
return "???";
}
/**
* video_shader_is_supported:
* Tests if a shader type is supported.
* This is only accurate once the context driver was initialized.
**/
2018-02-25 16:20:22 +00:00
bool video_shader_is_supported(enum rarch_shader_type type)
{
gfx_ctx_flags_t flags;
enum display_flags testflag;
2019-03-13 19:39:36 +00:00
2019-07-12 14:31:16 +00:00
flags.flags = 0;
2019-03-13 19:39:36 +00:00
switch (type)
{
case RARCH_SHADER_SLANG:
testflag = GFX_CTX_FLAGS_SHADERS_SLANG;
2019-03-13 19:39:36 +00:00
break;
case RARCH_SHADER_GLSL:
testflag = GFX_CTX_FLAGS_SHADERS_GLSL;
2019-03-13 19:39:36 +00:00
break;
case RARCH_SHADER_CG:
testflag = GFX_CTX_FLAGS_SHADERS_CG;
2019-03-13 19:39:36 +00:00
break;
case RARCH_SHADER_HLSL:
testflag = GFX_CTX_FLAGS_SHADERS_HLSL;
2019-03-13 19:39:36 +00:00
break;
case RARCH_SHADER_NONE:
default:
return false;
}
video_context_driver_get_flags(&flags);
return BIT32_GET(flags.flags, testflag);
}
const char *video_shader_get_preset_extension(enum rarch_shader_type type)
{
switch (type)
{
case RARCH_SHADER_GLSL:
return file_path_str(FILE_PATH_GLSLP_EXTENSION);
case RARCH_SHADER_SLANG:
return file_path_str(FILE_PATH_SLANGP_EXTENSION);
case RARCH_SHADER_HLSL:
case RARCH_SHADER_CG:
return file_path_str(FILE_PATH_CGP_EXTENSION);
default:
break;
}
return NULL;
2018-02-25 16:20:22 +00:00
}
2018-02-25 22:03:39 +00:00
bool video_shader_any_supported(void)
{
return
video_shader_is_supported(RARCH_SHADER_SLANG) ||
video_shader_is_supported(RARCH_SHADER_HLSL) ||
video_shader_is_supported(RARCH_SHADER_GLSL) ||
video_shader_is_supported(RARCH_SHADER_CG);
2018-02-25 22:03:39 +00:00
}
enum rarch_shader_type video_shader_get_type_from_ext(const char *ext,
bool *is_preset)
2018-02-25 16:20:22 +00:00
{
if (string_is_empty(ext))
return RARCH_SHADER_NONE;
if (strlen(ext) > 1 && ext[0] == '.')
ext++;
*is_preset =
string_is_equal_case_insensitive(ext, "cgp") ||
string_is_equal_case_insensitive(ext, "glslp") ||
string_is_equal_case_insensitive(ext, "slangp");
if (string_is_equal_case_insensitive(ext, "cgp") ||
string_is_equal_case_insensitive(ext, "cg")
)
return RARCH_SHADER_CG;
if (string_is_equal_case_insensitive(ext, "glslp") ||
string_is_equal_case_insensitive(ext, "glsl")
)
return RARCH_SHADER_GLSL;
if (string_is_equal_case_insensitive(ext, "slangp") ||
string_is_equal_case_insensitive(ext, "slang")
)
return RARCH_SHADER_SLANG;
2018-02-25 16:20:22 +00:00
return RARCH_SHADER_NONE;
}
/**
* video_shader_parse_type:
* @path : Shader path.
*
* Parses type of shader.
*
* Returns: value of shader type if it could be determined,
* otherwise RARCH_SHADER_NONE.
**/
enum rarch_shader_type video_shader_parse_type(const char *path)
{
bool is_preset = false;
if (!path)
return RARCH_SHADER_NONE;
return video_shader_get_type_from_ext(path_get_extension(path), &is_preset);
}
bool video_shader_check_for_changes(void)
{
if (!file_change_data)
return false;
return frontend_driver_check_for_path_changes(file_change_data);
}