From 5cd1d13d5f479b9032f7cf6bd095dd6540946edf Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sun, 8 Nov 2020 06:47:47 +0100 Subject: [PATCH] nvidia/cuda/context: Merge context-stack into context header --- CMakeLists.txt | 2 - .../nvidia/cuda/nvidia-cuda-context-stack.cpp | 48 ------------------- .../nvidia/cuda/nvidia-cuda-context-stack.hpp | 34 ------------- source/nvidia/cuda/nvidia-cuda-context.cpp | 41 ++++++++++++++++ source/nvidia/cuda/nvidia-cuda-context.hpp | 26 +++++++++- 5 files changed, 66 insertions(+), 85 deletions(-) delete mode 100644 source/nvidia/cuda/nvidia-cuda-context-stack.cpp delete mode 100644 source/nvidia/cuda/nvidia-cuda-context-stack.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9eef7903..78e9fd49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -950,8 +950,6 @@ if(HAVE_NVIDIA_CUDA) "source/nvidia/cuda/nvidia-cuda.cpp" "source/nvidia/cuda/nvidia-cuda-context.hpp" "source/nvidia/cuda/nvidia-cuda-context.cpp" - "source/nvidia/cuda/nvidia-cuda-context-stack.hpp" - "source/nvidia/cuda/nvidia-cuda-context-stack.cpp" "source/nvidia/cuda/nvidia-cuda-gs-texture.hpp" "source/nvidia/cuda/nvidia-cuda-gs-texture.cpp" "source/nvidia/cuda/nvidia-cuda-memory.hpp" diff --git a/source/nvidia/cuda/nvidia-cuda-context-stack.cpp b/source/nvidia/cuda/nvidia-cuda-context-stack.cpp deleted file mode 100644 index 731056a6..00000000 --- a/source/nvidia/cuda/nvidia-cuda-context-stack.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Modern effects for a modern Streamer - * Copyright (C) 2020 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 "nvidia-cuda-context-stack.hpp" -#include - -nvidia::cuda::context_stack::context_stack(std::shared_ptr<::nvidia::cuda::cuda> cuda, - std::shared_ptr<::nvidia::cuda::context> context) - : _cuda(cuda), _ctx(context) -{ - using namespace ::nvidia::cuda; - - if (!cuda) - throw std::invalid_argument("cuda"); - if (!context) - throw std::invalid_argument("context"); - - if (result res = _cuda->cuCtxPushCurrent(_ctx->get()); res != result::SUCCESS) { - throw std::runtime_error("Failed to push context."); - } -} - -nvidia::cuda::context_stack::~context_stack() -{ - using namespace ::nvidia::cuda; - - context_t ctx; - _cuda->cuCtxGetCurrent(&ctx); - if (ctx == _ctx->get()) { - _cuda->cuCtxPopCurrent(&ctx); - } -} diff --git a/source/nvidia/cuda/nvidia-cuda-context-stack.hpp b/source/nvidia/cuda/nvidia-cuda-context-stack.hpp deleted file mode 100644 index b9d6baf4..00000000 --- a/source/nvidia/cuda/nvidia-cuda-context-stack.hpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Modern effects for a modern Streamer - * Copyright (C) 2020 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 "nvidia-cuda-context.hpp" -#include "nvidia-cuda.hpp" - -namespace nvidia::cuda { - class context_stack { - std::shared_ptr<::nvidia::cuda::cuda> _cuda; - std::shared_ptr<::nvidia::cuda::context> _ctx; - - public: - context_stack(std::shared_ptr<::nvidia::cuda::cuda> cuda, std::shared_ptr<::nvidia::cuda::context> context); - ~context_stack(); - }; -} // namespace nvidia::cuda diff --git a/source/nvidia/cuda/nvidia-cuda-context.cpp b/source/nvidia/cuda/nvidia-cuda-context.cpp index 250c4ae1..e2e0f6d9 100644 --- a/source/nvidia/cuda/nvidia-cuda-context.cpp +++ b/source/nvidia/cuda/nvidia-cuda-context.cpp @@ -18,6 +18,7 @@ */ #include "nvidia-cuda-context.hpp" +#include #include #include "util/util-logging.hpp" @@ -46,6 +47,8 @@ #endif #endif +#define ENABLE_STACK_CHECKS + nvidia::cuda::context::context() : _cuda(::nvidia::cuda::cuda::get()), _ctx(), _has_device(false), _device() { D_LOG_DEBUG("Initializating... (Addr: 0x%" PRIuPTR ")", this); @@ -93,3 +96,41 @@ nvidia::cuda::context::context(ID3D11Device* device) : context() { return _ctx; } + +std::shared_ptr<::nvidia::cuda::context_stack> nvidia::cuda::context::enter() +{ + return std::make_shared<::nvidia::cuda::context_stack>(shared_from_this()); +} + +void nvidia::cuda::context::push() +{ + if (auto res = _cuda->cuCtxPushCurrent(_ctx); res != ::nvidia::cuda::result::SUCCESS) { + throw ::nvidia::cuda::cuda_error(res); + } +} + +void nvidia::cuda::context::pop() +{ +#ifdef ENABLE_STACK_CHECKS + ::nvidia::cuda::context_t ctx; + if (_cuda->cuCtxGetCurrent(&ctx) == ::nvidia::cuda::result::SUCCESS) + assert(ctx == _ctx); +#endif + + assert(_cuda->cuCtxPopCurrent(&ctx) == ::nvidia::cuda::result::SUCCESS); +} + +void nvidia::cuda::context::synchronize() +{ + D_LOG_DEBUG("Synchronizing... (Addr: 0x%" PRIuPTR ")", this); + +#ifdef ENABLE_STACK_CHECKS + ::nvidia::cuda::context_t ctx; + if (_cuda->cuCtxGetCurrent(&ctx) == ::nvidia::cuda::result::SUCCESS) + assert(ctx == _ctx); +#endif + + if (auto res = _cuda->cuCtxSynchronize(); res != ::nvidia::cuda::result::SUCCESS) { + throw ::nvidia::cuda::cuda_error(res); + } +} diff --git a/source/nvidia/cuda/nvidia-cuda-context.hpp b/source/nvidia/cuda/nvidia-cuda-context.hpp index d43c8674..feeeafde 100644 --- a/source/nvidia/cuda/nvidia-cuda-context.hpp +++ b/source/nvidia/cuda/nvidia-cuda-context.hpp @@ -22,7 +22,9 @@ #include "nvidia-cuda.hpp" namespace nvidia::cuda { - class context { + class context_stack; + + class context : public std::enable_shared_from_this<::nvidia::cuda::context> { std::shared_ptr<::nvidia::cuda::cuda> _cuda; ::nvidia::cuda::context_t _ctx; bool _has_device; @@ -39,5 +41,27 @@ namespace nvidia::cuda { #endif ::nvidia::cuda::context_t get(); + + void push(); + void pop(); + + void synchronize(); + + public: + std::shared_ptr<::nvidia::cuda::context_stack> enter(); + }; + + class context_stack { + std::shared_ptr<::nvidia::cuda::context> _ctx; + + public: + inline ~context_stack() + { + _ctx->pop(); + } + inline context_stack(std::shared_ptr<::nvidia::cuda::context> ctx) : _ctx(ctx) + { + _ctx->push(); + } }; } // namespace nvidia::cuda