From 738b08de36cf5cc51aa6dcd3de0d016331eea05a Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sun, 15 Dec 2019 09:05:23 +0100 Subject: [PATCH] gs-effect-pass/technique: Implement wrapper code based on std::shared_ptr These two wrap the underlying gs_epass and gs_effect_technique objects, to allow direct and improved access to them without relying on the libobs API to provide this access for us. Additionally these make it safe for us to use them instead of relying on C-like code to deal with it. --- CMakeLists.txt | 4 ++ source/obs/gs/gs-effect-pass.cpp | 45 +++++++++++++++++ source/obs/gs/gs-effect-pass.hpp | 52 +++++++++++++++++++ source/obs/gs/gs-effect-technique.cpp | 72 +++++++++++++++++++++++++++ source/obs/gs/gs-effect-technique.hpp | 44 ++++++++++++++++ 5 files changed, 217 insertions(+) create mode 100644 source/obs/gs/gs-effect-pass.cpp create mode 100644 source/obs/gs/gs-effect-pass.hpp create mode 100644 source/obs/gs/gs-effect-technique.cpp create mode 100644 source/obs/gs/gs-effect-technique.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b5812aa..bc46cf28 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -317,6 +317,10 @@ 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-pass.hpp" + "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-effect-pass.cpp" + "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-effect-technique.hpp" + "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-effect-technique.cpp" "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-indexbuffer.hpp" "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-indexbuffer.cpp" "${PROJECT_SOURCE_DIR}/source/obs/gs/gs-limits.hpp" diff --git a/source/obs/gs/gs-effect-pass.cpp b/source/obs/gs/gs-effect-pass.cpp new file mode 100644 index 00000000..dbca7f22 --- /dev/null +++ b/source/obs/gs/gs-effect-pass.cpp @@ -0,0 +1,45 @@ +/* + * 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-pass.hpp" +#include + +#include + +gs::effect_pass::effect_pass(gs_epass_t* pass, std::shared_ptr* parent) : _parent(parent) +{ + reset(pass, [](void*) {}); +} + +gs::effect_pass::~effect_pass() {} + +std::string gs::effect_pass::name() +{ + return std::string(get()->name, get()->name + strnlen_s(get()->name, 256)); +} + +size_t gs::effect_pass::count_vertex_parameters() +{ + return static_cast(get()->vertshader_params.num); +} + +size_t gs::effect_pass::count_pixel_parameters() +{ + return static_cast(get()->pixelshader_params.num); +} diff --git a/source/obs/gs/gs-effect-pass.hpp b/source/obs/gs/gs-effect-pass.hpp new file mode 100644 index 00000000..5c0ea854 --- /dev/null +++ b/source/obs/gs/gs-effect-pass.hpp @@ -0,0 +1,52 @@ +/* + * 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_pass : public std::shared_ptr { + std::shared_ptr* _parent; + + public: + effect_pass(gs_epass_t* pass, std::shared_ptr* parent = nullptr); + ~effect_pass(); + + std::string name(); + + //gs::shader get_pixel_shader(); + //gs::shader get_vertex_shader(); + + size_t count_vertex_parameters(); + //gs::parameter get_vertex_parameter(size_t idx); + //gs::parameter get_vertex_parameter(std::string name); + //bool has_vertex_parameter(std::string name); + //bool has_vertex_parameter(std::string name, ... type); + + size_t count_pixel_parameters(); + //gs::parameter get_vertex_parameter(size_t idx); + //gs::parameter get_vertex_parameter(std::string name); + //bool has_vertex_parameter(std::string name); + //bool has_vertex_parameter(std::string name, ... type); + }; +} // namespace gs diff --git a/source/obs/gs/gs-effect-technique.cpp b/source/obs/gs/gs-effect-technique.cpp new file mode 100644 index 00000000..c25c31d9 --- /dev/null +++ b/source/obs/gs/gs-effect-technique.cpp @@ -0,0 +1,72 @@ +/* + * 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-technique.hpp" +#include +#include + +#include + +gs::effect_technique::effect_technique(gs_technique_t* technique, std::shared_ptr* parent) : _parent(parent) +{ + reset(technique, [](void*) {}); +} + +gs::effect_technique::~effect_technique() {} + +std::string gs::effect_technique::name() +{ + + return std::string(get()->name, get()->name + strnlen_s(get()->name, 256)); +} + +size_t gs::effect_technique::count_passes() +{ + return static_cast(get()->passes.num); +} + +gs::effect_pass gs::effect_technique::get_pass(size_t idx) +{ + if (idx >= get()->passes.num) { + throw std::out_of_range("Index is out of range."); + } + + return gs::effect_pass(get()->passes.array + idx, this); +} + +gs::effect_pass gs::effect_technique::get_pass(std::string name) +{ + for (size_t idx = 0; idx < get()->passes.num; idx++) { + auto ptr = get()->passes.array + idx; + if (strcmp(ptr->name, name.c_str()) == 0) + return gs::effect_pass(ptr, this); + } + + throw std::invalid_argument("Pass with given name does not exist."); +} + +bool gs::effect_technique::has_pass(std::string name) +{ + for (size_t idx = 0; idx < get()->passes.num; idx++) { + auto ptr = get()->passes.array + idx; + if (strcmp(ptr->name, name.c_str()) == 0) + return true; + } + return false; +} diff --git a/source/obs/gs/gs-effect-technique.hpp b/source/obs/gs/gs-effect-technique.hpp new file mode 100644 index 00000000..acc412b7 --- /dev/null +++ b/source/obs/gs/gs-effect-technique.hpp @@ -0,0 +1,44 @@ +/* + * 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 "gs-effect-pass.hpp" + +#include + +namespace gs { + class effect_technique : public std::shared_ptr { + std::shared_ptr* _parent; + + public: + effect_technique(gs_technique_t* technique, std::shared_ptr* parent = nullptr); + ~effect_technique(); + + std::string name(); + + size_t count_passes(); + gs::effect_pass get_pass(size_t idx); + gs::effect_pass get_pass(std::string name); + bool has_pass(std::string name); + + }; +} // namespace gs