xref: /aosp_15_r20/external/angle/src/compiler/translator/ShaderLang.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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 
7 //
8 // Implement the top-level of interface to the compiler,
9 // as defined in ShaderLang.h
10 //
11 
12 #include "GLSLANG/ShaderLang.h"
13 
14 #include "common/PackedEnums.h"
15 #include "compiler/translator/Compiler.h"
16 #include "compiler/translator/InitializeDll.h"
17 #include "compiler/translator/length_limits.h"
18 #ifdef ANGLE_ENABLE_HLSL
19 #    include "compiler/translator/hlsl/TranslatorHLSL.h"
20 #endif  // ANGLE_ENABLE_HLSL
21 #include "angle_gl.h"
22 #include "compiler/translator/VariablePacker.h"
23 
24 namespace sh
25 {
26 
27 namespace
28 {
29 
30 bool isInitialized = false;
31 
32 //
33 // This is the platform independent interface between an OGL driver
34 // and the shading language compiler.
35 //
36 
37 template <typename VarT>
38 const std::vector<VarT> *GetVariableList(const TCompiler *compiler);
39 
40 template <>
GetVariableList(const TCompiler * compiler)41 const std::vector<InterfaceBlock> *GetVariableList(const TCompiler *compiler)
42 {
43     return &compiler->getInterfaceBlocks();
44 }
45 
GetCompilerFromHandle(ShHandle handle)46 TCompiler *GetCompilerFromHandle(ShHandle handle)
47 {
48     if (!handle)
49     {
50         return nullptr;
51     }
52 
53     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
54     return base->getAsCompiler();
55 }
56 
57 template <typename VarT>
GetShaderVariables(const ShHandle handle)58 const std::vector<VarT> *GetShaderVariables(const ShHandle handle)
59 {
60     TCompiler *compiler = GetCompilerFromHandle(handle);
61     if (!compiler)
62     {
63         return nullptr;
64     }
65 
66     return GetVariableList<VarT>(compiler);
67 }
68 
69 #ifdef ANGLE_ENABLE_HLSL
GetTranslatorHLSLFromHandle(ShHandle handle)70 TranslatorHLSL *GetTranslatorHLSLFromHandle(ShHandle handle)
71 {
72     if (!handle)
73         return nullptr;
74     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
75     return base->getAsTranslatorHLSL();
76 }
77 #endif  // ANGLE_ENABLE_HLSL
78 
GetGeometryShaderPrimitiveTypeEnum(sh::TLayoutPrimitiveType primitiveType)79 GLenum GetGeometryShaderPrimitiveTypeEnum(sh::TLayoutPrimitiveType primitiveType)
80 {
81     switch (primitiveType)
82     {
83         case EptPoints:
84             return GL_POINTS;
85         case EptLines:
86             return GL_LINES;
87         case EptLinesAdjacency:
88             return GL_LINES_ADJACENCY_EXT;
89         case EptTriangles:
90             return GL_TRIANGLES;
91         case EptTrianglesAdjacency:
92             return GL_TRIANGLES_ADJACENCY_EXT;
93 
94         case EptLineStrip:
95             return GL_LINE_STRIP;
96         case EptTriangleStrip:
97             return GL_TRIANGLE_STRIP;
98 
99         case EptUndefined:
100         default:
101             UNREACHABLE();
102             return GL_INVALID_VALUE;
103     }
104 }
105 
GetTessellationShaderTypeEnum(sh::TLayoutTessEvaluationType type)106 GLenum GetTessellationShaderTypeEnum(sh::TLayoutTessEvaluationType type)
107 {
108     switch (type)
109     {
110         case EtetTriangles:
111             return GL_TRIANGLES;
112         case EtetQuads:
113             return GL_QUADS;
114         case EtetIsolines:
115             return GL_ISOLINES;
116         case EtetEqualSpacing:
117             return GL_EQUAL;
118         case EtetFractionalEvenSpacing:
119             return GL_FRACTIONAL_EVEN;
120         case EtetFractionalOddSpacing:
121             return GL_FRACTIONAL_ODD;
122         case EtetCw:
123             return GL_CW;
124         case EtetCcw:
125             return GL_CCW;
126         case EtetPointMode:
127             return GL_TESS_GEN_POINT_MODE;
128 
129         case EtetUndefined:
130         default:
131             UNREACHABLE();
132             return GL_INVALID_VALUE;
133     }
134 }
135 
136 }  // anonymous namespace
137 
138 //
139 // Driver must call this first, once, before doing any other compiler operations.
140 // Subsequent calls to this function are no-op.
141 //
Initialize()142 bool Initialize()
143 {
144     if (!isInitialized)
145     {
146         isInitialized = InitProcess();
147     }
148     return isInitialized;
149 }
150 
151 //
152 // Cleanup symbol tables
153 //
Finalize()154 bool Finalize()
155 {
156     if (isInitialized)
157     {
158         DetachProcess();
159         isInitialized = false;
160     }
161     return true;
162 }
163 
164 //
165 // Initialize built-in resources with minimum expected values.
166 //
InitBuiltInResources(ShBuiltInResources * resources)167 void InitBuiltInResources(ShBuiltInResources *resources)
168 {
169     // Make comparable.
170     memset(resources, 0, sizeof(*resources));
171 
172     // Constants.
173     resources->MaxVertexAttribs             = 8;
174     resources->MaxVertexUniformVectors      = 128;
175     resources->MaxVaryingVectors            = 8;
176     resources->MaxVertexTextureImageUnits   = 0;
177     resources->MaxCombinedTextureImageUnits = 8;
178     resources->MaxTextureImageUnits         = 8;
179     resources->MaxFragmentUniformVectors    = 16;
180     resources->MaxDrawBuffers               = 1;
181 
182     // Extensions.
183     resources->OES_standard_derivatives                       = 0;
184     resources->OES_EGL_image_external                         = 0;
185     resources->OES_EGL_image_external_essl3                   = 0;
186     resources->NV_EGL_stream_consumer_external                = 0;
187     resources->ARB_texture_rectangle                          = 0;
188     resources->EXT_blend_func_extended                        = 0;
189     resources->EXT_conservative_depth                         = 0;
190     resources->EXT_draw_buffers                               = 0;
191     resources->EXT_frag_depth                                 = 0;
192     resources->EXT_shader_texture_lod                         = 0;
193     resources->EXT_shader_framebuffer_fetch                   = 0;
194     resources->EXT_shader_framebuffer_fetch_non_coherent      = 0;
195     resources->NV_shader_framebuffer_fetch                    = 0;
196     resources->ARM_shader_framebuffer_fetch                   = 0;
197     resources->ARM_shader_framebuffer_fetch_depth_stencil     = 0;
198     resources->OVR_multiview                                  = 0;
199     resources->OVR_multiview2                                 = 0;
200     resources->EXT_YUV_target                                 = 0;
201     resources->EXT_geometry_shader                            = 0;
202     resources->OES_geometry_shader                            = 0;
203     resources->EXT_gpu_shader5                                = 0;
204     resources->OES_gpu_shader5                                = 0;
205     resources->OES_shader_io_blocks                           = 0;
206     resources->EXT_shader_io_blocks                           = 0;
207     resources->EXT_shader_non_constant_global_initializers    = 0;
208     resources->NV_shader_noperspective_interpolation          = 0;
209     resources->OES_texture_storage_multisample_2d_array       = 0;
210     resources->OES_texture_3D                                 = 0;
211     resources->ANGLE_shader_pixel_local_storage               = 0;
212     resources->ANGLE_texture_multisample                      = 0;
213     resources->ANGLE_multi_draw                               = 0;
214     resources->ANGLE_base_vertex_base_instance                = 0;
215     resources->ANGLE_base_vertex_base_instance_shader_builtin = 0;
216     resources->WEBGL_video_texture                            = 0;
217     resources->APPLE_clip_distance                            = 0;
218     resources->OES_texture_cube_map_array                     = 0;
219     resources->EXT_texture_cube_map_array                     = 0;
220     resources->EXT_texture_query_lod                          = 0;
221     resources->EXT_texture_shadow_lod                         = 0;
222     resources->EXT_shadow_samplers                            = 0;
223     resources->OES_shader_multisample_interpolation           = 0;
224     resources->NV_draw_buffers                                = 0;
225     resources->OES_shader_image_atomic                        = 0;
226     resources->EXT_tessellation_shader                        = 0;
227     resources->OES_tessellation_shader                        = 0;
228     resources->OES_texture_buffer                             = 0;
229     resources->EXT_texture_buffer                             = 0;
230     resources->OES_sample_variables                           = 0;
231     resources->EXT_clip_cull_distance                         = 0;
232     resources->ANGLE_clip_cull_distance                       = 0;
233     resources->KHR_blend_equation_advanced                    = 0;
234 
235     resources->MaxClipDistances                = 8;
236     resources->MaxCullDistances                = 8;
237     resources->MaxCombinedClipAndCullDistances = 8;
238 
239     // Disable highp precision in fragment shader by default.
240     resources->FragmentPrecisionHigh = 0;
241 
242     // GLSL ES 3.0 constants.
243     resources->MaxVertexOutputVectors  = 16;
244     resources->MaxFragmentInputVectors = 15;
245     resources->MinProgramTexelOffset   = -8;
246     resources->MaxProgramTexelOffset   = 7;
247 
248     // Extensions constants.
249     resources->MaxDualSourceDrawBuffers = 0;
250 
251     resources->MaxViewsOVR = 4;
252 
253     // Disable name hashing by default.
254     resources->HashFunction = nullptr;
255 
256     resources->MaxExpressionComplexity = 256;
257     resources->MaxStatementDepth       = 256;
258     resources->MaxCallStackDepth       = 256;
259     resources->MaxFunctionParameters   = 1024;
260 
261     // ES 3.1 Revision 4, 7.2 Built-in Constants
262 
263     // ES 3.1, Revision 4, 8.13 Texture minification
264     // "The value of MIN_PROGRAM_TEXTURE_GATHER_OFFSET must be less than or equal to the value of
265     // MIN_PROGRAM_TEXEL_OFFSET. The value of MAX_PROGRAM_TEXTURE_GATHER_OFFSET must be greater than
266     // or equal to the value of MAX_PROGRAM_TEXEL_OFFSET"
267     resources->MinProgramTextureGatherOffset = -8;
268     resources->MaxProgramTextureGatherOffset = 7;
269 
270     resources->MaxImageUnits            = 4;
271     resources->MaxVertexImageUniforms   = 0;
272     resources->MaxFragmentImageUniforms = 0;
273     resources->MaxComputeImageUniforms  = 4;
274     resources->MaxCombinedImageUniforms = 4;
275 
276     resources->MaxUniformLocations = 1024;
277 
278     resources->MaxCombinedShaderOutputResources = 4;
279 
280     resources->MaxComputeWorkGroupCount[0] = 65535;
281     resources->MaxComputeWorkGroupCount[1] = 65535;
282     resources->MaxComputeWorkGroupCount[2] = 65535;
283     resources->MaxComputeWorkGroupSize[0]  = 128;
284     resources->MaxComputeWorkGroupSize[1]  = 128;
285     resources->MaxComputeWorkGroupSize[2]  = 64;
286     resources->MaxComputeUniformComponents = 512;
287     resources->MaxComputeTextureImageUnits = 16;
288 
289     resources->MaxComputeAtomicCounters       = 8;
290     resources->MaxComputeAtomicCounterBuffers = 1;
291 
292     resources->MaxVertexAtomicCounters   = 0;
293     resources->MaxFragmentAtomicCounters = 0;
294     resources->MaxCombinedAtomicCounters = 8;
295     resources->MaxAtomicCounterBindings  = 1;
296 
297     resources->MaxVertexAtomicCounterBuffers   = 0;
298     resources->MaxFragmentAtomicCounterBuffers = 0;
299     resources->MaxCombinedAtomicCounterBuffers = 1;
300     resources->MaxAtomicCounterBufferSize      = 32;
301 
302     resources->MaxUniformBufferBindings       = 32;
303     resources->MaxShaderStorageBufferBindings = 4;
304 
305     resources->MaxGeometryUniformComponents     = 1024;
306     resources->MaxGeometryUniformBlocks         = 12;
307     resources->MaxGeometryInputComponents       = 64;
308     resources->MaxGeometryOutputComponents      = 64;
309     resources->MaxGeometryOutputVertices        = 256;
310     resources->MaxGeometryTotalOutputComponents = 1024;
311     resources->MaxGeometryTextureImageUnits     = 16;
312     resources->MaxGeometryAtomicCounterBuffers  = 0;
313     resources->MaxGeometryAtomicCounters        = 0;
314     resources->MaxGeometryShaderStorageBlocks   = 0;
315     resources->MaxGeometryShaderInvocations     = 32;
316     resources->MaxGeometryImageUniforms         = 0;
317 
318     resources->MaxTessControlInputComponents       = 64;
319     resources->MaxTessControlOutputComponents      = 64;
320     resources->MaxTessControlTextureImageUnits     = 16;
321     resources->MaxTessControlUniformComponents     = 1024;
322     resources->MaxTessControlTotalOutputComponents = 2048;
323     resources->MaxTessControlImageUniforms         = 0;
324     resources->MaxTessControlAtomicCounters        = 0;
325     resources->MaxTessControlAtomicCounterBuffers  = 0;
326 
327     resources->MaxTessPatchComponents = 120;
328     resources->MaxPatchVertices       = 32;
329     resources->MaxTessGenLevel        = 64;
330 
331     resources->MaxTessEvaluationInputComponents      = 64;
332     resources->MaxTessEvaluationOutputComponents     = 64;
333     resources->MaxTessEvaluationTextureImageUnits    = 16;
334     resources->MaxTessEvaluationUniformComponents    = 1024;
335     resources->MaxTessEvaluationImageUniforms        = 0;
336     resources->MaxTessEvaluationAtomicCounters       = 0;
337     resources->MaxTessEvaluationAtomicCounterBuffers = 0;
338 
339     resources->SubPixelBits = 8;
340 
341     resources->MaxSamples = 4;
342 }
343 
344 //
345 // Driver calls these to create and destroy compiler objects.
346 //
ConstructCompiler(sh::GLenum type,ShShaderSpec spec,ShShaderOutput output,const ShBuiltInResources * resources)347 ShHandle ConstructCompiler(sh::GLenum type,
348                            ShShaderSpec spec,
349                            ShShaderOutput output,
350                            const ShBuiltInResources *resources)
351 {
352     TShHandleBase *base = static_cast<TShHandleBase *>(ConstructCompiler(type, spec, output));
353     if (base == nullptr)
354     {
355         return 0;
356     }
357 
358     TCompiler *compiler = base->getAsCompiler();
359     if (compiler == nullptr)
360     {
361         return 0;
362     }
363 
364     // Generate built-in symbol table.
365     if (!compiler->Init(*resources))
366     {
367         Destruct(base);
368         return 0;
369     }
370 
371     return base;
372 }
373 
Destruct(ShHandle handle)374 void Destruct(ShHandle handle)
375 {
376     if (handle == 0)
377         return;
378 
379     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
380 
381     if (base->getAsCompiler())
382         DeleteCompiler(base->getAsCompiler());
383 }
384 
GetBuiltInResources(const ShHandle handle)385 ShBuiltInResources GetBuiltInResources(const ShHandle handle)
386 {
387     TCompiler *compiler = GetCompilerFromHandle(handle);
388     ASSERT(compiler);
389     return compiler->getBuiltInResources();
390 }
391 
GetBuiltInResourcesString(const ShHandle handle)392 const std::string &GetBuiltInResourcesString(const ShHandle handle)
393 {
394     TCompiler *compiler = GetCompilerFromHandle(handle);
395     ASSERT(compiler);
396     return compiler->getBuiltInResourcesString();
397 }
398 
399 //
400 // Do an actual compile on the given strings.  The result is left
401 // in the given compile object.
402 //
403 // Return:  The return value of ShCompile is really boolean, indicating
404 // success or failure.
405 //
Compile(const ShHandle handle,const char * const shaderStrings[],size_t numStrings,const ShCompileOptions & compileOptions)406 bool Compile(const ShHandle handle,
407              const char *const shaderStrings[],
408              size_t numStrings,
409              const ShCompileOptions &compileOptions)
410 {
411     TCompiler *compiler = GetCompilerFromHandle(handle);
412     ASSERT(compiler);
413 
414     return compiler->compile(shaderStrings, numStrings, compileOptions);
415 }
416 
ClearResults(const ShHandle handle)417 void ClearResults(const ShHandle handle)
418 {
419     TCompiler *compiler = GetCompilerFromHandle(handle);
420     ASSERT(compiler);
421     compiler->clearResults();
422 }
423 
GetShaderVersion(const ShHandle handle)424 int GetShaderVersion(const ShHandle handle)
425 {
426     TCompiler *compiler = GetCompilerFromHandle(handle);
427     ASSERT(compiler);
428     return compiler->getShaderVersion();
429 }
430 
GetShaderOutputType(const ShHandle handle)431 ShShaderOutput GetShaderOutputType(const ShHandle handle)
432 {
433     TCompiler *compiler = GetCompilerFromHandle(handle);
434     ASSERT(compiler);
435     return compiler->getOutputType();
436 }
437 
438 //
439 // Return any compiler log of messages for the application.
440 //
GetInfoLog(const ShHandle handle)441 const std::string &GetInfoLog(const ShHandle handle)
442 {
443     TCompiler *compiler = GetCompilerFromHandle(handle);
444     ASSERT(compiler);
445 
446     TInfoSink &infoSink = compiler->getInfoSink();
447     return infoSink.info.str();
448 }
449 
450 //
451 // Return any object code.
452 //
GetObjectCode(const ShHandle handle)453 const std::string &GetObjectCode(const ShHandle handle)
454 {
455     TCompiler *compiler = GetCompilerFromHandle(handle);
456     ASSERT(compiler);
457 
458     TInfoSink &infoSink = compiler->getInfoSink();
459     return infoSink.obj.str();
460 }
461 
462 //
463 // Return any object binary code.
464 //
GetObjectBinaryBlob(const ShHandle handle)465 const BinaryBlob &GetObjectBinaryBlob(const ShHandle handle)
466 {
467     TCompiler *compiler = GetCompilerFromHandle(handle);
468     ASSERT(compiler);
469 
470     TInfoSink &infoSink = compiler->getInfoSink();
471     return infoSink.obj.getBinary();
472 }
473 
GetShaderBinary(const ShHandle handle,const char * const shaderStrings[],size_t numStrings,const ShCompileOptions & compileOptions,ShaderBinaryBlob * const binaryOut)474 bool GetShaderBinary(const ShHandle handle,
475                      const char *const shaderStrings[],
476                      size_t numStrings,
477                      const ShCompileOptions &compileOptions,
478                      ShaderBinaryBlob *const binaryOut)
479 {
480     TCompiler *compiler = GetCompilerFromHandle(handle);
481     ASSERT(compiler);
482 
483     return compiler->getShaderBinary(handle, shaderStrings, numStrings, compileOptions, binaryOut);
484 }
485 
GetNameHashingMap(const ShHandle handle)486 const std::map<std::string, std::string> *GetNameHashingMap(const ShHandle handle)
487 {
488     TCompiler *compiler = GetCompilerFromHandle(handle);
489     ASSERT(compiler);
490     return &(compiler->getNameMap());
491 }
492 
GetUniforms(const ShHandle handle)493 const std::vector<ShaderVariable> *GetUniforms(const ShHandle handle)
494 {
495     TCompiler *compiler = GetCompilerFromHandle(handle);
496     if (!compiler)
497     {
498         return nullptr;
499     }
500     return &compiler->getUniforms();
501 }
502 
GetInputVaryings(const ShHandle handle)503 const std::vector<ShaderVariable> *GetInputVaryings(const ShHandle handle)
504 {
505     TCompiler *compiler = GetCompilerFromHandle(handle);
506     if (compiler == nullptr)
507     {
508         return nullptr;
509     }
510     return &compiler->getInputVaryings();
511 }
512 
GetOutputVaryings(const ShHandle handle)513 const std::vector<ShaderVariable> *GetOutputVaryings(const ShHandle handle)
514 {
515     TCompiler *compiler = GetCompilerFromHandle(handle);
516     if (compiler == nullptr)
517     {
518         return nullptr;
519     }
520     return &compiler->getOutputVaryings();
521 }
522 
GetVaryings(const ShHandle handle)523 const std::vector<ShaderVariable> *GetVaryings(const ShHandle handle)
524 {
525     TCompiler *compiler = GetCompilerFromHandle(handle);
526     if (compiler == nullptr)
527     {
528         return nullptr;
529     }
530 
531     switch (compiler->getShaderType())
532     {
533         case GL_VERTEX_SHADER:
534             return &compiler->getOutputVaryings();
535         case GL_FRAGMENT_SHADER:
536             return &compiler->getInputVaryings();
537         case GL_COMPUTE_SHADER:
538             ASSERT(compiler->getOutputVaryings().empty() && compiler->getInputVaryings().empty());
539             return &compiler->getOutputVaryings();
540         // Since geometry shaders have both input and output varyings, we shouldn't call GetVaryings
541         // on a geometry shader.
542         default:
543             return nullptr;
544     }
545 }
546 
GetAttributes(const ShHandle handle)547 const std::vector<ShaderVariable> *GetAttributes(const ShHandle handle)
548 {
549     TCompiler *compiler = GetCompilerFromHandle(handle);
550     if (!compiler)
551     {
552         return nullptr;
553     }
554     return &compiler->getAttributes();
555 }
556 
GetOutputVariables(const ShHandle handle)557 const std::vector<ShaderVariable> *GetOutputVariables(const ShHandle handle)
558 {
559     TCompiler *compiler = GetCompilerFromHandle(handle);
560     if (!compiler)
561     {
562         return nullptr;
563     }
564     return &compiler->getOutputVariables();
565 }
566 
GetInterfaceBlocks(const ShHandle handle)567 const std::vector<InterfaceBlock> *GetInterfaceBlocks(const ShHandle handle)
568 {
569     return GetShaderVariables<InterfaceBlock>(handle);
570 }
571 
GetUniformBlocks(const ShHandle handle)572 const std::vector<InterfaceBlock> *GetUniformBlocks(const ShHandle handle)
573 {
574     ASSERT(handle);
575     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
576     TCompiler *compiler = base->getAsCompiler();
577     ASSERT(compiler);
578 
579     return &compiler->getUniformBlocks();
580 }
581 
GetShaderStorageBlocks(const ShHandle handle)582 const std::vector<InterfaceBlock> *GetShaderStorageBlocks(const ShHandle handle)
583 {
584     ASSERT(handle);
585     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
586     TCompiler *compiler = base->getAsCompiler();
587     ASSERT(compiler);
588 
589     return &compiler->getShaderStorageBlocks();
590 }
591 
GetComputeShaderLocalGroupSize(const ShHandle handle)592 WorkGroupSize GetComputeShaderLocalGroupSize(const ShHandle handle)
593 {
594     ASSERT(handle);
595 
596     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
597     TCompiler *compiler = base->getAsCompiler();
598     ASSERT(compiler);
599 
600     return compiler->getComputeShaderLocalSize();
601 }
602 
GetVertexShaderNumViews(const ShHandle handle)603 int GetVertexShaderNumViews(const ShHandle handle)
604 {
605     ASSERT(handle);
606     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
607     TCompiler *compiler = base->getAsCompiler();
608     ASSERT(compiler);
609 
610     return compiler->getNumViews();
611 }
612 
GetPixelLocalStorageFormats(const ShHandle handle)613 const std::vector<ShPixelLocalStorageFormat> *GetPixelLocalStorageFormats(const ShHandle handle)
614 {
615     TCompiler *compiler = GetCompilerFromHandle(handle);
616     ASSERT(compiler);
617 
618     return &compiler->GetPixelLocalStorageFormats();
619 }
620 
GetShaderSpecConstUsageBits(const ShHandle handle)621 uint32_t GetShaderSpecConstUsageBits(const ShHandle handle)
622 {
623     TCompiler *compiler = GetCompilerFromHandle(handle);
624     if (compiler == nullptr)
625     {
626         return 0;
627     }
628     return compiler->getSpecConstUsageBits().bits();
629 }
630 
CheckVariablesWithinPackingLimits(int maxVectors,const std::vector<ShaderVariable> & variables)631 bool CheckVariablesWithinPackingLimits(int maxVectors, const std::vector<ShaderVariable> &variables)
632 {
633     return CheckVariablesInPackingLimits(maxVectors, variables);
634 }
635 
GetShaderStorageBlockRegister(const ShHandle handle,const std::string & shaderStorageBlockName,unsigned int * indexOut)636 bool GetShaderStorageBlockRegister(const ShHandle handle,
637                                    const std::string &shaderStorageBlockName,
638                                    unsigned int *indexOut)
639 {
640 #ifdef ANGLE_ENABLE_HLSL
641     ASSERT(indexOut);
642 
643     TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
644     ASSERT(translator);
645 
646     if (!translator->hasShaderStorageBlock(shaderStorageBlockName))
647     {
648         return false;
649     }
650 
651     *indexOut = translator->getShaderStorageBlockRegister(shaderStorageBlockName);
652     return true;
653 #else
654     return false;
655 #endif  // ANGLE_ENABLE_HLSL
656 }
657 
GetUniformBlockRegister(const ShHandle handle,const std::string & uniformBlockName,unsigned int * indexOut)658 bool GetUniformBlockRegister(const ShHandle handle,
659                              const std::string &uniformBlockName,
660                              unsigned int *indexOut)
661 {
662 #ifdef ANGLE_ENABLE_HLSL
663     ASSERT(indexOut);
664 
665     TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
666     ASSERT(translator);
667 
668     if (!translator->hasUniformBlock(uniformBlockName))
669     {
670         return false;
671     }
672 
673     *indexOut = translator->getUniformBlockRegister(uniformBlockName);
674     return true;
675 #else
676     return false;
677 #endif  // ANGLE_ENABLE_HLSL
678 }
679 
ShouldUniformBlockUseStructuredBuffer(const ShHandle handle,const std::string & uniformBlockName)680 bool ShouldUniformBlockUseStructuredBuffer(const ShHandle handle,
681                                            const std::string &uniformBlockName)
682 {
683 #ifdef ANGLE_ENABLE_HLSL
684     TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
685     ASSERT(translator);
686 
687     return translator->shouldUniformBlockUseStructuredBuffer(uniformBlockName);
688 #else
689     return false;
690 #endif  // ANGLE_ENABLE_HLSL
691 }
692 
GetUniformRegisterMap(const ShHandle handle)693 const std::map<std::string, unsigned int> *GetUniformRegisterMap(const ShHandle handle)
694 {
695 #ifdef ANGLE_ENABLE_HLSL
696     TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
697     ASSERT(translator);
698 
699     return translator->getUniformRegisterMap();
700 #else
701     return nullptr;
702 #endif  // ANGLE_ENABLE_HLSL
703 }
704 
GetSlowCompilingUniformBlockSet(const ShHandle handle)705 const std::set<std::string> *GetSlowCompilingUniformBlockSet(const ShHandle handle)
706 {
707 #ifdef ANGLE_ENABLE_HLSL
708     TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
709     ASSERT(translator);
710 
711     return translator->getSlowCompilingUniformBlockSet();
712 #else
713     return nullptr;
714 #endif  // ANGLE_ENABLE_HLSL
715 }
716 
GetReadonlyImage2DRegisterIndex(const ShHandle handle)717 unsigned int GetReadonlyImage2DRegisterIndex(const ShHandle handle)
718 {
719 #ifdef ANGLE_ENABLE_HLSL
720     TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
721     ASSERT(translator);
722 
723     return translator->getReadonlyImage2DRegisterIndex();
724 #else
725     return 0;
726 #endif  // ANGLE_ENABLE_HLSL
727 }
728 
GetImage2DRegisterIndex(const ShHandle handle)729 unsigned int GetImage2DRegisterIndex(const ShHandle handle)
730 {
731 #ifdef ANGLE_ENABLE_HLSL
732     TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
733     ASSERT(translator);
734 
735     return translator->getImage2DRegisterIndex();
736 #else
737     return 0;
738 #endif  // ANGLE_ENABLE_HLSL
739 }
740 
GetUsedImage2DFunctionNames(const ShHandle handle)741 const std::set<std::string> *GetUsedImage2DFunctionNames(const ShHandle handle)
742 {
743 #ifdef ANGLE_ENABLE_HLSL
744     TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
745     ASSERT(translator);
746 
747     return translator->getUsedImage2DFunctionNames();
748 #else
749     return nullptr;
750 #endif  // ANGLE_ENABLE_HLSL
751 }
752 
GetClipDistanceArraySize(const ShHandle handle)753 uint8_t GetClipDistanceArraySize(const ShHandle handle)
754 {
755     ASSERT(handle);
756     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
757     TCompiler *compiler = base->getAsCompiler();
758     ASSERT(compiler);
759 
760     return compiler->getClipDistanceArraySize();
761 }
762 
GetCullDistanceArraySize(const ShHandle handle)763 uint8_t GetCullDistanceArraySize(const ShHandle handle)
764 {
765     ASSERT(handle);
766     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
767     TCompiler *compiler = base->getAsCompiler();
768     ASSERT(compiler);
769 
770     return compiler->getCullDistanceArraySize();
771 }
772 
GetMetadataFlags(const ShHandle handle)773 uint32_t GetMetadataFlags(const ShHandle handle)
774 {
775     ASSERT(handle);
776 
777     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
778     TCompiler *compiler = base->getAsCompiler();
779     ASSERT(compiler);
780 
781     return compiler->getMetadataFlags().bits();
782 }
783 
GetGeometryShaderInputPrimitiveType(const ShHandle handle)784 GLenum GetGeometryShaderInputPrimitiveType(const ShHandle handle)
785 {
786     ASSERT(handle);
787 
788     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
789     TCompiler *compiler = base->getAsCompiler();
790     ASSERT(compiler);
791 
792     return GetGeometryShaderPrimitiveTypeEnum(compiler->getGeometryShaderInputPrimitiveType());
793 }
794 
GetGeometryShaderOutputPrimitiveType(const ShHandle handle)795 GLenum GetGeometryShaderOutputPrimitiveType(const ShHandle handle)
796 {
797     ASSERT(handle);
798 
799     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
800     TCompiler *compiler = base->getAsCompiler();
801     ASSERT(compiler);
802 
803     return GetGeometryShaderPrimitiveTypeEnum(compiler->getGeometryShaderOutputPrimitiveType());
804 }
805 
GetGeometryShaderInvocations(const ShHandle handle)806 int GetGeometryShaderInvocations(const ShHandle handle)
807 {
808     ASSERT(handle);
809 
810     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
811     TCompiler *compiler = base->getAsCompiler();
812     ASSERT(compiler);
813 
814     return compiler->getGeometryShaderInvocations();
815 }
816 
GetGeometryShaderMaxVertices(const ShHandle handle)817 int GetGeometryShaderMaxVertices(const ShHandle handle)
818 {
819     ASSERT(handle);
820 
821     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
822     TCompiler *compiler = base->getAsCompiler();
823     ASSERT(compiler);
824 
825     int maxVertices = compiler->getGeometryShaderMaxVertices();
826     ASSERT(maxVertices >= 0);
827     return maxVertices;
828 }
829 
GetTessControlShaderVertices(const ShHandle handle)830 int GetTessControlShaderVertices(const ShHandle handle)
831 {
832     ASSERT(handle);
833 
834     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
835     TCompiler *compiler = base->getAsCompiler();
836     ASSERT(compiler);
837 
838     int vertices = compiler->getTessControlShaderOutputVertices();
839     return vertices;
840 }
841 
GetTessGenMode(const ShHandle handle)842 GLenum GetTessGenMode(const ShHandle handle)
843 {
844     ASSERT(handle);
845 
846     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
847     TCompiler *compiler = base->getAsCompiler();
848     ASSERT(compiler);
849 
850     return GetTessellationShaderTypeEnum(compiler->getTessEvaluationShaderInputPrimitiveType());
851 }
852 
GetTessGenSpacing(const ShHandle handle)853 GLenum GetTessGenSpacing(const ShHandle handle)
854 {
855     ASSERT(handle);
856 
857     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
858     TCompiler *compiler = base->getAsCompiler();
859     ASSERT(compiler);
860 
861     return GetTessellationShaderTypeEnum(compiler->getTessEvaluationShaderInputVertexSpacingType());
862 }
863 
GetTessGenVertexOrder(const ShHandle handle)864 GLenum GetTessGenVertexOrder(const ShHandle handle)
865 {
866     ASSERT(handle);
867 
868     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
869     TCompiler *compiler = base->getAsCompiler();
870     ASSERT(compiler);
871 
872     return GetTessellationShaderTypeEnum(compiler->getTessEvaluationShaderInputOrderingType());
873 }
874 
GetTessGenPointMode(const ShHandle handle)875 GLenum GetTessGenPointMode(const ShHandle handle)
876 {
877     ASSERT(handle);
878 
879     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
880     TCompiler *compiler = base->getAsCompiler();
881     ASSERT(compiler);
882 
883     return GetTessellationShaderTypeEnum(compiler->getTessEvaluationShaderInputPointType());
884 }
885 
GetShaderSharedMemorySize(const ShHandle handle)886 unsigned int GetShaderSharedMemorySize(const ShHandle handle)
887 {
888     ASSERT(handle);
889 
890     TShHandleBase *base = static_cast<TShHandleBase *>(handle);
891     TCompiler *compiler = base->getAsCompiler();
892     ASSERT(compiler);
893 
894     unsigned int sharedMemorySize = compiler->getSharedMemorySize();
895     return sharedMemorySize;
896 }
897 
GetAdvancedBlendEquations(const ShHandle handle)898 uint32_t GetAdvancedBlendEquations(const ShHandle handle)
899 {
900     TCompiler *compiler = GetCompilerFromHandle(handle);
901     ASSERT(compiler);
902 
903     return compiler->getAdvancedBlendEquations().bits();
904 }
905 
906 // Can't prefix with just _ because then we might introduce a double underscore, which is not safe
907 // in GLSL (ESSL 3.00.6 section 3.8: All identifiers containing a double underscore are reserved for
908 // use by the underlying implementation). u is short for user-defined.
909 const char kUserDefinedNamePrefix[] = "_u";
910 
BlockLayoutTypeToString(BlockLayoutType type)911 const char *BlockLayoutTypeToString(BlockLayoutType type)
912 {
913     switch (type)
914     {
915         case BlockLayoutType::BLOCKLAYOUT_STD140:
916             return "std140";
917         case BlockLayoutType::BLOCKLAYOUT_STD430:
918             return "std430";
919         case BlockLayoutType::BLOCKLAYOUT_PACKED:
920             return "packed";
921         case BlockLayoutType::BLOCKLAYOUT_SHARED:
922             return "shared";
923         default:
924             return "invalid";
925     }
926 }
927 
BlockTypeToString(BlockType type)928 const char *BlockTypeToString(BlockType type)
929 {
930     switch (type)
931     {
932         case BlockType::kBlockBuffer:
933             return "buffer";
934         case BlockType::kBlockUniform:
935             return "uniform";
936         default:
937             return "invalid";
938     }
939 }
940 
InterpolationTypeToString(InterpolationType type)941 const char *InterpolationTypeToString(InterpolationType type)
942 {
943     switch (type)
944     {
945         case InterpolationType::INTERPOLATION_SMOOTH:
946             return "smooth";
947         case InterpolationType::INTERPOLATION_CENTROID:
948             return "centroid";
949         case InterpolationType::INTERPOLATION_SAMPLE:
950             return "sample";
951         case InterpolationType::INTERPOLATION_FLAT:
952             return "flat";
953         case InterpolationType::INTERPOLATION_NOPERSPECTIVE:
954             return "noperspective";
955         case InterpolationType::INTERPOLATION_NOPERSPECTIVE_CENTROID:
956             return "noperspective centroid";
957         case InterpolationType::INTERPOLATION_NOPERSPECTIVE_SAMPLE:
958             return "noperspective sample";
959         default:
960             return "invalid";
961     }
962 }
963 }  // namespace sh
964 
ShCompileOptions()965 ShCompileOptions::ShCompileOptions()
966 {
967     memset(this, 0, sizeof(*this));
968 }
969 
ShCompileOptions(const ShCompileOptions & other)970 ShCompileOptions::ShCompileOptions(const ShCompileOptions &other)
971 {
972     memcpy(this, &other, sizeof(*this));
973 }
operator =(const ShCompileOptions & other)974 ShCompileOptions &ShCompileOptions::operator=(const ShCompileOptions &other)
975 {
976     memcpy(this, &other, sizeof(*this));
977     return *this;
978 }
979 
ShBuiltInResources()980 ShBuiltInResources::ShBuiltInResources()
981 {
982     memset(this, 0, sizeof(*this));
983 }
984 
ShBuiltInResources(const ShBuiltInResources & other)985 ShBuiltInResources::ShBuiltInResources(const ShBuiltInResources &other)
986 {
987     memcpy(this, &other, sizeof(*this));
988 }
operator =(const ShBuiltInResources & other)989 ShBuiltInResources &ShBuiltInResources::operator=(const ShBuiltInResources &other)
990 {
991     memcpy(this, &other, sizeof(*this));
992     return *this;
993 }
994