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->OVR_multiview = 0;
198 resources->OVR_multiview2 = 0;
199 resources->EXT_YUV_target = 0;
200 resources->EXT_geometry_shader = 0;
201 resources->OES_geometry_shader = 0;
202 resources->EXT_gpu_shader5 = 0;
203 resources->OES_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_shader_pixel_local_storage = 0;
211 resources->ANGLE_texture_multisample = 0;
212 resources->ANGLE_multi_draw = 0;
213 resources->ANGLE_base_vertex_base_instance = 0;
214 resources->ANGLE_base_vertex_base_instance_shader_builtin = 0;
215 resources->WEBGL_video_texture = 0;
216 resources->APPLE_clip_distance = 0;
217 resources->OES_texture_cube_map_array = 0;
218 resources->EXT_texture_cube_map_array = 0;
219 resources->EXT_shadow_samplers = 0;
220 resources->OES_shader_multisample_interpolation = 0;
221 resources->NV_draw_buffers = 0;
222 resources->OES_shader_image_atomic = 0;
223 resources->EXT_tessellation_shader = 0;
224 resources->OES_tessellation_shader = 0;
225 resources->OES_texture_buffer = 0;
226 resources->EXT_texture_buffer = 0;
227 resources->OES_sample_variables = 0;
228 resources->EXT_clip_cull_distance = 0;
229 resources->ANGLE_clip_cull_distance = 0;
230 resources->KHR_blend_equation_advanced = 0;
231
232 resources->MaxClipDistances = 8;
233 resources->MaxCullDistances = 8;
234 resources->MaxCombinedClipAndCullDistances = 8;
235
236 // Disable highp precision in fragment shader by default.
237 resources->FragmentPrecisionHigh = 0;
238
239 // GLSL ES 3.0 constants.
240 resources->MaxVertexOutputVectors = 16;
241 resources->MaxFragmentInputVectors = 15;
242 resources->MinProgramTexelOffset = -8;
243 resources->MaxProgramTexelOffset = 7;
244
245 // Extensions constants.
246 resources->MaxDualSourceDrawBuffers = 0;
247
248 resources->MaxViewsOVR = 4;
249
250 // Disable name hashing by default.
251 resources->HashFunction = nullptr;
252
253 resources->MaxExpressionComplexity = 256;
254 resources->MaxStatementDepth = 256;
255 resources->MaxCallStackDepth = 256;
256 resources->MaxFunctionParameters = 1024;
257
258 // ES 3.1 Revision 4, 7.2 Built-in Constants
259
260 // ES 3.1, Revision 4, 8.13 Texture minification
261 // "The value of MIN_PROGRAM_TEXTURE_GATHER_OFFSET must be less than or equal to the value of
262 // MIN_PROGRAM_TEXEL_OFFSET. The value of MAX_PROGRAM_TEXTURE_GATHER_OFFSET must be greater than
263 // or equal to the value of MAX_PROGRAM_TEXEL_OFFSET"
264 resources->MinProgramTextureGatherOffset = -8;
265 resources->MaxProgramTextureGatherOffset = 7;
266
267 resources->MaxImageUnits = 4;
268 resources->MaxVertexImageUniforms = 0;
269 resources->MaxFragmentImageUniforms = 0;
270 resources->MaxComputeImageUniforms = 4;
271 resources->MaxCombinedImageUniforms = 4;
272
273 resources->MaxUniformLocations = 1024;
274
275 resources->MaxCombinedShaderOutputResources = 4;
276
277 resources->MaxComputeWorkGroupCount[0] = 65535;
278 resources->MaxComputeWorkGroupCount[1] = 65535;
279 resources->MaxComputeWorkGroupCount[2] = 65535;
280 resources->MaxComputeWorkGroupSize[0] = 128;
281 resources->MaxComputeWorkGroupSize[1] = 128;
282 resources->MaxComputeWorkGroupSize[2] = 64;
283 resources->MaxComputeUniformComponents = 512;
284 resources->MaxComputeTextureImageUnits = 16;
285
286 resources->MaxComputeAtomicCounters = 8;
287 resources->MaxComputeAtomicCounterBuffers = 1;
288
289 resources->MaxVertexAtomicCounters = 0;
290 resources->MaxFragmentAtomicCounters = 0;
291 resources->MaxCombinedAtomicCounters = 8;
292 resources->MaxAtomicCounterBindings = 1;
293
294 resources->MaxVertexAtomicCounterBuffers = 0;
295 resources->MaxFragmentAtomicCounterBuffers = 0;
296 resources->MaxCombinedAtomicCounterBuffers = 1;
297 resources->MaxAtomicCounterBufferSize = 32;
298
299 resources->MaxUniformBufferBindings = 32;
300 resources->MaxShaderStorageBufferBindings = 4;
301
302 resources->MaxGeometryUniformComponents = 1024;
303 resources->MaxGeometryUniformBlocks = 12;
304 resources->MaxGeometryInputComponents = 64;
305 resources->MaxGeometryOutputComponents = 64;
306 resources->MaxGeometryOutputVertices = 256;
307 resources->MaxGeometryTotalOutputComponents = 1024;
308 resources->MaxGeometryTextureImageUnits = 16;
309 resources->MaxGeometryAtomicCounterBuffers = 0;
310 resources->MaxGeometryAtomicCounters = 0;
311 resources->MaxGeometryShaderStorageBlocks = 0;
312 resources->MaxGeometryShaderInvocations = 32;
313 resources->MaxGeometryImageUniforms = 0;
314
315 resources->MaxTessControlInputComponents = 64;
316 resources->MaxTessControlOutputComponents = 64;
317 resources->MaxTessControlTextureImageUnits = 16;
318 resources->MaxTessControlUniformComponents = 1024;
319 resources->MaxTessControlTotalOutputComponents = 2048;
320 resources->MaxTessControlImageUniforms = 0;
321 resources->MaxTessControlAtomicCounters = 0;
322 resources->MaxTessControlAtomicCounterBuffers = 0;
323
324 resources->MaxTessPatchComponents = 120;
325 resources->MaxPatchVertices = 32;
326 resources->MaxTessGenLevel = 64;
327
328 resources->MaxTessEvaluationInputComponents = 64;
329 resources->MaxTessEvaluationOutputComponents = 64;
330 resources->MaxTessEvaluationTextureImageUnits = 16;
331 resources->MaxTessEvaluationUniformComponents = 1024;
332 resources->MaxTessEvaluationImageUniforms = 0;
333 resources->MaxTessEvaluationAtomicCounters = 0;
334 resources->MaxTessEvaluationAtomicCounterBuffers = 0;
335
336 resources->SubPixelBits = 8;
337
338 resources->MaxSamples = 4;
339 }
340
341 //
342 // Driver calls these to create and destroy compiler objects.
343 //
ConstructCompiler(sh::GLenum type,ShShaderSpec spec,ShShaderOutput output,const ShBuiltInResources * resources)344 ShHandle ConstructCompiler(sh::GLenum type,
345 ShShaderSpec spec,
346 ShShaderOutput output,
347 const ShBuiltInResources *resources)
348 {
349 TShHandleBase *base = static_cast<TShHandleBase *>(ConstructCompiler(type, spec, output));
350 if (base == nullptr)
351 {
352 return 0;
353 }
354
355 TCompiler *compiler = base->getAsCompiler();
356 if (compiler == nullptr)
357 {
358 return 0;
359 }
360
361 // Generate built-in symbol table.
362 if (!compiler->Init(*resources))
363 {
364 Destruct(base);
365 return 0;
366 }
367
368 return base;
369 }
370
Destruct(ShHandle handle)371 void Destruct(ShHandle handle)
372 {
373 if (handle == 0)
374 return;
375
376 TShHandleBase *base = static_cast<TShHandleBase *>(handle);
377
378 if (base->getAsCompiler())
379 DeleteCompiler(base->getAsCompiler());
380 }
381
GetBuiltInResources(const ShHandle handle)382 ShBuiltInResources GetBuiltInResources(const ShHandle handle)
383 {
384 TCompiler *compiler = GetCompilerFromHandle(handle);
385 ASSERT(compiler);
386 return compiler->getBuiltInResources();
387 }
388
GetBuiltInResourcesString(const ShHandle handle)389 const std::string &GetBuiltInResourcesString(const ShHandle handle)
390 {
391 TCompiler *compiler = GetCompilerFromHandle(handle);
392 ASSERT(compiler);
393 return compiler->getBuiltInResourcesString();
394 }
395
396 //
397 // Do an actual compile on the given strings. The result is left
398 // in the given compile object.
399 //
400 // Return: The return value of ShCompile is really boolean, indicating
401 // success or failure.
402 //
Compile(const ShHandle handle,const char * const shaderStrings[],size_t numStrings,const ShCompileOptions & compileOptions)403 bool Compile(const ShHandle handle,
404 const char *const shaderStrings[],
405 size_t numStrings,
406 const ShCompileOptions &compileOptions)
407 {
408 TCompiler *compiler = GetCompilerFromHandle(handle);
409 ASSERT(compiler);
410
411 return compiler->compile(shaderStrings, numStrings, compileOptions);
412 }
413
ClearResults(const ShHandle handle)414 void ClearResults(const ShHandle handle)
415 {
416 TCompiler *compiler = GetCompilerFromHandle(handle);
417 ASSERT(compiler);
418 compiler->clearResults();
419 }
420
GetShaderVersion(const ShHandle handle)421 int GetShaderVersion(const ShHandle handle)
422 {
423 TCompiler *compiler = GetCompilerFromHandle(handle);
424 ASSERT(compiler);
425 return compiler->getShaderVersion();
426 }
427
GetShaderOutputType(const ShHandle handle)428 ShShaderOutput GetShaderOutputType(const ShHandle handle)
429 {
430 TCompiler *compiler = GetCompilerFromHandle(handle);
431 ASSERT(compiler);
432 return compiler->getOutputType();
433 }
434
435 //
436 // Return any compiler log of messages for the application.
437 //
GetInfoLog(const ShHandle handle)438 const std::string &GetInfoLog(const ShHandle handle)
439 {
440 TCompiler *compiler = GetCompilerFromHandle(handle);
441 ASSERT(compiler);
442
443 TInfoSink &infoSink = compiler->getInfoSink();
444 return infoSink.info.str();
445 }
446
447 //
448 // Return any object code.
449 //
GetObjectCode(const ShHandle handle)450 const std::string &GetObjectCode(const ShHandle handle)
451 {
452 TCompiler *compiler = GetCompilerFromHandle(handle);
453 ASSERT(compiler);
454
455 TInfoSink &infoSink = compiler->getInfoSink();
456 return infoSink.obj.str();
457 }
458
459 //
460 // Return any object binary code.
461 //
GetObjectBinaryBlob(const ShHandle handle)462 const BinaryBlob &GetObjectBinaryBlob(const ShHandle handle)
463 {
464 TCompiler *compiler = GetCompilerFromHandle(handle);
465 ASSERT(compiler);
466
467 TInfoSink &infoSink = compiler->getInfoSink();
468 return infoSink.obj.getBinary();
469 }
470
GetShaderBinary(const ShHandle handle,const char * const shaderStrings[],size_t numStrings,const ShCompileOptions & compileOptions,ShaderBinaryBlob * const binaryOut)471 bool GetShaderBinary(const ShHandle handle,
472 const char *const shaderStrings[],
473 size_t numStrings,
474 const ShCompileOptions &compileOptions,
475 ShaderBinaryBlob *const binaryOut)
476 {
477 TCompiler *compiler = GetCompilerFromHandle(handle);
478 ASSERT(compiler);
479
480 return compiler->getShaderBinary(handle, shaderStrings, numStrings, compileOptions, binaryOut);
481 }
482
GetNameHashingMap(const ShHandle handle)483 const std::map<std::string, std::string> *GetNameHashingMap(const ShHandle handle)
484 {
485 TCompiler *compiler = GetCompilerFromHandle(handle);
486 ASSERT(compiler);
487 return &(compiler->getNameMap());
488 }
489
GetUniforms(const ShHandle handle)490 const std::vector<ShaderVariable> *GetUniforms(const ShHandle handle)
491 {
492 TCompiler *compiler = GetCompilerFromHandle(handle);
493 if (!compiler)
494 {
495 return nullptr;
496 }
497 return &compiler->getUniforms();
498 }
499
GetInputVaryings(const ShHandle handle)500 const std::vector<ShaderVariable> *GetInputVaryings(const ShHandle handle)
501 {
502 TCompiler *compiler = GetCompilerFromHandle(handle);
503 if (compiler == nullptr)
504 {
505 return nullptr;
506 }
507 return &compiler->getInputVaryings();
508 }
509
GetOutputVaryings(const ShHandle handle)510 const std::vector<ShaderVariable> *GetOutputVaryings(const ShHandle handle)
511 {
512 TCompiler *compiler = GetCompilerFromHandle(handle);
513 if (compiler == nullptr)
514 {
515 return nullptr;
516 }
517 return &compiler->getOutputVaryings();
518 }
519
GetVaryings(const ShHandle handle)520 const std::vector<ShaderVariable> *GetVaryings(const ShHandle handle)
521 {
522 TCompiler *compiler = GetCompilerFromHandle(handle);
523 if (compiler == nullptr)
524 {
525 return nullptr;
526 }
527
528 switch (compiler->getShaderType())
529 {
530 case GL_VERTEX_SHADER:
531 return &compiler->getOutputVaryings();
532 case GL_FRAGMENT_SHADER:
533 return &compiler->getInputVaryings();
534 case GL_COMPUTE_SHADER:
535 ASSERT(compiler->getOutputVaryings().empty() && compiler->getInputVaryings().empty());
536 return &compiler->getOutputVaryings();
537 // Since geometry shaders have both input and output varyings, we shouldn't call GetVaryings
538 // on a geometry shader.
539 default:
540 return nullptr;
541 }
542 }
543
GetAttributes(const ShHandle handle)544 const std::vector<ShaderVariable> *GetAttributes(const ShHandle handle)
545 {
546 TCompiler *compiler = GetCompilerFromHandle(handle);
547 if (!compiler)
548 {
549 return nullptr;
550 }
551 return &compiler->getAttributes();
552 }
553
GetOutputVariables(const ShHandle handle)554 const std::vector<ShaderVariable> *GetOutputVariables(const ShHandle handle)
555 {
556 TCompiler *compiler = GetCompilerFromHandle(handle);
557 if (!compiler)
558 {
559 return nullptr;
560 }
561 return &compiler->getOutputVariables();
562 }
563
GetInterfaceBlocks(const ShHandle handle)564 const std::vector<InterfaceBlock> *GetInterfaceBlocks(const ShHandle handle)
565 {
566 return GetShaderVariables<InterfaceBlock>(handle);
567 }
568
GetUniformBlocks(const ShHandle handle)569 const std::vector<InterfaceBlock> *GetUniformBlocks(const ShHandle handle)
570 {
571 ASSERT(handle);
572 TShHandleBase *base = static_cast<TShHandleBase *>(handle);
573 TCompiler *compiler = base->getAsCompiler();
574 ASSERT(compiler);
575
576 return &compiler->getUniformBlocks();
577 }
578
GetShaderStorageBlocks(const ShHandle handle)579 const std::vector<InterfaceBlock> *GetShaderStorageBlocks(const ShHandle handle)
580 {
581 ASSERT(handle);
582 TShHandleBase *base = static_cast<TShHandleBase *>(handle);
583 TCompiler *compiler = base->getAsCompiler();
584 ASSERT(compiler);
585
586 return &compiler->getShaderStorageBlocks();
587 }
588
GetComputeShaderLocalGroupSize(const ShHandle handle)589 WorkGroupSize GetComputeShaderLocalGroupSize(const ShHandle handle)
590 {
591 ASSERT(handle);
592
593 TShHandleBase *base = static_cast<TShHandleBase *>(handle);
594 TCompiler *compiler = base->getAsCompiler();
595 ASSERT(compiler);
596
597 return compiler->getComputeShaderLocalSize();
598 }
599
GetVertexShaderNumViews(const ShHandle handle)600 int GetVertexShaderNumViews(const ShHandle handle)
601 {
602 ASSERT(handle);
603 TShHandleBase *base = static_cast<TShHandleBase *>(handle);
604 TCompiler *compiler = base->getAsCompiler();
605 ASSERT(compiler);
606
607 return compiler->getNumViews();
608 }
609
GetShaderSpecConstUsageBits(const ShHandle handle)610 uint32_t GetShaderSpecConstUsageBits(const ShHandle handle)
611 {
612 TCompiler *compiler = GetCompilerFromHandle(handle);
613 if (compiler == nullptr)
614 {
615 return 0;
616 }
617 return compiler->getSpecConstUsageBits().bits();
618 }
619
CheckVariablesWithinPackingLimits(int maxVectors,const std::vector<ShaderVariable> & variables)620 bool CheckVariablesWithinPackingLimits(int maxVectors, const std::vector<ShaderVariable> &variables)
621 {
622 return CheckVariablesInPackingLimits(maxVectors, variables);
623 }
624
GetShaderStorageBlockRegister(const ShHandle handle,const std::string & shaderStorageBlockName,unsigned int * indexOut)625 bool GetShaderStorageBlockRegister(const ShHandle handle,
626 const std::string &shaderStorageBlockName,
627 unsigned int *indexOut)
628 {
629 #ifdef ANGLE_ENABLE_HLSL
630 ASSERT(indexOut);
631
632 TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
633 ASSERT(translator);
634
635 if (!translator->hasShaderStorageBlock(shaderStorageBlockName))
636 {
637 return false;
638 }
639
640 *indexOut = translator->getShaderStorageBlockRegister(shaderStorageBlockName);
641 return true;
642 #else
643 return false;
644 #endif // ANGLE_ENABLE_HLSL
645 }
646
GetUniformBlockRegister(const ShHandle handle,const std::string & uniformBlockName,unsigned int * indexOut)647 bool GetUniformBlockRegister(const ShHandle handle,
648 const std::string &uniformBlockName,
649 unsigned int *indexOut)
650 {
651 #ifdef ANGLE_ENABLE_HLSL
652 ASSERT(indexOut);
653
654 TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
655 ASSERT(translator);
656
657 if (!translator->hasUniformBlock(uniformBlockName))
658 {
659 return false;
660 }
661
662 *indexOut = translator->getUniformBlockRegister(uniformBlockName);
663 return true;
664 #else
665 return false;
666 #endif // ANGLE_ENABLE_HLSL
667 }
668
ShouldUniformBlockUseStructuredBuffer(const ShHandle handle,const std::string & uniformBlockName)669 bool ShouldUniformBlockUseStructuredBuffer(const ShHandle handle,
670 const std::string &uniformBlockName)
671 {
672 #ifdef ANGLE_ENABLE_HLSL
673 TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
674 ASSERT(translator);
675
676 return translator->shouldUniformBlockUseStructuredBuffer(uniformBlockName);
677 #else
678 return false;
679 #endif // ANGLE_ENABLE_HLSL
680 }
681
GetUniformRegisterMap(const ShHandle handle)682 const std::map<std::string, unsigned int> *GetUniformRegisterMap(const ShHandle handle)
683 {
684 #ifdef ANGLE_ENABLE_HLSL
685 TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
686 ASSERT(translator);
687
688 return translator->getUniformRegisterMap();
689 #else
690 return nullptr;
691 #endif // ANGLE_ENABLE_HLSL
692 }
693
GetSlowCompilingUniformBlockSet(const ShHandle handle)694 const std::set<std::string> *GetSlowCompilingUniformBlockSet(const ShHandle handle)
695 {
696 #ifdef ANGLE_ENABLE_HLSL
697 TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
698 ASSERT(translator);
699
700 return translator->getSlowCompilingUniformBlockSet();
701 #else
702 return nullptr;
703 #endif // ANGLE_ENABLE_HLSL
704 }
705
GetReadonlyImage2DRegisterIndex(const ShHandle handle)706 unsigned int GetReadonlyImage2DRegisterIndex(const ShHandle handle)
707 {
708 #ifdef ANGLE_ENABLE_HLSL
709 TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
710 ASSERT(translator);
711
712 return translator->getReadonlyImage2DRegisterIndex();
713 #else
714 return 0;
715 #endif // ANGLE_ENABLE_HLSL
716 }
717
GetImage2DRegisterIndex(const ShHandle handle)718 unsigned int GetImage2DRegisterIndex(const ShHandle handle)
719 {
720 #ifdef ANGLE_ENABLE_HLSL
721 TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
722 ASSERT(translator);
723
724 return translator->getImage2DRegisterIndex();
725 #else
726 return 0;
727 #endif // ANGLE_ENABLE_HLSL
728 }
729
GetUsedImage2DFunctionNames(const ShHandle handle)730 const std::set<std::string> *GetUsedImage2DFunctionNames(const ShHandle handle)
731 {
732 #ifdef ANGLE_ENABLE_HLSL
733 TranslatorHLSL *translator = GetTranslatorHLSLFromHandle(handle);
734 ASSERT(translator);
735
736 return translator->getUsedImage2DFunctionNames();
737 #else
738 return nullptr;
739 #endif // ANGLE_ENABLE_HLSL
740 }
741
GetClipDistanceArraySize(const ShHandle handle)742 uint8_t GetClipDistanceArraySize(const ShHandle handle)
743 {
744 ASSERT(handle);
745 TShHandleBase *base = static_cast<TShHandleBase *>(handle);
746 TCompiler *compiler = base->getAsCompiler();
747 ASSERT(compiler);
748
749 return compiler->getClipDistanceArraySize();
750 }
751
GetCullDistanceArraySize(const ShHandle handle)752 uint8_t GetCullDistanceArraySize(const ShHandle handle)
753 {
754 ASSERT(handle);
755 TShHandleBase *base = static_cast<TShHandleBase *>(handle);
756 TCompiler *compiler = base->getAsCompiler();
757 ASSERT(compiler);
758
759 return compiler->getCullDistanceArraySize();
760 }
761
GetMetadataFlags(const ShHandle handle)762 uint32_t GetMetadataFlags(const ShHandle handle)
763 {
764 ASSERT(handle);
765
766 TShHandleBase *base = static_cast<TShHandleBase *>(handle);
767 TCompiler *compiler = base->getAsCompiler();
768 ASSERT(compiler);
769
770 return compiler->getMetadataFlags().bits();
771 }
772
GetGeometryShaderInputPrimitiveType(const ShHandle handle)773 GLenum GetGeometryShaderInputPrimitiveType(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 GetGeometryShaderPrimitiveTypeEnum(compiler->getGeometryShaderInputPrimitiveType());
782 }
783
GetGeometryShaderOutputPrimitiveType(const ShHandle handle)784 GLenum GetGeometryShaderOutputPrimitiveType(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->getGeometryShaderOutputPrimitiveType());
793 }
794
GetGeometryShaderInvocations(const ShHandle handle)795 int GetGeometryShaderInvocations(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 compiler->getGeometryShaderInvocations();
804 }
805
GetGeometryShaderMaxVertices(const ShHandle handle)806 int GetGeometryShaderMaxVertices(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 int maxVertices = compiler->getGeometryShaderMaxVertices();
815 ASSERT(maxVertices >= 0);
816 return maxVertices;
817 }
818
GetTessControlShaderVertices(const ShHandle handle)819 int GetTessControlShaderVertices(const ShHandle handle)
820 {
821 ASSERT(handle);
822
823 TShHandleBase *base = static_cast<TShHandleBase *>(handle);
824 TCompiler *compiler = base->getAsCompiler();
825 ASSERT(compiler);
826
827 int vertices = compiler->getTessControlShaderOutputVertices();
828 return vertices;
829 }
830
GetTessGenMode(const ShHandle handle)831 GLenum GetTessGenMode(const ShHandle handle)
832 {
833 ASSERT(handle);
834
835 TShHandleBase *base = static_cast<TShHandleBase *>(handle);
836 TCompiler *compiler = base->getAsCompiler();
837 ASSERT(compiler);
838
839 return GetTessellationShaderTypeEnum(compiler->getTessEvaluationShaderInputPrimitiveType());
840 }
841
GetTessGenSpacing(const ShHandle handle)842 GLenum GetTessGenSpacing(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->getTessEvaluationShaderInputVertexSpacingType());
851 }
852
GetTessGenVertexOrder(const ShHandle handle)853 GLenum GetTessGenVertexOrder(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->getTessEvaluationShaderInputOrderingType());
862 }
863
GetTessGenPointMode(const ShHandle handle)864 GLenum GetTessGenPointMode(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->getTessEvaluationShaderInputPointType());
873 }
874
GetShaderSharedMemorySize(const ShHandle handle)875 unsigned int GetShaderSharedMemorySize(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 unsigned int sharedMemorySize = compiler->getSharedMemorySize();
884 return sharedMemorySize;
885 }
886
GetAdvancedBlendEquations(const ShHandle handle)887 uint32_t GetAdvancedBlendEquations(const ShHandle handle)
888 {
889 TCompiler *compiler = GetCompilerFromHandle(handle);
890 ASSERT(compiler);
891
892 return compiler->getAdvancedBlendEquations().bits();
893 }
894
895 // Can't prefix with just _ because then we might introduce a double underscore, which is not safe
896 // in GLSL (ESSL 3.00.6 section 3.8: All identifiers containing a double underscore are reserved for
897 // use by the underlying implementation). u is short for user-defined.
898 const char kUserDefinedNamePrefix[] = "_u";
899
BlockLayoutTypeToString(BlockLayoutType type)900 const char *BlockLayoutTypeToString(BlockLayoutType type)
901 {
902 switch (type)
903 {
904 case BlockLayoutType::BLOCKLAYOUT_STD140:
905 return "std140";
906 case BlockLayoutType::BLOCKLAYOUT_STD430:
907 return "std430";
908 case BlockLayoutType::BLOCKLAYOUT_PACKED:
909 return "packed";
910 case BlockLayoutType::BLOCKLAYOUT_SHARED:
911 return "shared";
912 default:
913 return "invalid";
914 }
915 }
916
BlockTypeToString(BlockType type)917 const char *BlockTypeToString(BlockType type)
918 {
919 switch (type)
920 {
921 case BlockType::kBlockBuffer:
922 return "buffer";
923 case BlockType::kBlockUniform:
924 return "uniform";
925 default:
926 return "invalid";
927 }
928 }
929
InterpolationTypeToString(InterpolationType type)930 const char *InterpolationTypeToString(InterpolationType type)
931 {
932 switch (type)
933 {
934 case InterpolationType::INTERPOLATION_SMOOTH:
935 return "smooth";
936 case InterpolationType::INTERPOLATION_CENTROID:
937 return "centroid";
938 case InterpolationType::INTERPOLATION_SAMPLE:
939 return "sample";
940 case InterpolationType::INTERPOLATION_FLAT:
941 return "flat";
942 case InterpolationType::INTERPOLATION_NOPERSPECTIVE:
943 return "noperspective";
944 case InterpolationType::INTERPOLATION_NOPERSPECTIVE_CENTROID:
945 return "noperspective centroid";
946 case InterpolationType::INTERPOLATION_NOPERSPECTIVE_SAMPLE:
947 return "noperspective sample";
948 default:
949 return "invalid";
950 }
951 }
952 } // namespace sh
953
ShCompileOptions()954 ShCompileOptions::ShCompileOptions()
955 {
956 memset(this, 0, sizeof(*this));
957 }
958
ShCompileOptions(const ShCompileOptions & other)959 ShCompileOptions::ShCompileOptions(const ShCompileOptions &other)
960 {
961 memcpy(this, &other, sizeof(*this));
962 }
operator =(const ShCompileOptions & other)963 ShCompileOptions &ShCompileOptions::operator=(const ShCompileOptions &other)
964 {
965 memcpy(this, &other, sizeof(*this));
966 return *this;
967 }
968
ShBuiltInResources()969 ShBuiltInResources::ShBuiltInResources()
970 {
971 memset(this, 0, sizeof(*this));
972 }
973
ShBuiltInResources(const ShBuiltInResources & other)974 ShBuiltInResources::ShBuiltInResources(const ShBuiltInResources &other)
975 {
976 memcpy(this, &other, sizeof(*this));
977 }
operator =(const ShBuiltInResources & other)978 ShBuiltInResources &ShBuiltInResources::operator=(const ShBuiltInResources &other)
979 {
980 memcpy(this, &other, sizeof(*this));
981 return *this;
982 }
983