From c0f131642b00aa2165f51a25418e46ce9032fe86 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Wed, 18 Dec 2019 06:35:56 +0100 Subject: [PATCH] gs-effect-parameter: Improve API and fix incorrect functions --- source/obs/gs/gs-effect-parameter.cpp | 35 ++++------- source/obs/gs/gs-effect-parameter.hpp | 86 +++++++++++++++++++++++---- 2 files changed, 88 insertions(+), 33 deletions(-) diff --git a/source/obs/gs/gs-effect-parameter.cpp b/source/obs/gs/gs-effect-parameter.cpp index 73d5aea5..8223d355 100644 --- a/source/obs/gs/gs-effect-parameter.cpp +++ b/source/obs/gs/gs-effect-parameter.cpp @@ -25,6 +25,11 @@ #include #include +gs::effect_parameter::effect_parameter() : _effect_parent(nullptr), _pass_parent(nullptr), _param_parent(nullptr) +{ + reset(); +} + gs::effect_parameter::effect_parameter(gs_eparam_t* param) : _effect_parent(nullptr), _pass_parent(nullptr), _param_parent(nullptr) { @@ -53,7 +58,9 @@ gs::effect_parameter::~effect_parameter() {} std::string gs::effect_parameter::get_name() { - return std::string(get()->name, get()->name + strnlen_s(get()->name, 256)); + const char* name_c = get()->name; + size_t name_len = strnlen_s(name_c, 256); + return name_c ? std::string(name_c, name_c + name_len) : std::string(); } gs::effect_parameter::type gs::effect_parameter::get_type() @@ -94,21 +101,21 @@ inline size_t gs::effect_parameter::count_annotations() return gs_param_get_num_annotations(get()); } -std::shared_ptr gs::effect_parameter::get_annotation(size_t idx) +gs::effect_parameter gs::effect_parameter::get_annotation(size_t idx) { if (idx >= get()->annotations.num) { return nullptr; } - return std::make_shared(get()->annotations.array + idx, this); + return effect_parameter(get()->annotations.array + idx, this); } -std::shared_ptr gs::effect_parameter::get_annotation(std::string name) +gs::effect_parameter gs::effect_parameter::get_annotation(std::string name) { for (size_t idx = 0; idx < get()->annotations.num; idx++) { auto ptr = get()->annotations.array + idx; if (strcmp(ptr->name, name.c_str()) == 0) { - return std::make_shared(ptr, this); + return gs::effect_parameter(ptr, this); } } @@ -127,7 +134,7 @@ bool gs::effect_parameter::has_annotation(std::string name, effect_parameter::ty { auto eprm = get_annotation(name); if (eprm) - return eprm->get_type() == type; + return eprm.get_type() == type; return false; } @@ -369,14 +376,6 @@ void gs::effect_parameter::get_default_float4(float_t& x, float_t& y, float_t& z } } -void gs::effect_parameter::set_float_array(float_t v[], size_t sz) -{ - if ((get_type() != type::Float) && (get_type() != type::Float2) && (get_type() != type::Float3) - && (get_type() != type::Float4)) - throw std::bad_cast(); - gs_effect_set_val(get(), v, sizeof(float_t) * sz); -} - void gs::effect_parameter::set_int(int32_t x) { if ((get_type() != type::Integer) && (get_type() != type::Unknown)) @@ -524,14 +523,6 @@ void gs::effect_parameter::get_default_int4(int32_t& x, int32_t& y, int32_t& z, } } -void gs::effect_parameter::set_int_array(int32_t v[], size_t sz) -{ - if ((get_type() != type::Integer) && (get_type() != type::Integer2) && (get_type() != type::Integer3) - && (get_type() != type::Integer4) && (get_type() != type::Unknown)) - throw std::bad_cast(); - gs_effect_set_val(get(), v, sizeof(int) * sz); -} - void gs::effect_parameter::set_matrix(matrix4 const& v) { if (get_type() != type::Matrix) diff --git a/source/obs/gs/gs-effect-parameter.hpp b/source/obs/gs/gs-effect-parameter.hpp index 8a301ab1..559fa7d2 100644 --- a/source/obs/gs/gs-effect-parameter.hpp +++ b/source/obs/gs/gs-effect-parameter.hpp @@ -32,6 +32,7 @@ extern "C" { #endif #include #include +#include #ifdef _MSC_VER #pragma warning(pop) #endif @@ -63,6 +64,7 @@ namespace gs { }; public: + effect_parameter(); effect_parameter(gs_eparam_t* param); effect_parameter(gs_eparam_t* param, std::shared_ptr* parent); effect_parameter(gs_eparam_t* param, std::shared_ptr* parent); @@ -73,13 +75,79 @@ namespace gs { type get_type(); - size_t count_annotations(); - std::shared_ptr get_annotation(size_t idx); - std::shared_ptr get_annotation(std::string name); - bool has_annotation(std::string name); - bool has_annotation(std::string name, effect_parameter::type type); + size_t count_annotations(); + effect_parameter get_annotation(size_t idx); + effect_parameter get_annotation(std::string name); + bool has_annotation(std::string name); + bool has_annotation(std::string name, effect_parameter::type type); - public /*value*/: + public /* Memory API */: + size_t get_default_value_size_in_bytes() + { + return gs_effect_get_default_val_size(get()); + } + + template + size_t get_default_value_size() + { + return gs_effect_get_default_val_size(get()) / sizeof(T); + } + + template + bool get_default_value(T v[], size_t len) + { + if (len != get_default_value_size()) { + return false; + } + + if (T* ptr = reinterpret_cast(gs_effect_get_default_val(get())); ptr != nullptr) { + for (size_t idx = 0; idx < len; idx++) { + v[idx] = *(ptr + idx); + } + + bfree(ptr); + return true; + } + return false; + } + + size_t get_value_size_in_bytes() + { + return gs_effect_get_val_size(get()); + } + + template + size_t get_value_size() + { + return gs_effect_get_val_size(get()) / sizeof(T); + } + + template + bool get_value(T v[], size_t len) + { + if (len != get_value_size()) { + return false; + } + + if (T* ptr = reinterpret_cast(gs_effect_get_val(get())); ptr != nullptr) { + for (size_t idx = 0; idx < len; idx++) { + v[idx] = *(ptr + idx); + } + + bfree(ptr); + return true; + } + return false; + } + + template + bool set_value(T v[], size_t len) + { + gs_effect_set_val(get(), v, sizeof(T) * len); + return true; + } + + public /* Value API */: void set_bool(bool v); void get_bool(bool& v); void get_default_bool(bool& v); @@ -111,8 +179,6 @@ namespace gs { void get_float4(float_t& x, float_t& y, float_t& z, float_t& w); void get_default_float4(float_t& x, float_t& y, float_t& z, float_t& w); - void set_float_array(float_t v[], size_t sz); - void set_int(int32_t x); void get_int(int32_t& x); void get_default_int(int32_t& x); @@ -129,8 +195,6 @@ namespace gs { void get_int4(int32_t& x, int32_t& y, int32_t& z, int32_t& w); void get_default_int4(int32_t& x, int32_t& y, int32_t& z, int32_t& w); - void set_int_array(int32_t v[], size_t sz); - void set_matrix(matrix4 const& v); void get_matrix(matrix4& v); void get_default_matrix(matrix4& v); @@ -144,7 +208,7 @@ namespace gs { void set_string(std::string const& v); void get_string(std::string& v); void get_default_string(std::string& v); - + public /* Helpers */: inline float_t get_bool() {