Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 1 | // |
| 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 Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 10 | |
| 11 | #include "common/debug.h" |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame^] | 12 | #include "libANGLE/Data.h" |
| 13 | #include "libANGLE/renderer/CompilerImpl.h" |
| 14 | #include "libANGLE/renderer/ImplFactory.h" |
Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 15 | |
| 16 | namespace gl |
| 17 | { |
| 18 | |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame^] | 19 | namespace |
Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 20 | { |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame^] | 21 | |
| 22 | // Global count of active shader compiler handles. Needed to know when to call ShInitialize and |
| 23 | // ShFinalize. |
| 24 | size_t activeCompilerHandles = 0; |
| 25 | |
| 26 | } // anonymous namespace |
| 27 | |
| 28 | Compiler::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 Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | Compiler::~Compiler() |
| 67 | { |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame^] | 68 | release(); |
| 69 | SafeDelete(mImplementation); |
Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | Error Compiler::release() |
| 73 | { |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame^] | 74 | 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 Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 100 | } |
| 101 | |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame^] | 102 | ShHandle Compiler::getCompilerHandle(GLenum type) |
Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 103 | { |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame^] | 104 | 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 Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame^] | 134 | } // namespace gl |