blob: 348c41bef33d7df7e88f6c23dd1ddca46c87a9d6 [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 Madill83f349e2015-09-23 09:50:36 -040012#include "libANGLE/Data.h"
13#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
28Compiler::Compiler(rx::ImplFactory *implFactory, const gl::Data &data)
29 : 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;
55 // TODO: use shader precision caps to determine if high precision is supported?
56 mResources.FragmentPrecisionHigh = 1;
57 mResources.EXT_frag_depth = extensions.fragDepth;
58
59 // GLSL ES 3.0 constants
60 mResources.MaxVertexOutputVectors = caps.maxVertexOutputComponents / 4;
61 mResources.MaxFragmentInputVectors = caps.maxFragmentInputComponents / 4;
62 mResources.MinProgramTexelOffset = caps.minProgramTexelOffset;
63 mResources.MaxProgramTexelOffset = caps.maxProgramTexelOffset;
Geoff Lang492a7e42014-11-05 13:27:06 -050064}
65
66Compiler::~Compiler()
67{
Jamie Madill83f349e2015-09-23 09:50:36 -040068 release();
69 SafeDelete(mImplementation);
Geoff Lang492a7e42014-11-05 13:27:06 -050070}
71
72Error Compiler::release()
73{
Jamie Madill83f349e2015-09-23 09:50:36 -040074 if (mFragmentCompiler)
75 {
76 ShDestruct(mFragmentCompiler);
77 mFragmentCompiler = nullptr;
78
79 ASSERT(activeCompilerHandles > 0);
80 activeCompilerHandles--;
81 }
82
83 if (mVertexCompiler)
84 {
85 ShDestruct(mVertexCompiler);
86 mVertexCompiler = nullptr;
87
88 ASSERT(activeCompilerHandles > 0);
89 activeCompilerHandles--;
90 }
91
92 if (activeCompilerHandles == 0)
93 {
94 ShFinalize();
95 }
96
97 mImplementation->release();
98
99 return gl::Error(GL_NO_ERROR);
Geoff Lang492a7e42014-11-05 13:27:06 -0500100}
101
Jamie Madill83f349e2015-09-23 09:50:36 -0400102ShHandle Compiler::getCompilerHandle(GLenum type)
Geoff Lang492a7e42014-11-05 13:27:06 -0500103{
Jamie Madill83f349e2015-09-23 09:50:36 -0400104 ShHandle *compiler = nullptr;
105 switch (type)
106 {
107 case GL_VERTEX_SHADER:
108 compiler = &mVertexCompiler;
109 break;
110
111 case GL_FRAGMENT_SHADER:
112 compiler = &mFragmentCompiler;
113 break;
114
115 default:
116 UNREACHABLE();
117 return nullptr;
118 }
119
120 if (!(*compiler))
121 {
122 if (activeCompilerHandles == 0)
123 {
124 ShInitialize();
125 }
126
127 *compiler = ShConstructCompiler(type, mSpec, mOutputType, &mResources);
128 activeCompilerHandles++;
129 }
130
131 return *compiler;
Geoff Lang492a7e42014-11-05 13:27:06 -0500132}
133
Jamie Madill83f349e2015-09-23 09:50:36 -0400134} // namespace gl