• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
3 // Copyright (C) 2012-2013 LunarG, Inc.
4 // Copyright (C) 2017 ARM Limited.
5 // Copyright (C) 2015-2020 Google, Inc.
6 // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
7 //
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions
12 // are met:
13 //
14 //    Redistributions of source code must retain the above copyright
15 //    notice, this list of conditions and the following disclaimer.
16 //
17 //    Redistributions in binary form must reproduce the above
18 //    copyright notice, this list of conditions and the following
19 //    disclaimer in the documentation and/or other materials provided
20 //    with the distribution.
21 //
22 //    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
23 //    contributors may be used to endorse or promote products derived
24 //    from this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 // POSSIBILITY OF SUCH DAMAGE.
38 //
39 
40 //
41 // Help manage multiple profiles, versions, extensions etc.
42 //
43 // These don't return error codes, as the presumption is parsing will
44 // always continue as if the tested feature were enabled, and thus there
45 // is no error recovery needed.
46 //
47 
48 //
49 // HOW TO add a feature enabled by an extension.
50 //
51 // To add a new hypothetical "Feature F" to the front end, where an extension
52 // "XXX_extension_X" can be used to enable the feature, do the following.
53 //
54 // OVERVIEW: Specific features are what are error-checked for, not
55 //    extensions:  A specific Feature F might be enabled by an extension, or a
56 //    particular version in a particular profile, or a stage, or combinations, etc.
57 //
58 //    The basic mechanism is to use the following to "declare" all the things that
59 //    enable/disable Feature F, in a code path that implements Feature F:
60 //
61 //        requireProfile()
62 //        profileRequires()
63 //        requireStage()
64 //        checkDeprecated()
65 //        requireNotRemoved()
66 //        requireExtensions()
67 //        extensionRequires()
68 //
69 //    Typically, only the first two calls are needed.  They go into a code path that
70 //    implements Feature F, and will log the proper error/warning messages.  Parsing
71 //    will then always continue as if the tested feature was enabled.
72 //
73 //    There is typically no if-testing or conditional parsing, just insertion of the calls above.
74 //    However, if symbols specific to the extension are added (step 5), they will
75 //    only be added under tests that the minimum version and profile are present.
76 //
77 // 1) Add a symbol name for the extension string at the bottom of Versions.h:
78 //
79 //     const char* const XXX_extension_X = "XXX_extension_X";
80 //
81 // 2) Add extension initialization to TParseVersions::initializeExtensionBehavior(),
82 //    the first function below and optionally a entry to extensionData for additional
83 //    error checks:
84 //
85 //     extensionBehavior[XXX_extension_X] = EBhDisable;
86 //     (Optional) exts[] = {XXX_extension_X, EShTargetSpv_1_4}
87 //
88 // 3) Add any preprocessor directives etc. in the next function, TParseVersions::getPreamble():
89 //
90 //           "#define XXX_extension_X 1\n"
91 //
92 //    The new-line is important, as that ends preprocess tokens.
93 //
94 // 4) Insert a profile check in the feature's path (unless all profiles support the feature,
95 //    for some version level).  That is, call requireProfile() to constrain the profiles, e.g.:
96 //
97 //         // ... in a path specific to Feature F...
98 //         requireProfile(loc,
99 //                        ECoreProfile | ECompatibilityProfile,
100 //                        "Feature F");
101 //
102 // 5) For each profile that supports the feature, insert version/extension checks:
103 //
104 //    The mostly likely scenario is that Feature F can only be used with a
105 //    particular profile if XXX_extension_X is present or the version is
106 //    high enough that the core specification already incorporated it.
107 //
108 //        // following the requireProfile() call...
109 //        profileRequires(loc,
110 //                        ECoreProfile | ECompatibilityProfile,
111 //                        420,             // 0 if no version incorporated the feature into the core spec.
112 //                        XXX_extension_X, // can be a list of extensions that all add the feature
113 //                        "Feature F Description");
114 //
115 //    This allows the feature if either A) one of the extensions is enabled or
116 //    B) the version is high enough.  If no version yet incorporates the feature
117 //    into core, pass in 0.
118 //
119 //    This can be called multiple times, if different profiles support the
120 //    feature starting at different version numbers or with different
121 //    extensions.
122 //
123 //    This must be called for each profile allowed by the initial call to requireProfile().
124 //
125 //    Profiles are all masks, which can be "or"-ed together.
126 //
127 //        ENoProfile
128 //        ECoreProfile
129 //        ECompatibilityProfile
130 //        EEsProfile
131 //
132 //    The ENoProfile profile is only for desktop, before profiles showed up in version 150;
133 //    All other #version with no profile default to either es or core, and so have profiles.
134 //
135 //    You can select all but a particular profile using ~.  The following basically means "desktop":
136 //
137 //        ~EEsProfile
138 //
139 // 6) If built-in symbols are added by the extension, add them in Initialize.cpp:  Their use
140 //    will be automatically error checked against the extensions enabled at that moment.
141 //    see the comment at the top of Initialize.cpp for where to put them.  Establish them at
142 //    the earliest release that supports the extension.  Then, tag them with the
143 //    set of extensions that both enable them and are necessary, given the version of the symbol
144 //    table. (There is a different symbol table for each version.)
145 //
146 // 7) If the extension has additional requirements like minimum SPIR-V version required, add them
147 //    to extensionRequires()
148 
149 #include "parseVersions.h"
150 #include "localintermediate.h"
151 
152 namespace glslang {
153 
154 #ifndef GLSLANG_WEB
155 
156 //
157 // Initialize all extensions, almost always to 'disable', as once their features
158 // are incorporated into a core version, their features are supported through allowing that
159 // core version, not through a pseudo-enablement of the extension.
160 //
initializeExtensionBehavior()161 void TParseVersions::initializeExtensionBehavior()
162 {
163     typedef struct {
164         const char *const extensionName;
165         EShTargetLanguageVersion minSpvVersion;
166     } extensionData;
167 
168     const extensionData exts[] = { {E_GL_EXT_ray_tracing, EShTargetSpv_1_4} };
169 
170     for (size_t ii = 0; ii < sizeof(exts) / sizeof(exts[0]); ii++) {
171         // Add only extensions which require > spv1.0 to save space in map
172         if (exts[ii].minSpvVersion > EShTargetSpv_1_0) {
173             extensionMinSpv[E_GL_EXT_ray_tracing] = exts[ii].minSpvVersion;
174         }
175     }
176 
177     extensionBehavior[E_GL_OES_texture_3D]                   = EBhDisable;
178     extensionBehavior[E_GL_OES_standard_derivatives]         = EBhDisable;
179     extensionBehavior[E_GL_EXT_frag_depth]                   = EBhDisable;
180     extensionBehavior[E_GL_OES_EGL_image_external]           = EBhDisable;
181     extensionBehavior[E_GL_OES_EGL_image_external_essl3]     = EBhDisable;
182     extensionBehavior[E_GL_EXT_YUV_target]                   = EBhDisable;
183     extensionBehavior[E_GL_EXT_shader_texture_lod]           = EBhDisable;
184     extensionBehavior[E_GL_EXT_shadow_samplers]              = EBhDisable;
185     extensionBehavior[E_GL_ARB_texture_rectangle]            = EBhDisable;
186     extensionBehavior[E_GL_3DL_array_objects]                = EBhDisable;
187     extensionBehavior[E_GL_ARB_shading_language_420pack]     = EBhDisable;
188     extensionBehavior[E_GL_ARB_texture_gather]               = EBhDisable;
189     extensionBehavior[E_GL_ARB_gpu_shader5]                  = EBhDisablePartial;
190     extensionBehavior[E_GL_ARB_separate_shader_objects]      = EBhDisable;
191     extensionBehavior[E_GL_ARB_compute_shader]               = EBhDisable;
192     extensionBehavior[E_GL_ARB_tessellation_shader]          = EBhDisable;
193     extensionBehavior[E_GL_ARB_enhanced_layouts]             = EBhDisable;
194     extensionBehavior[E_GL_ARB_texture_cube_map_array]       = EBhDisable;
195     extensionBehavior[E_GL_ARB_texture_multisample]          = EBhDisable;
196     extensionBehavior[E_GL_ARB_shader_texture_lod]           = EBhDisable;
197     extensionBehavior[E_GL_ARB_explicit_attrib_location]     = EBhDisable;
198     extensionBehavior[E_GL_ARB_explicit_uniform_location]    = EBhDisable;
199     extensionBehavior[E_GL_ARB_shader_image_load_store]      = EBhDisable;
200     extensionBehavior[E_GL_ARB_shader_atomic_counters]       = EBhDisable;
201     extensionBehavior[E_GL_ARB_shader_draw_parameters]       = EBhDisable;
202     extensionBehavior[E_GL_ARB_shader_group_vote]            = EBhDisable;
203     extensionBehavior[E_GL_ARB_derivative_control]           = EBhDisable;
204     extensionBehavior[E_GL_ARB_shader_texture_image_samples] = EBhDisable;
205     extensionBehavior[E_GL_ARB_viewport_array]               = EBhDisable;
206     extensionBehavior[E_GL_ARB_gpu_shader_int64]             = EBhDisable;
207     extensionBehavior[E_GL_ARB_gpu_shader_fp64]              = EBhDisable;
208     extensionBehavior[E_GL_ARB_shader_ballot]                = EBhDisable;
209     extensionBehavior[E_GL_ARB_sparse_texture2]              = EBhDisable;
210     extensionBehavior[E_GL_ARB_sparse_texture_clamp]         = EBhDisable;
211     extensionBehavior[E_GL_ARB_shader_stencil_export]        = EBhDisable;
212 //    extensionBehavior[E_GL_ARB_cull_distance]                = EBhDisable;    // present for 4.5, but need extension control over block members
213     extensionBehavior[E_GL_ARB_post_depth_coverage]          = EBhDisable;
214     extensionBehavior[E_GL_ARB_shader_viewport_layer_array]  = EBhDisable;
215     extensionBehavior[E_GL_ARB_fragment_shader_interlock]    = EBhDisable;
216     extensionBehavior[E_GL_ARB_shader_clock]                 = EBhDisable;
217     extensionBehavior[E_GL_ARB_uniform_buffer_object]        = EBhDisable;
218     extensionBehavior[E_GL_ARB_sample_shading]               = EBhDisable;
219     extensionBehavior[E_GL_ARB_shader_bit_encoding]          = EBhDisable;
220     extensionBehavior[E_GL_ARB_shader_image_size]            = EBhDisable;
221     extensionBehavior[E_GL_ARB_shader_storage_buffer_object] = EBhDisable;
222     extensionBehavior[E_GL_ARB_shading_language_packing]     = EBhDisable;
223     extensionBehavior[E_GL_ARB_texture_query_lod]            = EBhDisable;
224     extensionBehavior[E_GL_ARB_vertex_attrib_64bit]          = EBhDisable;
225 
226     extensionBehavior[E_GL_KHR_shader_subgroup_basic]            = EBhDisable;
227     extensionBehavior[E_GL_KHR_shader_subgroup_vote]             = EBhDisable;
228     extensionBehavior[E_GL_KHR_shader_subgroup_arithmetic]       = EBhDisable;
229     extensionBehavior[E_GL_KHR_shader_subgroup_ballot]           = EBhDisable;
230     extensionBehavior[E_GL_KHR_shader_subgroup_shuffle]          = EBhDisable;
231     extensionBehavior[E_GL_KHR_shader_subgroup_shuffle_relative] = EBhDisable;
232     extensionBehavior[E_GL_KHR_shader_subgroup_clustered]        = EBhDisable;
233     extensionBehavior[E_GL_KHR_shader_subgroup_quad]             = EBhDisable;
234     extensionBehavior[E_GL_KHR_memory_scope_semantics]           = EBhDisable;
235 
236     extensionBehavior[E_GL_EXT_shader_atomic_int64]              = EBhDisable;
237 
238     extensionBehavior[E_GL_EXT_shader_non_constant_global_initializers] = EBhDisable;
239     extensionBehavior[E_GL_EXT_shader_image_load_formatted]             = EBhDisable;
240     extensionBehavior[E_GL_EXT_post_depth_coverage]                     = EBhDisable;
241     extensionBehavior[E_GL_EXT_control_flow_attributes]                 = EBhDisable;
242     extensionBehavior[E_GL_EXT_nonuniform_qualifier]                    = EBhDisable;
243     extensionBehavior[E_GL_EXT_samplerless_texture_functions]           = EBhDisable;
244     extensionBehavior[E_GL_EXT_scalar_block_layout]                     = EBhDisable;
245     extensionBehavior[E_GL_EXT_fragment_invocation_density]             = EBhDisable;
246     extensionBehavior[E_GL_EXT_buffer_reference]                        = EBhDisable;
247     extensionBehavior[E_GL_EXT_buffer_reference2]                       = EBhDisable;
248     extensionBehavior[E_GL_EXT_buffer_reference_uvec2]                  = EBhDisable;
249     extensionBehavior[E_GL_EXT_demote_to_helper_invocation]             = EBhDisable;
250     extensionBehavior[E_GL_EXT_debug_printf]                            = EBhDisable;
251 
252     extensionBehavior[E_GL_EXT_shader_16bit_storage]                    = EBhDisable;
253     extensionBehavior[E_GL_EXT_shader_8bit_storage]                     = EBhDisable;
254 
255     // #line and #include
256     extensionBehavior[E_GL_GOOGLE_cpp_style_line_directive]          = EBhDisable;
257     extensionBehavior[E_GL_GOOGLE_include_directive]                 = EBhDisable;
258 
259     extensionBehavior[E_GL_AMD_shader_ballot]                        = EBhDisable;
260     extensionBehavior[E_GL_AMD_shader_trinary_minmax]                = EBhDisable;
261     extensionBehavior[E_GL_AMD_shader_explicit_vertex_parameter]     = EBhDisable;
262     extensionBehavior[E_GL_AMD_gcn_shader]                           = EBhDisable;
263     extensionBehavior[E_GL_AMD_gpu_shader_half_float]                = EBhDisable;
264     extensionBehavior[E_GL_AMD_texture_gather_bias_lod]              = EBhDisable;
265     extensionBehavior[E_GL_AMD_gpu_shader_int16]                     = EBhDisable;
266     extensionBehavior[E_GL_AMD_shader_image_load_store_lod]          = EBhDisable;
267     extensionBehavior[E_GL_AMD_shader_fragment_mask]                 = EBhDisable;
268     extensionBehavior[E_GL_AMD_gpu_shader_half_float_fetch]          = EBhDisable;
269 
270     extensionBehavior[E_GL_INTEL_shader_integer_functions2]          = EBhDisable;
271 
272     extensionBehavior[E_GL_NV_sample_mask_override_coverage]         = EBhDisable;
273     extensionBehavior[E_SPV_NV_geometry_shader_passthrough]          = EBhDisable;
274     extensionBehavior[E_GL_NV_viewport_array2]                       = EBhDisable;
275     extensionBehavior[E_GL_NV_stereo_view_rendering]                 = EBhDisable;
276     extensionBehavior[E_GL_NVX_multiview_per_view_attributes]        = EBhDisable;
277     extensionBehavior[E_GL_NV_shader_atomic_int64]                   = EBhDisable;
278     extensionBehavior[E_GL_NV_conservative_raster_underestimation]   = EBhDisable;
279     extensionBehavior[E_GL_NV_shader_noperspective_interpolation]    = EBhDisable;
280     extensionBehavior[E_GL_NV_shader_subgroup_partitioned]           = EBhDisable;
281     extensionBehavior[E_GL_NV_shading_rate_image]                    = EBhDisable;
282     extensionBehavior[E_GL_NV_ray_tracing]                           = EBhDisable;
283     extensionBehavior[E_GL_NV_fragment_shader_barycentric]           = EBhDisable;
284     extensionBehavior[E_GL_NV_compute_shader_derivatives]            = EBhDisable;
285     extensionBehavior[E_GL_NV_shader_texture_footprint]              = EBhDisable;
286     extensionBehavior[E_GL_NV_mesh_shader]                           = EBhDisable;
287 
288     extensionBehavior[E_GL_NV_cooperative_matrix]                    = EBhDisable;
289     extensionBehavior[E_GL_NV_shader_sm_builtins]                    = EBhDisable;
290     extensionBehavior[E_GL_NV_integer_cooperative_matrix]            = EBhDisable;
291 
292     // AEP
293     extensionBehavior[E_GL_ANDROID_extension_pack_es31a]             = EBhDisable;
294     extensionBehavior[E_GL_KHR_blend_equation_advanced]              = EBhDisable;
295     extensionBehavior[E_GL_OES_sample_variables]                     = EBhDisable;
296     extensionBehavior[E_GL_OES_shader_image_atomic]                  = EBhDisable;
297     extensionBehavior[E_GL_OES_shader_multisample_interpolation]     = EBhDisable;
298     extensionBehavior[E_GL_OES_texture_storage_multisample_2d_array] = EBhDisable;
299     extensionBehavior[E_GL_EXT_geometry_shader]                      = EBhDisable;
300     extensionBehavior[E_GL_EXT_geometry_point_size]                  = EBhDisable;
301     extensionBehavior[E_GL_EXT_gpu_shader5]                          = EBhDisable;
302     extensionBehavior[E_GL_EXT_primitive_bounding_box]               = EBhDisable;
303     extensionBehavior[E_GL_EXT_shader_io_blocks]                     = EBhDisable;
304     extensionBehavior[E_GL_EXT_tessellation_shader]                  = EBhDisable;
305     extensionBehavior[E_GL_EXT_tessellation_point_size]              = EBhDisable;
306     extensionBehavior[E_GL_EXT_texture_buffer]                       = EBhDisable;
307     extensionBehavior[E_GL_EXT_texture_cube_map_array]               = EBhDisable;
308 
309     // OES matching AEP
310     extensionBehavior[E_GL_OES_geometry_shader]          = EBhDisable;
311     extensionBehavior[E_GL_OES_geometry_point_size]      = EBhDisable;
312     extensionBehavior[E_GL_OES_gpu_shader5]              = EBhDisable;
313     extensionBehavior[E_GL_OES_primitive_bounding_box]   = EBhDisable;
314     extensionBehavior[E_GL_OES_shader_io_blocks]         = EBhDisable;
315     extensionBehavior[E_GL_OES_tessellation_shader]      = EBhDisable;
316     extensionBehavior[E_GL_OES_tessellation_point_size]  = EBhDisable;
317     extensionBehavior[E_GL_OES_texture_buffer]           = EBhDisable;
318     extensionBehavior[E_GL_OES_texture_cube_map_array]   = EBhDisable;
319     extensionBehavior[E_GL_EXT_shader_integer_mix]       = EBhDisable;
320 
321     // EXT extensions
322     extensionBehavior[E_GL_EXT_device_group]                = EBhDisable;
323     extensionBehavior[E_GL_EXT_multiview]                   = EBhDisable;
324     extensionBehavior[E_GL_EXT_shader_realtime_clock]       = EBhDisable;
325     extensionBehavior[E_GL_EXT_ray_tracing]                 = EBhDisable;
326     extensionBehavior[E_GL_EXT_ray_query]                   = EBhDisable;
327     extensionBehavior[E_GL_EXT_ray_flags_primitive_culling] = EBhDisable;
328     extensionBehavior[E_GL_EXT_blend_func_extended]         = EBhDisable;
329     extensionBehavior[E_GL_EXT_shader_implicit_conversions] = EBhDisable;
330     extensionBehavior[E_GL_EXT_fragment_shading_rate]       = EBhDisable;
331 
332     // OVR extensions
333     extensionBehavior[E_GL_OVR_multiview]                = EBhDisable;
334     extensionBehavior[E_GL_OVR_multiview2]               = EBhDisable;
335 
336     // explicit types
337     extensionBehavior[E_GL_EXT_shader_explicit_arithmetic_types]         = EBhDisable;
338     extensionBehavior[E_GL_EXT_shader_explicit_arithmetic_types_int8]    = EBhDisable;
339     extensionBehavior[E_GL_EXT_shader_explicit_arithmetic_types_int16]   = EBhDisable;
340     extensionBehavior[E_GL_EXT_shader_explicit_arithmetic_types_int32]   = EBhDisable;
341     extensionBehavior[E_GL_EXT_shader_explicit_arithmetic_types_int64]   = EBhDisable;
342     extensionBehavior[E_GL_EXT_shader_explicit_arithmetic_types_float16] = EBhDisable;
343     extensionBehavior[E_GL_EXT_shader_explicit_arithmetic_types_float32] = EBhDisable;
344     extensionBehavior[E_GL_EXT_shader_explicit_arithmetic_types_float64] = EBhDisable;
345 
346     // subgroup extended types
347     extensionBehavior[E_GL_EXT_shader_subgroup_extended_types_int8]    = EBhDisable;
348     extensionBehavior[E_GL_EXT_shader_subgroup_extended_types_int16]   = EBhDisable;
349     extensionBehavior[E_GL_EXT_shader_subgroup_extended_types_int64]   = EBhDisable;
350     extensionBehavior[E_GL_EXT_shader_subgroup_extended_types_float16] = EBhDisable;
351     extensionBehavior[E_GL_EXT_shader_atomic_float]                    = EBhDisable;
352 }
353 
354 #endif // GLSLANG_WEB
355 
356 // Get code that is not part of a shared symbol table, is specific to this shader,
357 // or needed by the preprocessor (which does not use a shared symbol table).
getPreamble(std::string & preamble)358 void TParseVersions::getPreamble(std::string& preamble)
359 {
360     if (isEsProfile()) {
361         preamble =
362             "#define GL_ES 1\n"
363             "#define GL_FRAGMENT_PRECISION_HIGH 1\n"
364 #ifdef GLSLANG_WEB
365             ;
366 #else
367             "#define GL_OES_texture_3D 1\n"
368             "#define GL_OES_standard_derivatives 1\n"
369             "#define GL_EXT_frag_depth 1\n"
370             "#define GL_OES_EGL_image_external 1\n"
371             "#define GL_OES_EGL_image_external_essl3 1\n"
372             "#define GL_EXT_YUV_target 1\n"
373             "#define GL_EXT_shader_texture_lod 1\n"
374             "#define GL_EXT_shadow_samplers 1\n"
375             "#define GL_EXT_fragment_shading_rate 1\n"
376 
377             // AEP
378             "#define GL_ANDROID_extension_pack_es31a 1\n"
379             "#define GL_OES_sample_variables 1\n"
380             "#define GL_OES_shader_image_atomic 1\n"
381             "#define GL_OES_shader_multisample_interpolation 1\n"
382             "#define GL_OES_texture_storage_multisample_2d_array 1\n"
383             "#define GL_EXT_geometry_shader 1\n"
384             "#define GL_EXT_geometry_point_size 1\n"
385             "#define GL_EXT_gpu_shader5 1\n"
386             "#define GL_EXT_primitive_bounding_box 1\n"
387             "#define GL_EXT_shader_io_blocks 1\n"
388             "#define GL_EXT_tessellation_shader 1\n"
389             "#define GL_EXT_tessellation_point_size 1\n"
390             "#define GL_EXT_texture_buffer 1\n"
391             "#define GL_EXT_texture_cube_map_array 1\n"
392             "#define GL_EXT_shader_implicit_conversions 1\n"
393             "#define GL_EXT_shader_integer_mix 1\n"
394             "#define GL_EXT_blend_func_extended 1\n"
395 
396             // OES matching AEP
397             "#define GL_OES_geometry_shader 1\n"
398             "#define GL_OES_geometry_point_size 1\n"
399             "#define GL_OES_gpu_shader5 1\n"
400             "#define GL_OES_primitive_bounding_box 1\n"
401             "#define GL_OES_shader_io_blocks 1\n"
402             "#define GL_OES_tessellation_shader 1\n"
403             "#define GL_OES_tessellation_point_size 1\n"
404             "#define GL_OES_texture_buffer 1\n"
405             "#define GL_OES_texture_cube_map_array 1\n"
406             "#define GL_EXT_shader_non_constant_global_initializers 1\n"
407             ;
408 
409             if (isEsProfile() && version >= 300) {
410                 preamble += "#define GL_NV_shader_noperspective_interpolation 1\n";
411             }
412 
413     } else {
414         preamble =
415             "#define GL_FRAGMENT_PRECISION_HIGH 1\n"
416             "#define GL_ARB_texture_rectangle 1\n"
417             "#define GL_ARB_shading_language_420pack 1\n"
418             "#define GL_ARB_texture_gather 1\n"
419             "#define GL_ARB_gpu_shader5 1\n"
420             "#define GL_ARB_separate_shader_objects 1\n"
421             "#define GL_ARB_compute_shader 1\n"
422             "#define GL_ARB_tessellation_shader 1\n"
423             "#define GL_ARB_enhanced_layouts 1\n"
424             "#define GL_ARB_texture_cube_map_array 1\n"
425             "#define GL_ARB_texture_multisample 1\n"
426             "#define GL_ARB_shader_texture_lod 1\n"
427             "#define GL_ARB_explicit_attrib_location 1\n"
428             "#define GL_ARB_explicit_uniform_location 1\n"
429             "#define GL_ARB_shader_image_load_store 1\n"
430             "#define GL_ARB_shader_atomic_counters 1\n"
431             "#define GL_ARB_shader_draw_parameters 1\n"
432             "#define GL_ARB_shader_group_vote 1\n"
433             "#define GL_ARB_derivative_control 1\n"
434             "#define GL_ARB_shader_texture_image_samples 1\n"
435             "#define GL_ARB_viewport_array 1\n"
436             "#define GL_ARB_gpu_shader_int64 1\n"
437             "#define GL_ARB_gpu_shader_fp64 1\n"
438             "#define GL_ARB_shader_ballot 1\n"
439             "#define GL_ARB_sparse_texture2 1\n"
440             "#define GL_ARB_sparse_texture_clamp 1\n"
441             "#define GL_ARB_shader_stencil_export 1\n"
442             "#define GL_ARB_sample_shading 1\n"
443             "#define GL_ARB_shader_image_size 1\n"
444             "#define GL_ARB_shading_language_packing 1\n"
445 //            "#define GL_ARB_cull_distance 1\n"    // present for 4.5, but need extension control over block members
446             "#define GL_ARB_post_depth_coverage 1\n"
447             "#define GL_ARB_fragment_shader_interlock 1\n"
448             "#define GL_ARB_uniform_buffer_object 1\n"
449             "#define GL_ARB_shader_bit_encoding 1\n"
450             "#define GL_ARB_shader_storage_buffer_object 1\n"
451             "#define GL_ARB_texture_query_lod 1\n"
452             "#define GL_ARB_vertex_attrib_64bit 1\n"
453             "#define GL_EXT_shader_non_constant_global_initializers 1\n"
454             "#define GL_EXT_shader_image_load_formatted 1\n"
455             "#define GL_EXT_post_depth_coverage 1\n"
456             "#define GL_EXT_control_flow_attributes 1\n"
457             "#define GL_EXT_nonuniform_qualifier 1\n"
458             "#define GL_EXT_shader_16bit_storage 1\n"
459             "#define GL_EXT_shader_8bit_storage 1\n"
460             "#define GL_EXT_samplerless_texture_functions 1\n"
461             "#define GL_EXT_scalar_block_layout 1\n"
462             "#define GL_EXT_fragment_invocation_density 1\n"
463             "#define GL_EXT_buffer_reference 1\n"
464             "#define GL_EXT_buffer_reference2 1\n"
465             "#define GL_EXT_buffer_reference_uvec2 1\n"
466             "#define GL_EXT_demote_to_helper_invocation 1\n"
467             "#define GL_EXT_debug_printf 1\n"
468             "#define GL_EXT_fragment_shading_rate 1\n"
469 
470             // GL_KHR_shader_subgroup
471             "#define GL_KHR_shader_subgroup_basic 1\n"
472             "#define GL_KHR_shader_subgroup_vote 1\n"
473             "#define GL_KHR_shader_subgroup_arithmetic 1\n"
474             "#define GL_KHR_shader_subgroup_ballot 1\n"
475             "#define GL_KHR_shader_subgroup_shuffle 1\n"
476             "#define GL_KHR_shader_subgroup_shuffle_relative 1\n"
477             "#define GL_KHR_shader_subgroup_clustered 1\n"
478             "#define GL_KHR_shader_subgroup_quad 1\n"
479 
480             "#define GL_EXT_shader_atomic_int64 1\n"
481             "#define GL_EXT_shader_realtime_clock 1\n"
482             "#define GL_EXT_ray_tracing 1\n"
483             "#define GL_EXT_ray_query 1\n"
484             "#define GL_EXT_ray_flags_primitive_culling 1\n"
485 
486             "#define GL_AMD_shader_ballot 1\n"
487             "#define GL_AMD_shader_trinary_minmax 1\n"
488             "#define GL_AMD_shader_explicit_vertex_parameter 1\n"
489             "#define GL_AMD_gcn_shader 1\n"
490             "#define GL_AMD_gpu_shader_half_float 1\n"
491             "#define GL_AMD_texture_gather_bias_lod 1\n"
492             "#define GL_AMD_gpu_shader_int16 1\n"
493             "#define GL_AMD_shader_image_load_store_lod 1\n"
494             "#define GL_AMD_shader_fragment_mask 1\n"
495             "#define GL_AMD_gpu_shader_half_float_fetch 1\n"
496 
497             "#define GL_INTEL_shader_integer_functions2 1\n"
498 
499             "#define GL_NV_sample_mask_override_coverage 1\n"
500             "#define GL_NV_geometry_shader_passthrough 1\n"
501             "#define GL_NV_viewport_array2 1\n"
502             "#define GL_NV_shader_atomic_int64 1\n"
503             "#define GL_NV_conservative_raster_underestimation 1\n"
504             "#define GL_NV_shader_subgroup_partitioned 1\n"
505             "#define GL_NV_shading_rate_image 1\n"
506             "#define GL_NV_ray_tracing 1\n"
507             "#define GL_NV_fragment_shader_barycentric 1\n"
508             "#define GL_NV_compute_shader_derivatives 1\n"
509             "#define GL_NV_shader_texture_footprint 1\n"
510             "#define GL_NV_mesh_shader 1\n"
511             "#define GL_NV_cooperative_matrix 1\n"
512             "#define GL_NV_integer_cooperative_matrix 1\n"
513 
514             "#define GL_EXT_shader_explicit_arithmetic_types 1\n"
515             "#define GL_EXT_shader_explicit_arithmetic_types_int8 1\n"
516             "#define GL_EXT_shader_explicit_arithmetic_types_int16 1\n"
517             "#define GL_EXT_shader_explicit_arithmetic_types_int32 1\n"
518             "#define GL_EXT_shader_explicit_arithmetic_types_int64 1\n"
519             "#define GL_EXT_shader_explicit_arithmetic_types_float16 1\n"
520             "#define GL_EXT_shader_explicit_arithmetic_types_float32 1\n"
521             "#define GL_EXT_shader_explicit_arithmetic_types_float64 1\n"
522 
523             "#define GL_EXT_shader_subgroup_extended_types_int8 1\n"
524             "#define GL_EXT_shader_subgroup_extended_types_int16 1\n"
525             "#define GL_EXT_shader_subgroup_extended_types_int64 1\n"
526             "#define GL_EXT_shader_subgroup_extended_types_float16 1\n"
527 
528             "#define GL_EXT_shader_atomic_float 1\n"
529             ;
530 
531         if (version >= 150) {
532             // define GL_core_profile and GL_compatibility_profile
533             preamble += "#define GL_core_profile 1\n";
534 
535             if (profile == ECompatibilityProfile)
536                 preamble += "#define GL_compatibility_profile 1\n";
537         }
538 #endif // GLSLANG_WEB
539     }
540 
541 #ifndef GLSLANG_WEB
542     if ((!isEsProfile() && version >= 140) ||
543         (isEsProfile() && version >= 310)) {
544         preamble +=
545             "#define GL_EXT_device_group 1\n"
546             "#define GL_EXT_multiview 1\n"
547             "#define GL_NV_shader_sm_builtins 1\n"
548             ;
549     }
550 
551     if (version >= 300 /* both ES and non-ES */) {
552         preamble +=
553             "#define GL_OVR_multiview 1\n"
554             "#define GL_OVR_multiview2 1\n"
555             ;
556     }
557 
558     // #line and #include
559     preamble +=
560             "#define GL_GOOGLE_cpp_style_line_directive 1\n"
561             "#define GL_GOOGLE_include_directive 1\n"
562             "#define GL_KHR_blend_equation_advanced 1\n"
563             ;
564 #endif
565 
566     // #define VULKAN XXXX
567     const int numberBufSize = 12;
568     char numberBuf[numberBufSize];
569     if (spvVersion.vulkanGlsl > 0) {
570         preamble += "#define VULKAN ";
571         snprintf(numberBuf, numberBufSize, "%d", spvVersion.vulkanGlsl);
572         preamble += numberBuf;
573         preamble += "\n";
574     }
575 
576 #ifndef GLSLANG_WEB
577     // #define GL_SPIRV XXXX
578     if (spvVersion.openGl > 0) {
579         preamble += "#define GL_SPIRV ";
580         snprintf(numberBuf, numberBufSize, "%d", spvVersion.openGl);
581         preamble += numberBuf;
582         preamble += "\n";
583     }
584 #endif
585 }
586 
587 //
588 // Map from stage enum to externally readable text name.
589 //
StageName(EShLanguage stage)590 const char* StageName(EShLanguage stage)
591 {
592     switch(stage) {
593     case EShLangVertex:         return "vertex";
594     case EShLangFragment:       return "fragment";
595     case EShLangCompute:        return "compute";
596 #ifndef GLSLANG_WEB
597     case EShLangTessControl:    return "tessellation control";
598     case EShLangTessEvaluation: return "tessellation evaluation";
599     case EShLangGeometry:       return "geometry";
600     case EShLangRayGen:         return "ray-generation";
601     case EShLangIntersect:      return "intersection";
602     case EShLangAnyHit:         return "any-hit";
603     case EShLangClosestHit:     return "closest-hit";
604     case EShLangMiss:           return "miss";
605     case EShLangCallable:       return "callable";
606     case EShLangMeshNV:         return "mesh";
607     case EShLangTaskNV:         return "task";
608 #endif
609     default:                    return "unknown stage";
610     }
611 }
612 
613 //
614 // When to use requireStage()
615 //
616 //     If only some stages support a feature.
617 //
618 // Operation: If the current stage is not present, give an error message.
619 //
requireStage(const TSourceLoc & loc,EShLanguageMask languageMask,const char * featureDesc)620 void TParseVersions::requireStage(const TSourceLoc& loc, EShLanguageMask languageMask, const char* featureDesc)
621 {
622     if (((1 << language) & languageMask) == 0)
623         error(loc, "not supported in this stage:", featureDesc, StageName(language));
624 }
625 
626 // If only one stage supports a feature, this can be called.  But, all supporting stages
627 // must be specified with one call.
requireStage(const TSourceLoc & loc,EShLanguage stage,const char * featureDesc)628 void TParseVersions::requireStage(const TSourceLoc& loc, EShLanguage stage, const char* featureDesc)
629 {
630     requireStage(loc, static_cast<EShLanguageMask>(1 << stage), featureDesc);
631 }
632 
633 #ifndef GLSLANG_WEB
634 //
635 // When to use requireProfile():
636 //
637 //     Use if only some profiles support a feature.  However, if within a profile the feature
638 //     is version or extension specific, follow this call with calls to profileRequires().
639 //
640 // Operation:  If the current profile is not one of the profileMask,
641 // give an error message.
642 //
requireProfile(const TSourceLoc & loc,int profileMask,const char * featureDesc)643 void TParseVersions::requireProfile(const TSourceLoc& loc, int profileMask, const char* featureDesc)
644 {
645     if (! (profile & profileMask))
646         error(loc, "not supported with this profile:", featureDesc, ProfileName(profile));
647 }
648 
649 //
650 // When to use profileRequires():
651 //
652 //     If a set of profiles have the same requirements for what version or extensions
653 //     are needed to support a feature.
654 //
655 //     It must be called for each profile that needs protection.  Use requireProfile() first
656 //     to reduce that set of profiles.
657 //
658 // Operation: Will issue warnings/errors based on the current profile, version, and extension
659 // behaviors.  It only checks extensions when the current profile is one of the profileMask.
660 //
661 // A minVersion of 0 means no version of the profileMask support this in core,
662 // the extension must be present.
663 //
664 
665 // entry point that takes multiple extensions
profileRequires(const TSourceLoc & loc,int profileMask,int minVersion,int numExtensions,const char * const extensions[],const char * featureDesc)666 void TParseVersions::profileRequires(const TSourceLoc& loc, int profileMask, int minVersion, int numExtensions,
667     const char* const extensions[], const char* featureDesc)
668 {
669     if (profile & profileMask) {
670         bool okay = minVersion > 0 && version >= minVersion;
671 #ifndef GLSLANG_WEB
672         for (int i = 0; i < numExtensions; ++i) {
673             switch (getExtensionBehavior(extensions[i])) {
674             case EBhWarn:
675                 infoSink.info.message(EPrefixWarning, ("extension " + TString(extensions[i]) + " is being used for " + featureDesc).c_str(), loc);
676                 // fall through
677             case EBhRequire:
678             case EBhEnable:
679                 okay = true;
680                 break;
681             default: break; // some compilers want this
682             }
683         }
684 #endif
685         if (! okay)
686             error(loc, "not supported for this version or the enabled extensions", featureDesc, "");
687     }
688 }
689 
690 // entry point for the above that takes a single extension
profileRequires(const TSourceLoc & loc,int profileMask,int minVersion,const char * extension,const char * featureDesc)691 void TParseVersions::profileRequires(const TSourceLoc& loc, int profileMask, int minVersion, const char* extension,
692     const char* featureDesc)
693 {
694     profileRequires(loc, profileMask, minVersion, extension ? 1 : 0, &extension, featureDesc);
695 }
696 
unimplemented(const TSourceLoc & loc,const char * featureDesc)697 void TParseVersions::unimplemented(const TSourceLoc& loc, const char* featureDesc)
698 {
699     error(loc, "feature not yet implemented", featureDesc, "");
700 }
701 
702 //
703 // Within a set of profiles, see if a feature is deprecated and give an error or warning based on whether
704 // a future compatibility context is being use.
705 //
checkDeprecated(const TSourceLoc & loc,int profileMask,int depVersion,const char * featureDesc)706 void TParseVersions::checkDeprecated(const TSourceLoc& loc, int profileMask, int depVersion, const char* featureDesc)
707 {
708     if (profile & profileMask) {
709         if (version >= depVersion) {
710             if (forwardCompatible)
711                 error(loc, "deprecated, may be removed in future release", featureDesc, "");
712             else if (! suppressWarnings())
713                 infoSink.info.message(EPrefixWarning, (TString(featureDesc) + " deprecated in version " +
714                                                        String(depVersion) + "; may be removed in future release").c_str(), loc);
715         }
716     }
717 }
718 
719 //
720 // Within a set of profiles, see if a feature has now been removed and if so, give an error.
721 // The version argument is the first version no longer having the feature.
722 //
requireNotRemoved(const TSourceLoc & loc,int profileMask,int removedVersion,const char * featureDesc)723 void TParseVersions::requireNotRemoved(const TSourceLoc& loc, int profileMask, int removedVersion, const char* featureDesc)
724 {
725     if (profile & profileMask) {
726         if (version >= removedVersion) {
727             const int maxSize = 60;
728             char buf[maxSize];
729             snprintf(buf, maxSize, "%s profile; removed in version %d", ProfileName(profile), removedVersion);
730             error(loc, "no longer supported in", featureDesc, buf);
731         }
732     }
733 }
734 
735 // Returns true if at least one of the extensions in the extensions parameter is requested. Otherwise, returns false.
736 // Warns appropriately if the requested behavior of an extension is "warn".
checkExtensionsRequested(const TSourceLoc & loc,int numExtensions,const char * const extensions[],const char * featureDesc)737 bool TParseVersions::checkExtensionsRequested(const TSourceLoc& loc, int numExtensions, const char* const extensions[], const char* featureDesc)
738 {
739     // First, see if any of the extensions are enabled
740     for (int i = 0; i < numExtensions; ++i) {
741         TExtensionBehavior behavior = getExtensionBehavior(extensions[i]);
742         if (behavior == EBhEnable || behavior == EBhRequire)
743             return true;
744     }
745 
746     // See if any extensions want to give a warning on use; give warnings for all such extensions
747     bool warned = false;
748     for (int i = 0; i < numExtensions; ++i) {
749         TExtensionBehavior behavior = getExtensionBehavior(extensions[i]);
750         if (behavior == EBhDisable && relaxedErrors()) {
751             infoSink.info.message(EPrefixWarning, "The following extension must be enabled to use this feature:", loc);
752             behavior = EBhWarn;
753         }
754         if (behavior == EBhWarn) {
755             infoSink.info.message(EPrefixWarning, ("extension " + TString(extensions[i]) + " is being used for " + featureDesc).c_str(), loc);
756             warned = true;
757         }
758     }
759     if (warned)
760         return true;
761     return false;
762 }
763 
764 //
765 // Use when there are no profile/version to check, it's just an error if one of the
766 // extensions is not present.
767 //
requireExtensions(const TSourceLoc & loc,int numExtensions,const char * const extensions[],const char * featureDesc)768 void TParseVersions::requireExtensions(const TSourceLoc& loc, int numExtensions, const char* const extensions[],
769     const char* featureDesc)
770 {
771     if (checkExtensionsRequested(loc, numExtensions, extensions, featureDesc))
772         return;
773 
774     // If we get this far, give errors explaining what extensions are needed
775     if (numExtensions == 1)
776         error(loc, "required extension not requested:", featureDesc, extensions[0]);
777     else {
778         error(loc, "required extension not requested:", featureDesc, "Possible extensions include:");
779         for (int i = 0; i < numExtensions; ++i)
780             infoSink.info.message(EPrefixNone, extensions[i]);
781     }
782 }
783 
784 //
785 // Use by preprocessor when there are no profile/version to check, it's just an error if one of the
786 // extensions is not present.
787 //
ppRequireExtensions(const TSourceLoc & loc,int numExtensions,const char * const extensions[],const char * featureDesc)788 void TParseVersions::ppRequireExtensions(const TSourceLoc& loc, int numExtensions, const char* const extensions[],
789     const char* featureDesc)
790 {
791     if (checkExtensionsRequested(loc, numExtensions, extensions, featureDesc))
792         return;
793 
794     // If we get this far, give errors explaining what extensions are needed
795     if (numExtensions == 1)
796         ppError(loc, "required extension not requested:", featureDesc, extensions[0]);
797     else {
798         ppError(loc, "required extension not requested:", featureDesc, "Possible extensions include:");
799         for (int i = 0; i < numExtensions; ++i)
800             infoSink.info.message(EPrefixNone, extensions[i]);
801     }
802 }
803 
getExtensionBehavior(const char * extension)804 TExtensionBehavior TParseVersions::getExtensionBehavior(const char* extension)
805 {
806     auto iter = extensionBehavior.find(TString(extension));
807     if (iter == extensionBehavior.end())
808         return EBhMissing;
809     else
810         return iter->second;
811 }
812 
813 // Returns true if the given extension is set to enable, require, or warn.
extensionTurnedOn(const char * const extension)814 bool TParseVersions::extensionTurnedOn(const char* const extension)
815 {
816       switch (getExtensionBehavior(extension)) {
817       case EBhEnable:
818       case EBhRequire:
819       case EBhWarn:
820           return true;
821       default:
822           break;
823       }
824       return false;
825 }
826 // See if any of the extensions are set to enable, require, or warn.
extensionsTurnedOn(int numExtensions,const char * const extensions[])827 bool TParseVersions::extensionsTurnedOn(int numExtensions, const char* const extensions[])
828 {
829     for (int i = 0; i < numExtensions; ++i) {
830         if (extensionTurnedOn(extensions[i]))
831             return true;
832     }
833     return false;
834 }
835 
836 //
837 // Change the current state of an extension's behavior.
838 //
updateExtensionBehavior(int line,const char * extension,const char * behaviorString)839 void TParseVersions::updateExtensionBehavior(int line, const char* extension, const char* behaviorString)
840 {
841     // Translate from text string of extension's behavior to an enum.
842     TExtensionBehavior behavior = EBhDisable;
843     if (! strcmp("require", behaviorString))
844         behavior = EBhRequire;
845     else if (! strcmp("enable", behaviorString))
846         behavior = EBhEnable;
847     else if (! strcmp("disable", behaviorString))
848         behavior = EBhDisable;
849     else if (! strcmp("warn", behaviorString))
850         behavior = EBhWarn;
851     else {
852         error(getCurrentLoc(), "behavior not supported:", "#extension", behaviorString);
853         return;
854     }
855     bool on = behavior != EBhDisable;
856 
857     // check if extension is used with correct shader stage
858     checkExtensionStage(getCurrentLoc(), extension);
859 
860     // check if extension has additional requirements
861     extensionRequires(getCurrentLoc(), extension ,behaviorString);
862 
863     // update the requested extension
864     updateExtensionBehavior(extension, behavior);
865 
866     // see if need to propagate to implicitly modified things
867     if (strcmp(extension, "GL_ANDROID_extension_pack_es31a") == 0) {
868         // to everything in AEP
869         updateExtensionBehavior(line, "GL_KHR_blend_equation_advanced", behaviorString);
870         updateExtensionBehavior(line, "GL_OES_sample_variables", behaviorString);
871         updateExtensionBehavior(line, "GL_OES_shader_image_atomic", behaviorString);
872         updateExtensionBehavior(line, "GL_OES_shader_multisample_interpolation", behaviorString);
873         updateExtensionBehavior(line, "GL_OES_texture_storage_multisample_2d_array", behaviorString);
874         updateExtensionBehavior(line, "GL_EXT_geometry_shader", behaviorString);
875         updateExtensionBehavior(line, "GL_EXT_gpu_shader5", behaviorString);
876         updateExtensionBehavior(line, "GL_EXT_primitive_bounding_box", behaviorString);
877         updateExtensionBehavior(line, "GL_EXT_shader_io_blocks", behaviorString);
878         updateExtensionBehavior(line, "GL_EXT_tessellation_shader", behaviorString);
879         updateExtensionBehavior(line, "GL_EXT_texture_buffer", behaviorString);
880         updateExtensionBehavior(line, "GL_EXT_texture_cube_map_array", behaviorString);
881     }
882     // geometry to io_blocks
883     else if (strcmp(extension, "GL_EXT_geometry_shader") == 0)
884         updateExtensionBehavior(line, "GL_EXT_shader_io_blocks", behaviorString);
885     else if (strcmp(extension, "GL_OES_geometry_shader") == 0)
886         updateExtensionBehavior(line, "GL_OES_shader_io_blocks", behaviorString);
887     // tessellation to io_blocks
888     else if (strcmp(extension, "GL_EXT_tessellation_shader") == 0)
889         updateExtensionBehavior(line, "GL_EXT_shader_io_blocks", behaviorString);
890     else if (strcmp(extension, "GL_OES_tessellation_shader") == 0)
891         updateExtensionBehavior(line, "GL_OES_shader_io_blocks", behaviorString);
892     else if (strcmp(extension, "GL_GOOGLE_include_directive") == 0)
893         updateExtensionBehavior(line, "GL_GOOGLE_cpp_style_line_directive", behaviorString);
894     // subgroup_* to subgroup_basic
895     else if (strcmp(extension, "GL_KHR_shader_subgroup_vote") == 0)
896         updateExtensionBehavior(line, "GL_KHR_shader_subgroup_basic", behaviorString);
897     else if (strcmp(extension, "GL_KHR_shader_subgroup_arithmetic") == 0)
898         updateExtensionBehavior(line, "GL_KHR_shader_subgroup_basic", behaviorString);
899     else if (strcmp(extension, "GL_KHR_shader_subgroup_ballot") == 0)
900         updateExtensionBehavior(line, "GL_KHR_shader_subgroup_basic", behaviorString);
901     else if (strcmp(extension, "GL_KHR_shader_subgroup_shuffle") == 0)
902         updateExtensionBehavior(line, "GL_KHR_shader_subgroup_basic", behaviorString);
903     else if (strcmp(extension, "GL_KHR_shader_subgroup_shuffle_relative") == 0)
904         updateExtensionBehavior(line, "GL_KHR_shader_subgroup_basic", behaviorString);
905     else if (strcmp(extension, "GL_KHR_shader_subgroup_clustered") == 0)
906         updateExtensionBehavior(line, "GL_KHR_shader_subgroup_basic", behaviorString);
907     else if (strcmp(extension, "GL_KHR_shader_subgroup_quad") == 0)
908         updateExtensionBehavior(line, "GL_KHR_shader_subgroup_basic", behaviorString);
909     else if (strcmp(extension, "GL_NV_shader_subgroup_partitioned") == 0)
910         updateExtensionBehavior(line, "GL_KHR_shader_subgroup_basic", behaviorString);
911     else if (strcmp(extension, "GL_EXT_buffer_reference2") == 0 ||
912              strcmp(extension, "GL_EXT_buffer_reference_uvec2") == 0)
913         updateExtensionBehavior(line, "GL_EXT_buffer_reference", behaviorString);
914     else if (strcmp(extension, "GL_NV_integer_cooperative_matrix") == 0)
915         updateExtensionBehavior(line, "GL_NV_cooperative_matrix", behaviorString);
916     // subgroup extended types to explicit types
917     else if (strcmp(extension, "GL_EXT_shader_subgroup_extended_types_int8") == 0)
918         updateExtensionBehavior(line, "GL_EXT_shader_explicit_arithmetic_types_int8", behaviorString);
919     else if (strcmp(extension, "GL_EXT_shader_subgroup_extended_types_int16") == 0)
920         updateExtensionBehavior(line, "GL_EXT_shader_explicit_arithmetic_types_int16", behaviorString);
921     else if (strcmp(extension, "GL_EXT_shader_subgroup_extended_types_int64") == 0)
922         updateExtensionBehavior(line, "GL_EXT_shader_explicit_arithmetic_types_int64", behaviorString);
923     else if (strcmp(extension, "GL_EXT_shader_subgroup_extended_types_float16") == 0)
924         updateExtensionBehavior(line, "GL_EXT_shader_explicit_arithmetic_types_float16", behaviorString);
925 
926     // see if we need to update the numeric features
927     else if (strcmp(extension, "GL_EXT_shader_explicit_arithmetic_types") == 0)
928         intermediate.updateNumericFeature(TNumericFeatures::shader_explicit_arithmetic_types, on);
929     else if (strcmp(extension, "GL_EXT_shader_explicit_arithmetic_types_int8") == 0)
930         intermediate.updateNumericFeature(TNumericFeatures::shader_explicit_arithmetic_types_int8, on);
931     else if (strcmp(extension, "GL_EXT_shader_explicit_arithmetic_types_int16") == 0)
932         intermediate.updateNumericFeature(TNumericFeatures::shader_explicit_arithmetic_types_int16, on);
933     else if (strcmp(extension, "GL_EXT_shader_explicit_arithmetic_types_int32") == 0)
934         intermediate.updateNumericFeature(TNumericFeatures::shader_explicit_arithmetic_types_int32, on);
935     else if (strcmp(extension, "GL_EXT_shader_explicit_arithmetic_types_int64") == 0)
936         intermediate.updateNumericFeature(TNumericFeatures::shader_explicit_arithmetic_types_int64, on);
937     else if (strcmp(extension, "GL_EXT_shader_explicit_arithmetic_types_float16") == 0)
938         intermediate.updateNumericFeature(TNumericFeatures::shader_explicit_arithmetic_types_float16, on);
939     else if (strcmp(extension, "GL_EXT_shader_explicit_arithmetic_types_float32") == 0)
940         intermediate.updateNumericFeature(TNumericFeatures::shader_explicit_arithmetic_types_float32, on);
941     else if (strcmp(extension, "GL_EXT_shader_explicit_arithmetic_types_float64") == 0)
942         intermediate.updateNumericFeature(TNumericFeatures::shader_explicit_arithmetic_types_float64, on);
943     else if (strcmp(extension, "GL_EXT_shader_implicit_conversions") == 0)
944         intermediate.updateNumericFeature(TNumericFeatures::shader_implicit_conversions, on);
945     else if (strcmp(extension, "GL_ARB_gpu_shader_fp64") == 0)
946         intermediate.updateNumericFeature(TNumericFeatures::gpu_shader_fp64, on);
947     else if (strcmp(extension, "GL_AMD_gpu_shader_int16") == 0)
948         intermediate.updateNumericFeature(TNumericFeatures::gpu_shader_int16, on);
949     else if (strcmp(extension, "GL_AMD_gpu_shader_half_float") == 0)
950         intermediate.updateNumericFeature(TNumericFeatures::gpu_shader_half_float, on);
951 }
952 
updateExtensionBehavior(const char * extension,TExtensionBehavior behavior)953 void TParseVersions::updateExtensionBehavior(const char* extension, TExtensionBehavior behavior)
954 {
955     // Update the current behavior
956     if (strcmp(extension, "all") == 0) {
957         // special case for the 'all' extension; apply it to every extension present
958         if (behavior == EBhRequire || behavior == EBhEnable) {
959             error(getCurrentLoc(), "extension 'all' cannot have 'require' or 'enable' behavior", "#extension", "");
960             return;
961         } else {
962             for (auto iter = extensionBehavior.begin(); iter != extensionBehavior.end(); ++iter)
963                 iter->second = behavior;
964         }
965     } else {
966         // Do the update for this single extension
967         auto iter = extensionBehavior.find(TString(extension));
968         if (iter == extensionBehavior.end()) {
969             switch (behavior) {
970             case EBhRequire:
971                 error(getCurrentLoc(), "extension not supported:", "#extension", extension);
972                 break;
973             case EBhEnable:
974             case EBhWarn:
975             case EBhDisable:
976                 warn(getCurrentLoc(), "extension not supported:", "#extension", extension);
977                 break;
978             default:
979                 assert(0 && "unexpected behavior");
980             }
981 
982             return;
983         } else {
984             if (iter->second == EBhDisablePartial)
985                 warn(getCurrentLoc(), "extension is only partially supported:", "#extension", extension);
986             if (behavior != EBhDisable)
987                 intermediate.addRequestedExtension(extension);
988             iter->second = behavior;
989         }
990     }
991 }
992 
993 // Check if extension is used with correct shader stage.
checkExtensionStage(const TSourceLoc & loc,const char * const extension)994 void TParseVersions::checkExtensionStage(const TSourceLoc& loc, const char * const extension)
995 {
996     // GL_NV_mesh_shader extension is only allowed in task/mesh shaders
997     if (strcmp(extension, "GL_NV_mesh_shader") == 0) {
998         requireStage(loc, (EShLanguageMask)(EShLangTaskNVMask | EShLangMeshNVMask | EShLangFragmentMask),
999                      "#extension GL_NV_mesh_shader");
1000         profileRequires(loc, ECoreProfile, 450, 0, "#extension GL_NV_mesh_shader");
1001         profileRequires(loc, EEsProfile, 320, 0, "#extension GL_NV_mesh_shader");
1002     }
1003 }
1004 
1005 // Check if extension has additional requirements
extensionRequires(const TSourceLoc & loc,const char * const extension,const char * behaviorString)1006 void TParseVersions::extensionRequires(const TSourceLoc &loc, const char * const extension, const char *behaviorString)
1007 {
1008     bool isEnabled = false;
1009     if (!strcmp("require", behaviorString))
1010         isEnabled = true;
1011     else if (!strcmp("enable", behaviorString))
1012         isEnabled = true;
1013 
1014     if (isEnabled) {
1015         unsigned int minSpvVersion = 0;
1016         auto iter = extensionMinSpv.find(TString(extension));
1017         if (iter != extensionMinSpv.end())
1018             minSpvVersion = iter->second;
1019         requireSpv(loc, extension, minSpvVersion);
1020     }
1021 }
1022 
1023 // Call for any operation needing full GLSL integer data-type support.
fullIntegerCheck(const TSourceLoc & loc,const char * op)1024 void TParseVersions::fullIntegerCheck(const TSourceLoc& loc, const char* op)
1025 {
1026     profileRequires(loc, ENoProfile, 130, nullptr, op);
1027     profileRequires(loc, EEsProfile, 300, nullptr, op);
1028 }
1029 
1030 // Call for any operation needing GLSL double data-type support.
doubleCheck(const TSourceLoc & loc,const char * op)1031 void TParseVersions::doubleCheck(const TSourceLoc& loc, const char* op)
1032 {
1033 
1034     //requireProfile(loc, ECoreProfile | ECompatibilityProfile, op);
1035     if (language == EShLangVertex) {
1036         const char* const f64_Extensions[] = {E_GL_ARB_gpu_shader_fp64, E_GL_ARB_vertex_attrib_64bit};
1037         profileRequires(loc, ECoreProfile | ECompatibilityProfile, 400, 2, f64_Extensions, op);
1038     } else
1039         profileRequires(loc, ECoreProfile | ECompatibilityProfile, 400, E_GL_ARB_gpu_shader_fp64, op);
1040 }
1041 
1042 // Call for any operation needing GLSL float16 data-type support.
float16Check(const TSourceLoc & loc,const char * op,bool builtIn)1043 void TParseVersions::float16Check(const TSourceLoc& loc, const char* op, bool builtIn)
1044 {
1045     if (!builtIn) {
1046         const char* const extensions[] = {
1047                                            E_GL_AMD_gpu_shader_half_float,
1048                                            E_GL_EXT_shader_explicit_arithmetic_types,
1049                                            E_GL_EXT_shader_explicit_arithmetic_types_float16};
1050         requireExtensions(loc, sizeof(extensions)/sizeof(extensions[0]), extensions, op);
1051     }
1052 }
1053 
float16Arithmetic()1054 bool TParseVersions::float16Arithmetic()
1055 {
1056     const char* const extensions[] = {
1057                                        E_GL_AMD_gpu_shader_half_float,
1058                                        E_GL_EXT_shader_explicit_arithmetic_types,
1059                                        E_GL_EXT_shader_explicit_arithmetic_types_float16};
1060     return extensionsTurnedOn(sizeof(extensions)/sizeof(extensions[0]), extensions);
1061 }
1062 
int16Arithmetic()1063 bool TParseVersions::int16Arithmetic()
1064 {
1065     const char* const extensions[] = {
1066                                        E_GL_AMD_gpu_shader_int16,
1067                                        E_GL_EXT_shader_explicit_arithmetic_types,
1068                                        E_GL_EXT_shader_explicit_arithmetic_types_int16};
1069     return extensionsTurnedOn(sizeof(extensions)/sizeof(extensions[0]), extensions);
1070 }
1071 
int8Arithmetic()1072 bool TParseVersions::int8Arithmetic()
1073 {
1074     const char* const extensions[] = {
1075                                        E_GL_EXT_shader_explicit_arithmetic_types,
1076                                        E_GL_EXT_shader_explicit_arithmetic_types_int8};
1077     return extensionsTurnedOn(sizeof(extensions)/sizeof(extensions[0]), extensions);
1078 }
1079 
requireFloat16Arithmetic(const TSourceLoc & loc,const char * op,const char * featureDesc)1080 void TParseVersions::requireFloat16Arithmetic(const TSourceLoc& loc, const char* op, const char* featureDesc)
1081 {
1082     TString combined;
1083     combined = op;
1084     combined += ": ";
1085     combined += featureDesc;
1086 
1087     const char* const extensions[] = {
1088                                        E_GL_AMD_gpu_shader_half_float,
1089                                        E_GL_EXT_shader_explicit_arithmetic_types,
1090                                        E_GL_EXT_shader_explicit_arithmetic_types_float16};
1091     requireExtensions(loc, sizeof(extensions)/sizeof(extensions[0]), extensions, combined.c_str());
1092 }
1093 
requireInt16Arithmetic(const TSourceLoc & loc,const char * op,const char * featureDesc)1094 void TParseVersions::requireInt16Arithmetic(const TSourceLoc& loc, const char* op, const char* featureDesc)
1095 {
1096     TString combined;
1097     combined = op;
1098     combined += ": ";
1099     combined += featureDesc;
1100 
1101     const char* const extensions[] = {
1102                                        E_GL_AMD_gpu_shader_int16,
1103                                        E_GL_EXT_shader_explicit_arithmetic_types,
1104                                        E_GL_EXT_shader_explicit_arithmetic_types_int16};
1105     requireExtensions(loc, sizeof(extensions)/sizeof(extensions[0]), extensions, combined.c_str());
1106 }
1107 
requireInt8Arithmetic(const TSourceLoc & loc,const char * op,const char * featureDesc)1108 void TParseVersions::requireInt8Arithmetic(const TSourceLoc& loc, const char* op, const char* featureDesc)
1109 {
1110     TString combined;
1111     combined = op;
1112     combined += ": ";
1113     combined += featureDesc;
1114 
1115     const char* const extensions[] = {
1116                                        E_GL_EXT_shader_explicit_arithmetic_types,
1117                                        E_GL_EXT_shader_explicit_arithmetic_types_int8};
1118     requireExtensions(loc, sizeof(extensions)/sizeof(extensions[0]), extensions, combined.c_str());
1119 }
1120 
float16ScalarVectorCheck(const TSourceLoc & loc,const char * op,bool builtIn)1121 void TParseVersions::float16ScalarVectorCheck(const TSourceLoc& loc, const char* op, bool builtIn)
1122 {
1123     if (!builtIn) {
1124         const char* const extensions[] = {
1125                                            E_GL_AMD_gpu_shader_half_float,
1126                                            E_GL_EXT_shader_16bit_storage,
1127                                            E_GL_EXT_shader_explicit_arithmetic_types,
1128                                            E_GL_EXT_shader_explicit_arithmetic_types_float16};
1129         requireExtensions(loc, sizeof(extensions)/sizeof(extensions[0]), extensions, op);
1130     }
1131 }
1132 
1133 // Call for any operation needing GLSL float32 data-type support.
explicitFloat32Check(const TSourceLoc & loc,const char * op,bool builtIn)1134 void TParseVersions::explicitFloat32Check(const TSourceLoc& loc, const char* op, bool builtIn)
1135 {
1136     if (!builtIn) {
1137         const char* const extensions[2] = {E_GL_EXT_shader_explicit_arithmetic_types,
1138                                            E_GL_EXT_shader_explicit_arithmetic_types_float32};
1139         requireExtensions(loc, 2, extensions, op);
1140     }
1141 }
1142 
1143 // Call for any operation needing GLSL float64 data-type support.
explicitFloat64Check(const TSourceLoc & loc,const char * op,bool builtIn)1144 void TParseVersions::explicitFloat64Check(const TSourceLoc& loc, const char* op, bool builtIn)
1145 {
1146     if (!builtIn) {
1147         const char* const extensions[2] = {E_GL_EXT_shader_explicit_arithmetic_types,
1148                                            E_GL_EXT_shader_explicit_arithmetic_types_float64};
1149         requireExtensions(loc, 2, extensions, op);
1150         requireProfile(loc, ECoreProfile | ECompatibilityProfile, op);
1151         profileRequires(loc, ECoreProfile | ECompatibilityProfile, 400, nullptr, op);
1152     }
1153 }
1154 
1155 // Call for any operation needing GLSL explicit int8 data-type support.
explicitInt8Check(const TSourceLoc & loc,const char * op,bool builtIn)1156 void TParseVersions::explicitInt8Check(const TSourceLoc& loc, const char* op, bool builtIn)
1157 {
1158     if (! builtIn) {
1159         const char* const extensions[2] = {E_GL_EXT_shader_explicit_arithmetic_types,
1160                                            E_GL_EXT_shader_explicit_arithmetic_types_int8};
1161         requireExtensions(loc, 2, extensions, op);
1162     }
1163 }
1164 
1165 // Call for any operation needing GLSL float16 opaque-type support
float16OpaqueCheck(const TSourceLoc & loc,const char * op,bool builtIn)1166 void TParseVersions::float16OpaqueCheck(const TSourceLoc& loc, const char* op, bool builtIn)
1167 {
1168     if (! builtIn) {
1169         requireExtensions(loc, 1, &E_GL_AMD_gpu_shader_half_float_fetch, op);
1170         requireProfile(loc, ECoreProfile | ECompatibilityProfile, op);
1171         profileRequires(loc, ECoreProfile | ECompatibilityProfile, 400, nullptr, op);
1172     }
1173 }
1174 
1175 // Call for any operation needing GLSL explicit int16 data-type support.
explicitInt16Check(const TSourceLoc & loc,const char * op,bool builtIn)1176 void TParseVersions::explicitInt16Check(const TSourceLoc& loc, const char* op, bool builtIn)
1177 {
1178     if (! builtIn) {
1179         const char* const extensions[] = {
1180                                            E_GL_AMD_gpu_shader_int16,
1181                                            E_GL_EXT_shader_explicit_arithmetic_types,
1182                                            E_GL_EXT_shader_explicit_arithmetic_types_int16};
1183         requireExtensions(loc, sizeof(extensions)/sizeof(extensions[0]), extensions, op);
1184     }
1185 }
1186 
int16ScalarVectorCheck(const TSourceLoc & loc,const char * op,bool builtIn)1187 void TParseVersions::int16ScalarVectorCheck(const TSourceLoc& loc, const char* op, bool builtIn)
1188 {
1189     if (! builtIn) {
1190     	const char* const extensions[] = {
1191                                            E_GL_AMD_gpu_shader_int16,
1192                                            E_GL_EXT_shader_16bit_storage,
1193                                            E_GL_EXT_shader_explicit_arithmetic_types,
1194                                            E_GL_EXT_shader_explicit_arithmetic_types_int16};
1195         requireExtensions(loc, sizeof(extensions)/sizeof(extensions[0]), extensions, op);
1196     }
1197 }
1198 
int8ScalarVectorCheck(const TSourceLoc & loc,const char * op,bool builtIn)1199 void TParseVersions::int8ScalarVectorCheck(const TSourceLoc& loc, const char* op, bool builtIn)
1200 {
1201     if (! builtIn) {
1202     	const char* const extensions[] = {
1203                                            E_GL_EXT_shader_8bit_storage,
1204                                            E_GL_EXT_shader_explicit_arithmetic_types,
1205                                            E_GL_EXT_shader_explicit_arithmetic_types_int8};
1206         requireExtensions(loc, sizeof(extensions)/sizeof(extensions[0]), extensions, op);
1207     }
1208 }
1209 
1210 // Call for any operation needing GLSL explicit int32 data-type support.
explicitInt32Check(const TSourceLoc & loc,const char * op,bool builtIn)1211 void TParseVersions::explicitInt32Check(const TSourceLoc& loc, const char* op, bool builtIn)
1212 {
1213     if (! builtIn) {
1214         const char* const extensions[2] = {E_GL_EXT_shader_explicit_arithmetic_types,
1215                                            E_GL_EXT_shader_explicit_arithmetic_types_int32};
1216         requireExtensions(loc, 2, extensions, op);
1217     }
1218 }
1219 
1220 // Call for any operation needing GLSL 64-bit integer data-type support.
int64Check(const TSourceLoc & loc,const char * op,bool builtIn)1221 void TParseVersions::int64Check(const TSourceLoc& loc, const char* op, bool builtIn)
1222 {
1223     if (! builtIn) {
1224         const char* const extensions[3] = {E_GL_ARB_gpu_shader_int64,
1225                                            E_GL_EXT_shader_explicit_arithmetic_types,
1226                                            E_GL_EXT_shader_explicit_arithmetic_types_int64};
1227         requireExtensions(loc, 3, extensions, op);
1228         requireProfile(loc, ECoreProfile | ECompatibilityProfile, op);
1229         profileRequires(loc, ECoreProfile | ECompatibilityProfile, 400, nullptr, op);
1230     }
1231 }
1232 
fcoopmatCheck(const TSourceLoc & loc,const char * op,bool builtIn)1233 void TParseVersions::fcoopmatCheck(const TSourceLoc& loc, const char* op, bool builtIn)
1234 {
1235     if (!builtIn) {
1236         const char* const extensions[] = {E_GL_NV_cooperative_matrix};
1237         requireExtensions(loc, sizeof(extensions)/sizeof(extensions[0]), extensions, op);
1238     }
1239 }
1240 
intcoopmatCheck(const TSourceLoc & loc,const char * op,bool builtIn)1241 void TParseVersions::intcoopmatCheck(const TSourceLoc& loc, const char* op, bool builtIn)
1242 {
1243     if (!builtIn) {
1244         const char* const extensions[] = {E_GL_NV_integer_cooperative_matrix};
1245         requireExtensions(loc, sizeof(extensions)/sizeof(extensions[0]), extensions, op);
1246     }
1247 }
1248 #endif // GLSLANG_WEB
1249 // Call for any operation removed because SPIR-V is in use.
spvRemoved(const TSourceLoc & loc,const char * op)1250 void TParseVersions::spvRemoved(const TSourceLoc& loc, const char* op)
1251 {
1252     if (spvVersion.spv != 0)
1253         error(loc, "not allowed when generating SPIR-V", op, "");
1254 }
1255 
1256 // Call for any operation removed because Vulkan SPIR-V is being generated.
vulkanRemoved(const TSourceLoc & loc,const char * op)1257 void TParseVersions::vulkanRemoved(const TSourceLoc& loc, const char* op)
1258 {
1259     if (spvVersion.vulkan > 0)
1260         error(loc, "not allowed when using GLSL for Vulkan", op, "");
1261 }
1262 
1263 // Call for any operation that requires Vulkan.
requireVulkan(const TSourceLoc & loc,const char * op)1264 void TParseVersions::requireVulkan(const TSourceLoc& loc, const char* op)
1265 {
1266 #ifndef GLSLANG_WEB
1267     if (spvVersion.vulkan == 0)
1268         error(loc, "only allowed when using GLSL for Vulkan", op, "");
1269 #endif
1270 }
1271 
1272 // Call for any operation that requires SPIR-V.
requireSpv(const TSourceLoc & loc,const char * op)1273 void TParseVersions::requireSpv(const TSourceLoc& loc, const char* op)
1274 {
1275 #ifndef GLSLANG_WEB
1276     if (spvVersion.spv == 0)
1277         error(loc, "only allowed when generating SPIR-V", op, "");
1278 #endif
1279 }
requireSpv(const TSourceLoc & loc,const char * op,unsigned int version)1280 void TParseVersions::requireSpv(const TSourceLoc& loc, const char *op, unsigned int version)
1281 {
1282 #ifndef GLSLANG_WEB
1283     if (spvVersion.spv < version)
1284         error(loc, "not supported for current targeted SPIR-V version", op, "");
1285 #endif
1286 }
1287 
1288 } // end namespace glslang
1289