History

lavu/vulkan: add support for using libshaderc as a GLSL compiler

It's got a much better API that's actually maintained, it eliminates race conditions, it comes with a pkg-config file by default, and unfortunately isn't currently packaged by Debian or other large distributions.

Lynne committed 4 years ago

Commit da72aca1 parent 1d060843 files · +140 −7Browse files

Unified · Split

3 files
  1. configure +14 −7
  2. libavutil/vulkan.c +4 −0
  3. libavutil/vulkan_shaderc.c +122 −0
configure+14−7
@@ -262,6 +262,7 @@ External library support: · 261 unmodified lines
262 --enable-librsvg enable SVG rasterization via librsvg [no]262 --enable-librsvg enable SVG rasterization via librsvg [no]
263 --enable-librubberband enable rubberband needed for rubberband filter [no]263 --enable-librubberband enable rubberband needed for rubberband filter [no]
264 --enable-librtmp enable RTMP[E] support via librtmp [no]264 --enable-librtmp enable RTMP[E] support via librtmp [no]
265 --enable-libshaderc enable GLSL->SPIRV compilation via libshaderc [no]
265 --enable-libshine enable fixed-point MP3 encoding via libshine [no]266 --enable-libshine enable fixed-point MP3 encoding via libshine [no]
266 --enable-libsmbclient enable Samba protocol via libsmbclient [no]267 --enable-libsmbclient enable Samba protocol via libsmbclient [no]
267 --enable-libsnappy enable Snappy compression, needed for hap encoding [no]268 --enable-libsnappy enable Snappy compression, needed for hap encoding [no]
@@ -1834,6 +1835,7 @@ EXTERNAL_LIBRARY_LIST=" · 1566 unmodified lines
1834 librist1835 librist
1835 librsvg1836 librsvg
1836 librtmp1837 librtmp
1838 libshaderc
1837 libshine1839 libshine
1838 libsmbclient1840 libsmbclient
1839 libsnappy1841 libsnappy
@@ -3565,7 +3567,7 @@ ass_filter_deps="libass" · 1725 unmodified lines
3565atempo_filter_deps="avcodec"3567atempo_filter_deps="avcodec"
3566atempo_filter_select="rdft"3568atempo_filter_select="rdft"
3567avgblur_opencl_filter_deps="opencl"3569avgblur_opencl_filter_deps="opencl"
3568avgblur_vulkan_filter_deps="vulkan libglslang"3570avgblur_vulkan_filter_deps="vulkan spirv_compiler"
3569azmq_filter_deps="libzmq"3571azmq_filter_deps="libzmq"
3570blackframe_filter_deps="gpl"3572blackframe_filter_deps="gpl"
3571bm3d_filter_deps="avcodec"3573bm3d_filter_deps="avcodec"
@@ -3573,7 +3575,7 @@ bm3d_filter_select="dct" · 1 unmodified line
3573boxblur_filter_deps="gpl"3575boxblur_filter_deps="gpl"
3574boxblur_opencl_filter_deps="opencl gpl"3576boxblur_opencl_filter_deps="opencl gpl"
3575bs2b_filter_deps="libbs2b"3577bs2b_filter_deps="libbs2b"
3576chromaber_vulkan_filter_deps="vulkan libglslang"3578chromaber_vulkan_filter_deps="vulkan spirv_compiler"
3577colorkey_opencl_filter_deps="opencl"3579colorkey_opencl_filter_deps="opencl"
3578colormatrix_filter_deps="gpl"3580colormatrix_filter_deps="gpl"
3579convolution_opencl_filter_deps="opencl"3581convolution_opencl_filter_deps="opencl"
@@ -3613,7 +3615,7 @@ frei0r_deps_any="libdl LoadLibrary" · 33 unmodified lines
3613frei0r_filter_deps="frei0r"3615frei0r_filter_deps="frei0r"
3614frei0r_src_filter_deps="frei0r"3616frei0r_src_filter_deps="frei0r"
3615fspp_filter_deps="gpl"3617fspp_filter_deps="gpl"
3616gblur_vulkan_filter_deps="vulkan libglslang"3618gblur_vulkan_filter_deps="vulkan spirv_compiler"
3617histeq_filter_deps="gpl"3619histeq_filter_deps="gpl"
3618hqdn3d_filter_deps="gpl"3620hqdn3d_filter_deps="gpl"
3619interlace_filter_deps="gpl"3621interlace_filter_deps="gpl"
@@ -3638,7 +3640,7 @@ openclsrc_filter_deps="opencl" · 18 unmodified lines
3638overlay_opencl_filter_deps="opencl"3640overlay_opencl_filter_deps="opencl"
3639overlay_qsv_filter_deps="libmfx"3641overlay_qsv_filter_deps="libmfx"
3640overlay_qsv_filter_select="qsvvpp"3642overlay_qsv_filter_select="qsvvpp"
3641overlay_vulkan_filter_deps="vulkan libglslang"3643overlay_vulkan_filter_deps="vulkan spirv_compiler"
3642owdenoise_filter_deps="gpl"3644owdenoise_filter_deps="gpl"
3643pad_opencl_filter_deps="opencl"3645pad_opencl_filter_deps="opencl"
3644pan_filter_deps="swresample"3646pan_filter_deps="swresample"
@@ -3701,7 +3703,7 @@ zmq_filter_deps="libzmq" · 56 unmodified lines
3701zoompan_filter_deps="swscale"3703zoompan_filter_deps="swscale"
3702zscale_filter_deps="libzimg const_nan"3704zscale_filter_deps="libzimg const_nan"
3703scale_vaapi_filter_deps="vaapi"3705scale_vaapi_filter_deps="vaapi"
3704scale_vulkan_filter_deps="vulkan libglslang"3706scale_vulkan_filter_deps="vulkan spirv_compiler"
3705vpp_qsv_filter_deps="libmfx"3707vpp_qsv_filter_deps="libmfx"
3706vpp_qsv_filter_select="qsvvpp"3708vpp_qsv_filter_select="qsvvpp"
3707xfade_opencl_filter_deps="opencl"3709xfade_opencl_filter_deps="opencl"
@@ -6327,6 +6329,10 @@ if ! disabled ffnvcodec; then · 2619 unmodified lines
6327 check_pkg_config ffnvcodec "ffnvcodec >= 8.1.24.11 ffnvcodec < 8.2" "$ffnv_hdr_list" ""6329 check_pkg_config ffnvcodec "ffnvcodec >= 8.1.24.11 ffnvcodec < 8.2" "$ffnv_hdr_list" ""
6328fi6330fi
6329 6331
6332if enabled_all libglslang libshaderc; then
6333 die "ERROR: libshaderc and libglslang are mutually exclusive, if in doubt, disable libglslang"
6334fi
6335
6330check_cpp_condition winrt windows.h "!WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)"6336check_cpp_condition winrt windows.h "!WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)"
6331 6337
6332if ! disabled w32threads && ! enabled pthreads; then6338if ! disabled w32threads && ! enabled pthreads; then
@@ -6443,10 +6449,10 @@ enabled fontconfig && enable libfontconfig · 110 unmodified lines
6443enabled libfontconfig && require_pkg_config libfontconfig fontconfig "fontconfig/fontconfig.h" FcInit6449enabled libfontconfig && require_pkg_config libfontconfig fontconfig "fontconfig/fontconfig.h" FcInit
6444enabled libfreetype && require_pkg_config libfreetype freetype2 "ft2build.h FT_FREETYPE_H" FT_Init_FreeType6450enabled libfreetype && require_pkg_config libfreetype freetype2 "ft2build.h FT_FREETYPE_H" FT_Init_FreeType
6445enabled libfribidi && require_pkg_config libfribidi fribidi fribidi.h fribidi_version_info6451enabled libfribidi && require_pkg_config libfribidi fribidi fribidi.h fribidi_version_info
6446enabled libglslang && { check_lib libglslang glslang/Include/glslang_c_interface.h glslang_initialize_process \6452enabled libglslang && { check_lib spirv_compiler glslang/Include/glslang_c_interface.h glslang_initialize_process \
6447 -lglslang -lMachineIndependent -lOSDependent -lHLSL -lOGLCompiler -lGenericCodeGen \6453 -lglslang -lMachineIndependent -lOSDependent -lHLSL -lOGLCompiler -lGenericCodeGen \
6448 -lSPVRemapper -lSPIRV -lSPIRV-Tools-opt -lSPIRV-Tools -lpthread -lstdc++ -lm ||6454 -lSPVRemapper -lSPIRV -lSPIRV-Tools-opt -lSPIRV-Tools -lpthread -lstdc++ -lm ||
6449 require libglslang glslang/Include/glslang_c_interface.h glslang_initialize_process \6455 require spirv_compiler glslang/Include/glslang_c_interface.h glslang_initialize_process \
6450 -lglslang -lOSDependent -lHLSL -lOGLCompiler \6456 -lglslang -lOSDependent -lHLSL -lOGLCompiler \
6451 -lSPVRemapper -lSPIRV -lSPIRV-Tools-opt -lSPIRV-Tools -lpthread -lstdc++ -lm; }6457 -lSPVRemapper -lSPIRV -lSPIRV-Tools-opt -lSPIRV-Tools -lpthread -lstdc++ -lm; }
6452enabled libgme && { check_pkg_config libgme libgme gme/gme.h gme_new_emu ||6458enabled libgme && { check_pkg_config libgme libgme gme/gme.h gme_new_emu ||
@@ -6504,6 +6510,7 @@ enabled librist && require_pkg_config librist "librist >= 0.2" librist · 51 unmodified lines
6504enabled librsvg && require_pkg_config librsvg librsvg-2.0 librsvg-2.0/librsvg/rsvg.h rsvg_handle_render_cairo6510enabled librsvg && require_pkg_config librsvg librsvg-2.0 librsvg-2.0/librsvg/rsvg.h rsvg_handle_render_cairo
6505enabled librtmp && require_pkg_config librtmp librtmp librtmp/rtmp.h RTMP_Socket6511enabled librtmp && require_pkg_config librtmp librtmp librtmp/rtmp.h RTMP_Socket
6506enabled librubberband && require_pkg_config librubberband "rubberband >= 1.8.1" rubberband/rubberband-c.h rubberband_new -lstdc++ && append librubberband_extralibs "-lstdc++"6512enabled librubberband && require_pkg_config librubberband "rubberband >= 1.8.1" rubberband/rubberband-c.h rubberband_new -lstdc++ && append librubberband_extralibs "-lstdc++"
6513enabled libshaderc && require_pkg_config spirv_compiler "shaderc >= 2019.1" shaderc/shaderc.h shaderc_compiler_initialize
6507enabled libshine && require_pkg_config libshine shine shine/layer3.h shine_encode_buffer6514enabled libshine && require_pkg_config libshine shine shine/layer3.h shine_encode_buffer
6508enabled libsmbclient && { check_pkg_config libsmbclient smbclient libsmbclient.h smbc_init ||6515enabled libsmbclient && { check_pkg_config libsmbclient smbclient libsmbclient.h smbc_init ||
6509 require libsmbclient libsmbclient.h smbc_init -lsmbclient; }6516 require libsmbclient libsmbclient.h smbc_init -lsmbclient; }
libavutil/vulkan.c+4−0
@@ -23,6 +23,8 @@ · 22 unmodified lines
23 23
24#if CONFIG_LIBGLSLANG24#if CONFIG_LIBGLSLANG
25#include "vulkan_glslang.c"25#include "vulkan_glslang.c"
26#elif CONFIG_LIBSHADERC
27#include "vulkan_shaderc.c"
26#endif28#endif
27 29
28/* Generic macro for creating contexts which need to keep their addresses30/* Generic macro for creating contexts which need to keep their addresses
@@ -859,6 +861,8 @@ int ff_vk_compile_shader(FFVulkanContext *s, FFVkSPIRVShader *shd, · 830 unmodified lines
859 if (!s->spirv_compiler) {861 if (!s->spirv_compiler) {
860#if CONFIG_LIBGLSLANG862#if CONFIG_LIBGLSLANG
861 s->spirv_compiler = ff_vk_glslang_init();863 s->spirv_compiler = ff_vk_glslang_init();
864#elif CONFIG_LIBSHADERC
865 s->spirv_compiler = ff_vk_shaderc_init();
862#else866#else
863 return AVERROR(ENOSYS);867 return AVERROR(ENOSYS);
864#endif868#endif
libavutil/vulkan_shaderc.c+122−0
@@ -0,0 +1,122 @@
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <shaderc/shaderc.h>
20
21#include "mem.h"
22
23static int shdc_shader_compile(FFVkSPIRVCompiler *ctx, void *avctx,
24 FFVkSPIRVShader *shd, uint8_t **data,
25 size_t *size, const char *entrypoint,
26 void **opaque)
27{
28 int loglevel, err, warn, ret;
29 const char *status, *message;
30 shaderc_compilation_result_t res;
31 static const char *shdc_result[] = {
32 [shaderc_compilation_status_success] = "success",
33 [shaderc_compilation_status_invalid_stage] = "invalid stage",
34 [shaderc_compilation_status_compilation_error] = "error",
35 [shaderc_compilation_status_internal_error] = "internal error",
36 [shaderc_compilation_status_null_result_object] = "no result",
37 [shaderc_compilation_status_invalid_assembly] = "invalid assembly",
38 };
39 static const shaderc_shader_kind shdc_kind[] = {
40 [VK_SHADER_STAGE_VERTEX_BIT] = shaderc_glsl_vertex_shader,
41 [VK_SHADER_STAGE_FRAGMENT_BIT] = shaderc_glsl_fragment_shader,
42 [VK_SHADER_STAGE_COMPUTE_BIT] = shaderc_glsl_compute_shader,
43 };
44
45 shaderc_compile_options_t opts = shaderc_compile_options_initialize();
46 if (!opts)
47 return AVERROR(ENOMEM);
48
49 shaderc_compile_options_set_target_env(opts, shaderc_target_env_vulkan,
50 shaderc_env_version_vulkan_1_2);
51 shaderc_compile_options_set_target_spirv(opts, shaderc_spirv_version_1_5);
52 shaderc_compile_options_set_optimization_level(opts,
53 shaderc_optimization_level_performance);
54
55 res = shaderc_compile_into_spv((shaderc_compiler_t)ctx->priv,
56 shd->src.str, strlen(shd->src.str),
57 shdc_kind[shd->shader.stage],
58 shd->name, entrypoint, opts);
59 shaderc_compile_options_release(opts);
60
61 ret = shaderc_result_get_compilation_status(res);
62 err = shaderc_result_get_num_errors(res);
63 warn = shaderc_result_get_num_warnings(res);
64 message = shaderc_result_get_error_message(res);
65
66 loglevel = err ? AV_LOG_ERROR : warn ? AV_LOG_WARNING : AV_LOG_VERBOSE;
67
68 ff_vk_print_shader(avctx, shd, loglevel);
69 if (message && (err || warn))
70 av_log(avctx, loglevel, "%s\n", message);
71 status = ret < FF_ARRAY_ELEMS(shdc_result) ? shdc_result[ret] : "unknown";
72 av_log(avctx, loglevel, "shaderc compile status '%s' (%d errors, %d warnings)\n",
73 status, err, warn);
74
75 if (err > 0)
76 return AVERROR(EINVAL);
77
78 *data = (uint8_t *)shaderc_result_get_bytes(res);
79 *size = shaderc_result_get_length(res);
80 *opaque = res;
81
82 return 0;
83}
84
85static void shdc_shader_free(FFVkSPIRVCompiler *ctx, void **opaque)
86{
87 if (!opaque || !*opaque)
88 return;
89
90 shaderc_result_release((shaderc_compilation_result_t)*opaque);
91 *opaque = NULL;
92}
93
94static void shdc_uninit(FFVkSPIRVCompiler **ctx)
95{
96 FFVkSPIRVCompiler *s;
97
98 if (!ctx || !*ctx)
99 return;
100
101 s = *ctx;
102
103 shaderc_compiler_release((shaderc_compiler_t)s->priv);
104 av_freep(ctx);
105}
106
107static FFVkSPIRVCompiler *ff_vk_shaderc_init(void)
108{
109 FFVkSPIRVCompiler *ret = av_mallocz(sizeof(*ret));
110 if (!ret)
111 return NULL;
112
113 ret->compile_shader = shdc_shader_compile;
114 ret->free_shader = shdc_shader_free;
115 ret->uninit = shdc_uninit;
116
117 ret->priv = (void *)shaderc_compiler_initialize();
118 if (!ret->priv)
119 av_freep(&ret);
120
121 return ret;
122}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t