From 50ac6893bf02e2a19f3f734c575d739dff3f109e Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sun, 15 Dec 2019 10:35:29 +0100 Subject: [PATCH] gs-effect-parameter: Refactor effect_parameter onto std::shared_ptr Prevents problems further down the line and supports multiple types of parents to ensure that lifetime is guaranteed. --- CMakeLists.txt | 2 + source/obs/gs/gs-effect-parameter.cpp | 131 ++++++++++++++++++++++++++ source/obs/gs/gs-effect-parameter.hpp | 69 ++++++++++++++ 3 files changed, 202 insertions(+) create mode 100644 source/obs/gs/gs-effect-parameter.cpp create mode 100644 source/obs/gs/gs-effect-parameter.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b17b3756..3d9e1871 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -328,6 +328,8 @@ set(PROJECT_PRIVATE_SOURCE "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-helper.cpp" "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-effect.hpp" "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-effect.cpp" + "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-effect-parameter.hpp" + "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-effect-parameter.cpp" "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-effect-pass.hpp" "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-effect-pass.cpp" "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-effect-technique.hpp" diff --git a/source/obs/gs/gs-effect-parameter.cpp b/source/obs/gs/gs-effect-parameter.cpp new file mode 100644 index 00000000..d73de01b --- /dev/null +++ b/source/obs/gs/gs-effect-parameter.cpp @@ -0,0 +1,131 @@ +/* + * 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 "gs-effect-parameter.hpp" +#include +#include +#include "gs-effect-pass.hpp" + +#include + +gs::effect_parameter::effect_parameter(gs_eparam_t* param) + : _effect_parent(nullptr), _pass_parent(nullptr), _param_parent(nullptr) +{ + reset(param, [](void*) {}); +} + +gs::effect_parameter::effect_parameter(gs_eparam_t* param, std::shared_ptr* parent) + : effect_parameter(param) +{ + _effect_parent = parent; +} + +gs::effect_parameter::effect_parameter(gs_eparam_t* param, std::shared_ptr* parent) + : effect_parameter(param) +{ + _pass_parent = parent; +} + +gs::effect_parameter::effect_parameter(gs_eparam_t* param, std::shared_ptr* parent) + : effect_parameter(param) +{ + _param_parent = parent; +} + +gs::effect_parameter::~effect_parameter() {} + +std::string gs::effect_parameter::get_name() +{ + return std::string(get()->name, get()->name + strnlen_s(get()->name, 256)); +} + +gs::effect_parameter::type gs::effect_parameter::get_type() +{ + switch (get()->type) { + case GS_SHADER_PARAM_BOOL: + return type::Boolean; + case GS_SHADER_PARAM_FLOAT: + return type::Float; + case GS_SHADER_PARAM_VEC2: + return type::Float2; + case GS_SHADER_PARAM_VEC3: + return type::Float3; + case GS_SHADER_PARAM_VEC4: + return type::Float4; + case GS_SHADER_PARAM_INT: + return type::Integer; + case GS_SHADER_PARAM_INT2: + return type::Integer2; + case GS_SHADER_PARAM_INT3: + return type::Integer3; + case GS_SHADER_PARAM_INT4: + return type::Integer4; + case GS_SHADER_PARAM_MATRIX4X4: + return type::Matrix; + case GS_SHADER_PARAM_TEXTURE: + return type::Texture; + case GS_SHADER_PARAM_STRING: + return type::String; + default: + case GS_SHADER_PARAM_UNKNOWN: + return type::Unknown; + } +} + +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) +{ + if (idx >= get()->annotations.num) { + return nullptr; + } + + return std::make_shared(get()->annotations.array + idx, this); +} + +std::shared_ptr 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 nullptr; +} + +bool gs::effect_parameter::has_annotation(std::string name) +{ + auto eprm = get_annotation(name); + if (eprm) + return true; + return false; +} + +bool gs::effect_parameter::has_annotation(std::string name, effect_parameter::type type) +{ + auto eprm = get_annotation(name); + if (eprm) + return eprm->get_type() == type; + return false; +} diff --git a/source/obs/gs/gs-effect-parameter.hpp b/source/obs/gs/gs-effect-parameter.hpp new file mode 100644 index 00000000..6f7414e4 --- /dev/null +++ b/source/obs/gs/gs-effect-parameter.hpp @@ -0,0 +1,69 @@ +/* + * 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 +#include +#include + +#include + +namespace gs { + class effect_parameter : protected std::shared_ptr { + std::shared_ptr* _effect_parent; + std::shared_ptr* _pass_parent; + std::shared_ptr* _param_parent; + + public: + enum class type { + Unknown, + Boolean, + Float, + Float2, + Float3, + Float4, + Integer, + Integer2, + Integer3, + Integer4, + Matrix, + String, + Texture, + + Invalid = -1, + }; + + public: + 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); + effect_parameter(gs_eparam_t* param, std::shared_ptr* parent); + ~effect_parameter(); + + std::string get_name(); + + 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); + }; +} // namespace gs