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