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