obs-StreamFX/source/gfx/shader/gfx-shader-param.cpp

207 lines
6.1 KiB
C++
Raw Normal View History

2019-12-18 05:37:38 +00:00
// Modern effects for a modern Streamer
// Copyright (C) 2019 Michael Fabian Dirks
//
// 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
// (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, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#include "gfx-shader-param.hpp"
#include <algorithm>
#include <sstream>
2019-12-18 05:37:38 +00:00
#include "gfx-shader-param-basic.hpp"
2021-06-08 02:47:04 +00:00
#define ST_ANNO_ORDER "order"
#define ST_ANNO_VISIBILITY "visible"
#define ST_ANNO_AUTOMATIC "automatic"
#define ST_ANNO_NAME "name"
#define ST_ANNO_DESCRIPTION "description"
#define ST_ANNO_TYPE "type"
#define ST_ANNO_SIZE "size"
2021-06-08 02:38:24 +00:00
typedef streamfx::obs::gs::effect_parameter::type eptype;
2021-06-08 02:47:04 +00:00
streamfx::gfx::shader::parameter_type
streamfx::gfx::shader::get_type_from_effect_type(streamfx::obs::gs::effect_parameter::type type)
{
switch (type) {
case eptype::Boolean:
return parameter_type::Boolean;
case eptype::Integer:
case eptype::Integer2:
case eptype::Integer3:
case eptype::Integer4:
return parameter_type::Integer;
case eptype::Float:
case eptype::Float2:
case eptype::Float3:
case eptype::Float4:
case eptype::Matrix:
return parameter_type::Float;
case eptype::String:
return parameter_type::String;
case eptype::Texture:
return parameter_type::Texture;
default:
return parameter_type::Unknown;
}
}
2021-06-08 02:47:04 +00:00
std::size_t streamfx::gfx::shader::get_length_from_effect_type(streamfx::obs::gs::effect_parameter::type type)
{
switch (type) {
default:
case eptype::Unknown:
case eptype::Invalid:
case eptype::String:
return 0;
case eptype::Boolean:
case eptype::Float:
case eptype::Integer:
case eptype::Texture:
return 1;
case eptype::Float2:
case eptype::Integer2:
return 2;
case eptype::Float3:
case eptype::Integer3:
return 3;
case eptype::Float4:
case eptype::Integer4:
return 4;
case eptype::Matrix:
return 16;
}
}
2021-06-08 02:47:04 +00:00
streamfx::gfx::shader::parameter_type streamfx::gfx::shader::get_type_from_string(std::string v)
{
if ((v == "bool") || (v == "boolean")) {
return parameter_type::Boolean;
}
if ((v == "float") || (v == "single")) {
return parameter_type::Float;
}
if ((v == "int") || (v == "integer")) {
return parameter_type::Integer;
}
if ((v == "text") || (v == "string")) {
return parameter_type::String;
}
if ((v == "tex") || (v == "texture")) {
return parameter_type::Texture;
}
if ((v == "sampler")) {
return parameter_type::Sampler;
}
/* To decide on in the future:
* - Double support?
* - Half Support?
* - Texture Arrays? (Likely not supported in libobs)
*/
throw std::invalid_argument("Invalid parameter type string.");
}
2021-06-08 02:47:04 +00:00
streamfx::gfx::shader::parameter::parameter(streamfx::obs::gs::effect_parameter param, std::string key_prefix)
: _param(param), _order(0), _key(_param.get_name()), _visible(true), _automatic(false), _name(_key), _description()
{
{
std::stringstream ss;
ss << key_prefix << "." << param.get_name();
_name = (_key);
}
// Read Order
2021-06-08 02:47:04 +00:00
if (auto anno = _param.get_annotation(ST_ANNO_VISIBILITY); anno) {
_visible = anno.get_default_bool();
}
2021-06-08 02:47:04 +00:00
if (auto anno = _param.get_annotation(ST_ANNO_AUTOMATIC); anno) {
_automatic = anno.get_default_bool();
}
// Read Order
2021-06-08 02:47:04 +00:00
if (auto anno = _param.get_annotation(ST_ANNO_ORDER); anno) {
_order = anno.get_default_int();
}
// Read Name
2021-06-08 02:47:04 +00:00
if (auto anno = _param.get_annotation(ST_ANNO_NAME); anno) {
if (std::string v = anno.get_default_string(); v.length() > 0) {
_name = v;
} else {
2021-06-08 02:47:04 +00:00
throw std::out_of_range("'" ST_ANNO_NAME "' annotation has zero length.");
}
}
// Read Description
2021-06-08 02:47:04 +00:00
if (auto anno = _param.get_annotation(ST_ANNO_DESCRIPTION); anno) {
if (std::string v = anno.get_default_string(); v.length() > 0) {
_description = v;
} else {
2021-06-08 02:47:04 +00:00
throw std::out_of_range("'" ST_ANNO_DESCRIPTION "' annotation has zero length.");
}
}
// Read Type override.
_type = get_type_from_effect_type(_param.get_type());
2021-06-08 02:47:04 +00:00
if (auto anno = _param.get_annotation(ST_ANNO_TYPE); anno) {
// We have a type override.
_type = get_type_from_string(anno.get_default_string());
}
// Read Size override.
_size = get_length_from_effect_type(_param.get_type());
2021-06-08 02:47:04 +00:00
if (auto anno = _param.get_annotation(ST_ANNO_SIZE); anno) {
2020-04-08 21:38:42 +00:00
std::size_t ov = static_cast<size_t>(anno.get_default_int());
if (ov > 0)
_size = ov;
}
_size = std::clamp<size_t>(_size, size_t{1}, size_t{32});
}
2021-06-08 02:47:04 +00:00
void streamfx::gfx::shader::parameter::defaults(obs_data_t* settings) {}
2021-06-08 02:47:04 +00:00
void streamfx::gfx::shader::parameter::properties(obs_properties_t* props, obs_data_t* settings) {}
2019-12-18 05:37:38 +00:00
2021-06-08 02:47:04 +00:00
void streamfx::gfx::shader::parameter::update(obs_data_t* settings) {}
2019-12-18 05:37:38 +00:00
2021-06-08 02:47:04 +00:00
void streamfx::gfx::shader::parameter::assign() {}
2019-12-18 05:37:38 +00:00
void streamfx::gfx::shader::parameter::visible(bool visible) {}
void streamfx::gfx::shader::parameter::active(bool active) {}
2021-06-08 02:47:04 +00:00
std::shared_ptr<streamfx::gfx::shader::parameter>
streamfx::gfx::shader::parameter::make_parameter(streamfx::obs::gs::effect_parameter param, std::string prefix)
2019-12-18 05:37:38 +00:00
{
if (!param) {
throw std::runtime_error("Bad call to make_parameter. This is a bug in the plugin.");
}
parameter_type real_type = get_type_from_effect_type(param.get_type());
2021-06-08 02:47:04 +00:00
if (auto anno = param.get_annotation(ST_ANNO_TYPE); anno) {
// We have a type override.
real_type = get_type_from_string(param.get_default_string());
2019-12-18 05:37:38 +00:00
}
switch (real_type) {
case parameter_type::Boolean:
2021-06-08 02:47:04 +00:00
return std::make_shared<streamfx::gfx::shader::bool_parameter>(param, prefix);
case parameter_type::Integer:
2021-06-08 02:47:04 +00:00
return std::make_shared<streamfx::gfx::shader::int_parameter>(param, prefix);
case parameter_type::Float:
2021-06-08 02:47:04 +00:00
return std::make_shared<streamfx::gfx::shader::float_parameter>(param, prefix);
2019-12-18 05:37:38 +00:00
default:
2019-12-21 16:04:38 +00:00
return nullptr;
2019-12-18 05:37:38 +00:00
}
}