Hash :
a6ee4641
Author :
Date :
2024-09-25T11:41:47
WGSL: Output default uniform block and accesses to it Default uniforms are put into a WGSL struct, and all accesses of those uniforms now output struct accesses. Similarly to I/O vars and builtins, these are outputted in a pre-pass, but in the future it might make sense to do what Vulkan does and do an AST transformation to put the default uniforms into a UBO which should be outputted similarly. This does not handle bool, matCx2, or array of element size < 16. Bug: angleproject:42267100 Change-Id: If29e2895a8aba3212b581813316af87273c1515c Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5878759 Reviewed-by: Liza Burakova <liza@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Matthew Denton <mpdenton@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
//
// 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(0) 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()));
}