• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2002 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 #ifndef GLSLANG_SHADERLANG_H_
7 #define GLSLANG_SHADERLANG_H_
8 
9 #include <stddef.h>
10 
11 #include "KHR/khrplatform.h"
12 
13 #include <array>
14 #include <map>
15 #include <set>
16 #include <string>
17 #include <vector>
18 
19 //
20 // This is the platform independent interface between an OGL driver
21 // and the shading language compiler.
22 //
23 
24 // Note: make sure to increment ANGLE_SH_VERSION when changing ShaderVars.h
25 #include "ShaderVars.h"
26 
27 // Version number for shader translation API.
28 // It is incremented every time the API changes.
29 #define ANGLE_SH_VERSION 212
30 
31 enum ShShaderSpec
32 {
33     SH_GLES2_SPEC,
34     SH_WEBGL_SPEC,
35 
36     SH_GLES3_SPEC,
37     SH_WEBGL2_SPEC,
38 
39     SH_GLES3_1_SPEC,
40     SH_WEBGL3_SPEC,
41 
42     SH_GL_CORE_SPEC,
43     SH_GL_COMPATIBILITY_SPEC,
44 };
45 
46 enum ShShaderOutput
47 {
48     // ESSL output only supported in some configurations.
49     SH_ESSL_OUTPUT = 0x8B45,
50 
51     // GLSL output only supported in some configurations.
52     SH_GLSL_COMPATIBILITY_OUTPUT = 0x8B46,
53     // Note: GL introduced core profiles in 1.5.
54     SH_GLSL_130_OUTPUT      = 0x8B47,
55     SH_GLSL_140_OUTPUT      = 0x8B80,
56     SH_GLSL_150_CORE_OUTPUT = 0x8B81,
57     SH_GLSL_330_CORE_OUTPUT = 0x8B82,
58     SH_GLSL_400_CORE_OUTPUT = 0x8B83,
59     SH_GLSL_410_CORE_OUTPUT = 0x8B84,
60     SH_GLSL_420_CORE_OUTPUT = 0x8B85,
61     SH_GLSL_430_CORE_OUTPUT = 0x8B86,
62     SH_GLSL_440_CORE_OUTPUT = 0x8B87,
63     SH_GLSL_450_CORE_OUTPUT = 0x8B88,
64 
65     // Prefer using these to specify HLSL output type:
66     SH_HLSL_3_0_OUTPUT       = 0x8B48,  // D3D 9
67     SH_HLSL_4_1_OUTPUT       = 0x8B49,  // D3D 11
68     SH_HLSL_4_0_FL9_3_OUTPUT = 0x8B4A,  // D3D 11 feature level 9_3
69 
70     // Output specialized GLSL to be fed to glslang for Vulkan SPIR.
71     SH_GLSL_VULKAN_OUTPUT = 0x8B4B,
72 };
73 
74 // Compile options.
75 // The Compile options type is defined in ShaderVars.h, to allow ANGLE to import the ShaderVars
76 // header without needing the ShaderLang header. This avoids some conflicts with glslang.
77 
78 const ShCompileOptions SH_VALIDATE               = 0;
79 const ShCompileOptions SH_VALIDATE_LOOP_INDEXING = UINT64_C(1) << 0;
80 const ShCompileOptions SH_INTERMEDIATE_TREE      = UINT64_C(1) << 1;
81 const ShCompileOptions SH_OBJECT_CODE            = UINT64_C(1) << 2;
82 const ShCompileOptions SH_VARIABLES              = UINT64_C(1) << 3;
83 const ShCompileOptions SH_LINE_DIRECTIVES        = UINT64_C(1) << 4;
84 const ShCompileOptions SH_SOURCE_PATH            = UINT64_C(1) << 5;
85 
86 // This flag will keep invariant declaration for input in fragment shader for GLSL >=4.20 on AMD.
87 // From GLSL >= 4.20, it's optional to add invariant for fragment input, but GPU vendors have
88 // different implementations about this. Some drivers forbid invariant in fragment for GLSL>= 4.20,
89 // e.g. Linux Mesa, some drivers treat that as optional, e.g. NVIDIA, some drivers require invariant
90 // must match between vertex and fragment shader, e.g. AMD. The behavior on AMD is obviously wrong.
91 // Remove invariant for input in fragment shader to workaround the restriction on Intel Mesa.
92 // But don't remove on AMD Linux to avoid triggering the bug on AMD.
93 const ShCompileOptions SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT = UINT64_C(1) << 6;
94 
95 // Due to spec difference between GLSL 4.1 or lower and ESSL3, some platforms (for example, Mac OSX
96 // core profile) require a variable's "invariant"/"centroid" qualifiers to match between vertex and
97 // fragment shader. A simple solution to allow such shaders to link is to omit the two qualifiers.
98 // AMD driver in Linux requires invariant qualifier to match between vertex and fragment shaders,
99 // while ESSL3 disallows invariant qualifier in fragment shader and GLSL >= 4.2 doesn't require
100 // invariant qualifier to match between shaders. Remove invariant qualifier from vertex shader to
101 // workaround AMD driver bug.
102 // Note that the two flags take effect on ESSL3 input shaders translated to GLSL 4.1 or lower and to
103 // GLSL 4.2 or newer on Linux AMD.
104 // TODO(zmo): This is not a good long-term solution. Simply dropping these qualifiers may break some
105 // developers' content. A more complex workaround of dynamically generating, compiling, and
106 // re-linking shaders that use these qualifiers should be implemented.
107 const ShCompileOptions SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3 = UINT64_C(1) << 7;
108 
109 // This flag works around bug in Intel Mac drivers related to abs(i) where
110 // i is an integer.
111 const ShCompileOptions SH_EMULATE_ABS_INT_FUNCTION = UINT64_C(1) << 8;
112 
113 // Enforce the GLSL 1.017 Appendix A section 7 packing restrictions.
114 // This flag only enforces (and can only enforce) the packing
115 // restrictions for uniform variables in both vertex and fragment
116 // shaders. ShCheckVariablesWithinPackingLimits() lets embedders
117 // enforce the packing restrictions for varying variables during
118 // program link time.
119 const ShCompileOptions SH_ENFORCE_PACKING_RESTRICTIONS = UINT64_C(1) << 9;
120 
121 // This flag ensures all indirect (expression-based) array indexing
122 // is clamped to the bounds of the array. This ensures, for example,
123 // that you cannot read off the end of a uniform, whether an array
124 // vec234, or mat234 type. The ShArrayIndexClampingStrategy enum,
125 // specified in the ShBuiltInResources when constructing the
126 // compiler, selects the strategy for the clamping implementation.
127 const ShCompileOptions SH_CLAMP_INDIRECT_ARRAY_BOUNDS = UINT64_C(1) << 10;
128 
129 // This flag limits the complexity of an expression.
130 const ShCompileOptions SH_LIMIT_EXPRESSION_COMPLEXITY = UINT64_C(1) << 11;
131 
132 // This flag limits the depth of the call stack.
133 const ShCompileOptions SH_LIMIT_CALL_STACK_DEPTH = UINT64_C(1) << 12;
134 
135 // This flag initializes gl_Position to vec4(0,0,0,0) at the
136 // beginning of the vertex shader's main(), and has no effect in the
137 // fragment shader. It is intended as a workaround for drivers which
138 // incorrectly fail to link programs if gl_Position is not written.
139 const ShCompileOptions SH_INIT_GL_POSITION = UINT64_C(1) << 13;
140 
141 // This flag replaces
142 //   "a && b" with "a ? b : false",
143 //   "a || b" with "a ? true : b".
144 // This is to work around a MacOSX driver bug that |b| is executed
145 // independent of |a|'s value.
146 const ShCompileOptions SH_UNFOLD_SHORT_CIRCUIT = UINT64_C(1) << 14;
147 
148 // This flag initializes output variables to 0 at the beginning of main().
149 // It is to avoid undefined behaviors.
150 const ShCompileOptions SH_INIT_OUTPUT_VARIABLES = UINT64_C(1) << 15;
151 
152 // This flag scalarizes vec/ivec/bvec/mat constructor args.
153 // It is intended as a workaround for Linux/Mac driver bugs.
154 const ShCompileOptions SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS = UINT64_C(1) << 16;
155 
156 // This flag overwrites a struct name with a unique prefix.
157 // It is intended as a workaround for drivers that do not handle
158 // struct scopes correctly, including all Mac drivers and Linux AMD.
159 const ShCompileOptions SH_REGENERATE_STRUCT_NAMES = UINT64_C(1) << 17;
160 
161 // This flag makes the compiler not prune unused function early in the
162 // compilation process. Pruning coupled with SH_LIMIT_CALL_STACK_DEPTH
163 // helps avoid bad shaders causing stack overflows.
164 const ShCompileOptions SH_DONT_PRUNE_UNUSED_FUNCTIONS = UINT64_C(1) << 18;
165 
166 // This flag works around a bug in NVIDIA 331 series drivers related
167 // to pow(x, y) where y is a constant vector.
168 const ShCompileOptions SH_REMOVE_POW_WITH_CONSTANT_EXPONENT = UINT64_C(1) << 19;
169 
170 // This flag works around bugs in Mac drivers related to do-while by
171 // transforming them into an other construct.
172 const ShCompileOptions SH_REWRITE_DO_WHILE_LOOPS = UINT64_C(1) << 20;
173 
174 // This flag works around a bug in the HLSL compiler optimizer that folds certain
175 // constant pow expressions incorrectly. Only applies to the HLSL back-end. It works
176 // by expanding the integer pow expressions into a series of multiplies.
177 const ShCompileOptions SH_EXPAND_SELECT_HLSL_INTEGER_POW_EXPRESSIONS = UINT64_C(1) << 21;
178 
179 // Flatten "#pragma STDGL invariant(all)" into the declarations of
180 // varying variables and built-in GLSL variables. This compiler
181 // option is enabled automatically when needed.
182 const ShCompileOptions SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL = UINT64_C(1) << 22;
183 
184 // Some drivers do not take into account the base level of the texture in the results of the
185 // HLSL GetDimensions builtin.  This flag instructs the compiler to manually add the base level
186 // offsetting.
187 const ShCompileOptions SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL = UINT64_C(1) << 23;
188 
189 // This flag works around an issue in translating GLSL function texelFetchOffset on
190 // INTEL drivers. It works by translating texelFetchOffset into texelFetch.
191 const ShCompileOptions SH_REWRITE_TEXELFETCHOFFSET_TO_TEXELFETCH = UINT64_C(1) << 24;
192 
193 // This flag works around condition bug of for and while loops in Intel Mac OSX drivers.
194 // Condition calculation is not correct. Rewrite it from "CONDITION" to "CONDITION && true".
195 const ShCompileOptions SH_ADD_AND_TRUE_TO_LOOP_CONDITION = UINT64_C(1) << 25;
196 
197 // This flag works around a bug in evaluating unary minus operator on integer on some INTEL
198 // drivers. It works by translating -(int) into ~(int) + 1.
199 const ShCompileOptions SH_REWRITE_INTEGER_UNARY_MINUS_OPERATOR = UINT64_C(1) << 26;
200 
201 // This flag works around a bug in evaluating isnan() on some INTEL D3D and Mac OSX drivers.
202 // It works by using an expression to emulate this function.
203 const ShCompileOptions SH_EMULATE_ISNAN_FLOAT_FUNCTION = UINT64_C(1) << 27;
204 
205 // This flag will use all uniforms of unused std140 and shared uniform blocks at the
206 // beginning of the vertex/fragment shader's main(). It is intended as a workaround for Mac
207 // drivers with shader version 4.10. In those drivers, they will treat unused
208 // std140 and shared uniform blocks' members as inactive. However, WebGL2.0 based on
209 // OpenGL ES3.0.4 requires all members of a named uniform block declared with a shared or std140
210 // layout qualifier to be considered active. The uniform block itself is also considered active.
211 const ShCompileOptions SH_USE_UNUSED_STANDARD_SHARED_BLOCKS = UINT64_C(1) << 28;
212 
213 // This flag works around a bug in unary minus operator on float numbers on Intel
214 // Mac OSX 10.11 drivers. It works by translating -float into 0.0 - float.
215 const ShCompileOptions SH_REWRITE_FLOAT_UNARY_MINUS_OPERATOR = UINT64_C(1) << 29;
216 
217 // This flag works around a bug in evaluating atan(y, x) on some NVIDIA OpenGL drivers.
218 // It works by using an expression to emulate this function.
219 const ShCompileOptions SH_EMULATE_ATAN2_FLOAT_FUNCTION = UINT64_C(1) << 30;
220 
221 // Set to initialize uninitialized local and global temporary variables. Should only be used with
222 // GLSL output. In HLSL output variables are initialized regardless of if this flag is set.
223 const ShCompileOptions SH_INITIALIZE_UNINITIALIZED_LOCALS = UINT64_C(1) << 31;
224 
225 // The flag modifies the shader in the following way:
226 // Every occurrence of gl_InstanceID is replaced by the global temporary variable InstanceID.
227 // Every occurrence of gl_ViewID_OVR is replaced by the varying variable ViewID_OVR.
228 // At the beginning of the body of main() in a vertex shader the following initializers are added:
229 // ViewID_OVR = uint(gl_InstanceID) % num_views;
230 // InstanceID = gl_InstanceID / num_views;
231 // ViewID_OVR is added as a varying variable to both the vertex and fragment shaders.
232 const ShCompileOptions SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW = UINT64_C(1) << 32;
233 
234 // With the flag enabled the GLSL/ESSL vertex shader is modified to include code for viewport
235 // selection in the following way:
236 // - Code to enable the extension ARB_shader_viewport_layer_array/NV_viewport_array2 is included.
237 // - Code to select the viewport index or layer is inserted at the beginning of main after
238 // ViewID_OVR's initialization.
239 // - A declaration of the uniform multiviewBaseViewLayerIndex.
240 // Note: The SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW flag also has to be enabled to have the
241 // temporary variable ViewID_OVR declared and initialized.
242 const ShCompileOptions SH_SELECT_VIEW_IN_NV_GLSL_VERTEX_SHADER = UINT64_C(1) << 33;
243 
244 // If the flag is enabled, gl_PointSize is clamped to the maximum point size specified in
245 // ShBuiltInResources in vertex shaders.
246 const ShCompileOptions SH_CLAMP_POINT_SIZE = UINT64_C(1) << 34;
247 
248 // Turn some arithmetic operations that operate on a float vector-scalar pair into vector-vector
249 // operations. This is done recursively. Some scalar binary operations inside vector constructors
250 // are also turned into vector operations.
251 //
252 // This is targeted to work around a bug in NVIDIA OpenGL drivers that was reproducible on NVIDIA
253 // driver version 387.92. It works around the most common occurrences of the bug.
254 const ShCompileOptions SH_REWRITE_VECTOR_SCALAR_ARITHMETIC = UINT64_C(1) << 35;
255 
256 // Don't use loops to initialize uninitialized variables. Only has an effect if some kind of
257 // variable initialization is turned on.
258 const ShCompileOptions SH_DONT_USE_LOOPS_TO_INITIALIZE_VARIABLES = UINT64_C(1) << 36;
259 
260 // Don't use D3D constant register zero when allocating space for uniforms. This is targeted to work
261 // around a bug in NVIDIA D3D driver version 388.59 where in very specific cases the driver would
262 // not handle constant register zero correctly. Only has an effect on HLSL translation.
263 const ShCompileOptions SH_SKIP_D3D_CONSTANT_REGISTER_ZERO = UINT64_C(1) << 37;
264 
265 // Clamp gl_FragDepth to the range [0.0, 1.0] in case it is statically used.
266 const ShCompileOptions SH_CLAMP_FRAG_DEPTH = UINT64_C(1) << 38;
267 
268 // Rewrite expressions like "v.x = z = expression;". Works around a bug in NVIDIA OpenGL drivers
269 // prior to version 397.31.
270 const ShCompileOptions SH_REWRITE_REPEATED_ASSIGN_TO_SWIZZLED = UINT64_C(1) << 39;
271 
272 // Rewrite gl_DrawID as a uniform int
273 const ShCompileOptions SH_EMULATE_GL_DRAW_ID = UINT64_C(1) << 40;
274 
275 // This flag initializes shared variables to 0.
276 // It is to avoid ompute shaders being able to read undefined values that could be coming from
277 // another webpage/application.
278 const ShCompileOptions SH_INIT_SHARED_VARIABLES = UINT64_C(1) << 41;
279 
280 // Forces the value returned from an atomic operations to be always be resolved. This is targeted to
281 // workaround a bug in NVIDIA D3D driver where the return value from
282 // RWByteAddressBuffer.InterlockedAdd does not get resolved when used in the .yzw components of a
283 // RWByteAddressBuffer.Store operation. Only has an effect on HLSL translation.
284 // http://anglebug.com/3246
285 const ShCompileOptions SH_FORCE_ATOMIC_VALUE_RESOLUTION = UINT64_C(1) << 42;
286 
287 // Rewrite gl_BaseVertex and gl_BaseInstance as uniform int
288 const ShCompileOptions SH_EMULATE_GL_BASE_VERTEX_BASE_INSTANCE = UINT64_C(1) << 43;
289 
290 // Emulate seamful cube map sampling for OpenGL ES2.0.  Currently only applies to the Vulkan
291 // backend, as is done after samplers are moved out of structs.  Can likely be made to work on
292 // the other backends as well.
293 //
294 // There are two variations of this.  One using subgroup operations where available, and another
295 // that emulates those operations using dFdxFine and dFdyFine.  The latter is more universally
296 // available, but is buggy on Nvidia.
297 const ShCompileOptions SH_EMULATE_SEAMFUL_CUBE_MAP_SAMPLING = UINT64_C(1) << 44;
298 const ShCompileOptions SH_EMULATE_SEAMFUL_CUBE_MAP_SAMPLING_WITH_SUBGROUP_OP = UINT64_C(1) << 45;
299 
300 // Defines alternate strategies for implementing array index clamping.
301 enum ShArrayIndexClampingStrategy
302 {
303     // Use the clamp intrinsic for array index clamping.
304     SH_CLAMP_WITH_CLAMP_INTRINSIC = 1,
305 
306     // Use a user-defined function for array index clamping.
307     SH_CLAMP_WITH_USER_DEFINED_INT_CLAMP_FUNCTION
308 };
309 
310 // The 64 bits hash function. The first parameter is the input string; the
311 // second parameter is the string length.
312 using ShHashFunction64 = khronos_uint64_t (*)(const char *, size_t);
313 
314 //
315 // Implementation dependent built-in resources (constants and extensions).
316 // The names for these resources has been obtained by stripping gl_/GL_.
317 //
318 struct ShBuiltInResources
319 {
320     // Constants.
321     int MaxVertexAttribs;
322     int MaxVertexUniformVectors;
323     int MaxVaryingVectors;
324     int MaxVertexTextureImageUnits;
325     int MaxCombinedTextureImageUnits;
326     int MaxTextureImageUnits;
327     int MaxFragmentUniformVectors;
328     int MaxDrawBuffers;
329 
330     // Extensions.
331     // Set to 1 to enable the extension, else 0.
332     int OES_standard_derivatives;
333     int OES_EGL_image_external;
334     int OES_EGL_image_external_essl3;
335     int NV_EGL_stream_consumer_external;
336     int ARB_texture_rectangle;
337     int EXT_blend_func_extended;
338     int EXT_draw_buffers;
339     int EXT_frag_depth;
340     int EXT_shader_texture_lod;
341     int WEBGL_debug_shader_precision;
342     int EXT_shader_framebuffer_fetch;
343     int NV_shader_framebuffer_fetch;
344     int ARM_shader_framebuffer_fetch;
345     int OVR_multiview;
346     int OVR_multiview2;
347     int EXT_YUV_target;
348     int EXT_geometry_shader;
349     int OES_texture_storage_multisample_2d_array;
350     int OES_texture_3D;
351     int ANGLE_texture_multisample;
352     int ANGLE_multi_draw;
353     int ANGLE_base_vertex_base_instance;
354 
355     // Set to 1 to enable replacing GL_EXT_draw_buffers #extension directives
356     // with GL_NV_draw_buffers in ESSL output. This flag can be used to emulate
357     // EXT_draw_buffers by using it in combination with GLES3.0 glDrawBuffers
358     // function. This applies to Tegra K1 devices.
359     int NV_draw_buffers;
360 
361     // Set to 1 if highp precision is supported in the ESSL 1.00 version of the
362     // fragment language. Does not affect versions of the language where highp
363     // support is mandatory.
364     // Default is 0.
365     int FragmentPrecisionHigh;
366 
367     // GLSL ES 3.0 constants.
368     int MaxVertexOutputVectors;
369     int MaxFragmentInputVectors;
370     int MinProgramTexelOffset;
371     int MaxProgramTexelOffset;
372 
373     // Extension constants.
374 
375     // Value of GL_MAX_DUAL_SOURCE_DRAW_BUFFERS_EXT for OpenGL ES output context.
376     // Value of GL_MAX_DUAL_SOURCE_DRAW_BUFFERS for OpenGL output context.
377     // GLES SL version 100 gl_MaxDualSourceDrawBuffersEXT value for EXT_blend_func_extended.
378     int MaxDualSourceDrawBuffers;
379 
380     // Value of GL_MAX_VIEWS_OVR.
381     int MaxViewsOVR;
382 
383     // Name Hashing.
384     // Set a 64 bit hash function to enable user-defined name hashing.
385     // Default is NULL.
386     ShHashFunction64 HashFunction;
387 
388     // Selects a strategy to use when implementing array index clamping.
389     // Default is SH_CLAMP_WITH_CLAMP_INTRINSIC.
390     ShArrayIndexClampingStrategy ArrayIndexClampingStrategy;
391 
392     // The maximum complexity an expression can be when SH_LIMIT_EXPRESSION_COMPLEXITY is turned on.
393     int MaxExpressionComplexity;
394 
395     // The maximum depth a call stack can be.
396     int MaxCallStackDepth;
397 
398     // The maximum number of parameters a function can have when SH_LIMIT_EXPRESSION_COMPLEXITY is
399     // turned on.
400     int MaxFunctionParameters;
401 
402     // GLES 3.1 constants
403 
404     // texture gather offset constraints.
405     int MinProgramTextureGatherOffset;
406     int MaxProgramTextureGatherOffset;
407 
408     // maximum number of available image units
409     int MaxImageUnits;
410 
411     // maximum number of image uniforms in a vertex shader
412     int MaxVertexImageUniforms;
413 
414     // maximum number of image uniforms in a fragment shader
415     int MaxFragmentImageUniforms;
416 
417     // maximum number of image uniforms in a compute shader
418     int MaxComputeImageUniforms;
419 
420     // maximum total number of image uniforms in a program
421     int MaxCombinedImageUniforms;
422 
423     // maximum number of uniform locations
424     int MaxUniformLocations;
425 
426     // maximum number of ssbos and images in a shader
427     int MaxCombinedShaderOutputResources;
428 
429     // maximum number of groups in each dimension
430     std::array<int, 3> MaxComputeWorkGroupCount;
431     // maximum number of threads per work group in each dimension
432     std::array<int, 3> MaxComputeWorkGroupSize;
433 
434     // maximum number of total uniform components
435     int MaxComputeUniformComponents;
436 
437     // maximum number of texture image units in a compute shader
438     int MaxComputeTextureImageUnits;
439 
440     // maximum number of atomic counters in a compute shader
441     int MaxComputeAtomicCounters;
442 
443     // maximum number of atomic counter buffers in a compute shader
444     int MaxComputeAtomicCounterBuffers;
445 
446     // maximum number of atomic counters in a vertex shader
447     int MaxVertexAtomicCounters;
448 
449     // maximum number of atomic counters in a fragment shader
450     int MaxFragmentAtomicCounters;
451 
452     // maximum number of atomic counters in a program
453     int MaxCombinedAtomicCounters;
454 
455     // maximum binding for an atomic counter
456     int MaxAtomicCounterBindings;
457 
458     // maximum number of atomic counter buffers in a vertex shader
459     int MaxVertexAtomicCounterBuffers;
460 
461     // maximum number of atomic counter buffers in a fragment shader
462     int MaxFragmentAtomicCounterBuffers;
463 
464     // maximum number of atomic counter buffers in a program
465     int MaxCombinedAtomicCounterBuffers;
466 
467     // maximum number of buffer object storage in machine units
468     int MaxAtomicCounterBufferSize;
469 
470     // maximum number of uniform block bindings
471     int MaxUniformBufferBindings;
472 
473     // maximum number of shader storage buffer bindings
474     int MaxShaderStorageBufferBindings;
475 
476     // maximum point size (higher limit from ALIASED_POINT_SIZE_RANGE)
477     float MaxPointSize;
478 
479     // EXT_geometry_shader constants
480     int MaxGeometryUniformComponents;
481     int MaxGeometryUniformBlocks;
482     int MaxGeometryInputComponents;
483     int MaxGeometryOutputComponents;
484     int MaxGeometryOutputVertices;
485     int MaxGeometryTotalOutputComponents;
486     int MaxGeometryTextureImageUnits;
487     int MaxGeometryAtomicCounterBuffers;
488     int MaxGeometryAtomicCounters;
489     int MaxGeometryShaderStorageBlocks;
490     int MaxGeometryShaderInvocations;
491     int MaxGeometryImageUniforms;
492 };
493 
494 //
495 // ShHandle held by but opaque to the driver.  It is allocated,
496 // managed, and de-allocated by the compiler. Its contents
497 // are defined by and used by the compiler.
498 //
499 // If handle creation fails, 0 will be returned.
500 //
501 using ShHandle = void *;
502 
503 namespace sh
504 {
505 
506 //
507 // Driver must call this first, once, before doing any other compiler operations.
508 // If the function succeeds, the return value is true, else false.
509 //
510 bool Initialize();
511 //
512 // Driver should call this at shutdown.
513 // If the function succeeds, the return value is true, else false.
514 //
515 bool Finalize();
516 
517 //
518 // Initialize built-in resources with minimum expected values.
519 // Parameters:
520 // resources: The object to initialize. Will be comparable with memcmp.
521 //
522 void InitBuiltInResources(ShBuiltInResources *resources);
523 
524 //
525 // Returns the a concatenated list of the items in ShBuiltInResources as a null-terminated string.
526 // This function must be updated whenever ShBuiltInResources is changed.
527 // Parameters:
528 // handle: Specifies the handle of the compiler to be used.
529 const std::string &GetBuiltInResourcesString(const ShHandle handle);
530 
531 //
532 // Driver calls these to create and destroy compiler objects.
533 //
534 // Returns the handle of constructed compiler, null if the requested compiler is not supported.
535 // Parameters:
536 // type: Specifies the type of shader - GL_FRAGMENT_SHADER or GL_VERTEX_SHADER.
537 // spec: Specifies the language spec the compiler must conform to - SH_GLES2_SPEC or SH_WEBGL_SPEC.
538 // output: Specifies the output code type - for example SH_ESSL_OUTPUT, SH_GLSL_OUTPUT,
539 //         SH_HLSL_3_0_OUTPUT or SH_HLSL_4_1_OUTPUT. Note: Each output type may only
540 //         be supported in some configurations.
541 // resources: Specifies the built-in resources.
542 ShHandle ConstructCompiler(sh::GLenum type,
543                            ShShaderSpec spec,
544                            ShShaderOutput output,
545                            const ShBuiltInResources *resources);
546 void Destruct(ShHandle handle);
547 
548 //
549 // Compiles the given shader source.
550 // If the function succeeds, the return value is true, else false.
551 // Parameters:
552 // handle: Specifies the handle of compiler to be used.
553 // shaderStrings: Specifies an array of pointers to null-terminated strings containing the shader
554 // source code.
555 // numStrings: Specifies the number of elements in shaderStrings array.
556 // compileOptions: A mask containing the following parameters:
557 // SH_VALIDATE: Validates shader to ensure that it conforms to the spec
558 //              specified during compiler construction.
559 // SH_VALIDATE_LOOP_INDEXING: Validates loop and indexing in the shader to
560 //                            ensure that they do not exceed the minimum
561 //                            functionality mandated in GLSL 1.0 spec,
562 //                            Appendix A, Section 4 and 5.
563 //                            There is no need to specify this parameter when
564 //                            compiling for WebGL - it is implied.
565 // SH_INTERMEDIATE_TREE: Writes intermediate tree to info log.
566 //                       Can be queried by calling sh::GetInfoLog().
567 // SH_OBJECT_CODE: Translates intermediate tree to glsl or hlsl shader.
568 //                 Can be queried by calling sh::GetObjectCode().
569 // SH_VARIABLES: Extracts attributes, uniforms, and varyings.
570 //               Can be queried by calling ShGetVariableInfo().
571 //
572 bool Compile(const ShHandle handle,
573              const char *const shaderStrings[],
574              size_t numStrings,
575              ShCompileOptions compileOptions);
576 
577 // Clears the results from the previous compilation.
578 void ClearResults(const ShHandle handle);
579 
580 // Return the version of the shader language.
581 int GetShaderVersion(const ShHandle handle);
582 
583 // Return the currently set language output type.
584 ShShaderOutput GetShaderOutputType(const ShHandle handle);
585 
586 // Returns null-terminated information log for a compiled shader.
587 // Parameters:
588 // handle: Specifies the compiler
589 const std::string &GetInfoLog(const ShHandle handle);
590 
591 // Returns null-terminated object code for a compiled shader.
592 // Parameters:
593 // handle: Specifies the compiler
594 const std::string &GetObjectCode(const ShHandle handle);
595 
596 // Returns a (original_name, hash) map containing all the user defined names in the shader,
597 // including variable names, function names, struct names, and struct field names.
598 // Parameters:
599 // handle: Specifies the compiler
600 const std::map<std::string, std::string> *GetNameHashingMap(const ShHandle handle);
601 
602 // Shader variable inspection.
603 // Returns a pointer to a list of variables of the designated type.
604 // (See ShaderVars.h for type definitions, included above)
605 // Returns NULL on failure.
606 // Parameters:
607 // handle: Specifies the compiler
608 const std::vector<sh::Uniform> *GetUniforms(const ShHandle handle);
609 const std::vector<sh::Varying> *GetVaryings(const ShHandle handle);
610 const std::vector<sh::Varying> *GetInputVaryings(const ShHandle handle);
611 const std::vector<sh::Varying> *GetOutputVaryings(const ShHandle handle);
612 const std::vector<sh::Attribute> *GetAttributes(const ShHandle handle);
613 const std::vector<sh::OutputVariable> *GetOutputVariables(const ShHandle handle);
614 const std::vector<sh::InterfaceBlock> *GetInterfaceBlocks(const ShHandle handle);
615 const std::vector<sh::InterfaceBlock> *GetUniformBlocks(const ShHandle handle);
616 const std::vector<sh::InterfaceBlock> *GetShaderStorageBlocks(const ShHandle handle);
617 sh::WorkGroupSize GetComputeShaderLocalGroupSize(const ShHandle handle);
618 // Returns the number of views specified through the num_views layout qualifier. If num_views is
619 // not set, the function returns -1.
620 int GetVertexShaderNumViews(const ShHandle handle);
621 
622 // Returns true if the passed in variables pack in maxVectors followingthe packing rules from the
623 // GLSL 1.017 spec, Appendix A, section 7.
624 // Returns false otherwise. Also look at the SH_ENFORCE_PACKING_RESTRICTIONS
625 // flag above.
626 // Parameters:
627 // maxVectors: the available rows of registers.
628 // variables: an array of variables.
629 bool CheckVariablesWithinPackingLimits(int maxVectors,
630                                        const std::vector<sh::ShaderVariable> &variables);
631 
632 // Gives the compiler-assigned register for a shader storage block.
633 // The method writes the value to the output variable "indexOut".
634 // Returns true if it found a valid shader storage block, false otherwise.
635 // Parameters:
636 // handle: Specifies the compiler
637 // shaderStorageBlockName: Specifies the shader storage block
638 // indexOut: output variable that stores the assigned register
639 bool GetShaderStorageBlockRegister(const ShHandle handle,
640                                    const std::string &shaderStorageBlockName,
641                                    unsigned int *indexOut);
642 
643 // Gives the compiler-assigned register for a uniform block.
644 // The method writes the value to the output variable "indexOut".
645 // Returns true if it found a valid uniform block, false otherwise.
646 // Parameters:
647 // handle: Specifies the compiler
648 // uniformBlockName: Specifies the uniform block
649 // indexOut: output variable that stores the assigned register
650 bool GetUniformBlockRegister(const ShHandle handle,
651                              const std::string &uniformBlockName,
652                              unsigned int *indexOut);
653 
654 // Gives a map from uniform names to compiler-assigned registers in the default uniform block.
655 // Note that the map contains also registers of samplers that have been extracted from structs.
656 const std::map<std::string, unsigned int> *GetUniformRegisterMap(const ShHandle handle);
657 
658 // Sampler, image and atomic counters share registers(t type and u type),
659 // GetReadonlyImage2DRegisterIndex and GetImage2DRegisterIndex return the first index into
660 // a range of reserved registers for image2D/iimage2D/uimage2D variables.
661 // Parameters: handle: Specifies the compiler
662 unsigned int GetReadonlyImage2DRegisterIndex(const ShHandle handle);
663 unsigned int GetImage2DRegisterIndex(const ShHandle handle);
664 
665 // The method records these used function names related with image2D/iimage2D/uimage2D, these
666 // functions will be dynamically generated.
667 // Parameters:
668 // handle: Specifies the compiler
669 const std::set<std::string> *GetUsedImage2DFunctionNames(const ShHandle handle);
670 
671 bool HasValidGeometryShaderInputPrimitiveType(const ShHandle handle);
672 bool HasValidGeometryShaderOutputPrimitiveType(const ShHandle handle);
673 bool HasValidGeometryShaderMaxVertices(const ShHandle handle);
674 GLenum GetGeometryShaderInputPrimitiveType(const ShHandle handle);
675 GLenum GetGeometryShaderOutputPrimitiveType(const ShHandle handle);
676 int GetGeometryShaderInvocations(const ShHandle handle);
677 int GetGeometryShaderMaxVertices(const ShHandle handle);
678 
679 //
680 // Helper function to identify specs that are based on the WebGL spec.
681 //
IsWebGLBasedSpec(ShShaderSpec spec)682 inline bool IsWebGLBasedSpec(ShShaderSpec spec)
683 {
684     return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
685 }
686 
687 //
688 // Helper function to identify DesktopGL specs
689 //
IsDesktopGLSpec(ShShaderSpec spec)690 inline bool IsDesktopGLSpec(ShShaderSpec spec)
691 {
692     return spec == SH_GL_CORE_SPEC || spec == SH_GL_COMPATIBILITY_SPEC;
693 }
694 }  // namespace sh
695 
696 #endif  // GLSLANG_SHADERLANG_H_
697