0
0
mirror of https://github.com/obsproject/obs-studio.git synced 2024-09-20 13:08:50 +02:00
obs-studio/libobs/graphics/effect.c

350 lines
7.9 KiB
C
Raw Normal View History

2013-10-01 04:37:13 +02:00
/******************************************************************************
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
This program 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 Foundation, either version 2 of the License, or
2013-10-01 04:37:13 +02:00
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "effect.h"
#include "graphics-internal.h"
#include "vec2.h"
#include "vec3.h"
#include "vec4.h"
void effect_destroy(effect_t effect)
{
if (effect) {
effect_free(effect);
bfree(effect);
}
}
technique_t effect_gettechnique(effect_t effect, const char *name)
{
2014-02-24 06:39:33 +01:00
if (!effect) return NULL;
2013-10-01 04:37:13 +02:00
2014-02-24 06:39:33 +01:00
for (size_t i = 0; i < effect->techniques.num; i++) {
struct effect_technique *tech = effect->techniques.array+i;
2013-10-01 04:37:13 +02:00
if (strcmp(tech->name, name) == 0)
return tech;
}
return NULL;
}
2013-10-18 02:21:42 +02:00
size_t technique_begin(technique_t tech)
2013-10-01 04:37:13 +02:00
{
2014-02-24 06:39:33 +01:00
if (!tech) return 0;
2013-10-01 04:37:13 +02:00
tech->effect->cur_technique = tech;
tech->effect->graphics->cur_effect = tech->effect;
2013-10-18 02:21:42 +02:00
return tech->passes.num;
2013-10-01 04:37:13 +02:00
}
void technique_end(technique_t tech)
{
2014-02-24 06:39:33 +01:00
if (!tech) return;
2013-10-01 04:37:13 +02:00
struct gs_effect *effect = tech->effect;
struct effect_param *params = effect->params.array;
size_t i;
gs_load_vertexshader(NULL);
gs_load_pixelshader(NULL);
tech->effect->cur_technique = NULL;
tech->effect->graphics->cur_effect = NULL;
for (i = 0; i < effect->params.num; i++) {
struct effect_param *param = params+i;
da_free(param->cur_val);
param->changed = false;
}
}
static inline void reset_params(struct darray *shaderparams)
{
struct pass_shaderparam *params = shaderparams->array;
size_t i;
for (i = 0; i < shaderparams->num; i++)
params[i].eparam->changed = false;
}
static void upload_shader_params(struct darray *pass_params, bool changed_only)
2013-10-01 04:37:13 +02:00
{
struct pass_shaderparam *params = pass_params->array;
size_t i;
for (i = 0; i < pass_params->num; i++) {
struct pass_shaderparam *param = params+i;
struct effect_param *eparam = param->eparam;
sparam_t sparam = param->sparam;
if (changed_only && !eparam->changed)
continue;
if (!eparam->cur_val.num) {
if (eparam->default_val.num)
da_copy(eparam->cur_val, eparam->default_val);
else
continue;
}
shader_setval(sparam, eparam->cur_val.array,
2013-10-01 04:37:13 +02:00
eparam->cur_val.num);
}
}
static inline void upload_parameters(struct gs_effect *effect,
bool changed_only)
{
struct darray *vshader_params, *pshader_params;
if (!effect->cur_pass)
return;
vshader_params = &effect->cur_pass->vertshader_params.da;
pshader_params = &effect->cur_pass->pixelshader_params.da;
upload_shader_params(vshader_params, changed_only);
upload_shader_params(pshader_params, changed_only);
2013-10-01 04:37:13 +02:00
reset_params(vshader_params);
reset_params(pshader_params);
}
void effect_updateparams(effect_t effect)
{
2014-02-24 06:39:33 +01:00
if (effect)
upload_parameters(effect, true);
2013-10-01 04:37:13 +02:00
}
bool technique_beginpass(technique_t tech, size_t idx)
{
struct effect_pass *passes;
struct effect_pass *cur_pass;
2014-02-24 06:39:33 +01:00
if (!tech || idx >= tech->passes.num)
2013-10-01 04:37:13 +02:00
return false;
passes = tech->passes.array;
cur_pass = passes+idx;
tech->effect->cur_pass = cur_pass;
gs_load_vertexshader(cur_pass->vertshader);
gs_load_pixelshader(cur_pass->pixelshader);
upload_parameters(tech->effect, false);
return true;
}
bool technique_beginpassbyname(technique_t tech,
const char *name)
{
2014-02-24 06:39:33 +01:00
if (!tech)
return false;
for (size_t i = 0; i < tech->passes.num; i++) {
2013-10-01 04:37:13 +02:00
struct effect_pass *pass = tech->passes.array+i;
if (strcmp(pass->name, name) == 0) {
2013-10-18 02:21:42 +02:00
technique_beginpass(tech, i);
2013-10-01 04:37:13 +02:00
return true;
}
}
return false;
}
static inline void clear_tex_params(struct darray *in_params)
2013-10-01 04:37:13 +02:00
{
struct pass_shaderparam *params = in_params->array;
2014-02-24 06:39:33 +01:00
for (size_t i = 0; i < in_params->num; i++) {
2013-10-01 04:37:13 +02:00
struct pass_shaderparam *param = params+i;
struct shader_param_info info;
shader_getparaminfo(param->sparam, &info);
2013-10-01 04:37:13 +02:00
if (info.type == SHADER_PARAM_TEXTURE)
shader_settexture(param->sparam, NULL);
2013-10-01 04:37:13 +02:00
}
}
void technique_endpass(technique_t tech)
{
2014-02-24 06:39:33 +01:00
if (!tech) return;
2013-10-01 04:37:13 +02:00
struct effect_pass *pass = tech->effect->cur_pass;
if (!pass)
return;
clear_tex_params(&pass->vertshader_params.da);
clear_tex_params(&pass->pixelshader_params.da);
2013-10-01 04:37:13 +02:00
tech->effect->cur_pass = NULL;
}
2013-10-18 02:21:42 +02:00
size_t effect_numparams(effect_t effect)
2013-10-01 04:37:13 +02:00
{
2014-02-24 06:39:33 +01:00
return effect ? effect->params.num : 0;
2013-10-01 04:37:13 +02:00
}
eparam_t effect_getparambyidx(effect_t effect, size_t param)
{
2014-02-24 06:39:33 +01:00
if (!effect) return NULL;
2013-10-01 04:37:13 +02:00
2014-02-24 06:39:33 +01:00
struct effect_param *params = effect->params.array;
2013-10-01 04:37:13 +02:00
if (param >= effect->params.num)
return NULL;
return params+param;
}
eparam_t effect_getparambyname(effect_t effect, const char *name)
{
2014-02-24 06:39:33 +01:00
if (!effect) return NULL;
2013-10-01 04:37:13 +02:00
struct effect_param *params = effect->params.array;
2014-02-24 06:39:33 +01:00
for (size_t i = 0; i < effect->params.num; i++) {
2013-10-01 04:37:13 +02:00
struct effect_param *param = params+i;
if (strcmp(param->name, name) == 0)
return param;
}
return NULL;
}
2013-10-18 02:21:42 +02:00
static inline bool matching_effect(effect_t effect, eparam_t param)
{
if (effect != param->effect) {
blog(LOG_ERROR, "Effect and effect parameter do not match");
return false;
}
return true;
}
2013-10-01 04:37:13 +02:00
void effect_getparaminfo(effect_t effect, eparam_t param,
struct effect_param_info *info)
{
2014-02-24 06:39:33 +01:00
if (!effect || !param)
return;
2013-10-18 02:21:42 +02:00
if (!matching_effect(effect, param))
return;
2013-10-01 04:37:13 +02:00
info->name = param->name;
info->type = param->type;
}
eparam_t effect_getviewprojmatrix(effect_t effect)
{
2014-02-24 06:39:33 +01:00
return effect ? effect->view_proj : NULL;
2013-10-01 04:37:13 +02:00
}
eparam_t effect_getworldmatrix(effect_t effect)
{
2014-02-24 06:39:33 +01:00
return effect ? effect->world : NULL;
2013-10-01 04:37:13 +02:00
}
static inline void effect_setval_inline(effect_t effect, eparam_t param,
const void *data, size_t size)
{
Revamp API and start using doxygen The API used to be designed in such a way to where it would expect exports for each individual source/output/encoder/etc. You would export functions for each and it would automatically load those functions based on a specific naming scheme from the module. The idea behind this was that I wanted to limit the usage of structures in the API so only functions could be used. It was an interesting idea in theory, but this idea turned out to be flawed in a number of ways: 1.) Requiring exports to create sources/outputs/encoders/etc meant that you could not create them by any other means, which meant that things like faruton's .net plugin would become difficult. 2.) Export function declarations could not be checked, therefore if you created a function with the wrong parameters and parameter types, the compiler wouldn't know how to check for that. 3.) Required overly complex load functions in libobs just to handle it. It makes much more sense to just have a load function that you call manually. Complexity is the bane of all good programs. 4.) It required that you have functions of specific names, which looked and felt somewhat unsightly. So, to fix these issues, I replaced it with a more commonly used API scheme, seen commonly in places like kernels and typical C libraries with abstraction. You simply create a structure that contains the callback definitions, and you pass it to a function to register that definition (such as obs_register_source), which you call in the obs_module_load of the module. It will also automatically check the structure size and ensure that it only loads the required values if the structure happened to add new values in an API change. The "main" source file for each module must include obs-module.h, and must use OBS_DECLARE_MODULE() within that source file. Also, started writing some doxygen documentation in to the main library headers. Will add more detailed documentation as I go.
2014-02-12 16:04:50 +01:00
bool size_changed;
2013-10-18 02:21:42 +02:00
Revamp API and start using doxygen The API used to be designed in such a way to where it would expect exports for each individual source/output/encoder/etc. You would export functions for each and it would automatically load those functions based on a specific naming scheme from the module. The idea behind this was that I wanted to limit the usage of structures in the API so only functions could be used. It was an interesting idea in theory, but this idea turned out to be flawed in a number of ways: 1.) Requiring exports to create sources/outputs/encoders/etc meant that you could not create them by any other means, which meant that things like faruton's .net plugin would become difficult. 2.) Export function declarations could not be checked, therefore if you created a function with the wrong parameters and parameter types, the compiler wouldn't know how to check for that. 3.) Required overly complex load functions in libobs just to handle it. It makes much more sense to just have a load function that you call manually. Complexity is the bane of all good programs. 4.) It required that you have functions of specific names, which looked and felt somewhat unsightly. So, to fix these issues, I replaced it with a more commonly used API scheme, seen commonly in places like kernels and typical C libraries with abstraction. You simply create a structure that contains the callback definitions, and you pass it to a function to register that definition (such as obs_register_source), which you call in the obs_module_load of the module. It will also automatically check the structure size and ensure that it only loads the required values if the structure happened to add new values in an API change. The "main" source file for each module must include obs-module.h, and must use OBS_DECLARE_MODULE() within that source file. Also, started writing some doxygen documentation in to the main library headers. Will add more detailed documentation as I go.
2014-02-12 16:04:50 +01:00
if (!effect) {
blog(LOG_ERROR, "effect_setval_inline: invalid effect");
Revamp API and start using doxygen The API used to be designed in such a way to where it would expect exports for each individual source/output/encoder/etc. You would export functions for each and it would automatically load those functions based on a specific naming scheme from the module. The idea behind this was that I wanted to limit the usage of structures in the API so only functions could be used. It was an interesting idea in theory, but this idea turned out to be flawed in a number of ways: 1.) Requiring exports to create sources/outputs/encoders/etc meant that you could not create them by any other means, which meant that things like faruton's .net plugin would become difficult. 2.) Export function declarations could not be checked, therefore if you created a function with the wrong parameters and parameter types, the compiler wouldn't know how to check for that. 3.) Required overly complex load functions in libobs just to handle it. It makes much more sense to just have a load function that you call manually. Complexity is the bane of all good programs. 4.) It required that you have functions of specific names, which looked and felt somewhat unsightly. So, to fix these issues, I replaced it with a more commonly used API scheme, seen commonly in places like kernels and typical C libraries with abstraction. You simply create a structure that contains the callback definitions, and you pass it to a function to register that definition (such as obs_register_source), which you call in the obs_module_load of the module. It will also automatically check the structure size and ensure that it only loads the required values if the structure happened to add new values in an API change. The "main" source file for each module must include obs-module.h, and must use OBS_DECLARE_MODULE() within that source file. Also, started writing some doxygen documentation in to the main library headers. Will add more detailed documentation as I go.
2014-02-12 16:04:50 +01:00
return;
}
if (!param) {
blog(LOG_ERROR, "effect_setval_inline: invalid param");
Revamp API and start using doxygen The API used to be designed in such a way to where it would expect exports for each individual source/output/encoder/etc. You would export functions for each and it would automatically load those functions based on a specific naming scheme from the module. The idea behind this was that I wanted to limit the usage of structures in the API so only functions could be used. It was an interesting idea in theory, but this idea turned out to be flawed in a number of ways: 1.) Requiring exports to create sources/outputs/encoders/etc meant that you could not create them by any other means, which meant that things like faruton's .net plugin would become difficult. 2.) Export function declarations could not be checked, therefore if you created a function with the wrong parameters and parameter types, the compiler wouldn't know how to check for that. 3.) Required overly complex load functions in libobs just to handle it. It makes much more sense to just have a load function that you call manually. Complexity is the bane of all good programs. 4.) It required that you have functions of specific names, which looked and felt somewhat unsightly. So, to fix these issues, I replaced it with a more commonly used API scheme, seen commonly in places like kernels and typical C libraries with abstraction. You simply create a structure that contains the callback definitions, and you pass it to a function to register that definition (such as obs_register_source), which you call in the obs_module_load of the module. It will also automatically check the structure size and ensure that it only loads the required values if the structure happened to add new values in an API change. The "main" source file for each module must include obs-module.h, and must use OBS_DECLARE_MODULE() within that source file. Also, started writing some doxygen documentation in to the main library headers. Will add more detailed documentation as I go.
2014-02-12 16:04:50 +01:00
return;
}
if (!data) {
blog(LOG_ERROR, "effect_setval_inline: invalid data");
Revamp API and start using doxygen The API used to be designed in such a way to where it would expect exports for each individual source/output/encoder/etc. You would export functions for each and it would automatically load those functions based on a specific naming scheme from the module. The idea behind this was that I wanted to limit the usage of structures in the API so only functions could be used. It was an interesting idea in theory, but this idea turned out to be flawed in a number of ways: 1.) Requiring exports to create sources/outputs/encoders/etc meant that you could not create them by any other means, which meant that things like faruton's .net plugin would become difficult. 2.) Export function declarations could not be checked, therefore if you created a function with the wrong parameters and parameter types, the compiler wouldn't know how to check for that. 3.) Required overly complex load functions in libobs just to handle it. It makes much more sense to just have a load function that you call manually. Complexity is the bane of all good programs. 4.) It required that you have functions of specific names, which looked and felt somewhat unsightly. So, to fix these issues, I replaced it with a more commonly used API scheme, seen commonly in places like kernels and typical C libraries with abstraction. You simply create a structure that contains the callback definitions, and you pass it to a function to register that definition (such as obs_register_source), which you call in the obs_module_load of the module. It will also automatically check the structure size and ensure that it only loads the required values if the structure happened to add new values in an API change. The "main" source file for each module must include obs-module.h, and must use OBS_DECLARE_MODULE() within that source file. Also, started writing some doxygen documentation in to the main library headers. Will add more detailed documentation as I go.
2014-02-12 16:04:50 +01:00
return;
}
size_changed = param->cur_val.num != size;
2013-10-18 02:21:42 +02:00
if (!matching_effect(effect, param))
return;
2013-10-01 04:37:13 +02:00
if (size_changed)
da_resize(param->cur_val, size);
if (size_changed || memcmp(param->cur_val.array, data, size) != 0) {
memcpy(param->cur_val.array, data, size);
param->changed = true;
}
}
void effect_setbool(effect_t effect, eparam_t param, bool val)
{
effect_setval_inline(effect, param, &val, sizeof(bool));
}
void effect_setfloat(effect_t effect, eparam_t param, float val)
{
effect_setval_inline(effect, param, &val, sizeof(float));
}
void effect_setint(effect_t effect, eparam_t param, int val)
{
effect_setval_inline(effect, param, &val, sizeof(int));
}
void effect_setmatrix4(effect_t effect, eparam_t param,
const struct matrix4 *val)
{
effect_setval_inline(effect, param, val, sizeof(struct matrix4));
}
void effect_setvec2(effect_t effect, eparam_t param,
const struct vec2 *val)
{
effect_setval_inline(effect, param, val, sizeof(struct vec2));
}
void effect_setvec3(effect_t effect, eparam_t param,
const struct vec3 *val)
{
effect_setval_inline(effect, param, val, sizeof(float) * 3);
}
void effect_setvec4(effect_t effect, eparam_t param,
const struct vec4 *val)
{
effect_setval_inline(effect, param, val, sizeof(struct vec4));
}
void effect_settexture(effect_t effect, eparam_t param, texture_t val)
{
effect_setval_inline(effect, param, &val, sizeof(texture_t));
}
void effect_setval(effect_t effect, eparam_t param, const void *val,
size_t size)
{
effect_setval_inline(effect, param, val, size);
}
void effect_setdefault(effect_t effect, eparam_t param)
{
effect_setval_inline(effect, param, param->default_val.array,
param->default_val.num);
}