Sunshine master
Self-hosted game stream host for Moonlight.
cuda.cpp File Reference

Definitions for CUDA encoding. More...

#include <bitset>
#include <fcntl.h>
#include <filesystem>
#include <thread>
#include <ffnvcodec/dynlink_loader.h>
#include <NvFBC.h>
#include <libavcodec/avcodec.h>
#include <libavutil/hwcontext_cuda.h>
#include <libavutil/imgutils.h>
#include "cuda.h"
#include "graphics.h"
#include "src/logging.h"
#include "src/utility.h"
#include "src/video.h"
#include "wayland.h"
Include dependency graph for cuda.cpp:

Classes

class  cuda::nvfbc::ctx_t
 NvFBC CUDA context selected for a capture session. More...
 
struct  cuda::cu_resources
 CUDA frame resources registered for interop conversion. More...
 
class  cuda::cuda_ram_t
 CUDA encode device path that converts frames through system memory. More...
 
class  cuda::cuda_t
 CUDA encode device that imports captured frames into CUDA memory. More...
 
class  cuda::cuda_vram_t
 CUDA encode device path that keeps converted frames in GPU memory. More...
 
class  cuda::nvfbc::display_t
 NvFBC display capture backend that produces CUDA frames. More...
 
class  cuda::gl_cuda_vram_t
 OpenGL/CUDA interop resources used for GPU-side frame conversion. More...
 
class  cuda::nvfbc::handle_t
 NvFBC dynamic-library handle and function table. More...
 
class  cuda::img_t
 CUDA image wrapper that owns mapped graphics resources for one frame. More...
 

Macros

#define CU_CHECK(x, y)
 Macro for CU CHECK.
 
#define CU_CHECK_IGNORE(x, y)
 Macro for CU CHECK IGNORE.
 
#define SUNSHINE_STRINGVIEW(x)
 Macro for SUNSHINE STRINGVIEW.
 
#define SUNSHINE_STRINGVIEW_HELPER(x)
 Macro for SUNSHINE STRINGVIEW HELPER.
 

Typedefs

using cuda::cdf_t = util::safe_ptr<CudaFunctions, cff>
 Handle to a CUDA dynamic-library function table.
 
using cuda::registered_resource_t = util::safe_ptr<CUgraphicsResource_st, unregisterResource>
 CUDA graphics resource pointer released with cuGraphicsUnregisterResource.
 

Functions

void cuda::cff (CudaFunctions *cf)
 Release a Core Foundation object when the wrapper is destroyed.
 
void cuda::freeStream (CUstream stream)
 Release stream resources.
 
int cuda::init ()
 Map CUDA graphics resources for use as an image.
 
int cuda::nvfbc::init ()
 Load NvFBC and create the CUDA capture helper.
 
std::unique_ptr< platf::avcodec_encode_device_tcuda::make_avcodec_encode_device (int width, int height, bool vram)
 Create AVCodec encode device.
 
std::unique_ptr< platf::avcodec_encode_device_tcuda::make_avcodec_gl_encode_device (int width, int height, int offset_x, int offset_y)
 Create a GL->CUDA encoding device for consuming captured dmabufs.
 
std::shared_ptr< display_tplatf::nvfbc_display (mem_type_e hwdevice_type, const std::string &display_name, const video::config_t &config)
 Create an NvFBC CUDA display capture backend.
 
std::vector< std::string > platf::nvfbc_display_names ()
 Enumerate display names exposed by NvFBC.
 
file_t cuda::open_drm_fd_for_cuda_device (int index)
 Opens the DRM device associated with the CUDA device index.
 
void cuda::pass_error (const std::string_view &sv, const char *name, const char *description)
 Convert a CUDA result code into Sunshine's capture status.
 
void cuda::unregisterResource (CUgraphicsResource resource)
 Unregister a CUDA graphics resource if it is still registered.
 

Variables

constexpr auto cuda::cudaDevAttrMaxThreadsPerBlock = (CUdevice_attribute) 1
 CUDA dev attr max threads per block.
 
constexpr auto cuda::cudaDevAttrMaxThreadsPerMultiProcessor = (CUdevice_attribute) 39
 CUDA dev attr max threads per multi processor.
 

Detailed Description

Definitions for CUDA encoding.

Macro Definition Documentation

◆ CU_CHECK

#define CU_CHECK ( x,
y )
Value:
if (check((x), SUNSHINE_STRINGVIEW(y ": "))) \
return -1
#define SUNSHINE_STRINGVIEW(x)
Macro for SUNSHINE STRINGVIEW.
Definition cuda.cpp:38

Macro for CU CHECK.

◆ CU_CHECK_IGNORE

#define CU_CHECK_IGNORE ( x,
y )
Value:
check((x), SUNSHINE_STRINGVIEW(y ": "))

Macro for CU CHECK IGNORE.

◆ SUNSHINE_STRINGVIEW

#define SUNSHINE_STRINGVIEW ( x)
Value:
#define SUNSHINE_STRINGVIEW_HELPER(x)
Macro for SUNSHINE STRINGVIEW HELPER.
Definition cuda.cpp:33

Macro for SUNSHINE STRINGVIEW.

◆ SUNSHINE_STRINGVIEW_HELPER

#define SUNSHINE_STRINGVIEW_HELPER ( x)
Value:
x##sv

Macro for SUNSHINE STRINGVIEW HELPER.

Function Documentation

◆ cff()

void cuda::cff ( CudaFunctions * cf)

Release a Core Foundation object when the wrapper is destroyed.

Parameters
cfCore Foundation object passed to the scoped releaser.

◆ freeStream()

void cuda::freeStream ( CUstream stream)

Release stream resources.

Parameters
streamCUDA stream or PipeWire stream involved in the operation.

◆ init() [1/2]

int cuda::init ( )

Map CUDA graphics resources for use as an image.

Returns
0 on success; nonzero or negative platform status on failure.

◆ init() [2/2]

int cuda::nvfbc::init ( )

Load NvFBC and create the CUDA capture helper.

Returns
0 on success; nonzero or negative platform status on failure.

◆ make_avcodec_encode_device()

std::unique_ptr< platf::avcodec_encode_device_t > cuda::make_avcodec_encode_device ( int width,
int height,
bool vram )

Create AVCodec encode device.

Parameters
widthFrame or display width in pixels.
heightFrame or display height in pixels.
vramWhether the image should use GPU memory instead of system memory.
Returns
Constructed AVCodec encode device object.

◆ make_avcodec_gl_encode_device()

std::unique_ptr< platf::avcodec_encode_device_t > cuda::make_avcodec_gl_encode_device ( int width,
int height,
int offset_x,
int offset_y )

Create a GL->CUDA encoding device for consuming captured dmabufs.

Parameters
widthWidth of captured frames.
heightHeight of captured frames.
offset_xOffset of content in captured frame.
offset_yOffset of content in captured frame.
Returns
FFmpeg encoding device context.

◆ nvfbc_display()

std::shared_ptr< display_t > platf::nvfbc_display ( mem_type_e hwdevice_type,
const std::string & display_name,
const video::config_t & config )

Create an NvFBC CUDA display capture backend.

Parameters
hwdevice_typeHardware device type requested for capture or encode.
display_nameDisplay name.
configConfiguration values to apply.
Returns
Display backend, or nullptr when NvFBC/CUDA initialization fails.

◆ nvfbc_display_names()

std::vector< std::string > platf::nvfbc_display_names ( )

Enumerate display names exposed by NvFBC.

Returns
NvFBC display names, or an empty list when enumeration fails.

◆ open_drm_fd_for_cuda_device()

file_t cuda::open_drm_fd_for_cuda_device ( int index)

Opens the DRM device associated with the CUDA device index.

Parameters
indexCUDA device index to open.
Returns
File descriptor or -1 on failure.

◆ pass_error()

void cuda::pass_error ( const std::string_view & sv,
const char * name,
const char * description )

Convert a CUDA result code into Sunshine's capture status.

Parameters
svString view containing the text to inspect.
nameHuman-readable name to assign.
descriptionHuman-readable description used in log output.

◆ unregisterResource()

void cuda::unregisterResource ( CUgraphicsResource resource)

Unregister a CUDA graphics resource if it is still registered.

Parameters
resourceCUDA graphics resource being mapped or unmapped.