add Comments

This commit is contained in:
2026-07-13 10:19:30 +08:00
parent 3c9592da45
commit e77de49406
12 changed files with 444 additions and 16 deletions
+59
View File
@@ -35,29 +35,45 @@ namespace olive
class VulkanRenderer : public Renderer {
Q_OBJECT
public:
// Creates a renderer object; Vulkan objects are created lazily in Init().
explicit VulkanRenderer(QObject *parent = nullptr);
// Releases Vulkan objects through the normal Renderer destruction path.
virtual ~VulkanRenderer() override;
// Creates the Vulkan instance, logical device, command pool, and descriptor
// pool required for offscreen rendering.
virtual bool Init() override;
// Creates reusable GPU resources that require a fully initialized device.
virtual void PostInit() override;
// Reserved for symmetry with OpenGLRenderer; Vulkan cleanup is handled by
// DestroyInternal().
virtual void PostDestroy() override;
// Clears either a texture render target or the currently bound output target.
virtual void ClearDestination(olive::Texture *texture = nullptr,
double r = 0.0, double g = 0.0,
double b = 0.0, double a = 0.0) override;
// Compiles GLSL to SPIR-V, creates shader modules, and prepares descriptor
// metadata for later blits.
virtual QVariant CreateNativeShader(olive::ShaderCode code) override;
// Destroys shader modules, descriptor layout, pipeline layout, and cached
// pipelines associated with a shader handle.
virtual void DestroyNativeShader(QVariant shader) override;
// Uploads CPU pixel data to a Vulkan image via a staging buffer.
virtual void UploadToTexture(const QVariant &handle,
const VideoParams &params, const void *data,
int linesize) override;
// Downloads a Vulkan image to CPU memory via a staging buffer.
virtual void DownloadFromTexture(const QVariant &handle,
const VideoParams &params, void *data,
int linesize) override;
// Waits for outstanding device work to complete.
virtual void Flush() override;
// Reads a single texture pixel using a one-pixel transfer readback.
virtual Color GetPixelFromTexture(olive::Texture *texture,
const QPointF &pt) override;
@@ -67,14 +83,19 @@ public:
}
protected:
// Runs one or more fullscreen shader passes into the destination texture.
virtual void Blit(QVariant shader, olive::AcceleratedJob &job,
olive::Texture *destination, VideoParams destination_params,
bool clear_destination) override;
// Creates a Vulkan image/view/memory bundle and optionally uploads initial
// pixel data.
virtual QVariant CreateNativeTexture(int width, int height, int depth,
PixelFormat format, int channel_count,
const void *data = nullptr,
int linesize = 0) override;
// Releases a Vulkan texture bundle.
virtual void DestroyNativeTexture(QVariant texture) override;
// Releases all Vulkan device resources owned by this renderer.
virtual void DestroyInternal() override;
private:
@@ -82,62 +103,99 @@ private:
struct VulkanShader;
struct UniformInfo;
// Creates the Vulkan instance used for all offscreen work.
bool CreateInstance();
// Chooses a graphics-capable physical device and creates the logical device.
bool CreateDevice();
// Creates a command pool for short-lived command buffers.
bool CreateCommandPool();
// Creates the descriptor pool used for per-blit UBO/sampler sets.
bool CreateDescriptorPool();
// Uploads the fullscreen quad vertex buffer used by BlitPass().
bool CreateVertexBuffer();
// Creates the persistent linear sampler.
bool CreateLinearSampler();
// Creates the persistent nearest-neighbor sampler.
bool CreateNearestSampler();
// Returns the persistent sampler matching the requested interpolation mode.
VkSampler GetSampler(Texture::Interpolation interpolation) const;
// Allocates a host-visible staging buffer for upload/download transfers.
bool CreateStagingBuffer(VkDeviceSize size, VkBuffer *out_buffer,
VkDeviceMemory *out_memory);
// Destroys a staging buffer pair allocated by CreateStagingBuffer().
void DestroyStagingBuffer(VkBuffer buffer, VkDeviceMemory memory);
// Begins a one-shot command buffer and records it immediately.
VkCommandBuffer BeginOneTimeCommands();
// Submits and waits for a one-shot command buffer.
void EndOneTimeCommands(VkCommandBuffer cmd);
// Emits an image memory barrier for the subset of layouts this renderer uses.
void TransitionImageLayout(VkCommandBuffer cmd, VkImage image,
VkImageLayout old_layout,
VkImageLayout new_layout);
// Records a tightly packed buffer-to-image copy.
void CopyBufferToImage(VkCommandBuffer cmd, VkBuffer buffer, VkImage image,
uint32_t width, uint32_t height, uint32_t depth);
// Records an image-to-buffer copy, optionally reading one pixel offset.
void CopyImageToBuffer(VkCommandBuffer cmd, VkImage image, VkBuffer buffer,
uint32_t width, uint32_t height,
uint32_t offset_x = 0, uint32_t offset_y = 0);
// Converts Oak pixel format/channel metadata to a preferred Vulkan format.
VkFormat PixelFormatToVkFormat(PixelFormat format, int channel_count) const;
// Picks a color-attachment-capable format, falling back from RGB to RGBA
// where drivers do not support 3-channel render targets.
VkFormat PickRenderableFormat(PixelFormat format, int channel_count) const;
// Checks whether a format can be used as a render target.
bool IsColorAttachmentSupported(VkFormat format) const;
// Returns the packed byte size for supported VkFormat values.
int GetVkFormatBytesPerPixel(VkFormat format) const;
// Returns the alpha fill value used when expanding RGB data to RGBA.
float GetFormatMaxAlpha(PixelFormat format) const;
// Repackages tightly packed pixels when the requested CPU channel count
// differs from the selected GPU format channel count.
void CopyPixelsWithChannelConversion(const void *src, void *dst,
int width, int height, int depth,
int src_channels, int dst_channels,
PixelFormat format) const;
// Rounds a size up to the requested alignment.
VkDeviceSize AlignSize(VkDeviceSize size, VkDeviceSize alignment) const;
// Finds a Vulkan memory type matching the requested properties.
uint32_t FindMemoryType(uint32_t type_filter,
VkMemoryPropertyFlags properties) const;
// Compiles GLSL source into SPIR-V using shaderc when available.
bool CompileGlslToSpv(const QString &glsl, VkShaderStageFlagBits stage,
QByteArray *out_spv);
// Rewrites an Oak GLSL shader into Vulkan-compatible GLSL.
QString ConvertGlslToVulkan(const QString &glsl, VkShaderStageFlagBits stage);
// Ensures a shader declares a Vulkan-compatible GLSL version.
QString EnsureGlslVersion450(const QString &glsl) const;
// Extracts uniforms and sampler names from GLSL declarations.
void ExtractUniforms(const QString &glsl, QVector<UniformInfo> *out_uniforms,
QVector<QString> *out_samplers) const;
// Computes std140 offsets and total UBO size for extracted uniforms.
void ComputeUniformLayout(QVector<UniformInfo> *uniforms) const;
// Builds the generated uniform block used by rewritten shaders.
QString BuildUboBlock(const QVector<UniformInfo> &uniforms) const;
// Rewrites standalone uniforms and samplers into explicit UBO/sampler
// bindings accepted by Vulkan GLSL.
QString RewriteShaderWithUbo(const QString &glsl,
const QVector<UniformInfo> &all_uniforms,
const QHash<QString, int> &sampler_bindings) const;
// Returns std140 storage size for a supported GLSL type.
VkDeviceSize GetStd140Size(const QString &type) const;
// Returns std140 alignment for a supported GLSL type.
VkDeviceSize GetStd140Alignment(const QString &type) const;
// Creates or retrieves the graphics pipeline for a shader/render format pair.
bool CreatePipelineForShader(VulkanShader *shader,
const VideoParams &dest_params,
VkFormat render_pass_format);
// Caches simple single-color-attachment render passes by format/clear mode.
VkRenderPass GetOrCreateRenderPass(VkFormat format, bool clear);
struct TextureBinding {
@@ -146,6 +204,7 @@ private:
Texture::Interpolation interp;
};
// Executes one fullscreen pass with the provided texture bindings and UBO.
void BlitPass(VulkanShader *shader, VulkanTexture *dest_tex,
const QVector<TextureBinding> &bindings,
const QByteArray &ubo_data,