mirror of
https://git.ffmpeg.org/ffmpeg.git
synced 2026-06-11 08:13:06 +00:00
chromaber_vulkan: switch to compile-time SPIR-V generation
This commit is contained in:
@@ -4155,7 +4155,7 @@ bs2b_filter_deps="libbs2b"
|
||||
bwdif_cuda_filter_deps="ffnvcodec"
|
||||
bwdif_cuda_filter_deps_any="cuda_nvcc cuda_llvm"
|
||||
bwdif_vulkan_filter_deps="vulkan spirv_compiler"
|
||||
chromaber_vulkan_filter_deps="vulkan spirv_library"
|
||||
chromaber_vulkan_filter_deps="vulkan spirv_compiler"
|
||||
color_vulkan_filter_deps="vulkan spirv_compiler"
|
||||
colorkey_opencl_filter_deps="opencl"
|
||||
colormatrix_filter_deps="gpl"
|
||||
|
||||
@@ -20,12 +20,14 @@
|
||||
|
||||
#include "libavutil/random_seed.h"
|
||||
#include "libavutil/opt.h"
|
||||
#include "libavutil/vulkan_spirv.h"
|
||||
#include "vulkan_filter.h"
|
||||
|
||||
#include "filters.h"
|
||||
#include "video.h"
|
||||
|
||||
extern const unsigned char ff_chromaber_comp_spv_data[];
|
||||
extern const unsigned int ff_chromaber_comp_spv_len;
|
||||
|
||||
typedef struct ChromaticAberrationVulkanContext {
|
||||
FFVulkanContext vkctx;
|
||||
|
||||
@@ -38,56 +40,22 @@ typedef struct ChromaticAberrationVulkanContext {
|
||||
/* Push constants / options */
|
||||
struct {
|
||||
float dist[2];
|
||||
uint32_t single_plane;
|
||||
} opts;
|
||||
} ChromaticAberrationVulkanContext;
|
||||
|
||||
static const char distort_chroma_kernel[] = {
|
||||
C(0, void distort_rgb(ivec2 size, ivec2 pos) )
|
||||
C(0, { )
|
||||
C(1, const vec2 p = ((vec2(pos)/vec2(size)) - 0.5f)*2.0f; )
|
||||
C(1, const vec2 o = p * (dist - 1.0f); )
|
||||
C(0, )
|
||||
C(1, vec4 res; )
|
||||
C(1, res.r = texture(input_img[0], ((p - o)/2.0f) + 0.5f).r; )
|
||||
C(1, res.g = texture(input_img[0], ((p )/2.0f) + 0.5f).g; )
|
||||
C(1, res.b = texture(input_img[0], ((p + o)/2.0f) + 0.5f).b; )
|
||||
C(1, res.a = texture(input_img[0], ((p )/2.0f) + 0.5f).a; )
|
||||
C(1, imageStore(output_img[0], pos, res); )
|
||||
C(0, } )
|
||||
C(0, )
|
||||
C(0, void distort_chroma(int idx, ivec2 size, ivec2 pos) )
|
||||
C(0, { )
|
||||
C(1, vec2 p = ((vec2(pos)/vec2(size)) - 0.5f)*2.0f; )
|
||||
C(1, float d = sqrt(p.x*p.x + p.y*p.y); )
|
||||
C(1, p *= d / (d*dist); )
|
||||
C(1, vec4 res = texture(input_img[idx], (p/2.0f) + 0.5f); )
|
||||
C(1, imageStore(output_img[idx], pos, res); )
|
||||
C(0, } )
|
||||
};
|
||||
|
||||
static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
|
||||
{
|
||||
int err;
|
||||
uint8_t *spv_data;
|
||||
size_t spv_len;
|
||||
void *spv_opaque = NULL;
|
||||
ChromaticAberrationVulkanContext *s = ctx->priv;
|
||||
FFVulkanContext *vkctx = &s->vkctx;
|
||||
const int planes = av_pix_fmt_count_planes(s->vkctx.output_format);
|
||||
FFVulkanShader *shd = &s->shd;
|
||||
FFVkSPIRVCompiler *spv;
|
||||
FFVulkanDescriptorSetBinding *desc;
|
||||
|
||||
/* Normalize options */
|
||||
s->opts.dist[0] = (s->opts.dist[0] / 100.0f) + 1.0f;
|
||||
s->opts.dist[1] = (s->opts.dist[1] / 100.0f) + 1.0f;
|
||||
|
||||
spv = ff_vk_spirv_init();
|
||||
if (!spv) {
|
||||
av_log(ctx, AV_LOG_ERROR, "Unable to initialize SPIR-V compiler!\n");
|
||||
return AVERROR_EXTERNAL;
|
||||
}
|
||||
|
||||
s->qf = ff_vk_qf_find(vkctx, VK_QUEUE_COMPUTE_BIT, 0);
|
||||
if (!s->qf) {
|
||||
av_log(ctx, AV_LOG_ERROR, "Device has no compute queues\n");
|
||||
@@ -97,77 +65,37 @@ static av_cold int init_filter(AVFilterContext *ctx, AVFrame *in)
|
||||
|
||||
RET(ff_vk_exec_pool_init(vkctx, s->qf, &s->e, s->qf->num*4, 0, 0, 0, NULL));
|
||||
RET(ff_vk_init_sampler(vkctx, &s->sampler, 0, VK_FILTER_LINEAR));
|
||||
RET(ff_vk_shader_init(vkctx, &s->shd, "chromatic_abberation",
|
||||
VK_SHADER_STAGE_COMPUTE_BIT,
|
||||
NULL, 0,
|
||||
32, 32, 1,
|
||||
0));
|
||||
|
||||
GLSLC(0, layout(push_constant, std430) uniform pushConstants { );
|
||||
GLSLC(1, vec2 dist; );
|
||||
GLSLC(0, }; );
|
||||
GLSLC(0, );
|
||||
ff_vk_shader_load(&s->shd, VK_SHADER_STAGE_COMPUTE_BIT, NULL,
|
||||
(uint32_t []) { 32, 32, 1 }, 0);
|
||||
|
||||
ff_vk_shader_add_push_const(&s->shd, 0, sizeof(s->opts),
|
||||
VK_SHADER_STAGE_COMPUTE_BIT);
|
||||
|
||||
desc = (FFVulkanDescriptorSetBinding []) {
|
||||
{
|
||||
.name = "input_img",
|
||||
const FFVulkanDescriptorSetBinding desc[] = {
|
||||
{ /* input_img */
|
||||
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
||||
.dimensions = 2,
|
||||
.elems = planes,
|
||||
.stages = VK_SHADER_STAGE_COMPUTE_BIT,
|
||||
.samplers = DUP_SAMPLER(s->sampler),
|
||||
.elems = planes,
|
||||
},
|
||||
{
|
||||
.name = "output_img",
|
||||
{ /* output_img */
|
||||
.type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
|
||||
.mem_layout = ff_vk_shader_rep_fmt(s->vkctx.output_format, FF_VK_REP_FLOAT),
|
||||
.mem_quali = "writeonly",
|
||||
.dimensions = 2,
|
||||
.elems = planes,
|
||||
.stages = VK_SHADER_STAGE_COMPUTE_BIT,
|
||||
.elems = planes,
|
||||
},
|
||||
};
|
||||
ff_vk_shader_add_descriptor_set(vkctx, &s->shd, desc, 2, 0, 0);
|
||||
|
||||
RET(ff_vk_shader_add_descriptor_set(vkctx, &s->shd, desc, 2, 0, 0));
|
||||
|
||||
GLSLD( distort_chroma_kernel );
|
||||
GLSLC(0, void main() );
|
||||
GLSLC(0, { );
|
||||
GLSLC(1, ivec2 pos = ivec2(gl_GlobalInvocationID.xy); );
|
||||
if (planes == 1) {
|
||||
GLSLC(1, distort_rgb(imageSize(output_img[0]), pos); );
|
||||
} else {
|
||||
GLSLC(1, ivec2 size = imageSize(output_img[0]); );
|
||||
GLSLC(1, vec2 npos = vec2(pos)/vec2(size); );
|
||||
GLSLC(1, vec4 res = texture(input_img[0], npos); );
|
||||
GLSLC(1, imageStore(output_img[0], pos, res); );
|
||||
for (int i = 1; i < planes; i++) {
|
||||
GLSLC(0, );
|
||||
GLSLF(1, size = imageSize(output_img[%i]); ,i);
|
||||
GLSLC(1, if (!IS_WITHIN(pos, size)) );
|
||||
GLSLC(2, return; );
|
||||
GLSLF(1, distort_chroma(%i, size, pos); ,i);
|
||||
}
|
||||
}
|
||||
GLSLC(0, } );
|
||||
|
||||
RET(spv->compile_shader(vkctx, spv, shd, &spv_data, &spv_len, "main",
|
||||
&spv_opaque));
|
||||
RET(ff_vk_shader_link(vkctx, shd, spv_data, spv_len, "main"));
|
||||
RET(ff_vk_shader_link(vkctx, shd,
|
||||
ff_chromaber_comp_spv_data,
|
||||
ff_chromaber_comp_spv_len, "main"));
|
||||
|
||||
RET(ff_vk_shader_register_exec(vkctx, &s->e, &s->shd));
|
||||
|
||||
s->opts.single_plane = planes == 1;
|
||||
s->initialized = 1;
|
||||
|
||||
fail:
|
||||
if (spv_opaque)
|
||||
spv->free_shader(spv, &spv_opaque);
|
||||
if (spv)
|
||||
spv->uninit(&spv);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ clean::
|
||||
|
||||
OBJS-$(CONFIG_AVGBLUR_VULKAN_FILTER) += vulkan/avgblur.comp.spv.o
|
||||
OBJS-$(CONFIG_BWDIF_VULKAN_FILTER) += vulkan/bwdif.comp.spv.o
|
||||
OBJS-$(CONFIG_CHROMABER_VULKAN_FILTER) += vulkan/chromaber.comp.spv.o
|
||||
OBJS-$(CONFIG_COLOR_VULKAN_FILTER) += vulkan/color.comp.spv.o
|
||||
OBJS-$(CONFIG_GBLUR_VULKAN_FILTER) += vulkan/gblur.comp.spv.o
|
||||
OBJS-$(CONFIG_SCALE_VULKAN_FILTER) += vulkan/debayer.comp.spv.o
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Lynne <dev@lynne.ee>
|
||||
*
|
||||
* This file is part of FFmpeg.
|
||||
*
|
||||
* FFmpeg is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* FFmpeg 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with FFmpeg; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#pragma shader_stage(compute)
|
||||
|
||||
#extension GL_EXT_shader_image_load_formatted : require
|
||||
#extension GL_EXT_scalar_block_layout : require
|
||||
#extension GL_EXT_nonuniform_qualifier : require
|
||||
|
||||
layout (local_size_x_id = 253, local_size_y_id = 254, local_size_z_id = 255) in;
|
||||
|
||||
layout (set = 0, binding = 0) uniform sampler2D input_img[];
|
||||
layout (set = 0, binding = 1) uniform writeonly image2D output_img[];
|
||||
|
||||
layout (push_constant, scalar) uniform pushConstants {
|
||||
vec2 dist;
|
||||
bool single_plane;
|
||||
};
|
||||
|
||||
void distort_rgba(ivec2 pos, ivec2 size)
|
||||
{
|
||||
const vec2 p = ((vec2(pos)/vec2(size)) - 0.5f)*2.0f;
|
||||
const vec2 o = p * (dist - 1.0f);
|
||||
|
||||
vec4 res;
|
||||
res.r = texture(input_img[0], ((p - o)/2.0f) + 0.5f).r;
|
||||
res.g = texture(input_img[0], ((p )/2.0f) + 0.5f).g;
|
||||
res.b = texture(input_img[0], ((p + o)/2.0f) + 0.5f).b;
|
||||
res.a = texture(input_img[0], ((p )/2.0f) + 0.5f).a;
|
||||
imageStore(output_img[0], pos, res);
|
||||
}
|
||||
|
||||
void distort_plane(ivec2 pos, ivec2 size)
|
||||
{
|
||||
vec2 p = ((vec2(pos)/vec2(size)) - 0.5f)*2.0f;
|
||||
float d = sqrt(p.x*p.x + p.y*p.y);
|
||||
p *= d / (d*dist);
|
||||
vec4 res = texture(input_img[nonuniformEXT(gl_WorkGroupID.z)], (p/2.0f) + 0.5f);
|
||||
imageStore(output_img[nonuniformEXT(gl_WorkGroupID.z)], pos, res);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
|
||||
ivec2 size = imageSize(output_img[nonuniformEXT(gl_WorkGroupID.z)]);
|
||||
if (any(greaterThanEqual(pos, size)))
|
||||
return;
|
||||
|
||||
if (single_plane) {
|
||||
distort_rgba(pos, pos);
|
||||
} else {
|
||||
vec2 npos = vec2(pos)/vec2(size);
|
||||
vec4 res = texture(input_img[0], npos);
|
||||
if (gl_WorkGroupID.z == 0)
|
||||
imageStore(output_img[0], pos, res);
|
||||
else
|
||||
distort_plane(pos, size);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user