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