feat(render): dynamic OpenGL/Vulkan backend split and backend-neutral viewer
- Extract libolive-rendercore static library to minimize backend link boundary. - Add DynamicRenderer adapter with C ABI (oakgl/oakvulkan shared libs). - Make OAK_ENABLE_DYNAMIC_RENDER_BACKEND default ON with OpenGL fallback. - Implement VulkanRenderer prototype (textures, shaders, UBO blit, readback). - Add backend-neutral viewer readback path (offscreen -> QImage -> QPainter). - Refactor PluginRenderer to be renderer-agnostic; OFX plugins fall back to CPU path on non-OpenGL backends while preserving OpenGL render path. - Add Renderer::AttachOutputTexture/DetachOutputTexture and C ABI forwards. - Update docs/zh/render-backend-dynamic-plan.md for Phase 3/4/5.
This commit is contained in:
@@ -1,41 +1,47 @@
|
||||
#include "render/backend/renderbackend_c.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QOpenGLContext>
|
||||
#include <QPointF>
|
||||
#include <QVariant>
|
||||
|
||||
#include "render/job/acceleratedjob.h"
|
||||
#include "render/shadercode.h"
|
||||
#include "render/texture.h"
|
||||
#include "render/videoparams.h"
|
||||
#include "render/vulkan/vulkanrenderer.h"
|
||||
|
||||
namespace {
|
||||
|
||||
struct VulkanBackend {
|
||||
bool warned = false;
|
||||
class BackendVulkanRenderer : public olive::VulkanRenderer {
|
||||
public:
|
||||
using olive::VulkanRenderer::VulkanRenderer;
|
||||
using olive::VulkanRenderer::Blit;
|
||||
using olive::VulkanRenderer::CreateNativeTexture;
|
||||
using olive::VulkanRenderer::DestroyInternal;
|
||||
using olive::VulkanRenderer::DestroyNativeTexture;
|
||||
};
|
||||
|
||||
VulkanBackend *Backend(OakRenderBackendHandle handle)
|
||||
BackendVulkanRenderer *Renderer(OakRenderBackendHandle handle)
|
||||
{
|
||||
return static_cast<VulkanBackend *>(handle);
|
||||
return static_cast<BackendVulkanRenderer *>(handle);
|
||||
}
|
||||
|
||||
void WarnUnavailable(OakRenderBackendHandle handle, const char *function)
|
||||
const QVariant &VariantRef(const void *variant)
|
||||
{
|
||||
auto *backend = Backend(handle);
|
||||
if (!backend || backend->warned) {
|
||||
return;
|
||||
}
|
||||
backend->warned = true;
|
||||
qWarning() << "Vulkan render backend is a C ABI placeholder; function"
|
||||
<< function << "is not implemented yet";
|
||||
return *static_cast<const QVariant *>(variant);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT OakRenderBackendHandle oak_renderer_create(void *parent)
|
||||
{
|
||||
Q_UNUSED(parent)
|
||||
return new VulkanBackend();
|
||||
return new BackendVulkanRenderer(static_cast<QObject *>(parent));
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_destroy(OakRenderBackendHandle handle)
|
||||
{
|
||||
delete Backend(handle);
|
||||
delete Renderer(handle);
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT bool oak_renderer_get_info(
|
||||
@@ -46,146 +52,134 @@ OAK_RENDER_BACKEND_EXPORT bool oak_renderer_get_info(
|
||||
}
|
||||
out_info->abi_version = 1;
|
||||
out_info->kind = OAK_RENDER_BACKEND_VULKAN;
|
||||
out_info->capabilities = 0;
|
||||
out_info->capabilities = OAK_RENDER_BACKEND_CAP_TEXTURES |
|
||||
OAK_RENDER_BACKEND_CAP_SHADERS | OAK_RENDER_BACKEND_CAP_BLIT |
|
||||
OAK_RENDER_BACKEND_CAP_READBACK;
|
||||
out_info->name = "vulkan";
|
||||
out_info->status = "placeholder-unavailable";
|
||||
out_info->status = Renderer(handle)->IsAvailable() ? "available" : "unavailable";
|
||||
return true;
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT bool oak_renderer_is_available(
|
||||
OakRenderBackendHandle handle)
|
||||
{
|
||||
Q_UNUSED(handle)
|
||||
return false;
|
||||
auto *r = Renderer(handle);
|
||||
if (!r || r->IsAvailable()) {
|
||||
return r && r->IsAvailable();
|
||||
}
|
||||
// Try to initialize if not already available
|
||||
if (r->Init()) {
|
||||
r->PostInit();
|
||||
}
|
||||
return r->IsAvailable();
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT bool oak_renderer_init(OakRenderBackendHandle handle)
|
||||
{
|
||||
WarnUnavailable(handle, "init");
|
||||
return false;
|
||||
return Renderer(handle)->Init();
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_init_with_context(
|
||||
OakRenderBackendHandle handle, void *context)
|
||||
{
|
||||
Q_UNUSED(context)
|
||||
WarnUnavailable(handle, "init_with_context");
|
||||
Renderer(handle)->Init();
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_post_init(
|
||||
OakRenderBackendHandle handle)
|
||||
{
|
||||
Q_UNUSED(handle)
|
||||
Renderer(handle)->PostInit();
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_post_destroy(
|
||||
OakRenderBackendHandle handle)
|
||||
{
|
||||
Q_UNUSED(handle)
|
||||
Renderer(handle)->PostDestroy();
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_destroy_internal(
|
||||
OakRenderBackendHandle handle)
|
||||
{
|
||||
Q_UNUSED(handle)
|
||||
Renderer(handle)->DestroyInternal();
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_clear_destination(
|
||||
OakRenderBackendHandle handle, void *texture, double r, double g, double b,
|
||||
double a)
|
||||
{
|
||||
Q_UNUSED(texture)
|
||||
Q_UNUSED(r)
|
||||
Q_UNUSED(g)
|
||||
Q_UNUSED(b)
|
||||
Q_UNUSED(a)
|
||||
WarnUnavailable(handle, "clear_destination");
|
||||
Renderer(handle)->ClearDestination(static_cast<olive::Texture *>(texture),
|
||||
r, g, b, a);
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_create_native_texture(
|
||||
OakRenderBackendHandle handle, int width, int height, int depth, int format,
|
||||
int channel_count, const void *data, int linesize, void *out_variant)
|
||||
{
|
||||
Q_UNUSED(width)
|
||||
Q_UNUSED(height)
|
||||
Q_UNUSED(depth)
|
||||
Q_UNUSED(format)
|
||||
Q_UNUSED(channel_count)
|
||||
Q_UNUSED(data)
|
||||
Q_UNUSED(linesize)
|
||||
static_cast<QVariant *>(out_variant)->clear();
|
||||
WarnUnavailable(handle, "create_native_texture");
|
||||
*static_cast<QVariant *>(out_variant) = Renderer(handle)->CreateNativeTexture(
|
||||
width, height, depth, static_cast<olive::PixelFormat::Format>(format),
|
||||
channel_count, data, linesize);
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_destroy_native_texture(
|
||||
OakRenderBackendHandle handle, const void *variant)
|
||||
{
|
||||
Q_UNUSED(variant)
|
||||
WarnUnavailable(handle, "destroy_native_texture");
|
||||
Renderer(handle)->DestroyNativeTexture(VariantRef(variant));
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_create_native_shader(
|
||||
OakRenderBackendHandle handle, const void *shader_code, void *out_variant)
|
||||
{
|
||||
Q_UNUSED(shader_code)
|
||||
static_cast<QVariant *>(out_variant)->clear();
|
||||
WarnUnavailable(handle, "create_native_shader");
|
||||
*static_cast<QVariant *>(out_variant) = Renderer(handle)->CreateNativeShader(
|
||||
*static_cast<const olive::ShaderCode *>(shader_code));
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_destroy_native_shader(
|
||||
OakRenderBackendHandle handle, const void *variant)
|
||||
{
|
||||
Q_UNUSED(variant)
|
||||
WarnUnavailable(handle, "destroy_native_shader");
|
||||
Renderer(handle)->DestroyNativeShader(VariantRef(variant));
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_upload_to_texture(
|
||||
OakRenderBackendHandle handle, const void *variant, const void *video_params,
|
||||
const void *data, int linesize)
|
||||
{
|
||||
Q_UNUSED(variant)
|
||||
Q_UNUSED(video_params)
|
||||
Q_UNUSED(data)
|
||||
Q_UNUSED(linesize)
|
||||
WarnUnavailable(handle, "upload_to_texture");
|
||||
Renderer(handle)->UploadToTexture(
|
||||
VariantRef(variant), *static_cast<const olive::VideoParams *>(video_params),
|
||||
data, linesize);
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_download_from_texture(
|
||||
OakRenderBackendHandle handle, const void *variant, const void *video_params,
|
||||
void *data, int linesize)
|
||||
{
|
||||
Q_UNUSED(variant)
|
||||
Q_UNUSED(video_params)
|
||||
Q_UNUSED(data)
|
||||
Q_UNUSED(linesize)
|
||||
WarnUnavailable(handle, "download_from_texture");
|
||||
Renderer(handle)->DownloadFromTexture(
|
||||
VariantRef(variant), *static_cast<const olive::VideoParams *>(video_params),
|
||||
data, linesize);
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_flush(OakRenderBackendHandle handle)
|
||||
{
|
||||
Q_UNUSED(handle)
|
||||
Renderer(handle)->Flush();
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_get_pixel_from_texture(
|
||||
OakRenderBackendHandle handle, void *texture, const void *point,
|
||||
void *out_color)
|
||||
{
|
||||
Q_UNUSED(texture)
|
||||
Q_UNUSED(point)
|
||||
Q_UNUSED(out_color)
|
||||
WarnUnavailable(handle, "get_pixel_from_texture");
|
||||
*static_cast<olive::Color *>(out_color) = Renderer(handle)->GetPixelFromTexture(
|
||||
static_cast<olive::Texture *>(texture), *static_cast<const QPointF *>(point));
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_blit(
|
||||
OakRenderBackendHandle handle, const void *shader, void *job,
|
||||
void *destination, const void *destination_params, bool clear_destination)
|
||||
{
|
||||
Q_UNUSED(shader)
|
||||
Q_UNUSED(job)
|
||||
Q_UNUSED(destination)
|
||||
Q_UNUSED(destination_params)
|
||||
Q_UNUSED(clear_destination)
|
||||
WarnUnavailable(handle, "blit");
|
||||
Renderer(handle)->Blit(
|
||||
VariantRef(shader), *static_cast<olive::AcceleratedJob *>(job),
|
||||
static_cast<olive::Texture *>(destination),
|
||||
*static_cast<const olive::VideoParams *>(destination_params),
|
||||
clear_destination);
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void *oak_renderer_opengl_context(
|
||||
@@ -194,3 +188,18 @@ OAK_RENDER_BACKEND_EXPORT void *oak_renderer_opengl_context(
|
||||
Q_UNUSED(handle)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_attach_output_texture(
|
||||
OakRenderBackendHandle handle, const void *texture_id)
|
||||
{
|
||||
Q_UNUSED(handle)
|
||||
Q_UNUSED(texture_id)
|
||||
// Vulkan does not support OFX OpenGL render output attachment.
|
||||
}
|
||||
|
||||
OAK_RENDER_BACKEND_EXPORT void oak_renderer_detach_output_texture(
|
||||
OakRenderBackendHandle handle)
|
||||
{
|
||||
Q_UNUSED(handle)
|
||||
// Vulkan does not support OFX OpenGL render output attachment.
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,159 @@
|
||||
/***************************************************************************
|
||||
|
||||
Oak Video Editor
|
||||
Copyright (C) 2025 mikesolar
|
||||
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef VULKANRENDERER_H
|
||||
#define VULKANRENDERER_H
|
||||
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
#include <QMutex>
|
||||
#include <QObject>
|
||||
#include <QVariant>
|
||||
|
||||
#include "render/renderer.h"
|
||||
|
||||
namespace olive
|
||||
{
|
||||
|
||||
class VulkanRenderer : public Renderer {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit VulkanRenderer(QObject *parent = nullptr);
|
||||
virtual ~VulkanRenderer() override;
|
||||
|
||||
virtual bool Init() override;
|
||||
virtual void PostInit() override;
|
||||
virtual void PostDestroy() override;
|
||||
|
||||
virtual void ClearDestination(olive::Texture *texture = nullptr,
|
||||
double r = 0.0, double g = 0.0,
|
||||
double b = 0.0, double a = 0.0) override;
|
||||
|
||||
virtual QVariant CreateNativeShader(olive::ShaderCode code) override;
|
||||
virtual void DestroyNativeShader(QVariant shader) override;
|
||||
|
||||
virtual void UploadToTexture(const QVariant &handle,
|
||||
const VideoParams ¶ms, const void *data,
|
||||
int linesize) override;
|
||||
virtual void DownloadFromTexture(const QVariant &handle,
|
||||
const VideoParams ¶ms, void *data,
|
||||
int linesize) override;
|
||||
|
||||
virtual void Flush() override;
|
||||
|
||||
virtual Color GetPixelFromTexture(olive::Texture *texture,
|
||||
const QPointF &pt) override;
|
||||
|
||||
bool IsAvailable() const
|
||||
{
|
||||
return device_ != VK_NULL_HANDLE;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void Blit(QVariant shader, olive::AcceleratedJob &job,
|
||||
olive::Texture *destination, VideoParams destination_params,
|
||||
bool clear_destination) override;
|
||||
virtual QVariant CreateNativeTexture(int width, int height, int depth,
|
||||
PixelFormat format, int channel_count,
|
||||
const void *data = nullptr,
|
||||
int linesize = 0) override;
|
||||
virtual void DestroyNativeTexture(QVariant texture) override;
|
||||
virtual void DestroyInternal() override;
|
||||
|
||||
private:
|
||||
struct VulkanTexture;
|
||||
struct VulkanShader;
|
||||
struct UniformInfo;
|
||||
|
||||
bool CreateInstance();
|
||||
bool CreateDevice();
|
||||
bool CreateCommandPool();
|
||||
bool CreateDescriptorPool();
|
||||
bool CreateVertexBuffer();
|
||||
bool CreateLinearSampler();
|
||||
bool CreateStagingBuffer(VkDeviceSize size, VkBuffer *out_buffer,
|
||||
VkDeviceMemory *out_memory);
|
||||
void DestroyStagingBuffer(VkBuffer buffer, VkDeviceMemory memory);
|
||||
|
||||
VkCommandBuffer BeginOneTimeCommands();
|
||||
void EndOneTimeCommands(VkCommandBuffer cmd);
|
||||
|
||||
void TransitionImageLayout(VkCommandBuffer cmd, VkImage image,
|
||||
VkImageLayout old_layout,
|
||||
VkImageLayout new_layout);
|
||||
void CopyBufferToImage(VkCommandBuffer cmd, VkBuffer buffer, VkImage image,
|
||||
uint32_t width, uint32_t height, uint32_t depth);
|
||||
void CopyImageToBuffer(VkCommandBuffer cmd, VkImage image, VkBuffer buffer,
|
||||
uint32_t width, uint32_t height,
|
||||
uint32_t offset_x = 0, uint32_t offset_y = 0);
|
||||
|
||||
VkFormat PixelFormatToVkFormat(PixelFormat format, int channel_count) const;
|
||||
VkDeviceSize AlignSize(VkDeviceSize size, VkDeviceSize alignment) const;
|
||||
|
||||
uint32_t FindMemoryType(uint32_t type_filter,
|
||||
VkMemoryPropertyFlags properties) const;
|
||||
|
||||
bool CompileGlslToSpv(const QString &glsl, VkShaderStageFlagBits stage,
|
||||
QByteArray *out_spv);
|
||||
QString ConvertGlslToVulkan(const QString &glsl, VkShaderStageFlagBits stage);
|
||||
QString ConvertGlslUniformsToUbo(const QString &glsl,
|
||||
QVector<UniformInfo> *out_uniforms);
|
||||
VkDeviceSize GetStd140Size(const QString &type) const;
|
||||
VkDeviceSize GetStd140Alignment(const QString &type) const;
|
||||
|
||||
bool CreatePipelineForShader(VulkanShader *shader,
|
||||
const VideoParams &dest_params,
|
||||
VkFormat render_pass_format);
|
||||
|
||||
VkRenderPass GetOrCreateRenderPass(VkFormat format);
|
||||
|
||||
VkInstance instance_ = VK_NULL_HANDLE;
|
||||
VkPhysicalDevice physical_device_ = VK_NULL_HANDLE;
|
||||
VkDevice device_ = VK_NULL_HANDLE;
|
||||
VkQueue graphics_queue_ = VK_NULL_HANDLE;
|
||||
uint32_t graphics_queue_family_ = UINT32_MAX;
|
||||
VkCommandPool command_pool_ = VK_NULL_HANDLE;
|
||||
VkDescriptorPool descriptor_pool_ = VK_NULL_HANDLE;
|
||||
VkSampler linear_sampler_ = VK_NULL_HANDLE;
|
||||
|
||||
QHash<VkFormat, VkRenderPass> render_pass_cache_;
|
||||
|
||||
VkBuffer vertex_buffer_ = VK_NULL_HANDLE;
|
||||
VkDeviceMemory vertex_buffer_memory_ = VK_NULL_HANDLE;
|
||||
|
||||
VkPhysicalDeviceMemoryProperties mem_properties_;
|
||||
VkPhysicalDeviceProperties device_properties_;
|
||||
|
||||
QMutex mutex_;
|
||||
|
||||
// Texture handle counter
|
||||
quint64 next_texture_id_ = 1;
|
||||
QHash<quint64, VulkanTexture *> textures_;
|
||||
|
||||
// Shader handle counter
|
||||
quint64 next_shader_id_ = 1;
|
||||
QHash<quint64, VulkanShader *> shaders_;
|
||||
|
||||
static const int kMaxDescriptorSets = 1024;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // VULKANRENDERER_H
|
||||
Reference in New Issue
Block a user