vf_scdet_vulkan: port to compile-time SPIR-V generation

This commit is contained in:
Lynne
2026-04-21 10:32:38 +02:00
parent 9acd820732
commit 4d6cd9f983
4 changed files with 87 additions and 69 deletions
+23 -68
View File
@@ -19,13 +19,15 @@
*/
#include "libavutil/avassert.h"
#include "libavutil/vulkan_spirv.h"
#include "libavutil/opt.h"
#include "libavutil/timestamp.h"
#include "vulkan_filter.h"
#include "filters.h"
extern const unsigned char ff_scdet_comp_spv_data[];
extern const unsigned int ff_scdet_comp_spv_len;
typedef struct SceneDetectVulkanContext {
FFVulkanContext vkctx;
@@ -52,26 +54,14 @@ typedef struct SceneDetectBuf {
static av_cold int init_filter(AVFilterContext *ctx)
{
int err;
uint8_t *spv_data;
size_t spv_len;
void *spv_opaque = NULL;
SceneDetectVulkanContext *s = ctx->priv;
FFVulkanContext *vkctx = &s->vkctx;
FFVulkanShader *shd;
FFVkSPIRVCompiler *spv;
FFVulkanDescriptorSetBinding *desc;
const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(s->vkctx.input_format);
const int lumaonly = !(pixdesc->flags & AV_PIX_FMT_FLAG_RGB) &&
(pixdesc->flags & AV_PIX_FMT_FLAG_PLANAR);
s->nb_planes = lumaonly ? 1 : av_pix_fmt_count_planes(s->vkctx.input_format);
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");
@@ -80,76 +70,41 @@ static av_cold int init_filter(AVFilterContext *ctx)
}
RET(ff_vk_exec_pool_init(vkctx, s->qf, &s->e, s->qf->num*4, 0, 0, 0, NULL));
RET(ff_vk_shader_init(vkctx, &s->shd, "scdet",
VK_SHADER_STAGE_COMPUTE_BIT,
(const char *[]) { "GL_KHR_shader_subgroup_arithmetic" }, 1,
32, 32, 1,
0));
shd = &s->shd;
desc = (FFVulkanDescriptorSetBinding []) {
{
.name = "prev_img",
SPEC_LIST_CREATE(sl, 2, 2*sizeof(uint32_t))
SPEC_LIST_ADD(sl, 0, 32, s->nb_planes);
SPEC_LIST_ADD(sl, 1, 32, SLICES);
ff_vk_shader_load(&s->shd, VK_SHADER_STAGE_COMPUTE_BIT, sl,
(int []) { 32, 32, 1 }, 0);
const FFVulkanDescriptorSetBinding desc[] = {
{ /* prev_img */
.type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.stages = VK_SHADER_STAGE_COMPUTE_BIT,
.elems = av_pix_fmt_count_planes(s->vkctx.input_format),
},
{ /* cur_img */
.type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.mem_layout = ff_vk_shader_rep_fmt(s->vkctx.input_format, FF_VK_REP_UINT),
.mem_quali = "readonly",
.dimensions = 2,
.elems = av_pix_fmt_count_planes(s->vkctx.input_format),
.stages = VK_SHADER_STAGE_COMPUTE_BIT,
}, {
.name = "cur_img",
.type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.mem_layout = ff_vk_shader_rep_fmt(s->vkctx.input_format, FF_VK_REP_UINT),
.mem_quali = "readonly",
.dimensions = 2,
.elems = av_pix_fmt_count_planes(s->vkctx.input_format),
.stages = VK_SHADER_STAGE_COMPUTE_BIT,
}, {
.name = "sad_buffer",
},
{ /* sad_buffer */
.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.stages = VK_SHADER_STAGE_COMPUTE_BIT,
.buf_content = "uint frame_sad[];",
}
};
ff_vk_shader_add_descriptor_set(vkctx, &s->shd, desc, 3, 0, 0);
RET(ff_vk_shader_add_descriptor_set(vkctx, &s->shd, desc, 3, 0, 0));
GLSLC(0, shared uint wg_sum; );
GLSLC(0, void main() );
GLSLC(0, { );
GLSLF(1, const uint slice = gl_WorkGroupID.x %% %u; ,SLICES);
GLSLC(1, const ivec2 pos = ivec2(gl_GlobalInvocationID.xy); );
GLSLC(1, wg_sum = 0; );
GLSLC(1, barrier(); );
for (int i = 0; i < s->nb_planes; i++) {
GLSLF(1, if (IS_WITHIN(pos, imageSize(cur_img[%d]))) { ,i);
GLSLF(2, uvec4 prev = imageLoad(prev_img[%d], pos); ,i);
GLSLF(2, uvec4 cur = imageLoad(cur_img[%d], pos); ,i);
GLSLC(2, uvec4 sad = abs(ivec4(cur) - ivec4(prev)); );
GLSLC(2, uint sum = subgroupAdd(sad.x + sad.y + sad.z); );
GLSLC(2, if (subgroupElect()) );
GLSLC(3, atomicAdd(wg_sum, sum); );
GLSLC(1, } );
}
GLSLC(1, barrier(); );
GLSLC(1, if (gl_LocalInvocationIndex == 0) );
GLSLC(2, atomicAdd(frame_sad[slice], wg_sum); );
GLSLC(0, } );
RET(spv->compile_shader(vkctx, spv, &s->shd, &spv_data, &spv_len, "main",
&spv_opaque));
RET(ff_vk_shader_link(vkctx, &s->shd, spv_data, spv_len, "main"));
RET(ff_vk_shader_link(vkctx, &s->shd,
ff_scdet_comp_spv_data,
ff_scdet_comp_spv_len, "main"));
RET(ff_vk_shader_register_exec(vkctx, &s->e, &s->shd));
s->initialized = 1;
fail:
if (spv_opaque)
spv->free_shader(spv, &spv_opaque);
if (spv)
spv->uninit(&spv);
return err;
}
+1
View File
@@ -7,6 +7,7 @@ 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
OBJS-$(CONFIG_SCDET_VULKAN_FILTER) += vulkan/scdet.comp.spv.o
OBJS-$(CONFIG_FLIP_VULKAN_FILTER) += vulkan/flip.comp.spv.o
OBJS-$(CONFIG_TRANSPOSE_VULKAN_FILTER) += vulkan/transpose.comp.spv.o
OBJS-$(CONFIG_V360_VULKAN_FILTER) += vulkan/v360.comp.spv.o
+62
View File
@@ -0,0 +1,62 @@
/*
* Copyright 2025 (c) Niklas Haas
*
* 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
#extension GL_KHR_shader_subgroup_arithmetic : require
#extension GL_EXT_null_initializer : require
layout (constant_id = 0) const uint planes = 0;
layout (constant_id = 1) const uint slices = 0;
layout (local_size_x_id = 253, local_size_y_id = 254, local_size_z_id = 255) in;
layout (set = 0, binding = 0) uniform readonly uimage2D prev_img[];
layout (set = 0, binding = 1) uniform readonly uimage2D cur_img[];
layout (set = 0, binding = 2, scalar) buffer sad_buffer {
uint frame_sad[];
};
shared uint wg_sum = { };
void main()
{
const uint slice = gl_WorkGroupID.x % slices;
const ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
for (uint i = 0; i < planes; i++) {
const ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
if (all(lessThan(pos, imageSize(cur_img[i])))) {
uvec4 prev = imageLoad(prev_img[i], pos);
uvec4 cur = imageLoad(cur_img[i], pos);
uvec4 sad = abs(ivec4(cur) - ivec4(prev));
uint sum = subgroupAdd(sad.x + sad.y + sad.z);
if (subgroupElect())
atomicAdd(wg_sum, sum);
}
}
barrier();
if (gl_LocalInvocationIndex == 0)
atomicAdd(frame_sad[slice], wg_sum);
}