blob: 7bf507346e67e0afb34e2edeee5a6e70d871677a [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"
14#include "libANGLE/renderer/ImplFactory.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 Madill9082b982016-04-27 15:21:51 -040028Compiler::Compiler(rx::ImplFactory *implFactory, const ContextState &data)
Jamie Madill83f349e2015-09-23 09:50:36 -040029 : mImplementation(implFactory->createCompiler()),
30 mSpec(data.clientVersion > 2 ? SH_GLES3_SPEC : SH_GLES2_SPEC),
31 mOutputType(mImplementation->getTranslatorOutputType()),
32 mResources(),
33 mFragmentCompiler(nullptr),
34 mVertexCompiler(nullptr)
35{
36 ASSERT(data.clientVersion == 2 || data.clientVersion == 3);
37
38 const gl::Caps &caps = *data.caps;
39 const gl::Extensions &extensions = *data.extensions;
40
41 ShInitBuiltInResources(&mResources);
42 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;
53 // TODO: disabled until the extension is actually supported.
54 mResources.OES_EGL_image_external = 0;
Ian Ewellbda75592016-04-18 17:25:54 -040055 mResources.NV_EGL_stream_consumer_external = extensions.eglStreamConsumerExternal;
Jamie Madill83f349e2015-09-23 09:50:36 -040056 // TODO: use shader precision caps to determine if high precision is supported?
57 mResources.FragmentPrecisionHigh = 1;
58 mResources.EXT_frag_depth = extensions.fragDepth;
59
60 // GLSL ES 3.0 constants
61 mResources.MaxVertexOutputVectors = caps.maxVertexOutputComponents / 4;
62 mResources.MaxFragmentInputVectors = caps.maxFragmentInputComponents / 4;
63 mResources.MinProgramTexelOffset = caps.minProgramTexelOffset;
64 mResources.MaxProgramTexelOffset = caps.maxProgramTexelOffset;
Geoff Lang492a7e42014-11-05 13:27:06 -050065}
66
67Compiler::~Compiler()
68{
Jamie Madill83f349e2015-09-23 09:50:36 -040069 release();
70 SafeDelete(mImplementation);
Geoff Lang492a7e42014-11-05 13:27:06 -050071}
72
73Error Compiler::release()
74{
Jamie Madill83f349e2015-09-23 09:50:36 -040075 if (mFragmentCompiler)
76 {
77 ShDestruct(mFragmentCompiler);
78 mFragmentCompiler = nullptr;
79
80 ASSERT(activeCompilerHandles > 0);
81 activeCompilerHandles--;
82 }
83
84 if (mVertexCompiler)
85 {
86 ShDestruct(mVertexCompiler);
87 mVertexCompiler = nullptr;
88
89 ASSERT(activeCompilerHandles > 0);
90 activeCompilerHandles--;
91 }
92
93 if (activeCompilerHandles == 0)
94 {
95 ShFinalize();
96 }
97
98 mImplementation->release();
99
100 return gl::Error(GL_NO_ERROR);
Geoff Lang492a7e42014-11-05 13:27:06 -0500101}
102
Jamie Madill83f349e2015-09-23 09:50:36 -0400103ShHandle Compiler::getCompilerHandle(GLenum type)
Geoff Lang492a7e42014-11-05 13:27:06 -0500104{
Jamie Madill83f349e2015-09-23 09:50:36 -0400105 ShHandle *compiler = nullptr;
106 switch (type)
107 {
108 case GL_VERTEX_SHADER:
109 compiler = &mVertexCompiler;
110 break;
111
112 case GL_FRAGMENT_SHADER:
113 compiler = &mFragmentCompiler;
114 break;
115
116 default:
117 UNREACHABLE();
118 return nullptr;
119 }
120
121 if (!(*compiler))
122 {
123 if (activeCompilerHandles == 0)
124 {
125 ShInitialize();
126 }
127
128 *compiler = ShConstructCompiler(type, mSpec, mOutputType, &mResources);
129 activeCompilerHandles++;
130 }
131
132 return *compiler;
Geoff Lang492a7e42014-11-05 13:27:06 -0500133}
134
Jamie Madill83f349e2015-09-23 09:50:36 -0400135} // namespace gl