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 | |
Martin Radev | 1be913c | 2016-07-11 17:59:16 +0300 | [diff] [blame^] | 26 | ShShaderSpec SelectShaderSpec(GLint majorVersion, GLint minorVersion) |
| 27 | { |
| 28 | if (majorVersion >= 3) |
| 29 | { |
| 30 | if (minorVersion == 1) |
| 31 | { |
| 32 | return SH_GLES3_1_SPEC; |
| 33 | } |
| 34 | else |
| 35 | { |
| 36 | return SH_GLES3_SPEC; |
| 37 | } |
| 38 | } |
| 39 | return SH_GLES2_SPEC; |
| 40 | } |
| 41 | |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 42 | } // anonymous namespace |
| 43 | |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 44 | Compiler::Compiler(rx::GLImplFactory *implFactory, const ContextState &state) |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 45 | : mImplementation(implFactory->createCompiler()), |
Martin Radev | 1be913c | 2016-07-11 17:59:16 +0300 | [diff] [blame^] | 46 | mSpec(SelectShaderSpec(state.getClientMajorVersion(), state.getClientMinorVersion())), |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 47 | mOutputType(mImplementation->getTranslatorOutputType()), |
| 48 | mResources(), |
| 49 | mFragmentCompiler(nullptr), |
| 50 | mVertexCompiler(nullptr) |
| 51 | { |
Martin Radev | 1be913c | 2016-07-11 17:59:16 +0300 | [diff] [blame^] | 52 | ASSERT(state.getClientMajorVersion() == 2 || state.getClientMajorVersion() == 3); |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 53 | |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 54 | const gl::Caps &caps = state.getCaps(); |
| 55 | const gl::Extensions &extensions = state.getExtensions(); |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 56 | |
| 57 | ShInitBuiltInResources(&mResources); |
Jamie Madill | 29f908b | 2016-07-19 23:21:01 +0000 | [diff] [blame] | 58 | mResources.MaxVertexAttribs = caps.maxVertexAttributes; |
| 59 | mResources.MaxVertexUniformVectors = caps.maxVertexUniformVectors; |
| 60 | mResources.MaxVaryingVectors = caps.maxVaryingVectors; |
| 61 | mResources.MaxVertexTextureImageUnits = caps.maxVertexTextureImageUnits; |
| 62 | mResources.MaxCombinedTextureImageUnits = caps.maxCombinedTextureImageUnits; |
| 63 | mResources.MaxTextureImageUnits = caps.maxTextureImageUnits; |
| 64 | mResources.MaxFragmentUniformVectors = caps.maxFragmentUniformVectors; |
| 65 | mResources.MaxDrawBuffers = caps.maxDrawBuffers; |
| 66 | mResources.OES_standard_derivatives = extensions.standardDerivatives; |
| 67 | mResources.EXT_draw_buffers = extensions.drawBuffers; |
| 68 | mResources.EXT_shader_texture_lod = extensions.shaderTextureLOD; |
Geoff Lang | b66a909 | 2016-05-16 15:59:14 -0400 | [diff] [blame] | 69 | mResources.OES_EGL_image_external = extensions.eglImageExternal; |
| 70 | mResources.OES_EGL_image_external_essl3 = extensions.eglImageExternalEssl3; |
Ian Ewell | bda7559 | 2016-04-18 17:25:54 -0400 | [diff] [blame] | 71 | mResources.NV_EGL_stream_consumer_external = extensions.eglStreamConsumerExternal; |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 72 | // TODO: use shader precision caps to determine if high precision is supported? |
| 73 | mResources.FragmentPrecisionHigh = 1; |
| 74 | mResources.EXT_frag_depth = extensions.fragDepth; |
| 75 | |
| 76 | // GLSL ES 3.0 constants |
| 77 | mResources.MaxVertexOutputVectors = caps.maxVertexOutputComponents / 4; |
| 78 | mResources.MaxFragmentInputVectors = caps.maxFragmentInputComponents / 4; |
| 79 | mResources.MinProgramTexelOffset = caps.minProgramTexelOffset; |
| 80 | mResources.MaxProgramTexelOffset = caps.maxProgramTexelOffset; |
Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | Compiler::~Compiler() |
| 84 | { |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 85 | release(); |
| 86 | SafeDelete(mImplementation); |
Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | Error Compiler::release() |
| 90 | { |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 91 | if (mFragmentCompiler) |
| 92 | { |
| 93 | ShDestruct(mFragmentCompiler); |
| 94 | mFragmentCompiler = nullptr; |
| 95 | |
| 96 | ASSERT(activeCompilerHandles > 0); |
| 97 | activeCompilerHandles--; |
| 98 | } |
| 99 | |
| 100 | if (mVertexCompiler) |
| 101 | { |
| 102 | ShDestruct(mVertexCompiler); |
| 103 | mVertexCompiler = nullptr; |
| 104 | |
| 105 | ASSERT(activeCompilerHandles > 0); |
| 106 | activeCompilerHandles--; |
| 107 | } |
| 108 | |
| 109 | if (activeCompilerHandles == 0) |
| 110 | { |
| 111 | ShFinalize(); |
| 112 | } |
| 113 | |
| 114 | mImplementation->release(); |
| 115 | |
| 116 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 119 | ShHandle Compiler::getCompilerHandle(GLenum type) |
Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 120 | { |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 121 | ShHandle *compiler = nullptr; |
| 122 | switch (type) |
| 123 | { |
| 124 | case GL_VERTEX_SHADER: |
| 125 | compiler = &mVertexCompiler; |
| 126 | break; |
| 127 | |
| 128 | case GL_FRAGMENT_SHADER: |
| 129 | compiler = &mFragmentCompiler; |
| 130 | break; |
| 131 | |
| 132 | default: |
| 133 | UNREACHABLE(); |
| 134 | return nullptr; |
| 135 | } |
| 136 | |
| 137 | if (!(*compiler)) |
| 138 | { |
| 139 | if (activeCompilerHandles == 0) |
| 140 | { |
| 141 | ShInitialize(); |
| 142 | } |
| 143 | |
| 144 | *compiler = ShConstructCompiler(type, mSpec, mOutputType, &mResources); |
| 145 | activeCompilerHandles++; |
| 146 | } |
| 147 | |
| 148 | return *compiler; |
Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 149 | } |
| 150 | |
Jamie Madill | 83f349e | 2015-09-23 09:50:36 -0400 | [diff] [blame] | 151 | } // namespace gl |