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 337
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_GLES3_2_SPEC,
43
44 SH_GL_CORE_SPEC,
45 SH_GL_COMPATIBILITY_SPEC,
46 };
47
48 enum ShShaderOutput
49 {
50 // ESSL output only supported in some configurations.
51 SH_ESSL_OUTPUT = 0x8B45,
52
53 // GLSL output only supported in some configurations.
54 SH_GLSL_COMPATIBILITY_OUTPUT = 0x8B46,
55 // Note: GL introduced core profiles in 1.5.
56 SH_GLSL_130_OUTPUT = 0x8B47,
57 SH_GLSL_140_OUTPUT = 0x8B80,
58 SH_GLSL_150_CORE_OUTPUT = 0x8B81,
59 SH_GLSL_330_CORE_OUTPUT = 0x8B82,
60 SH_GLSL_400_CORE_OUTPUT = 0x8B83,
61 SH_GLSL_410_CORE_OUTPUT = 0x8B84,
62 SH_GLSL_420_CORE_OUTPUT = 0x8B85,
63 SH_GLSL_430_CORE_OUTPUT = 0x8B86,
64 SH_GLSL_440_CORE_OUTPUT = 0x8B87,
65 SH_GLSL_450_CORE_OUTPUT = 0x8B88,
66
67 // Prefer using these to specify HLSL output type:
68 SH_HLSL_3_0_OUTPUT = 0x8B48, // D3D 9
69 SH_HLSL_4_1_OUTPUT = 0x8B49, // D3D 11
70 SH_HLSL_4_0_FL9_3_OUTPUT = 0x8B4A, // D3D 11 feature level 9_3
71
72 // Output SPIR-V for the Vulkan backend.
73 SH_SPIRV_VULKAN_OUTPUT = 0x8B4B,
74
75 // Output for MSL
76 SH_MSL_METAL_OUTPUT = 0x8B4D,
77 };
78
79 struct ShCompileOptionsMetal
80 {
81 // Direct-to-metal backend constants:
82
83 // Binding index for driver uniforms:
84 int driverUniformsBindingIndex;
85 // Binding index for default uniforms:
86 int defaultUniformsBindingIndex;
87 // Binding index for UBO's argument buffer
88 int UBOArgumentBufferBindingIndex;
89
90 bool generateShareableShaders;
91 };
92
93 // For ANGLE_shader_pixel_local_storage.
94 // Instructs the compiler which pixel local storage configuration to generate code for.
95 enum class ShPixelLocalStorageType : uint8_t
96 {
97 NotSupported,
98 ImageLoadStore,
99 FramebufferFetch,
100 PixelLocalStorageEXT, // GL_EXT_shader_pixel_local_storage.
101 };
102
103 // For ANGLE_shader_pixel_local_storage_coherent.
104 // Instructs the compiler which fragment synchronization method to use, if any.
105 enum class ShFragmentSynchronizationType : uint8_t
106 {
107 NotSupported, // Fragments cannot be ordered or synchronized.
108
109 Automatic, // Fragments are automatically raster-ordered and synchronized.
110
111 FragmentShaderInterlock_NV_GL,
112 FragmentShaderOrdering_INTEL_GL,
113 FragmentShaderInterlock_ARB_GL, // Also compiles to SPV_EXT_fragment_shader_interlock.
114
115 RasterizerOrderViews_D3D,
116
117 RasterOrderGroups_Metal,
118
119 InvalidEnum,
120 EnumCount = InvalidEnum,
121 };
122
123 struct ShPixelLocalStorageOptions
124 {
125 ShPixelLocalStorageType type = ShPixelLocalStorageType::NotSupported;
126
127 // For ANGLE_shader_pixel_local_storage_coherent.
128 ShFragmentSynchronizationType fragmentSyncType = ShFragmentSynchronizationType::NotSupported;
129
130 // ShPixelLocalStorageType::ImageLoadStore only: Can we use rgba8/rgba8i/rgba8ui image formats?
131 // Or do we need to manually pack and unpack from r32i/r32ui?
132 bool supportsNativeRGBA8ImageFormats = false;
133
134 // anglebug.com/7792 -- Metal [[raster_order_group()]] does not work for read_write textures on
135 // AMD when the render pass doesn't have a color attachment on slot 0. To work around this we
136 // attach one of the PLS textures to GL_COLOR_ATTACHMENT0, if there isn't one already.
137 bool renderPassNeedsAMDRasterOrderGroupsWorkaround = false;
138 };
139
140 struct ShCompileOptions
141 {
142 ShCompileOptions();
143 ShCompileOptions(const ShCompileOptions &other);
144 ShCompileOptions &operator=(const ShCompileOptions &other);
145
146 // Translates intermediate tree to glsl, hlsl, msl, or SPIR-V binary. Can be queried by
147 // calling sh::GetObjectCode().
148 uint64_t objectCode : 1;
149
150 // Whether debug info should be output in the shader.
151 uint64_t outputDebugInfo : 1;
152
153 // Tracks the source path for shaders. Can be queried with getSourcePath().
154 uint64_t sourcePath : 1;
155
156 // Whether the internal representation of the AST should be output.
157 uint64_t intermediateTree : 1;
158
159 // If requested, validates the AST after every transformation. Useful for debugging.
160 uint64_t validateAST : 1;
161
162 // Validates loop and indexing in the shader to ensure that they do not exceed the minimum
163 // functionality mandated in GLSL 1.0 spec, Appendix A, Section 4 and 5. There is no need to
164 // specify this parameter when compiling for WebGL - it is implied.
165 uint64_t validateLoopIndexing : 1;
166
167 // Emits #line directives in HLSL.
168 uint64_t lineDirectives : 1;
169
170 // Due to spec difference between GLSL 4.1 or lower and ESSL3, some platforms (for example, Mac
171 // OSX core profile) require a variable's "invariant"/"centroid" qualifiers to match between
172 // vertex and fragment shader. A simple solution to allow such shaders to link is to omit the
173 // two qualifiers. AMD driver in Linux requires invariant qualifier to match between vertex and
174 // fragment shaders, while ESSL3 disallows invariant qualifier in fragment shader and GLSL >=
175 // 4.2 doesn't require invariant qualifier to match between shaders. Remove invariant qualifier
176 // from vertex shader to workaround AMD driver bug.
177 // Note that the two flags take effect on ESSL3 input shaders translated to GLSL 4.1 or lower
178 // and to GLSL 4.2 or newer on Linux AMD.
179 // TODO(zmo): This is not a good long-term solution. Simply dropping these qualifiers may break
180 // some developers' content. A more complex workaround of dynamically generating, compiling, and
181 // re-linking shaders that use these qualifiers should be implemented.
182 uint64_t removeInvariantAndCentroidForESSL3 : 1;
183
184 // This flag works around bug in Intel Mac drivers related to abs(i) where i is an integer.
185 uint64_t emulateAbsIntFunction : 1;
186
187 // Enforce the GLSL 1.017 Appendix A section 7 packing restrictions. This flag only enforces
188 // (and can only enforce) the packing restrictions for uniform variables in both vertex and
189 // fragment shaders. ShCheckVariablesWithinPackingLimits() lets embedders enforce the packing
190 // restrictions for varying variables during program link time.
191 uint64_t enforcePackingRestrictions : 1;
192
193 // This flag ensures all indirect (expression-based) array indexing is clamped to the bounds of
194 // the array. This ensures, for example, that you cannot read off the end of a uniform, whether
195 // an array vec234, or mat234 type.
196 uint64_t clampIndirectArrayBounds : 1;
197
198 // This flag limits the complexity of an expression.
199 uint64_t limitExpressionComplexity : 1;
200
201 // This flag limits the depth of the call stack.
202 uint64_t limitCallStackDepth : 1;
203
204 // This flag initializes gl_Position to vec4(0,0,0,0) at the beginning of the vertex shader's
205 // main(), and has no effect in the fragment shader. It is intended as a workaround for drivers
206 // which incorrectly fail to link programs if gl_Position is not written.
207 uint64_t initGLPosition : 1;
208
209 // This flag replaces
210 // "a && b" with "a ? b : false",
211 // "a || b" with "a ? true : b".
212 // This is to work around a MacOSX driver bug that |b| is executed independent of |a|'s value.
213 uint64_t unfoldShortCircuit : 1;
214
215 // This flag initializes output variables to 0 at the beginning of main(). It is to avoid
216 // undefined behaviors.
217 uint64_t initOutputVariables : 1;
218
219 // This flag scalarizes vec/ivec/bvec/mat constructor args. It is intended as a workaround for
220 // Linux/Mac driver bugs.
221 uint64_t scalarizeVecAndMatConstructorArgs : 1;
222
223 // This flag overwrites a struct name with a unique prefix. It is intended as a workaround for
224 // drivers that do not handle struct scopes correctly, including all Mac drivers and Linux AMD.
225 uint64_t regenerateStructNames : 1;
226
227 // This flag works around bugs in Mac drivers related to do-while by transforming them into an
228 // other construct.
229 uint64_t rewriteDoWhileLoops : 1;
230
231 // This flag works around a bug in the HLSL compiler optimizer that folds certain constant pow
232 // expressions incorrectly. Only applies to the HLSL back-end. It works by expanding the integer
233 // pow expressions into a series of multiplies.
234 uint64_t expandSelectHLSLIntegerPowExpressions : 1;
235
236 // Flatten "#pragma STDGL invariant(all)" into the declarations of varying variables and
237 // built-in GLSL variables. This compiler option is enabled automatically when needed.
238 uint64_t flattenPragmaSTDGLInvariantAll : 1;
239
240 // Some drivers do not take into account the base level of the texture in the results of the
241 // HLSL GetDimensions builtin. This flag instructs the compiler to manually add the base level
242 // offsetting.
243 uint64_t HLSLGetDimensionsIgnoresBaseLevel : 1;
244
245 // This flag works around an issue in translating GLSL function texelFetchOffset on INTEL
246 // drivers. It works by translating texelFetchOffset into texelFetch.
247 uint64_t rewriteTexelFetchOffsetToTexelFetch : 1;
248
249 // This flag works around condition bug of for and while loops in Intel Mac OSX drivers.
250 // Condition calculation is not correct. Rewrite it from "CONDITION" to "CONDITION && true".
251 uint64_t addAndTrueToLoopCondition : 1;
252
253 // This flag works around a bug in evaluating unary minus operator on integer on some INTEL
254 // drivers. It works by translating -(int) into ~(int) + 1.
255 uint64_t rewriteIntegerUnaryMinusOperator : 1;
256
257 // This flag works around a bug in evaluating isnan() on some INTEL D3D and Mac OSX drivers. It
258 // works by using an expression to emulate this function.
259 uint64_t emulateIsnanFloatFunction : 1;
260
261 // This flag will use all uniforms of unused std140 and shared uniform blocks at the beginning
262 // of the vertex/fragment shader's main(). It is intended as a workaround for Mac drivers with
263 // shader version 4.10. In those drivers, they will treat unused std140 and shared uniform
264 // blocks' members as inactive. However, WebGL2.0 based on OpenGL ES3.0.4 requires all members
265 // of a named uniform block declared with a shared or std140 layout qualifier to be considered
266 // active. The uniform block itself is also considered active.
267 uint64_t useUnusedStandardSharedBlocks : 1;
268
269 // This flag works around a bug in unary minus operator on float numbers on Intel Mac OSX 10.11
270 // drivers. It works by translating -float into 0.0 - float.
271 uint64_t rewriteFloatUnaryMinusOperator : 1;
272
273 // This flag works around a bug in evaluating atan(y, x) on some NVIDIA OpenGL drivers. It
274 // works by using an expression to emulate this function.
275 uint64_t emulateAtan2FloatFunction : 1;
276
277 // Set to initialize uninitialized local and global temporary variables. Should only be used
278 // with GLSL output. In HLSL output variables are initialized regardless of if this flag is set.
279 uint64_t initializeUninitializedLocals : 1;
280
281 // The flag modifies the shader in the following way:
282 //
283 // Every occurrence of gl_InstanceID is replaced by the global temporary variable InstanceID.
284 // Every occurrence of gl_ViewID_OVR is replaced by the varying variable ViewID_OVR.
285 // At the beginning of the body of main() in a vertex shader the following initializers are
286 // added:
287 // ViewID_OVR = uint(gl_InstanceID) % num_views;
288 // InstanceID = gl_InstanceID / num_views;
289 // ViewID_OVR is added as a varying variable to both the vertex and fragment shaders.
290 uint64_t initializeBuiltinsForInstancedMultiview : 1;
291
292 // With the flag enabled the GLSL/ESSL vertex shader is modified to include code for viewport
293 // selection in the following way:
294 // - Code to enable the extension ARB_shader_viewport_layer_array/NV_viewport_array2 is
295 // included.
296 // - Code to select the viewport index or layer is inserted at the beginning of main after
297 // ViewID_OVR's initialization.
298 // - A declaration of the uniform multiviewBaseViewLayerIndex.
299 // Note: The initializeBuiltinsForInstancedMultiview flag also has to be enabled to have the
300 // temporary variable ViewID_OVR declared and initialized.
301 uint64_t selectViewInNvGLSLVertexShader : 1;
302
303 // If the flag is enabled, gl_PointSize is clamped to the maximum point size specified in
304 // ShBuiltInResources in vertex shaders.
305 uint64_t clampPointSize : 1;
306
307 // This flag indicates whether advanced blend equation should be emulated. Currently only
308 // implemented for the Vulkan backend.
309 uint64_t addAdvancedBlendEquationsEmulation : 1;
310
311 // Don't use loops to initialize uninitialized variables. Only has an effect if some kind of
312 // variable initialization is turned on.
313 uint64_t dontUseLoopsToInitializeVariables : 1;
314
315 // Don't use D3D constant register zero when allocating space for uniforms. This is targeted to
316 // work around a bug in NVIDIA D3D driver version 388.59 where in very specific cases the driver
317 // would not handle constant register zero correctly. Only has an effect on HLSL translation.
318 uint64_t skipD3DConstantRegisterZero : 1;
319
320 // Clamp gl_FragDepth to the range [0.0, 1.0] in case it is statically used.
321 uint64_t clampFragDepth : 1;
322
323 // Rewrite expressions like "v.x = z = expression;". Works around a bug in NVIDIA OpenGL drivers
324 // prior to version 397.31.
325 uint64_t rewriteRepeatedAssignToSwizzled : 1;
326
327 // Rewrite gl_DrawID as a uniform int
328 uint64_t emulateGLDrawID : 1;
329
330 // This flag initializes shared variables to 0. It is to avoid ompute shaders being able to
331 // read undefined values that could be coming from another webpage/application.
332 uint64_t initSharedVariables : 1;
333
334 // Forces the value returned from an atomic operations to be always be resolved. This is
335 // targeted to workaround a bug in NVIDIA D3D driver where the return value from
336 // RWByteAddressBuffer.InterlockedAdd does not get resolved when used in the .yzw components of
337 // a RWByteAddressBuffer.Store operation. Only has an effect on HLSL translation.
338 // http://anglebug.com/3246
339 uint64_t forceAtomicValueResolution : 1;
340
341 // Rewrite gl_BaseVertex and gl_BaseInstance as uniform int
342 uint64_t emulateGLBaseVertexBaseInstance : 1;
343
344 // Emulate seamful cube map sampling for OpenGL ES2.0. Currently only applies to the Vulkan
345 // backend, as is done after samplers are moved out of structs. Can likely be made to work on
346 // the other backends as well.
347 uint64_t emulateSeamfulCubeMapSampling : 1;
348
349 // This flag controls how to translate WEBGL_video_texture sampling function.
350 uint64_t takeVideoTextureAsExternalOES : 1;
351
352 // This flag works around a inconsistent behavior in Mac AMD driver where gl_VertexID doesn't
353 // include base vertex value. It replaces gl_VertexID with (gl_VertexID + angle_BaseVertex) when
354 // angle_BaseVertex is available.
355 uint64_t addBaseVertexToVertexID : 1;
356
357 // This works around the dynamic lvalue indexing of swizzled vectors on various platforms.
358 uint64_t removeDynamicIndexingOfSwizzledVector : 1;
359
360 // This flag works around a slow fxc compile performance issue with dynamic uniform indexing.
361 uint64_t allowTranslateUniformBlockToStructuredBuffer : 1;
362
363 // This flag allows us to add a decoration for layout(yuv) in shaders.
364 uint64_t addVulkanYUVLayoutQualifier : 1;
365
366 // This flag allows disabling ARB_texture_rectangle on a per-compile basis. This is necessary
367 // for WebGL contexts becuase ARB_texture_rectangle may be necessary for the WebGL
368 // implementation internally but shouldn't be exposed to WebGL user code.
369 uint64_t disableARBTextureRectangle : 1;
370
371 // This flag works around a driver bug by rewriting uses of row-major matrices as column-major
372 // in ESSL 3.00 and greater shaders.
373 uint64_t rewriteRowMajorMatrices : 1;
374
375 // Drop any explicit precision qualifiers from shader.
376 uint64_t ignorePrecisionQualifiers : 1;
377
378 // Ask compiler to generate code for depth correction to conform to the Vulkan clip space. If
379 // VK_EXT_depth_clip_control is supported, this code is not generated, saving a uniform look up.
380 uint64_t addVulkanDepthCorrection : 1;
381
382 uint64_t forceShaderPrecisionHighpToMediump : 1;
383
384 // Allow compiler to use specialization constant to do pre-rotation and y flip.
385 uint64_t useSpecializationConstant : 1;
386
387 // Ask compiler to generate Vulkan transform feedback emulation support code.
388 uint64_t addVulkanXfbEmulationSupportCode : 1;
389
390 // Ask compiler to generate Vulkan transform feedback support code when using the
391 // VK_EXT_transform_feedback extension.
392 uint64_t addVulkanXfbExtensionSupportCode : 1;
393
394 // This flag initializes fragment shader's output variables to zero at the beginning of the
395 // fragment shader's main(). It is intended as a workaround for drivers which get context lost
396 // if gl_FragColor is not written.
397 uint64_t initFragmentOutputVariables : 1;
398
399 // Unused. Kept to avoid unnecessarily changing the layout of this structure and tripping up
400 // the fuzzer's hash->bug map.
401 uint64_t unused : 1;
402
403 // Insert explicit casts for float/double/unsigned/signed int on macOS 10.15 with Intel driver
404 uint64_t addExplicitBoolCasts : 1;
405
406 // Add round() after applying dither. This works around a Qualcomm quirk where values can get
407 // ceil()ed instead.
408 uint64_t roundOutputAfterDithering : 1;
409
410 // issuetracker.google.com/274859104 add OpQuantizeToF16 instruction to cast
411 // mediump floating-point values to 16 bit. ARM compiler utilized RelaxedPrecision
412 // to minimize type case and keep a mediump float as 32 bit when assigning it with
413 // a highp floating-point value. It is possible that GLSL shader code is comparing
414 // two meiump values, but ARM compiler is comparing a 32 bit value with a 16 bit value,
415 // causing the comparison to fail.
416 uint64_t castMediumpFloatTo16Bit : 1;
417
418 // anglebug.com/7527: packUnorm4x8 fails on Pixel 4 if it is not passed a highp vec4.
419 // TODO(anglebug.com/7527): This workaround is currently only applied for pixel local storage.
420 // We may want to apply it generally.
421 uint64_t passHighpToPackUnormSnormBuiltins : 1;
422
423 // Use an integer uniform to pass a bitset of enabled clip distances.
424 uint64_t emulateClipDistanceState : 1;
425
426 // issuetracker.google.com/266235549 add aliased memory decoration to ssbo if the variable is
427 // not declared with "restrict" memory qualifier in GLSL
428 uint64_t aliasedUnlessRestrict : 1;
429
430 // Use fragment shaders to compute and set coverage mask based on the alpha value
431 uint64_t emulateAlphaToCoverage : 1;
432
433 ShCompileOptionsMetal metal;
434 ShPixelLocalStorageOptions pls;
435 };
436
437 // The 64 bits hash function. The first parameter is the input string; the
438 // second parameter is the string length.
439 using ShHashFunction64 = khronos_uint64_t (*)(const char *, size_t);
440
441 //
442 // Implementation dependent built-in resources (constants and extensions).
443 // The names for these resources has been obtained by stripping gl_/GL_.
444 //
445 struct ShBuiltInResources
446 {
447 ShBuiltInResources();
448 ShBuiltInResources(const ShBuiltInResources &other);
449 ShBuiltInResources &operator=(const ShBuiltInResources &other);
450
451 // Constants.
452 int MaxVertexAttribs;
453 int MaxVertexUniformVectors;
454 int MaxVaryingVectors;
455 int MaxVertexTextureImageUnits;
456 int MaxCombinedTextureImageUnits;
457 int MaxTextureImageUnits;
458 int MaxFragmentUniformVectors;
459 int MaxDrawBuffers;
460
461 // Extensions.
462 // Set to 1 to enable the extension, else 0.
463 int OES_standard_derivatives;
464 int OES_EGL_image_external;
465 int OES_EGL_image_external_essl3;
466 int NV_EGL_stream_consumer_external;
467 int ARB_texture_rectangle;
468 int EXT_blend_func_extended;
469 int EXT_conservative_depth;
470 int EXT_draw_buffers;
471 int EXT_frag_depth;
472 int EXT_shader_texture_lod;
473 int EXT_shader_framebuffer_fetch;
474 int EXT_shader_framebuffer_fetch_non_coherent;
475 int NV_shader_framebuffer_fetch;
476 int NV_shader_noperspective_interpolation;
477 int ARM_shader_framebuffer_fetch;
478 int OVR_multiview;
479 int OVR_multiview2;
480 int EXT_multisampled_render_to_texture;
481 int EXT_multisampled_render_to_texture2;
482 int EXT_YUV_target;
483 int EXT_geometry_shader;
484 int OES_geometry_shader;
485 int OES_shader_io_blocks;
486 int EXT_shader_io_blocks;
487 int EXT_gpu_shader5;
488 int EXT_shader_non_constant_global_initializers;
489 int OES_texture_storage_multisample_2d_array;
490 int OES_texture_3D;
491 int ANGLE_shader_pixel_local_storage;
492 int ANGLE_texture_multisample;
493 int ANGLE_multi_draw;
494 // TODO(angleproject:3402) remove after chromium side removal to pass compilation
495 int ANGLE_base_vertex_base_instance;
496 int WEBGL_video_texture;
497 int APPLE_clip_distance;
498 int OES_texture_cube_map_array;
499 int EXT_texture_cube_map_array;
500 int EXT_shadow_samplers;
501 int OES_shader_multisample_interpolation;
502 int OES_shader_image_atomic;
503 int EXT_tessellation_shader;
504 int OES_texture_buffer;
505 int EXT_texture_buffer;
506 int OES_sample_variables;
507 int EXT_clip_cull_distance;
508 int ANGLE_clip_cull_distance;
509 int EXT_primitive_bounding_box;
510 int OES_primitive_bounding_box;
511 int EXT_separate_shader_objects;
512 int ANGLE_base_vertex_base_instance_shader_builtin;
513 int ANDROID_extension_pack_es31a;
514 int KHR_blend_equation_advanced;
515
516 // Set to 1 to enable replacing GL_EXT_draw_buffers #extension directives
517 // with GL_NV_draw_buffers in ESSL output. This flag can be used to emulate
518 // EXT_draw_buffers by using it in combination with GLES3.0 glDrawBuffers
519 // function. This applies to Tegra K1 devices.
520 int NV_draw_buffers;
521
522 // Set to 1 if highp precision is supported in the ESSL 1.00 version of the
523 // fragment language. Does not affect versions of the language where highp
524 // support is mandatory.
525 // Default is 0.
526 int FragmentPrecisionHigh;
527
528 // GLSL ES 3.0 constants.
529 int MaxVertexOutputVectors;
530 int MaxFragmentInputVectors;
531 int MinProgramTexelOffset;
532 int MaxProgramTexelOffset;
533
534 // Extension constants.
535
536 // Value of GL_MAX_DUAL_SOURCE_DRAW_BUFFERS_EXT for OpenGL ES output context.
537 // Value of GL_MAX_DUAL_SOURCE_DRAW_BUFFERS for OpenGL output context.
538 // GLES SL version 100 gl_MaxDualSourceDrawBuffersEXT value for EXT_blend_func_extended.
539 int MaxDualSourceDrawBuffers;
540
541 // Value of GL_MAX_VIEWS_OVR.
542 int MaxViewsOVR;
543
544 // Name Hashing.
545 // Set a 64 bit hash function to enable user-defined name hashing.
546 // Default is NULL.
547 ShHashFunction64 HashFunction;
548
549 // The maximum complexity an expression can be when limitExpressionComplexity is turned on.
550 int MaxExpressionComplexity;
551
552 // The maximum depth a call stack can be.
553 int MaxCallStackDepth;
554
555 // The maximum number of parameters a function can have when limitExpressionComplexity is turned
556 // on.
557 int MaxFunctionParameters;
558
559 // GLES 3.1 constants
560
561 // texture gather offset constraints.
562 int MinProgramTextureGatherOffset;
563 int MaxProgramTextureGatherOffset;
564
565 // maximum number of available image units
566 int MaxImageUnits;
567
568 // OES_sample_variables constant
569 // maximum number of available samples
570 int MaxSamples;
571
572 // maximum number of image uniforms in a vertex shader
573 int MaxVertexImageUniforms;
574
575 // maximum number of image uniforms in a fragment shader
576 int MaxFragmentImageUniforms;
577
578 // maximum number of image uniforms in a compute shader
579 int MaxComputeImageUniforms;
580
581 // maximum total number of image uniforms in a program
582 int MaxCombinedImageUniforms;
583
584 // maximum number of uniform locations
585 int MaxUniformLocations;
586
587 // maximum number of ssbos and images in a shader
588 int MaxCombinedShaderOutputResources;
589
590 // maximum number of groups in each dimension
591 std::array<int, 3> MaxComputeWorkGroupCount;
592 // maximum number of threads per work group in each dimension
593 std::array<int, 3> MaxComputeWorkGroupSize;
594
595 // maximum number of total uniform components
596 int MaxComputeUniformComponents;
597
598 // maximum number of texture image units in a compute shader
599 int MaxComputeTextureImageUnits;
600
601 // maximum number of atomic counters in a compute shader
602 int MaxComputeAtomicCounters;
603
604 // maximum number of atomic counter buffers in a compute shader
605 int MaxComputeAtomicCounterBuffers;
606
607 // maximum number of atomic counters in a vertex shader
608 int MaxVertexAtomicCounters;
609
610 // maximum number of atomic counters in a fragment shader
611 int MaxFragmentAtomicCounters;
612
613 // maximum number of atomic counters in a program
614 int MaxCombinedAtomicCounters;
615
616 // maximum binding for an atomic counter
617 int MaxAtomicCounterBindings;
618
619 // maximum number of atomic counter buffers in a vertex shader
620 int MaxVertexAtomicCounterBuffers;
621
622 // maximum number of atomic counter buffers in a fragment shader
623 int MaxFragmentAtomicCounterBuffers;
624
625 // maximum number of atomic counter buffers in a program
626 int MaxCombinedAtomicCounterBuffers;
627
628 // maximum number of buffer object storage in machine units
629 int MaxAtomicCounterBufferSize;
630
631 // maximum number of uniform block bindings
632 int MaxUniformBufferBindings;
633
634 // maximum number of shader storage buffer bindings
635 int MaxShaderStorageBufferBindings;
636
637 // maximum point size (higher limit from ALIASED_POINT_SIZE_RANGE)
638 float MaxPointSize;
639
640 // EXT_geometry_shader constants
641 int MaxGeometryUniformComponents;
642 int MaxGeometryUniformBlocks;
643 int MaxGeometryInputComponents;
644 int MaxGeometryOutputComponents;
645 int MaxGeometryOutputVertices;
646 int MaxGeometryTotalOutputComponents;
647 int MaxGeometryTextureImageUnits;
648 int MaxGeometryAtomicCounterBuffers;
649 int MaxGeometryAtomicCounters;
650 int MaxGeometryShaderStorageBlocks;
651 int MaxGeometryShaderInvocations;
652 int MaxGeometryImageUniforms;
653
654 // EXT_tessellation_shader constants
655 int MaxTessControlInputComponents;
656 int MaxTessControlOutputComponents;
657 int MaxTessControlTextureImageUnits;
658 int MaxTessControlUniformComponents;
659 int MaxTessControlTotalOutputComponents;
660 int MaxTessControlImageUniforms;
661 int MaxTessControlAtomicCounters;
662 int MaxTessControlAtomicCounterBuffers;
663
664 int MaxTessPatchComponents;
665 int MaxPatchVertices;
666 int MaxTessGenLevel;
667
668 int MaxTessEvaluationInputComponents;
669 int MaxTessEvaluationOutputComponents;
670 int MaxTessEvaluationTextureImageUnits;
671 int MaxTessEvaluationUniformComponents;
672 int MaxTessEvaluationImageUniforms;
673 int MaxTessEvaluationAtomicCounters;
674 int MaxTessEvaluationAtomicCounterBuffers;
675
676 // Subpixel bits used in rasterization.
677 int SubPixelBits;
678
679 // APPLE_clip_distance / EXT_clip_cull_distance / ANGLE_clip_cull_distance constants
680 int MaxClipDistances;
681 int MaxCullDistances;
682 int MaxCombinedClipAndCullDistances;
683
684 // ANGLE_shader_pixel_local_storage.
685 int MaxPixelLocalStoragePlanes;
686 int MaxColorAttachmentsWithActivePixelLocalStorage;
687 int MaxCombinedDrawBuffersAndPixelLocalStoragePlanes;
688 };
689
690 //
691 // ShHandle held by but opaque to the driver. It is allocated,
692 // managed, and de-allocated by the compiler. Its contents
693 // are defined by and used by the compiler.
694 //
695 // If handle creation fails, 0 will be returned.
696 //
697 using ShHandle = void *;
698
699 namespace sh
700 {
701 using BinaryBlob = std::vector<uint32_t>;
702 using ShaderBinaryBlob = std::vector<uint8_t>;
703
704 //
705 // Driver must call this first, once, before doing any other compiler operations.
706 // If the function succeeds, the return value is true, else false.
707 //
708 bool Initialize();
709 //
710 // Driver should call this at shutdown.
711 // If the function succeeds, the return value is true, else false.
712 //
713 bool Finalize();
714
715 //
716 // Initialize built-in resources with minimum expected values.
717 // Parameters:
718 // resources: The object to initialize. Will be comparable with memcmp.
719 //
720 void InitBuiltInResources(ShBuiltInResources *resources);
721
722 //
723 // Returns a copy of the current ShBuiltInResources stored in the compiler.
724 // Parameters:
725 // handle: Specifies the handle of the compiler to be used.
726 ShBuiltInResources GetBuiltInResources(const ShHandle handle);
727
728 //
729 // Returns the a concatenated list of the items in ShBuiltInResources as a null-terminated string.
730 // This function must be updated whenever ShBuiltInResources is changed.
731 // Parameters:
732 // handle: Specifies the handle of the compiler to be used.
733 const std::string &GetBuiltInResourcesString(const ShHandle handle);
734
735 //
736 // Driver calls these to create and destroy compiler objects.
737 //
738 // Returns the handle of constructed compiler, null if the requested compiler is not supported.
739 // Parameters:
740 // type: Specifies the type of shader - GL_FRAGMENT_SHADER or GL_VERTEX_SHADER.
741 // spec: Specifies the language spec the compiler must conform to - SH_GLES2_SPEC or SH_WEBGL_SPEC.
742 // output: Specifies the output code type - for example SH_ESSL_OUTPUT, SH_GLSL_OUTPUT,
743 // SH_HLSL_3_0_OUTPUT or SH_HLSL_4_1_OUTPUT. Note: Each output type may only
744 // be supported in some configurations.
745 // resources: Specifies the built-in resources.
746 ShHandle ConstructCompiler(sh::GLenum type,
747 ShShaderSpec spec,
748 ShShaderOutput output,
749 const ShBuiltInResources *resources);
750 void Destruct(ShHandle handle);
751
752 //
753 // Compiles the given shader source.
754 // If the function succeeds, the return value is true, else false.
755 // Parameters:
756 // handle: Specifies the handle of compiler to be used.
757 // shaderStrings: Specifies an array of pointers to null-terminated strings containing the shader
758 // source code.
759 // numStrings: Specifies the number of elements in shaderStrings array.
760 // compileOptions: A mask of compile options defined above.
761 bool Compile(const ShHandle handle,
762 const char *const shaderStrings[],
763 size_t numStrings,
764 const ShCompileOptions &compileOptions);
765
766 // Clears the results from the previous compilation.
767 void ClearResults(const ShHandle handle);
768
769 // Return the version of the shader language.
770 int GetShaderVersion(const ShHandle handle);
771
772 // Return the currently set language output type.
773 ShShaderOutput GetShaderOutputType(const ShHandle handle);
774
775 // Returns null-terminated information log for a compiled shader.
776 // Parameters:
777 // handle: Specifies the compiler
778 const std::string &GetInfoLog(const ShHandle handle);
779
780 // Returns null-terminated object code for a compiled shader. Only valid for output types that
781 // generate human-readable code (GLSL, ESSL or HLSL).
782 // Parameters:
783 // handle: Specifies the compiler
784 const std::string &GetObjectCode(const ShHandle handle);
785
786 // Returns object binary blob for a compiled shader. Only valid for output types that
787 // generate binary blob (SPIR-V).
788 // Parameters:
789 // handle: Specifies the compiler
790 const BinaryBlob &GetObjectBinaryBlob(const ShHandle handle);
791
792 // Returns a full binary for a compiled shader, to be loaded with glShaderBinary during runtime.
793 // Parameters:
794 // handle: Specifies the compiler
795 bool GetShaderBinary(const ShHandle handle,
796 const char *const shaderStrings[],
797 size_t numStrings,
798 const ShCompileOptions &compileOptions,
799 ShaderBinaryBlob *const binaryOut);
800
801 // Returns a (original_name, hash) map containing all the user defined names in the shader,
802 // including variable names, function names, struct names, and struct field names.
803 // Parameters:
804 // handle: Specifies the compiler
805 const std::map<std::string, std::string> *GetNameHashingMap(const ShHandle handle);
806
807 // Shader variable inspection.
808 // Returns a pointer to a list of variables of the designated type.
809 // (See ShaderVars.h for type definitions, included above)
810 // Returns NULL on failure.
811 // Parameters:
812 // handle: Specifies the compiler
813 const std::vector<sh::ShaderVariable> *GetUniforms(const ShHandle handle);
814 const std::vector<sh::ShaderVariable> *GetVaryings(const ShHandle handle);
815 const std::vector<sh::ShaderVariable> *GetInputVaryings(const ShHandle handle);
816 const std::vector<sh::ShaderVariable> *GetOutputVaryings(const ShHandle handle);
817 const std::vector<sh::ShaderVariable> *GetAttributes(const ShHandle handle);
818 const std::vector<sh::ShaderVariable> *GetOutputVariables(const ShHandle handle);
819 const std::vector<sh::InterfaceBlock> *GetInterfaceBlocks(const ShHandle handle);
820 const std::vector<sh::InterfaceBlock> *GetUniformBlocks(const ShHandle handle);
821 const std::vector<sh::InterfaceBlock> *GetShaderStorageBlocks(const ShHandle handle);
822 sh::WorkGroupSize GetComputeShaderLocalGroupSize(const ShHandle handle);
823 // Returns the number of views specified through the num_views layout qualifier. If num_views is
824 // not set, the function returns -1.
825 int GetVertexShaderNumViews(const ShHandle handle);
826 // Returns true if the shader has specified the |sample| qualifier, implying that per-sample shading
827 // should be enabled
828 bool EnablesPerSampleShading(const ShHandle handle);
829
830 // Returns specialization constant usage bits
831 uint32_t GetShaderSpecConstUsageBits(const ShHandle handle);
832
833 // Returns true if the passed in variables pack in maxVectors followingthe packing rules from the
834 // GLSL 1.017 spec, Appendix A, section 7.
835 // Returns false otherwise. Also look at the enforcePackingRestrictions flag above.
836 // Parameters:
837 // maxVectors: the available rows of registers.
838 // variables: an array of variables.
839 bool CheckVariablesWithinPackingLimits(int maxVectors,
840 const std::vector<sh::ShaderVariable> &variables);
841
842 // Gives the compiler-assigned register for a shader storage block.
843 // The method writes the value to the output variable "indexOut".
844 // Returns true if it found a valid shader storage block, false otherwise.
845 // Parameters:
846 // handle: Specifies the compiler
847 // shaderStorageBlockName: Specifies the shader storage block
848 // indexOut: output variable that stores the assigned register
849 bool GetShaderStorageBlockRegister(const ShHandle handle,
850 const std::string &shaderStorageBlockName,
851 unsigned int *indexOut);
852
853 // Gives the compiler-assigned register for a uniform block.
854 // The method writes the value to the output variable "indexOut".
855 // Returns true if it found a valid uniform block, false otherwise.
856 // Parameters:
857 // handle: Specifies the compiler
858 // uniformBlockName: Specifies the uniform block
859 // indexOut: output variable that stores the assigned register
860 bool GetUniformBlockRegister(const ShHandle handle,
861 const std::string &uniformBlockName,
862 unsigned int *indexOut);
863
864 bool ShouldUniformBlockUseStructuredBuffer(const ShHandle handle,
865 const std::string &uniformBlockName);
866 const std::set<std::string> *GetSlowCompilingUniformBlockSet(const ShHandle handle);
867
868 // Gives a map from uniform names to compiler-assigned registers in the default uniform block.
869 // Note that the map contains also registers of samplers that have been extracted from structs.
870 const std::map<std::string, unsigned int> *GetUniformRegisterMap(const ShHandle handle);
871
872 // Sampler, image and atomic counters share registers(t type and u type),
873 // GetReadonlyImage2DRegisterIndex and GetImage2DRegisterIndex return the first index into
874 // a range of reserved registers for image2D/iimage2D/uimage2D variables.
875 // Parameters: handle: Specifies the compiler
876 unsigned int GetReadonlyImage2DRegisterIndex(const ShHandle handle);
877 unsigned int GetImage2DRegisterIndex(const ShHandle handle);
878
879 // The method records these used function names related with image2D/iimage2D/uimage2D, these
880 // functions will be dynamically generated.
881 // Parameters:
882 // handle: Specifies the compiler
883 const std::set<std::string> *GetUsedImage2DFunctionNames(const ShHandle handle);
884
885 uint8_t GetClipDistanceArraySize(const ShHandle handle);
886 uint8_t GetCullDistanceArraySize(const ShHandle handle);
887 bool HasClipDistanceInVertexShader(const ShHandle handle);
888 bool HasDiscardInFragmentShader(const ShHandle handle);
889 bool HasValidGeometryShaderInputPrimitiveType(const ShHandle handle);
890 bool HasValidGeometryShaderOutputPrimitiveType(const ShHandle handle);
891 bool HasValidGeometryShaderMaxVertices(const ShHandle handle);
892 bool HasValidTessGenMode(const ShHandle handle);
893 bool HasValidTessGenSpacing(const ShHandle handle);
894 bool HasValidTessGenVertexOrder(const ShHandle handle);
895 bool HasValidTessGenPointMode(const ShHandle handle);
896 GLenum GetGeometryShaderInputPrimitiveType(const ShHandle handle);
897 GLenum GetGeometryShaderOutputPrimitiveType(const ShHandle handle);
898 int GetGeometryShaderInvocations(const ShHandle handle);
899 int GetGeometryShaderMaxVertices(const ShHandle handle);
900 unsigned int GetShaderSharedMemorySize(const ShHandle handle);
901 int GetTessControlShaderVertices(const ShHandle handle);
902 GLenum GetTessGenMode(const ShHandle handle);
903 GLenum GetTessGenSpacing(const ShHandle handle);
904 GLenum GetTessGenVertexOrder(const ShHandle handle);
905 GLenum GetTessGenPointMode(const ShHandle handle);
906
907 // Returns the blend equation list supported in the fragment shader. This is a bitset of
908 // gl::BlendEquationType, and can only include bits from KHR_blend_equation_advanced.
909 uint32_t GetAdvancedBlendEquations(const ShHandle handle);
910
911 //
912 // Helper function to identify specs that are based on the WebGL spec.
913 //
IsWebGLBasedSpec(ShShaderSpec spec)914 inline bool IsWebGLBasedSpec(ShShaderSpec spec)
915 {
916 return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
917 }
918
919 //
920 // Helper function to identify DesktopGL specs
921 //
IsDesktopGLSpec(ShShaderSpec spec)922 inline bool IsDesktopGLSpec(ShShaderSpec spec)
923 {
924 return spec == SH_GL_CORE_SPEC || spec == SH_GL_COMPATIBILITY_SPEC;
925 }
926
927 // Can't prefix with just _ because then we might introduce a double underscore, which is not safe
928 // in GLSL (ESSL 3.00.6 section 3.8: All identifiers containing a double underscore are reserved for
929 // use by the underlying implementation). u is short for user-defined.
930 extern const char kUserDefinedNamePrefix[];
931
932 namespace vk
933 {
934
935 // Specialization constant ids
936 enum class SpecializationConstantId : uint32_t
937 {
938 SurfaceRotation = 0,
939 Dither = 1,
940
941 InvalidEnum = 2,
942 EnumCount = InvalidEnum,
943 };
944
945 enum class SpecConstUsage : uint32_t
946 {
947 Rotation = 0,
948 Dither = 1,
949
950 InvalidEnum = 2,
951 EnumCount = InvalidEnum,
952 };
953
954 enum ColorAttachmentDitherControl
955 {
956 // See comments in ContextVk::updateDither and EmulateDithering.cpp
957 kDitherControlNoDither = 0,
958 kDitherControlDither4444 = 1,
959 kDitherControlDither5551 = 2,
960 kDitherControlDither565 = 3,
961 };
962
963 namespace spirv
964 {
965 enum NonSemanticInstruction
966 {
967 // The overview instruction containing information such as what predefined ids are present in
968 // the SPIR-V. Simultaneously, this instruction identifies the location where the
969 // types/constants/variables section ends and the functions section starts.
970 kNonSemanticOverview,
971 // The instruction identifying the entry to the shader, i.e. at the start of main()
972 kNonSemanticEnter,
973 // The instruction identifying where vertex data is output. This is before return from main()
974 // in vertex and tessellation shaders, and before OpEmitVertex in geometry shaders.
975 kNonSemanticVertexOutput,
976 // The instruction identifying the location where transform feedback emulation should be
977 // written.
978 kNonSemanticTransformFeedbackEmulation,
979 };
980
981 // The non-semantic instruction id has many bits available. With kNonSemanticOverview, they are
982 // used to provide additional overview details. Providing this information in the instruction's
983 // payload require OpConstants and recovering those, which is unnecessary complexity.
984 constexpr uint32_t kNonSemanticInstructionBits = 4;
985 constexpr uint32_t kNonSemanticInstructionMask = 0xF;
986 constexpr uint32_t kOverviewHasSampleRateShadingMask = 0x10;
987 constexpr uint32_t kOverviewHasSampleIDMask = 0x20;
988 constexpr uint32_t kOverviewHasOutputPerVertexMask = 0x40;
989
990 enum ReservedIds
991 {
992 kIdInvalid = 0,
993
994 // =============================================================================================
995 // Ids that are fixed and are always present in the SPIR-V where applicable. The SPIR-V
996 // transformer can thus reliably use these ids.
997
998 // Global information
999 kIdNonSemanticInstructionSet,
1000 kIdEntryPoint,
1001
1002 // Basic types
1003 kIdVoid,
1004 kIdFloat,
1005 kIdVec2,
1006 kIdVec3,
1007 kIdVec4,
1008 kIdMat2,
1009 kIdMat3,
1010 kIdMat4,
1011 kIdInt,
1012 kIdIVec4,
1013 kIdUint,
1014
1015 // Common constants
1016 kIdIntZero,
1017 kIdIntOne,
1018 kIdIntTwo,
1019 kIdIntThree,
1020
1021 // Type pointers
1022 kIdIntInputTypePointer,
1023 kIdVec4OutputTypePointer,
1024 kIdIVec4FunctionTypePointer,
1025 kIdOutputPerVertexTypePointer,
1026
1027 // Pre-rotation and Z-correction support
1028 kIdTransformPositionFunction,
1029 kIdInputPerVertexBlockArray,
1030 kIdOutputPerVertexBlockArray,
1031 kIdOutputPerVertexVar,
1032
1033 // Transform feedback support
1034 kIdXfbEmulationGetOffsetsFunction,
1035 kIdXfbEmulationCaptureFunction,
1036 kIdXfbEmulationBufferVarZero,
1037 kIdXfbEmulationBufferVarOne,
1038 kIdXfbEmulationBufferVarTwo,
1039 kIdXfbEmulationBufferVarThree,
1040
1041 // Multisampling support
1042 kIdSampleID,
1043
1044 // =============================================================================================
1045 // ANGLE internal shader variables, which are not produced as ShaderVariables.
1046 // kIdShaderVariablesBegin marks the beginning of these ids. variableId -> info maps in the
1047 // backend can use |variableId - kIdShaderVariablesBegin| as key into a flat array.
1048 //
1049 // Note that for blocks, only the block id is in this section as that is the id used in the
1050 // variableId -> info maps.
1051 kIdShaderVariablesBegin,
1052
1053 // gl_PerVertex
1054 kIdInputPerVertexBlock = kIdShaderVariablesBegin,
1055 kIdOutputPerVertexBlock,
1056 // The driver and default uniform blocks
1057 kIdDriverUniformsBlock,
1058 kIdDefaultUniformsBlock,
1059 // The atomic counter block
1060 kIdAtomicCounterBlock,
1061 // Buffer block used for transform feedback emulation
1062 kIdXfbEmulationBufferBlockZero,
1063 kIdXfbEmulationBufferBlockOne,
1064 kIdXfbEmulationBufferBlockTwo,
1065 kIdXfbEmulationBufferBlockThree,
1066 // Additional varying added to hold untransformed gl_Position for transform feedback capture
1067 kIdXfbExtensionPosition,
1068
1069 kIdFirstUnreserved,
1070 };
1071 } // namespace spirv
1072
1073 // Packing information for driver uniform's misc field:
1074 // - 1 bit for whether surface rotation results in swapped axes
1075 // - 5 bits for advanced blend equation
1076 // - 6 bits for sample count
1077 // - 8 bits for enabled clip planes
1078 // - 1 bit for whether depth should be transformed to Vulkan clip space
1079 // - 1 bit for whether alpha to coverage is enabled
1080 // - 10 bits unused
1081 constexpr uint32_t kDriverUniformsMiscSwapXYMask = 0x1;
1082 constexpr uint32_t kDriverUniformsMiscAdvancedBlendEquationOffset = 1;
1083 constexpr uint32_t kDriverUniformsMiscAdvancedBlendEquationMask = 0x1F;
1084 constexpr uint32_t kDriverUniformsMiscSampleCountOffset = 6;
1085 constexpr uint32_t kDriverUniformsMiscSampleCountMask = 0x3F;
1086 constexpr uint32_t kDriverUniformsMiscEnabledClipPlanesOffset = 12;
1087 constexpr uint32_t kDriverUniformsMiscEnabledClipPlanesMask = 0xFF;
1088 constexpr uint32_t kDriverUniformsMiscTransformDepthOffset = 20;
1089 constexpr uint32_t kDriverUniformsMiscTransformDepthMask = 0x1;
1090 constexpr uint32_t kDriverUniformsMiscAlphaToCoverageOffset = 21;
1091 constexpr uint32_t kDriverUniformsMiscAlphaToCoverageMask = 0x1;
1092 } // namespace vk
1093
1094 namespace mtl
1095 {
1096 // Specialization constant to enable multisampled rendering behavior.
1097 extern const char kMultisampledRenderingConstName[];
1098
1099 // Specialization constant to emulate rasterizer discard.
1100 extern const char kRasterizerDiscardEnabledConstName[];
1101
1102 // Specialization constant to enable depth write in fragment shaders.
1103 extern const char kDepthWriteEnabledConstName[];
1104
1105 // Specialization constant to enable alpha to coverage.
1106 extern const char kEmulateAlphaToCoverageConstName[];
1107 } // namespace mtl
1108
1109 } // namespace sh
1110
1111 #endif // GLSLANG_SHADERLANG_H_
1112