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

Vulkan-native encoder: DMA-BUF -> Vulkan compute (RGB->YUV) -> Vulkan Video encode. No EGL/GL dependency — all GPU work stays in a single Vulkan queue. More...

#include <algorithm>
#include <array>
#include <cstdint>
#include <drm_fourcc.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <vector>
#include <vulkan/vulkan.h>
#include <libavcodec/avcodec.h>
#include <libavutil/hwcontext.h>
#include <libavutil/hwcontext_vulkan.h>
#include "graphics.h"
#include "src/config.h"
#include "src/logging.h"
#include "src/video_colorspace.h"
#include "vulkan_encode.h"
#include "shaders/rgb2yuv.spv.inc"
Include dependency graph for vulkan_encode.cpp:

Classes

struct  vk::PushConstants
 Vulkan shader constants used by the conversion pass. More...
 
class  vk::vk_vram_t
 Vulkan encode device that keeps converted frames in GPU memory. More...
 

Macros

#define VK_CHECK(expr)
 Macro for VK CHECK.
 
#define VK_CHECK_BOOL(expr)
 Macro for VK CHECK BOOL.
 

Functions

std::unique_ptr< platf::avcodec_encode_device_tvk::make_avcodec_encode_device_ram (int width, int height)
 Create a Vulkan encode device for RAM capture.
 
std::unique_ptr< platf::avcodec_encode_device_tvk::make_avcodec_encode_device_vram (int width, int height, int offset_x, int offset_y)
 Create a Vulkan encode device for VRAM capture.
 
bool vk::validate ()
 Check if FFmpeg Vulkan Video encoding is available.
 
int vk::vulkan_init_avcodec_hardware_input_buffer (platf::avcodec_encode_device_t *encode_device, AVBufferRef **hw_device_buf)
 Initialize Vulkan hardware device for FFmpeg encoding.
 

Detailed Description

Vulkan-native encoder: DMA-BUF -> Vulkan compute (RGB->YUV) -> Vulkan Video encode. No EGL/GL dependency — all GPU work stays in a single Vulkan queue.

Macro Definition Documentation

◆ VK_CHECK

#define VK_CHECK ( expr)
Value:
do { \
VkResult _r = (expr); \
if (_r != VK_SUCCESS) { \
BOOST_LOG(error) << #expr << " failed: " << _r; \
return -1; \
} \
} while (0)
bl::sources::severity_logger< int > error
Recoverable errors.
Definition logging.cpp:44

Macro for VK CHECK.

◆ VK_CHECK_BOOL

#define VK_CHECK_BOOL ( expr)
Value:
do { \
VkResult _r = (expr); \
if (_r != VK_SUCCESS) { \
BOOST_LOG(error) << #expr << " failed: " << _r; \
return false; \
} \
} while (0)

Macro for VK CHECK BOOL.

Function Documentation

◆ make_avcodec_encode_device_ram()

std::unique_ptr< platf::avcodec_encode_device_t > vk::make_avcodec_encode_device_ram ( int width,
int height )

Create a Vulkan encode device for RAM capture.

Parameters
widthFrame or display width in pixels.
heightFrame or display height in pixels.
Returns
Constructed AVCodec encode device ram object.

◆ make_avcodec_encode_device_vram()

std::unique_ptr< platf::avcodec_encode_device_t > vk::make_avcodec_encode_device_vram ( int width,
int height,
int offset_x,
int offset_y )

Create a Vulkan encode device for VRAM capture.

Parameters
widthFrame or display width in pixels.
heightFrame or display height in pixels.
offset_xOffset x.
offset_yOffset y.
Returns
Constructed AVCodec encode device VRAM object.

◆ validate()

bool vk::validate ( )

Check if FFmpeg Vulkan Video encoding is available.

Returns
True when FFmpeg Vulkan Video encoding is available.

◆ vulkan_init_avcodec_hardware_input_buffer()

int vk::vulkan_init_avcodec_hardware_input_buffer ( platf::avcodec_encode_device_t * encode_device,
AVBufferRef ** hw_device_buf )

Initialize Vulkan hardware device for FFmpeg encoding.

Parameters
encode_deviceThe encode device (vk_t).
hw_device_bufOutput hardware device buffer.
Returns
0 on success, negative on error.