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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include <cstddef>
|
2021-04-26 02:05:44 +00:00
|
|
|
#include <tuple>
|
2020-11-05 05:39:01 +00:00
|
|
|
#include "util/util-bitmask.hpp"
|
|
|
|
#include "util/util-library.hpp"
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4365)
|
2020-04-02 18:29:00 +00:00
|
|
|
#pragma warning(disable : 5204)
|
2020-03-20 22:10:19 +00:00
|
|
|
#include <d3d11.h>
|
|
|
|
#include <dxgi.h>
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2020-06-14 17:17:26 +00:00
|
|
|
#define CUDA_DEFINE_FUNCTION(name, ...) \
|
|
|
|
private: \
|
|
|
|
typedef ::nvidia::cuda::result (*t##name)(__VA_ARGS__); \
|
|
|
|
\
|
|
|
|
public: \
|
2021-04-26 01:50:17 +00:00
|
|
|
t##name name = nullptr;
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
namespace nvidia::cuda {
|
2020-06-14 17:17:26 +00:00
|
|
|
enum class result : std::size_t {
|
2020-03-20 22:10:19 +00:00
|
|
|
SUCCESS = 0,
|
|
|
|
INVALID_VALUE = 1,
|
|
|
|
OUT_OF_MEMORY = 2,
|
|
|
|
NOT_INITIALIZED = 3,
|
|
|
|
DEINITIALIZED = 4,
|
|
|
|
NO_DEVICE = 100,
|
|
|
|
INVALID_DEVICE = 101,
|
|
|
|
INVALID_CONTEXT = 201,
|
|
|
|
MAP_FAILED = 205,
|
|
|
|
UNMAP_FAILED = 206,
|
|
|
|
ARRAY_IS_MAPPED = 207,
|
|
|
|
ALREADY_MAPPED = 208,
|
|
|
|
NOT_MAPPED = 211,
|
|
|
|
INVALID_GRAPHICS_CONTEXT = 219,
|
|
|
|
// Still missing some.
|
|
|
|
};
|
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
enum class memory_type : uint32_t {
|
2020-03-20 22:10:19 +00:00
|
|
|
HOST = 1,
|
|
|
|
DEVICE = 2,
|
|
|
|
ARRAY = 3,
|
|
|
|
UNIFIED = 4,
|
|
|
|
};
|
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
enum class array_format : uint32_t {
|
2020-03-20 22:10:19 +00:00
|
|
|
UNSIGNED_INT8 = 0b00000001,
|
|
|
|
UNSIGNED_INT16 = 0b00000010,
|
|
|
|
UNSIGNED_INT32 = 0b00000011,
|
|
|
|
SIGNED_INT8 = 0b00001000,
|
|
|
|
SIGNED_INT16 = 0b00001001,
|
|
|
|
SIGNED_INT32 = 0b00001010,
|
|
|
|
HALF = 0b00010000,
|
|
|
|
FLOAT = 0b00100000,
|
|
|
|
};
|
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
enum class context_flags : uint32_t {
|
2020-04-11 01:12:39 +00:00
|
|
|
SCHEDULER_AUTO = 0x0,
|
|
|
|
SCHEDULER_SPIN = 0x1,
|
|
|
|
SCHEDULER_YIELD = 0x2,
|
|
|
|
SCHEDULER_BLOCKING_SYNC = 0x4,
|
|
|
|
MAP_HOST = 0x8,
|
|
|
|
LOCAL_MEMORY_RESIZE_TO_MAXIMUM = 0x10,
|
|
|
|
};
|
|
|
|
|
2021-04-26 01:50:17 +00:00
|
|
|
enum class external_memory_handle_type : uint32_t {
|
|
|
|
INVALID = 0,
|
|
|
|
FILE_DESCRIPTOR = 1,
|
|
|
|
WIN32_SHARED_HANDLE = 2,
|
|
|
|
WIN32_GLOBAL_SHARED_HANDLE = 3,
|
|
|
|
D3D12_HEAP = 4,
|
|
|
|
D3D12_RESOURCE = 5,
|
|
|
|
D3D11_SHARED_RESOURCE = 6,
|
|
|
|
D3D11_GLOBAL_SHARED_RESOURCE = 7,
|
|
|
|
NVSCIBUF = 8,
|
|
|
|
};
|
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
enum class stream_flags : uint32_t {
|
2020-04-11 01:12:39 +00:00
|
|
|
DEFAULT = 0x0,
|
|
|
|
NON_BLOCKING = 0x1,
|
|
|
|
};
|
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
typedef void* array_t;
|
|
|
|
typedef void* context_t;
|
|
|
|
typedef uint64_t device_ptr_t;
|
2021-04-26 01:50:17 +00:00
|
|
|
typedef void* external_memory_t;
|
2020-08-10 01:29:05 +00:00
|
|
|
typedef void* graphics_resource_t;
|
|
|
|
typedef void* stream_t;
|
|
|
|
typedef int32_t device_t;
|
2020-03-20 22:10:19 +00:00
|
|
|
|
2021-04-26 01:50:17 +00:00
|
|
|
struct memcpy2d_v2_t {
|
2020-04-08 21:38:42 +00:00
|
|
|
std::size_t src_x_in_bytes;
|
|
|
|
std::size_t src_y;
|
2020-03-20 22:10:19 +00:00
|
|
|
|
2020-06-14 17:17:26 +00:00
|
|
|
memory_type src_memory_type;
|
|
|
|
const void* src_host;
|
|
|
|
device_ptr_t src_device;
|
|
|
|
array_t src_array;
|
|
|
|
std::size_t src_pitch;
|
2020-03-20 22:10:19 +00:00
|
|
|
|
2020-04-08 21:38:42 +00:00
|
|
|
std::size_t dst_x_in_bytes;
|
|
|
|
std::size_t dst_y;
|
2020-03-20 22:10:19 +00:00
|
|
|
|
2020-06-14 17:17:26 +00:00
|
|
|
memory_type dst_memory_type;
|
|
|
|
const void* dst_host;
|
|
|
|
device_ptr_t dst_device;
|
|
|
|
array_t dst_array;
|
|
|
|
std::size_t dst_pitch;
|
2020-03-20 22:10:19 +00:00
|
|
|
|
2020-04-05 04:13:14 +00:00
|
|
|
std::size_t width_in_bytes;
|
|
|
|
std::size_t height;
|
2020-03-20 22:10:19 +00:00
|
|
|
};
|
|
|
|
|
2021-04-26 01:50:17 +00:00
|
|
|
struct array_descriptor_v2_t {
|
2020-08-10 01:29:05 +00:00
|
|
|
std::size_t width;
|
|
|
|
std::size_t height;
|
|
|
|
uint32_t num_channels;
|
|
|
|
array_format format;
|
2020-03-20 22:10:19 +00:00
|
|
|
};
|
|
|
|
|
2021-04-26 01:50:17 +00:00
|
|
|
struct external_memory_buffer_info_v1_t {
|
|
|
|
uint64_t offset;
|
|
|
|
uint64_t size;
|
|
|
|
uint32_t flags;
|
|
|
|
uint32_t reserved[16];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct external_memory_handle_info_v1_t {
|
|
|
|
external_memory_handle_type type;
|
|
|
|
union {
|
|
|
|
int32_t file;
|
|
|
|
struct {
|
|
|
|
void* handle;
|
|
|
|
const void* name;
|
|
|
|
};
|
|
|
|
const void* nvscibuf;
|
|
|
|
};
|
|
|
|
uint64_t size;
|
|
|
|
uint32_t flags;
|
|
|
|
uint32_t reserved[16];
|
|
|
|
};
|
|
|
|
|
2020-11-07 03:30:36 +00:00
|
|
|
class cuda_error : public std::exception {
|
|
|
|
::nvidia::cuda::result _code;
|
|
|
|
|
|
|
|
public:
|
|
|
|
~cuda_error(){};
|
2020-11-08 05:47:47 +00:00
|
|
|
cuda_error(::nvidia::cuda::result code) : _code(code) {}
|
2020-11-07 03:30:36 +00:00
|
|
|
|
|
|
|
::nvidia::cuda::result code()
|
|
|
|
{
|
|
|
|
return _code;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-20 22:10:19 +00:00
|
|
|
class cuda {
|
2020-11-05 05:39:01 +00:00
|
|
|
std::shared_ptr<util::library> _library;
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
~cuda();
|
2020-11-08 05:47:47 +00:00
|
|
|
cuda();
|
2020-03-20 22:10:19 +00:00
|
|
|
|
2021-04-26 02:05:44 +00:00
|
|
|
int32_t version();
|
|
|
|
|
2020-03-20 22:10:19 +00:00
|
|
|
public:
|
|
|
|
// Initialization
|
2020-08-10 01:29:05 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuInit, int32_t flags);
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Version Management
|
2020-08-10 01:29:05 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuDriverGetVersion, int32_t* driverVersion);
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Device Management
|
2021-04-26 01:50:17 +00:00
|
|
|
// - Not yet needed.
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Primary Context Management
|
2020-06-14 17:17:26 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuDevicePrimaryCtxRelease, device_t device);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuDevicePrimaryCtxRetain, context_t* ctx, device_t device);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuDevicePrimaryCtxSetFlags, device_t device, context_flags flags);
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Context Management
|
2020-06-14 17:17:26 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuCtxCreate, context_t* ctx, context_flags flags, device_t device);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuCtxDestroy, context_t ctx);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuCtxGetCurrent, context_t* ctx);
|
2020-08-10 01:29:05 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuCtxGetStreamPriorityRange, int32_t* lowestPriority, int32_t* highestPriority);
|
2020-06-14 17:17:26 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuCtxPopCurrent, context_t* ctx);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuCtxPushCurrent, context_t ctx);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuCtxSetCurrent, context_t ctx);
|
2020-05-12 00:48:11 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuCtxSynchronize);
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Module Management
|
2021-04-26 01:50:17 +00:00
|
|
|
// - Not yet needed.
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Memory Management
|
2021-04-26 01:50:17 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuArrayGetDescriptor, array_descriptor_v2_t* pArrayDescripter, array_t array);
|
2020-06-14 17:17:26 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuMemAlloc, device_ptr_t* ptr, std::size_t bytes);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuMemAllocPitch, device_ptr_t* ptr, std::size_t* pitch, std::size_t width_in_bytes,
|
2020-08-10 01:29:05 +00:00
|
|
|
std::size_t height, uint32_t element_size_bytes);
|
2020-06-14 17:17:26 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuMemFree, device_ptr_t ptr);
|
2020-08-10 01:29:05 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuMemHostGetDevicePointer, device_ptr_t* devptr, void* ptr, uint32_t flags);
|
2020-06-14 17:17:26 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuMemcpy, device_ptr_t dst, device_ptr_t src, std::size_t bytes);
|
2021-04-26 01:50:17 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuMemcpy2D, const memcpy2d_v2_t* copy);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuMemcpy2DAsync, const memcpy2d_v2_t* copy, stream_t stream);
|
2020-06-14 17:17:26 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuMemcpyAtoA, array_t dst, std::size_t dstOffset, array_t src, std::size_t srcOffset,
|
2020-03-20 22:10:19 +00:00
|
|
|
std::size_t byteCount);
|
2020-06-14 17:17:26 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuMemcpyAtoD, device_ptr_t dst, array_t src, std::size_t srcOffset, std::size_t byteCount);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuMemcpyAtoH, void* dst, array_t src, std::size_t srcOffset, std::size_t byteCount);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuMemcpyAtoHAsync, void* dst, array_t src, std::size_t srcOffset, std::size_t byteCount);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuMemcpyDtoA, array_t dst, std::size_t dstOffset, device_ptr_t src, std::size_t byteCount);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuMemcpyDtoD, device_ptr_t dst, array_t srcArray, std::size_t byteCount);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuMemcpyDtoH, void* dst, array_t src, std::size_t byteCount);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuMemcpyDtoHAsync, void* dst, array_t src, std::size_t byteCount);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuMemcpyHtoA, array_t dst, std::size_t dstOffset, void* src, std::size_t byteCount);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuMemcpyHtoAAsync, array_t dst, std::size_t dstOffset, void* src, std::size_t byteCount);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuMemcpyHtoD, device_ptr_t dst, void* src, std::size_t byteCount);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuMemcpyHtoDAsync, device_ptr_t dst, void* src, std::size_t byteCount);
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Virtual Memory Management
|
2021-04-26 01:50:17 +00:00
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Stream Ordered Memory Allocator
|
|
|
|
// - Not yet needed.
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Unified Addressing
|
2021-04-26 01:50:17 +00:00
|
|
|
// - Not yet needed.
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Stream Managment
|
2020-06-14 17:17:26 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuStreamCreate, stream_t* stream, stream_flags flags);
|
2020-08-10 01:29:05 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuStreamCreateWithPriority, stream_t* stream, stream_flags flags, int32_t priority);
|
2020-06-14 17:17:26 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuStreamDestroy, stream_t stream);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuStreamSynchronize, stream_t stream);
|
2021-04-26 01:50:17 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuStreamGetPriority, stream_t stream, int32_t* priority);
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Event Management
|
2021-04-26 01:50:17 +00:00
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// External Resource Interoperability (CUDA 11.1+)
|
|
|
|
// - Not yet needed.
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Stream Memory Operations
|
2021-04-26 01:50:17 +00:00
|
|
|
// - Not yet needed.
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Execution Control
|
2021-04-26 01:50:17 +00:00
|
|
|
// - Not yet needed.
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Graph Management
|
2021-04-26 01:50:17 +00:00
|
|
|
// - Not yet needed.
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Occupancy
|
2021-04-26 01:50:17 +00:00
|
|
|
// - Not yet needed.
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Texture Object Management
|
2021-04-26 01:50:17 +00:00
|
|
|
// - Not yet needed.
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Surface Object Management
|
2021-04-26 01:50:17 +00:00
|
|
|
// - Not yet needed.
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Peer Context Memory Access
|
2021-04-26 01:50:17 +00:00
|
|
|
// - Not yet needed.
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// Graphics Interoperability
|
2020-08-10 01:29:05 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuGraphicsMapResources, uint32_t count, graphics_resource_t* resources, stream_t stream);
|
2020-06-14 17:17:26 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuGraphicsSubResourceGetMappedArray, array_t* array, graphics_resource_t resource,
|
2020-08-10 01:29:05 +00:00
|
|
|
uint32_t index, uint32_t level);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuGraphicsUnmapResources, uint32_t count, graphics_resource_t* resources, stream_t stream);
|
2020-06-14 17:17:26 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuGraphicsUnregisterResource, graphics_resource_t resource);
|
2020-03-20 22:10:19 +00:00
|
|
|
|
2021-04-26 01:50:17 +00:00
|
|
|
// Driver Entry Point Access
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Profiler Control
|
|
|
|
// - Not yet needed.
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
// OpenGL Interoperability
|
2021-04-26 01:50:17 +00:00
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// VDPAU Interoperability
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// EGL Interoperability
|
|
|
|
// - Not yet needed.
|
|
|
|
|
2020-03-20 22:10:19 +00:00
|
|
|
#ifdef WIN32
|
2021-04-26 01:50:17 +00:00
|
|
|
// Direct3D9 Interoperability
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Direct3D10 Interoperability
|
|
|
|
CUDA_DEFINE_FUNCTION(cuD3D10GetDevice, device_t* device, IDXGIAdapter* adapter);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuGraphicsD3D10RegisterResource, graphics_resource_t* resource,
|
|
|
|
ID3D10Resource* d3dresource, uint32_t flags);
|
|
|
|
|
|
|
|
// Direct3D11 Interoperability
|
2020-06-14 17:17:26 +00:00
|
|
|
CUDA_DEFINE_FUNCTION(cuD3D11GetDevice, device_t* device, IDXGIAdapter* adapter);
|
|
|
|
CUDA_DEFINE_FUNCTION(cuGraphicsD3D11RegisterResource, graphics_resource_t* resource,
|
2020-08-10 01:29:05 +00:00
|
|
|
ID3D11Resource* d3dresource, uint32_t flags);
|
2020-03-20 22:10:19 +00:00
|
|
|
#endif
|
2020-06-14 23:10:02 +00:00
|
|
|
public:
|
2020-11-05 05:39:01 +00:00
|
|
|
static std::shared_ptr<::nvidia::cuda::cuda> get();
|
2020-03-20 22:10:19 +00:00
|
|
|
};
|
|
|
|
} // namespace nvidia::cuda
|
2020-04-11 01:12:39 +00:00
|
|
|
|
2020-06-14 17:17:26 +00:00
|
|
|
P_ENABLE_BITMASK_OPERATORS(::nvidia::cuda::context_flags)
|
|
|
|
P_ENABLE_BITMASK_OPERATORS(::nvidia::cuda::stream_flags)
|