RetroArch/menu/menu_shader.c

454 lines
13 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2016-01-10 03:06:50 +00:00
* Copyright (C) 2011-2016 - 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/>.
*/
2015-05-12 11:14:04 +00:00
#include <string.h>
#include <compat/strl.h>
#include <retro_assert.h>
2014-10-21 22:23:06 +00:00
#include <file/file_path.h>
2016-06-28 09:02:51 +00:00
#include <string/stdstring.h>
2016-09-08 03:43:10 +00:00
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
2015-12-06 16:55:27 +00:00
#include "menu_driver.h"
2015-05-12 11:14:04 +00:00
#include "menu_shader.h"
2016-07-01 09:16:56 +00:00
#include "../file_path_special.h"
2015-05-12 11:14:04 +00:00
#include "../configuration.h"
#include "../runloop.h"
2015-11-23 11:03:38 +00:00
#include "../verbosity.h"
2015-09-04 19:43:08 +00:00
#include "../gfx/video_shader_driver.h"
2016-09-05 15:30:12 +00:00
#ifdef HAVE_SHADER_MANAGER
/* Menu shader */
static char default_glslp[PATH_MAX_LENGTH];
static char default_cgp[PATH_MAX_LENGTH];
static char default_slangp[PATH_MAX_LENGTH];
2016-09-05 15:30:12 +00:00
#endif
2015-01-10 04:53:41 +00:00
/**
* menu_shader_manager_init:
*
* Initializes shader manager.
**/
void menu_shader_manager_init(void)
{
2015-01-10 04:53:41 +00:00
#ifdef HAVE_SHADER_MANAGER
2015-01-19 20:24:08 +00:00
struct video_shader *shader = NULL;
2015-03-20 19:43:22 +00:00
config_file_t *conf = NULL;
settings_t *settings = config_get_ptr();
2016-07-23 12:18:17 +00:00
const char *config_path = config_get_active_path();
2015-12-10 18:59:21 +00:00
menu_driver_ctl(RARCH_MENU_CTL_SHADER_GET,
&shader);
/* In a multi-config setting, we can't have
* conflicts on menu.cgp/menu.glslp. */
if (config_path)
{
fill_pathname_base_ext(default_glslp, config_path,
2016-07-01 09:16:56 +00:00
file_path_str(FILE_PATH_GLSLP_EXTENSION),
sizeof(default_glslp));
2016-02-16 19:24:00 +00:00
fill_pathname_base_ext(default_cgp, config_path,
2016-07-01 09:16:56 +00:00
file_path_str(FILE_PATH_CGP_EXTENSION),
sizeof(default_cgp));
2016-02-16 19:24:00 +00:00
fill_pathname_base_ext(default_slangp, config_path,
2016-07-01 09:16:56 +00:00
file_path_str(FILE_PATH_SLANGP_EXTENSION),
sizeof(default_slangp));
}
else
{
strlcpy(default_glslp, "menu.glslp",
sizeof(default_glslp));
strlcpy(default_cgp, "menu.cgp",
sizeof(default_cgp));
strlcpy(default_slangp, "menu.slangp",
sizeof(default_slangp));
}
2016-07-01 09:16:56 +00:00
switch (msg_hash_to_file_type(msg_hash_calculate(
path_get_extension(settings->path.shader))))
{
2016-06-20 13:50:37 +00:00
case FILE_TYPE_SHADER_PRESET_GLSLP:
case FILE_TYPE_SHADER_PRESET_CGP:
case FILE_TYPE_SHADER_PRESET_SLANGP:
2016-04-28 17:52:25 +00:00
conf = config_file_new(settings->path.shader);
if (conf)
{
if (video_shader_read_conf_cgp(conf, shader))
{
2016-02-14 16:44:00 +00:00
video_shader_resolve_relative(shader,
2016-04-28 17:52:25 +00:00
settings->path.shader);
video_shader_resolve_parameters(conf, shader);
}
config_file_free(conf);
}
break;
2016-06-20 13:50:37 +00:00
case FILE_TYPE_SHADER_GLSL:
case FILE_TYPE_SHADER_CG:
case FILE_TYPE_SHADER_SLANG:
2016-04-28 17:52:25 +00:00
strlcpy(shader->pass[0].source.path, settings->path.shader,
sizeof(shader->pass[0].source.path));
shader->passes = 1;
break;
default:
{
char preset_path[PATH_MAX_LENGTH] = {0};
const char *shader_dir =
*settings->directory.video_shader ?
2016-04-28 18:49:13 +00:00
settings->directory.video_shader : settings->directory.system;
2016-02-04 20:27:58 +00:00
fill_pathname_join(preset_path, shader_dir,
"menu.glslp", sizeof(preset_path));
conf = config_file_new(preset_path);
if (!conf)
{
2016-02-04 20:27:58 +00:00
fill_pathname_join(preset_path, shader_dir,
"menu.cgp", sizeof(preset_path));
conf = config_file_new(preset_path);
}
2016-02-16 19:24:00 +00:00
if (!conf)
{
fill_pathname_join(preset_path, shader_dir,
"menu.slangp", sizeof(preset_path));
2016-02-16 19:24:00 +00:00
conf = config_file_new(preset_path);
}
if (conf)
{
if (video_shader_read_conf_cgp(conf, shader))
{
video_shader_resolve_relative(shader, preset_path);
video_shader_resolve_parameters(conf, shader);
}
config_file_free(conf);
}
}
break;
}
2015-01-10 04:53:41 +00:00
#endif
}
2015-01-10 04:53:41 +00:00
/**
* menu_shader_manager_set_preset:
* @shader : Shader handle.
* @type : Type of shader.
* @preset_path : Preset path to load from.
*
* Sets shader preset.
**/
void menu_shader_manager_set_preset(void *data,
2015-01-10 04:53:41 +00:00
unsigned type, const char *preset_path)
{
2015-01-10 04:53:41 +00:00
#ifdef HAVE_SHADER_MANAGER
struct video_shader *shader = (struct video_shader*)data;
2015-03-20 19:43:22 +00:00
config_file_t *conf = NULL;
bool refresh = false;
2015-03-20 19:43:22 +00:00
settings_t *settings = config_get_ptr();
2015-01-11 17:30:56 +00:00
2015-03-20 19:43:22 +00:00
settings->video.shader_enable = false;
2015-03-22 21:17:52 +00:00
if (!video_driver_set_shader((enum rarch_shader_type)type, preset_path))
2015-01-11 17:30:56 +00:00
return;
/* Makes sure that we use Menu Preset shader on driver reinit.
* Only do this when the cgp actually works to avoid potential errors. */
2016-04-28 17:52:25 +00:00
strlcpy(settings->path.shader,
2016-02-14 16:44:00 +00:00
preset_path ? preset_path : "",
2016-04-28 17:52:25 +00:00
sizeof(settings->path.shader));
2015-03-20 19:43:22 +00:00
settings->video.shader_enable = true;
2015-01-11 17:30:56 +00:00
2016-02-14 16:44:00 +00:00
if (!preset_path || !shader)
2015-01-11 17:30:56 +00:00
return;
/* Load stored Preset into menu on success.
* Used when a preset is directly loaded.
* No point in updating when the Preset was
* created from the menu itself. */
conf = config_file_new(preset_path);
2015-01-11 17:32:16 +00:00
if (!conf)
return;
2016-05-24 20:12:41 +00:00
RARCH_LOG("Setting Menu shader: %s.\n", preset_path);
2015-01-11 17:32:16 +00:00
if (video_shader_read_conf_cgp(conf, shader))
2015-01-11 17:32:16 +00:00
{
video_shader_resolve_relative(shader, preset_path);
video_shader_resolve_parameters(conf, shader);
}
2015-01-11 17:32:16 +00:00
config_file_free(conf);
2015-01-11 17:30:56 +00:00
menu_entries_ctl(MENU_ENTRIES_CTL_SET_REFRESH, &refresh);
2015-01-10 04:53:41 +00:00
#endif
}
2015-01-10 04:53:41 +00:00
/**
* menu_shader_manager_save_preset:
* @basename : basename of preset
* @apply : immediately set preset after saving
*
* Save a shader preset to disk.
**/
bool menu_shader_manager_save_preset(
const char *basename, bool apply, bool fullpath)
{
2015-01-10 04:53:41 +00:00
#ifdef HAVE_SHADER_MANAGER
2016-06-03 02:18:28 +00:00
char buffer[PATH_MAX_LENGTH] = {0};
char config_directory[PATH_MAX_LENGTH] = {0};
char preset_path[PATH_MAX_LENGTH] = {0};
unsigned d, type = RARCH_SHADER_NONE;
const char *dirs[3] = {0};
config_file_t *conf = NULL;
bool ret = false;
struct video_shader *shader = NULL;
global_t *global = global_get_ptr();
settings_t *settings = config_get_ptr();
menu_handle_t *menu = NULL;
2016-02-10 20:15:23 +00:00
if (!menu_driver_ctl(RARCH_MENU_CTL_DRIVER_DATA_GET, &menu))
{
2016-02-04 20:27:58 +00:00
RARCH_ERR("Cannot save shader preset, menu handle"
" is not initialized.\n");
return false;
}
2015-12-10 18:59:21 +00:00
menu_driver_ctl(RARCH_MENU_CTL_SHADER_GET,
&shader);
if (!shader)
return false;
2015-12-10 18:59:21 +00:00
type = menu_shader_manager_get_type(shader);
if (type == RARCH_SHADER_NONE)
return false;
*config_directory = '\0';
if (basename)
{
strlcpy(buffer, basename, sizeof(buffer));
/* Append extension automatically as appropriate. */
2016-07-01 09:16:56 +00:00
if ( !strstr(basename, file_path_str(FILE_PATH_CGP_EXTENSION))
&& !strstr(basename, file_path_str(FILE_PATH_GLSLP_EXTENSION))
&& !strstr(basename, file_path_str(FILE_PATH_SLANGP_EXTENSION)))
{
2015-06-15 04:08:49 +00:00
switch (type)
{
case RARCH_SHADER_GLSL:
2016-07-01 09:16:56 +00:00
strlcat(buffer,
file_path_str(FILE_PATH_GLSLP_EXTENSION),
sizeof(buffer));
2015-06-15 04:08:49 +00:00
break;
2016-02-16 19:24:00 +00:00
case RARCH_SHADER_SLANG:
2016-07-01 09:16:56 +00:00
strlcat(buffer,
file_path_str(FILE_PATH_SLANGP_EXTENSION),
sizeof(buffer));
2016-02-16 19:24:00 +00:00
break;
2015-06-15 04:08:49 +00:00
case RARCH_SHADER_CG:
2016-07-01 09:16:56 +00:00
strlcat(buffer,
file_path_str(FILE_PATH_CGP_EXTENSION),
sizeof(buffer));
2015-06-15 04:08:49 +00:00
break;
}
}
}
else
{
2016-02-16 19:24:00 +00:00
const char *conf_path = NULL;
switch (type)
{
case RARCH_SHADER_GLSL:
conf_path = default_glslp;
2016-02-16 19:24:00 +00:00
break;
case RARCH_SHADER_SLANG:
conf_path = default_slangp;
2016-02-16 19:24:00 +00:00
break;
default:
case RARCH_SHADER_CG:
conf_path = default_cgp;
2016-02-16 19:24:00 +00:00
break;
}
strlcpy(buffer, conf_path, sizeof(buffer));
}
2016-06-28 09:02:51 +00:00
if (!string_is_empty(global->path.config))
2016-04-28 17:26:02 +00:00
fill_pathname_basedir(
config_directory,
global->path.config,
sizeof(config_directory));
if (!fullpath)
{
dirs[0] = settings->directory.video_shader;
dirs[1] = settings->directory.menu_config;
dirs[2] = config_directory;
}
if (!(conf = (config_file_t*)config_file_new(NULL)))
return false;
2015-12-10 18:59:21 +00:00
video_shader_write_conf_cgp(conf, shader);
if (!fullpath)
{
for (d = 0; d < ARRAY_SIZE(dirs); d++)
{
if (!*dirs[d])
continue;
fill_pathname_join(preset_path, dirs[d],
buffer, sizeof(preset_path));
2016-02-14 16:44:00 +00:00
if (config_file_write(conf, preset_path))
{
RARCH_LOG("Saved shader preset to %s.\n", preset_path);
if (apply)
menu_shader_manager_set_preset(NULL, type, preset_path);
ret = true;
break;
}
else
RARCH_LOG("Failed writing shader preset to %s.\n", preset_path);
}
}
else
{
if (!string_is_empty(basename))
strlcpy(preset_path, buffer, sizeof(preset_path));
2016-08-17 11:53:29 +00:00
if (config_file_write(conf, preset_path))
{
RARCH_LOG("Saved shader preset to %s.\n", preset_path);
if (apply)
menu_shader_manager_set_preset(NULL, type, preset_path);
ret = true;
}
else
RARCH_LOG("Failed writing shader preset to %s.\n", preset_path);
}
config_file_free(conf);
if (!ret)
{
2016-02-04 20:27:58 +00:00
RARCH_ERR("Failed to save shader preset. Make sure config directory"
" and/or shader dir are writable.\n");
return false;
}
else
return true;
2016-07-31 23:02:26 +00:00
#else
return false;
2015-01-10 04:53:41 +00:00
#endif
}
2015-01-10 04:53:41 +00:00
/**
* menu_shader_manager_get_type:
* @shader : shader handle
*
* Gets type of shader.
*
* Returns: type of shader.
**/
unsigned menu_shader_manager_get_type(const void *data)
{
2015-01-10 04:53:41 +00:00
#ifndef HAVE_SHADER_MANAGER
return RARCH_SHADER_NONE;
#else
2016-09-05 06:03:25 +00:00
const struct video_shader *shader = (const struct video_shader*)data;
/* All shader types must be the same, or we cannot use it. */
unsigned i = 0, type = 0;
if (!shader)
return RARCH_SHADER_NONE;
for (i = 0; i < shader->passes; i++)
{
enum rarch_shader_type pass_type =
video_shader_parse_type(shader->pass[i].source.path,
RARCH_SHADER_NONE);
switch (pass_type)
{
case RARCH_SHADER_CG:
case RARCH_SHADER_GLSL:
2016-02-16 19:24:00 +00:00
case RARCH_SHADER_SLANG:
if (type == RARCH_SHADER_NONE)
type = pass_type;
else if (type != pass_type)
return RARCH_SHADER_NONE;
break;
default:
return RARCH_SHADER_NONE;
}
}
return type;
2015-01-10 04:53:41 +00:00
#endif
}
2015-01-10 04:53:41 +00:00
/**
* menu_shader_manager_apply_changes:
*
* Apply shader state changes.
**/
void menu_shader_manager_apply_changes(void)
{
2015-01-10 04:53:41 +00:00
#ifdef HAVE_SHADER_MANAGER
2015-12-10 18:59:21 +00:00
unsigned shader_type;
struct video_shader *shader = NULL;
menu_driver_ctl(RARCH_MENU_CTL_SHADER_GET,
&shader);
if (!shader)
return;
shader_type = menu_shader_manager_get_type(shader);
2015-12-10 18:59:21 +00:00
if (shader->passes && shader_type != RARCH_SHADER_NONE)
{
menu_shader_manager_save_preset(NULL, true, false);
return;
}
/* Fall-back */
#if defined(HAVE_CG) || defined(HAVE_HLSL) || defined(HAVE_GLSL)
shader_type = video_shader_parse_type("", DEFAULT_SHADER_TYPE);
#endif
if (shader_type == RARCH_SHADER_NONE)
{
#if defined(HAVE_GLSL)
shader_type = RARCH_SHADER_GLSL;
#elif defined(HAVE_CG) || defined(HAVE_HLSL)
shader_type = RARCH_SHADER_CG;
2016-02-16 19:24:00 +00:00
#elif defined(HAVE_VULKAN)
shader_type = RARCH_SHADER_SLANG;
#endif
}
menu_shader_manager_set_preset(NULL, shader_type, NULL);
#endif
2015-01-10 04:53:41 +00:00
}