FFmpeg
glslang.cpp
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 extern "C" {
22 #include "libavutil/mem.h"
23 #include "libavutil/avassert.h"
24 }
25 
26 #include <glslang/Include/ResourceLimits.h>
27 #include <glslang/Public/ShaderLang.h>
28 #include <glslang/SPIRV/GlslangToSpv.h>
29 
30 #include "glslang.h"
31 
32 using namespace glslang;
33 
35 static int glslang_refcount = 0;
36 
37 /* We require Vulkan 1.1 */
38 #define GLSL_VERSION EShTargetVulkan_1_1
39 
40 /* Vulkan 1.1 implementations require SPIR-V 1.3 to be implemented */
41 #define SPIRV_VERSION EShTargetSpv_1_3
42 
43 // Taken from glslang's examples, which apparently generally bases the choices
44 // on OpenGL specification limits
45 static const TBuiltInResource DefaultTBuiltInResource = {
46  /* .MaxLights = */ 32,
47  /* .MaxClipPlanes = */ 6,
48  /* .MaxTextureUnits = */ 32,
49  /* .MaxTextureCoords = */ 32,
50  /* .MaxVertexAttribs = */ 64,
51  /* .MaxVertexUniformComponents = */ 4096,
52  /* .MaxVaryingFloats = */ 64,
53  /* .MaxVertexTextureImageUnits = */ 32,
54  /* .MaxCombinedTextureImageUnits = */ 80,
55  /* .MaxTextureImageUnits = */ 32,
56  /* .MaxFragmentUniformComponents = */ 4096,
57  /* .MaxDrawBuffers = */ 32,
58  /* .MaxVertexUniformVectors = */ 128,
59  /* .MaxVaryingVectors = */ 8,
60  /* .MaxFragmentUniformVectors = */ 16,
61  /* .MaxVertexOutputVectors = */ 16,
62  /* .MaxFragmentInputVectors = */ 15,
63  /* .MinProgramTexelOffset = */ -8,
64  /* .MaxProgramTexelOffset = */ 7,
65  /* .MaxClipDistances = */ 8,
66  /* .MaxComputeWorkGroupCountX = */ 65535,
67  /* .MaxComputeWorkGroupCountY = */ 65535,
68  /* .MaxComputeWorkGroupCountZ = */ 65535,
69  /* .MaxComputeWorkGroupSizeX = */ 1024,
70  /* .MaxComputeWorkGroupSizeY = */ 1024,
71  /* .MaxComputeWorkGroupSizeZ = */ 64,
72  /* .MaxComputeUniformComponents = */ 1024,
73  /* .MaxComputeTextureImageUnits = */ 16,
74  /* .MaxComputeImageUniforms = */ 8,
75  /* .MaxComputeAtomicCounters = */ 8,
76  /* .MaxComputeAtomicCounterBuffers = */ 1,
77  /* .MaxVaryingComponents = */ 60,
78  /* .MaxVertexOutputComponents = */ 64,
79  /* .MaxGeometryInputComponents = */ 64,
80  /* .MaxGeometryOutputComponents = */ 128,
81  /* .MaxFragmentInputComponents = */ 128,
82  /* .MaxImageUnits = */ 8,
83  /* .MaxCombinedImageUnitsAndFragmentOutputs = */ 8,
84  /* .MaxCombinedShaderOutputResources = */ 8,
85  /* .MaxImageSamples = */ 0,
86  /* .MaxVertexImageUniforms = */ 0,
87  /* .MaxTessControlImageUniforms = */ 0,
88  /* .MaxTessEvaluationImageUniforms = */ 0,
89  /* .MaxGeometryImageUniforms = */ 0,
90  /* .MaxFragmentImageUniforms = */ 8,
91  /* .MaxCombinedImageUniforms = */ 8,
92  /* .MaxGeometryTextureImageUnits = */ 16,
93  /* .MaxGeometryOutputVertices = */ 256,
94  /* .MaxGeometryTotalOutputComponents = */ 1024,
95  /* .MaxGeometryUniformComponents = */ 1024,
96  /* .MaxGeometryVaryingComponents = */ 64,
97  /* .MaxTessControlInputComponents = */ 128,
98  /* .MaxTessControlOutputComponents = */ 128,
99  /* .MaxTessControlTextureImageUnits = */ 16,
100  /* .MaxTessControlUniformComponents = */ 1024,
101  /* .MaxTessControlTotalOutputComponents = */ 4096,
102  /* .MaxTessEvaluationInputComponents = */ 128,
103  /* .MaxTessEvaluationOutputComponents = */ 128,
104  /* .MaxTessEvaluationTextureImageUnits = */ 16,
105  /* .MaxTessEvaluationUniformComponents = */ 1024,
106  /* .MaxTessPatchComponents = */ 120,
107  /* .MaxPatchVertices = */ 32,
108  /* .MaxTessGenLevel = */ 64,
109  /* .MaxViewports = */ 16,
110  /* .MaxVertexAtomicCounters = */ 0,
111  /* .MaxTessControlAtomicCounters = */ 0,
112  /* .MaxTessEvaluationAtomicCounters = */ 0,
113  /* .MaxGeometryAtomicCounters = */ 0,
114  /* .MaxFragmentAtomicCounters = */ 8,
115  /* .MaxCombinedAtomicCounters = */ 8,
116  /* .MaxAtomicCounterBindings = */ 1,
117  /* .MaxVertexAtomicCounterBuffers = */ 0,
118  /* .MaxTessControlAtomicCounterBuffers = */ 0,
119  /* .MaxTessEvaluationAtomicCounterBuffers = */ 0,
120  /* .MaxGeometryAtomicCounterBuffers = */ 0,
121  /* .MaxFragmentAtomicCounterBuffers = */ 1,
122  /* .MaxCombinedAtomicCounterBuffers = */ 1,
123  /* .MaxAtomicCounterBufferSize = */ 16384,
124  /* .MaxTransformFeedbackBuffers = */ 4,
125  /* .MaxTransformFeedbackInterleavedComponents = */ 64,
126  /* .MaxCullDistances = */ 8,
127  /* .MaxCombinedClipAndCullDistances = */ 8,
128  /* .MaxSamples = */ 4,
129  /* .maxMeshOutputVerticesNV = */ 256,
130  /* .maxMeshOutputPrimitivesNV = */ 512,
131  /* .maxMeshWorkGroupSizeX_NV = */ 32,
132  /* .maxMeshWorkGroupSizeY_NV = */ 1,
133  /* .maxMeshWorkGroupSizeZ_NV = */ 1,
134  /* .maxTaskWorkGroupSizeX_NV = */ 32,
135  /* .maxTaskWorkGroupSizeY_NV = */ 1,
136  /* .maxTaskWorkGroupSizeZ_NV = */ 1,
137  /* .maxMeshViewCountNV = */ 4,
138 
139  .limits = {
140  /* .nonInductiveForLoops = */ 1,
141  /* .whileLoops = */ 1,
142  /* .doWhileLoops = */ 1,
143  /* .generalUniformIndexing = */ 1,
144  /* .generalAttributeMatrixVectorIndexing = */ 1,
145  /* .generalVaryingIndexing = */ 1,
146  /* .generalSamplerIndexing = */ 1,
147  /* .generalVariableIndexing = */ 1,
148  /* .generalConstantMatrixVectorIndexing = */ 1,
149  }
150 };
151 
152 GLSlangResult *glslang_compile(const char *glsl, enum GLSlangStage stage)
153 {
154  GLSlangResult *res = (GLSlangResult *)av_mallocz(sizeof(*res));
155  if (!res)
156  return NULL;
157 
158  static const EShLanguage lang[] = {
159  [GLSLANG_VERTEX] = EShLangVertex,
160  [GLSLANG_FRAGMENT] = EShLangFragment,
161  [GLSLANG_COMPUTE] = EShLangCompute,
162  };
163 
164  assert(glslang_refcount);
165  TShader *shader = new TShader(lang[stage]);
166  if (!shader) {
167  res->rval = AVERROR(ENOMEM);
168  return res;
169  }
170 
171  shader->setEnvClient(EShClientVulkan, GLSL_VERSION);
172  shader->setEnvTarget(EShTargetSpv, SPIRV_VERSION);
173  shader->setStrings(&glsl, 1);
174  if (!shader->parse(&DefaultTBuiltInResource, GLSL_VERSION, true, EShMsgDefault)) {
175  res->error_msg = av_strdup(shader->getInfoLog());
176  res->rval = AVERROR_EXTERNAL;
177  delete shader;
178  return res;
179  }
180 
181  TProgram *prog = new TProgram();
182  if (!prog) {
183  res->rval = AVERROR(ENOMEM);
184  delete shader;
185  return res;
186  }
187 
188  prog->addShader(shader);
189  if (!prog->link(EShMsgDefault)) {
190  res->error_msg = av_strdup(prog->getInfoLog());
191  res->rval = AVERROR_EXTERNAL;
192  delete shader;
193  delete prog;
194  return res;
195  }
196 
197  std::vector<unsigned int> spirv; /* Result */
198 
199  SpvOptions options; /* Options - by default all optimizations are off */
200  options.generateDebugInfo = false; /* Makes sense for files but not here */
201  options.disassemble = false; /* Will print disassembly on compilation */
202  options.validate = false; /* Validates the generated SPIRV, unneeded */
203  options.disableOptimizer = false; /* For debugging */
204  options.optimizeSize = true; /* Its faster */
205 
206  GlslangToSpv(*prog->getIntermediate(lang[stage]), spirv, NULL, &options);
207 
208  res->size = spirv.size()*sizeof(unsigned int);
209  res->data = av_memdup(spirv.data(), res->size);
210  if (!res->data) {
211  res->rval = AVERROR(ENOMEM);
212  delete shader;
213  delete prog;
214  return res;
215  }
216 
217  delete shader;
218  delete prog;
219 
220  return res;
221 }
222 
223 int glslang_init(void)
224 {
225  int ret = 0;
226 
228  if (glslang_refcount++ == 0)
229  ret = !InitializeProcess();
231 
232  return ret;
233 }
234 
235 void glslang_uninit(void)
236 {
239  if (--glslang_refcount == 0)
240  FinalizeProcess();
242 }
#define SPIRV_VERSION
Definition: glslang.cpp:41
#define NULL
Definition: coverity.c:32
#define pthread_mutex_lock(a)
Definition: ffprobe.c:62
Memory handling functions.
static const TBuiltInResource DefaultTBuiltInResource
Definition: glslang.cpp:45
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
#define GLSL_VERSION
Definition: glslang.cpp:38
size_t size
Definition: glslang.h:36
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int glslang_init(void)
Definition: glslang.cpp:223
simple assert() macros that are a bit more flexible than ISO C assert().
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:285
void glslang_uninit(void)
Definition: glslang.cpp:235
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:66
static pthread_mutex_t glslang_mutex
Definition: glslang.cpp:34
#define PTHREAD_MUTEX_INITIALIZER
Definition: os2threads.h:56
GLSlangResult * glslang_compile(const char *glsl, enum GLSlangStage stage)
Definition: glslang.cpp:152
GLuint shader
Definition: opengl_enc.c:115
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:253
static int glslang_refcount
Definition: glslang.cpp:35
void * data
Definition: glslang.h:35
GLSlangStage
Definition: glslang.h:39
int
const OptionDef options[]
Definition: ffmpeg_opt.c:3400
_fmutex pthread_mutex_t
Definition: os2threads.h:53
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
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
char * error_msg
Definition: glslang.h:33