blob: 2fc0fe34fea30ac82c211f2bcd8aef93631df10d [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
26} // anonymous namespace
27
Jamie Madilldfde6ab2016-06-09 07:07:18 -070028Compiler::Compiler(rx::GLImplFactory *implFactory, const ContextState &state)
Jamie Madill83f349e2015-09-23 09:50:36 -040029 : mImplementation(implFactory->createCompiler()),
Jamie Madilldfde6ab2016-06-09 07:07:18 -070030 mSpec(state.getClientVersion() > 2 ? SH_GLES3_SPEC : SH_GLES2_SPEC),
Jamie Madill83f349e2015-09-23 09:50:36 -040031 mOutputType(mImplementation->getTranslatorOutputType()),
32 mResources(),
33 mFragmentCompiler(nullptr),
34 mVertexCompiler(nullptr)
35{
Jamie Madilldfde6ab2016-06-09 07:07:18 -070036 ASSERT(state.getClientVersion() == 2 || state.getClientVersion() == 3);
Jamie Madill83f349e2015-09-23 09:50:36 -040037
Jamie Madilldfde6ab2016-06-09 07:07:18 -070038 const gl::Caps &caps = state.getCaps();
39 const gl::Extensions &extensions = state.getExtensions();
Jamie Madill83f349e2015-09-23 09:50:36 -040040
41 ShInitBuiltInResources(&mResources);
Sami Väisänen7f539ea2016-05-20 13:29:08 +030042 mResources.MaxVertexAttribs = caps.maxVertexAttributes;
43 mResources.MaxVertexUniformVectors = caps.maxVertexUniformVectors;
44 mResources.MaxVaryingVectors = caps.maxVaryingVectors;
45 mResources.MaxVertexTextureImageUnits = caps.maxVertexTextureImageUnits;
46 mResources.MaxCombinedTextureImageUnits = caps.maxCombinedTextureImageUnits;
47 mResources.MaxTextureImageUnits = caps.maxTextureImageUnits;
48 mResources.MaxFragmentUniformVectors = caps.maxFragmentUniformVectors;
49 mResources.MaxDrawBuffers = caps.maxDrawBuffers;
50 mResources.OES_standard_derivatives = extensions.standardDerivatives;
51 mResources.EXT_draw_buffers = extensions.drawBuffers;
52 mResources.EXT_shader_texture_lod = extensions.shaderTextureLOD;
Geoff Langb66a9092016-05-16 15:59:14 -040053 mResources.OES_EGL_image_external = extensions.eglImageExternal;
54 mResources.OES_EGL_image_external_essl3 = extensions.eglImageExternalEssl3;
Sami Väisänen7f539ea2016-05-20 13:29:08 +030055 mResources.EXT_blend_func_extended = extensions.blendFuncExtended;
Ian Ewellbda75592016-04-18 17:25:54 -040056 mResources.NV_EGL_stream_consumer_external = extensions.eglStreamConsumerExternal;
Jamie Madill83f349e2015-09-23 09:50:36 -040057 // TODO: use shader precision caps to determine if high precision is supported?
58 mResources.FragmentPrecisionHigh = 1;
59 mResources.EXT_frag_depth = extensions.fragDepth;
60
61 // GLSL ES 3.0 constants
62 mResources.MaxVertexOutputVectors = caps.maxVertexOutputComponents / 4;
63 mResources.MaxFragmentInputVectors = caps.maxFragmentInputComponents / 4;
64 mResources.MinProgramTexelOffset = caps.minProgramTexelOffset;
65 mResources.MaxProgramTexelOffset = caps.maxProgramTexelOffset;
Sami Väisänen7f539ea2016-05-20 13:29:08 +030066
67 // EXT_blend_func_extended
68 mResources.MaxDualSourceDrawBuffers = extensions.maxDualSourceDrawBuffers;
Geoff Lang492a7e42014-11-05 13:27:06 -050069}
70
71Compiler::~Compiler()
72{
Jamie Madill83f349e2015-09-23 09:50:36 -040073 release();
74 SafeDelete(mImplementation);
Geoff Lang492a7e42014-11-05 13:27:06 -050075}
76
77Error Compiler::release()
78{
Jamie Madill83f349e2015-09-23 09:50:36 -040079 if (mFragmentCompiler)
80 {
81 ShDestruct(mFragmentCompiler);
82 mFragmentCompiler = nullptr;
83
84 ASSERT(activeCompilerHandles > 0);
85 activeCompilerHandles--;
86 }
87
88 if (mVertexCompiler)
89 {
90 ShDestruct(mVertexCompiler);
91 mVertexCompiler = nullptr;
92
93 ASSERT(activeCompilerHandles > 0);
94 activeCompilerHandles--;
95 }
96
97 if (activeCompilerHandles == 0)
98 {
99 ShFinalize();
100 }
101
102 mImplementation->release();
103
104 return gl::Error(GL_NO_ERROR);
Geoff Lang492a7e42014-11-05 13:27:06 -0500105}
106
Jamie Madill83f349e2015-09-23 09:50:36 -0400107ShHandle Compiler::getCompilerHandle(GLenum type)
Geoff Lang492a7e42014-11-05 13:27:06 -0500108{
Jamie Madill83f349e2015-09-23 09:50:36 -0400109 ShHandle *compiler = nullptr;
110 switch (type)
111 {
112 case GL_VERTEX_SHADER:
113 compiler = &mVertexCompiler;
114 break;
115
116 case GL_FRAGMENT_SHADER:
117 compiler = &mFragmentCompiler;
118 break;
119
120 default:
121 UNREACHABLE();
122 return nullptr;
123 }
124
125 if (!(*compiler))
126 {
127 if (activeCompilerHandles == 0)
128 {
129 ShInitialize();
130 }
131
132 *compiler = ShConstructCompiler(type, mSpec, mOutputType, &mResources);
133 activeCompilerHandles++;
134 }
135
136 return *compiler;
Geoff Lang492a7e42014-11-05 13:27:06 -0500137}
138
Jamie Madill83f349e2015-09-23 09:50:36 -0400139} // namespace gl