From 22786005d604e6fd7e85192f9bb14fca67cc75e4 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Thu, 25 Nov 2021 05:57:37 +0100 Subject: [PATCH] gfx/shader/param: Track the parent shader object --- source/gfx/shader/gfx-shader-param-basic.cpp | 22 ++++++++++++-------- source/gfx/shader/gfx-shader-param-basic.hpp | 12 +++++++---- source/gfx/shader/gfx-shader-param.cpp | 17 ++++++++------- source/gfx/shader/gfx-shader-param.hpp | 16 ++++++++++++-- source/gfx/shader/gfx-shader.cpp | 5 +++-- 5 files changed, 48 insertions(+), 24 deletions(-) diff --git a/source/gfx/shader/gfx-shader-param-basic.cpp b/source/gfx/shader/gfx-shader-param-basic.cpp index 2025d184..df03c46e 100644 --- a/source/gfx/shader/gfx-shader-param-basic.cpp +++ b/source/gfx/shader/gfx-shader-param-basic.cpp @@ -76,9 +76,10 @@ streamfx::gfx::shader::basic_field_type streamfx::gfx::shader::get_field_type_fr return basic_field_type::Input; } -streamfx::gfx::shader::basic_parameter::basic_parameter(streamfx::obs::gs::effect_parameter param, std::string prefix) - : parameter(param, prefix), _field_type(basic_field_type::Input), _suffix(), _keys(), _names(), _min(), _max(), - _step(), _values() +streamfx::gfx::shader::basic_parameter::basic_parameter(streamfx::gfx::shader::shader* parent, + streamfx::obs::gs::effect_parameter param, std::string prefix) + : parameter(parent, param, prefix), _field_type(basic_field_type::Input), _suffix(), _keys(), _names(), _min(), + _max(), _step(), _values() { char string_buffer[256]; @@ -161,8 +162,9 @@ void streamfx::gfx::shader::basic_parameter::load_parameter_data(streamfx::obs:: parameter.get_default_value(&data.i32, 1); } -streamfx::gfx::shader::bool_parameter::bool_parameter(streamfx::obs::gs::effect_parameter param, std::string prefix) - : basic_parameter(param, prefix) +streamfx::gfx::shader::bool_parameter::bool_parameter(streamfx::gfx::shader::shader* parent, + streamfx::obs::gs::effect_parameter param, std::string prefix) + : basic_parameter(parent, param, prefix) { _min.resize(0); _max.resize(0); @@ -214,8 +216,9 @@ void streamfx::gfx::shader::bool_parameter::assign() get_parameter().set_value(_data.data(), _data.size()); } -streamfx::gfx::shader::float_parameter::float_parameter(streamfx::obs::gs::effect_parameter param, std::string prefix) - : basic_parameter(param, prefix) +streamfx::gfx::shader::float_parameter::float_parameter(streamfx::gfx::shader::shader* parent, + streamfx::obs::gs::effect_parameter param, std::string prefix) + : basic_parameter(parent, param, prefix) { _data.resize(get_size()); @@ -341,8 +344,9 @@ static inline obs_property_t* build_int_property(streamfx::gfx::shader::basic_fi } } -streamfx::gfx::shader::int_parameter::int_parameter(streamfx::obs::gs::effect_parameter param, std::string prefix) - : basic_parameter(param, prefix) +streamfx::gfx::shader::int_parameter::int_parameter(streamfx::gfx::shader::shader* parent, + streamfx::obs::gs::effect_parameter param, std::string prefix) + : basic_parameter(parent, param, prefix) { _data.resize(get_size()); diff --git a/source/gfx/shader/gfx-shader-param-basic.hpp b/source/gfx/shader/gfx-shader-param-basic.hpp index facfb6ae..982282f4 100644 --- a/source/gfx/shader/gfx-shader-param-basic.hpp +++ b/source/gfx/shader/gfx-shader-param-basic.hpp @@ -62,7 +62,8 @@ namespace streamfx::gfx { std::list _values; public: - basic_parameter(streamfx::obs::gs::effect_parameter param, std::string prefix); + basic_parameter(streamfx::gfx::shader::shader* parent, streamfx::obs::gs::effect_parameter param, + std::string prefix); virtual ~basic_parameter(); virtual void load_parameter_data(streamfx::obs::gs::effect_parameter parameter, basic_data& data); @@ -98,7 +99,8 @@ namespace streamfx::gfx { std::vector _data; public: - bool_parameter(streamfx::obs::gs::effect_parameter param, std::string prefix); + bool_parameter(streamfx::gfx::shader::shader* parent, streamfx::obs::gs::effect_parameter param, + std::string prefix); virtual ~bool_parameter(); void defaults(obs_data_t* settings) override; @@ -114,7 +116,8 @@ namespace streamfx::gfx { std::vector _data; public: - float_parameter(streamfx::obs::gs::effect_parameter param, std::string prefix); + float_parameter(streamfx::gfx::shader::shader* parent, streamfx::obs::gs::effect_parameter param, + std::string prefix); virtual ~float_parameter(); void defaults(obs_data_t* settings) override; @@ -130,7 +133,8 @@ namespace streamfx::gfx { std::vector _data; public: - int_parameter(streamfx::obs::gs::effect_parameter param, std::string prefix); + int_parameter(streamfx::gfx::shader::shader* parent, streamfx::obs::gs::effect_parameter param, + std::string prefix); virtual ~int_parameter(); void defaults(obs_data_t* settings) override; diff --git a/source/gfx/shader/gfx-shader-param.cpp b/source/gfx/shader/gfx-shader-param.cpp index 9e525465..54dba13d 100644 --- a/source/gfx/shader/gfx-shader-param.cpp +++ b/source/gfx/shader/gfx-shader-param.cpp @@ -111,8 +111,10 @@ streamfx::gfx::shader::parameter_type streamfx::gfx::shader::get_type_from_strin throw std::invalid_argument("Invalid parameter type string."); } -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() +streamfx::gfx::shader::parameter::parameter(streamfx::gfx::shader::shader* parent, + streamfx::obs::gs::effect_parameter param, std::string key_prefix) + : _parent(parent), _param(param), _order(0), _key(_param.get_name()), _visible(true), _automatic(false), + _name(_key), _description() { { std::stringstream ss; @@ -181,9 +183,10 @@ void streamfx::gfx::shader::parameter::visible(bool visible) {} void streamfx::gfx::shader::parameter::active(bool active) {} std::shared_ptr - streamfx::gfx::shader::parameter::make_parameter(streamfx::obs::gs::effect_parameter param, std::string prefix) + streamfx::gfx::shader::parameter::make_parameter(streamfx::gfx::shader::shader* parent, + streamfx::obs::gs::effect_parameter param, std::string prefix) { - if (!param) { + if (!parent || !param) { throw std::runtime_error("Bad call to make_parameter. This is a bug in the plugin."); } @@ -195,11 +198,11 @@ std::shared_ptr switch (real_type) { case parameter_type::Boolean: - return std::make_shared(param, prefix); + return std::make_shared(parent, param, prefix); case parameter_type::Integer: - return std::make_shared(param, prefix); + return std::make_shared(parent, param, prefix); case parameter_type::Float: - return std::make_shared(param, prefix); + return std::make_shared(parent, param, prefix); default: return nullptr; } diff --git a/source/gfx/shader/gfx-shader-param.hpp b/source/gfx/shader/gfx-shader-param.hpp index 8f4f0d5c..7bc47850 100644 --- a/source/gfx/shader/gfx-shader-param.hpp +++ b/source/gfx/shader/gfx-shader-param.hpp @@ -22,6 +22,8 @@ namespace streamfx::gfx { namespace shader { + class shader; + enum class parameter_type { // Unknown type, could be anything. Unknown, @@ -46,6 +48,9 @@ namespace streamfx::gfx { parameter_type get_type_from_string(std::string v); class parameter { + // Parent Shader + streamfx::gfx::shader::shader* _parent; + // Parameter used for all functionality. streamfx::obs::gs::effect_parameter _param; @@ -68,7 +73,8 @@ namespace streamfx::gfx { std::string _description; protected: - parameter(streamfx::obs::gs::effect_parameter param, std::string key_prefix); + parameter(streamfx::gfx::shader::shader* parent, streamfx::obs::gs::effect_parameter param, + std::string key_prefix); virtual ~parameter(){}; public: @@ -85,6 +91,11 @@ namespace streamfx::gfx { virtual void active(bool enabled); public: + inline streamfx::gfx::shader::shader* get_parent() + { + return _parent; + } + inline streamfx::obs::gs::effect_parameter get_parameter() { return _param; @@ -141,7 +152,8 @@ namespace streamfx::gfx { } public: - static std::shared_ptr make_parameter(streamfx::obs::gs::effect_parameter param, + static std::shared_ptr make_parameter(streamfx::gfx::shader::shader* parent, + streamfx::obs::gs::effect_parameter param, std::string prefix); }; } // namespace shader diff --git a/source/gfx/shader/gfx-shader.cpp b/source/gfx/shader/gfx-shader.cpp index 8a8c28e5..cfb85d85 100644 --- a/source/gfx/shader/gfx-shader.cpp +++ b/source/gfx/shader/gfx-shader.cpp @@ -152,7 +152,7 @@ try { if (fnd != _shader_params.end()) continue; - auto param = streamfx::gfx::shader::parameter::make_parameter(el, ST_KEY_PARAMETERS); + auto param = streamfx::gfx::shader::parameter::make_parameter(this, el, ST_KEY_PARAMETERS); if (param) { _shader_params.insert_or_assign(el_name, param); @@ -616,6 +616,7 @@ void streamfx::gfx::shader::shader::set_active(bool active) } } -obs_source_t* streamfx::gfx::shader::shader::get() { +obs_source_t* streamfx::gfx::shader::shader::get() +{ return _self; }