diff options
author | Lynne <dev@lynne.ee> | 2024-10-04 11:22:02 +0200 |
---|---|---|
committer | Lynne <dev@lynne.ee> | 2024-10-06 01:21:44 +0200 |
commit | 0a1f8107bb78e0db5e0e8d3041f377416102bdea (patch) | |
tree | b1bce1b2bb9236c6402b16ff6f8905ac533a3c30 | |
parent | fcd1cdcb7b749abc97d3f2252d843ca1730a6e24 (diff) | |
download | ffmpeg-0a1f8107bb78e0db5e0e8d3041f377416102bdea.tar.gz |
vulkan: move SPIR-V compilation code to libavutil
The code is not currently used by libavutil, its just where our
common Vulkan code is.
Since SPIR-V compilation will be needed by lavc, move it, rather
than having lavc including lavfi.
-rw-r--r-- | libavfilter/Makefile | 2 | ||||
-rw-r--r-- | libavfilter/vf_avgblur_vulkan.c | 2 | ||||
-rw-r--r-- | libavfilter/vf_blend_vulkan.c | 2 | ||||
-rw-r--r-- | libavfilter/vf_bwdif_vulkan.c | 2 | ||||
-rw-r--r-- | libavfilter/vf_chromaber_vulkan.c | 2 | ||||
-rw-r--r-- | libavfilter/vf_flip_vulkan.c | 2 | ||||
-rw-r--r-- | libavfilter/vf_gblur_vulkan.c | 2 | ||||
-rw-r--r-- | libavfilter/vf_nlmeans_vulkan.c | 2 | ||||
-rw-r--r-- | libavfilter/vf_overlay_vulkan.c | 2 | ||||
-rw-r--r-- | libavfilter/vf_scale_vulkan.c | 2 | ||||
-rw-r--r-- | libavfilter/vf_transpose_vulkan.c | 2 | ||||
-rw-r--r-- | libavfilter/vf_xfade_vulkan.c | 2 | ||||
-rw-r--r-- | libavfilter/vsrc_testsrc_vulkan.c | 2 | ||||
-rw-r--r-- | libavfilter/vulkan_glslang.c | 291 | ||||
-rw-r--r-- | libavfilter/vulkan_shaderc.c | 127 | ||||
-rw-r--r-- | libavutil/Makefile | 2 | ||||
-rw-r--r-- | libavutil/vulkan_glslang.c | 308 | ||||
-rw-r--r-- | libavutil/vulkan_shaderc.c | 144 | ||||
-rw-r--r-- | libavutil/vulkan_spirv.h (renamed from libavfilter/vulkan_spirv.h) | 2 |
19 files changed, 469 insertions, 431 deletions
diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 9253e362fc..7d8391f132 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -663,8 +663,6 @@ SKIPHEADERS-$(CONFIG_QSVVPP) += qsvvpp.h stack_internal.h SKIPHEADERS-$(CONFIG_OPENCL) += opencl.h SKIPHEADERS-$(CONFIG_VAAPI) += vaapi_vpp.h stack_internal.h SKIPHEADERS-$(CONFIG_VULKAN) += vulkan_filter.h -SKIPHEADERS-$(CONFIG_LIBSHADERC) += vulkan_spirv.h -SKIPHEADERS-$(CONFIG_LIBGLSLANG) += vulkan_spirv.h TOOLS = graph2dot TESTPROGS = drawutils filtfmts formats integral diff --git a/libavfilter/vf_avgblur_vulkan.c b/libavfilter/vf_avgblur_vulkan.c index 9214ebcc0f..36f00a7a74 100644 --- a/libavfilter/vf_avgblur_vulkan.c +++ b/libavfilter/vf_avgblur_vulkan.c @@ -19,9 +19,9 @@ */ #include "libavutil/random_seed.h" +#include "libavutil/vulkan_spirv.h" #include "libavutil/opt.h" #include "vulkan_filter.h" -#include "vulkan_spirv.h" #include "filters.h" #include "video.h" diff --git a/libavfilter/vf_blend_vulkan.c b/libavfilter/vf_blend_vulkan.c index 406b087503..a2834353e7 100644 --- a/libavfilter/vf_blend_vulkan.c +++ b/libavfilter/vf_blend_vulkan.c @@ -22,9 +22,9 @@ */ #include "libavutil/random_seed.h" +#include "libavutil/vulkan_spirv.h" #include "libavutil/opt.h" #include "vulkan_filter.h" -#include "vulkan_spirv.h" #include "filters.h" #include "framesync.h" diff --git a/libavfilter/vf_bwdif_vulkan.c b/libavfilter/vf_bwdif_vulkan.c index 2ea9512fcb..0d6f1dc39e 100644 --- a/libavfilter/vf_bwdif_vulkan.c +++ b/libavfilter/vf_bwdif_vulkan.c @@ -22,8 +22,8 @@ #include "libavutil/random_seed.h" #include "libavutil/opt.h" +#include "libavutil/vulkan_spirv.h" #include "vulkan_filter.h" -#include "vulkan_spirv.h" #include "yadif.h" #include "filters.h" diff --git a/libavfilter/vf_chromaber_vulkan.c b/libavfilter/vf_chromaber_vulkan.c index 8a62e514fa..e677261259 100644 --- a/libavfilter/vf_chromaber_vulkan.c +++ b/libavfilter/vf_chromaber_vulkan.c @@ -20,8 +20,8 @@ #include "libavutil/random_seed.h" #include "libavutil/opt.h" +#include "libavutil/vulkan_spirv.h" #include "vulkan_filter.h" -#include "vulkan_spirv.h" #include "filters.h" #include "video.h" diff --git a/libavfilter/vf_flip_vulkan.c b/libavfilter/vf_flip_vulkan.c index 3e0a7ebc85..396f85da9b 100644 --- a/libavfilter/vf_flip_vulkan.c +++ b/libavfilter/vf_flip_vulkan.c @@ -21,8 +21,8 @@ #include "libavutil/random_seed.h" #include "libavutil/opt.h" +#include "libavutil/vulkan_spirv.h" #include "vulkan_filter.h" -#include "vulkan_spirv.h" #include "filters.h" #include "video.h" diff --git a/libavfilter/vf_gblur_vulkan.c b/libavfilter/vf_gblur_vulkan.c index 9632a084e4..110f19e555 100644 --- a/libavfilter/vf_gblur_vulkan.c +++ b/libavfilter/vf_gblur_vulkan.c @@ -22,8 +22,8 @@ #include "libavutil/mem.h" #include "libavutil/random_seed.h" #include "libavutil/opt.h" +#include "libavutil/vulkan_spirv.h" #include "vulkan_filter.h" -#include "vulkan_spirv.h" #include "filters.h" #include "video.h" diff --git a/libavfilter/vf_nlmeans_vulkan.c b/libavfilter/vf_nlmeans_vulkan.c index 39460dc194..1c729a2e7a 100644 --- a/libavfilter/vf_nlmeans_vulkan.c +++ b/libavfilter/vf_nlmeans_vulkan.c @@ -20,9 +20,9 @@ #include "libavutil/mem.h" #include "libavutil/random_seed.h" +#include "libavutil/vulkan_spirv.h" #include "libavutil/opt.h" #include "vulkan_filter.h" -#include "vulkan_spirv.h" #include "filters.h" #include "video.h" diff --git a/libavfilter/vf_overlay_vulkan.c b/libavfilter/vf_overlay_vulkan.c index 3af2482772..4c77dcc8fe 100644 --- a/libavfilter/vf_overlay_vulkan.c +++ b/libavfilter/vf_overlay_vulkan.c @@ -20,8 +20,8 @@ #include "libavutil/random_seed.h" #include "libavutil/opt.h" +#include "libavutil/vulkan_spirv.h" #include "vulkan_filter.h" -#include "vulkan_spirv.h" #include "filters.h" #include "framesync.h" diff --git a/libavfilter/vf_scale_vulkan.c b/libavfilter/vf_scale_vulkan.c index 95cc25f8d7..d8f2b5303d 100644 --- a/libavfilter/vf_scale_vulkan.c +++ b/libavfilter/vf_scale_vulkan.c @@ -20,8 +20,8 @@ #include "libavutil/random_seed.h" #include "libavutil/opt.h" +#include "libavutil/vulkan_spirv.h" #include "vulkan_filter.h" -#include "vulkan_spirv.h" #include "scale_eval.h" #include "filters.h" #include "colorspace.h" diff --git a/libavfilter/vf_transpose_vulkan.c b/libavfilter/vf_transpose_vulkan.c index 8ab782cc79..48d60be6cc 100644 --- a/libavfilter/vf_transpose_vulkan.c +++ b/libavfilter/vf_transpose_vulkan.c @@ -21,8 +21,8 @@ #include "libavutil/random_seed.h" #include "libavutil/opt.h" +#include "libavutil/vulkan_spirv.h" #include "vulkan_filter.h" -#include "vulkan_spirv.h" #include "filters.h" #include "transpose.h" diff --git a/libavfilter/vf_xfade_vulkan.c b/libavfilter/vf_xfade_vulkan.c index 0b6f1350fb..8f7b8a0913 100644 --- a/libavfilter/vf_xfade_vulkan.c +++ b/libavfilter/vf_xfade_vulkan.c @@ -19,8 +19,8 @@ #include "libavutil/avassert.h" #include "libavutil/random_seed.h" #include "libavutil/opt.h" +#include "libavutil/vulkan_spirv.h" #include "vulkan_filter.h" -#include "vulkan_spirv.h" #include "filters.h" #include "video.h" diff --git a/libavfilter/vsrc_testsrc_vulkan.c b/libavfilter/vsrc_testsrc_vulkan.c index 0641beed5b..a7d573b529 100644 --- a/libavfilter/vsrc_testsrc_vulkan.c +++ b/libavfilter/vsrc_testsrc_vulkan.c @@ -21,8 +21,8 @@ #include "libavutil/random_seed.h" #include "libavutil/csp.h" #include "libavutil/opt.h" +#include "libavutil/vulkan_spirv.h" #include "vulkan_filter.h" -#include "vulkan_spirv.h" #include "filters.h" #include "colorspace.h" #include "video.h" diff --git a/libavfilter/vulkan_glslang.c b/libavfilter/vulkan_glslang.c index 9e1b8f4d8d..9aa41567a3 100644 --- a/libavfilter/vulkan_glslang.c +++ b/libavfilter/vulkan_glslang.c @@ -16,293 +16,4 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include <pthread.h> - -#include <glslang/build_info.h> -#include <glslang/Include/glslang_c_interface.h> - -#include "vulkan_spirv.h" -#include "libavutil/mem.h" -#include "libavutil/avassert.h" - -static pthread_mutex_t glslc_mutex = PTHREAD_MUTEX_INITIALIZER; -static int glslc_refcount = 0; - -static const glslang_resource_t glslc_resource_limits = { - .max_lights = 32, - .max_clip_planes = 6, - .max_texture_units = 32, - .max_texture_coords = 32, - .max_vertex_attribs = 64, - .max_vertex_uniform_components = 4096, - .max_varying_floats = 64, - .max_vertex_texture_image_units = 32, - .max_combined_texture_image_units = 80, - .max_texture_image_units = 32, - .max_fragment_uniform_components = 4096, - .max_draw_buffers = 32, - .max_vertex_uniform_vectors = 128, - .max_varying_vectors = 8, - .max_fragment_uniform_vectors = 16, - .max_vertex_output_vectors = 16, - .max_fragment_input_vectors = 15, - .min_program_texel_offset = -8, - .max_program_texel_offset = 7, - .max_clip_distances = 8, - .max_compute_work_group_count_x = 65535, - .max_compute_work_group_count_y = 65535, - .max_compute_work_group_count_z = 65535, - .max_compute_work_group_size_x = 1024, - .max_compute_work_group_size_y = 1024, - .max_compute_work_group_size_z = 64, - .max_compute_uniform_components = 1024, - .max_compute_texture_image_units = 16, - .max_compute_image_uniforms = 8, - .max_compute_atomic_counters = 8, - .max_compute_atomic_counter_buffers = 1, - .max_varying_components = 60, - .max_vertex_output_components = 64, - .max_geometry_input_components = 64, - .max_geometry_output_components = 128, - .max_fragment_input_components = 128, - .max_image_units = 8, - .max_combined_image_units_and_fragment_outputs = 8, - .max_combined_shader_output_resources = 8, - .max_image_samples = 0, - .max_vertex_image_uniforms = 0, - .max_tess_control_image_uniforms = 0, - .max_tess_evaluation_image_uniforms = 0, - .max_geometry_image_uniforms = 0, - .max_fragment_image_uniforms = 8, - .max_combined_image_uniforms = 8, - .max_geometry_texture_image_units = 16, - .max_geometry_output_vertices = 256, - .max_geometry_total_output_components = 1024, - .max_geometry_uniform_components = 1024, - .max_geometry_varying_components = 64, - .max_tess_control_input_components = 128, - .max_tess_control_output_components = 128, - .max_tess_control_texture_image_units = 16, - .max_tess_control_uniform_components = 1024, - .max_tess_control_total_output_components = 4096, - .max_tess_evaluation_input_components = 128, - .max_tess_evaluation_output_components = 128, - .max_tess_evaluation_texture_image_units = 16, - .max_tess_evaluation_uniform_components = 1024, - .max_tess_patch_components = 120, - .max_patch_vertices = 32, - .max_tess_gen_level = 64, - .max_viewports = 16, - .max_vertex_atomic_counters = 0, - .max_tess_control_atomic_counters = 0, - .max_tess_evaluation_atomic_counters = 0, - .max_geometry_atomic_counters = 0, - .max_fragment_atomic_counters = 8, - .max_combined_atomic_counters = 8, - .max_atomic_counter_bindings = 1, - .max_vertex_atomic_counter_buffers = 0, - .max_tess_control_atomic_counter_buffers = 0, - .max_tess_evaluation_atomic_counter_buffers = 0, - .max_geometry_atomic_counter_buffers = 0, - .max_fragment_atomic_counter_buffers = 1, - .max_combined_atomic_counter_buffers = 1, - .max_atomic_counter_buffer_size = 16384, - .max_transform_feedback_buffers = 4, - .max_transform_feedback_interleaved_components = 64, - .max_cull_distances = 8, - .max_combined_clip_and_cull_distances = 8, - .max_samples = 4, - .max_mesh_output_vertices_nv = 256, - .max_mesh_output_primitives_nv = 512, - .max_mesh_work_group_size_x_nv = 32, - .max_mesh_work_group_size_y_nv = 1, - .max_mesh_work_group_size_z_nv = 1, - .max_task_work_group_size_x_nv = 32, - .max_task_work_group_size_y_nv = 1, - .max_task_work_group_size_z_nv = 1, - .max_mesh_view_count_nv = 4, - .maxDualSourceDrawBuffersEXT = 1, - - .limits = { - .non_inductive_for_loops = 1, - .while_loops = 1, - .do_while_loops = 1, - .general_uniform_indexing = 1, - .general_attribute_matrix_vector_indexing = 1, - .general_varying_indexing = 1, - .general_sampler_indexing = 1, - .general_variable_indexing = 1, - .general_constant_matrix_vector_indexing = 1, - } -}; - -static int glslc_shader_compile(FFVulkanContext *s, FFVkSPIRVCompiler *ctx, - FFVulkanShader *shd, uint8_t **data, - size_t *size, const char *entrypoint, - void **opaque) -{ - const char *messages; - glslang_shader_t *glslc_shader; - glslang_program_t *glslc_program; - - static const glslang_stage_t glslc_stage[] = { - [VK_SHADER_STAGE_VERTEX_BIT] = GLSLANG_STAGE_VERTEX, - [VK_SHADER_STAGE_FRAGMENT_BIT] = GLSLANG_STAGE_FRAGMENT, - [VK_SHADER_STAGE_COMPUTE_BIT] = GLSLANG_STAGE_COMPUTE, -#if ((GLSLANG_VERSION_MAJOR) > 12) - [VK_SHADER_STAGE_TASK_BIT_EXT] = GLSLANG_STAGE_TASK, - [VK_SHADER_STAGE_MESH_BIT_EXT] = GLSLANG_STAGE_MESH, - [VK_SHADER_STAGE_RAYGEN_BIT_KHR] = GLSLANG_STAGE_RAYGEN, - [VK_SHADER_STAGE_INTERSECTION_BIT_KHR] = GLSLANG_STAGE_INTERSECT, - [VK_SHADER_STAGE_ANY_HIT_BIT_KHR] = GLSLANG_STAGE_ANYHIT, - [VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR] = GLSLANG_STAGE_CLOSESTHIT, - [VK_SHADER_STAGE_MISS_BIT_KHR] = GLSLANG_STAGE_MISS, - [VK_SHADER_STAGE_CALLABLE_BIT_KHR] = GLSLANG_STAGE_CALLABLE, -#endif - }; - - const glslang_input_t glslc_input = { - .language = GLSLANG_SOURCE_GLSL, - .stage = glslc_stage[shd->stage], - .client = GLSLANG_CLIENT_VULKAN, -#if ((GLSLANG_VERSION_MAJOR) >= 12) - .client_version = GLSLANG_TARGET_VULKAN_1_3, - .target_language_version = GLSLANG_TARGET_SPV_1_6, -#else - .client_version = GLSLANG_TARGET_VULKAN_1_2, - .target_language_version = GLSLANG_TARGET_SPV_1_5, -#endif - .target_language = GLSLANG_TARGET_SPV, - .code = shd->src.str, - .default_version = 460, - .default_profile = GLSLANG_NO_PROFILE, - .force_default_version_and_profile = false, - .forward_compatible = false, - .messages = GLSLANG_MSG_DEFAULT_BIT, - .resource = &glslc_resource_limits, - }; - -#if ((GLSLANG_VERSION_MAJOR) >= 12) - glslang_spv_options_t glslc_opts = { - .generate_debug_info = !!(s->extensions & (FF_VK_EXT_DEBUG_UTILS | FF_VK_EXT_RELAXED_EXTENDED_INSTR)), - .emit_nonsemantic_shader_debug_info = !!(s->extensions & FF_VK_EXT_RELAXED_EXTENDED_INSTR), - .emit_nonsemantic_shader_debug_source = !!(s->extensions & FF_VK_EXT_RELAXED_EXTENDED_INSTR), - .disable_optimizer = !!(s->extensions & FF_VK_EXT_DEBUG_UTILS), - .strip_debug_info = !(s->extensions & (FF_VK_EXT_DEBUG_UTILS | FF_VK_EXT_RELAXED_EXTENDED_INSTR)), - .optimize_size = 0, - .disassemble = 0, - .validate = 1, - .compile_only = 0, - }; -#endif - - av_assert0(glslc_refcount); - - *opaque = NULL; - - if (!(glslc_shader = glslang_shader_create(&glslc_input))) - return AVERROR(ENOMEM); - - if (!glslang_shader_preprocess(glslc_shader, &glslc_input)) { - ff_vk_shader_print(s, shd, AV_LOG_WARNING); - av_log(s, AV_LOG_ERROR, "Unable to preprocess shader: %s (%s)!\n", - glslang_shader_get_info_log(glslc_shader), - glslang_shader_get_info_debug_log(glslc_shader)); - glslang_shader_delete(glslc_shader); - return AVERROR(EINVAL); - } - - if (!glslang_shader_parse(glslc_shader, &glslc_input)) { - ff_vk_shader_print(s, shd, AV_LOG_WARNING); - av_log(s, AV_LOG_ERROR, "Unable to parse shader: %s (%s)!\n", - glslang_shader_get_info_log(glslc_shader), - glslang_shader_get_info_debug_log(glslc_shader)); - glslang_shader_delete(glslc_shader); - return AVERROR(EINVAL); - } - - if (!(glslc_program = glslang_program_create())) { - glslang_shader_delete(glslc_shader); - return AVERROR(EINVAL); - } - - glslang_program_add_shader(glslc_program, glslc_shader); - - if (!glslang_program_link(glslc_program, GLSLANG_MSG_SPV_RULES_BIT | - GLSLANG_MSG_VULKAN_RULES_BIT)) { - ff_vk_shader_print(s, shd, AV_LOG_WARNING); - av_log(s, AV_LOG_ERROR, "Unable to link shader: %s (%s)!\n", - glslang_program_get_info_log(glslc_program), - glslang_program_get_info_debug_log(glslc_program)); - glslang_program_delete(glslc_program); - glslang_shader_delete(glslc_shader); - return AVERROR(EINVAL); - } - -#if ((GLSLANG_VERSION_MAJOR) >= 12) - glslang_program_SPIRV_generate_with_options(glslc_program, glslc_input.stage, &glslc_opts); -#else - glslang_program_SPIRV_generate(glslc_program, glslc_input.stage); -#endif - - messages = glslang_program_SPIRV_get_messages(glslc_program); - if (messages) { - ff_vk_shader_print(s, shd, AV_LOG_WARNING); - av_log(s, AV_LOG_WARNING, "%s\n", messages); - } else { - ff_vk_shader_print(s, shd, AV_LOG_VERBOSE); - } - - glslang_shader_delete(glslc_shader); - - *size = glslang_program_SPIRV_get_size(glslc_program) * sizeof(unsigned int); - *data = (void *)glslang_program_SPIRV_get_ptr(glslc_program); - *opaque = glslc_program; - - return 0; -} - -static void glslc_shader_free(FFVkSPIRVCompiler *ctx, void **opaque) -{ - if (!opaque || !*opaque) - return; - - av_assert0(glslc_refcount); - glslang_program_delete(*opaque); - *opaque = NULL; -} - -static void glslc_uninit(FFVkSPIRVCompiler **ctx) -{ - if (!ctx || !*ctx) - return; - - pthread_mutex_lock(&glslc_mutex); - if (glslc_refcount && (--glslc_refcount == 0)) - glslang_finalize_process(); - pthread_mutex_unlock(&glslc_mutex); - - av_freep(ctx); -} - -FFVkSPIRVCompiler *ff_vk_glslang_init(void) -{ - FFVkSPIRVCompiler *ret = av_mallocz(sizeof(*ret)); - if (!ret) - return NULL; - - ret->compile_shader = glslc_shader_compile; - ret->free_shader = glslc_shader_free; - ret->uninit = glslc_uninit; - - pthread_mutex_lock(&glslc_mutex); - if (!glslc_refcount++) { - if (!glslang_initialize_process()) { - av_freep(&ret); - glslc_refcount--; - } - } - pthread_mutex_unlock(&glslc_mutex); - - return ret; -} +#include "libavutil/vulkan_glslang.c" diff --git a/libavfilter/vulkan_shaderc.c b/libavfilter/vulkan_shaderc.c index 6e7475c49f..9f60bf4dfd 100644 --- a/libavfilter/vulkan_shaderc.c +++ b/libavfilter/vulkan_shaderc.c @@ -16,129 +16,4 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include <shaderc/shaderc.h> - -#include "libavutil/mem.h" -#include "vulkan_spirv.h" - -static int shdc_shader_compile(FFVulkanContext *s, FFVkSPIRVCompiler *ctx, - FFVulkanShader *shd, uint8_t **data, - size_t *size, const char *entrypoint, - void **opaque) -{ - int loglevel, err, warn, ret; - const char *status, *message; - shaderc_compilation_result_t res; - static const char *shdc_result[] = { - [shaderc_compilation_status_success] = "success", - [shaderc_compilation_status_invalid_stage] = "invalid stage", - [shaderc_compilation_status_compilation_error] = "error", - [shaderc_compilation_status_internal_error] = "internal error", - [shaderc_compilation_status_null_result_object] = "no result", - [shaderc_compilation_status_invalid_assembly] = "invalid assembly", - }; - static const shaderc_shader_kind shdc_kind[] = { - [VK_SHADER_STAGE_VERTEX_BIT] = shaderc_glsl_vertex_shader, - [VK_SHADER_STAGE_FRAGMENT_BIT] = shaderc_glsl_fragment_shader, - [VK_SHADER_STAGE_COMPUTE_BIT] = shaderc_glsl_compute_shader, - [VK_SHADER_STAGE_MESH_BIT_EXT] = shaderc_mesh_shader, - [VK_SHADER_STAGE_TASK_BIT_EXT] = shaderc_task_shader, - [VK_SHADER_STAGE_RAYGEN_BIT_KHR] = shaderc_raygen_shader, - [VK_SHADER_STAGE_ANY_HIT_BIT_KHR] = shaderc_anyhit_shader, - [VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR] = shaderc_closesthit_shader, - [VK_SHADER_STAGE_MISS_BIT_KHR] = shaderc_miss_shader, - [VK_SHADER_STAGE_INTERSECTION_BIT_KHR] = shaderc_intersection_shader, - [VK_SHADER_STAGE_CALLABLE_BIT_KHR] = shaderc_callable_shader, - }; - - shaderc_compile_options_t opts = shaderc_compile_options_initialize(); - *opaque = NULL; - if (!opts) - return AVERROR(ENOMEM); - - shaderc_compile_options_set_target_env(opts, shaderc_target_env_vulkan, - shaderc_env_version_vulkan_1_3); - shaderc_compile_options_set_target_spirv(opts, shaderc_spirv_version_1_6); - - /* If either extension is set, turn on debug info */ - if (s->extensions & (FF_VK_EXT_DEBUG_UTILS | FF_VK_EXT_RELAXED_EXTENDED_INSTR)) - shaderc_compile_options_set_generate_debug_info(opts); - - if (s->extensions & FF_VK_EXT_DEBUG_UTILS) - shaderc_compile_options_set_optimization_level(opts, - shaderc_optimization_level_zero); - else - shaderc_compile_options_set_optimization_level(opts, - shaderc_optimization_level_performance); - - res = shaderc_compile_into_spv((shaderc_compiler_t)ctx->priv, - shd->src.str, strlen(shd->src.str), - shdc_kind[shd->stage], - shd->name, entrypoint, opts); - shaderc_compile_options_release(opts); - - ret = shaderc_result_get_compilation_status(res); - err = shaderc_result_get_num_errors(res); - warn = shaderc_result_get_num_warnings(res); - message = shaderc_result_get_error_message(res); - - if (ret != shaderc_compilation_status_success && !err) - err = 1; - - loglevel = err ? AV_LOG_ERROR : warn ? AV_LOG_WARNING : AV_LOG_VERBOSE; - - ff_vk_shader_print(s, shd, loglevel); - if (message && (err || warn)) - av_log(s, loglevel, "%s\n", message); - status = ret < FF_ARRAY_ELEMS(shdc_result) ? shdc_result[ret] : "unknown"; - av_log(s, loglevel, "shaderc compile status '%s' (%d errors, %d warnings)\n", - status, err, warn); - - if (err > 0) - return AVERROR(EINVAL); - - *data = (uint8_t *)shaderc_result_get_bytes(res); - *size = shaderc_result_get_length(res); - *opaque = res; - - return 0; -} - -static void shdc_shader_free(FFVkSPIRVCompiler *ctx, void **opaque) -{ - if (!opaque || !*opaque) - return; - - shaderc_result_release((shaderc_compilation_result_t)*opaque); - *opaque = NULL; -} - -static void shdc_uninit(FFVkSPIRVCompiler **ctx) -{ - FFVkSPIRVCompiler *s; - - if (!ctx || !*ctx) - return; - - s = *ctx; - - shaderc_compiler_release((shaderc_compiler_t)s->priv); - av_freep(ctx); -} - -FFVkSPIRVCompiler *ff_vk_shaderc_init(void) -{ - FFVkSPIRVCompiler *ret = av_mallocz(sizeof(*ret)); - if (!ret) - return NULL; - - ret->compile_shader = shdc_shader_compile; - ret->free_shader = shdc_shader_free; - ret->uninit = shdc_uninit; - - ret->priv = (void *)shaderc_compiler_initialize(); - if (!ret->priv) - av_freep(&ret); - - return ret; -} +#include "libavutil/vulkan_shaderc.c" diff --git a/libavutil/Makefile b/libavutil/Makefile index d3c95b12a0..847878d7a4 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -228,6 +228,8 @@ SKIPHEADERS-$(CONFIG_VDPAU) += hwcontext_vdpau.h SKIPHEADERS-$(CONFIG_VULKAN) += hwcontext_vulkan.h vulkan.h \ vulkan_functions.h \ vulkan_loader.h +SKIPHEADERS-$(CONFIG_LIBSHADERC) += vulkan_spirv.h +SKIPHEADERS-$(CONFIG_LIBGLSLANG) += vulkan_spirv.h TESTPROGS = adler32 \ aes \ diff --git a/libavutil/vulkan_glslang.c b/libavutil/vulkan_glslang.c new file mode 100644 index 0000000000..9e1b8f4d8d --- /dev/null +++ b/libavutil/vulkan_glslang.c @@ -0,0 +1,308 @@ +/* + * 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 + */ + +#include <pthread.h> + +#include <glslang/build_info.h> +#include <glslang/Include/glslang_c_interface.h> + +#include "vulkan_spirv.h" +#include "libavutil/mem.h" +#include "libavutil/avassert.h" + +static pthread_mutex_t glslc_mutex = PTHREAD_MUTEX_INITIALIZER; +static int glslc_refcount = 0; + +static const glslang_resource_t glslc_resource_limits = { + .max_lights = 32, + .max_clip_planes = 6, + .max_texture_units = 32, + .max_texture_coords = 32, + .max_vertex_attribs = 64, + .max_vertex_uniform_components = 4096, + .max_varying_floats = 64, + .max_vertex_texture_image_units = 32, + .max_combined_texture_image_units = 80, + .max_texture_image_units = 32, + .max_fragment_uniform_components = 4096, + .max_draw_buffers = 32, + .max_vertex_uniform_vectors = 128, + .max_varying_vectors = 8, + .max_fragment_uniform_vectors = 16, + .max_vertex_output_vectors = 16, + .max_fragment_input_vectors = 15, + .min_program_texel_offset = -8, + .max_program_texel_offset = 7, + .max_clip_distances = 8, + .max_compute_work_group_count_x = 65535, + .max_compute_work_group_count_y = 65535, + .max_compute_work_group_count_z = 65535, + .max_compute_work_group_size_x = 1024, + .max_compute_work_group_size_y = 1024, + .max_compute_work_group_size_z = 64, + .max_compute_uniform_components = 1024, + .max_compute_texture_image_units = 16, + .max_compute_image_uniforms = 8, + .max_compute_atomic_counters = 8, + .max_compute_atomic_counter_buffers = 1, + .max_varying_components = 60, + .max_vertex_output_components = 64, + .max_geometry_input_components = 64, + .max_geometry_output_components = 128, + .max_fragment_input_components = 128, + .max_image_units = 8, + .max_combined_image_units_and_fragment_outputs = 8, + .max_combined_shader_output_resources = 8, + .max_image_samples = 0, + .max_vertex_image_uniforms = 0, + .max_tess_control_image_uniforms = 0, + .max_tess_evaluation_image_uniforms = 0, + .max_geometry_image_uniforms = 0, + .max_fragment_image_uniforms = 8, + .max_combined_image_uniforms = 8, + .max_geometry_texture_image_units = 16, + .max_geometry_output_vertices = 256, + .max_geometry_total_output_components = 1024, + .max_geometry_uniform_components = 1024, + .max_geometry_varying_components = 64, + .max_tess_control_input_components = 128, + .max_tess_control_output_components = 128, + .max_tess_control_texture_image_units = 16, + .max_tess_control_uniform_components = 1024, + .max_tess_control_total_output_components = 4096, + .max_tess_evaluation_input_components = 128, + .max_tess_evaluation_output_components = 128, + .max_tess_evaluation_texture_image_units = 16, + .max_tess_evaluation_uniform_components = 1024, + .max_tess_patch_components = 120, + .max_patch_vertices = 32, + .max_tess_gen_level = 64, + .max_viewports = 16, + .max_vertex_atomic_counters = 0, + .max_tess_control_atomic_counters = 0, + .max_tess_evaluation_atomic_counters = 0, + .max_geometry_atomic_counters = 0, + .max_fragment_atomic_counters = 8, + .max_combined_atomic_counters = 8, + .max_atomic_counter_bindings = 1, + .max_vertex_atomic_counter_buffers = 0, + .max_tess_control_atomic_counter_buffers = 0, + .max_tess_evaluation_atomic_counter_buffers = 0, + .max_geometry_atomic_counter_buffers = 0, + .max_fragment_atomic_counter_buffers = 1, + .max_combined_atomic_counter_buffers = 1, + .max_atomic_counter_buffer_size = 16384, + .max_transform_feedback_buffers = 4, + .max_transform_feedback_interleaved_components = 64, + .max_cull_distances = 8, + .max_combined_clip_and_cull_distances = 8, + .max_samples = 4, + .max_mesh_output_vertices_nv = 256, + .max_mesh_output_primitives_nv = 512, + .max_mesh_work_group_size_x_nv = 32, + .max_mesh_work_group_size_y_nv = 1, + .max_mesh_work_group_size_z_nv = 1, + .max_task_work_group_size_x_nv = 32, + .max_task_work_group_size_y_nv = 1, + .max_task_work_group_size_z_nv = 1, + .max_mesh_view_count_nv = 4, + .maxDualSourceDrawBuffersEXT = 1, + + .limits = { + .non_inductive_for_loops = 1, + .while_loops = 1, + .do_while_loops = 1, + .general_uniform_indexing = 1, + .general_attribute_matrix_vector_indexing = 1, + .general_varying_indexing = 1, + .general_sampler_indexing = 1, + .general_variable_indexing = 1, + .general_constant_matrix_vector_indexing = 1, + } +}; + +static int glslc_shader_compile(FFVulkanContext *s, FFVkSPIRVCompiler *ctx, + FFVulkanShader *shd, uint8_t **data, + size_t *size, const char *entrypoint, + void **opaque) +{ + const char *messages; + glslang_shader_t *glslc_shader; + glslang_program_t *glslc_program; + + static const glslang_stage_t glslc_stage[] = { + [VK_SHADER_STAGE_VERTEX_BIT] = GLSLANG_STAGE_VERTEX, + [VK_SHADER_STAGE_FRAGMENT_BIT] = GLSLANG_STAGE_FRAGMENT, + [VK_SHADER_STAGE_COMPUTE_BIT] = GLSLANG_STAGE_COMPUTE, +#if ((GLSLANG_VERSION_MAJOR) > 12) + [VK_SHADER_STAGE_TASK_BIT_EXT] = GLSLANG_STAGE_TASK, + [VK_SHADER_STAGE_MESH_BIT_EXT] = GLSLANG_STAGE_MESH, + [VK_SHADER_STAGE_RAYGEN_BIT_KHR] = GLSLANG_STAGE_RAYGEN, + [VK_SHADER_STAGE_INTERSECTION_BIT_KHR] = GLSLANG_STAGE_INTERSECT, + [VK_SHADER_STAGE_ANY_HIT_BIT_KHR] = GLSLANG_STAGE_ANYHIT, + [VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR] = GLSLANG_STAGE_CLOSESTHIT, + [VK_SHADER_STAGE_MISS_BIT_KHR] = GLSLANG_STAGE_MISS, + [VK_SHADER_STAGE_CALLABLE_BIT_KHR] = GLSLANG_STAGE_CALLABLE, +#endif + }; + + const glslang_input_t glslc_input = { + .language = GLSLANG_SOURCE_GLSL, + .stage = glslc_stage[shd->stage], + .client = GLSLANG_CLIENT_VULKAN, +#if ((GLSLANG_VERSION_MAJOR) >= 12) + .client_version = GLSLANG_TARGET_VULKAN_1_3, + .target_language_version = GLSLANG_TARGET_SPV_1_6, +#else + .client_version = GLSLANG_TARGET_VULKAN_1_2, + .target_language_version = GLSLANG_TARGET_SPV_1_5, +#endif + .target_language = GLSLANG_TARGET_SPV, + .code = shd->src.str, + .default_version = 460, + .default_profile = GLSLANG_NO_PROFILE, + .force_default_version_and_profile = false, + .forward_compatible = false, + .messages = GLSLANG_MSG_DEFAULT_BIT, + .resource = &glslc_resource_limits, + }; + +#if ((GLSLANG_VERSION_MAJOR) >= 12) + glslang_spv_options_t glslc_opts = { + .generate_debug_info = !!(s->extensions & (FF_VK_EXT_DEBUG_UTILS | FF_VK_EXT_RELAXED_EXTENDED_INSTR)), + .emit_nonsemantic_shader_debug_info = !!(s->extensions & FF_VK_EXT_RELAXED_EXTENDED_INSTR), + .emit_nonsemantic_shader_debug_source = !!(s->extensions & FF_VK_EXT_RELAXED_EXTENDED_INSTR), + .disable_optimizer = !!(s->extensions & FF_VK_EXT_DEBUG_UTILS), + .strip_debug_info = !(s->extensions & (FF_VK_EXT_DEBUG_UTILS | FF_VK_EXT_RELAXED_EXTENDED_INSTR)), + .optimize_size = 0, + .disassemble = 0, + .validate = 1, + .compile_only = 0, + }; +#endif + + av_assert0(glslc_refcount); + + *opaque = NULL; + + if (!(glslc_shader = glslang_shader_create(&glslc_input))) + return AVERROR(ENOMEM); + + if (!glslang_shader_preprocess(glslc_shader, &glslc_input)) { + ff_vk_shader_print(s, shd, AV_LOG_WARNING); + av_log(s, AV_LOG_ERROR, "Unable to preprocess shader: %s (%s)!\n", + glslang_shader_get_info_log(glslc_shader), + glslang_shader_get_info_debug_log(glslc_shader)); + glslang_shader_delete(glslc_shader); + return AVERROR(EINVAL); + } + + if (!glslang_shader_parse(glslc_shader, &glslc_input)) { + ff_vk_shader_print(s, shd, AV_LOG_WARNING); + av_log(s, AV_LOG_ERROR, "Unable to parse shader: %s (%s)!\n", + glslang_shader_get_info_log(glslc_shader), + glslang_shader_get_info_debug_log(glslc_shader)); + glslang_shader_delete(glslc_shader); + return AVERROR(EINVAL); + } + + if (!(glslc_program = glslang_program_create())) { + glslang_shader_delete(glslc_shader); + return AVERROR(EINVAL); + } + + glslang_program_add_shader(glslc_program, glslc_shader); + + if (!glslang_program_link(glslc_program, GLSLANG_MSG_SPV_RULES_BIT | + GLSLANG_MSG_VULKAN_RULES_BIT)) { + ff_vk_shader_print(s, shd, AV_LOG_WARNING); + av_log(s, AV_LOG_ERROR, "Unable to link shader: %s (%s)!\n", + glslang_program_get_info_log(glslc_program), + glslang_program_get_info_debug_log(glslc_program)); + glslang_program_delete(glslc_program); + glslang_shader_delete(glslc_shader); + return AVERROR(EINVAL); + } + +#if ((GLSLANG_VERSION_MAJOR) >= 12) + glslang_program_SPIRV_generate_with_options(glslc_program, glslc_input.stage, &glslc_opts); +#else + glslang_program_SPIRV_generate(glslc_program, glslc_input.stage); +#endif + + messages = glslang_program_SPIRV_get_messages(glslc_program); + if (messages) { + ff_vk_shader_print(s, shd, AV_LOG_WARNING); + av_log(s, AV_LOG_WARNING, "%s\n", messages); + } else { + ff_vk_shader_print(s, shd, AV_LOG_VERBOSE); + } + + glslang_shader_delete(glslc_shader); + + *size = glslang_program_SPIRV_get_size(glslc_program) * sizeof(unsigned int); + *data = (void *)glslang_program_SPIRV_get_ptr(glslc_program); + *opaque = glslc_program; + + return 0; +} + +static void glslc_shader_free(FFVkSPIRVCompiler *ctx, void **opaque) +{ + if (!opaque || !*opaque) + return; + + av_assert0(glslc_refcount); + glslang_program_delete(*opaque); + *opaque = NULL; +} + +static void glslc_uninit(FFVkSPIRVCompiler **ctx) +{ + if (!ctx || !*ctx) + return; + + pthread_mutex_lock(&glslc_mutex); + if (glslc_refcount && (--glslc_refcount == 0)) + glslang_finalize_process(); + pthread_mutex_unlock(&glslc_mutex); + + av_freep(ctx); +} + +FFVkSPIRVCompiler *ff_vk_glslang_init(void) +{ + FFVkSPIRVCompiler *ret = av_mallocz(sizeof(*ret)); + if (!ret) + return NULL; + + ret->compile_shader = glslc_shader_compile; + ret->free_shader = glslc_shader_free; + ret->uninit = glslc_uninit; + + pthread_mutex_lock(&glslc_mutex); + if (!glslc_refcount++) { + if (!glslang_initialize_process()) { + av_freep(&ret); + glslc_refcount--; + } + } + pthread_mutex_unlock(&glslc_mutex); + + return ret; +} diff --git a/libavutil/vulkan_shaderc.c b/libavutil/vulkan_shaderc.c new file mode 100644 index 0000000000..6e7475c49f --- /dev/null +++ b/libavutil/vulkan_shaderc.c @@ -0,0 +1,144 @@ +/* + * 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 + */ + +#include <shaderc/shaderc.h> + +#include "libavutil/mem.h" +#include "vulkan_spirv.h" + +static int shdc_shader_compile(FFVulkanContext *s, FFVkSPIRVCompiler *ctx, + FFVulkanShader *shd, uint8_t **data, + size_t *size, const char *entrypoint, + void **opaque) +{ + int loglevel, err, warn, ret; + const char *status, *message; + shaderc_compilation_result_t res; + static const char *shdc_result[] = { + [shaderc_compilation_status_success] = "success", + [shaderc_compilation_status_invalid_stage] = "invalid stage", + [shaderc_compilation_status_compilation_error] = "error", + [shaderc_compilation_status_internal_error] = "internal error", + [shaderc_compilation_status_null_result_object] = "no result", + [shaderc_compilation_status_invalid_assembly] = "invalid assembly", + }; + static const shaderc_shader_kind shdc_kind[] = { + [VK_SHADER_STAGE_VERTEX_BIT] = shaderc_glsl_vertex_shader, + [VK_SHADER_STAGE_FRAGMENT_BIT] = shaderc_glsl_fragment_shader, + [VK_SHADER_STAGE_COMPUTE_BIT] = shaderc_glsl_compute_shader, + [VK_SHADER_STAGE_MESH_BIT_EXT] = shaderc_mesh_shader, + [VK_SHADER_STAGE_TASK_BIT_EXT] = shaderc_task_shader, + [VK_SHADER_STAGE_RAYGEN_BIT_KHR] = shaderc_raygen_shader, + [VK_SHADER_STAGE_ANY_HIT_BIT_KHR] = shaderc_anyhit_shader, + [VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR] = shaderc_closesthit_shader, + [VK_SHADER_STAGE_MISS_BIT_KHR] = shaderc_miss_shader, + [VK_SHADER_STAGE_INTERSECTION_BIT_KHR] = shaderc_intersection_shader, + [VK_SHADER_STAGE_CALLABLE_BIT_KHR] = shaderc_callable_shader, + }; + + shaderc_compile_options_t opts = shaderc_compile_options_initialize(); + *opaque = NULL; + if (!opts) + return AVERROR(ENOMEM); + + shaderc_compile_options_set_target_env(opts, shaderc_target_env_vulkan, + shaderc_env_version_vulkan_1_3); + shaderc_compile_options_set_target_spirv(opts, shaderc_spirv_version_1_6); + + /* If either extension is set, turn on debug info */ + if (s->extensions & (FF_VK_EXT_DEBUG_UTILS | FF_VK_EXT_RELAXED_EXTENDED_INSTR)) + shaderc_compile_options_set_generate_debug_info(opts); + + if (s->extensions & FF_VK_EXT_DEBUG_UTILS) + shaderc_compile_options_set_optimization_level(opts, + shaderc_optimization_level_zero); + else + shaderc_compile_options_set_optimization_level(opts, + shaderc_optimization_level_performance); + + res = shaderc_compile_into_spv((shaderc_compiler_t)ctx->priv, + shd->src.str, strlen(shd->src.str), + shdc_kind[shd->stage], + shd->name, entrypoint, opts); + shaderc_compile_options_release(opts); + + ret = shaderc_result_get_compilation_status(res); + err = shaderc_result_get_num_errors(res); + warn = shaderc_result_get_num_warnings(res); + message = shaderc_result_get_error_message(res); + + if (ret != shaderc_compilation_status_success && !err) + err = 1; + + loglevel = err ? AV_LOG_ERROR : warn ? AV_LOG_WARNING : AV_LOG_VERBOSE; + + ff_vk_shader_print(s, shd, loglevel); + if (message && (err || warn)) + av_log(s, loglevel, "%s\n", message); + status = ret < FF_ARRAY_ELEMS(shdc_result) ? shdc_result[ret] : "unknown"; + av_log(s, loglevel, "shaderc compile status '%s' (%d errors, %d warnings)\n", + status, err, warn); + + if (err > 0) + return AVERROR(EINVAL); + + *data = (uint8_t *)shaderc_result_get_bytes(res); + *size = shaderc_result_get_length(res); + *opaque = res; + + return 0; +} + +static void shdc_shader_free(FFVkSPIRVCompiler *ctx, void **opaque) +{ + if (!opaque || !*opaque) + return; + + shaderc_result_release((shaderc_compilation_result_t)*opaque); + *opaque = NULL; +} + +static void shdc_uninit(FFVkSPIRVCompiler **ctx) +{ + FFVkSPIRVCompiler *s; + + if (!ctx || !*ctx) + return; + + s = *ctx; + + shaderc_compiler_release((shaderc_compiler_t)s->priv); + av_freep(ctx); +} + +FFVkSPIRVCompiler *ff_vk_shaderc_init(void) +{ + FFVkSPIRVCompiler *ret = av_mallocz(sizeof(*ret)); + if (!ret) + return NULL; + + ret->compile_shader = shdc_shader_compile; + ret->free_shader = shdc_shader_free; + ret->uninit = shdc_uninit; + + ret->priv = (void *)shaderc_compiler_initialize(); + if (!ret->priv) + av_freep(&ret); + + return ret; +} diff --git a/libavfilter/vulkan_spirv.h b/libavutil/vulkan_spirv.h index b0349b6b54..c51f444997 100644 --- a/libavfilter/vulkan_spirv.h +++ b/libavutil/vulkan_spirv.h @@ -19,7 +19,7 @@ #ifndef AVFILTER_VULKAN_SPIRV_H #define AVFILTER_VULKAN_SPIRV_H -#include "libavutil/vulkan.h" +#include "vulkan.h" #include "config.h" |