FFmpeg
vulkan_glslang.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 <pthread.h>
20 
21 #include <glslang/build_info.h>
22 #include <glslang/Include/glslang_c_interface.h>
23 
24 #include "vulkan_spirv.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/avassert.h"
27 
29 static int glslc_refcount = 0;
30 
31 static const glslang_resource_t glslc_resource_limits = {
32  .max_lights = 32,
33  .max_clip_planes = 6,
34  .max_texture_units = 32,
35  .max_texture_coords = 32,
36  .max_vertex_attribs = 64,
37  .max_vertex_uniform_components = 4096,
38  .max_varying_floats = 64,
39  .max_vertex_texture_image_units = 32,
40  .max_combined_texture_image_units = 80,
41  .max_texture_image_units = 32,
42  .max_fragment_uniform_components = 4096,
43  .max_draw_buffers = 32,
44  .max_vertex_uniform_vectors = 128,
45  .max_varying_vectors = 8,
46  .max_fragment_uniform_vectors = 16,
47  .max_vertex_output_vectors = 16,
48  .max_fragment_input_vectors = 15,
49  .min_program_texel_offset = -8,
50  .max_program_texel_offset = 7,
51  .max_clip_distances = 8,
52  .max_compute_work_group_count_x = 65535,
53  .max_compute_work_group_count_y = 65535,
54  .max_compute_work_group_count_z = 65535,
55  .max_compute_work_group_size_x = 1024,
56  .max_compute_work_group_size_y = 1024,
57  .max_compute_work_group_size_z = 64,
58  .max_compute_uniform_components = 1024,
59  .max_compute_texture_image_units = 16,
60  .max_compute_image_uniforms = 8,
61  .max_compute_atomic_counters = 8,
62  .max_compute_atomic_counter_buffers = 1,
63  .max_varying_components = 60,
64  .max_vertex_output_components = 64,
65  .max_geometry_input_components = 64,
66  .max_geometry_output_components = 128,
67  .max_fragment_input_components = 128,
68  .max_image_units = 8,
69  .max_combined_image_units_and_fragment_outputs = 8,
70  .max_combined_shader_output_resources = 8,
71  .max_image_samples = 0,
72  .max_vertex_image_uniforms = 0,
73  .max_tess_control_image_uniforms = 0,
74  .max_tess_evaluation_image_uniforms = 0,
75  .max_geometry_image_uniforms = 0,
76  .max_fragment_image_uniforms = 8,
77  .max_combined_image_uniforms = 8,
78  .max_geometry_texture_image_units = 16,
79  .max_geometry_output_vertices = 256,
80  .max_geometry_total_output_components = 1024,
81  .max_geometry_uniform_components = 1024,
82  .max_geometry_varying_components = 64,
83  .max_tess_control_input_components = 128,
84  .max_tess_control_output_components = 128,
85  .max_tess_control_texture_image_units = 16,
86  .max_tess_control_uniform_components = 1024,
87  .max_tess_control_total_output_components = 4096,
88  .max_tess_evaluation_input_components = 128,
89  .max_tess_evaluation_output_components = 128,
90  .max_tess_evaluation_texture_image_units = 16,
91  .max_tess_evaluation_uniform_components = 1024,
92  .max_tess_patch_components = 120,
93  .max_patch_vertices = 32,
94  .max_tess_gen_level = 64,
95  .max_viewports = 16,
96  .max_vertex_atomic_counters = 0,
97  .max_tess_control_atomic_counters = 0,
98  .max_tess_evaluation_atomic_counters = 0,
99  .max_geometry_atomic_counters = 0,
100  .max_fragment_atomic_counters = 8,
101  .max_combined_atomic_counters = 8,
102  .max_atomic_counter_bindings = 1,
103  .max_vertex_atomic_counter_buffers = 0,
104  .max_tess_control_atomic_counter_buffers = 0,
105  .max_tess_evaluation_atomic_counter_buffers = 0,
106  .max_geometry_atomic_counter_buffers = 0,
107  .max_fragment_atomic_counter_buffers = 1,
108  .max_combined_atomic_counter_buffers = 1,
109  .max_atomic_counter_buffer_size = 16384,
110  .max_transform_feedback_buffers = 4,
111  .max_transform_feedback_interleaved_components = 64,
112  .max_cull_distances = 8,
113  .max_combined_clip_and_cull_distances = 8,
114  .max_samples = 4,
115  .max_mesh_output_vertices_nv = 256,
116  .max_mesh_output_primitives_nv = 512,
117  .max_mesh_work_group_size_x_nv = 32,
118  .max_mesh_work_group_size_y_nv = 1,
119  .max_mesh_work_group_size_z_nv = 1,
120  .max_task_work_group_size_x_nv = 32,
121  .max_task_work_group_size_y_nv = 1,
122  .max_task_work_group_size_z_nv = 1,
123  .max_mesh_view_count_nv = 4,
124  .maxDualSourceDrawBuffersEXT = 1,
125 
126  .limits = {
127  .non_inductive_for_loops = 1,
128  .while_loops = 1,
129  .do_while_loops = 1,
130  .general_uniform_indexing = 1,
131  .general_attribute_matrix_vector_indexing = 1,
132  .general_varying_indexing = 1,
133  .general_sampler_indexing = 1,
134  .general_variable_indexing = 1,
135  .general_constant_matrix_vector_indexing = 1,
136  }
137 };
138 
140  FFVulkanShader *shd, uint8_t **data,
141  size_t *size, const char *entrypoint,
142  void **opaque)
143 {
144  const char *messages;
145  glslang_shader_t *glslc_shader;
146  glslang_program_t *glslc_program;
147 
148  static const glslang_stage_t glslc_stage[] = {
149  [VK_SHADER_STAGE_VERTEX_BIT] = GLSLANG_STAGE_VERTEX,
150  [VK_SHADER_STAGE_FRAGMENT_BIT] = GLSLANG_STAGE_FRAGMENT,
151  [VK_SHADER_STAGE_COMPUTE_BIT] = GLSLANG_STAGE_COMPUTE,
152 #if ((GLSLANG_VERSION_MAJOR) > 12)
153  [VK_SHADER_STAGE_TASK_BIT_EXT] = GLSLANG_STAGE_TASK,
154  [VK_SHADER_STAGE_MESH_BIT_EXT] = GLSLANG_STAGE_MESH,
155  [VK_SHADER_STAGE_RAYGEN_BIT_KHR] = GLSLANG_STAGE_RAYGEN,
156  [VK_SHADER_STAGE_INTERSECTION_BIT_KHR] = GLSLANG_STAGE_INTERSECT,
157  [VK_SHADER_STAGE_ANY_HIT_BIT_KHR] = GLSLANG_STAGE_ANYHIT,
158  [VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR] = GLSLANG_STAGE_CLOSESTHIT,
159  [VK_SHADER_STAGE_MISS_BIT_KHR] = GLSLANG_STAGE_MISS,
160  [VK_SHADER_STAGE_CALLABLE_BIT_KHR] = GLSLANG_STAGE_CALLABLE,
161 #endif
162  };
163 
164  const glslang_input_t glslc_input = {
165  .language = GLSLANG_SOURCE_GLSL,
166  .stage = glslc_stage[shd->stage],
167  .client = GLSLANG_CLIENT_VULKAN,
168 #if ((GLSLANG_VERSION_MAJOR) >= 12)
169  .client_version = GLSLANG_TARGET_VULKAN_1_3,
170  .target_language_version = GLSLANG_TARGET_SPV_1_6,
171 #else
172  .client_version = GLSLANG_TARGET_VULKAN_1_2,
173  .target_language_version = GLSLANG_TARGET_SPV_1_5,
174 #endif
175  .target_language = GLSLANG_TARGET_SPV,
176  .code = shd->src.str,
177  .default_version = 460,
178  .default_profile = GLSLANG_NO_PROFILE,
179  .force_default_version_and_profile = false,
180  .forward_compatible = false,
181  .messages = GLSLANG_MSG_DEFAULT_BIT,
182  .resource = &glslc_resource_limits,
183  };
184 
185 #if ((GLSLANG_VERSION_MAJOR) >= 12)
186  glslang_spv_options_t glslc_opts = {
187  .generate_debug_info = !!(s->extensions & (FF_VK_EXT_DEBUG_UTILS | FF_VK_EXT_RELAXED_EXTENDED_INSTR)),
188  .emit_nonsemantic_shader_debug_info = !!(s->extensions & FF_VK_EXT_RELAXED_EXTENDED_INSTR),
189  .emit_nonsemantic_shader_debug_source = !!(s->extensions & FF_VK_EXT_RELAXED_EXTENDED_INSTR),
190  .disable_optimizer = !!(s->extensions & FF_VK_EXT_DEBUG_UTILS),
191  .strip_debug_info = !(s->extensions & (FF_VK_EXT_DEBUG_UTILS | FF_VK_EXT_RELAXED_EXTENDED_INSTR)),
192  .optimize_size = 0,
193  .disassemble = 0,
194  .validate = 1,
195  .compile_only = 0,
196  };
197 #endif
198 
200 
201  *opaque = NULL;
202 
203  if (!(glslc_shader = glslang_shader_create(&glslc_input)))
204  return AVERROR(ENOMEM);
205 
206  if (!glslang_shader_preprocess(glslc_shader, &glslc_input)) {
208  av_log(s, AV_LOG_ERROR, "Unable to preprocess shader: %s (%s)!\n",
209  glslang_shader_get_info_log(glslc_shader),
210  glslang_shader_get_info_debug_log(glslc_shader));
211  glslang_shader_delete(glslc_shader);
212  return AVERROR(EINVAL);
213  }
214 
215  if (!glslang_shader_parse(glslc_shader, &glslc_input)) {
217  av_log(s, AV_LOG_ERROR, "Unable to parse shader: %s (%s)!\n",
218  glslang_shader_get_info_log(glslc_shader),
219  glslang_shader_get_info_debug_log(glslc_shader));
220  glslang_shader_delete(glslc_shader);
221  return AVERROR(EINVAL);
222  }
223 
224  if (!(glslc_program = glslang_program_create())) {
225  glslang_shader_delete(glslc_shader);
226  return AVERROR(EINVAL);
227  }
228 
229  glslang_program_add_shader(glslc_program, glslc_shader);
230 
231  if (!glslang_program_link(glslc_program, GLSLANG_MSG_SPV_RULES_BIT |
232  GLSLANG_MSG_VULKAN_RULES_BIT)) {
234  av_log(s, AV_LOG_ERROR, "Unable to link shader: %s (%s)!\n",
235  glslang_program_get_info_log(glslc_program),
236  glslang_program_get_info_debug_log(glslc_program));
237  glslang_program_delete(glslc_program);
238  glslang_shader_delete(glslc_shader);
239  return AVERROR(EINVAL);
240  }
241 
242 #if ((GLSLANG_VERSION_MAJOR) >= 12)
243  glslang_program_SPIRV_generate_with_options(glslc_program, glslc_input.stage, &glslc_opts);
244 #else
245  glslang_program_SPIRV_generate(glslc_program, glslc_input.stage);
246 #endif
247 
248  messages = glslang_program_SPIRV_get_messages(glslc_program);
249  if (messages) {
251  av_log(s, AV_LOG_WARNING, "%s\n", messages);
252  } else {
254  }
255 
256  glslang_shader_delete(glslc_shader);
257 
258  *size = glslang_program_SPIRV_get_size(glslc_program) * sizeof(unsigned int);
259  *data = (void *)glslang_program_SPIRV_get_ptr(glslc_program);
260  *opaque = glslc_program;
261 
262  return 0;
263 }
264 
265 static void glslc_shader_free(FFVkSPIRVCompiler *ctx, void **opaque)
266 {
267  if (!opaque || !*opaque)
268  return;
269 
271  glslang_program_delete(*opaque);
272  *opaque = NULL;
273 }
274 
276 {
277  if (!ctx || !*ctx)
278  return;
279 
281  if (glslc_refcount && (--glslc_refcount == 0))
282  glslang_finalize_process();
284 
285  av_freep(ctx);
286 }
287 
289 {
290  FFVkSPIRVCompiler *ret = av_mallocz(sizeof(*ret));
291  if (!ret)
292  return NULL;
293 
294  ret->compile_shader = glslc_shader_compile;
295  ret->free_shader = glslc_shader_free;
296  ret->uninit = glslc_uninit;
297 
299  if (!glslc_refcount++) {
300  if (!glslang_initialize_process()) {
301  av_freep(&ret);
302  glslc_refcount--;
303  }
304  }
306 
307  return ret;
308 }
pthread_mutex_t
_fmutex pthread_mutex_t
Definition: os2threads.h:53
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
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
FFVulkanShader::src
AVBPrint src
Definition: vulkan.h:189
data
const char data[16]
Definition: mxf.c:149
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
glslc_shader_compile
static int glslc_shader_compile(FFVulkanContext *s, FFVkSPIRVCompiler *ctx, FFVulkanShader *shd, uint8_t **data, size_t *size, const char *entrypoint, void **opaque)
Definition: vulkan_glslang.c:139
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
FFVulkanShader::stage
VkPipelineStageFlags stage
Definition: vulkan.h:195
ctx
AVFormatContext * ctx
Definition: movenc.c:49
NULL
#define NULL
Definition: coverity.c:32
ff_vk_glslang_init
FFVkSPIRVCompiler * ff_vk_glslang_init(void)
Definition: vulkan_glslang.c:288
FFVulkanContext
Definition: vulkan.h:268
pthread_mutex_unlock
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:82
size
int size
Definition: twinvq_data.h:10344
glslc_uninit
static void glslc_uninit(FFVkSPIRVCompiler **ctx)
Definition: vulkan_glslang.c:275
glslc_shader_free
static void glslc_shader_free(FFVkSPIRVCompiler *ctx, void **opaque)
Definition: vulkan_glslang.c:265
FFVulkanShader
Definition: vulkan.h:184
FFVkSPIRVCompiler
Definition: vulkan_spirv.h:26
FF_VK_EXT_RELAXED_EXTENDED_INSTR
#define FF_VK_EXT_RELAXED_EXTENDED_INSTR
Definition: vulkan_functions.h:50
glslc_refcount
static int glslc_refcount
Definition: vulkan_glslang.c:29
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:256
glslc_resource_limits
static const glslang_resource_t glslc_resource_limits
Definition: vulkan_glslang.c:31
vulkan_spirv.h
ret
ret
Definition: filter_design.txt:187
mem.h
ff_vk_shader_print
void ff_vk_shader_print(void *ctx, FFVulkanShader *shd, int prio)
Output the shader code as logging data, with a specific priority.
Definition: vulkan.c:1802
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
FF_VK_EXT_DEBUG_UTILS
#define FF_VK_EXT_DEBUG_UTILS
Definition: vulkan_functions.h:36
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
PTHREAD_MUTEX_INITIALIZER
#define PTHREAD_MUTEX_INITIALIZER
Definition: os2threads.h:56
glslc_mutex
static pthread_mutex_t glslc_mutex
Definition: vulkan_glslang.c:28
pthread_mutex_lock
#define pthread_mutex_lock(a)
Definition: ffprobe.c:78