Hash :
d7d42540
        
        Author :
  
        
        Date :
2019-08-21T15:22:49
        
      
Don't build symbol table for GLSL built-ins if on Android The GLSL + ESSL autogenerated symbol table is too large for android, and android also doesn't need desktop GL functionality If on android, compile the ESSL only symbol table Bug: chromium:996286 Change-Id: I14dfc7748dae389e78c35f82a390c67962665356 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1757372 Commit-Queue: Clemen Deng <clemendeng@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
//
// Copyright 2002 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "compiler/translator/BuiltInFunctionEmulatorGLSL.h"
#include "angle_gl.h"
#include "compiler/translator/BuiltInFunctionEmulator.h"
#include "compiler/translator/VersionGLSL.h"
#include "compiler/translator/tree_util/BuiltIn.h"
namespace sh
{
void InitBuiltInAbsFunctionEmulatorForGLSLWorkarounds(BuiltInFunctionEmulator *emu,
                                                      sh::GLenum shaderType)
{
    if (shaderType == GL_VERTEX_SHADER)
    {
        emu->addEmulatedFunction(BuiltInId::abs_Int1, "int abs_emu(int x) { return x * sign(x); }");
    }
}
void InitBuiltInIsnanFunctionEmulatorForGLSLWorkarounds(BuiltInFunctionEmulator *emu,
                                                        int targetGLSLVersion)
{
    // isnan() is supported since GLSL 1.3.
    if (targetGLSLVersion < GLSL_VERSION_130)
        return;
    // !(x > 0.0 || x < 0.0 || x == 0.0) will be optimized and always equal to false.
    emu->addEmulatedFunction(
        BuiltInId::isnan_Float1,
        "bool isnan_emu(float x) { return (x > 0.0 || x < 0.0) ? false : x != 0.0; }");
    emu->addEmulatedFunction(
        BuiltInId::isnan_Float2,
        "bvec2 isnan_emu(vec2 x)\n"
        "{\n"
        "    bvec2 isnan;\n"
        "    for (int i = 0; i < 2; i++)\n"
        "    {\n"
        "        isnan[i] = (x[i] > 0.0 || x[i] < 0.0) ? false : x[i] != 0.0;\n"
        "    }\n"
        "    return isnan;\n"
        "}\n");
    emu->addEmulatedFunction(
        BuiltInId::isnan_Float3,
        "bvec3 isnan_emu(vec3 x)\n"
        "{\n"
        "    bvec3 isnan;\n"
        "    for (int i = 0; i < 3; i++)\n"
        "    {\n"
        "        isnan[i] = (x[i] > 0.0 || x[i] < 0.0) ? false : x[i] != 0.0;\n"
        "    }\n"
        "    return isnan;\n"
        "}\n");
    emu->addEmulatedFunction(
        BuiltInId::isnan_Float4,
        "bvec4 isnan_emu(vec4 x)\n"
        "{\n"
        "    bvec4 isnan;\n"
        "    for (int i = 0; i < 4; i++)\n"
        "    {\n"
        "        isnan[i] = (x[i] > 0.0 || x[i] < 0.0) ? false : x[i] != 0.0;\n"
        "    }\n"
        "    return isnan;\n"
        "}\n");
}
void InitBuiltInAtanFunctionEmulatorForGLSLWorkarounds(BuiltInFunctionEmulator *emu)
{
    emu->addEmulatedFunction(BuiltInId::atan_Float1_Float1,
                             "emu_precision float atan_emu(emu_precision float y, emu_precision "
                             "float x)\n"
                             "{\n"
                             "    if (x > 0.0) return atan(y / x);\n"
                             "    else if (x < 0.0 && y >= 0.0) return atan(y / x) + 3.14159265;\n"
                             "    else if (x < 0.0 && y < 0.0) return atan(y / x) - 3.14159265;\n"
                             "    else return 1.57079632 * sign(y);\n"
                             "}\n");
    static const std::array<TSymbolUniqueId, 4> ids = {
        BuiltInId::atan_Float1_Float1,
        BuiltInId::atan_Float2_Float2,
        BuiltInId::atan_Float3_Float3,
        BuiltInId::atan_Float4_Float4,
    };
    for (int dim = 2; dim <= 4; ++dim)
    {
        std::stringstream ss = sh::InitializeStream<std::stringstream>();
        ss << "emu_precision vec" << dim << " atan_emu(emu_precision vec" << dim
           << " y, emu_precision vec" << dim << " x)\n"
           << "{\n"
              "    return vec"
           << dim << "(";
        for (int i = 0; i < dim; ++i)
        {
            ss << "atan_emu(y[" << i << "], x[" << i << "])";
            if (i < dim - 1)
            {
                ss << ", ";
            }
        }
        ss << ");\n"
              "}\n";
        emu->addEmulatedFunctionWithDependency(BuiltInId::atan_Float1_Float1, ids[dim - 1],
                                               ss.str().c_str());
    }
}
// Emulate built-in functions missing from GLSL 1.30 and higher
void InitBuiltInFunctionEmulatorForGLSLMissingFunctions(BuiltInFunctionEmulator *emu,
                                                        sh::GLenum shaderType,
                                                        int targetGLSLVersion)
{
    // Emulate packUnorm2x16 and unpackUnorm2x16 (GLSL 4.10)
    if (targetGLSLVersion < GLSL_VERSION_410)
    {
        // clang-format off
        emu->addEmulatedFunction(BuiltInId::packUnorm2x16_Float2,
            "uint packUnorm2x16_emu(vec2 v)\n"
            "{\n"
            "    int x = int(round(clamp(v.x, 0.0, 1.0) * 65535.0));\n"
            "    int y = int(round(clamp(v.y, 0.0, 1.0) * 65535.0));\n"
            "    return uint((y << 16) | (x & 0xFFFF));\n"
            "}\n");
        emu->addEmulatedFunction(BuiltInId::unpackUnorm2x16_UInt1,
            "vec2 unpackUnorm2x16_emu(uint u)\n"
            "{\n"
            "    float x = float(u & 0xFFFFu) / 65535.0;\n"
            "    float y = float(u >> 16) / 65535.0;\n"
            "    return vec2(x, y);\n"
            "}\n");
        // clang-format on
    }
    // Emulate packSnorm2x16, packHalf2x16, unpackSnorm2x16, and unpackHalf2x16 (GLSL 4.20)
    // by using floatBitsToInt, floatBitsToUint, intBitsToFloat, and uintBitsToFloat (GLSL 3.30).
    if (targetGLSLVersion >= GLSL_VERSION_330 && targetGLSLVersion < GLSL_VERSION_420)
    {
        // clang-format off
        emu->addEmulatedFunction(BuiltInId::packSnorm2x16_Float2,
            "uint packSnorm2x16_emu(vec2 v)\n"
            "{\n"
            "    #if defined(GL_ARB_shading_language_packing)\n"
            "        return packSnorm2x16(v);\n"
            "    #else\n"
            "        int x = int(round(clamp(v.x, -1.0, 1.0) * 32767.0));\n"
            "        int y = int(round(clamp(v.y, -1.0, 1.0) * 32767.0));\n"
            "        return uint((y << 16) | (x & 0xFFFF));\n"
            "    #endif\n"
            "}\n");
        emu->addEmulatedFunction(BuiltInId::unpackSnorm2x16_UInt1,
            "#if !defined(GL_ARB_shading_language_packing)\n"
            "    float fromSnorm(uint x)\n"
            "    {\n"
            "        int xi = (int(x) & 0x7FFF) - (int(x) & 0x8000);\n"
            "        return clamp(float(xi) / 32767.0, -1.0, 1.0);\n"
            "    }\n"
            "#endif\n"
            "\n"
            "vec2 unpackSnorm2x16_emu(uint u)\n"
            "{\n"
            "    #if defined(GL_ARB_shading_language_packing)\n"
            "        return unpackSnorm2x16(u);\n"
            "    #else\n"
            "        uint y = (u >> 16);\n"
            "        uint x = u;\n"
            "        return vec2(fromSnorm(x), fromSnorm(y));\n"
            "    #endif\n"
            "}\n");
        // Functions uint f32tof16(float val) and float f16tof32(uint val) are
        // based on the OpenGL redbook Appendix Session "Floating-Point Formats Used in OpenGL".
        emu->addEmulatedFunction(BuiltInId::packHalf2x16_Float2,
            "#if !defined(GL_ARB_shading_language_packing)\n"
            "    uint f32tof16(float val)\n"
            "    {\n"
            "        uint f32 = floatBitsToUint(val);\n"
            "        uint f16 = 0u;\n"
            "        uint sign = (f32 >> 16) & 0x8000u;\n"
            "        int exponent = int((f32 >> 23) & 0xFFu) - 127;\n"
            "        uint mantissa = f32 & 0x007FFFFFu;\n"
            "        if (exponent == 128)\n"
            "        {\n"
            "            // Infinity or NaN\n"
            "            // NaN bits that are masked out by 0x3FF get discarded.\n"
            "            // This can turn some NaNs to infinity, but this is allowed by the spec.\n"
            "            f16 = sign | (0x1Fu << 10);\n"
            "            f16 |= (mantissa & 0x3FFu);\n"
            "        }\n"
            "        else if (exponent > 15)\n"
            "        {\n"
            "            // Overflow - flush to Infinity\n"
            "            f16 = sign | (0x1Fu << 10);\n"
            "        }\n"
            "        else if (exponent > -15)\n"
            "        {\n"
            "            // Representable value\n"
            "            exponent += 15;\n"
            "            mantissa >>= 13;\n"
            "            f16 = sign | uint(exponent << 10) | mantissa;\n"
            "        }\n"
            "        else\n"
            "        {\n"
            "            f16 = sign;\n"
            "        }\n"
            "        return f16;\n"
            "    }\n"
            "#endif\n"
            "\n"
            "uint packHalf2x16_emu(vec2 v)\n"
            "{\n"
            "    #if defined(GL_ARB_shading_language_packing)\n"
            "        return packHalf2x16(v);\n"
            "    #else\n"
            "        uint x = f32tof16(v.x);\n"
            "        uint y = f32tof16(v.y);\n"
            "        return (y << 16) | x;\n"
            "    #endif\n"
            "}\n");
        emu->addEmulatedFunction(BuiltInId::unpackHalf2x16_UInt1,
            "#if !defined(GL_ARB_shading_language_packing)\n"
            "    float f16tof32(uint val)\n"
            "    {\n"
            "        uint sign = (val & 0x8000u) << 16;\n"
            "        int exponent = int((val & 0x7C00u) >> 10);\n"
            "        uint mantissa = val & 0x03FFu;\n"
            "        float f32 = 0.0;\n"
            "        if(exponent == 0)\n"
            "        {\n"
            "            if (mantissa != 0u)\n"
            "            {\n"
            "                const float scale = 1.0 / (1 << 24);\n"
            "                f32 = scale * mantissa;\n"
            "            }\n"
            "        }\n"
            "        else if (exponent == 31)\n"
            "        {\n"
            "            return uintBitsToFloat(sign | 0x7F800000u | mantissa);\n"
            "        }\n"
            "        else\n"
            "        {\n"
            "            exponent -= 15;\n"
            "            float scale;\n"
            "            if(exponent < 0)\n"
            "            {\n"
            "                // The negative unary operator is buggy on OSX.\n"
            "                // Work around this by using abs instead.\n"
            "                scale = 1.0 / (1 << abs(exponent));\n"
            "            }\n"
            "            else\n"
            "            {\n"
            "                scale = 1 << exponent;\n"
            "            }\n"
            "            float decimal = 1.0 + float(mantissa) / float(1 << 10);\n"
            "            f32 = scale * decimal;\n"
            "        }\n"
            "\n"
            "        if (sign != 0u)\n"
            "        {\n"
            "            f32 = -f32;\n"
            "        }\n"
            "\n"
            "        return f32;\n"
            "    }\n"
            "#endif\n"
            "\n"
            "vec2 unpackHalf2x16_emu(uint u)\n"
            "{\n"
            "    #if defined(GL_ARB_shading_language_packing)\n"
            "        return unpackHalf2x16(u);\n"
            "    #else\n"
            "        uint y = (u >> 16);\n"
            "        uint x = u & 0xFFFFu;\n"
            "        return vec2(f16tof32(x), f16tof32(y));\n"
            "    #endif\n"
            "}\n");
        // clang-format on
    }
}
}  // namespace sh