From f27320c6c99a27393b51df595e50c396c3b7a6ee Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Wed, 4 Sep 2019 02:47:27 +0200 Subject: [PATCH] project: Remove use of P_INITIALIZER Does not support cross-platform very well and breaks on Apple for no apparent reason. Add resolution scale to shader filter. --- source/filters/filter-blur.cpp | 7 --- source/filters/filter-color-grade.cpp | 6 --- source/filters/filter-displacement.cpp | 7 --- source/filters/filter-dynamic-mask.cpp | 6 --- source/filters/filter-sdf-effects.cpp | 7 --- source/filters/filter-shader.cpp | 59 +++++++++++++++++++++----- source/filters/filter-shader.hpp | 4 ++ source/filters/filter-transform.cpp | 7 --- source/gfx/gfx-effect-source.cpp | 12 ++++-- source/gfx/gfx-effect-source.hpp | 2 +- source/plugin.cpp | 29 +++++++++++++ source/sources/source-mirror.cpp | 7 --- source/sources/source-shader.cpp | 6 --- source/utility.hpp | 28 ------------ 14 files changed, 90 insertions(+), 97 deletions(-) diff --git a/source/filters/filter-blur.cpp b/source/filters/filter-blur.cpp index e48ea229..f4ffbf0b 100644 --- a/source/filters/filter-blur.cpp +++ b/source/filters/filter-blur.cpp @@ -99,13 +99,6 @@ static std::map list_of_subtypes = { {"zoom", {::gfx::blur::type::Zoom, S_BLUR_SUBTYPE_ZOOM}}, }; -// Initializer & Finalizer -P_INITIALIZER(filterBlurFactoryInitializer) -{ - initializer_functions.push_back([] { filter::blur::blur_factory::initialize(); }); - finalizer_functions.push_back([] { filter::blur::blur_factory::finalize(); }); -} - static std::shared_ptr factory_instance = nullptr; void filter::blur::blur_factory::initialize() diff --git a/source/filters/filter-color-grade.cpp b/source/filters/filter-color-grade.cpp index 3a534aa7..71a2e288 100644 --- a/source/filters/filter-color-grade.cpp +++ b/source/filters/filter-color-grade.cpp @@ -73,12 +73,6 @@ #define MODE_LOG Log #define MODE_LOG10 Log10 -// Initializer & Finalizer -P_INITIALIZER(FilterColorGradeInit) -{ - initializer_functions.push_back([] { filter::color_grade::color_grade_factory::initialize(); }); - finalizer_functions.push_back([] { filter::color_grade::color_grade_factory::finalize(); }); -} const char* get_name(void*) { diff --git a/source/filters/filter-displacement.cpp b/source/filters/filter-displacement.cpp index c0f01a13..e4488ddf 100644 --- a/source/filters/filter-displacement.cpp +++ b/source/filters/filter-displacement.cpp @@ -27,13 +27,6 @@ #define ST_RATIO "Filter.Displacement.Ratio" #define ST_SCALE "Filter.Displacement.Scale" -// Initializer & Finalizer -P_INITIALIZER(FilterDisplacementInit) -{ - initializer_functions.push_back([] { filter::displacement::displacement_factory::initialize(); }); - finalizer_functions.push_back([] { filter::displacement::displacement_factory::finalize(); }); -} - static std::shared_ptr factory_instance = nullptr; void filter::displacement::displacement_factory::initialize() diff --git a/source/filters/filter-dynamic-mask.cpp b/source/filters/filter-dynamic-mask.cpp index 41b1dacb..e3d6fe15 100644 --- a/source/filters/filter-dynamic-mask.cpp +++ b/source/filters/filter-dynamic-mask.cpp @@ -45,12 +45,6 @@ static std::pair channel_translation {filter::dynamic_mask::channel::Alpha, S_CHANNEL_ALPHA}, }; -P_INITIALIZER(FilterDynamicMaskInit) -{ - initializer_functions.push_back([] { filter::dynamic_mask::dynamic_mask_factory::initialize(); }); - finalizer_functions.push_back([] { filter::dynamic_mask::dynamic_mask_factory::finalize(); }); -} - static std::shared_ptr factory_instance = nullptr; void filter::dynamic_mask::dynamic_mask_factory::initialize() diff --git a/source/filters/filter-sdf-effects.cpp b/source/filters/filter-sdf-effects.cpp index 8c9564cb..b9e94ec5 100644 --- a/source/filters/filter-sdf-effects.cpp +++ b/source/filters/filter-sdf-effects.cpp @@ -64,13 +64,6 @@ #define ST_SDF_SCALE "Filter.SDFEffects.SDF.Scale" #define ST_SDF_THRESHOLD "Filter.SDFEffects.SDF.Threshold" -// Initializer & Finalizer -P_INITIALIZER(filterShadowFactoryInitializer) -{ - initializer_functions.push_back([] { filter::sdf_effects::sdf_effects_factory::initialize(); }); - finalizer_functions.push_back([] { filter::sdf_effects::sdf_effects_factory::finalize(); }); -} - static std::shared_ptr factory_instance = nullptr; void filter::sdf_effects::sdf_effects_factory::initialize() diff --git a/source/filters/filter-shader.cpp b/source/filters/filter-shader.cpp index e644d418..f2e89ac9 100644 --- a/source/filters/filter-shader.cpp +++ b/source/filters/filter-shader.cpp @@ -22,12 +22,10 @@ #include "utility.hpp" #define ST "Filter.Shader" - -P_INITIALIZER(FilterShaderInit) -{ - initializer_functions.push_back([] { filter::shader::shader_factory::initialize(); }); - finalizer_functions.push_back([] { filter::shader::shader_factory::finalize(); }); -} +#define ST_SCALE_LOCKED "Filter.Shader.Scale.Locked" +#define ST_SCALE_SCALE "Filter.Shader.Scale.Scale" +#define ST_SCALE_WIDTH "Filter.Shader.Scale.Width" +#define ST_SCALE_HEIGHT "Filter.Shader.Scale.Height" static std::shared_ptr factory_instance = nullptr; @@ -50,6 +48,10 @@ static void get_defaults(obs_data_t* data) { obs_data_set_default_string(data, S_SHADER_FILE, obs_module_file("shaders/filter/example.effect")); obs_data_set_default_string(data, S_SHADER_TECHNIQUE, "Draw"); + obs_data_set_default_bool(data, ST_SCALE_LOCKED, true); + obs_data_set_default_double(data, ST_SCALE_SCALE, 1.0); + obs_data_set_default_double(data, ST_SCALE_WIDTH, 1.0); + obs_data_set_default_double(data, ST_SCALE_HEIGHT, 1.0); } filter::shader::shader_factory::shader_factory() @@ -207,16 +209,30 @@ filter::shader::shader_instance::~shader_instance() {} uint32_t filter::shader::shader_instance::width() { - return _width; + return _swidth; } uint32_t filter::shader::shader_instance::height() { - return _height; + return _sheight; } void filter::shader::shader_instance::properties(obs_properties_t* props) { + auto p = obs_properties_add_bool(props, ST_SCALE_LOCKED, D_TRANSLATE(ST_SCALE_LOCKED)); + obs_property_set_modified_callback(p, [](obs_properties_t* props, obs_property_t* property, obs_data_t* settings) { + obs_property_set_visible(obs_properties_get(props, ST_SCALE_SCALE), + obs_data_get_bool(settings, ST_SCALE_LOCKED)); + obs_property_set_visible(obs_properties_get(props, ST_SCALE_WIDTH), + !obs_data_get_bool(settings, ST_SCALE_LOCKED)); + obs_property_set_visible(obs_properties_get(props, ST_SCALE_HEIGHT), + !obs_data_get_bool(settings, ST_SCALE_LOCKED)); + return true; + }); + obs_properties_add_float(props, ST_SCALE_SCALE, D_TRANSLATE(ST_SCALE_SCALE), 0.01, 5.0, 0.01); + obs_properties_add_float(props, ST_SCALE_WIDTH, D_TRANSLATE(ST_SCALE_WIDTH), 0.01, 5.0, 0.01); + obs_properties_add_float(props, ST_SCALE_HEIGHT, D_TRANSLATE(ST_SCALE_HEIGHT), 0.01, 5.0, 0.01); + _fx->properties(props); } @@ -227,6 +243,13 @@ void filter::shader::shader_instance::load(obs_data_t* data) void filter::shader::shader_instance::update(obs_data_t* data) { + _scale_locked = obs_data_get_bool(data, ST_SCALE_LOCKED); + if (_scale_locked) { + _hscale = _wscale = obs_data_get_double(data, ST_SCALE_SCALE); + } else { + _wscale = obs_data_get_double(data, ST_SCALE_WIDTH); + _hscale = obs_data_get_double(data, ST_SCALE_HEIGHT); + } _fx->update(data); } @@ -248,6 +271,10 @@ bool filter::shader::shader_instance::valid_param(std::shared_ptrget_name().c_str(), "ImageSource_Texel") == 0) return false; + if (strcmpi(param->get_name().c_str(), "ImageTarget_Size") == 0) + return false; + if (strcmpi(param->get_name().c_str(), "ImageTarget_Texel") == 0) + return false; return true; } @@ -256,7 +283,6 @@ void filter::shader::shader_instance::override_param(std::shared_ptr auto p_source = effect->get_parameter("ImageSource"); auto p_source_size = effect->get_parameter("ImageSource_Size"); auto p_source_texel = effect->get_parameter("ImageSource_Texel"); - if (p_source && (p_source->get_type() == gs::effect_parameter::type::Texture)) { p_source->set_texture(_rt_tex); } @@ -266,6 +292,15 @@ void filter::shader::shader_instance::override_param(std::shared_ptr if (p_source_texel && (p_source_size->get_type() == gs::effect_parameter::type::Float2)) { p_source_texel->set_float2(1.0f / _width, 1.0f / _height); } + + auto p_target_size = effect->get_parameter("ImageTarget_Size"); + auto p_target_texel = effect->get_parameter("ImageTarget_Texel"); + if (p_target_size && (p_target_size->get_type() == gs::effect_parameter::type::Float2)) { + p_target_size->set_float2(_swidth, _sheight); + } + if (p_target_texel && (p_target_texel->get_type() == gs::effect_parameter::type::Float2)) { + p_target_texel->set_float2(1.0f / _swidth, 1.0f / _sheight); + } } void filter::shader::shader_instance::enum_active_sources(obs_source_enum_proc_t r, void* p) @@ -280,6 +315,8 @@ void filter::shader::shader_instance::video_tick(float_t sec_since_last) { // Update width and height. _width = obs_source_get_base_width(target); _height = obs_source_get_base_height(target); + _swidth = _width * _wscale; + _sheight = _height * _hscale; } if (_fx->tick(sec_since_last)) { @@ -326,7 +363,7 @@ void filter::shader::shader_instance::video_render(gs_effect_t* effect) if (!_rt2_updated) { try { - auto op = _rt2->render(_width, _height); + auto op = _rt2->render(_swidth, _sheight); _fx->render(); } catch (...) { obs_source_skip_video_filter(_self); @@ -343,6 +380,6 @@ void filter::shader::shader_instance::video_render(gs_effect_t* effect) gs_effect_set_texture(prm, _rt2_tex->get_object()); while (gs_effect_loop(ef, "Draw")) { - gs_draw_sprite(nullptr, 0, _width, _height); + gs_draw_sprite(nullptr, 0, _swidth, _sheight); } } diff --git a/source/filters/filter-shader.hpp b/source/filters/filter-shader.hpp index 789cf13a..49908aea 100644 --- a/source/filters/filter-shader.hpp +++ b/source/filters/filter-shader.hpp @@ -48,6 +48,10 @@ namespace filter { uint32_t _width, _height; + bool _scale_locked; + float_t _wscale, _hscale; + uint32_t _swidth, _sheight; + std::shared_ptr _rt; bool _rt_updated; std::shared_ptr _rt_tex; diff --git a/source/filters/filter-transform.cpp b/source/filters/filter-transform.cpp index 47c64796..281f5344 100644 --- a/source/filters/filter-transform.cpp +++ b/source/filters/filter-transform.cpp @@ -75,13 +75,6 @@ enum RotationOrder : int64_t { ZYX, }; -// Initializer & Finalizer -P_INITIALIZER(FilterTransformInit) -{ - initializer_functions.push_back([] { filter::transform::transform_factory::initialize(); }); - finalizer_functions.push_back([] { filter::transform::transform_factory::finalize(); }); -} - static std::shared_ptr factory_instance = nullptr; void filter::transform::transform_factory::initialize() diff --git a/source/gfx/gfx-effect-source.cpp b/source/gfx/gfx-effect-source.cpp index c40f2493..ff2eb651 100644 --- a/source/gfx/gfx-effect-source.cpp +++ b/source/gfx/gfx-effect-source.cpp @@ -1087,7 +1087,7 @@ bool gfx::effect_source::effect_source::tick(float_t time) return false; } -void gfx::effect_source::effect_source::render() +void gfx::effect_source::effect_source::render(bool is_matrix_valid) { if (!_effect) return; @@ -1129,7 +1129,6 @@ void gfx::effect_source::effect_source::render() } gs_blend_state_push(); - gs_matrix_push(); gs_reset_blend_state(); gs_enable_blending(false); @@ -1137,7 +1136,10 @@ void gfx::effect_source::effect_source::render() gs_enable_depth_test(false); gs_enable_stencil_test(false); gs_enable_stencil_write(false); - gs_ortho(0, 1, 0, 1, -1., 1.); + if (!is_matrix_valid) { + gs_matrix_push(); + gs_ortho(0, 1, 0, 1, -1., 1.); + } while (gs_effect_loop(_effect->get_object(), _tech.c_str())) { gs_load_vertexbuffer(_tri->update()); @@ -1145,7 +1147,9 @@ void gfx::effect_source::effect_source::render() gs_draw(gs_draw_mode::GS_TRIS, 0, _tri->size()); } - gs_matrix_pop(); + if (!is_matrix_valid) { + gs_matrix_pop(); + } gs_blend_state_pop(); } diff --git a/source/gfx/gfx-effect-source.hpp b/source/gfx/gfx-effect-source.hpp index 65d76917..5471cc25 100644 --- a/source/gfx/gfx-effect-source.hpp +++ b/source/gfx/gfx-effect-source.hpp @@ -314,7 +314,7 @@ namespace gfx { bool tick(float_t time); - void render(); + void render(bool is_matrix_valid = false); obs_source_t* get_self(); diff --git a/source/plugin.cpp b/source/plugin.cpp index dd5984c5..6607b5b6 100644 --- a/source/plugin.cpp +++ b/source/plugin.cpp @@ -19,12 +19,41 @@ #include "plugin.hpp" #include "obs/obs-source-tracker.hpp" +#include "filters/filter-blur.hpp" +#include "filters/filter-color-grade.hpp" +#include "filters/filter-displacement.hpp" +#include "filters/filter-dynamic-mask.hpp" +#include "filters/filter-sdf-effects.hpp" +#include "filters/filter-shader.hpp" +#include "filters/filter-transform.hpp" +#include "sources/source-mirror.hpp" +#include "sources/source-shader.hpp" std::list> initializer_functions; std::list> finalizer_functions; MODULE_EXPORT bool obs_module_load(void) { + // Previously through P_INITIALIZER. + initializer_functions.push_back([] { filter::blur::blur_factory::initialize(); }); + finalizer_functions.push_back([] { filter::blur::blur_factory::finalize(); }); + initializer_functions.push_back([] { filter::color_grade::color_grade_factory::initialize(); }); + finalizer_functions.push_back([] { filter::color_grade::color_grade_factory::finalize(); }); + initializer_functions.push_back([] { filter::displacement::displacement_factory::initialize(); }); + finalizer_functions.push_back([] { filter::displacement::displacement_factory::finalize(); }); + initializer_functions.push_back([] { filter::dynamic_mask::dynamic_mask_factory::initialize(); }); + finalizer_functions.push_back([] { filter::dynamic_mask::dynamic_mask_factory::finalize(); }); + initializer_functions.push_back([] { filter::sdf_effects::sdf_effects_factory::initialize(); }); + finalizer_functions.push_back([] { filter::sdf_effects::sdf_effects_factory::finalize(); }); + initializer_functions.push_back([] { filter::shader::shader_factory::initialize(); }); + finalizer_functions.push_back([] { filter::shader::shader_factory::finalize(); }); + initializer_functions.push_back([] { filter::transform::transform_factory::initialize(); }); + finalizer_functions.push_back([] { filter::transform::transform_factory::finalize(); }); + initializer_functions.push_back([] { source::mirror::mirror_factory::initialize(); }); + finalizer_functions.push_back([] { source::mirror::mirror_factory::finalize(); }); + initializer_functions.push_back([] { source::shader::shader_factory::initialize(); }); + finalizer_functions.push_back([] { source::shader::shader_factory::finalize(); }); + P_LOG_INFO("Loading Version %u.%u.%u (Build %u)", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_VERSION_PATCH, PROJECT_VERSION_TWEAK); obs::source_tracker::initialize(); diff --git a/source/sources/source-mirror.cpp b/source/sources/source-mirror.cpp index a2c435ba..04beed26 100644 --- a/source/sources/source-mirror.cpp +++ b/source/sources/source-mirror.cpp @@ -60,13 +60,6 @@ #define ST_SCALING_BOUNDS_FILLHEIGHT "Source.Mirror.Scaling.Bounds.FillHeight" #define ST_SCALING_ALIGNMENT "Source.Mirror.Scaling.Alignment" -// Initializer & Finalizer -P_INITIALIZER(SourceMirrorInit) -{ - initializer_functions.push_back([] { source::mirror::mirror_factory::initialize(); }); - finalizer_functions.push_back([] { source::mirror::mirror_factory::finalize(); }); -} - static std::shared_ptr factory_instance = nullptr; void source::mirror::mirror_factory::initialize() diff --git a/source/sources/source-shader.cpp b/source/sources/source-shader.cpp index 7bcefe35..c1771b2f 100644 --- a/source/sources/source-shader.cpp +++ b/source/sources/source-shader.cpp @@ -25,12 +25,6 @@ #define ST_WIDTH ST ".Width" #define ST_HEIGHT ST ".Height" -P_INITIALIZER(SourceShaderInit) -{ - initializer_functions.push_back([] { source::shader::shader_factory::initialize(); }); - finalizer_functions.push_back([] { source::shader::shader_factory::finalize(); }); -} - static std::shared_ptr factory_instance = nullptr; void source::shader::shader_factory::initialize() diff --git a/source/utility.hpp b/source/utility.hpp index a17b027a..50e9da9a 100644 --- a/source/utility.hpp +++ b/source/utility.hpp @@ -57,34 +57,6 @@ typename std::enable_if::enable, Enum>::type oper #define D_STR(s) #s #define D_VSTR(s) D_STR(s) -#ifdef __cplusplus -#define P_INITIALIZER(f) \ - static void f(void); \ - struct f##_t_ { \ - f##_t_(void) \ - { \ - f(); \ - } \ - }; \ - static f##_t_ f##_; \ - static void f(void) -#elif defined(_MSC_VER) -#pragma section(".CRT$XCU", read) -#define INITIALIZER2_(f, p) \ - static void f(void); \ - __declspec(allocate(".CRT$XCU")) void (*f##_)(void) = f; \ - __pragma(comment(linker, "/include:" p #f "_")) static void f(void) -#ifdef _WIN64 -#define P_INITIALIZER(f) INITIALIZER2_(f, "") -#else -#define P_INITIALIZER(f) INITIALIZER2_(f, "_") -#endif -#else -#define P_INITIALIZER(f) \ - static void f(void) __attribute__((constructor)); \ - static void f(void) -#endif - namespace util { bool inline are_property_groups_broken() {