Hash :
1d8279a2
Author :
Date :
2025-08-13T13:35:02
Do not sort ANGLE internal uniforms or built-in uniforms On Mac, for webGL app on chrome, if the uniforms added by ANGLE are not placed at the top of the ANGLE_UserUniforms struct, the user defined uniforms in the same ANGLE_UserUniforms struct seem to be messed up and not read correctly by shader code. This change fixes it by moving the sortUniforms() function call to the place before we add ANGLE internal uniforms. That way ANGLE internal uniforms will be inserted later and stay on top of ANGLE_UserUniforms struct. Bug: chromium:437678149 Change-Id: I34a0d3dcb98c5fe30ad248381ecb5f7f78252275 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6847281 Auto-Submit: Yuxin Hu <yuxinhu@google.com> Commit-Queue: Yuxin Hu <yuxinhu@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Amirali Abdolrashidi <abdolrashidi@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
//
// Copyright 2024 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.
//
// SeparateDeclarations.cpp:
// Tests that compound declarations are rewritten to type declarations and variable declarations.
//
#include "GLSLANG/ShaderLang.h"
#include "angle_gl.h"
#include "gtest/gtest.h"
#include "tests/test_utils/compiler_test.h"
using namespace sh;
namespace
{
class SeparateDeclarations : public MatchOutputCodeTest
{
public:
SeparateDeclarations() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, SH_ESSL_OUTPUT)
{
ShCompileOptions defaultCompileOptions = {};
defaultCompileOptions.validateAST = true;
setDefaultCompileOptions(defaultCompileOptions);
}
};
class SeparateCompoundStructDeclarations : public MatchOutputCodeTest
{
public:
SeparateCompoundStructDeclarations() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, SH_ESSL_OUTPUT)
{
ShCompileOptions defaultCompileOptions = {};
defaultCompileOptions.validateAST = true;
defaultCompileOptions.separateCompoundStructDeclarations = true;
setDefaultCompileOptions(defaultCompileOptions);
}
};
class SeparateStructFunctionDeclarations : public MatchOutputCodeTest
{
public:
SeparateStructFunctionDeclarations() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, SH_ESSL_OUTPUT)
{
ShCompileOptions defaultCompileOptions = {};
defaultCompileOptions.validateAST = true;
setDefaultCompileOptions(defaultCompileOptions);
}
};
TEST_F(SeparateDeclarations, Arrays)
{
const char kShader[] = R"(#version 300 es
precision highp float;
int a[1] = int[1](1), b[1] = int[1](2);
out vec4 o;
void main() {
if (a[0] == b[0])
o = vec4(1);
})";
const char kExpected[] = R"(#version 300 es
const mediump int sbbd = 1;
mediump int _ua[1] = int[1](sbbd);
const mediump int sbbe = 2;
mediump int _ub[1] = int[1](sbbe);
out highp vec4 _uo;
void main(){
if ((_ua[0] == _ub[0]))
{
(_uo = vec4(1.0, 1.0, 1.0, 1.0));
}
}
)";
compile(kShader);
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
TEST_F(SeparateDeclarations, StructNoChange)
{
const char kShader[] = R"(#version 300 es
precision highp float;
struct S { vec3 d; } a;
out vec4 o;
void main() {
if (a.d == vec3(2))
o = vec4(1);
})";
const char kExpected[] = R"(#version 300 es
struct _uS {
highp vec3 _ud;
} _ua;
out highp vec4 _uo;
void main(){
if ((_ua._ud == vec3(2.0, 2.0, 2.0)))
{
(_uo = vec4(1.0, 1.0, 1.0, 1.0));
}
}
)";
compile(kShader);
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
TEST_F(SeparateDeclarations, Structs)
{
const char kShader[] = R"(#version 300 es
precision highp float;
struct S { vec3 d; } a, b;
out vec4 o;
void main() {
if (a.d == b.d)
o = vec4(1);
})";
const char kExpected[] = R"(#version 300 es
struct _uS {
highp vec3 _ud;
} _ua;
_uS _ub;
out highp vec4 _uo;
void main(){
if ((_ua._ud == _ub._ud))
{
(_uo = vec4(1.0, 1.0, 1.0, 1.0));
}
}
)";
compile(kShader);
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
TEST_F(SeparateDeclarations, AnonymousStructNoChange)
{
const char kShader[] = R"(#version 300 es
precision highp float;
struct { vec3 d; } a;
out vec4 o;
void main() {
if (any(lessThan(a.d, vec3(2))))
o = vec4(1);
})";
const char kExpected[] = R"(#version 300 es
struct {
highp vec3 _ud;
} _ua;
out highp vec4 _uo;
void main(){
if (any(lessThan(_ua._ud, vec3(2.0, 2.0, 2.0))))
{
(_uo = vec4(1.0, 1.0, 1.0, 1.0));
}
}
)";
compile(kShader);
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
TEST_F(SeparateDeclarations, AnonymousStructs)
{
const char kShader[] = R"(#version 300 es
precision highp float;
struct { vec3 d; } a, b;
out vec4 o;
void main() {
if (any(lessThan(a.d, b.d)))
o = vec4(1);
})";
const char kExpected[] = R"(#version 300 es
struct sbbe {
highp vec3 _ud;
} _ua;
sbbe _ub;
out highp vec4 _uo;
void main(){
if (any(lessThan(_ua._ud, _ub._ud)))
{
(_uo = vec4(1.0, 1.0, 1.0, 1.0));
}
}
)";
compile(kShader);
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
TEST_F(SeparateCompoundStructDeclarations, AnonymousStruct)
{
const char kShader[] = R"(#version 300 es
precision highp float;
struct { vec3 d; } a;
out vec4 o;
void main() {
if (any(lessThan(a.d, vec3(2))))
o = vec4(1);
})";
const char kExpected[] = R"(#version 300 es
struct sbbd {
highp vec3 _ud;
};
sbbd _ua;
out highp vec4 _uo;
void main(){
if (any(lessThan(_ua._ud, vec3(2.0, 2.0, 2.0))))
{
(_uo = vec4(1.0, 1.0, 1.0, 1.0));
}
}
)";
compile(kShader);
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
TEST_F(SeparateCompoundStructDeclarations, AnonymousStructs)
{
const char kShader[] = R"(#version 300 es
precision highp float;
struct { vec3 d; } a, b;
out vec4 o;
void main() {
if (any(lessThan(a.d, b.d)))
o = vec4(1);
})";
const char kExpected[] = R"(#version 300 es
struct sbbe {
highp vec3 _ud;
};
sbbe _ua;
sbbe _ub;
out highp vec4 _uo;
void main(){
if (any(lessThan(_ua._ud, _ub._ud)))
{
(_uo = vec4(1.0, 1.0, 1.0, 1.0));
}
}
)";
compile(kShader);
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
TEST_F(SeparateCompoundStructDeclarations, Struct)
{
const char kShader[] = R"(#version 300 es
precision highp float;
struct S { vec3 d; } a, b;
out vec4 o;
void main() {
if (a.d == b.d)
o = vec4(1);
})";
const char kExpected[] = R"(#version 300 es
struct _uS {
highp vec3 _ud;
};
_uS _ua;
_uS _ub;
out highp vec4 _uo;
void main(){
if ((_ua._ud == _ub._ud))
{
(_uo = vec4(1.0, 1.0, 1.0, 1.0));
}
}
)";
compile(kShader);
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
TEST_F(SeparateCompoundStructDeclarations, ConstStruct)
{
const char kShader[] = R"(#version 300 es
precision highp float;
out vec4 o;
void main()
{
const struct s2 {
int i;
} s22 = s2(8);
const struct s1 {
s2 ss;
mat4 m;
} s11 = s1(s22, mat4(5));
if (s11.ss.i > int(o.x))
o = vec4(1);
})";
const char kExpected[] = R"(#version 300 es
out highp vec4 _uo;
void main(){
const mediump int sbc3 = 8;
if ((sbc3 > int(_uo.x)))
{
(_uo = vec4(1.0, 1.0, 1.0, 1.0));
}
}
)";
compile(kShader);
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
// Example of what the const struct does before evaluating constants.
TEST_F(SeparateCompoundStructDeclarations, ConstStructsCrossRef)
{
const char kShader[] = R"(#version 300 es
precision highp float;
out vec4 o;
void main()
{
struct s2 {
int i;
} s22 = s2(8);
struct s1 {
s2 ss;
mat4 m;
} s11 = s1(s22, mat4(5));
if (s11.ss.i > int(o.x))
o = vec4(1);
})";
const char kExpected[] = R"(#version 300 es
out highp vec4 _uo;
void main(){
struct _us2 {
mediump int _ui;
};
_us2 _us22 = _us2(8);
struct _us1 {
_us2 _uss;
highp mat4 _um;
};
_us1 _us11 = _us1(_us22, mat4(5.0, 0.0, 0.0, 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 5.0));
if ((_us11._uss._ui > int(_uo.x)))
{
(_uo = vec4(1.0, 1.0, 1.0, 1.0));
}
}
)";
compile(kShader);
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
// Test that struct name validation takes into account that intenal symbol namespace
// is different to user namespace. The test should be kept in sync so that struct sbbf is the same
// textual symbol as what the anonymous struct gets.
TEST_F(SeparateCompoundStructDeclarations, InternalSymbolNoCrash)
{
const char kShader[] = R"(
precision highp float;
struct { vec4 e; } g;
struct sbbf { vec4 f; };
void main(){
sbbf s;
gl_FragColor = g.e + s.f;
})";
compile(kShader);
const char kExpected[] = R"(struct sbbf {
highp vec4 _ue;
};
sbbf _ug;
struct _usbbf {
highp vec4 _uf;
};
void main(){
_usbbf _us;
(gl_FragColor = (_ug._ue + _us._uf));
}
)";
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
TEST_F(SeparateStructFunctionDeclarations, StructInStruct)
{
const char kShader[] = R"(#version 300 es
struct S {
int f;
};
struct S2 { S h; } o()
{
return S2(S(1));
}
void main() {
S2 s2 = o();
})";
const char kExpected[] = R"(#version 300 es
struct _uS {
mediump int _uf;
};
struct _uS2 {
_uS _uh;
};
_uS2 _uo(){
return _uS2(_uS(1));
}
void main(){
_uS2 _us2 = _uo();
}
)";
compile(kShader);
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
TEST_F(SeparateStructFunctionDeclarations, StructInAnonymousStruct)
{
const char kShader[] = R"(#version 300 es
struct S {
int f;
};
struct { S h; } o();
void main() {
})";
const char kExpected[] = R"(#version 300 es
void main(){
}
)";
compile(kShader);
EXPECT_EQ(kExpected, outputCode(SH_ESSL_OUTPUT));
}
} // namespace