Hash :
f4e776a8
        
        Author :
  
        
        Date :
2024-10-14T22:41:34
        
      
GLES1: Fix eye distance for fog
Distance should be non-negative, so use abs().
From the spec, OpenGL ES 1.1 §3.8. Note the 'abs' symbols.
    An implementation may choose to approximate the eye-coordinate
    distance from the eye to each fragment center by |z_{e}|.
Bug: b/369665616
Test: angle_trace_tests --gtest_filter="*minetest*"
Change-Id: Ic6c162ba2469600fa6a8c8d61e5bccf5c0cb12d9
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5933566
Reviewed-by: Charlie Lao <cclao@google.com>
Commit-Queue: Lina Versace <linyaa@google.com>
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Commit-Queue: Cody Northrop <cnorthrop@google.com>
Reviewed-by: Lina Versace <linyaa@google.com>
      
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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
//
// Copyright 2018 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.
//
// GLES1Shaders.inc: Defines GLES1 emulation shader.
//
// According to the GLES1 specification:
//
// We require simply that numbers' floating point parts contain enough bits and that their exponent
// fields are large enough so that individual results of floating-point operations are accurate to
// about 1 part in 10^5.  The maximum representable magnitude of a floating-point number used to
// represent positional or normal coordinates must be at least 2^32; the maximum representable
// magnitude for colors or texture coordinates must be at least 2^10.  The maximum representable
// magnitude for all other floating-point values must be at least 2^32 .
//
// Internal computations can use either fixed-point or floating-point arithmetic.  Fixed-point
// computations must be accurate to within ±2^-15.  The maximum representable magnitude for a
// fixed-point number used to represent positional or normal coordinates must be at least 2^15; the
// maximum representable magnitude for colors or texture coordinates must be at least 2^10.  The
// maximum representable magnitude for all other fixed-point values must be at least 2^15
//
// Accordingly, ANGLE uses highp floats for position and normal data, mediump for color and texture
// coordinates, and highp for everything else.
// The following variables are added in GLES1Renderer::initializeRendererProgram
// #define kTexUnits
// bool clip_plane_enables
// bool enable_alpha_test
// bool enable_clip_planes
// bool enable_color_material
// bool enable_draw_texture
// bool enable_fog
// bool enable_lighting
// bool enable_normalize
// bool enable_rescale_normal
// bool enable_texture_2d[kMaxTexUnits]
// bool enable_texture_cube_map[kMaxTexUnits]
// bool light_enables[kMaxLights]
// bool light_model_two_sided
// bool point_rasterization
// bool point_sprite_coord_replace
// bool point_sprite_enabled
// bool shade_model_flat
// uint texture_format[kMaxTexUnits];
// uint texture_env_mode[kMaxTexUnits];
// uint combine_rgb[kMaxTexUnits];
// uint combine_alpha[kMaxTexUnits];
// uint src0_rgb[kMaxTexUnits];
// uint src0_alpha[kMaxTexUnits];
// uint src1_rgb[kMaxTexUnits];
// uint src1_alpha[kMaxTexUnits];
// uint src2_rgb[kMaxTexUnits];
// uint src2_alpha[kMaxTexUnits];
// uint op0_rgb[kMaxTexUnits];
// uint op0_alpha[kMaxTexUnits];
// uint op1_rgb[kMaxTexUnits];
// uint op1_alpha[kMaxTexUnits];
// uint op2_rgb[kMaxTexUnits];
// uint op2_alpha[kMaxTexUnits];
// uint alpha_func;
// uint fog_mode;
constexpr char kGLES1TexUnitsDefine[] = R"(#define kTexUnits )";
constexpr char kGLES1DrawVShaderHeader[] = R"(#version 300 es
precision highp float;
#define kMaxTexUnits 4u
#define kMaxLights   8u
)";
constexpr char kGLES1DrawVShader[] = R"(
in vec4 pos;
in vec3 normal;
in mediump vec4 color;
in float pointsize;
#if kTexUnits >= 1u
in mediump vec4 texcoord0;
#endif
#if kTexUnits >= 2u
in mediump vec4 texcoord1;
#endif
#if kTexUnits >= 3u
in mediump vec4 texcoord2;
#endif
#if kTexUnits >= 4u
in mediump vec4 texcoord3;
#endif
uniform mat4 projection;
uniform mat4 modelview;
uniform mat4 modelview_invtr;
uniform mat4 texture_matrix[kMaxTexUnits];
// Point rasterization//////////////////////////////////////////////////////////
uniform float point_size_min;
uniform float point_size_max;
uniform vec3 point_distance_attenuation;
// Shading: flat shading, lighting, and materials///////////////////////////////
uniform mediump vec4 material_ambient;
uniform mediump vec4 material_diffuse;
uniform mediump vec4 material_specular;
uniform mediump vec4 material_emissive;
uniform float material_specular_exponent;
uniform mediump vec4 light_model_scene_ambient;
uniform mediump vec4 light_ambients[kMaxLights];
uniform mediump vec4 light_diffuses[kMaxLights];
uniform mediump vec4 light_speculars[kMaxLights];
uniform vec4 light_positions[kMaxLights];
uniform vec3 light_directions[kMaxLights];
uniform float light_spotlight_exponents[kMaxLights];
uniform float light_spotlight_cutoff_angles[kMaxLights];
uniform float light_attenuation_consts[kMaxLights];
uniform float light_attenuation_linears[kMaxLights];
uniform float light_attenuation_quadratics[kMaxLights];
// GL_OES_draw_texture uniforms/////////////////////////////////////////////////
uniform vec4 draw_texture_coords;
uniform vec2 draw_texture_dims;
uniform mediump vec4 draw_texture_normalized_crop_rect[kMaxTexUnits];
// Varyings/////////////////////////////////////////////////////////////////////
out vec4 pos_varying;
out vec3 normal_varying;
out mediump vec4 color_varying;
flat out mediump vec4 color_varying_flat;
#if kTexUnits >= 1u
out mediump vec3 texcoord0_varying;
#endif
#if kTexUnits >= 2u
out mediump vec3 texcoord1_varying;
#endif
#if kTexUnits >= 3u
out mediump vec3 texcoord2_varying;
#endif
#if kTexUnits >= 4u
out mediump vec3 texcoord3_varying;
#endif
float posDot(vec3 a, vec3 b)
{
    return max(dot(a, b), 0.0);
}
mediump vec4 doLighting(mediump vec4 vertexColor)
{
    mediump vec4 materialAmbientActual = material_ambient;
    mediump vec4 materialDiffuseActual = material_diffuse;
    if (enable_color_material)
    {
        materialAmbientActual = vertexColor;
        materialDiffuseActual = vertexColor;
    }
    mediump vec4 lightingResult = material_emissive + materialAmbientActual * light_model_scene_ambient;
    for (uint i = 0u; i < kMaxLights; i++)
    {
        if (!light_enables[i])
            continue;
        mediump vec4 lightAmbient  = light_ambients[i];
        mediump vec4 lightDiffuse  = light_diffuses[i];
        mediump vec4 lightSpecular = light_speculars[i];
        vec4 lightPos      = light_positions[i];
        vec3 lightDir      = light_directions[i];
        float attConst     = light_attenuation_consts[i];
        float attLinear    = light_attenuation_linears[i];
        float attQuadratic = light_attenuation_quadratics[i];
        float spotAngle    = light_spotlight_cutoff_angles[i];
        float spotExponent = light_spotlight_exponents[i];
        vec3 toLight;
        if (lightPos.w == 0.0)
        {
            toLight = lightPos.xyz;
        }
        else
        {
            toLight = (lightPos.xyz / lightPos.w - pos_varying.xyz);
        }
        float lightDist = length(toLight);
        vec3 toLightNormalized = normalize(toLight);
        vec3 h                 = toLightNormalized + vec3(0.0, 0.0, 1.0);
        float ndotL            = posDot(normal_varying, toLightNormalized);
        float ndoth            = posDot(normal_varying, normalize(h));
        float specAtt;
        if (ndotL != 0.0)
        {
            specAtt = 1.0;
        }
        else
        {
            specAtt = 0.0;
        }
        float att;
        if (lightPos.w != 0.0)
        {
            float attDenom =
                (attConst + attLinear * lightDist + attQuadratic * lightDist * lightDist);
            att = 1.0 / attDenom;
        }
        else
        {
            att = 1.0;
        }
        mediump float spot;
        mediump float spotAngleCos = cos(radians(spotAngle));
        vec3 toSurfaceDir  = -toLightNormalized;
        mediump float spotDot      = posDot(toSurfaceDir, normalize(lightDir));
        if (spotAngle == 180.0 || lightPos.w == 0.0)
        {
            spot = 1.0;
        }
        else
        {
            if (spotDot < spotAngleCos)
            {
                spot = 0.0;
            }
            else
            {
                spot = pow(spotDot, spotExponent);
            }
        }
        mediump vec4 contrib = materialAmbientActual * lightAmbient;
        contrib += ndotL * materialDiffuseActual * lightDiffuse;
        if (ndoth > 0.0 && material_specular_exponent > 0.0)
        {
            contrib += specAtt * pow(ndoth, material_specular_exponent) * material_specular *
                       lightSpecular;
        }
        else
        {
            if (ndoth > 0.0)
            {
                contrib += specAtt * material_specular * lightSpecular;
            }
        }
        contrib *= att * spot;
        lightingResult += contrib;
    }
    return lightingResult;
}
const mediump vec4 drawTextureVertices[6] = vec4[](
    vec4(0.0, 0.0, 0.0, 1.0),
    vec4(1.0, 0.0, 0.0, 1.0),
    vec4(1.0, 1.0, 0.0, 1.0),
    vec4(0.0, 0.0, 0.0, 1.0),
    vec4(1.0, 1.0, 0.0, 1.0),
    vec4(0.0, 1.0, 0.0, 1.0));
mediump vec4 drawTexturePosition(int vertexId)
{
    // The texture is drawn in the XY plane, so Z is constant.
    vec2 positionXY = draw_texture_coords.xy + drawTextureVertices[vertexId].xy * draw_texture_dims;
    return vec4(positionXY, draw_texture_coords.z, 1.0);
}
mediump vec3 drawTextureTexCoord(int vertexId, uint textureUnit)
{
    // The texture is drawn in the XY plane, so Z is 0.
    mediump vec2 texCropPos = draw_texture_normalized_crop_rect[textureUnit].xy;
    mediump vec2 texCropDim = draw_texture_normalized_crop_rect[textureUnit].zw;
    mediump vec2 texCoords  = texCropPos + drawTextureVertices[vertexId].xy * texCropDim;
    return vec3(texCoords, 0.0);
}
vec4 calcWorldPosition(vec4 posInput)
{
    return modelview * posInput;
}
vec4 calcNdcFromWorldPosition(vec4 worldPos)
{
    return projection * worldPos;
}
float calcPointSize(vec4 ndcPos)
{
    float dist         = length(ndcPos.z);
    float attConst     = point_distance_attenuation[0];
    float attLinear    = point_distance_attenuation[1];
    float attQuad      = point_distance_attenuation[2];
    float attPart      = attConst + attLinear * dist + attQuad * dist * dist;
    float attPointSize = pointsize / pow(attPart, 0.5);
    return clamp(attPointSize, point_size_min, point_size_max);
}
vec3 calcNormal(vec3 normalInput)
{
    mat3 mvInvTr3 = mat3(modelview_invtr);
    vec3 result   = mvInvTr3 * normalInput;
    if (enable_rescale_normal)
    {
        float rescale   = 1.0;
        vec3 rescaleVec = vec3(mvInvTr3[2]);
        float len       = length(rescaleVec);
        if (len > 0.0)
        {
            rescale = 1.0 / len;
        }
        result *= rescale;
    }
    if (enable_normalize)
    {
        result = normalize(result);
    }
    return result;
}
void main()
{
    if (enable_draw_texture)
    {
        int vertexId        = gl_VertexID;
        mediump vec4 posDrawTexture = drawTexturePosition(vertexId);
        gl_Position = posDrawTexture;
        pos_varying = posDrawTexture;
        normal_varying = normal;
        gl_PointSize = pointsize;
#if kTexUnits >= 1u
        texcoord0_varying = drawTextureTexCoord(vertexId, 0u);
#endif
#if kTexUnits >= 2u
        texcoord1_varying = drawTextureTexCoord(vertexId, 1u);
#endif
#if kTexUnits >= 3u
        texcoord2_varying = drawTextureTexCoord(vertexId, 2u);
#endif
#if kTexUnits >= 4u
        texcoord3_varying = drawTextureTexCoord(vertexId, 3u);
#endif
    }
    else
    {
        vec4 worldPos = calcWorldPosition(pos);
        vec4 ndcPos   = calcNdcFromWorldPosition(worldPos);
        gl_Position = ndcPos;
        pos_varying = worldPos;
        normal_varying = calcNormal(normal);
        // Avoid calculating point size stuff
        // if we are not rendering points.
        if (point_rasterization)
        {
            gl_PointSize = calcPointSize(ndcPos);
        }
        else
        {
            gl_PointSize = pointsize;
        }
#if kTexUnits >= 1u
        texcoord0_varying = (texture_matrix[0] * texcoord0).xyz;
#endif
#if kTexUnits >= 2u
        texcoord1_varying = (texture_matrix[1] * texcoord1).xyz;
#endif
#if kTexUnits >= 3u
        texcoord2_varying = (texture_matrix[2] * texcoord2).xyz;
#endif
#if kTexUnits >= 4u
        texcoord3_varying = (texture_matrix[3] * texcoord3).xyz;
#endif
    }
    mediump vec4 vertex_color = color;
    if (enable_lighting)
    {
        vertex_color = doLighting(color);
    }
    vertex_color = clamp(vertex_color, vec4(0), vec4(1));
    color_varying      = vertex_color;
    color_varying_flat = vertex_color;
}
)";
constexpr char kGLES1DrawFShaderVersion[] = R"(#version 300 es
)";
constexpr char kGLES1DrawFShaderHeader[] = R"(precision highp float;
// Defines for GL constants
#define kMaxTexUnits                         4u
#define kMaxClipPlanes                       6u
#define kModulate                       0x2100u
#define kDecal                          0x2101u
#define kCombine                        0x8570u
#define kReplace                        0x1E01u
#define kBlend                          0x0BE2u
#define kAdd                            0x0104u
#define kAddSigned                      0x8574u
#define kInterpolate                    0x8575u
#define kSubtract                       0x84E7u
#define kDot3Rgb                        0x86AEu
#define kDot3Rgba                       0x86AFu
#define kAlpha                          0x1906u
#define kRGB                            0x1907u
#define kRGBA                           0x1908u
#define kLuminance                      0x1909u
#define kLuminanceAlpha                 0x190Au
#define kTexture                        0x1702u
#define kConstant                       0x8576u
#define kPrimaryColor                   0x8577u
#define kPrevious                       0x8578u
#define kSrcColor                       0x0300u
#define kOneMinusSrcColor               0x0301u
#define kSrcAlpha                       0x0302u
#define kOneMinusSrcAlpha               0x0303u
#define kLinear                         0x2601u
#define kExp                            0x0800u
#define kExp2                           0x0801u
#define kNever                          0x0200u
#define kLess                           0x0201u
#define kEqual                          0x0202u
#define kLequal                         0x0203u
#define kGreater                        0x0204u
#define kNotequal                       0x0205u
#define kGequal                         0x0206u
#define kAlways                         0x0207u
#define kZero                              0x0u
#define kOne                               0x1u
#define kAnd                            0u
#define kAndInverted                    1u
#define kAndReverse                     2u
#define kClear                          3u
#define kCopy                           4u
#define kCopyInverted                   5u
#define kEquiv                          6u
#define kInvert                         7u
#define kNand                           8u
#define kNoop                           9u
#define kNor                            10u
#define kOr                             11u
#define kOrInverted                     12u
#define kOrReverse                      13u
#define kSet                            14u
#define kXor                            15u
)";
constexpr char kGLES1DrawFShaderUniformDefs[] = R"(
// Texture units ///////////////////////////////////////////////////////////////
// These are not arrays because hw support for arrays
// of samplers is rather lacking.
uniform mediump sampler2D tex_sampler0;
uniform mediump samplerCube tex_cube_sampler0;
uniform mediump sampler2D tex_sampler1;
uniform mediump samplerCube tex_cube_sampler1;
uniform mediump sampler2D tex_sampler2;
uniform mediump samplerCube tex_cube_sampler2;
uniform mediump sampler2D tex_sampler3;
uniform mediump samplerCube tex_cube_sampler3;
uniform mediump vec4 texture_env_color[kMaxTexUnits];
uniform mediump float texture_env_rgb_scale[kMaxTexUnits];
uniform mediump float texture_env_alpha_scale[kMaxTexUnits];
// Vertex attributes////////////////////////////////////////////////////////////
in vec4 pos_varying;
in vec3 normal_varying;
in mediump vec4 color_varying;
flat in mediump vec4 color_varying_flat;
#if kTexUnits >= 1u
in mediump vec3 texcoord0_varying;
#endif
#if kTexUnits >= 2u
in mediump vec3 texcoord1_varying;
#endif
#if kTexUnits >= 3u
in mediump vec3 texcoord2_varying;
#endif
#if kTexUnits >= 4u
in mediump vec3 texcoord3_varying;
#endif
// Alpha test///////////////////////////////////////////////////////////////////
uniform mediump float alpha_test_ref;
// Fog /////////////////////////////////////////////////////////////////////////
uniform float fog_density;
uniform float fog_start;
uniform float fog_end;
uniform mediump vec4 fog_color;
// User clip plane /////////////////////////////////////////////////////////////
uniform vec4 clip_planes[kMaxClipPlanes];
// Logic Op ////////////////////////////////////////////////////////////////////
// Format is:
// - 4x4 bits depicting the bit width of each channel of color output
// - 4 bits for the op based on LogicalOperation's packing
uniform highp uint logic_op;
// Point rasterization//////////////////////////////////////////////////////////
// GL_OES_draw_texture//////////////////////////////////////////////////////////
)";
constexpr char kGLES1DrawFShaderOutputDef[] = R"(
out mediump vec4 frag_color;
)";
constexpr char kGLES1DrawFShaderFramebufferFetchOutputDef[] = R"(
inout mediump vec4 frag_color;
)";
constexpr char kGLES1DrawFShaderFramebufferFetchNonCoherentOutputDef[] = R"(
layout(noncoherent) inout mediump vec4 frag_color;
)";
constexpr char kGLES1DrawFShaderFunctions[] = R"(
bool doAlphaTest(mediump vec4 currentFragment)
{
    bool shouldPassAlpha   = false;
    mediump float incAlpha = currentFragment.a;
    switch (alpha_func)
    {
        case kNever:
            shouldPassAlpha = false;
            break;
        case kLess:
            shouldPassAlpha = incAlpha < alpha_test_ref;
            break;
        case kLequal:
            shouldPassAlpha = incAlpha <= alpha_test_ref;
            break;
        case kEqual:
            shouldPassAlpha = incAlpha == alpha_test_ref;
            break;
        case kGequal:
            shouldPassAlpha = incAlpha >= alpha_test_ref;
            break;
        case kGreater:
            shouldPassAlpha = incAlpha > alpha_test_ref;
            break;
        case kNotequal:
            shouldPassAlpha = incAlpha != alpha_test_ref;
            break;
        case kAlways:
        default:
            shouldPassAlpha = true;
            break;
    }
    return shouldPassAlpha;
}
bool doClipPlaneTest()
{
    bool res = true;
    for (uint i = 0u; i < kMaxClipPlanes; i++)
    {
        if (clip_plane_enables[i])
        {
            float dist = dot(clip_planes[i].xyz, pos_varying.xyz) + clip_planes[i].w * pos_varying.w;
            res        = res && (dist >= 0.0);
        }
    }
    return res;
}
mediump vec4 doFog(mediump vec4 currentFragment)
{
    float eyeDist = abs(pos_varying.z / pos_varying.w);
    float f       = 1.0;
    switch (fog_mode)
    {
        case kExp:
            f = exp(-fog_density * eyeDist);
            break;
        case kExp2:
            f = exp(-(pow(fog_density * eyeDist, 2.0)));
            break;
        case kLinear:
            f = (fog_end - eyeDist) / (fog_end - fog_start);
            break;
        default:
            break;
    }
    f = clamp(f, 0.0, 1.0);
    mediump vec4 result = vec4(f * currentFragment.rgb + (1.0 - f) * fog_color.rgb, currentFragment.a);
    return result;
}
)";
constexpr char kGLES1DrawFShaderLogicOpFramebufferFetchDisabled[] = R"(
mediump vec4 applyLogicOp(mediump vec4 currentFragment)
{
    return currentFragment;
}
)";
// applyLogicOp takes logic-op information from a packed uniform and applies it to the color
// attachment using framebuffer fetch.  See the description of logic_op above for the format of the
// uniform.
//
// In particular, 4 bits in logic_op (at offset 16) contain the packed logical operation (of
// LogicalOperation type).  Based on the selected operation, the formula specified in the spec is
// applied (applied as bitwise operations on unorm values).
constexpr char kGLES1DrawFShaderLogicOpFramebufferFetchEnabled[] = R"(
mediump vec4 applyLogicOp(mediump vec4 currentFragment)
{
    mediump vec4 previousFragment = frag_color;
    mediump uvec4 channelWidths = uvec4(logic_op & 0xFu,
                                        logic_op >> 4u & 0xFu,
                                        logic_op >> 8u & 0xFu,
                                        logic_op >> 12u & 0xFu);
    mediump uvec4 channelMasks = (uvec4(1) << channelWidths) - 1u;
    mediump uvec4 src = uvec4(round(currentFragment * vec4(channelMasks)));
    mediump uvec4 dst = uvec4(round(previousFragment * vec4(channelMasks)));
    mediump uvec4 result;
    switch (logic_op >> 16u & 0xFu)
    {
        case kAnd:
            result = src & dst;
            break;
        case kAndInverted:
            result = ~src & dst;
            break;
        case kAndReverse:
            result = src & ~dst;
            break;
        case kClear:
            result = uvec4(0);
            break;
        case kCopy:
            result = src;
            break;
        case kCopyInverted:
            result = ~src;
            break;
        case kEquiv:
            result = ~(src ^ dst);
            break;
        case kInvert:
            result = ~dst;
            break;
        case kNand:
            result = ~(src & dst);
            break;
        case kNoop:
            result = dst;
            break;
        case kNor:
            result = ~(src | dst);
            break;
        case kOr:
            result = src | dst;
            break;
        case kOrInverted:
            result = ~src | dst;
            break;
        case kOrReverse:
            result = src | ~dst;
            break;
        case kSet:
            result = channelMasks;
            break;
        case kXor:
            result = src ^ dst;
            break;
    }
    result &= channelMasks;
    // Avoid division by zero for formats without alpha
    channelMasks.a = max(channelMasks.a, 1u);
    return vec4(result) / vec4(channelMasks);
}
)";
constexpr char kGLES1DrawFShaderMultitexturing[] = R"(
bool isTextureUnitEnabled(uint unit)
{
    return enable_texture_2d[unit] || enable_texture_cube_map[unit];
}
mediump vec4 getTextureColor(uint unit)
{
    mediump vec4 res;
    switch (unit)
    {
#if kTexUnits >= 1u
        case 0u:
            if (enable_texture_2d[0])
            {
                res = texture(tex_sampler0, texcoord0_varying.xy);
            }
            else if (enable_texture_cube_map[0])
            {
                res = texture(tex_cube_sampler0, texcoord0_varying);
            }
            break;
#endif
#if kTexUnits >= 2u
        case 1u:
            if (enable_texture_2d[1])
            {
                res = texture(tex_sampler1, texcoord1_varying.xy);
            }
            else if (enable_texture_cube_map[1])
            {
                res = texture(tex_cube_sampler1, texcoord1_varying);
            }
            break;
#endif
#if kTexUnits >= 3u
        case 2u:
            if (enable_texture_2d[2])
            {
                res = texture(tex_sampler2, texcoord2_varying.xy);
            }
            else if (enable_texture_cube_map[2])
            {
                res = texture(tex_cube_sampler2, texcoord2_varying);
            }
            break;
#endif
#if kTexUnits >= 4u
        case 3u:
            if (enable_texture_2d[3])
            {
                res = texture(tex_sampler3, texcoord3_varying.xy);
            }
            else if (enable_texture_cube_map[3])
            {
                // TODO: Weird stuff happens
                // res = texture(tex_cube_sampler3, texcoord3_varying);
            }
            break;
#endif
        default:
            break;
    }
    return res;
}
mediump vec4 getPointSpriteTextureColor(uint unit)
{
    mediump vec4 res;
    switch (unit)
    {
        case 0u:
            if (enable_texture_2d[0])
            {
                res = texture(tex_sampler0, gl_PointCoord.xy);
            }
            break;
        case 1u:
            if (enable_texture_2d[1])
            {
                res = texture(tex_sampler1, gl_PointCoord.xy);
            }
            break;
        case 2u:
            if (enable_texture_2d[2])
            {
                res = texture(tex_sampler2, gl_PointCoord.xy);
            }
            break;
        case 3u:
            if (enable_texture_2d[3])
            {
                res = texture(tex_sampler3, gl_PointCoord.xy);
            }
            break;
        default:
            break;
    }
    return res;
}
mediump vec3 textureCombineSrcnOpnRgb(uint srcnRgb,
                                      uint opnRgb,
                                      mediump vec4 textureEnvColor,
                                      mediump vec4 vertexColor,
                                      mediump vec4 texturePrevColor,
                                      mediump vec4 textureColor)
{
    mediump vec3 res;
    mediump vec4 op;
    switch (srcnRgb)
    {
        case kTexture:
            op = textureColor;
            break;
        case kConstant:
            op = textureEnvColor;
            break;
        case kPrimaryColor:
            op = vertexColor;
            break;
        case kPrevious:
            op = texturePrevColor;
            break;
        default:
            op = texturePrevColor;
            break;
    }
    switch (opnRgb)
    {
        case kSrcColor:
            res = op.rgb;
            break;
        case kOneMinusSrcColor:
            res = 1.0 - op.rgb;
            break;
        case kSrcAlpha:
            res = vec3(op.a, op.a, op.a);
            break;
        case kOneMinusSrcAlpha:
            res = vec3(1.0 - op.a, 1.0 - op.a, 1.0 - op.a);
            break;
        default:
            break;
    }
    return res;
}
mediump float textureCombineSrcnOpnAlpha(uint srcn,
                                         uint opn,
                                         mediump vec4 textureEnvColor,
                                         mediump vec4 vertexColor,
                                         mediump vec4 texturePrevColor,
                                         mediump vec4 textureColor)
{
    mediump float res;
    mediump vec4 op;
    switch (srcn)
    {
        case kTexture:
            op = textureColor;
            break;
        case kConstant:
            op = textureEnvColor;
            break;
        case kPrimaryColor:
            op = vertexColor;
            break;
        case kPrevious:
            op = texturePrevColor;
            break;
        default:
            op = texturePrevColor;
            break;
    }
    switch (opn)
    {
        case kSrcAlpha:
            res = op.a;
            break;
        case kOneMinusSrcAlpha:
            res = 1.0 - op.a;
            break;
        default:
            break;
    }
    return res;
}
mediump vec4 textureCombine(uint combineRgb,
                            uint combineAlpha,
                            uint src0Rgb,
                            uint src0Alpha,
                            uint src1Rgb,
                            uint src1Alpha,
                            uint src2Rgb,
                            uint src2Alpha,
                            uint op0Rgb,
                            uint op0Alpha,
                            uint op1Rgb,
                            uint op1Alpha,
                            uint op2Rgb,
                            uint op2Alpha,
                            mediump vec4 textureEnvColor,
                            mediump float rgbScale,
                            mediump float alphaScale,
                            mediump vec4 vertexColor,
                            mediump vec4 texturePrevColor,
                            mediump vec4 textureColor)
{
    mediump vec3 resRgb;
    mediump float resAlpha;
    mediump vec3 arg0Rgb;
    mediump float arg0Alpha;
    mediump vec3 arg1Rgb;
    mediump float arg1Alpha;
    mediump vec3 arg2Rgb;
    mediump float arg2Alpha;
    mediump float dotVal;
    arg0Rgb   = textureCombineSrcnOpnRgb(src0Rgb, op0Rgb, textureEnvColor, vertexColor,
                                       texturePrevColor, textureColor);
    arg0Alpha = textureCombineSrcnOpnAlpha(src0Alpha, op0Alpha, textureEnvColor, vertexColor,
                                           texturePrevColor, textureColor);
    if (combineRgb != kReplace)
    {
        arg1Rgb = textureCombineSrcnOpnRgb(src1Rgb, op1Rgb, textureEnvColor, vertexColor,
                                           texturePrevColor, textureColor);
    }
    if (combineAlpha != kReplace)
    {
        arg1Alpha = textureCombineSrcnOpnAlpha(src1Alpha, op1Alpha, textureEnvColor, vertexColor,
                                               texturePrevColor, textureColor);
    }
    if (combineRgb == kInterpolate)
    {
        arg2Rgb = textureCombineSrcnOpnRgb(src2Rgb, op2Rgb, textureEnvColor, vertexColor,
                                           texturePrevColor, textureColor);
    }
    if (combineAlpha == kInterpolate)
    {
        arg2Alpha = textureCombineSrcnOpnAlpha(src2Alpha, op2Alpha, textureEnvColor, vertexColor,
                                               texturePrevColor, textureColor);
    }
    switch (combineRgb)
    {
        case kReplace:
            resRgb = arg0Rgb;
            break;
        case kModulate:
            resRgb = arg0Rgb * arg1Rgb;
            break;
        case kAdd:
            resRgb = arg0Rgb + arg1Rgb;
            break;
        case kAddSigned:
            resRgb = arg0Rgb + arg1Rgb - 0.5;
            break;
        case kInterpolate:
            resRgb = arg0Rgb * arg2Rgb + arg1Rgb * (1.0 - arg2Rgb);
            break;
        case kSubtract:
            resRgb = arg0Rgb - arg1Rgb;
            break;
        default:
            break;
    }
    switch (combineAlpha)
    {
        case kReplace:
            resAlpha = arg0Alpha;
            break;
        case kModulate:
            resAlpha = arg0Alpha * arg1Alpha;
            break;
        case kAdd:
            resAlpha = arg0Alpha + arg1Alpha;
            break;
        case kAddSigned:
            resAlpha = arg0Alpha + arg1Alpha - 0.5;
            break;
        case kInterpolate:
            resAlpha = arg0Alpha * arg2Alpha + arg1Alpha * (1.0 - arg2Alpha);
            break;
        case kSubtract:
            resAlpha = arg0Alpha - arg1Alpha;
            break;
        default:
            break;
    }
    if (combineRgb == kDot3Rgb || combineRgb == kDot3Rgba)
    {
        dotVal = 4.0 * dot(arg0Rgb - 0.5, arg1Rgb - 0.5);
        if (combineRgb == kDot3Rgb)
        {
            return vec4(dotVal, dotVal, dotVal, resAlpha);
        }
        else
        {
            return vec4(dotVal, dotVal, dotVal, dotVal);
        }
    }
    else
    {
        return vec4(resRgb, resAlpha);
    }
}
mediump vec4 textureFunction(uint unit,
                             uint texFormat,
                             uint envMode,
                             uint combineRgb,
                             uint combineAlpha,
                             uint src0Rgb,
                             uint src0Alpha,
                             uint src1Rgb,
                             uint src1Alpha,
                             uint src2Rgb,
                             uint src2Alpha,
                             uint op0Rgb,
                             uint op0Alpha,
                             uint op1Rgb,
                             uint op1Alpha,
                             uint op2Rgb,
                             uint op2Alpha,
                             mediump vec4 textureEnvColor,
                             mediump float rgbScale,
                             mediump float alphaScale,
                             mediump vec4 vertexColor,
                             mediump vec4 texturePrevColor,
                             mediump vec4 textureColor)
{
    if (!isTextureUnitEnabled(unit))
    {
        return texturePrevColor;
    }
    mediump vec4 res;
    switch (envMode)
    {
        case kReplace:
            switch (texFormat)
            {
                case kAlpha:
                    res.rgb = texturePrevColor.rgb;
                    res.a   = textureColor.a;
                    break;
                case kRGBA:
                case kLuminanceAlpha:
                    res.rgba = textureColor.rgba;
                    break;
                case kRGB:
                case kLuminance:
                default:
                    res.rgb = textureColor.rgb;
                    res.a   = texturePrevColor.a;
                    break;
            }
            break;
        case kModulate:
            switch (texFormat)
            {
                case kAlpha:
                    res.rgb = texturePrevColor.rgb;
                    res.a   = texturePrevColor.a * textureColor.a;
                    break;
                case kRGBA:
                case kLuminanceAlpha:
                    res.rgba = texturePrevColor.rgba * textureColor.rgba;
                    break;
                case kRGB:
                case kLuminance:
                default:
                    res.rgb = texturePrevColor.rgb * textureColor.rgb;
                    res.a   = texturePrevColor.a;
                    break;
            }
            break;
        case kDecal:
            switch (texFormat)
            {
                case kRGB:
                    res.rgb = textureColor.rgb;
                    res.a   = texturePrevColor.a;
                    break;
                case kRGBA:
                    res.rgb = texturePrevColor.rgb * (1.0 - textureColor.a) +
                              textureColor.rgb * textureColor.a;
                    res.a = texturePrevColor.a;
                    break;
                case kAlpha:
                case kLuminance:
                case kLuminanceAlpha:
                default:
                    res.rgb = texturePrevColor.rgb * textureColor.rgb;
                    res.a   = texturePrevColor.a;
                    break;
            }
            break;
        case kBlend:
            switch (texFormat)
            {
                case kAlpha:
                    res.rgb = texturePrevColor.rgb;
                    res.a   = textureColor.a * texturePrevColor.a;
                    break;
                case kLuminance:
                case kRGB:
                    res.rgb = texturePrevColor.rgb * (1.0 - textureColor.rgb) +
                              textureEnvColor.rgb * textureColor.rgb;
                    res.a = texturePrevColor.a;
                    break;
                case kLuminanceAlpha:
                case kRGBA:
                default:
                    res.rgb = texturePrevColor.rgb * (1.0 - textureColor.rgb) +
                              textureEnvColor.rgb * textureColor.rgb;
                    res.a = textureColor.a * texturePrevColor.a;
                    break;
            }
            break;
        case kAdd:
            switch (texFormat)
            {
                case kAlpha:
                    res.rgb = texturePrevColor.rgb;
                    res.a   = textureColor.a * texturePrevColor.a;
                    break;
                case kLuminance:
                case kRGB:
                    res.rgb = texturePrevColor.rgb + textureColor.rgb;
                    res.a   = texturePrevColor.a;
                    break;
                case kLuminanceAlpha:
                case kRGBA:
                default:
                    res.rgb = texturePrevColor.rgb + textureColor.rgb;
                    res.a   = textureColor.a * texturePrevColor.a;
                    break;
            }
            break;
        case kCombine:
            res = textureCombine(combineRgb, combineAlpha, src0Rgb, src0Alpha, src1Rgb, src1Alpha,
                                 src2Rgb, src2Alpha, op0Rgb, op0Alpha, op1Rgb, op1Alpha, op2Rgb,
                                 op2Alpha, textureEnvColor, rgbScale, alphaScale, vertexColor,
                                 texturePrevColor, textureColor);
            res.rgb *= rgbScale;
            res.a *= alphaScale;
            break;
        default:
            break;
    }
    return clamp(res, 0.0, 1.0);
}
)";
constexpr char kGLES1DrawFShaderMain[] = R"(
void main()
{
    if (enable_clip_planes && !enable_draw_texture)
    {
        if (!doClipPlaneTest())
        {
            discard;
        }
    }
    mediump vec4 vertex_color;
    if (shade_model_flat)
    {
        vertex_color = color_varying_flat;
    }
    else
    {
        vertex_color = color_varying;
    }
    mediump vec4 currentFragment = vertex_color;
    mediump vec4 texturePrevColor = currentFragment;
    for (uint i = 0u; i < kTexUnits; i++)
    {
        mediump vec4 textureColor;
        if (point_rasterization && point_sprite_enabled &&
            point_sprite_coord_replace[i]) {
            textureColor = getPointSpriteTextureColor(i);
        } else {
            textureColor = getTextureColor(i);
        }
        currentFragment = textureFunction(
            i, texture_format[i], texture_env_mode[i], combine_rgb[i], combine_alpha[i],
            src0_rgb[i], src0_alpha[i], src1_rgb[i], src1_alpha[i], src2_rgb[i], src2_alpha[i],
            op0_rgb[i], op0_alpha[i], op1_rgb[i], op1_alpha[i], op2_rgb[i], op2_alpha[i],
            texture_env_color[i], texture_env_rgb_scale[i], texture_env_alpha_scale[i],
            vertex_color, texturePrevColor, textureColor);
        texturePrevColor = currentFragment;
    }
    if (enable_fog)
    {
        currentFragment = doFog(currentFragment);
    }
    if (enable_alpha_test && !doAlphaTest(currentFragment))
    {
        discard;
    }
    frag_color = applyLogicOp(currentFragment);
}
)";