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 | 9082b98 | 2016-04-27 15:21:51 -0400 | [diff] [blame] | 12 | #include "libANGLE/ContextState.h" |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 13 | #include "libANGLE/renderer/CompilerImpl.h" |
Jamie Madill | 7aea7e0 | 2016-05-10 10:39:45 -0400 | [diff] [blame^] | 14 | #include "libANGLE/renderer/GLImplFactory.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 | |
Jamie Madill | 7aea7e0 | 2016-05-10 10:39:45 -0400 | [diff] [blame^] | 28 | Compiler::Compiler(rx::GLImplFactory *implFactory, const ContextState &data) |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 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; |
Ian Ewell | bda7559 | 2016-04-18 17:25:54 -0400 | [diff] [blame] | 55 | mResources.NV_EGL_stream_consumer_external = extensions.eglStreamConsumerExternal; |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 56 | // 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 Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | Compiler::~Compiler() |
| 68 | { |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 69 | release(); |
| 70 | SafeDelete(mImplementation); |
Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | Error Compiler::release() |
| 74 | { |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 75 | 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 Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 103 | ShHandle Compiler::getCompilerHandle(GLenum type) |
Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 104 | { |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 105 | 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 Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 135 | } // namespace gl |