blob: 37346a0fd3831b66b3c3baefc9708da755f863ea [file] [log] [blame]
Geoff Lang492a7e42014-11-05 13:27:06 -05001//
2// Copyright (c) 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"
Geoff Lang492a7e42014-11-05 13:27:06 -050010
11#include "common/debug.h"
Jamie Madill9082b982016-04-27 15:21:51 -040012#include "libANGLE/ContextState.h"
Jamie Madill83f349e2015-09-23 09:50:36 -040013#include "libANGLE/renderer/CompilerImpl.h"
Jamie Madill7aea7e02016-05-10 10:39:45 -040014#include "libANGLE/renderer/GLImplFactory.h"
Geoff Lang492a7e42014-11-05 13:27:06 -050015
16namespace gl
17{
18
Jamie Madill83f349e2015-09-23 09:50:36 -040019namespace
Geoff Lang492a7e42014-11-05 13:27:06 -050020{
Jamie Madill83f349e2015-09-23 09:50:36 -040021
22// Global count of active shader compiler handles. Needed to know when to call ShInitialize and
23// ShFinalize.
24size_t activeCompilerHandles = 0;
25
Martin Radev1be913c2016-07-11 17:59:16 +030026ShShaderSpec SelectShaderSpec(GLint majorVersion, GLint minorVersion)
27{
28 if (majorVersion >= 3)
29 {
30 if (minorVersion == 1)
31 {
32 return SH_GLES3_1_SPEC;
33 }
34 else
35 {
36 return SH_GLES3_SPEC;
37 }
38 }
39 return SH_GLES2_SPEC;
40}
41
Jamie Madill83f349e2015-09-23 09:50:36 -040042} // anonymous namespace
43
Jamie Madilldfde6ab2016-06-09 07:07:18 -070044Compiler::Compiler(rx::GLImplFactory *implFactory, const ContextState &state)
Jamie Madill83f349e2015-09-23 09:50:36 -040045 : mImplementation(implFactory->createCompiler()),
Martin Radev1be913c2016-07-11 17:59:16 +030046 mSpec(SelectShaderSpec(state.getClientMajorVersion(), state.getClientMinorVersion())),
Jamie Madill83f349e2015-09-23 09:50:36 -040047 mOutputType(mImplementation->getTranslatorOutputType()),
48 mResources(),
49 mFragmentCompiler(nullptr),
50 mVertexCompiler(nullptr)
51{
Martin Radev1be913c2016-07-11 17:59:16 +030052 ASSERT(state.getClientMajorVersion() == 2 || state.getClientMajorVersion() == 3);
Jamie Madill83f349e2015-09-23 09:50:36 -040053
Jamie Madilldfde6ab2016-06-09 07:07:18 -070054 const gl::Caps &caps = state.getCaps();
55 const gl::Extensions &extensions = state.getExtensions();
Jamie Madill83f349e2015-09-23 09:50:36 -040056
57 ShInitBuiltInResources(&mResources);
Jamie Madill29f908b2016-07-19 23:21:01 +000058 mResources.MaxVertexAttribs = caps.maxVertexAttributes;
59 mResources.MaxVertexUniformVectors = caps.maxVertexUniformVectors;
60 mResources.MaxVaryingVectors = caps.maxVaryingVectors;
61 mResources.MaxVertexTextureImageUnits = caps.maxVertexTextureImageUnits;
62 mResources.MaxCombinedTextureImageUnits = caps.maxCombinedTextureImageUnits;
63 mResources.MaxTextureImageUnits = caps.maxTextureImageUnits;
64 mResources.MaxFragmentUniformVectors = caps.maxFragmentUniformVectors;
65 mResources.MaxDrawBuffers = caps.maxDrawBuffers;
66 mResources.OES_standard_derivatives = extensions.standardDerivatives;
67 mResources.EXT_draw_buffers = extensions.drawBuffers;
68 mResources.EXT_shader_texture_lod = extensions.shaderTextureLOD;
Geoff Langb66a9092016-05-16 15:59:14 -040069 mResources.OES_EGL_image_external = extensions.eglImageExternal;
70 mResources.OES_EGL_image_external_essl3 = extensions.eglImageExternalEssl3;
Ian Ewellbda75592016-04-18 17:25:54 -040071 mResources.NV_EGL_stream_consumer_external = extensions.eglStreamConsumerExternal;
Jamie Madill83f349e2015-09-23 09:50:36 -040072 // TODO: use shader precision caps to determine if high precision is supported?
73 mResources.FragmentPrecisionHigh = 1;
74 mResources.EXT_frag_depth = extensions.fragDepth;
75
76 // GLSL ES 3.0 constants
77 mResources.MaxVertexOutputVectors = caps.maxVertexOutputComponents / 4;
78 mResources.MaxFragmentInputVectors = caps.maxFragmentInputComponents / 4;
79 mResources.MinProgramTexelOffset = caps.minProgramTexelOffset;
80 mResources.MaxProgramTexelOffset = caps.maxProgramTexelOffset;
Geoff Lang492a7e42014-11-05 13:27:06 -050081}
82
83Compiler::~Compiler()
84{
Jamie Madill83f349e2015-09-23 09:50:36 -040085 release();
86 SafeDelete(mImplementation);
Geoff Lang492a7e42014-11-05 13:27:06 -050087}
88
89Error Compiler::release()
90{
Jamie Madill83f349e2015-09-23 09:50:36 -040091 if (mFragmentCompiler)
92 {
93 ShDestruct(mFragmentCompiler);
94 mFragmentCompiler = nullptr;
95
96 ASSERT(activeCompilerHandles > 0);
97 activeCompilerHandles--;
98 }
99
100 if (mVertexCompiler)
101 {
102 ShDestruct(mVertexCompiler);
103 mVertexCompiler = nullptr;
104
105 ASSERT(activeCompilerHandles > 0);
106 activeCompilerHandles--;
107 }
108
109 if (activeCompilerHandles == 0)
110 {
111 ShFinalize();
112 }
113
114 mImplementation->release();
115
116 return gl::Error(GL_NO_ERROR);
Geoff Lang492a7e42014-11-05 13:27:06 -0500117}
118
Jamie Madill83f349e2015-09-23 09:50:36 -0400119ShHandle Compiler::getCompilerHandle(GLenum type)
Geoff Lang492a7e42014-11-05 13:27:06 -0500120{
Jamie Madill83f349e2015-09-23 09:50:36 -0400121 ShHandle *compiler = nullptr;
122 switch (type)
123 {
124 case GL_VERTEX_SHADER:
125 compiler = &mVertexCompiler;
126 break;
127
128 case GL_FRAGMENT_SHADER:
129 compiler = &mFragmentCompiler;
130 break;
131
132 default:
133 UNREACHABLE();
134 return nullptr;
135 }
136
137 if (!(*compiler))
138 {
139 if (activeCompilerHandles == 0)
140 {
141 ShInitialize();
142 }
143
144 *compiler = ShConstructCompiler(type, mSpec, mOutputType, &mResources);
145 activeCompilerHandles++;
146 }
147
148 return *compiler;
Geoff Lang492a7e42014-11-05 13:27:06 -0500149}
150
Jamie Madill83f349e2015-09-23 09:50:36 -0400151} // namespace gl