• 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.isWebGL(),
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.drawBuffersEXT;
101     mResources.EXT_shader_texture_lod       = extensions.shaderTextureLodEXT;
102     mResources.EXT_shader_non_constant_global_initializers =
103         extensions.shaderNonConstantGlobalInitializersEXT;
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 =
108         extensions.shaderNoperspectiveInterpolationNV;
109     mResources.ARB_texture_rectangle = extensions.textureRectangleANGLE;
110     mResources.EXT_gpu_shader5       = extensions.gpuShader5EXT;
111     mResources.OES_shader_io_blocks  = extensions.shaderIoBlocksOES;
112     mResources.EXT_shader_io_blocks  = extensions.shaderIoBlocksEXT;
113     mResources.OES_texture_storage_multisample_2d_array =
114         extensions.textureStorageMultisample2dArrayOES;
115     mResources.OES_texture_3D            = extensions.texture3DOES;
116     mResources.ANGLE_texture_multisample = extensions.textureMultisampleANGLE;
117     mResources.ANGLE_multi_draw          = extensions.multiDrawANGLE;
118     mResources.ANGLE_base_vertex_base_instance_shader_builtin =
119         extensions.baseVertexBaseInstanceShaderBuiltinANGLE;
120     mResources.APPLE_clip_distance = extensions.clipDistanceAPPLE;
121     // OES_shader_multisample_interpolation
122     mResources.OES_shader_multisample_interpolation = extensions.shaderMultisampleInterpolationOES;
123     mResources.OES_shader_image_atomic              = extensions.shaderImageAtomicOES;
124     // TODO: use shader precision caps to determine if high precision is supported?
125     mResources.FragmentPrecisionHigh = 1;
126     mResources.EXT_frag_depth        = extensions.fragDepthEXT;
127 
128     // OVR_multiview state
129     mResources.OVR_multiview = extensions.multiviewOVR;
130 
131     // OVR_multiview2 state
132     mResources.OVR_multiview2 = extensions.multiview2OVR;
133     mResources.MaxViewsOVR    = caps.maxViews;
134 
135     // EXT_multisampled_render_to_texture and EXT_multisampled_render_to_texture2
136     mResources.EXT_multisampled_render_to_texture  = extensions.multisampledRenderToTextureEXT;
137     mResources.EXT_multisampled_render_to_texture2 = extensions.multisampledRenderToTexture2EXT;
138 
139     // WEBGL_video_texture
140     mResources.WEBGL_video_texture = extensions.videoTextureWEBGL;
141 
142     // OES_texture_cube_map_array
143     mResources.OES_texture_cube_map_array = extensions.textureCubeMapArrayOES;
144     mResources.EXT_texture_cube_map_array = extensions.textureCubeMapArrayEXT;
145 
146     // EXT_shadow_samplers
147     mResources.EXT_shadow_samplers = extensions.shadowSamplersEXT;
148 
149     // OES_texture_buffer
150     mResources.OES_texture_buffer = extensions.textureBufferOES;
151     mResources.EXT_texture_buffer = extensions.textureBufferEXT;
152 
153     // GL_EXT_YUV_target
154     mResources.EXT_YUV_target = extensions.YUVTargetEXT;
155 
156     mResources.EXT_shader_framebuffer_fetch_non_coherent =
157         extensions.shaderFramebufferFetchNonCoherentEXT;
158 
159     // GL_EXT_clip_cull_distance
160     mResources.EXT_clip_cull_distance = extensions.clipCullDistanceEXT;
161 
162     // GL_EXT_primitive_bounding_box
163     mResources.EXT_primitive_bounding_box = extensions.primitiveBoundingBoxEXT;
164 
165     // GLSL ES 3.0 constants
166     mResources.MaxVertexOutputVectors  = caps.maxVertexOutputComponents / 4;
167     mResources.MaxFragmentInputVectors = caps.maxFragmentInputComponents / 4;
168     mResources.MinProgramTexelOffset   = caps.minProgramTexelOffset;
169     mResources.MaxProgramTexelOffset   = caps.maxProgramTexelOffset;
170 
171     // EXT_blend_func_extended
172     mResources.EXT_blend_func_extended  = extensions.blendFuncExtendedEXT;
173     mResources.MaxDualSourceDrawBuffers = caps.maxDualSourceDrawBuffers;
174 
175     // APPLE_clip_distance/EXT_clip_cull_distance
176     mResources.MaxClipDistances                = caps.maxClipDistances;
177     mResources.MaxCullDistances                = caps.maxCullDistances;
178     mResources.MaxCombinedClipAndCullDistances = caps.maxCombinedClipAndCullDistances;
179 
180     // OES_sample_variables
181     mResources.OES_sample_variables = extensions.sampleVariablesOES;
182     mResources.MaxSamples           = caps.maxSamples;
183 
184     // GLSL ES 3.1 constants
185     mResources.MaxProgramTextureGatherOffset    = caps.maxProgramTextureGatherOffset;
186     mResources.MinProgramTextureGatherOffset    = caps.minProgramTextureGatherOffset;
187     mResources.MaxImageUnits                    = caps.maxImageUnits;
188     mResources.MaxVertexImageUniforms           = caps.maxShaderImageUniforms[ShaderType::Vertex];
189     mResources.MaxFragmentImageUniforms         = caps.maxShaderImageUniforms[ShaderType::Fragment];
190     mResources.MaxComputeImageUniforms          = caps.maxShaderImageUniforms[ShaderType::Compute];
191     mResources.MaxCombinedImageUniforms         = caps.maxCombinedImageUniforms;
192     mResources.MaxCombinedShaderOutputResources = caps.maxCombinedShaderOutputResources;
193     mResources.MaxUniformLocations              = caps.maxUniformLocations;
194 
195     for (size_t index = 0u; index < 3u; ++index)
196     {
197         mResources.MaxComputeWorkGroupCount[index] = caps.maxComputeWorkGroupCount[index];
198         mResources.MaxComputeWorkGroupSize[index]  = caps.maxComputeWorkGroupSize[index];
199     }
200 
201     mResources.MaxComputeUniformComponents = caps.maxShaderUniformComponents[ShaderType::Compute];
202     mResources.MaxComputeTextureImageUnits = caps.maxShaderTextureImageUnits[ShaderType::Compute];
203 
204     mResources.MaxComputeAtomicCounters = caps.maxShaderAtomicCounters[ShaderType::Compute];
205     mResources.MaxComputeAtomicCounterBuffers =
206         caps.maxShaderAtomicCounterBuffers[ShaderType::Compute];
207 
208     mResources.MaxVertexAtomicCounters   = caps.maxShaderAtomicCounters[ShaderType::Vertex];
209     mResources.MaxFragmentAtomicCounters = caps.maxShaderAtomicCounters[ShaderType::Fragment];
210     mResources.MaxCombinedAtomicCounters = caps.maxCombinedAtomicCounters;
211     mResources.MaxAtomicCounterBindings  = caps.maxAtomicCounterBufferBindings;
212     mResources.MaxVertexAtomicCounterBuffers =
213         caps.maxShaderAtomicCounterBuffers[ShaderType::Vertex];
214     mResources.MaxFragmentAtomicCounterBuffers =
215         caps.maxShaderAtomicCounterBuffers[ShaderType::Fragment];
216     mResources.MaxCombinedAtomicCounterBuffers = caps.maxCombinedAtomicCounterBuffers;
217     mResources.MaxAtomicCounterBufferSize      = caps.maxAtomicCounterBufferSize;
218 
219     mResources.MaxUniformBufferBindings       = caps.maxUniformBufferBindings;
220     mResources.MaxShaderStorageBufferBindings = caps.maxShaderStorageBufferBindings;
221 
222     // Needed by point size clamping workaround
223     mResources.MaxPointSize = caps.maxAliasedPointSize;
224 
225     if (state.getClientMajorVersion() == 2 && !extensions.drawBuffersEXT)
226     {
227         mResources.MaxDrawBuffers = 1;
228     }
229 
230     // Geometry Shader constants
231     mResources.EXT_geometry_shader          = extensions.geometryShaderEXT;
232     mResources.OES_geometry_shader          = extensions.geometryShaderOES;
233     mResources.MaxGeometryUniformComponents = caps.maxShaderUniformComponents[ShaderType::Geometry];
234     mResources.MaxGeometryUniformBlocks     = caps.maxShaderUniformBlocks[ShaderType::Geometry];
235     mResources.MaxGeometryInputComponents   = caps.maxGeometryInputComponents;
236     mResources.MaxGeometryOutputComponents  = caps.maxGeometryOutputComponents;
237     mResources.MaxGeometryOutputVertices    = caps.maxGeometryOutputVertices;
238     mResources.MaxGeometryTotalOutputComponents = caps.maxGeometryTotalOutputComponents;
239     mResources.MaxGeometryTextureImageUnits = caps.maxShaderTextureImageUnits[ShaderType::Geometry];
240 
241     mResources.MaxGeometryAtomicCounterBuffers =
242         caps.maxShaderAtomicCounterBuffers[ShaderType::Geometry];
243     mResources.MaxGeometryAtomicCounters      = caps.maxShaderAtomicCounters[ShaderType::Geometry];
244     mResources.MaxGeometryShaderStorageBlocks = caps.maxShaderStorageBlocks[ShaderType::Geometry];
245     mResources.MaxGeometryShaderInvocations   = caps.maxGeometryShaderInvocations;
246     mResources.MaxGeometryImageUniforms       = caps.maxShaderImageUniforms[ShaderType::Geometry];
247 
248     // Tessellation Shader constants
249     mResources.EXT_tessellation_shader        = extensions.tessellationShaderEXT;
250     mResources.MaxTessControlInputComponents  = caps.maxTessControlInputComponents;
251     mResources.MaxTessControlOutputComponents = caps.maxTessControlOutputComponents;
252     mResources.MaxTessControlTextureImageUnits =
253         caps.maxShaderTextureImageUnits[ShaderType::TessControl];
254     mResources.MaxTessControlUniformComponents =
255         caps.maxShaderUniformComponents[ShaderType::TessControl];
256     mResources.MaxTessControlTotalOutputComponents = caps.maxTessControlTotalOutputComponents;
257     mResources.MaxTessControlImageUniforms  = caps.maxShaderImageUniforms[ShaderType::TessControl];
258     mResources.MaxTessControlAtomicCounters = caps.maxShaderAtomicCounters[ShaderType::TessControl];
259     mResources.MaxTessControlAtomicCounterBuffers =
260         caps.maxShaderAtomicCounterBuffers[ShaderType::TessControl];
261 
262     mResources.MaxTessPatchComponents = caps.maxTessPatchComponents;
263     mResources.MaxPatchVertices       = caps.maxPatchVertices;
264     mResources.MaxTessGenLevel        = caps.maxTessGenLevel;
265 
266     mResources.MaxTessEvaluationInputComponents  = caps.maxTessEvaluationInputComponents;
267     mResources.MaxTessEvaluationOutputComponents = caps.maxTessEvaluationOutputComponents;
268     mResources.MaxTessEvaluationTextureImageUnits =
269         caps.maxShaderTextureImageUnits[ShaderType::TessEvaluation];
270     mResources.MaxTessEvaluationUniformComponents =
271         caps.maxShaderUniformComponents[ShaderType::TessEvaluation];
272     mResources.MaxTessEvaluationImageUniforms =
273         caps.maxShaderImageUniforms[ShaderType::TessEvaluation];
274     mResources.MaxTessEvaluationAtomicCounters =
275         caps.maxShaderAtomicCounters[ShaderType::TessEvaluation];
276     mResources.MaxTessEvaluationAtomicCounterBuffers =
277         caps.maxShaderAtomicCounterBuffers[ShaderType::TessEvaluation];
278 
279     // Subpixel bits.
280     mResources.SubPixelBits = static_cast<int>(caps.subPixelBits);
281 
282     // Direct-to-metal constants:
283     mResources.DriverUniformsBindingIndex    = caps.driverUniformsBindingIndex;
284     mResources.DefaultUniformsBindingIndex   = caps.defaultUniformsBindingIndex;
285     mResources.UBOArgumentBufferBindingIndex = caps.UBOArgumentBufferBindingIndex;
286 }
287 
288 Compiler::~Compiler() = default;
289 
onDestroy(const Context * context)290 void Compiler::onDestroy(const Context *context)
291 {
292     std::lock_guard<std::mutex> lock(context->getDisplay()->getDisplayGlobalMutex());
293     for (auto &pool : mPools)
294     {
295         for (ShCompilerInstance &instance : pool)
296         {
297             instance.destroy();
298         }
299     }
300     --gActiveCompilers;
301     if (gActiveCompilers == 0)
302     {
303         sh::Finalize();
304     }
305 }
306 
getInstance(ShaderType type)307 ShCompilerInstance Compiler::getInstance(ShaderType type)
308 {
309     ASSERT(type != ShaderType::InvalidEnum);
310     auto &pool = mPools[type];
311     if (pool.empty())
312     {
313         ShHandle handle = sh::ConstructCompiler(ToGLenum(type), mSpec, mOutputType, &mResources);
314         ASSERT(handle);
315         return ShCompilerInstance(handle, mOutputType, type);
316     }
317     else
318     {
319         ShCompilerInstance instance = std::move(pool.back());
320         pool.pop_back();
321         return instance;
322     }
323 }
324 
putInstance(ShCompilerInstance && instance)325 void Compiler::putInstance(ShCompilerInstance &&instance)
326 {
327     static constexpr size_t kMaxPoolSize = 32;
328     auto &pool                           = mPools[instance.getShaderType()];
329     if (pool.size() < kMaxPoolSize)
330     {
331         pool.push_back(std::move(instance));
332     }
333     else
334     {
335         instance.destroy();
336     }
337 }
338 
ShCompilerInstance()339 ShCompilerInstance::ShCompilerInstance() : mHandle(nullptr) {}
340 
ShCompilerInstance(ShHandle handle,ShShaderOutput outputType,ShaderType shaderType)341 ShCompilerInstance::ShCompilerInstance(ShHandle handle,
342                                        ShShaderOutput outputType,
343                                        ShaderType shaderType)
344     : mHandle(handle), mOutputType(outputType), mShaderType(shaderType)
345 {}
346 
~ShCompilerInstance()347 ShCompilerInstance::~ShCompilerInstance()
348 {
349     ASSERT(mHandle == nullptr);
350 }
351 
destroy()352 void ShCompilerInstance::destroy()
353 {
354     if (mHandle != nullptr)
355     {
356         sh::Destruct(mHandle);
357         mHandle = nullptr;
358     }
359 }
360 
ShCompilerInstance(ShCompilerInstance && other)361 ShCompilerInstance::ShCompilerInstance(ShCompilerInstance &&other)
362     : mHandle(other.mHandle), mOutputType(other.mOutputType), mShaderType(other.mShaderType)
363 {
364     other.mHandle = nullptr;
365 }
366 
operator =(ShCompilerInstance && other)367 ShCompilerInstance &ShCompilerInstance::operator=(ShCompilerInstance &&other)
368 {
369     mHandle       = other.mHandle;
370     mOutputType   = other.mOutputType;
371     mShaderType   = other.mShaderType;
372     other.mHandle = nullptr;
373     return *this;
374 }
375 
getHandle()376 ShHandle ShCompilerInstance::getHandle()
377 {
378     return mHandle;
379 }
380 
getShaderType() const381 ShaderType ShCompilerInstance::getShaderType() const
382 {
383     return mShaderType;
384 }
385 
getBuiltinResourcesString()386 const std::string &ShCompilerInstance::getBuiltinResourcesString()
387 {
388     return sh::GetBuiltInResourcesString(mHandle);
389 }
390 
getShaderOutputType() const391 ShShaderOutput ShCompilerInstance::getShaderOutputType() const
392 {
393     return mOutputType;
394 }
395 
396 }  // namespace gl
397