ACMX 2.139.0
Dual-Backend Real-Time GPU Video Synthesis
Loading...
Searching...
No Matches
gpu_filters.cpp
Go to the documentation of this file.
1#include "gpu_filters.hpp"
2
3#include <ac-gpu/ac-gpu.hpp>
4#include <cuda_runtime_api.h>
5
6#include <algorithm>
7#include <cstddef>
8#include <iostream>
9#include <stdexcept>
10#include <string>
11#include <utility>
12
13namespace acmxvk::gpu {
14 namespace {
15 void check_cuda(cudaError_t result, const std::string &operation) {
16 if (result != cudaSuccess) {
17 throw std::runtime_error(operation + ": " + cudaGetErrorString(result));
18 }
19 }
20
21 int validate_frame_buffer_size(int frame_buffer_size) {
22 if (frame_buffer_size < 4 || frame_buffer_size > 32) {
23 throw std::runtime_error("GPU frame buffer must be between 4 and 32");
24 }
25 return frame_buffer_size;
26 }
27 } // namespace
28
30 public:
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) {
34 filters.push_back({index, ac_gpu::filters[index].name});
35 }
36
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";
39 for (const ac_gpu::Filter &filter : filters) {
40 std::cout << " " << filter.index << ": " << filter.name << '\n';
41 }
42 }
43
45 if (device_filter_list != nullptr) {
46 cudaFree(device_filter_list);
47 }
48 if (device_frame_pointers != nullptr) {
49 cudaFree(device_frame_pointers);
50 }
51 }
52
53 [[nodiscard]] bool process(const cv::Mat &rgba) {
54 if (rgba.empty() || rgba.type() != CV_8UC4) {
55 return false;
56 }
57 frame_buffer->update(rgba);
58 return process_current_frame();
59 }
60
61 [[nodiscard]] bool process(const cv::cuda::GpuMat &rgba, cv::cuda::Stream &source_stream) {
62 if (rgba.empty() || rgba.type() != CV_8UC4) {
63 return false;
64 }
65 frame_buffer->update(rgba, source_stream);
66 source_stream.waitForCompletion();
67 return process_current_frame();
68 }
69
70 [[nodiscard]] bool process_current_frame() {
71 if (working_buffer.empty() || working_buffer.cols != frame_buffer->w || working_buffer.rows != frame_buffer->h) {
72 working_buffer.create(frame_buffer->h, frame_buffer->w, CV_8UC4);
73 }
74
76 check_cuda(cudaMemcpy(device_frame_pointers, frame_buffer->getDeviceFramePointers(), static_cast<std::size_t>(frame_buffer->arraySize) * sizeof(unsigned char *), cudaMemcpyHostToDevice), "could not upload CUDA frame-pointer list");
77 check_cuda(cudaMemcpy2D(working_buffer.ptr<unsigned char>(), working_buffer.step, frame_buffer->deviceFrames.back().ptr<unsigned char>(), frame_buffer->framePitch, static_cast<std::size_t>(frame_buffer->w) * 4U, static_cast<std::size_t>(frame_buffer->h), cudaMemcpyDeviceToDevice), "could not prepare CUDA filter frame");
78
80 filters_changed = false;
81 return true;
82 }
83
84 [[nodiscard]] bool select_relative_filter(int direction) {
85 if (direction == 0 || filters.empty()) {
86 return false;
87 }
88
89 const int step = direction < 0 ? -1 : 1;
90 const int filter_count = ac_gpu::AC_FILTER_MAX;
91 const int current_index = filters.front().index;
92 const int next_index = (current_index + step + filter_count) % filter_count;
93 filters.assign(1, {next_index, ac_gpu::filters[next_index].name});
94 filters_changed = true;
95 std::cout << "acmxvk: CUDA filter: " << filters.front().name << " [" << next_index << "]\n";
96 return true;
97 }
98
99 [[nodiscard]] std::string active_filter_description() const {
100 std::string description;
101 for (const ac_gpu::Filter &filter : filters) {
102 if (!description.empty()) {
103 description += ", ";
104 }
105 description += filter.name;
106 description += " [" + std::to_string(filter.index) + "]";
107 }
108 return description;
109 }
110
112 alpha += alpha_direction > 0 ? 0.01F : -0.01F;
113 if (alpha >= 3.0F) {
114 alpha = 3.0F;
115 alpha_direction = -1;
116 } else if (alpha <= 1.0F) {
117 alpha = 1.0F;
118 alpha_direction = 1;
119 }
120
122 const int last = frame_buffer->arraySize - 1;
123 if (frame_index >= last) {
124 frame_index = last;
125 frame_direction = -1;
126 } else if (frame_index <= 0) {
127 frame_index = 0;
128 frame_direction = 1;
129 }
130 }
131
132 std::vector<ac_gpu::Filter> filters;
133 std::unique_ptr<ac_gpu::DynamicFrameBuffer> frame_buffer;
134 cv::cuda::GpuMat working_buffer;
135 cv::cuda::Stream upload_stream;
136 unsigned char **device_frame_pointers = nullptr;
138 bool filters_changed = true;
139 float alpha = 1.0F;
141 int square_size = 8;
142 int frame_index = 0;
144 };
145
146 FilterEngine::FilterEngine(std::vector<int> filter_indices, int frame_buffer_size) : impl(std::make_unique<Impl>(std::move(filter_indices), frame_buffer_size)) {}
147
148 FilterEngine::~FilterEngine() = default;
149
150 bool FilterEngine::process(const cv::Mat &rgba) { return impl->process(rgba); }
151
152 bool FilterEngine::process(const cv::cuda::GpuMat &rgba, cv::cuda::Stream &source_stream) { return impl->process(rgba, source_stream); }
153
154 bool FilterEngine::select_relative_filter(int direction) { return impl->select_relative_filter(direction); }
155
156 std::string FilterEngine::active_filter_description() const { return impl->active_filter_description(); }
157
158 const cv::cuda::GpuMat &FilterEngine::output() const { return impl->working_buffer; }
159
160 cv::cuda::Stream &FilterEngine::stream() { return impl->upload_stream; }
161
162 void FilterEngine::select_device(int device_index) {
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)));
167 }
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';
171 }
172
173 void FilterEngine::validate_filter_indices(const std::vector<int> &filter_indices) {
174 if (filter_indices.empty()) {
175 throw std::runtime_error("CUDA filter list cannot be empty");
176 }
177 for (int index : filter_indices) {
178 if (index < 0 || index >= ac_gpu::AC_FILTER_MAX) {
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));
180 }
181 }
182 }
183
184 void FilterEngine::list_devices(std::ostream &output_stream) {
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";
191 }
192 }
193
194 void FilterEngine::list_filters(std::ostream &output_stream) {
195 output_stream << "acmxvk: found " << ac_gpu::AC_FILTER_MAX << " CUDA filter(s)\n";
196 for (int index = 0; index < ac_gpu::AC_FILTER_MAX; ++index) {
197 output_stream << " " << index << ": " << ac_gpu::filters[index].name << '\n';
198 }
199 }
200
201} // namespace acmxvk::gpu
ac_gpu::GPUFilter * device_filter_list
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
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
Filter filters[1]
int validate_frame_buffer_size(int frame_buffer_size)
void check_cuda(cudaError_t result, const std::string &operation)
std::string name