nvidia/cuda/context: Merge context-stack into context header

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2020-11-08 06:47:47 +01:00
parent 7b38114469
commit 5cd1d13d5f
5 changed files with 66 additions and 85 deletions

View file

@ -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"

View file

@ -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 <stdexcept>
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);
}
}

View file

@ -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 <memory>
#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

View file

@ -18,6 +18,7 @@
*/
#include "nvidia-cuda-context.hpp"
#include <cassert>
#include <stdexcept>
#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);
}
}

View file

@ -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