diff --git a/CMakeLists.txt b/CMakeLists.txt index 8cfe5cc..527b091 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1061,6 +1061,8 @@ if(HAVE_NVIDIA_AR_SDK) list(APPEND PROJECT_PRIVATE_SOURCE "source/nvidia/ar/nvidia-ar.hpp" "source/nvidia/ar/nvidia-ar.cpp" + "source/nvidia/ar/nvidia-ar-feature.hpp" + "source/nvidia/ar/nvidia-ar-feature.cpp" ) list(APPEND PROJECT_LIBRARIES NVIDIA::AR diff --git a/source/nvidia/ar/nvidia-ar-feature.cpp b/source/nvidia/ar/nvidia-ar-feature.cpp new file mode 100644 index 0000000..76ac243 --- /dev/null +++ b/source/nvidia/ar/nvidia-ar-feature.cpp @@ -0,0 +1,93 @@ +// Copyright (c) 2021 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-ar-feature.hpp" +#include "obs/gs/gs-helper.hpp" +#include "util/util-logging.hpp" +#include "util/util-platform.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 + +streamfx::nvidia::ar::feature::~feature() +{ + D_LOG_DEBUG("Finalizing... (Addr: 0x%" PRIuPTR ")", this); +} + +streamfx::nvidia::ar::feature::feature(feature_t feature) + : _nvcuda(::streamfx::nvidia::cuda::obs::get()), _nvcv(::streamfx::nvidia::cv::cv::get()), + _nvar(::streamfx::nvidia::ar::ar::get()), _fx() +{ + D_LOG_DEBUG("Initializating... (Addr: 0x%" PRIuPTR ")", this); + auto gctx = ::streamfx::obs::gs::context(); + auto cctx = cuda::obs::get()->get_context()->enter(); + + // Create the Effect/Feature. + ::streamfx::nvidia::ar::handle_t handle; + if (cv::result res = _nvar->NvAR_Create(feature, &handle); res != cv::result::SUCCESS) { + throw cv::exception("Failed to create feature.", res); + } + _fx = + std::shared_ptr(handle, [this](::streamfx::nvidia::ar::handle_t handle) { _nvar->NvAR_Destroy(handle); }); + + // Set CUDA stream and model directory. + set(P_NVAR_CONFIG "CUDAStream", _nvcuda->get_stream()); + _model_path = _nvar->get_model_path().generic_u8string(); + set(P_NVAR_CONFIG "ModelDir", _model_path); +} + +streamfx::nvidia::cv::result streamfx::nvidia::ar::feature::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; +} + +streamfx::nvidia::cv::result streamfx::nvidia::ar::feature::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/ar/nvidia-ar-feature.hpp b/source/nvidia/ar/nvidia-ar-feature.hpp new file mode 100644 index 0000000..773591e --- /dev/null +++ b/source/nvidia/ar/nvidia-ar-feature.hpp @@ -0,0 +1,205 @@ +// Copyright (c) 2021 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 "nvidia/ar/nvidia-ar.hpp" +#include "nvidia/cuda/nvidia-cuda-obs.hpp" +#include "nvidia/cv/nvidia-cv-image.hpp" +#include "nvidia/cv/nvidia-cv-texture.hpp" +#include "nvidia/cv/nvidia-cv.hpp" + +namespace streamfx::nvidia::ar { + class feature { + protected: + std::shared_ptr<::streamfx::nvidia::cuda::obs> _nvcuda; + std::shared_ptr<::streamfx::nvidia::cv::cv> _nvcv; + std::shared_ptr<::streamfx::nvidia::ar::ar> _nvar; + std::shared_ptr _fx; + std::string _model_path; + + public: + ~feature(); + feature(feature_t feature); + + ::streamfx::nvidia::ar::handle_t get() + { + return _fx.get(); + } + + public /* Int32 */: + inline cv::result set(parameter_t param, uint32_t const value) + { + return _nvar->NvAR_SetU32(_fx.get(), param, value); + } + inline cv::result get(parameter_t param, uint32_t* value) + { + return _nvar->NvAR_GetU32(_fx.get(), param, value); + } + + inline cv::result set(parameter_t param, int32_t const value) + { + return _nvar->NvAR_SetS32(_fx.get(), param, value); + } + inline cv::result get(parameter_t param, int32_t* value) + { + return _nvar->NvAR_GetS32(_fx.get(), param, value); + } + + public /* Int64 */: + inline cv::result set(parameter_t param, uint64_t const value) + { + return _nvar->NvAR_SetU64(_fx.get(), param, value); + } + inline cv::result get(parameter_t param, uint64_t* value) + { + return _nvar->NvAR_GetU64(_fx.get(), param, value); + } + + public /* Float32 */: + inline cv::result set(parameter_t param, float const value) + { + return _nvar->NvAR_SetF32(_fx.get(), param, value); + } + inline cv::result get(parameter_t param, float* value) + { + return _nvar->NvAR_GetF32(_fx.get(), param, value); + } + + inline cv::result set(parameter_t param, float* const value, int32_t size) + { + return _nvar->NvAR_SetF32Array(_fx.get(), param, value, static_cast(size)); + } + inline cv::result get(parameter_t param, const float* value, int32_t size) + { + return _nvar->NvAR_GetF32Array(_fx.get(), param, &value, &size); + } + + inline cv::result set(parameter_t param, std::vector const& value) + { + return _nvar->NvAR_SetF32Array(_fx.get(), param, value.data(), static_cast(value.size())); + } + inline cv::result get(parameter_t param, std::vector& value) + { + const float* data; + int32_t size; + cv::result result; + + result = _nvar->NvAR_GetF32Array(_fx.get(), param, &data, &size); + + value.resize(size); + memcpy(value.data(), data, size * sizeof(float)); + + return result; + } + + public /* Float64 */: + inline cv::result set(parameter_t param, double const value) + { + return _nvar->NvAR_SetF64(_fx.get(), param, value); + } + inline cv::result get(parameter_t param, double* value) + { + return _nvar->NvAR_GetF64(_fx.get(), param, value); + } + + public /* String */: + inline cv::result set(parameter_t param, const char* const value) + { + return _nvar->NvAR_SetString(_fx.get(), param, value); + }; + inline cv::result get(parameter_t param, const char*& value) + { + return _nvar->NvAR_GetString(_fx.get(), param, &value); + }; + + inline cv::result set(parameter_t param, std::string_view const value) + { + return _nvar->NvAR_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 const& value) + { + return _nvar->NvAR_SetString(_fx.get(), param, value.c_str()); + }; + cv::result get(parameter_t param, std::string& value); + + public /* CUDA Stream */: + inline cv::result set(parameter_t param, cuda::stream_t const value) + { + return _nvar->NvAR_SetCudaStream(_fx.get(), param, value); + }; + inline cv::result get(parameter_t param, cuda::stream_t& value) + { + return _nvar->NvAR_GetCudaStream(_fx.get(), param, &value); + }; + + inline cv::result set(parameter_t param, std::shared_ptr<::streamfx::nvidia::cuda::stream> const value) + { + return _nvar->NvAR_SetCudaStream(_fx.get(), param, value->get()); + } + //inline cv::result get(parameter_t param, std::shared_ptr<::streamfx::nvidia::cuda::stream> value); + + public /* CV Image */: + inline cv::result set(parameter_t param, cv::image_t& value) + { + return _nvar->NvAR_SetObject(_fx.get(), param, &value, sizeof(cv::image_t)); + }; + inline cv::result get(parameter_t param, cv::image_t*& value) + { + return _nvar->NvAR_GetObject(_fx.get(), param, reinterpret_cast(&value), sizeof(cv::image_t)); + }; + + inline cv::result set(parameter_t param, std::shared_ptr const value) + { + return _nvar->NvAR_SetObject(_fx.get(), param, value->get_image(), sizeof(cv::image_t)); + }; + //inline cv::result get(parameter_t param, std::shared_ptr& value); + + public /* CV Texture */: + inline cv::result set(parameter_t param, std::shared_ptr const value) + { + return _nvar->NvAR_SetObject(_fx.get(), param, value->get_image(), sizeof(cv::image_t)); + }; + //inline cv::result get(parameter_t param, std::shared_ptr& value); + + public /* Objects */: + inline cv::result set_object(parameter_t param, void* const data, size_t size) + { + return _nvar->NvAR_SetObject(_fx.get(), param, data, static_cast(size)); + } + inline cv::result get_object(parameter_t param, void*& data, size_t size) + { + return _nvar->NvAR_GetObject(_fx.get(), param, &data, static_cast(size)); + } + + public /* Control */: + inline cv::result load() + { + return _nvar->NvAR_Load(_fx.get()); + } + + inline cv::result run() + { + return _nvar->NvAR_Run(_fx.get()); + } + }; +} // namespace streamfx::nvidia::ar