gfx-shader-param*: Initial code
This commit is contained in:
		
							parent
							
								
									f49f3a30b6
								
							
						
					
					
						commit
						0a761b97a2
					
				|  | @ -0,0 +1,192 @@ | |||
| // 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-basic.hpp" | ||||
| #include <sstream> | ||||
| #include "strings.hpp" | ||||
| 
 | ||||
| inline bool get_annotation_string(gs::effect_parameter param, std::string anno_name, std::string& out) | ||||
| { | ||||
| 	if (!param) | ||||
| 		return false; | ||||
| 
 | ||||
| 	if (auto el = param.get_annotation(anno_name); el != nullptr) { | ||||
| 		if (auto val = el.get_default_string(); val.length() > 0) { | ||||
| 			out = val; | ||||
| 			return true; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	return false; | ||||
| } | ||||
| 
 | ||||
| inline bool get_annotation_float(gs::effect_parameter param, std::string anno_name, float_t& out) | ||||
| { | ||||
| 	if (!param) { | ||||
| 		return false; | ||||
| 	} | ||||
| 
 | ||||
| 	if (auto el = param.get_annotation(anno_name); el != nullptr) { | ||||
| 		out = el.get_default_float(); | ||||
| 		return true; | ||||
| 	} | ||||
| 
 | ||||
| 	return false; | ||||
| } | ||||
| 
 | ||||
| gfx::shader::bool_parameter::bool_parameter(gs::effect_parameter param, std::string prefix) : parameter(param) | ||||
| { | ||||
| 	std::stringstream ss; | ||||
| 	ss << prefix << "." << param.get_name(); | ||||
| 
 | ||||
| 	_key  = ss.str(); | ||||
| 	_name = _key; | ||||
| 	_desc = ""; | ||||
| 
 | ||||
| 	get_annotation_string(_param, "name", _name); | ||||
| 	get_annotation_string(_param, "description", _desc); | ||||
| } | ||||
| 
 | ||||
| gfx::shader::bool_parameter::~bool_parameter() {} | ||||
| 
 | ||||
| void gfx::shader::bool_parameter::properties(obs_properties_t* props, obs_data_t* settings) | ||||
| { | ||||
| 	auto p = obs_properties_add_list(props, _key.c_str(), _name.c_str(), OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT); | ||||
| 	if (_desc.length() > 0) | ||||
| 		obs_property_set_long_description(p, _desc.c_str()); | ||||
| 	obs_property_list_add_int(p, D_TRANSLATE(S_STATE_DISABLED), 0); | ||||
| 	obs_property_list_add_int(p, D_TRANSLATE(S_STATE_ENABLED), 1); | ||||
| 
 | ||||
| 	obs_data_set_default_bool(settings, _key.c_str(), _param.get_default_bool()); | ||||
| } | ||||
| 
 | ||||
| void gfx::shader::bool_parameter::update(obs_data_t* settings) | ||||
| { | ||||
| 	_value = obs_data_get_int(settings, _key.c_str()) != 0; | ||||
| } | ||||
| 
 | ||||
| void gfx::shader::bool_parameter::assign() | ||||
| { | ||||
| 	_param.set_bool(_value); | ||||
| } | ||||
| 
 | ||||
| gfx::shader::float_parameter::float_parameter(gs::effect_parameter param, std::string prefix) : parameter(param) | ||||
| { | ||||
| 	switch (_param.get_type()) { | ||||
| 	case gs::effect_parameter::type::Float: | ||||
| 		_len = 1; | ||||
| 		break; | ||||
| 	case gs::effect_parameter::type::Float2: | ||||
| 		_len = 2; | ||||
| 		break; | ||||
| 	case gs::effect_parameter::type::Float3: | ||||
| 		_len = 3; | ||||
| 		break; | ||||
| 	case gs::effect_parameter::type::Float4: | ||||
| 		_len = 4; | ||||
| 		break; | ||||
| 	default: | ||||
| 		_len = 0; | ||||
| 	} | ||||
| 
 | ||||
| 	// Build baseline key.
 | ||||
| 	std::stringstream ss; | ||||
| 	ss << prefix << "." << param.get_name(); | ||||
| 
 | ||||
| 	// Build primary key.
 | ||||
| 	_key[4]  = ss.str(); | ||||
| 	_name[4] = _key[4]; | ||||
| 	_desc    = ""; | ||||
| 	get_annotation_string(_param, "name", _name[4]); | ||||
| 	get_annotation_string(_param, "description", _desc); | ||||
| 
 | ||||
| 	// Build sub-keys
 | ||||
| 	for (size_t len = 0; len < _len; len++) { | ||||
| 		char buf[4]; | ||||
| 		snprintf(buf, 4, "[%d]", static_cast<int32_t>(len - 1)); | ||||
| 		_name[len] = std::string(buf, buf + sizeof(buf)); | ||||
| 		_key[len]  = _key[4] + _name[len]; | ||||
| 
 | ||||
| 		_min[len]  = std::numeric_limits<float_t>::min(); | ||||
| 		_max[len]  = std::numeric_limits<float_t>::max(); | ||||
| 		_step[len] = 0.01f; | ||||
| 	} | ||||
| 
 | ||||
| 	if (auto anno = _param.get_annotation("minimum"); (anno != nullptr)) { | ||||
| 		if (anno.get_type() == gs::effect_parameter::type::Float) { | ||||
| 			for (size_t len = 0; len < _len; len++) { | ||||
| 				anno.get_default_value(&_min[len], 1); | ||||
| 			} | ||||
| 		} else if (anno.get_type() == _param.get_type()) { | ||||
| 			anno.get_default_value(_min, _len); | ||||
| 		} | ||||
| 	} | ||||
| 	if (auto anno = _param.get_annotation("maximum"); (anno != nullptr)) { | ||||
| 		if (anno.get_type() == gs::effect_parameter::type::Float) { | ||||
| 			for (size_t len = 0; len < _len; len++) { | ||||
| 				anno.get_default_value(&_max[len], 1); | ||||
| 			} | ||||
| 		} else if (anno.get_type() == _param.get_type()) { | ||||
| 			anno.get_default_value(_max, _len); | ||||
| 		} | ||||
| 	} | ||||
| 	if (auto anno = _param.get_annotation("step"); (anno != nullptr)) { | ||||
| 		if (anno.get_type() == gs::effect_parameter::type::Float) { | ||||
| 			for (size_t len = 0; len < _len; len++) { | ||||
| 				anno.get_default_value(&_step[len], 1); | ||||
| 			} | ||||
| 		} else if (anno.get_type() == _param.get_type()) { | ||||
| 			anno.get_default_value(_step, _len); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| gfx::shader::float_parameter::~float_parameter() {} | ||||
| 
 | ||||
| void gfx::shader::float_parameter::properties(obs_properties_t* props, obs_data_t* settings) | ||||
| { | ||||
| 	auto grp = obs_properties_create(); | ||||
| 	obs_properties_add_group(props, _key[4].c_str(), _name[4].c_str(), OBS_GROUP_NORMAL, grp); | ||||
| 
 | ||||
| 	float_t defaults[4] = {0, 0, 0, 0}; | ||||
| 	_param.get_default_value(defaults, _len); | ||||
| 
 | ||||
| 	for (size_t len = 0; len < _len; len++) { | ||||
| 		auto p = | ||||
| 			obs_properties_add_float(props, _key[len].c_str(), _name[len].c_str(), _min[len], _max[len], _step[len]); | ||||
| 		obs_property_set_long_description(p, _desc.c_str()); | ||||
| 		obs_data_set_default_double(settings, _key[len].c_str(), static_cast<double_t>(defaults[len])); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void gfx::shader::float_parameter::update(obs_data_t* settings) | ||||
| { | ||||
| 	for (size_t len = 0; len < _len; len++) { | ||||
| 		_value[len] = static_cast<float_t>(obs_data_get_double(settings, _key[len].c_str())); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void gfx::shader::float_parameter::assign() | ||||
| { | ||||
| 	_param.set_value(_value, _len); | ||||
| } | ||||
| 
 | ||||
| gfx::shader::int_parameter::int_parameter(gs::effect_parameter param, std::string prefix) : parameter(param) {} | ||||
| 
 | ||||
| gfx::shader::int_parameter::~int_parameter() {} | ||||
| 
 | ||||
| void gfx::shader::int_parameter::properties(obs_properties_t* props, obs_data_t* settings) {} | ||||
|  | @ -0,0 +1,77 @@ | |||
| // 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
 | ||||
| 
 | ||||
| #pragma once | ||||
| #include <string> | ||||
| #include "gfx-shader-param.hpp" | ||||
| #include "obs/gs/gs-effect-parameter.hpp" | ||||
| 
 | ||||
| namespace gfx { | ||||
| 	namespace shader { | ||||
| 		class bool_parameter : public parameter { | ||||
| 			std::string _key; | ||||
| 			std::string _name; | ||||
| 			std::string _desc; | ||||
| 
 | ||||
| 			bool _value; | ||||
| 
 | ||||
| 			public: | ||||
| 			bool_parameter(gs::effect_parameter param, std::string prefix); | ||||
| 			virtual ~bool_parameter(); | ||||
| 
 | ||||
| 			public: | ||||
| 			virtual void properties(obs_properties_t* props, obs_data_t* settings) override; | ||||
| 
 | ||||
| 			virtual void update(obs_data_t* settings) override; | ||||
| 
 | ||||
| 			virtual void assign() override; | ||||
| 		}; | ||||
| 
 | ||||
| 		struct float_parameter : public parameter { | ||||
| 			size_t _len; | ||||
| 			std::string _key[5]; | ||||
| 			std::string _name[5]; | ||||
| 			std::string _desc; | ||||
| 
 | ||||
| 			float_t _min[4]; | ||||
| 			float_t _max[4]; | ||||
| 			float_t _step[4]; | ||||
| 			float_t _value[4]; | ||||
| 
 | ||||
| 			public: | ||||
| 			float_parameter(gs::effect_parameter param, std::string prefix); | ||||
| 			virtual ~float_parameter(); | ||||
| 
 | ||||
| 			public: | ||||
| 			virtual void properties(obs_properties_t* props, obs_data_t* settings) override; | ||||
| 
 | ||||
| 			virtual void update(obs_data_t* settings) override; | ||||
| 
 | ||||
| 			virtual void assign() override; | ||||
| 		}; | ||||
| 
 | ||||
| 		struct int_parameter : public parameter { | ||||
| 			public: | ||||
| 			int_parameter(gs::effect_parameter param, std::string prefix); | ||||
| 			virtual ~int_parameter(); | ||||
| 
 | ||||
| 			public: | ||||
| 			virtual void properties(obs_properties_t* props, obs_data_t* settings) override; | ||||
| 		}; | ||||
| 
 | ||||
| 	} // namespace shader
 | ||||
| } // namespace gfx
 | ||||
|  | @ -0,0 +1,56 @@ | |||
| // 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 "gfx-shader-param-basic.hpp" | ||||
| 
 | ||||
| void gfx::shader::parameter::properties(obs_properties_t* props, obs_data_t* settings) {} | ||||
| 
 | ||||
| void gfx::shader::parameter::update(obs_data_t* settings) {} | ||||
| 
 | ||||
| void gfx::shader::parameter::assign() {} | ||||
| 
 | ||||
| std::shared_ptr<gfx::shader::parameter> gfx::shader::parameter::make_parameter(gs::effect_parameter param, | ||||
| 																			   std::string          prefix) | ||||
| { | ||||
| 	if (!param) | ||||
| 		return nullptr; | ||||
| 
 | ||||
| 	typedef gs::effect_parameter::type eptype; | ||||
| 	switch (param.get_type()) { | ||||
| 	case eptype::Boolean: { | ||||
| 		auto el = std::make_shared<gfx::shader::bool_parameter>(param, prefix); | ||||
| 		return el; | ||||
| 	} | ||||
| 	case eptype::Integer: | ||||
| 	case eptype::Integer2: | ||||
| 	case eptype::Integer3: | ||||
| 	case eptype::Integer4: { | ||||
| 		auto el = std::make_shared<gfx::shader::int_parameter>(param, prefix); | ||||
| 		return el; | ||||
| 	} | ||||
| 	case eptype::Float: | ||||
| 	case eptype::Float2: | ||||
| 	case eptype::Float3: | ||||
| 	case eptype::Float4: { | ||||
| 		auto el = std::make_shared<gfx::shader::float_parameter>(param, prefix); | ||||
| 		return el; | ||||
| 	} | ||||
| 	default: | ||||
| 	return nullptr; | ||||
| 	} | ||||
| } | ||||
|  | @ -0,0 +1,55 @@ | |||
| // 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
 | ||||
| 
 | ||||
| #pragma once | ||||
| #include <list> | ||||
| #include <string> | ||||
| #include "obs/gs/gs-effect-parameter.hpp" | ||||
| 
 | ||||
| // OBS
 | ||||
| extern "C" { | ||||
| #ifdef _MSC_VER | ||||
| #pragma warning(push) | ||||
| #pragma warning(disable : 4201) | ||||
| #endif | ||||
| #include <obs.h> | ||||
| #ifdef _MSC_VER | ||||
| #pragma warning(pop) | ||||
| #endif | ||||
| } | ||||
| 
 | ||||
| namespace gfx { | ||||
| 	namespace shader { | ||||
| 		class parameter { | ||||
| 			protected: | ||||
| 			gs::effect_parameter _param; | ||||
| 
 | ||||
| 			parameter(gs::effect_parameter param) : _param(param){}; | ||||
| 			virtual ~parameter(){}; | ||||
| 
 | ||||
| 			public: | ||||
| 			virtual void properties(obs_properties_t* props, obs_data_t* settings); | ||||
| 
 | ||||
| 			virtual void update(obs_data_t* settings); | ||||
| 
 | ||||
| 			virtual void assign(); | ||||
| 
 | ||||
| 			public: | ||||
| 			static std::shared_ptr<parameter> make_parameter(gs::effect_parameter param, std::string prefix); | ||||
| 		}; | ||||
| 	} // namespace shader
 | ||||
| } // namespace gfx
 | ||||
		Loading…
	
		Reference in New Issue