From 88e2cca3dbd1f509982778804ba2463058bb729a Mon Sep 17 00:00:00 2001
From: Lynne <dev@lynne.ee>
Date: Sat, 18 Mar 2023 19:15:41 +0100
Subject: [PATCH] tools/cl2c: change to tools/source2c and allow non-OpenCL
 source files

---
 libavfilter/Makefile                | 11 +++++++++--
 libavfilter/opencl_source.h         | 28 ++++++++++++++--------------
 libavfilter/vf_avgblur_opencl.c     |  2 +-
 libavfilter/vf_colorkey_opencl.c    |  2 +-
 libavfilter/vf_convolution_opencl.c |  2 +-
 libavfilter/vf_deshake_opencl.c     |  2 +-
 libavfilter/vf_neighbor_opencl.c    |  2 +-
 libavfilter/vf_nlmeans_opencl.c     |  2 +-
 libavfilter/vf_overlay_opencl.c     |  2 +-
 libavfilter/vf_pad_opencl.c         |  2 +-
 libavfilter/vf_remap_opencl.c       |  2 +-
 libavfilter/vf_tonemap_opencl.c     |  4 ++--
 libavfilter/vf_transpose_opencl.c   |  2 +-
 libavfilter/vf_unsharp_opencl.c     |  2 +-
 libavfilter/vf_xfade_opencl.c       |  2 +-
 libavfilter/vulkan/.gitignore       |  1 +
 tools/{cl2c => source2c}            | 10 ++++------
 17 files changed, 42 insertions(+), 36 deletions(-)
 create mode 100644 libavfilter/vulkan/.gitignore
 rename tools/{cl2c => source2c} (78%)

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index aa1d9c0497..01c083e77d 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -651,10 +651,17 @@ TESTPROGS = drawutils filtfmts formats integral
 TOOLS-$(CONFIG_LIBZMQ) += zmqsend
 
 clean::
-	$(RM) $(CLEANSUFFIXES:%=libavfilter/dnn/%) $(CLEANSUFFIXES:%=libavfilter/opencl/%)
+	$(RM) $(CLEANSUFFIXES:%=libavfilter/dnn/%) $(CLEANSUFFIXES:%=libavfilter/opencl/%) \
+              $(CLEANSUFFIXES:%=libavfilter/vulkan/%)
 
 OPENCL = $(subst $(SRC_PATH)/,,$(wildcard $(SRC_PATH)/libavfilter/opencl/*.cl))
 .SECONDARY: $(OPENCL:.cl=.c)
 libavfilter/opencl/%.c: TAG = OPENCL
 libavfilter/opencl/%.c: $(SRC_PATH)/libavfilter/opencl/%.cl
-	$(M)$(SRC_PATH)/tools/cl2c $< $@
+	$(M)$(SRC_PATH)/tools/source2c $< $@
+
+VULKAN = $(subst $(SRC_PATH)/,,$(wildcard $(SRC_PATH)/libavfilter/vulkan/*.comp))
+.SECONDARY: $(VULKAN:.comp=.c)
+libavfilter/vulkan/%.c: TAG = OPENCL
+libavfilter/vulkan/%.c: $(SRC_PATH)/libavfilter/vulkan/%.comp
+	$(M)$(SRC_PATH)/tools/source2c $< $@
diff --git a/libavfilter/opencl_source.h b/libavfilter/opencl_source.h
index 9eac2dc516..b6930fb686 100644
--- a/libavfilter/opencl_source.h
+++ b/libavfilter/opencl_source.h
@@ -19,19 +19,19 @@
 #ifndef AVFILTER_OPENCL_SOURCE_H
 #define AVFILTER_OPENCL_SOURCE_H
 
-extern const char *ff_opencl_source_avgblur;
-extern const char *ff_opencl_source_colorkey;
-extern const char *ff_opencl_source_colorspace_common;
-extern const char *ff_opencl_source_convolution;
-extern const char *ff_opencl_source_deshake;
-extern const char *ff_opencl_source_neighbor;
-extern const char *ff_opencl_source_nlmeans;
-extern const char *ff_opencl_source_overlay;
-extern const char *ff_opencl_source_pad;
-extern const char *ff_opencl_source_remap;
-extern const char *ff_opencl_source_tonemap;
-extern const char *ff_opencl_source_transpose;
-extern const char *ff_opencl_source_unsharp;
-extern const char *ff_opencl_source_xfade;
+extern const char *ff_source_avgblur_cl;
+extern const char *ff_source_colorkey_cl;
+extern const char *ff_source_colorspace_common_cl;
+extern const char *ff_source_convolution_cl;
+extern const char *ff_source_deshake_cl;
+extern const char *ff_source_neighbor_cl;
+extern const char *ff_source_nlmeans_cl;
+extern const char *ff_source_overlay_cl;
+extern const char *ff_source_pad_cl;
+extern const char *ff_source_remap_cl;
+extern const char *ff_source_tonemap_cl;
+extern const char *ff_source_transpose_cl;
+extern const char *ff_source_unsharp_cl;
+extern const char *ff_source_xfade_cl;
 
 #endif /* AVFILTER_OPENCL_SOURCE_H */
diff --git a/libavfilter/vf_avgblur_opencl.c b/libavfilter/vf_avgblur_opencl.c
index 68f3a63249..c00d2f6363 100644
--- a/libavfilter/vf_avgblur_opencl.c
+++ b/libavfilter/vf_avgblur_opencl.c
@@ -59,7 +59,7 @@ static int avgblur_opencl_init(AVFilterContext *avctx)
     cl_int cle;
     int err;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_avgblur, 1);
+    err = ff_opencl_filter_load_program(avctx, &ff_source_avgblur_cl, 1);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_colorkey_opencl.c b/libavfilter/vf_colorkey_opencl.c
index 2b019b290c..94361df88f 100644
--- a/libavfilter/vf_colorkey_opencl.c
+++ b/libavfilter/vf_colorkey_opencl.c
@@ -52,7 +52,7 @@ static int colorkey_opencl_init(AVFilterContext *avctx)
     cl_int cle;
     int err;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_colorkey, 1);
+    err = ff_opencl_filter_load_program(avctx, &ff_source_colorkey_cl, 1);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_convolution_opencl.c b/libavfilter/vf_convolution_opencl.c
index bf721a7416..0eff9f40d3 100644
--- a/libavfilter/vf_convolution_opencl.c
+++ b/libavfilter/vf_convolution_opencl.c
@@ -62,7 +62,7 @@ static int convolution_opencl_init(AVFilterContext *avctx)
     cl_int cle;
     int err;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_convolution, 1);
+    err = ff_opencl_filter_load_program(avctx, &ff_source_convolution_cl, 1);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_deshake_opencl.c b/libavfilter/vf_deshake_opencl.c
index e670a4cc23..8db59767bd 100644
--- a/libavfilter/vf_deshake_opencl.c
+++ b/libavfilter/vf_deshake_opencl.c
@@ -1251,7 +1251,7 @@ static int deshake_opencl_init(AVFilterContext *avctx)
     }
     ctx->sw_format = hw_frames_ctx->sw_format;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_deshake, 1);
+    err = ff_opencl_filter_load_program(avctx, &ff_source_deshake_cl, 1);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_neighbor_opencl.c b/libavfilter/vf_neighbor_opencl.c
index d2d93cd240..b2939f841a 100644
--- a/libavfilter/vf_neighbor_opencl.c
+++ b/libavfilter/vf_neighbor_opencl.c
@@ -55,7 +55,7 @@ static int neighbor_opencl_init(AVFilterContext *avctx)
     cl_int cle;
     int err;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_neighbor, 1);
+    err = ff_opencl_filter_load_program(avctx, &ff_source_neighbor_cl, 1);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_nlmeans_opencl.c b/libavfilter/vf_nlmeans_opencl.c
index ca3ec45d7a..5149be02ca 100644
--- a/libavfilter/vf_nlmeans_opencl.c
+++ b/libavfilter/vf_nlmeans_opencl.c
@@ -98,7 +98,7 @@ static int nlmeans_opencl_init(AVFilterContext *avctx, int width, int height)
     if (!ctx->patch_size_uv)
         ctx->patch_size_uv = ctx->patch_size;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_nlmeans, 1);
+    err = ff_opencl_filter_load_program(avctx, &ff_source_nlmeans_cl, 1);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_overlay_opencl.c b/libavfilter/vf_overlay_opencl.c
index 38a3fc8795..9beb09f05a 100644
--- a/libavfilter/vf_overlay_opencl.c
+++ b/libavfilter/vf_overlay_opencl.c
@@ -51,7 +51,7 @@ static int overlay_opencl_load(AVFilterContext *avctx,
 {
     OverlayOpenCLContext *ctx = avctx->priv;
     cl_int cle;
-    const char *source = ff_opencl_source_overlay;
+    const char *source = ff_source_overlay_cl;
     const char *kernel;
     const AVPixFmtDescriptor *main_desc, *overlay_desc;
     int err, i, main_planes, overlay_planes;
diff --git a/libavfilter/vf_pad_opencl.c b/libavfilter/vf_pad_opencl.c
index d6b71765ee..b4b10397a4 100644
--- a/libavfilter/vf_pad_opencl.c
+++ b/libavfilter/vf_pad_opencl.c
@@ -93,7 +93,7 @@ static int pad_opencl_init(AVFilterContext *avctx, AVFrame *input_frame)
     ctx->hsub = desc->log2_chroma_w;
     ctx->vsub = desc->log2_chroma_h;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_pad, 1);
+    err = ff_opencl_filter_load_program(avctx, &ff_source_pad_cl, 1);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_remap_opencl.c b/libavfilter/vf_remap_opencl.c
index eeb1eb5d69..89d47426c0 100644
--- a/libavfilter/vf_remap_opencl.c
+++ b/libavfilter/vf_remap_opencl.c
@@ -73,7 +73,7 @@ static int remap_opencl_load(AVFilterContext *avctx,
 {
     RemapOpenCLContext *ctx = avctx->priv;
     cl_int cle;
-    const char *source = ff_opencl_source_remap;
+    const char *source = ff_source_remap_cl;
     const char *kernel = kernels[ctx->interp];
     const AVPixFmtDescriptor *main_desc;
     int err, main_planes;
diff --git a/libavfilter/vf_tonemap_opencl.c b/libavfilter/vf_tonemap_opencl.c
index 883eb04342..84bf394e75 100644
--- a/libavfilter/vf_tonemap_opencl.c
+++ b/libavfilter/vf_tonemap_opencl.c
@@ -240,8 +240,8 @@ static int tonemap_opencl_init(AVFilterContext *avctx)
 
     av_log(avctx, AV_LOG_DEBUG, "Generated OpenCL header:\n%s\n", header.str);
     opencl_sources[0] = header.str;
-    opencl_sources[1] = ff_opencl_source_tonemap;
-    opencl_sources[2] = ff_opencl_source_colorspace_common;
+    opencl_sources[1] = ff_source_tonemap_cl;
+    opencl_sources[2] = ff_source_colorspace_common_cl;
     err = ff_opencl_filter_load_program(avctx, opencl_sources, OPENCL_SOURCE_NB);
 
     av_bprint_finalize(&header, NULL);
diff --git a/libavfilter/vf_transpose_opencl.c b/libavfilter/vf_transpose_opencl.c
index 56d34d193b..b212804953 100644
--- a/libavfilter/vf_transpose_opencl.c
+++ b/libavfilter/vf_transpose_opencl.c
@@ -44,7 +44,7 @@ static int transpose_opencl_init(AVFilterContext *avctx)
     cl_int cle;
     int err;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_transpose, 1);
+    err = ff_opencl_filter_load_program(avctx, &ff_source_transpose_cl, 1);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_unsharp_opencl.c b/libavfilter/vf_unsharp_opencl.c
index 2c3ac14050..09398464ca 100644
--- a/libavfilter/vf_unsharp_opencl.c
+++ b/libavfilter/vf_unsharp_opencl.c
@@ -69,7 +69,7 @@ static int unsharp_opencl_init(AVFilterContext *avctx)
     cl_int cle;
     int err;
 
-    err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_unsharp, 1);
+    err = ff_opencl_filter_load_program(avctx, &ff_source_unsharp_cl, 1);
     if (err < 0)
         goto fail;
 
diff --git a/libavfilter/vf_xfade_opencl.c b/libavfilter/vf_xfade_opencl.c
index 415cf7ac35..fb567aa7fd 100644
--- a/libavfilter/vf_xfade_opencl.c
+++ b/libavfilter/vf_xfade_opencl.c
@@ -93,7 +93,7 @@ static int xfade_opencl_load(AVFilterContext *avctx,
     if (ctx->transition == CUSTOM) {
         err = ff_opencl_filter_load_program_from_file(avctx, ctx->source_file);
     } else {
-        err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_xfade, 1);
+        err = ff_opencl_filter_load_program(avctx, &ff_source_xfade_cl, 1);
     }
     if (err < 0)
         return err;
diff --git a/libavfilter/vulkan/.gitignore b/libavfilter/vulkan/.gitignore
new file mode 100644
index 0000000000..064a8d8ef5
--- /dev/null
+++ b/libavfilter/vulkan/.gitignore
@@ -0,0 +1 @@
+*.c
diff --git a/tools/cl2c b/tools/source2c
similarity index 78%
rename from tools/cl2c
rename to tools/source2c
index e3f92bab1c..6e5f123144 100755
--- a/tools/cl2c
+++ b/tools/source2c
@@ -1,7 +1,6 @@
 #!/bin/sh
-# Convert an OpenCL source file into a C source file containing the
-# OpenCL source as a C string.  Also adds a #line directive so that
-# compiler messages are useful.
+# Convert a source file into a C source file containing the
+# source code as a C string.
 
 # This file is part of FFmpeg.
 #
@@ -22,12 +21,11 @@
 input="$1"
 output="$2"
 
-name=$(basename "$input" | sed 's/.cl$//')
+name=$(basename "$input" | sed 's/\./_/')
 
 cat >$output <<EOF
 // Generated from $input
-const char *ff_opencl_source_$name =
-"#line 1 \"$input\"\n"
+const char *ff_source_$name =
 EOF
 
 # Convert \ to \\ and " to \", then add " to the start and end of the line.