From db30786a759c31b97cc14667e198687515ff7ed3 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sun, 4 Jul 2021 03:56:20 +0200 Subject: [PATCH] nvidia/vfx/effect: Implement managed effect wrapper Slightly simplifies interaction with effects. --- CMakeLists.txt | 2 + source/nvidia/vfx/nvidia-vfx-effect.cpp | 94 +++++++++++++ source/nvidia/vfx/nvidia-vfx-effect.hpp | 173 ++++++++++++++++++++++++ 3 files changed, 269 insertions(+) create mode 100644 source/nvidia/vfx/nvidia-vfx-effect.cpp create mode 100644 source/nvidia/vfx/nvidia-vfx-effect.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index edeacc47..92692665 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1040,6 +1040,8 @@ if(HAVE_NVIDIA_VFX_SDK) list(APPEND PROJECT_PRIVATE_SOURCE "source/nvidia/vfx/nvidia-vfx.hpp" "source/nvidia/vfx/nvidia-vfx.cpp" + "source/nvidia/vfx/nvidia-vfx-effect.hpp" + "source/nvidia/vfx/nvidia-vfx-effect.cpp" "source/nvidia/vfx/nvidia-vfx-superresolution.hpp" "source/nvidia/vfx/nvidia-vfx-superresolution.cpp" ) diff --git a/source/nvidia/vfx/nvidia-vfx-effect.cpp b/source/nvidia/vfx/nvidia-vfx-effect.cpp new file mode 100644 index 00000000..2e691005 --- /dev/null +++ b/source/nvidia/vfx/nvidia-vfx-effect.cpp @@ -0,0 +1,94 @@ +// Copyright (c) 2020 Michael Fabian Dirks +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include "nvidia-vfx-effect.hpp" +#include "obs/gs/gs-helper.hpp" +#include "util/util-logging.hpp" + +#ifdef _DEBUG +#define ST_PREFIX "<%s> " +#define D_LOG_ERROR(x, ...) P_LOG_ERROR(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__) +#define D_LOG_WARNING(x, ...) P_LOG_WARN(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__) +#define D_LOG_INFO(x, ...) P_LOG_INFO(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__) +#define D_LOG_DEBUG(x, ...) P_LOG_DEBUG(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__) +#else +#define ST_PREFIX " " +#define D_LOG_ERROR(...) P_LOG_ERROR(ST_PREFIX __VA_ARGS__) +#define D_LOG_WARNING(...) P_LOG_WARN(ST_PREFIX __VA_ARGS__) +#define D_LOG_INFO(...) P_LOG_INFO(ST_PREFIX __VA_ARGS__) +#define D_LOG_DEBUG(...) P_LOG_DEBUG(ST_PREFIX __VA_ARGS__) +#endif + +using namespace ::streamfx::nvidia; + +vfx::effect::~effect() +{ + auto gctx = ::streamfx::obs::gs::context(); + auto cctx = cuda::obs::get()->get_context()->enter(); + + _fx.reset(); + _nvvfx.reset(); + _nvcvi.reset(); + _nvcuda.reset(); +} + +vfx::effect::effect(effect_t effect) + : _nvcuda(cuda::obs::get()), _nvcvi(cv::cv::get()), _nvvfx(vfx::vfx::get()), _fx(), _fx_dirty(true) +{ + auto gctx = ::streamfx::obs::gs::context(); + auto cctx = cuda::obs::get()->get_context()->enter(); + + // Create the Effect/Feature. + ::vfx::handle_t handle; + if (cv::result res = _nvvfx->NvVFX_CreateEffect(effect, &handle); res != cv::result::SUCCESS) { + D_LOG_ERROR("Unable to create effect: %s", _nvcvi->NvCV_GetErrorStringFromCode(res)); + throw std::runtime_error("Unable to create effect."); + } + + _fx = std::shared_ptr(handle, [](::vfx::handle_t handle) { ::vfx::vfx::get()->NvVFX_DestroyEffect(handle); }); +} + +cv::result vfx::effect::get(parameter_t param, std::string_view& value) +{ + const char* cvalue = nullptr; + cv::result res = get(param, cvalue); + if (res == cv::result::SUCCESS) { + if (cvalue) { + value.swap(std::string_view(cvalue)); + } else { + value.swap(std::string_view()); + } + } + return res; +} + +cv::result vfx::effect::get(parameter_t param, std::string& value) +{ + const char* cvalue = nullptr; + cv::result res = get(param, cvalue); + if (res == cv::result::SUCCESS) { + if (cvalue) { + value = cvalue; + } else { + value.clear(); + } + } + return res; +} diff --git a/source/nvidia/vfx/nvidia-vfx-effect.hpp b/source/nvidia/vfx/nvidia-vfx-effect.hpp new file mode 100644 index 00000000..461ae074 --- /dev/null +++ b/source/nvidia/vfx/nvidia-vfx-effect.hpp @@ -0,0 +1,173 @@ +// Copyright (c) 2020 Michael Fabian Dirks +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#pragma once +#include +#include +#include +#include "nvidia-vfx.hpp" +#include "nvidia/cuda/nvidia-cuda-obs.hpp" +#include "nvidia/cuda/nvidia-cuda-stream.hpp" +#include "nvidia/cuda/nvidia-cuda.hpp" +#include "nvidia/cv/nvidia-cv-image.hpp" +#include "nvidia/cv/nvidia-cv-texture.hpp" +#include "nvidia/cv/nvidia-cv.hpp" +#include "nvidia/vfx/nvidia-vfx.hpp" + +namespace streamfx::nvidia::vfx { + using namespace ::streamfx::nvidia; + + class effect { + std::shared_ptr _nvcuda; + std::shared_ptr _nvcvi; + std::shared_ptr _nvvfx; + + std::shared_ptr _fx; + bool _fx_dirty; + + public: + ~effect(); + effect(effect_t name); + + inline cv::result set(parameter_t param, uint32_t value) + { + return _nvvfx->NvVFX_SetU32(_fx.get(), param, value); + }; + inline cv::result get(parameter_t param, uint32_t& value) + { + return _nvvfx->NvVFX_GetU32(_fx.get(), param, &value); + }; + + inline cv::result set(parameter_t param, int32_t value) + { + return _nvvfx->NvVFX_SetS32(_fx.get(), param, value); + }; + inline cv::result get(parameter_t param, int32_t& value) + { + return _nvvfx->NvVFX_GetS32(_fx.get(), param, &value); + }; + + inline cv::result set(parameter_t param, uint64_t value) + { + return _nvvfx->NvVFX_SetU64(_fx.get(), param, value); + }; + inline cv::result get(parameter_t param, uint64_t& value) + { + return _nvvfx->NvVFX_GetU64(_fx.get(), param, &value); + }; + + inline cv::result set(parameter_t param, float value) + { + return _nvvfx->NvVFX_SetF32(_fx.get(), param, value); + }; + inline cv::result get(parameter_t param, float& value) + { + return _nvvfx->NvVFX_GetF32(_fx.get(), param, &value); + }; + + inline cv::result set(parameter_t param, double value) + { + return _nvvfx->NvVFX_SetF64(_fx.get(), param, value); + }; + inline cv::result get(parameter_t param, double& value) + { + return _nvvfx->NvVFX_GetF64(_fx.get(), param, &value); + }; + + inline cv::result set(parameter_t param, const char* value) + { + return _nvvfx->NvVFX_SetString(_fx.get(), param, value); + }; + inline cv::result get(parameter_t param, const char*& value) + { + return _nvvfx->NvVFX_GetString(_fx.get(), param, &value); + }; + + inline cv::result set(parameter_t param, std::string_view value) + { + return _nvvfx->NvVFX_SetString(_fx.get(), param, value.data()); + }; + cv::result get(parameter_t param, std::string_view& value); + + inline cv::result set(parameter_t param, std::string value) + { + return _nvvfx->NvVFX_SetString(_fx.get(), param, value.c_str()); + }; + cv::result get(parameter_t param, std::string& value); + + inline cv::result set_stream(parameter_t param, cuda::stream_t value) + { + return _nvvfx->NvVFX_SetCudaStream(_fx.get(), param, value); + }; + inline cv::result get_stream(parameter_t param, cuda::stream_t& value) + { + return _nvvfx->NvVFX_GetCudaStream(_fx.get(), param, &value); + }; + + inline cv::result set_stream(parameter_t param, std::shared_ptr value) + { + return _nvvfx->NvVFX_SetCudaStream(_fx.get(), param, value->get()); + }; + //cv::result get_stream(parameter_t param, std::shared_ptr& value); + + inline cv::result set_image(parameter_t param, cv::image_t& value) + { + return _nvvfx->NvVFX_SetImage(_fx.get(), param, &value); + }; + inline cv::result get_image(parameter_t param, cv::image_t& value) + { + return _nvvfx->NvVFX_GetImage(_fx.get(), param, &value); + }; + + inline cv::result set_image(parameter_t param, std::shared_ptr value) + { + return _nvvfx->NvVFX_SetImage(_fx.get(), param, value->get_image()); + }; + inline cv::result get_image(parameter_t param, std::shared_ptr& value) + { + return _nvvfx->NvVFX_GetImage(_fx.get(), param, value->get_image()); + }; + + inline cv::result set_image(parameter_t param, std::shared_ptr value) + { + return _nvvfx->NvVFX_SetImage(_fx.get(), param, value->get_image()); + }; + //cv::result get(parameter_t param, std::shared_ptr& value); + + inline cv::result set_object(parameter_t param, void* value) + { + return _nvvfx->NvVFX_SetObject(_fx.get(), param, value); + }; + inline cv::result get_object(parameter_t param, void*& value) + { + return _nvvfx->NvVFX_GetObject(_fx.get(), param, &value); + }; + + inline cv::result load() + { + return _nvvfx->NvVFX_Load(_fx.get()); + }; + + inline cv::result run(bool async = false) + { + return _nvvfx->NvVFX_Run(_fx.get(), async ? 0 : 1); + }; + }; +} // namespace streamfx::nvidia::vfx