2020-03-20 22:10:19 +00:00
|
|
|
/*
|
|
|
|
* 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-gs-texture.hpp"
|
|
|
|
#include "obs/gs/gs-helper.hpp"
|
2020-11-07 08:00:12 +00:00
|
|
|
#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 "<nvidia::cuda::gstexture> "
|
|
|
|
#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
|
2020-03-20 22:10:19 +00:00
|
|
|
|
2020-11-05 05:39:01 +00:00
|
|
|
nvidia::cuda::gstexture::gstexture(std::shared_ptr<gs::texture> texture)
|
|
|
|
: _cuda(::nvidia::cuda::cuda::get()), _texture(texture), _resource(), _is_mapped(false), _pointer()
|
2020-03-20 22:10:19 +00:00
|
|
|
{
|
2020-11-07 08:00:12 +00:00
|
|
|
D_LOG_DEBUG("Initializating... (Addr: 0x%" PRIuPTR ")", this);
|
|
|
|
|
2020-03-20 22:10:19 +00:00
|
|
|
if (!texture)
|
|
|
|
throw std::invalid_argument("texture");
|
|
|
|
|
2020-04-11 01:12:39 +00:00
|
|
|
gs::context gctx;
|
|
|
|
int dev_type = gs_get_device_type();
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
if (dev_type == GS_DEVICE_OPENGL) {
|
|
|
|
// ToDo
|
|
|
|
}
|
|
|
|
#ifdef WIN32
|
|
|
|
if (dev_type == GS_DEVICE_DIRECT3D_11) {
|
|
|
|
ID3D11Resource* resource = nullptr;
|
|
|
|
switch (_texture->get_type()) {
|
|
|
|
case gs::texture::type::Cube:
|
|
|
|
case gs::texture::type::Normal: {
|
|
|
|
resource = static_cast<ID3D11Resource*>(gs_texture_get_obj(_texture->get_object()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case gs::texture::type::Volume: {
|
|
|
|
resource = static_cast<ID3D11Resource*>(gs_texture_get_obj(_texture->get_object()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!resource) {
|
|
|
|
throw std::runtime_error("nvidia::cuda::gstexture: Failed to get resource from gs::texture.");
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (_cuda->cuGraphicsD3D11RegisterResource(&_resource, resource, 0)) {
|
2020-06-14 17:17:26 +00:00
|
|
|
case nvidia::cuda::result::SUCCESS:
|
2020-03-20 22:10:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw std::runtime_error("nvidia::cuda::gstexture: Failed to register resource.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
nvidia::cuda::gstexture::~gstexture()
|
|
|
|
{
|
2020-11-07 08:00:12 +00:00
|
|
|
D_LOG_DEBUG("Finalizing... (Addr: 0x%" PRIuPTR ")", this);
|
|
|
|
|
2020-03-20 22:10:19 +00:00
|
|
|
unmap();
|
|
|
|
_cuda->cuGraphicsUnregisterResource(_resource);
|
|
|
|
}
|
|
|
|
|
2020-06-14 17:17:26 +00:00
|
|
|
nvidia::cuda::array_t nvidia::cuda::gstexture::map(std::shared_ptr<nvidia::cuda::stream> stream)
|
2020-03-20 22:10:19 +00:00
|
|
|
{
|
|
|
|
if (_is_mapped) {
|
|
|
|
return _pointer;
|
|
|
|
}
|
|
|
|
|
2020-06-14 17:17:26 +00:00
|
|
|
graphics_resource_t resources[] = {_resource};
|
2020-03-20 22:10:19 +00:00
|
|
|
switch (_cuda->cuGraphicsMapResources(1, resources, stream->get())) {
|
2020-06-14 17:17:26 +00:00
|
|
|
case nvidia::cuda::result::SUCCESS:
|
2020-03-20 22:10:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw std::runtime_error("nvidia::cuda::gstexture: Mapping failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
_stream = stream;
|
|
|
|
_is_mapped = true;
|
|
|
|
|
|
|
|
switch (_cuda->cuGraphicsSubResourceGetMappedArray(&_pointer, _resource, 0, 0)) {
|
2020-06-14 17:17:26 +00:00
|
|
|
case nvidia::cuda::result::SUCCESS:
|
2020-03-20 22:10:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
unmap();
|
|
|
|
throw std::runtime_error("nvidia::cuda::gstexture: Mapping pointer failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return _pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nvidia::cuda::gstexture::unmap()
|
|
|
|
{
|
|
|
|
if (!_is_mapped)
|
|
|
|
return;
|
|
|
|
|
2020-06-14 17:17:26 +00:00
|
|
|
graphics_resource_t resources[] = {_resource};
|
2020-03-20 22:10:19 +00:00
|
|
|
switch (_cuda->cuGraphicsUnmapResources(1, resources, _stream->get())) {
|
2020-06-14 17:17:26 +00:00
|
|
|
case nvidia::cuda::result::SUCCESS:
|
2020-03-20 22:10:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw std::runtime_error("nvidia::cuda::gstexture: Unmapping failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
_is_mapped = false;
|
|
|
|
_pointer = nullptr;
|
|
|
|
_stream.reset();
|
|
|
|
}
|
2021-04-25 22:16:28 +00:00
|
|
|
|
|
|
|
std::shared_ptr<gs::texture> nvidia::cuda::gstexture::get_texture()
|
|
|
|
{
|
|
|
|
return _texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
::nvidia::cuda::graphics_resource_t nvidia::cuda::gstexture::get()
|
|
|
|
{
|
|
|
|
return _resource;
|
|
|
|
}
|