2014-05-08 17:39:12 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2016-01-10 03:41:52 +00:00
|
|
|
* Copyright (C) 2011-2016 - Daniel De Matteis
|
2014-05-08 17:39:12 +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/>.
|
|
|
|
*/
|
|
|
|
|
2014-10-02 11:42:40 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-01-20 03:14:55 +00:00
|
|
|
#include <string/stdstring.h>
|
|
|
|
|
2015-06-29 22:38:10 +00:00
|
|
|
#include "video_shader_driver.h"
|
2015-11-23 11:03:38 +00:00
|
|
|
#include "../verbosity.h"
|
2015-06-29 22:38:10 +00:00
|
|
|
|
2014-10-02 11:42:40 +00:00
|
|
|
static const shader_backend_t *shader_ctx_drivers[] = {
|
|
|
|
#ifdef HAVE_GLSL
|
|
|
|
&gl_glsl_backend,
|
|
|
|
#endif
|
2015-02-09 20:49:29 +00:00
|
|
|
#ifdef HAVE_CG
|
|
|
|
&gl_cg_backend,
|
|
|
|
#endif
|
2014-10-02 11:42:40 +00:00
|
|
|
#ifdef HAVE_HLSL
|
|
|
|
&hlsl_backend,
|
|
|
|
#endif
|
|
|
|
&shader_null_backend,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2016-04-16 03:20:41 +00:00
|
|
|
static const shader_backend_t *video_shader_set_backend(enum rarch_shader_type type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case RARCH_SHADER_CG:
|
|
|
|
#ifdef HAVE_CG
|
|
|
|
RARCH_LOG("[Shader driver]: Using Cg shader backend.\n");
|
|
|
|
return &gl_cg_backend;
|
|
|
|
#else
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case RARCH_SHADER_GLSL:
|
|
|
|
#ifdef HAVE_GLSL
|
|
|
|
RARCH_LOG("[Shader driver]: Using GLSL shader backend.\n");
|
|
|
|
return &gl_glsl_backend;
|
|
|
|
#else
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case RARCH_SHADER_NONE:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-02-14 17:12:42 +00:00
|
|
|
bool video_shader_driver_ctl(enum video_shader_driver_ctl_state state, void *data)
|
|
|
|
{
|
2016-02-14 21:22:40 +00:00
|
|
|
static const shader_backend_t *current_shader = NULL;
|
|
|
|
static void *shader_data = NULL;
|
|
|
|
|
2016-02-14 17:12:42 +00:00
|
|
|
switch (state)
|
|
|
|
{
|
2016-02-14 21:22:40 +00:00
|
|
|
case SHADER_CTL_GET_PREV_TEXTURES:
|
|
|
|
{
|
|
|
|
video_shader_ctx_texture_t *texture = (video_shader_ctx_texture_t*)data;
|
2016-02-14 21:23:30 +00:00
|
|
|
if (!!texture || !current_shader)
|
2016-02-14 21:22:40 +00:00
|
|
|
{
|
|
|
|
texture->id = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
texture->id = current_shader->get_prev_textures(shader_data);
|
|
|
|
}
|
|
|
|
break;
|
2016-02-14 21:17:00 +00:00
|
|
|
case SHADER_CTL_GET_IDENT:
|
|
|
|
{
|
|
|
|
video_shader_ctx_ident_t *ident = (video_shader_ctx_ident_t*)data;
|
|
|
|
if (!current_shader || !ident)
|
|
|
|
return false;
|
|
|
|
ident->ident = current_shader->ident;
|
|
|
|
}
|
|
|
|
break;
|
2016-02-14 21:13:21 +00:00
|
|
|
case SHADER_CTL_GET_CURRENT_SHADER:
|
|
|
|
{
|
|
|
|
video_shader_ctx_t *shader = (video_shader_ctx_t*)data;
|
|
|
|
void *video_driver = video_driver_get_ptr(false);
|
|
|
|
const video_poke_interface_t *video_poke = video_driver_get_poke();
|
|
|
|
|
|
|
|
shader->data = NULL;
|
|
|
|
if (!video_poke || !video_driver)
|
|
|
|
return false;
|
|
|
|
if (!video_poke->get_current_shader)
|
|
|
|
return false;
|
|
|
|
shader->data = video_poke->get_current_shader(video_driver);
|
|
|
|
}
|
|
|
|
break;
|
2016-02-14 20:55:19 +00:00
|
|
|
case SHADER_CTL_DIRECT_GET_CURRENT_SHADER:
|
|
|
|
{
|
|
|
|
video_shader_ctx_t *shader = (video_shader_ctx_t*)data;
|
|
|
|
|
|
|
|
shader->data = NULL;
|
|
|
|
if (!current_shader || !current_shader->get_current_shader)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
shader->data = current_shader->get_current_shader(shader_data);
|
|
|
|
}
|
|
|
|
break;
|
2016-02-14 17:22:38 +00:00
|
|
|
case SHADER_CTL_DEINIT:
|
|
|
|
if (!current_shader)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (current_shader->deinit)
|
|
|
|
current_shader->deinit(shader_data);
|
|
|
|
|
|
|
|
shader_data = NULL;
|
|
|
|
current_shader = NULL;
|
|
|
|
break;
|
2016-02-14 17:14:51 +00:00
|
|
|
case SHADER_CTL_SET_PARAMS:
|
|
|
|
{
|
2016-02-14 18:51:32 +00:00
|
|
|
video_shader_ctx_params_t *params =
|
|
|
|
(video_shader_ctx_params_t*)data;
|
2016-02-14 17:14:51 +00:00
|
|
|
|
|
|
|
if (!current_shader || !current_shader->set_params)
|
|
|
|
return false;
|
|
|
|
current_shader->set_params(
|
|
|
|
params->data,
|
|
|
|
shader_data,
|
|
|
|
params->width,
|
|
|
|
params->height,
|
|
|
|
params->tex_width,
|
|
|
|
params->tex_height,
|
|
|
|
params->out_width,
|
|
|
|
params->out_height,
|
|
|
|
params->frame_counter,
|
|
|
|
params->info,
|
|
|
|
params->prev_info,
|
|
|
|
params->feedback_info,
|
|
|
|
params->fbo_info,
|
|
|
|
params->fbo_info_cnt);
|
|
|
|
}
|
|
|
|
break;
|
2016-02-14 17:17:43 +00:00
|
|
|
/* Finds first suitable shader context driver. */
|
|
|
|
case SHADER_CTL_INIT_FIRST:
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; shader_ctx_drivers[i]; i++)
|
|
|
|
{
|
|
|
|
current_shader = shader_ctx_drivers[i];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2016-02-14 17:41:45 +00:00
|
|
|
case SHADER_CTL_INIT:
|
|
|
|
{
|
|
|
|
video_shader_ctx_init_t *init = (video_shader_ctx_init_t*)data;
|
|
|
|
void *tmp = NULL;
|
|
|
|
|
|
|
|
if (!init->shader || !init->shader->init)
|
2016-04-16 03:14:39 +00:00
|
|
|
{
|
2016-04-16 03:20:41 +00:00
|
|
|
init->shader = video_shader_set_backend(init->shader_type);
|
|
|
|
|
|
|
|
if (!init->shader)
|
|
|
|
return false;
|
2016-04-16 03:14:39 +00:00
|
|
|
}
|
2016-02-14 17:41:45 +00:00
|
|
|
|
|
|
|
tmp = init->shader->init(init->data, init->path);
|
|
|
|
|
|
|
|
if (!tmp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
shader_data = tmp;
|
|
|
|
current_shader = init->shader;
|
|
|
|
}
|
|
|
|
break;
|
2016-02-14 17:19:48 +00:00
|
|
|
case SHADER_CTL_GET_FEEDBACK_PASS:
|
|
|
|
if (!current_shader || !current_shader->get_feedback_pass)
|
|
|
|
return false;
|
2016-03-20 16:41:28 +00:00
|
|
|
if (!current_shader->get_feedback_pass(shader_data,
|
|
|
|
(unsigned*)data))
|
|
|
|
return false;
|
|
|
|
break;
|
2016-02-14 17:48:17 +00:00
|
|
|
case SHADER_CTL_MIPMAP_INPUT:
|
|
|
|
if (!current_shader)
|
|
|
|
return false;
|
|
|
|
{
|
|
|
|
unsigned *index = (unsigned*)data;
|
2016-03-20 16:41:28 +00:00
|
|
|
if (!current_shader->mipmap_input(shader_data, *index))
|
|
|
|
return false;
|
2016-02-14 17:48:17 +00:00
|
|
|
}
|
2016-03-20 16:41:28 +00:00
|
|
|
break;
|
2016-02-14 17:59:42 +00:00
|
|
|
case SHADER_CTL_SET_COORDS:
|
|
|
|
{
|
|
|
|
video_shader_ctx_coords_t *coords = (video_shader_ctx_coords_t*)
|
|
|
|
data;
|
|
|
|
if (!current_shader || !current_shader->set_coords)
|
|
|
|
return false;
|
2016-03-20 16:41:28 +00:00
|
|
|
if (!current_shader->set_coords(coords->handle_data,
|
|
|
|
shader_data, coords->data))
|
|
|
|
return false;
|
2016-02-14 17:59:42 +00:00
|
|
|
}
|
2016-03-20 16:41:28 +00:00
|
|
|
break;
|
2016-02-14 18:21:54 +00:00
|
|
|
case SHADER_CTL_SCALE:
|
|
|
|
{
|
|
|
|
video_shader_ctx_scale_t *scaler = (video_shader_ctx_scale_t*)data;
|
|
|
|
if (!scaler || !scaler->scale)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
scaler->scale->valid = false;
|
|
|
|
|
|
|
|
if (!current_shader || !current_shader->shader_scale)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
current_shader->shader_scale(shader_data, scaler->idx, scaler->scale);
|
|
|
|
}
|
|
|
|
break;
|
2016-02-14 18:30:48 +00:00
|
|
|
case SHADER_CTL_INFO:
|
|
|
|
{
|
2016-02-14 18:51:32 +00:00
|
|
|
video_shader_ctx_info_t *shader_info =
|
|
|
|
(video_shader_ctx_info_t*)data;
|
2016-02-14 18:30:48 +00:00
|
|
|
if (!shader_info || !current_shader)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
shader_info->num = 0;
|
|
|
|
if (current_shader->num_shaders)
|
|
|
|
shader_info->num = current_shader->num_shaders(shader_data);
|
|
|
|
}
|
|
|
|
break;
|
2016-02-14 18:43:47 +00:00
|
|
|
case SHADER_CTL_SET_MVP:
|
|
|
|
{
|
|
|
|
video_shader_ctx_mvp_t *mvp = (video_shader_ctx_mvp_t*)data;
|
|
|
|
if (!current_shader || !current_shader->set_mvp)
|
|
|
|
return false;
|
|
|
|
if (!mvp || !mvp->matrix)
|
|
|
|
return false;
|
2016-03-20 16:41:28 +00:00
|
|
|
if (!current_shader->set_mvp(mvp->data, shader_data, mvp->matrix))
|
|
|
|
return false;
|
2016-02-14 18:43:47 +00:00
|
|
|
}
|
2016-03-20 16:41:28 +00:00
|
|
|
break;
|
2016-02-14 18:51:32 +00:00
|
|
|
case SHADER_CTL_FILTER_TYPE:
|
|
|
|
{
|
|
|
|
video_shader_ctx_filter_t *filter =
|
|
|
|
(video_shader_ctx_filter_t*)data;
|
|
|
|
if (!current_shader || !current_shader->filter_type || !filter)
|
|
|
|
return false;
|
2016-03-20 16:41:28 +00:00
|
|
|
if (!current_shader->filter_type(shader_data,
|
|
|
|
filter->index, filter->smooth))
|
|
|
|
return false;
|
2016-02-14 18:51:32 +00:00
|
|
|
}
|
2016-03-20 16:41:28 +00:00
|
|
|
break;
|
2016-04-16 02:56:15 +00:00
|
|
|
case SHADER_CTL_COMPILE_PROGRAM:
|
|
|
|
{
|
|
|
|
struct shader_program_info *program_info = (struct shader_program_info*)data;
|
|
|
|
if (!current_shader || !program_info)
|
|
|
|
return false;
|
|
|
|
return current_shader->compile_program(program_info->data,
|
|
|
|
program_info->idx, NULL, program_info);
|
|
|
|
}
|
|
|
|
break;
|
2016-02-14 19:01:39 +00:00
|
|
|
case SHADER_CTL_USE:
|
|
|
|
{
|
|
|
|
video_shader_ctx_info_t *shader_info = (video_shader_ctx_info_t*)data;
|
|
|
|
if (!current_shader || !shader_info)
|
|
|
|
return false;
|
2016-04-15 05:37:06 +00:00
|
|
|
current_shader->use(shader_info->data, shader_data, shader_info->idx, shader_info->set_active);
|
2016-02-14 19:01:39 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-02-14 20:41:16 +00:00
|
|
|
case SHADER_CTL_WRAP_TYPE:
|
|
|
|
{
|
|
|
|
video_shader_ctx_wrap_t *wrap = (video_shader_ctx_wrap_t*)data;
|
|
|
|
if (!current_shader || !current_shader->wrap_type)
|
|
|
|
return false;
|
|
|
|
wrap->type = current_shader->wrap_type(shader_data, wrap->idx);
|
|
|
|
}
|
|
|
|
break;
|
2016-02-14 17:12:42 +00:00
|
|
|
case SHADER_CTL_NONE:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|