3#include <ac-gpu/ac-gpu.hpp>
4#include <cuda_runtime_api.h>
15 void check_cuda(cudaError_t result,
const std::string &operation) {
16 if (result != cudaSuccess) {
17 throw std::runtime_error(operation +
": " + cudaGetErrorString(result));
22 if (frame_buffer_size < 4 || frame_buffer_size > 32) {
23 throw std::runtime_error(
"GPU frame buffer must be between 4 and 32");
25 return frame_buffer_size;
31 Impl(std::vector<int> filter_indices,
int frame_buffer_size) :
frame_buffer(std::make_unique<
ac_gpu::DynamicFrameBuffer>(validate_frame_buffer_size(frame_buffer_size))) {
33 for (
int index : filter_indices) {
37 check_cuda(cudaMalloc(&
device_frame_pointers,
static_cast<std::size_t
>(frame_buffer_size) *
sizeof(
unsigned char *)),
"could not allocate CUDA frame-pointer list");
38 std::cout <<
"acmxvk: CUDA filter chain (" <<
filters.size() <<
" filters, " << frame_buffer_size <<
" history frames):\n";
40 std::cout <<
" " << filter.index <<
": " << filter.name <<
'\n';
53 [[nodiscard]]
bool process(
const cv::Mat &rgba) {
54 if (rgba.empty() || rgba.type() != CV_8UC4) {
61 [[nodiscard]]
bool process(
const cv::cuda::GpuMat &rgba, cv::cuda::Stream &source_stream) {
62 if (rgba.empty() || rgba.type() != CV_8UC4) {
66 source_stream.waitForCompletion();
79 launch_filter(
filters.data(),
filters.size(),
working_buffer.ptr<
unsigned char>(),
device_frame_pointers,
frame_buffer->
arraySize,
working_buffer.cols,
working_buffer.rows,
working_buffer.step,
alpha,
false,
square_size,
frame_index,
frame_direction, &
device_filter_list,
filters_changed);
85 if (direction == 0 ||
filters.empty()) {
89 const int step = direction < 0 ? -1 : 1;
91 const int current_index =
filters.front().index;
92 const int next_index = (current_index + step + filter_count) % filter_count;
95 std::cout <<
"acmxvk: CUDA filter: " <<
filters.front().name <<
" [" << next_index <<
"]\n";
100 std::string description;
102 if (!description.empty()) {
105 description += filter.name;
106 description +=
" [" + std::to_string(filter.index) +
"]";
116 }
else if (
alpha <= 1.0F) {
152 bool FilterEngine::process(
const cv::cuda::GpuMat &rgba, cv::cuda::Stream &source_stream) {
return impl->process(rgba, source_stream); }
163 int device_count = 0;
164 check_cuda(cudaGetDeviceCount(&device_count),
"could not enumerate CUDA devices");
165 if (device_index < 0 || device_index >= device_count) {
166 throw std::runtime_error(
"CUDA device index must be between 0 and " + std::to_string(std::max(device_count - 1, 0)));
168 check_cuda(cudaSetDevice(device_index),
"could not select CUDA device");
169 const cv::cuda::DeviceInfo device(device_index);
170 std::cout <<
"acmxvk: CUDA device " << device_index <<
": " << device.name() <<
'\n';
174 if (filter_indices.empty()) {
175 throw std::runtime_error(
"CUDA filter list cannot be empty");
177 for (
int index : filter_indices) {
179 throw std::runtime_error(
"CUDA filter index must be between 0 and " + std::to_string(
ac_gpu::AC_FILTER_MAX - 1) +
": " + std::to_string(index));
185 int device_count = 0;
186 check_cuda(cudaGetDeviceCount(&device_count),
"could not enumerate CUDA devices");
187 output_stream <<
"acmxvk: found " << device_count <<
" CUDA device(s)\n";
188 for (
int index = 0; index < device_count; ++index) {
189 const cv::cuda::DeviceInfo device(index);
190 output_stream <<
" " << index <<
": " << device.name() <<
" (" << (device.totalMemory() / (1024U * 1024U)) <<
" MiB)\n";
ac_gpu::GPUFilter * device_filter_list
cv::cuda::GpuMat working_buffer
std::string active_filter_description() const
bool process(const cv::Mat &rgba)
std::vector< ac_gpu::Filter > filters
Impl(std::vector< int > filter_indices, int frame_buffer_size)
bool process(const cv::cuda::GpuMat &rgba, cv::cuda::Stream &source_stream)
bool select_relative_filter(int direction)
std::unique_ptr< ac_gpu::DynamicFrameBuffer > frame_buffer
bool process_current_frame()
cv::cuda::Stream upload_stream
unsigned char ** device_frame_pointers
static void select_device(int device_index)
static void validate_filter_indices(const std::vector< int > &filter_indices)
std::unique_ptr< Impl > impl
cv::cuda::Stream & stream()
static void list_filters(std::ostream &output)
std::string active_filter_description() const
const cv::cuda::GpuMat & output() const
static void list_devices(std::ostream &output)
bool select_relative_filter(int direction)
FilterEngine(std::vector< int > filter_indices, int frame_buffer_size)
bool process(const cv::Mat &rgba)
constexpr int AC_FILTER_MAX
int validate_frame_buffer_size(int frame_buffer_size)
void check_cuda(cudaError_t result, const std::string &operation)