gs-effect-parameter: Improve API and fix incorrect functions

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2019-12-18 06:35:56 +01:00
parent cd82e2cac7
commit c0f131642b
2 changed files with 88 additions and 33 deletions

View file

@ -25,6 +25,11 @@
#include <graphics/effect.h> #include <graphics/effect.h>
#include <graphics/vec2.h> #include <graphics/vec2.h>
gs::effect_parameter::effect_parameter() : _effect_parent(nullptr), _pass_parent(nullptr), _param_parent(nullptr)
{
reset();
}
gs::effect_parameter::effect_parameter(gs_eparam_t* param) gs::effect_parameter::effect_parameter(gs_eparam_t* param)
: _effect_parent(nullptr), _pass_parent(nullptr), _param_parent(nullptr) : _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() 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() 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()); return gs_param_get_num_annotations(get());
} }
std::shared_ptr<gs::effect_parameter> gs::effect_parameter::get_annotation(size_t idx) gs::effect_parameter gs::effect_parameter::get_annotation(size_t idx)
{ {
if (idx >= get()->annotations.num) { if (idx >= get()->annotations.num) {
return nullptr; return nullptr;
} }
return std::make_shared<effect_parameter>(get()->annotations.array + idx, this); return effect_parameter(get()->annotations.array + idx, this);
} }
std::shared_ptr<gs::effect_parameter> 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++) { for (size_t idx = 0; idx < get()->annotations.num; idx++) {
auto ptr = get()->annotations.array + idx; auto ptr = get()->annotations.array + idx;
if (strcmp(ptr->name, name.c_str()) == 0) { if (strcmp(ptr->name, name.c_str()) == 0) {
return std::make_shared<effect_parameter>(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); auto eprm = get_annotation(name);
if (eprm) if (eprm)
return eprm->get_type() == type; return eprm.get_type() == type;
return false; 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) void gs::effect_parameter::set_int(int32_t x)
{ {
if ((get_type() != type::Integer) && (get_type() != type::Unknown)) 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) void gs::effect_parameter::set_matrix(matrix4 const& v)
{ {
if (get_type() != type::Matrix) if (get_type() != type::Matrix)

View file

@ -32,6 +32,7 @@ extern "C" {
#endif #endif
#include <graphics/graphics.h> #include <graphics/graphics.h>
#include <graphics/matrix4.h> #include <graphics/matrix4.h>
#include <obs.h>
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning(pop) #pragma warning(pop)
#endif #endif
@ -63,6 +64,7 @@ namespace gs {
}; };
public: public:
effect_parameter();
effect_parameter(gs_eparam_t* param); effect_parameter(gs_eparam_t* param);
effect_parameter(gs_eparam_t* param, std::shared_ptr<gs_effect_t>* parent); effect_parameter(gs_eparam_t* param, std::shared_ptr<gs_effect_t>* parent);
effect_parameter(gs_eparam_t* param, std::shared_ptr<gs_epass_t>* parent); effect_parameter(gs_eparam_t* param, std::shared_ptr<gs_epass_t>* parent);
@ -73,13 +75,79 @@ namespace gs {
type get_type(); type get_type();
size_t count_annotations(); size_t count_annotations();
std::shared_ptr<effect_parameter> get_annotation(size_t idx); effect_parameter get_annotation(size_t idx);
std::shared_ptr<effect_parameter> get_annotation(std::string name); effect_parameter get_annotation(std::string name);
bool has_annotation(std::string name); bool has_annotation(std::string name);
bool has_annotation(std::string name, effect_parameter::type type); 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<typename T>
size_t get_default_value_size()
{
return gs_effect_get_default_val_size(get()) / sizeof(T);
}
template<typename T>
bool get_default_value(T v[], size_t len)
{
if (len != get_default_value_size<T>()) {
return false;
}
if (T* ptr = reinterpret_cast<T*>(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<typename T>
size_t get_value_size()
{
return gs_effect_get_val_size(get()) / sizeof(T);
}
template<typename T>
bool get_value(T v[], size_t len)
{
if (len != get_value_size<T>()) {
return false;
}
if (T* ptr = reinterpret_cast<T*>(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<typename T>
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 set_bool(bool v);
void get_bool(bool& v); void get_bool(bool& v);
void get_default_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_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 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 set_int(int32_t x);
void get_int(int32_t& x); void get_int(int32_t& x);
void get_default_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_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 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 set_matrix(matrix4 const& v);
void get_matrix(matrix4& v); void get_matrix(matrix4& v);
void get_default_matrix(matrix4& v); void get_default_matrix(matrix4& v);
@ -144,7 +208,7 @@ namespace gs {
void set_string(std::string const& v); void set_string(std::string const& v);
void get_string(std::string& v); void get_string(std::string& v);
void get_default_string(std::string& v); void get_default_string(std::string& v);
public /* Helpers */: public /* Helpers */:
inline float_t get_bool() inline float_t get_bool()
{ {