Hash :
b1a0d60f
Author :
Date :
2025-01-08T15:10:41
WGSL: Fix accidentally overloaded functions Small-stride arrays in uniforms with the same element type, but different array sizes, would cause the WGSL generator to produce conversion functions with the same name but different array sizes. This CL puts the array size in the name of the function to avoid overloading, which is unsupported in WGSL. Bug: angleproject:376553328 Change-Id: I446e91ccb9da2872c88f1a4e05283aacc9d6f8b1 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6160334 Commit-Queue: Matt Denton <mpdenton@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Auto-Submit: Matthew Denton <mpdenton@chromium.org> Reviewed-by: Matt Denton <mpdenton@google.com> Reviewed-by: Liza Burakova <liza@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 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
//
// 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.
//
// WGSLOutput_test.cpp:
// Tests for corect WGSL translations.
//
#include <regex>
#include "GLSLANG/ShaderLang.h"
#include "angle_gl.h"
#include "gtest/gtest.h"
#include "tests/test_utils/compiler_test.h"
using namespace sh;
class WGSLVertexOutputTest : public MatchOutputCodeTest
{
public:
WGSLVertexOutputTest() : MatchOutputCodeTest(GL_VERTEX_SHADER, SH_WGSL_OUTPUT)
{
ShCompileOptions defaultCompileOptions = {};
defaultCompileOptions.validateAST = true;
setDefaultCompileOptions(defaultCompileOptions);
}
};
class WGSLOutputTest : public MatchOutputCodeTest
{
public:
WGSLOutputTest() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, SH_WGSL_OUTPUT)
{
ShCompileOptions defaultCompileOptions = {};
defaultCompileOptions.validateAST = true;
setDefaultCompileOptions(defaultCompileOptions);
}
};
TEST_F(WGSLOutputTest, BasicTranslation)
{
const std::string &shaderString =
R"(#version 310 es
precision highp float;
out vec4 outColor;
struct Foo {
float x;
float y;
vec3 multiArray[2][3];
mat3 aMatrix;
};
vec4 doFoo(Foo foo, float zw);
vec4 doFoo(Foo foo, float zw)
{
// foo.x = foo.y;
return vec4(foo.x, foo.y, zw, zw);
}
Foo returnFoo(Foo foo) {
return foo;
}
float returnFloat(float x) {
return x;
}
float takeArgs(vec2 x, float y) {
return y;
}
void main()
{
Foo foo;
// Struct field accesses.
foo.x = 2.0;
foo.y = 2.0;
// Complicated constUnion should be emitted correctly.
foo.multiArray = vec3[][](
vec3[](
vec3(1.0, 2.0, 3.0),
vec3(1.0, 2.0, 3.0),
vec3(1.0, 2.0, 3.0)),
vec3[](
vec3(4.0, 5.0, 6.0),
vec3(4.0, 5.0, 6.0),
vec3(4.0, 5.0, 6.0)
)
);
int arrIndex = 1;
// Access an array index with a constant index.
float f = foo.multiArray[0][1].x;
// Access an array index with a non-const index, should clamp by default.
float f2 = foo.multiArray[0][arrIndex].x;
gl_FragDepth = f + f2;
doFoo(returnFoo(foo), returnFloat(3.0));
takeArgs(vec2(1.0, 2.0), foo.x);
returnFloat(doFoo(foo, 7.0 + 9.0).x);
outColor = vec4(0.0, 0.0, 0.0, 0.0);
})";
const std::string &outputString =
R"(struct ANGLE_Output_Global {
outColor : vec4<f32>,
gl_FragDepth_ : f32,
};
var<private> ANGLE_output_global : ANGLE_Output_Global;
struct ANGLE_Output_Annotated {
@location(@@@@@@) outColor : vec4<f32>,
@builtin(frag_depth) gl_FragDepth_ : f32,
};
;
struct _uFoo
{
_ux : f32,
_uy : f32,
_umultiArray : array<array<vec3<f32>, 3>, 2>,
_uaMatrix : mat3x3<f32>,
};
fn _udoFoo(_ufoo : _uFoo, _uzw : f32) -> vec4<f32>;
fn _udoFoo(_ufoo : _uFoo, _uzw : f32) -> vec4<f32>
{
return vec4<f32>((_ufoo)._ux, (_ufoo)._uy, _uzw, _uzw);
}
fn _ureturnFoo(_ufoo : _uFoo) -> _uFoo
{
return _ufoo;
}
fn _ureturnFloat(_ux : f32) -> f32
{
return _ux;
}
fn _utakeArgs(_ux : vec2<f32>, _uy : f32) -> f32
{
return _uy;
}
fn _umain()
{
var _ufoo : _uFoo;
((_ufoo)._ux) = (2.0f);
((_ufoo)._uy) = (2.0f);
((_ufoo)._umultiArray) = (array<array<vec3<f32>, 3>, 2>(array<vec3<f32>, 3>(vec3<f32>(1.0f, 2.0f, 3.0f), vec3<f32>(1.0f, 2.0f, 3.0f), vec3<f32>(1.0f, 2.0f, 3.0f)), array<vec3<f32>, 3>(vec3<f32>(4.0f, 5.0f, 6.0f), vec3<f32>(4.0f, 5.0f, 6.0f), vec3<f32>(4.0f, 5.0f, 6.0f))));
var _uarrIndex : i32 = (1i);
var _uf : f32 = (((((_ufoo)._umultiArray)[0i])[1i]).x);
var _uf2 : f32 = (((((_ufoo)._umultiArray)[0i])[clamp((_uarrIndex), 0, 2)]).x);
(ANGLE_output_global.gl_FragDepth_) = ((_uf) + (_uf2));
_udoFoo(_ureturnFoo(_ufoo), _ureturnFloat(3.0f));
_utakeArgs(vec2<f32>(1.0f, 2.0f), (_ufoo)._ux);
_ureturnFloat((_udoFoo(_ufoo, 16.0f)).x);
(ANGLE_output_global.outColor) = (vec4<f32>(0.0f, 0.0f, 0.0f, 0.0f));
}
@fragment
fn wgslMain() -> ANGLE_Output_Annotated
{
_umain();
var ANGLE_output_annotated : ANGLE_Output_Annotated;
ANGLE_output_annotated.outColor = ANGLE_output_global.outColor;
ANGLE_output_annotated.gl_FragDepth_ = ANGLE_output_global.gl_FragDepth_;
return ANGLE_output_annotated;
}
)";
compile(shaderString);
EXPECT_TRUE(foundInCode(outputString.c_str()));
}
TEST_F(WGSLOutputTest, ControlFlow)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
int ifElseDemo() {
int x = 5;
if (x == 5) {
return 6;
} else if (x == 6) {
return 7;
} else {
return 8;
}
}
void switchDemo() {
int x = 5;
switch (x) {
case 5:
case 6:
discard;
case 7: {
return;
}
case 8:
case 9:
{
x = 7;
}
return;
default:
return;
}
}
void forLoopDemo() {
for (int i = 0; i < 5; i++) {
if (i == 4) {
break;
} else if (i == 5) {
continue;
}
}
}
void whileLoopDemo() {
int i = 0;
while (i < 5) {
i++;
}
do {
i++;
} while (i < 5);
}
void main()
{
ifElseDemo();
switchDemo();
forLoopDemo();
whileLoopDemo();
})";
const std::string &outputString =
R"(fn _uifElseDemo() -> i32
{
var _ux : i32 = (5i);
if ((_ux) == (5i))
{
return 6i;
}
else
{
if ((_ux) == (6i))
{
return 7i;
}
else
{
return 8i;
}
}
}
fn _uswitchDemo()
{
var _ux : i32 = (5i);
switch _ux
{
case 5i, 6i:
{
discard;
}
case 7i:
{
{
return;
}
}
case 8i, 9i:
{
{
(_ux) = (7i);
}
return;
}
case default:
{
return;
}
}
}
fn _uforLoopDemo()
{
for (var _ui : i32 = (0i); (_ui) < (5i); (_ui)++)
{
if ((_ui) == (4i))
{
break;
}
else
{
if ((_ui) == (5i))
{
continue;
}
}
}
}
fn _uwhileLoopDemo()
{
var _ui : i32 = (0i);
while ((_ui) < (5i))
{
(_ui)++;
}
loop {
{
(_ui)++;
}
if (!((_ui) < (5i)) { break; }
}
}
fn _umain()
{
_uifElseDemo();
_uswitchDemo();
_uforLoopDemo();
_uwhileLoopDemo();
}
@fragment
fn wgslMain()
{
_umain();
}
)";
compile(shaderString);
EXPECT_TRUE(foundInCode(outputString.c_str()));
}
TEST_F(WGSLOutputTest, GLFragColorWithUniform)
{
const std::string &shaderString =
R"(
uniform mediump vec4 u_color;
void main(void)
{
gl_FragColor = u_color;
})";
const std::string &outputString =
R"(struct ANGLE_Output_Global {
gl_FragColor_ : vec4<f32>,
};
var<private> ANGLE_output_global : ANGLE_Output_Global;
struct ANGLE_Output_Annotated {
@location(0) gl_FragColor_ : vec4<f32>,
};
struct ANGLE_DefaultUniformBlock {
u_color : vec4<f32>,
};
@group(0) @binding(1) var<uniform> ANGLE_defaultUniformBlock : ANGLE_DefaultUniformBlock;
;
fn _umain()
{
(ANGLE_output_global.gl_FragColor_) = (ANGLE_defaultUniformBlock.u_color);
}
@fragment
fn wgslMain() -> ANGLE_Output_Annotated
{
_umain();
var ANGLE_output_annotated : ANGLE_Output_Annotated;
ANGLE_output_annotated.gl_FragColor_ = ANGLE_output_global.gl_FragColor_;
return ANGLE_output_annotated;
}
)";
compile(shaderString);
EXPECT_TRUE(foundInCode(outputString.c_str()));
}
TEST_F(WGSLOutputTest, UniformsWithNestedStructs)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
struct NestedUniforms {
float x;
};
struct Uniforms {
NestedUniforms a;
float b;
float c;
float[5] d;
float e;
vec3 f[7];
float[5] g;
};
uniform Uniforms unis;
out vec4 fragColor;
void main() {
float[5] dCopy = unis.d;
fragColor = vec4(unis.a.x, unis.b, unis.c, dCopy[1]);
fragColor += vec4(unis.d[2], unis.e, unis.f[0][2], (unis.e > 0.5 ? unis.d : unis.g)[1]);
})";
const std::string &outputString =
R"(struct ANGLE_Output_Global {
fragColor : vec4<f32>,
};
var<private> ANGLE_output_global : ANGLE_Output_Global;
struct ANGLE_Output_Annotated {
@location(@@@@@@) fragColor : vec4<f32>,
};
struct ANGLE_DefaultUniformBlock {
unis : _uUniforms,
};
@group(0) @binding(1) var<uniform> ANGLE_defaultUniformBlock : ANGLE_DefaultUniformBlock;
struct ANGLE_wrapped_float
{
@align(16) elem : f32
};
fn ANGLE_Convert_Array5_ANGLE_wrapped_float_ElementsTo_float_Elements(wrappedArr : array<ANGLE_wrapped_float, 5>) -> array<f32, 5>
{
var retVal : array<f32, 5>;
for (var i : u32 = 0; i < 5; i++) {;
retVal[i] = wrappedArr[i].elem;
}
return retVal;
}
struct _uNestedUniforms
{
@align(16) _ux : f32,
};
struct _uUniforms
{
@align(16) _ua : _uNestedUniforms,
@align(16) _ub : f32,
_uc : f32,
@align(16) _ud : array<ANGLE_wrapped_float, 5>,
_ue : f32,
@align(16) _uf : array<vec3<f32>, 7>,
@align(16) _ug : array<ANGLE_wrapped_float, 5>,
};
;
;
fn _umain()
{
var _udCopy : array<f32, 5> = (ANGLE_Convert_Array5_ANGLE_wrapped_float_ElementsTo_float_Elements((ANGLE_defaultUniformBlock.unis)._ud));
(ANGLE_output_global.fragColor) = (vec4<f32>(((ANGLE_defaultUniformBlock.unis)._ua)._ux, (ANGLE_defaultUniformBlock.unis)._ub, (ANGLE_defaultUniformBlock.unis)._uc, (_udCopy)[1i]));
(ANGLE_output_global.fragColor) += (vec4<f32>((ANGLE_defaultUniformBlock.unis)._ud[2i].elem, (ANGLE_defaultUniformBlock.unis)._ue, (((ANGLE_defaultUniformBlock.unis)._uf)[0i])[2i], (select((ANGLE_Convert_Array5_ANGLE_wrapped_float_ElementsTo_float_Elements((ANGLE_defaultUniformBlock.unis)._ug)), (ANGLE_Convert_Array5_ANGLE_wrapped_float_ElementsTo_float_Elements((ANGLE_defaultUniformBlock.unis)._ud)), (((ANGLE_defaultUniformBlock.unis)._ue) > (0.5f))))[1i]));
}
@fragment
fn wgslMain() -> ANGLE_Output_Annotated
{
_umain();
var ANGLE_output_annotated : ANGLE_Output_Annotated;
ANGLE_output_annotated.fragColor = ANGLE_output_global.fragColor;
return ANGLE_output_annotated;
}
)";
compile(shaderString);
EXPECT_TRUE(foundInCode(outputString.c_str()));
}