FFmpeg
vulkan_shaderc.c
Go to the documentation of this file.
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 "libavutil/mem.h"
22 #include "vulkan_spirv.h"
23 
24 static int shdc_shader_compile(FFVkSPIRVCompiler *ctx, void *avctx,
25  FFVkSPIRVShader *shd, uint8_t **data,
26  size_t *size, const char *entrypoint,
27  void **opaque)
28 {
29  int loglevel, err, warn, ret;
30  const char *status, *message;
31  shaderc_compilation_result_t res;
32  static const char *shdc_result[] = {
33  [shaderc_compilation_status_success] = "success",
34  [shaderc_compilation_status_invalid_stage] = "invalid stage",
35  [shaderc_compilation_status_compilation_error] = "error",
36  [shaderc_compilation_status_internal_error] = "internal error",
37  [shaderc_compilation_status_null_result_object] = "no result",
38  [shaderc_compilation_status_invalid_assembly] = "invalid assembly",
39  };
40  static const shaderc_shader_kind shdc_kind[] = {
41  [VK_SHADER_STAGE_VERTEX_BIT] = shaderc_glsl_vertex_shader,
42  [VK_SHADER_STAGE_FRAGMENT_BIT] = shaderc_glsl_fragment_shader,
43  [VK_SHADER_STAGE_COMPUTE_BIT] = shaderc_glsl_compute_shader,
44  };
45 
46  shaderc_compile_options_t opts = shaderc_compile_options_initialize();
47  *opaque = NULL;
48  if (!opts)
49  return AVERROR(ENOMEM);
50 
51  shaderc_compile_options_set_target_env(opts, shaderc_target_env_vulkan,
52  shaderc_env_version_vulkan_1_2);
53  shaderc_compile_options_set_target_spirv(opts, shaderc_spirv_version_1_5);
54  shaderc_compile_options_set_optimization_level(opts,
55  shaderc_optimization_level_performance);
56 
57  res = shaderc_compile_into_spv((shaderc_compiler_t)ctx->priv,
58  shd->src.str, strlen(shd->src.str),
59  shdc_kind[shd->shader.stage],
60  shd->name, entrypoint, opts);
61  shaderc_compile_options_release(opts);
62 
63  ret = shaderc_result_get_compilation_status(res);
64  err = shaderc_result_get_num_errors(res);
65  warn = shaderc_result_get_num_warnings(res);
66  message = shaderc_result_get_error_message(res);
67 
68  loglevel = err ? AV_LOG_ERROR : warn ? AV_LOG_WARNING : AV_LOG_VERBOSE;
69 
70  ff_vk_shader_print(avctx, shd, loglevel);
71  if (message && (err || warn))
72  av_log(avctx, loglevel, "%s\n", message);
73  status = ret < FF_ARRAY_ELEMS(shdc_result) ? shdc_result[ret] : "unknown";
74  av_log(avctx, loglevel, "shaderc compile status '%s' (%d errors, %d warnings)\n",
75  status, err, warn);
76 
77  if (err > 0)
78  return AVERROR(EINVAL);
79 
80  *data = (uint8_t *)shaderc_result_get_bytes(res);
81  *size = shaderc_result_get_length(res);
82  *opaque = res;
83 
84  return 0;
85 }
86 
87 static void shdc_shader_free(FFVkSPIRVCompiler *ctx, void **opaque)
88 {
89  if (!opaque || !*opaque)
90  return;
91 
92  shaderc_result_release((shaderc_compilation_result_t)*opaque);
93  *opaque = NULL;
94 }
95 
97 {
99 
100  if (!ctx || !*ctx)
101  return;
102 
103  s = *ctx;
104 
105  shaderc_compiler_release((shaderc_compiler_t)s->priv);
106  av_freep(ctx);
107 }
108 
110 {
111  FFVkSPIRVCompiler *ret = av_mallocz(sizeof(*ret));
112  if (!ret)
113  return NULL;
114 
115  ret->compile_shader = shdc_shader_compile;
116  ret->free_shader = shdc_shader_free;
117  ret->uninit = shdc_uninit;
118 
119  ret->priv = (void *)shaderc_compiler_initialize();
120  if (!ret->priv)
121  av_freep(&ret);
122 
123  return ret;
124 }
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
message
Definition: api-threadmessage-test.c:46
data
const char data[16]
Definition: mxf.c:148
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
shdc_shader_compile
static int shdc_shader_compile(FFVkSPIRVCompiler *ctx, void *avctx, FFVkSPIRVShader *shd, uint8_t **data, size_t *size, const char *entrypoint, void **opaque)
Definition: vulkan_shaderc.c:24
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
shdc_shader_free
static void shdc_shader_free(FFVkSPIRVCompiler *ctx, void **opaque)
Definition: vulkan_shaderc.c:87
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
s
#define s(width, name)
Definition: cbs_vp9.c:198
ctx
AVFormatContext * ctx
Definition: movenc.c:48
opts
AVDictionary * opts
Definition: movenc.c:50
NULL
#define NULL
Definition: coverity.c:32
ff_vk_shader_print
void ff_vk_shader_print(void *ctx, FFVkSPIRVShader *shd, int prio)
Definition: vulkan.c:1383
ff_vk_shaderc_init
FFVkSPIRVCompiler * ff_vk_shaderc_init(void)
Definition: vulkan_shaderc.c:109
size
int size
Definition: twinvq_data.h:10344
FFVkSPIRVCompiler
Definition: vulkan_spirv.h:27
FFVkSPIRVShader::shader
VkPipelineShaderStageCreateInfo shader
Definition: vulkan.h:79
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
vulkan_spirv.h
FFVkSPIRVShader::src
AVBPrint src
Definition: vulkan.h:77
ret
ret
Definition: filter_design.txt:187
status
ov_status_e status
Definition: dnn_backend_openvino.c:120
FFVkSPIRVShader
Definition: vulkan.h:75
mem.h
FFVkSPIRVShader::name
const char * name
Definition: vulkan.h:76
message
static int FUNC() message(CodedBitstreamContext *ctx, RWContext *rw, SEIRawMessage *current)
Definition: cbs_sei_syntax_template.c:165
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
shdc_uninit
static void shdc_uninit(FFVkSPIRVCompiler **ctx)
Definition: vulkan_shaderc.c:96