• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2014 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 // Compiler.cpp: implements the gl::Compiler class.
8 
9 #include "libANGLE/Compiler.h"
10 
11 #include "common/debug.h"
12 #include "libANGLE/Context.h"
13 #include "libANGLE/Display.h"
14 #include "libANGLE/State.h"
15 #include "libANGLE/renderer/CompilerImpl.h"
16 #include "libANGLE/renderer/GLImplFactory.h"
17 
18 namespace gl
19 {
20 
21 namespace
22 {
23 
24 // To know when to call sh::Initialize and sh::Finalize.
25 size_t gActiveCompilers = 0;
26 
SelectShaderSpec(GLint majorVersion,GLint minorVersion,bool isWebGL,EGLenum clientType)27 ShShaderSpec SelectShaderSpec(GLint majorVersion,
28                               GLint minorVersion,
29                               bool isWebGL,
30                               EGLenum clientType)
31 {
32     // For Desktop GL
33     if (clientType == EGL_OPENGL_API)
34     {
35         return SH_GL_COMPATIBILITY_SPEC;
36     }
37 
38     if (majorVersion >= 3)
39     {
40         switch (minorVersion)
41         {
42             case 2:
43                 ASSERT(!isWebGL);
44                 return SH_GLES3_2_SPEC;
45             case 1:
46                 return isWebGL ? SH_WEBGL3_SPEC : SH_GLES3_1_SPEC;
47             case 0:
48                 return isWebGL ? SH_WEBGL2_SPEC : SH_GLES3_SPEC;
49             default:
50                 UNREACHABLE();
51         }
52     }
53 
54     // GLES1 emulation: Use GLES3 shader spec.
55     if (!isWebGL && majorVersion == 1)
56     {
57         return SH_GLES3_SPEC;
58     }
59 
60     return isWebGL ? SH_WEBGL_SPEC : SH_GLES2_SPEC;
61 }
62 
63 }  // anonymous namespace
64 
Compiler(rx::GLImplFactory * implFactory,const State & state,egl::Display * display)65 Compiler::Compiler(rx::GLImplFactory *implFactory, const State &state, egl::Display *display)
66     : mImplementation(implFactory->createCompiler()),
67       mSpec(SelectShaderSpec(state.getClientMajorVersion(),
68                              state.getClientMinorVersion(),
69                              state.getExtensions().webglCompatibility,
70                              state.getClientType())),
71       mOutputType(mImplementation->getTranslatorOutputType()),
72       mResources()
73 {
74     // TODO(http://anglebug.com/3819): Update for GL version specific validation
75     ASSERT(state.getClientMajorVersion() == 1 || state.getClientMajorVersion() == 2 ||
76            state.getClientMajorVersion() == 3 || state.getClientMajorVersion() == 4);
77 
78     const gl::Caps &caps             = state.getCaps();
79     const gl::Extensions &extensions = state.getExtensions();
80 
81     {
82         std::lock_guard<std::mutex> lock(display->getDisplayGlobalMutex());
83         if (gActiveCompilers == 0)
84         {
85             sh::Initialize();
86         }
87         ++gActiveCompilers;
88     }
89 
90     sh::InitBuiltInResources(&mResources);
91     mResources.MaxVertexAttribs             = caps.maxVertexAttributes;
92     mResources.MaxVertexUniformVectors      = caps.maxVertexUniformVectors;
93     mResources.MaxVaryingVectors            = caps.maxVaryingVectors;
94     mResources.MaxVertexTextureImageUnits   = caps.maxShaderTextureImageUnits[ShaderType::Vertex];
95     mResources.MaxCombinedTextureImageUnits = caps.maxCombinedTextureImageUnits;
96     mResources.MaxTextureImageUnits         = caps.maxShaderTextureImageUnits[ShaderType::Fragment];
97     mResources.MaxFragmentUniformVectors    = caps.maxFragmentUniformVectors;
98     mResources.MaxDrawBuffers               = caps.maxDrawBuffers;
99     mResources.OES_standard_derivatives     = extensions.standardDerivativesOES;
100     mResources.EXT_draw_buffers             = extensions.drawBuffers;
101     mResources.EXT_shader_texture_lod       = extensions.shaderTextureLOD;
102     mResources.EXT_shader_non_constant_global_initializers =
103         extensions.shaderNonConstGlobalInitializersEXT;
104     mResources.OES_EGL_image_external                = extensions.eglImageExternalOES;
105     mResources.OES_EGL_image_external_essl3          = extensions.eglImageExternalEssl3OES;
106     mResources.NV_EGL_stream_consumer_external       = extensions.eglStreamConsumerExternalNV;
107     mResources.NV_shader_noperspective_interpolation = extensions.noperspectiveInterpolationNV;
108     mResources.ARB_texture_rectangle                 = extensions.textureRectangle;
109     mResources.EXT_gpu_shader5                       = extensions.gpuShader5EXT;
110     mResources.OES_shader_io_blocks                  = extensions.shaderIoBlocksOES;
111     mResources.EXT_shader_io_blocks                  = extensions.shaderIoBlocksEXT;
112     mResources.OES_texture_storage_multisample_2d_array =
113         extensions.textureStorageMultisample2DArrayOES;
114     mResources.OES_texture_3D                  = extensions.texture3DOES;
115     mResources.ANGLE_texture_multisample       = extensions.textureMultisample;
116     mResources.ANGLE_multi_draw                = extensions.multiDraw;
117     mResources.ANGLE_base_vertex_base_instance = extensions.baseVertexBaseInstance;
118     mResources.APPLE_clip_distance             = extensions.clipDistanceAPPLE;
119     // OES_shader_multisample_interpolation
120     mResources.OES_shader_multisample_interpolation = extensions.multisampleInterpolationOES;
121     mResources.OES_shader_image_atomic              = extensions.shaderImageAtomicOES;
122     // TODO: use shader precision caps to determine if high precision is supported?
123     mResources.FragmentPrecisionHigh = 1;
124     mResources.EXT_frag_depth        = extensions.fragDepth;
125 
126     // OVR_multiview state
127     mResources.OVR_multiview = extensions.multiview;
128 
129     // OVR_multiview2 state
130     mResources.OVR_multiview2 = extensions.multiview2;
131     mResources.MaxViewsOVR    = extensions.maxViews;
132 
133     // EXT_multisampled_render_to_texture and EXT_multisampled_render_to_texture2
134     mResources.EXT_multisampled_render_to_texture  = extensions.multisampledRenderToTexture;
135     mResources.EXT_multisampled_render_to_texture2 = extensions.multisampledRenderToTexture2;
136 
137     // WEBGL_video_texture
138     mResources.WEBGL_video_texture = extensions.webglVideoTexture;
139 
140     // OES_texture_cube_map_array
141     mResources.OES_texture_cube_map_array = extensions.textureCubeMapArrayOES;
142     mResources.EXT_texture_cube_map_array = extensions.textureCubeMapArrayEXT;
143 
144     // EXT_shadow_samplers
145     mResources.EXT_shadow_samplers = extensions.shadowSamplersEXT;
146 
147     // OES_texture_buffer
148     mResources.OES_texture_buffer = extensions.textureBufferOES;
149     mResources.EXT_texture_buffer = extensions.textureBufferEXT;
150 
151     // GL_EXT_YUV_target
152     mResources.EXT_YUV_target = extensions.yuvTargetEXT;
153 
154     mResources.EXT_shader_framebuffer_fetch_non_coherent =
155         extensions.shaderFramebufferFetchNonCoherentEXT;
156 
157     // GL_EXT_clip_cull_distance
158     mResources.EXT_clip_cull_distance = extensions.clipCullDistanceEXT;
159 
160     // GLSL ES 3.0 constants
161     mResources.MaxVertexOutputVectors  = caps.maxVertexOutputComponents / 4;
162     mResources.MaxFragmentInputVectors = caps.maxFragmentInputComponents / 4;
163     mResources.MinProgramTexelOffset   = caps.minProgramTexelOffset;
164     mResources.MaxProgramTexelOffset   = caps.maxProgramTexelOffset;
165 
166     // EXT_blend_func_extended
167     mResources.EXT_blend_func_extended  = extensions.blendFuncExtended;
168     mResources.MaxDualSourceDrawBuffers = extensions.maxDualSourceDrawBuffers;
169 
170     // APPLE_clip_distance/EXT_clip_cull_distance
171     mResources.MaxClipDistances                = caps.maxClipDistances;
172     mResources.MaxCullDistances                = caps.maxCullDistances;
173     mResources.MaxCombinedClipAndCullDistances = caps.maxCombinedClipAndCullDistances;
174 
175     // OES_sample_variables
176     mResources.OES_sample_variables = extensions.sampleVariablesOES;
177     mResources.MaxSamples           = caps.maxSamples;
178 
179     // GLSL ES 3.1 constants
180     mResources.MaxProgramTextureGatherOffset    = caps.maxProgramTextureGatherOffset;
181     mResources.MinProgramTextureGatherOffset    = caps.minProgramTextureGatherOffset;
182     mResources.MaxImageUnits                    = caps.maxImageUnits;
183     mResources.MaxVertexImageUniforms           = caps.maxShaderImageUniforms[ShaderType::Vertex];
184     mResources.MaxFragmentImageUniforms         = caps.maxShaderImageUniforms[ShaderType::Fragment];
185     mResources.MaxComputeImageUniforms          = caps.maxShaderImageUniforms[ShaderType::Compute];
186     mResources.MaxCombinedImageUniforms         = caps.maxCombinedImageUniforms;
187     mResources.MaxCombinedShaderOutputResources = caps.maxCombinedShaderOutputResources;
188     mResources.MaxUniformLocations              = caps.maxUniformLocations;
189 
190     for (size_t index = 0u; index < 3u; ++index)
191     {
192         mResources.MaxComputeWorkGroupCount[index] = caps.maxComputeWorkGroupCount[index];
193         mResources.MaxComputeWorkGroupSize[index]  = caps.maxComputeWorkGroupSize[index];
194     }
195 
196     mResources.MaxComputeUniformComponents = caps.maxShaderUniformComponents[ShaderType::Compute];
197     mResources.MaxComputeTextureImageUnits = caps.maxShaderTextureImageUnits[ShaderType::Compute];
198 
199     mResources.MaxComputeAtomicCounters = caps.maxShaderAtomicCounters[ShaderType::Compute];
200     mResources.MaxComputeAtomicCounterBuffers =
201         caps.maxShaderAtomicCounterBuffers[ShaderType::Compute];
202 
203     mResources.MaxVertexAtomicCounters   = caps.maxShaderAtomicCounters[ShaderType::Vertex];
204     mResources.MaxFragmentAtomicCounters = caps.maxShaderAtomicCounters[ShaderType::Fragment];
205     mResources.MaxCombinedAtomicCounters = caps.maxCombinedAtomicCounters;
206     mResources.MaxAtomicCounterBindings  = caps.maxAtomicCounterBufferBindings;
207     mResources.MaxVertexAtomicCounterBuffers =
208         caps.maxShaderAtomicCounterBuffers[ShaderType::Vertex];
209     mResources.MaxFragmentAtomicCounterBuffers =
210         caps.maxShaderAtomicCounterBuffers[ShaderType::Fragment];
211     mResources.MaxCombinedAtomicCounterBuffers = caps.maxCombinedAtomicCounterBuffers;
212     mResources.MaxAtomicCounterBufferSize      = caps.maxAtomicCounterBufferSize;
213 
214     mResources.MaxUniformBufferBindings       = caps.maxUniformBufferBindings;
215     mResources.MaxShaderStorageBufferBindings = caps.maxShaderStorageBufferBindings;
216 
217     // Needed by point size clamping workaround
218     mResources.MaxPointSize = caps.maxAliasedPointSize;
219 
220     if (state.getClientMajorVersion() == 2 && !extensions.drawBuffers)
221     {
222         mResources.MaxDrawBuffers = 1;
223     }
224 
225     // Geometry Shader constants
226     mResources.EXT_geometry_shader          = extensions.geometryShaderEXT;
227     mResources.OES_geometry_shader          = extensions.geometryShaderOES;
228     mResources.MaxGeometryUniformComponents = caps.maxShaderUniformComponents[ShaderType::Geometry];
229     mResources.MaxGeometryUniformBlocks     = caps.maxShaderUniformBlocks[ShaderType::Geometry];
230     mResources.MaxGeometryInputComponents   = caps.maxGeometryInputComponents;
231     mResources.MaxGeometryOutputComponents  = caps.maxGeometryOutputComponents;
232     mResources.MaxGeometryOutputVertices    = caps.maxGeometryOutputVertices;
233     mResources.MaxGeometryTotalOutputComponents = caps.maxGeometryTotalOutputComponents;
234     mResources.MaxGeometryTextureImageUnits = caps.maxShaderTextureImageUnits[ShaderType::Geometry];
235 
236     mResources.MaxGeometryAtomicCounterBuffers =
237         caps.maxShaderAtomicCounterBuffers[ShaderType::Geometry];
238     mResources.MaxGeometryAtomicCounters      = caps.maxShaderAtomicCounters[ShaderType::Geometry];
239     mResources.MaxGeometryShaderStorageBlocks = caps.maxShaderStorageBlocks[ShaderType::Geometry];
240     mResources.MaxGeometryShaderInvocations   = caps.maxGeometryShaderInvocations;
241     mResources.MaxGeometryImageUniforms       = caps.maxShaderImageUniforms[ShaderType::Geometry];
242 
243     // Tessellation Shader constants
244     mResources.EXT_tessellation_shader        = extensions.tessellationShaderEXT;
245     mResources.MaxTessControlInputComponents  = caps.maxTessControlInputComponents;
246     mResources.MaxTessControlOutputComponents = caps.maxTessControlOutputComponents;
247     mResources.MaxTessControlTextureImageUnits =
248         caps.maxShaderTextureImageUnits[ShaderType::TessControl];
249     mResources.MaxTessControlUniformComponents =
250         caps.maxShaderUniformComponents[ShaderType::TessControl];
251     mResources.MaxTessControlTotalOutputComponents = caps.maxTessControlTotalOutputComponents;
252     mResources.MaxTessControlImageUniforms  = caps.maxShaderImageUniforms[ShaderType::TessControl];
253     mResources.MaxTessControlAtomicCounters = caps.maxShaderAtomicCounters[ShaderType::TessControl];
254     mResources.MaxTessControlAtomicCounterBuffers =
255         caps.maxShaderAtomicCounterBuffers[ShaderType::TessControl];
256 
257     mResources.MaxTessPatchComponents = caps.maxTessPatchComponents;
258     mResources.MaxPatchVertices       = caps.maxPatchVertices;
259     mResources.MaxTessGenLevel        = caps.maxTessGenLevel;
260 
261     mResources.MaxTessEvaluationInputComponents  = caps.maxTessEvaluationInputComponents;
262     mResources.MaxTessEvaluationOutputComponents = caps.maxTessEvaluationOutputComponents;
263     mResources.MaxTessEvaluationTextureImageUnits =
264         caps.maxShaderTextureImageUnits[ShaderType::TessEvaluation];
265     mResources.MaxTessEvaluationUniformComponents =
266         caps.maxShaderUniformComponents[ShaderType::TessEvaluation];
267     mResources.MaxTessEvaluationImageUniforms =
268         caps.maxShaderImageUniforms[ShaderType::TessEvaluation];
269     mResources.MaxTessEvaluationAtomicCounters =
270         caps.maxShaderAtomicCounters[ShaderType::TessEvaluation];
271     mResources.MaxTessEvaluationAtomicCounterBuffers =
272         caps.maxShaderAtomicCounterBuffers[ShaderType::TessEvaluation];
273 
274     // Subpixel bits.
275     mResources.SubPixelBits = static_cast<int>(caps.subPixelBits);
276 
277     // Direct-to-metal constants:
278     mResources.DriverUniformsBindingIndex    = caps.driverUniformsBindingIndex;
279     mResources.DefaultUniformsBindingIndex   = caps.defaultUniformsBindingIndex;
280     mResources.UBOArgumentBufferBindingIndex = caps.UBOArgumentBufferBindingIndex;
281 }
282 
283 Compiler::~Compiler() = default;
284 
onDestroy(const Context * context)285 void Compiler::onDestroy(const Context *context)
286 {
287     std::lock_guard<std::mutex> lock(context->getDisplay()->getDisplayGlobalMutex());
288     for (auto &pool : mPools)
289     {
290         for (ShCompilerInstance &instance : pool)
291         {
292             instance.destroy();
293         }
294     }
295     --gActiveCompilers;
296     if (gActiveCompilers == 0)
297     {
298         sh::Finalize();
299     }
300 }
301 
getInstance(ShaderType type)302 ShCompilerInstance Compiler::getInstance(ShaderType type)
303 {
304     ASSERT(type != ShaderType::InvalidEnum);
305     auto &pool = mPools[type];
306     if (pool.empty())
307     {
308         ShHandle handle = sh::ConstructCompiler(ToGLenum(type), mSpec, mOutputType, &mResources);
309         ASSERT(handle);
310         return ShCompilerInstance(handle, mOutputType, type);
311     }
312     else
313     {
314         ShCompilerInstance instance = std::move(pool.back());
315         pool.pop_back();
316         return instance;
317     }
318 }
319 
putInstance(ShCompilerInstance && instance)320 void Compiler::putInstance(ShCompilerInstance &&instance)
321 {
322     static constexpr size_t kMaxPoolSize = 32;
323     auto &pool                           = mPools[instance.getShaderType()];
324     if (pool.size() < kMaxPoolSize)
325     {
326         pool.push_back(std::move(instance));
327     }
328     else
329     {
330         instance.destroy();
331     }
332 }
333 
ShCompilerInstance()334 ShCompilerInstance::ShCompilerInstance() : mHandle(nullptr) {}
335 
ShCompilerInstance(ShHandle handle,ShShaderOutput outputType,ShaderType shaderType)336 ShCompilerInstance::ShCompilerInstance(ShHandle handle,
337                                        ShShaderOutput outputType,
338                                        ShaderType shaderType)
339     : mHandle(handle), mOutputType(outputType), mShaderType(shaderType)
340 {}
341 
~ShCompilerInstance()342 ShCompilerInstance::~ShCompilerInstance()
343 {
344     ASSERT(mHandle == nullptr);
345 }
346 
destroy()347 void ShCompilerInstance::destroy()
348 {
349     if (mHandle != nullptr)
350     {
351         sh::Destruct(mHandle);
352         mHandle = nullptr;
353     }
354 }
355 
ShCompilerInstance(ShCompilerInstance && other)356 ShCompilerInstance::ShCompilerInstance(ShCompilerInstance &&other)
357     : mHandle(other.mHandle), mOutputType(other.mOutputType), mShaderType(other.mShaderType)
358 {
359     other.mHandle = nullptr;
360 }
361 
operator =(ShCompilerInstance && other)362 ShCompilerInstance &ShCompilerInstance::operator=(ShCompilerInstance &&other)
363 {
364     mHandle       = other.mHandle;
365     mOutputType   = other.mOutputType;
366     mShaderType   = other.mShaderType;
367     other.mHandle = nullptr;
368     return *this;
369 }
370 
getHandle()371 ShHandle ShCompilerInstance::getHandle()
372 {
373     return mHandle;
374 }
375 
getShaderType() const376 ShaderType ShCompilerInstance::getShaderType() const
377 {
378     return mShaderType;
379 }
380 
getBuiltinResourcesString()381 const std::string &ShCompilerInstance::getBuiltinResourcesString()
382 {
383     return sh::GetBuiltInResourcesString(mHandle);
384 }
385 
getShaderOutputType() const386 ShShaderOutput ShCompilerInstance::getShaderOutputType() const
387 {
388     return mOutputType;
389 }
390 
391 }  // namespace gl
392