Hash :
35f01e7f
Author :
Date :
2024-06-26T14:30:27
ESSL -> WGSL: Support basic control flow If/else, while, do/while, for. Return, break, continue, discard Bug: angleproject:42267100 Change-Id: I0c8ef30b45aec639a07323e333db970d4c42dec0 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5661103 Reviewed-by: Liza Burakova <liza@chromium.org> Commit-Queue: Matthew Denton <mpdenton@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 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
//
// 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);
})";
const std::string &outputString =
R"(_uoutColor : vec4<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()
{
_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))));
_uarrIndex : i32 = (1i);
_uf : f32 = (((((_ufoo)._umultiArray)[0i])[1i]).x);
_uf2 : f32 = (((((_ufoo)._umultiArray)[0i])[clamp((_uarrIndex), 0, 2)]).x);
(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);
}
)";
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
{
_ux : i32 = (5i);
if ((_ux) == (5i))
{
return 6i;
}
else
{
if ((_ux) == (6i))
{
return 7i;
}
else
{
return 8i;
}
}
}
fn _uswitchDemo()
{
_ux : i32 = (5i);
switch _ux
{
case 5i, 6i:
{
discard;
}
case 7i:
{
{
return;
}
}
case 8i, 9i:
{
{
(_ux) = (7i);
}
return;
}
case default:
{
return;
}
}
}
fn _uforLoopDemo()
{
for (_ui : i32 = (0i); (_ui) < (5i); (_ui)++)
{
if ((_ui) == (4i))
{
break;
}
else
{
if ((_ui) == (5i))
{
continue;
}
}
}
}
fn _uwhileLoopDemo()
{
_ui : i32 = (0i);
while ((_ui) < (5i))
{
(_ui)++;
}
loop {
{
(_ui)++;
}
if (!((_ui) < (5i)) { break; }
}
}
fn _umain()
{
_uifElseDemo();
_uswitchDemo();
_uforLoopDemo();
_uwhileLoopDemo();
}
)";
compile(shaderString);
EXPECT_TRUE(foundInCode(outputString.c_str()));
}