Share CUDA context between all nvenc encoder instances
This commit is contained in:
@@ -127,7 +127,11 @@ if (WIVRN_BUILD_SERVER)
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(WIVRN_USE_NVENC)
|
if(WIVRN_USE_NVENC)
|
||||||
target_sources(wivrn-server PRIVATE encoder/video_encoder_nvenc.cpp)
|
target_sources(
|
||||||
|
wivrn-server
|
||||||
|
PRIVATE encoder/video_encoder_nvenc.cpp
|
||||||
|
encoder/video_encoder_nvenc_shared_state.cpp
|
||||||
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(WIVRN_USE_VAAPI)
|
if(WIVRN_USE_VAAPI)
|
||||||
|
|||||||
@@ -37,15 +37,15 @@
|
|||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define NVENC_CHECK(x) \
|
#define NVENC_CHECK(x) \
|
||||||
do \
|
do \
|
||||||
{ \
|
{ \
|
||||||
NVENCSTATUS status = x; \
|
NVENCSTATUS status = x; \
|
||||||
if (status != NV_ENC_SUCCESS) \
|
if (status != NV_ENC_SUCCESS) \
|
||||||
{ \
|
{ \
|
||||||
U_LOG_E("%s:%d: %d, %s", __FILE__, __LINE__, status, fn.nvEncGetLastErrorString(session_handle)); \
|
U_LOG_E("%s:%d: %d, %s", __FILE__, __LINE__, status, shared_state->fn.nvEncGetLastErrorString(session_handle)); \
|
||||||
throw std::runtime_error("nvenc error"); \
|
throw std::runtime_error("nvenc error"); \
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define CU_CHECK(x) \
|
#define CU_CHECK(x) \
|
||||||
@@ -55,7 +55,7 @@
|
|||||||
if (status != CUDA_SUCCESS) \
|
if (status != CUDA_SUCCESS) \
|
||||||
{ \
|
{ \
|
||||||
const char * error_string; \
|
const char * error_string; \
|
||||||
cuda_fn->cuGetErrorString(status, &error_string); \
|
shared_state->cuda_fn->cuGetErrorString(status, &error_string); \
|
||||||
U_LOG_E("%s:%d: %s (%d)", __FILE__, __LINE__, error_string, (int)status); \
|
U_LOG_E("%s:%d: %s (%d)", __FILE__, __LINE__, error_string, (int)status); \
|
||||||
throw std::runtime_error(std::string("CUDA error: ") + error_string); \
|
throw std::runtime_error(std::string("CUDA error: ") + error_string); \
|
||||||
} \
|
} \
|
||||||
@@ -64,62 +64,6 @@
|
|||||||
namespace wivrn
|
namespace wivrn
|
||||||
{
|
{
|
||||||
|
|
||||||
void video_encoder_nvenc::deleter::operator()(CudaFunctions * fn)
|
|
||||||
{
|
|
||||||
cuda_free_functions(&fn);
|
|
||||||
}
|
|
||||||
void video_encoder_nvenc::deleter::operator()(NvencFunctions * fn)
|
|
||||||
{
|
|
||||||
nvenc_free_functions(&fn);
|
|
||||||
}
|
|
||||||
|
|
||||||
static auto init()
|
|
||||||
{
|
|
||||||
std::unique_ptr<CudaFunctions, video_encoder_nvenc::deleter> cuda_fn;
|
|
||||||
std::unique_ptr<NvencFunctions, video_encoder_nvenc::deleter> nvenc_fn;
|
|
||||||
void * session_handle;
|
|
||||||
|
|
||||||
{
|
|
||||||
CudaFunctions * tmp = nullptr;
|
|
||||||
if (cuda_load_functions(&tmp, nullptr))
|
|
||||||
throw std::runtime_error("Failed to load CUDA");
|
|
||||||
cuda_fn.reset(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
NvencFunctions * tmp = nullptr;
|
|
||||||
if (nvenc_load_functions(&tmp, nullptr))
|
|
||||||
throw std::runtime_error("Failed to load nvenc");
|
|
||||||
nvenc_fn.reset(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
CU_CHECK(cuda_fn->cuInit(0));
|
|
||||||
|
|
||||||
CUcontext cuda;
|
|
||||||
CU_CHECK(cuda_fn->cuCtxCreate(&cuda, 0, 0));
|
|
||||||
|
|
||||||
NV_ENCODE_API_FUNCTION_LIST fn{
|
|
||||||
.version = NV_ENCODE_API_FUNCTION_LIST_VER,
|
|
||||||
};
|
|
||||||
NVENC_CHECK_NOENCODER(nvenc_fn->NvEncodeAPICreateInstance(&fn));
|
|
||||||
|
|
||||||
{
|
|
||||||
NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params{
|
|
||||||
.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER,
|
|
||||||
.deviceType = NV_ENC_DEVICE_TYPE_CUDA,
|
|
||||||
.device = cuda,
|
|
||||||
.reserved = {},
|
|
||||||
.apiVersion = NVENCAPI_VERSION,
|
|
||||||
.reserved1 = {},
|
|
||||||
.reserved2 = {},
|
|
||||||
};
|
|
||||||
|
|
||||||
NVENC_CHECK_NOENCODER(fn.nvEncOpenEncodeSessionEx(¶ms, &session_handle));
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::make_tuple(std::move(cuda_fn), std::move(nvenc_fn), fn, cuda, session_handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
static auto encode_guid(video_codec codec)
|
static auto encode_guid(video_codec codec)
|
||||||
{
|
{
|
||||||
switch (codec)
|
switch (codec)
|
||||||
@@ -141,13 +85,13 @@ video_encoder_nvenc::video_encoder_nvenc(
|
|||||||
uint8_t stream_idx) :
|
uint8_t stream_idx) :
|
||||||
video_encoder(stream_idx, settings.channels, settings.bitrate_multiplier, true),
|
video_encoder(stream_idx, settings.channels, settings.bitrate_multiplier, true),
|
||||||
vk(vk),
|
vk(vk),
|
||||||
|
shared_state(video_encoder_nvenc_shared_state::get()),
|
||||||
fps(fps),
|
fps(fps),
|
||||||
bitrate(settings.bitrate)
|
bitrate(settings.bitrate)
|
||||||
{
|
{
|
||||||
if (settings.bit_depth != 8)
|
if (settings.bit_depth != 8)
|
||||||
throw std::runtime_error("nvenc encoder only supports 8-bit encoding");
|
throw std::runtime_error("nvenc encoder only supports 8-bit encoding");
|
||||||
|
|
||||||
std::tie(cuda_fn, nvenc_fn, fn, cuda, session_handle) = init();
|
|
||||||
assert(settings.width % 32 == 0);
|
assert(settings.width % 32 == 0);
|
||||||
assert(settings.height % 32 == 0);
|
assert(settings.height % 32 == 0);
|
||||||
rect = vk::Rect2D{
|
rect = vk::Rect2D{
|
||||||
@@ -161,12 +105,20 @@ video_encoder_nvenc::video_encoder_nvenc(
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
auto encodeGUID = encode_guid(settings.codec);
|
||||||
|
NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params_ex = {
|
||||||
|
.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER,
|
||||||
|
.deviceType = NV_ENC_DEVICE_TYPE_CUDA,
|
||||||
|
.device = shared_state->cuda,
|
||||||
|
.apiVersion = NVENCAPI_VERSION,
|
||||||
|
};
|
||||||
|
NVENC_CHECK_NOENCODER(shared_state->fn.nvEncOpenEncodeSessionEx(¶ms_ex, &session_handle));
|
||||||
|
|
||||||
uint32_t count;
|
uint32_t count;
|
||||||
std::vector<GUID> presets;
|
std::vector<GUID> presets;
|
||||||
auto encodeGUID = encode_guid(settings.codec);
|
NVENC_CHECK(shared_state->fn.nvEncGetEncodePresetCount(session_handle, encodeGUID, &count));
|
||||||
NVENC_CHECK(fn.nvEncGetEncodePresetCount(session_handle, encodeGUID, &count));
|
|
||||||
presets.resize(count);
|
presets.resize(count);
|
||||||
NVENC_CHECK(fn.nvEncGetEncodePresetGUIDs(session_handle, encodeGUID, presets.data(), count, &count));
|
NVENC_CHECK(shared_state->fn.nvEncGetEncodePresetGUIDs(session_handle, encodeGUID, presets.data(), count, &count));
|
||||||
|
|
||||||
#pragma GCC diagnostic push
|
#pragma GCC diagnostic push
|
||||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||||
@@ -178,7 +130,7 @@ video_encoder_nvenc::video_encoder_nvenc(
|
|||||||
.presetCfg = {
|
.presetCfg = {
|
||||||
.version = NV_ENC_CONFIG_VER,
|
.version = NV_ENC_CONFIG_VER,
|
||||||
}};
|
}};
|
||||||
NVENC_CHECK(fn.nvEncGetEncodePresetConfigEx(session_handle, encodeGUID, presetGUID, tuningInfo, &preset_config));
|
NVENC_CHECK(shared_state->fn.nvEncGetEncodePresetConfigEx(session_handle, encodeGUID, presetGUID, tuningInfo, &preset_config));
|
||||||
|
|
||||||
NV_ENC_CONFIG params = preset_config.presetCfg;
|
NV_ENC_CONFIG params = preset_config.presetCfg;
|
||||||
|
|
||||||
@@ -228,12 +180,12 @@ video_encoder_nvenc::video_encoder_nvenc(
|
|||||||
.enablePTD = 1,
|
.enablePTD = 1,
|
||||||
.encodeConfig = ¶ms,
|
.encodeConfig = ¶ms,
|
||||||
.tuningInfo = tuningInfo};
|
.tuningInfo = tuningInfo};
|
||||||
NVENC_CHECK(fn.nvEncInitializeEncoder(session_handle, ¶ms2));
|
NVENC_CHECK(shared_state->fn.nvEncInitializeEncoder(session_handle, ¶ms2));
|
||||||
|
|
||||||
NV_ENC_CREATE_BITSTREAM_BUFFER params3{
|
NV_ENC_CREATE_BITSTREAM_BUFFER params3{
|
||||||
.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER,
|
.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER,
|
||||||
};
|
};
|
||||||
NVENC_CHECK(fn.nvEncCreateBitstreamBuffer(session_handle, ¶ms3));
|
NVENC_CHECK(shared_state->fn.nvEncCreateBitstreamBuffer(session_handle, ¶ms3));
|
||||||
bitstreamBuffer = params3.bitstreamBuffer;
|
bitstreamBuffer = params3.bitstreamBuffer;
|
||||||
|
|
||||||
vk::DeviceSize buffer_size = rect.extent.width * settings.video_height * 3 / 2;
|
vk::DeviceSize buffer_size = rect.extent.width * settings.video_height * 3 / 2;
|
||||||
@@ -276,7 +228,7 @@ video_encoder_nvenc::video_encoder_nvenc(
|
|||||||
});
|
});
|
||||||
|
|
||||||
CUdeviceptr frame;
|
CUdeviceptr frame;
|
||||||
CU_CHECK(cuda_fn->cuCtxPushCurrent(cuda));
|
CU_CHECK(shared_state->cuda_fn->cuCtxPushCurrent(shared_state->cuda));
|
||||||
{
|
{
|
||||||
CUexternalMemory extmem;
|
CUexternalMemory extmem;
|
||||||
CUDA_EXTERNAL_MEMORY_HANDLE_DESC param{
|
CUDA_EXTERNAL_MEMORY_HANDLE_DESC param{
|
||||||
@@ -285,14 +237,14 @@ video_encoder_nvenc::video_encoder_nvenc(
|
|||||||
.size = memory_req.size,
|
.size = memory_req.size,
|
||||||
.flags = 0,
|
.flags = 0,
|
||||||
};
|
};
|
||||||
CU_CHECK(cuda_fn->cuImportExternalMemory(&extmem, ¶m));
|
CU_CHECK(shared_state->cuda_fn->cuImportExternalMemory(&extmem, ¶m));
|
||||||
|
|
||||||
CUDA_EXTERNAL_MEMORY_BUFFER_DESC map_param{
|
CUDA_EXTERNAL_MEMORY_BUFFER_DESC map_param{
|
||||||
.offset = 0,
|
.offset = 0,
|
||||||
.size = buffer_size,
|
.size = buffer_size,
|
||||||
.flags = 0,
|
.flags = 0,
|
||||||
};
|
};
|
||||||
CU_CHECK(cuda_fn->cuExternalMemoryGetMappedBuffer(&frame, extmem, &map_param));
|
CU_CHECK(shared_state->cuda_fn->cuExternalMemoryGetMappedBuffer(&frame, extmem, &map_param));
|
||||||
}
|
}
|
||||||
|
|
||||||
NV_ENC_REGISTER_RESOURCE param3{
|
NV_ENC_REGISTER_RESOURCE param3{
|
||||||
@@ -305,18 +257,16 @@ video_encoder_nvenc::video_encoder_nvenc(
|
|||||||
.bufferFormat = NV_ENC_BUFFER_FORMAT_NV12,
|
.bufferFormat = NV_ENC_BUFFER_FORMAT_NV12,
|
||||||
.bufferUsage = NV_ENC_INPUT_IMAGE,
|
.bufferUsage = NV_ENC_INPUT_IMAGE,
|
||||||
};
|
};
|
||||||
NVENC_CHECK(fn.nvEncRegisterResource(session_handle, ¶m3));
|
NVENC_CHECK(shared_state->fn.nvEncRegisterResource(session_handle, ¶m3));
|
||||||
i.nvenc_resource = param3.registeredResource;
|
i.nvenc_resource = param3.registeredResource;
|
||||||
}
|
}
|
||||||
CU_CHECK(cuda_fn->cuCtxPopCurrent(NULL));
|
CU_CHECK(shared_state->cuda_fn->cuCtxPopCurrent(NULL));
|
||||||
}
|
}
|
||||||
|
|
||||||
video_encoder_nvenc::~video_encoder_nvenc()
|
video_encoder_nvenc::~video_encoder_nvenc()
|
||||||
{
|
{
|
||||||
if (session_handle)
|
if (session_handle)
|
||||||
fn.nvEncDestroyEncoder(session_handle);
|
shared_state->fn.nvEncDestroyEncoder(session_handle);
|
||||||
if (cuda)
|
|
||||||
cuda_fn->cuCtxDestroy(cuda);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<bool, vk::Semaphore> video_encoder_nvenc::present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, uint8_t slot, uint64_t)
|
std::pair<bool, vk::Semaphore> video_encoder_nvenc::present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, uint8_t slot, uint64_t)
|
||||||
@@ -367,12 +317,12 @@ std::pair<bool, vk::Semaphore> video_encoder_nvenc::present_image(vk::Image y_cb
|
|||||||
|
|
||||||
std::optional<video_encoder::data> video_encoder_nvenc::encode(bool idr, std::chrono::steady_clock::time_point pts, uint8_t slot)
|
std::optional<video_encoder::data> video_encoder_nvenc::encode(bool idr, std::chrono::steady_clock::time_point pts, uint8_t slot)
|
||||||
{
|
{
|
||||||
CU_CHECK(cuda_fn->cuCtxPushCurrent(cuda));
|
CU_CHECK(shared_state->cuda_fn->cuCtxPushCurrent(shared_state->cuda));
|
||||||
|
|
||||||
NV_ENC_MAP_INPUT_RESOURCE param4{};
|
NV_ENC_MAP_INPUT_RESOURCE param4{};
|
||||||
param4.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
|
param4.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
|
||||||
param4.registeredResource = in[slot].nvenc_resource;
|
param4.registeredResource = in[slot].nvenc_resource;
|
||||||
NVENC_CHECK(fn.nvEncMapInputResource(session_handle, ¶m4));
|
NVENC_CHECK(shared_state->fn.nvEncMapInputResource(session_handle, ¶m4));
|
||||||
|
|
||||||
NV_ENC_PIC_PARAMS param{
|
NV_ENC_PIC_PARAMS param{
|
||||||
.version = NV_ENC_PIC_PARAMS_VER,
|
.version = NV_ENC_PIC_PARAMS_VER,
|
||||||
@@ -387,30 +337,41 @@ std::optional<video_encoder::data> video_encoder_nvenc::encode(bool idr, std::ch
|
|||||||
.bufferFmt = param4.mappedBufferFmt,
|
.bufferFmt = param4.mappedBufferFmt,
|
||||||
.pictureStruct = NV_ENC_PIC_STRUCT_FRAME,
|
.pictureStruct = NV_ENC_PIC_STRUCT_FRAME,
|
||||||
};
|
};
|
||||||
NVENC_CHECK(fn.nvEncEncodePicture(session_handle, ¶m));
|
NVENC_CHECK(shared_state->fn.nvEncEncodePicture(session_handle, ¶m));
|
||||||
|
|
||||||
NV_ENC_LOCK_BITSTREAM param2{
|
NV_ENC_LOCK_BITSTREAM param2{
|
||||||
.version = NV_ENC_LOCK_BITSTREAM_VER,
|
.version = NV_ENC_LOCK_BITSTREAM_VER,
|
||||||
.doNotWait = 0,
|
.doNotWait = 0,
|
||||||
.outputBitstream = bitstreamBuffer,
|
.outputBitstream = bitstreamBuffer,
|
||||||
};
|
};
|
||||||
NVENC_CHECK(fn.nvEncLockBitstream(session_handle, ¶m2));
|
NVENC_CHECK(shared_state->fn.nvEncLockBitstream(session_handle, ¶m2));
|
||||||
|
|
||||||
CU_CHECK(cuda_fn->cuCtxPopCurrent(NULL));
|
CU_CHECK(shared_state->cuda_fn->cuCtxPopCurrent(NULL));
|
||||||
return data{
|
return data{
|
||||||
.encoder = this,
|
.encoder = this,
|
||||||
.span = std::span((uint8_t *)param2.bitstreamBufferPtr, param2.bitstreamSizeInBytes),
|
.span = std::span((uint8_t *)param2.bitstreamBufferPtr, param2.bitstreamSizeInBytes),
|
||||||
.mem = std::shared_ptr<void>(param2.bitstreamBufferPtr, [this](void *) {
|
.mem = std::shared_ptr<void>(param2.bitstreamBufferPtr, [this](void *) {
|
||||||
NVENCSTATUS status = fn.nvEncUnlockBitstream(session_handle, bitstreamBuffer);
|
NVENCSTATUS status = shared_state->fn.nvEncUnlockBitstream(session_handle, bitstreamBuffer);
|
||||||
if (status != NV_ENC_SUCCESS)
|
if (status != NV_ENC_SUCCESS)
|
||||||
U_LOG_E("%s:%d: %d, %s", __FILE__, __LINE__, status, fn.nvEncGetLastErrorString(session_handle));
|
U_LOG_E("%s:%d: %d, %s", __FILE__, __LINE__, status, shared_state->fn.nvEncGetLastErrorString(session_handle));
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::array<int, 2> video_encoder_nvenc::get_max_size(video_codec codec)
|
std::array<int, 2> video_encoder_nvenc::get_max_size(video_codec codec)
|
||||||
{
|
{
|
||||||
auto [cuda_fn, nvenc_fn, fn, cuda, session_handle] = init();
|
video_encoder_nvenc_shared_state state;
|
||||||
|
void * session_handle = nullptr;
|
||||||
|
NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params_ex = {
|
||||||
|
.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER,
|
||||||
|
.deviceType = NV_ENC_DEVICE_TYPE_CUDA,
|
||||||
|
.device = state.cuda,
|
||||||
|
.apiVersion = NVENCAPI_VERSION,
|
||||||
|
};
|
||||||
|
if (state.fn.nvEncOpenEncodeSessionEx(¶ms_ex, &session_handle) != NV_ENC_SUCCESS)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("nvenc get_max_size: Failed to open session");
|
||||||
|
}
|
||||||
std::array<int, 2> result;
|
std::array<int, 2> result;
|
||||||
std::exception_ptr ex;
|
std::exception_ptr ex;
|
||||||
try
|
try
|
||||||
@@ -425,15 +386,21 @@ std::array<int, 2> video_encoder_nvenc::get_max_size(video_codec codec)
|
|||||||
.version = NV_ENC_CAPS_PARAM_VER,
|
.version = NV_ENC_CAPS_PARAM_VER,
|
||||||
.capsToQuery = NV_ENC_CAPS_WIDTH_MAX,
|
.capsToQuery = NV_ENC_CAPS_WIDTH_MAX,
|
||||||
};
|
};
|
||||||
NVENC_CHECK(fn.nvEncGetEncodeCaps(session_handle, encodeGUID, &cap_param, res));
|
NVENCSTATUS status = state.fn.nvEncGetEncodeCaps(session_handle, encodeGUID, &cap_param, res);
|
||||||
|
if (status != NV_ENC_SUCCESS)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("nvenc get_max_size: failed to get caps");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
ex = std::current_exception();
|
ex = std::current_exception();
|
||||||
}
|
}
|
||||||
cuda_fn->cuCtxPopCurrent(NULL);
|
if (session_handle)
|
||||||
fn.nvEncDestroyEncoder(session_handle);
|
{
|
||||||
|
state.fn.nvEncDestroyEncoder(session_handle);
|
||||||
|
}
|
||||||
if (ex)
|
if (ex)
|
||||||
std::rethrow_exception(ex);
|
std::rethrow_exception(ex);
|
||||||
U_LOG_D("nvenc maximum encoded size: %dx%d", result[0], result[1]);
|
U_LOG_D("nvenc maximum encoded size: %dx%d", result[0], result[1]);
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "video_encoder.h"
|
#include "video_encoder.h"
|
||||||
|
#include "video_encoder_nvenc_shared_state.h"
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <ffnvcodec/dynlink_cuda.h>
|
#include <ffnvcodec/dynlink_cuda.h>
|
||||||
#include <ffnvcodec/dynlink_loader.h>
|
#include <ffnvcodec/dynlink_loader.h>
|
||||||
@@ -31,22 +32,13 @@ namespace wivrn
|
|||||||
|
|
||||||
class video_encoder_nvenc : public video_encoder
|
class video_encoder_nvenc : public video_encoder
|
||||||
{
|
{
|
||||||
public:
|
|
||||||
struct deleter
|
|
||||||
{
|
|
||||||
void operator()(CudaFunctions * fn);
|
|
||||||
void operator()(NvencFunctions * fn);
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
wivrn_vk_bundle & vk;
|
wivrn_vk_bundle & vk;
|
||||||
// relevant part of the input image to encode
|
// relevant part of the input image to encode
|
||||||
vk::Rect2D rect;
|
vk::Rect2D rect;
|
||||||
|
|
||||||
std::unique_ptr<CudaFunctions, deleter> cuda_fn;
|
std::shared_ptr<video_encoder_nvenc_shared_state> shared_state;
|
||||||
std::unique_ptr<NvencFunctions, deleter> nvenc_fn;
|
|
||||||
NV_ENCODE_API_FUNCTION_LIST fn;
|
|
||||||
CUcontext cuda;
|
|
||||||
void * session_handle = nullptr;
|
void * session_handle = nullptr;
|
||||||
NV_ENC_OUTPUT_PTR bitstreamBuffer;
|
NV_ENC_OUTPUT_PTR bitstreamBuffer;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,100 @@
|
|||||||
|
/*
|
||||||
|
* WiVRn VR streaming
|
||||||
|
* Copyright (C) 2022-2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||||
|
* Copyright (C) 2022-2024 Patrick Nicolas <patricknicolas@laposte.net>
|
||||||
|
*
|
||||||
|
* 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 3 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, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "video_encoder_nvenc_shared_state.h"
|
||||||
|
#include "util/u_logging.h"
|
||||||
|
|
||||||
|
#include <bits/unique_lock.h>
|
||||||
|
|
||||||
|
void video_encoder_nvenc_shared_state::deleter::operator()(CudaFunctions * fn)
|
||||||
|
{
|
||||||
|
cuda_free_functions(&fn);
|
||||||
|
}
|
||||||
|
void video_encoder_nvenc_shared_state::deleter::operator()(NvencFunctions * fn)
|
||||||
|
{
|
||||||
|
nvenc_free_functions(&fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define NVENC_CHECK_NOENCODER(x) \
|
||||||
|
do \
|
||||||
|
{ \
|
||||||
|
NVENCSTATUS status = x; \
|
||||||
|
if (status != NV_ENC_SUCCESS) \
|
||||||
|
{ \
|
||||||
|
U_LOG_E("NVENC Init Error: %s:%d: %d", __FILE__, __LINE__, status); \
|
||||||
|
throw std::runtime_error("nvenc init error"); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define CU_CHECK(x) \
|
||||||
|
do \
|
||||||
|
{ \
|
||||||
|
CUresult status = x; \
|
||||||
|
if (status != CUDA_SUCCESS) \
|
||||||
|
{ \
|
||||||
|
const char * error_string; \
|
||||||
|
cuda_fn->cuGetErrorString(status, &error_string); \
|
||||||
|
U_LOG_E("CUDA Init Error: %s:%d: %s (%d)", __FILE__, __LINE__, error_string, (int)status); \
|
||||||
|
throw std::runtime_error(std::string("CUDA init error: ") + error_string); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
std::shared_ptr<video_encoder_nvenc_shared_state> video_encoder_nvenc_shared_state::get()
|
||||||
|
{
|
||||||
|
static std::weak_ptr<video_encoder_nvenc_shared_state> instance;
|
||||||
|
static std::mutex m;
|
||||||
|
std::unique_lock lock(m);
|
||||||
|
auto s = instance.lock();
|
||||||
|
if (s)
|
||||||
|
return s;
|
||||||
|
s.reset(new video_encoder_nvenc_shared_state());
|
||||||
|
instance = s;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
video_encoder_nvenc_shared_state::video_encoder_nvenc_shared_state()
|
||||||
|
{
|
||||||
|
CudaFunctions * tmp_cuda_fn = nullptr;
|
||||||
|
if (cuda_load_functions(&tmp_cuda_fn, nullptr))
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Failed to load CUDA functions");
|
||||||
|
}
|
||||||
|
cuda_fn.reset(tmp_cuda_fn);
|
||||||
|
|
||||||
|
NvencFunctions * tmp_nvenc_fn = nullptr;
|
||||||
|
if (nvenc_load_functions(&tmp_nvenc_fn, nullptr))
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Failed to load nvenc functions");
|
||||||
|
}
|
||||||
|
nvenc_fn.reset(tmp_nvenc_fn);
|
||||||
|
|
||||||
|
CU_CHECK(cuda_fn->cuInit(0));
|
||||||
|
CU_CHECK(cuda_fn->cuCtxCreate(&cuda, 0, 0));
|
||||||
|
|
||||||
|
fn.version = NV_ENCODE_API_FUNCTION_LIST_VER;
|
||||||
|
NVENC_CHECK_NOENCODER(nvenc_fn->NvEncodeAPICreateInstance(&fn));
|
||||||
|
}
|
||||||
|
|
||||||
|
video_encoder_nvenc_shared_state::~video_encoder_nvenc_shared_state()
|
||||||
|
{
|
||||||
|
if (cuda)
|
||||||
|
{
|
||||||
|
cuda_fn->cuCtxDestroy(cuda);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* WiVRn VR streaming
|
||||||
|
* Copyright (C) 2022-2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||||
|
* Copyright (C) 2022-2024 Patrick Nicolas <patricknicolas@laposte.net>
|
||||||
|
*
|
||||||
|
* 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 3 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, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include <ffnvcodec/dynlink_cuda.h>
|
||||||
|
#include <ffnvcodec/dynlink_loader.h>
|
||||||
|
#include <ffnvcodec/nvEncodeAPI.h>
|
||||||
|
|
||||||
|
class video_encoder_nvenc_shared_state
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
struct deleter
|
||||||
|
{
|
||||||
|
void operator()(CudaFunctions * fn);
|
||||||
|
void operator()(NvencFunctions * fn);
|
||||||
|
};
|
||||||
|
|
||||||
|
static std::shared_ptr<video_encoder_nvenc_shared_state> get();
|
||||||
|
std::unique_ptr<CudaFunctions, deleter> cuda_fn;
|
||||||
|
std::unique_ptr<NvencFunctions, deleter> nvenc_fn;
|
||||||
|
NV_ENCODE_API_FUNCTION_LIST fn = {};
|
||||||
|
CUcontext cuda = nullptr;
|
||||||
|
|
||||||
|
video_encoder_nvenc_shared_state();
|
||||||
|
~video_encoder_nvenc_shared_state();
|
||||||
|
|
||||||
|
video_encoder_nvenc_shared_state(const video_encoder_nvenc_shared_state &) = delete;
|
||||||
|
video_encoder_nvenc_shared_state & operator=(const video_encoder_nvenc_shared_state &) = delete;
|
||||||
|
video_encoder_nvenc_shared_state(video_encoder_nvenc_shared_state &&) = delete;
|
||||||
|
video_encoder_nvenc_shared_state & operator=(video_encoder_nvenc_shared_state &&) = delete;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user