Hash :
cfe2c8fe
Author :
Date :
2025-06-25T13:25:01
WGSL: RewriteMultielementSwizzle WGSL doesn't support assignments to multi-element swizzles. This is used in a lot of shader tests, so temporarily work around this with an AST traverser that splits these assignments into multiple assignments that only assign to single element swizzles. One special case is multiplication-by-a-matrix assignment: vec.xy *= mat; is converted to vec.x = (vec.xy * mat).x; vec.y = (vec.xy * mat).y; Bug: angleproject:392542001 Change-Id: I3f393039aae13eb3f2c5dc5e553f68eb03b6316d Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6847280 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Matthew Denton <mpdenton@chromium.org> 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
//
// Copyright 2015 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.
//
#ifdef UNSAFE_BUFFERS_BUILD
# pragma allow_unsafe_buffers
#endif
#include "test_utils/ANGLETest.h"
#include "test_utils/angle_test_configs.h"
#include "test_utils/gl_raii.h"
#include "util/gles_loader_autogen.h"
using namespace angle;
class CubeMapTextureTest : public ANGLETest<>
{
protected:
CubeMapTextureTest()
{
setWindowWidth(256);
setWindowHeight(256);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
}
void testSetUp() override
{
mProgram = CompileProgram(essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
if (mProgram == 0)
{
FAIL() << "shader compilation failed.";
}
mColorLocation = glGetUniformLocation(mProgram, essl1_shaders::ColorUniform());
glUseProgram(mProgram);
glClearColor(0, 0, 0, 0);
glClearDepthf(0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
ASSERT_GL_NO_ERROR();
}
void testTearDown() override { glDeleteProgram(mProgram); }
void runSampleCoordinateTransformTest(const char *shader, const bool useES3);
GLuint mProgram;
GLint mColorLocation;
};
// Verify that uploading to the faces of a cube map consecutively will correctly upload to each
// face.
TEST_P(CubeMapTextureTest, UploadToFacesConsecutively)
{
const GLColor faceColors[] = {
GLColor::red, GLColor::green, GLColor::blue,
GLColor::yellow, GLColor::magenta, GLColor::cyan,
};
GLuint tex = 0;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
for (int face = 5; face >= 0; face--)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGBA, 1, 1, 0, GL_RGBA,
GL_UNSIGNED_BYTE, faceColors[face].data());
EXPECT_GL_NO_ERROR();
}
EXPECT_GL_NO_ERROR();
GLuint fbo = 0;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
EXPECT_GL_NO_ERROR();
for (GLenum face = 0; face < 6; face++)
{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, tex, 0);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(0, 0, faceColors[face]);
EXPECT_GL_NO_ERROR();
}
glDeleteFramebuffers(1, &fbo);
glDeleteTextures(1, &tex);
EXPECT_GL_NO_ERROR();
}
// Verify that rendering to the faces of a cube map consecutively will correctly render to each
// face.
TEST_P(CubeMapTextureTest, RenderToFacesConsecutively)
{
// TODO: Diagnose and fix. http://anglebug.com/42261648
ANGLE_SKIP_TEST_IF(IsVulkan() && IsIntel() && IsWindows());
// http://anglebug.com/42261821
ANGLE_SKIP_TEST_IF(IsVulkan() && IsIntel() && IsFuchsia());
const GLfloat faceColors[] = {
1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
};
GLuint tex = 0;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
for (GLenum face = 0; face < 6; face++)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGBA, 1, 1, 0, GL_RGBA,
GL_UNSIGNED_BYTE, nullptr);
}
EXPECT_GL_NO_ERROR();
GLuint fbo = 0;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
EXPECT_GL_NO_ERROR();
for (GLenum face = 0; face < 6; face++)
{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, tex, 0);
EXPECT_GL_NO_ERROR();
glUseProgram(mProgram);
const GLfloat *faceColor = faceColors + (face * 4);
glUniform4f(mColorLocation, faceColor[0], faceColor[1], faceColor[2], faceColor[3]);
drawQuad(mProgram, essl1_shaders::PositionAttrib(), 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(0, 0, faceColor[0] * 255, faceColor[1] * 255, faceColor[2] * 255,
faceColor[3] * 255);
EXPECT_GL_NO_ERROR();
}
for (GLenum face = 0; face < 6; face++)
{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, tex, 0);
EXPECT_GL_NO_ERROR();
const GLfloat *faceColor = faceColors + (face * 4);
EXPECT_PIXEL_EQ(0, 0, faceColor[0] * 255, faceColor[1] * 255, faceColor[2] * 255,
faceColor[3] * 255);
EXPECT_GL_NO_ERROR();
}
glDeleteFramebuffers(1, &fbo);
glDeleteTextures(1, &tex);
EXPECT_GL_NO_ERROR();
}
void CubeMapTextureTest::runSampleCoordinateTransformTest(const char *shader, const bool useES3)
{
// Fails to compile the shader. anglebug.com/42262420
ANGLE_SKIP_TEST_IF(IsOpenGL() && IsIntel() && IsWindows());
constexpr GLsizei kCubeFaceCount = 6;
constexpr GLsizei kCubeFaceSectionCount = 4;
constexpr GLsizei kCubeFaceSectionCountSqrt = 2;
constexpr GLColor faceColors[kCubeFaceCount][kCubeFaceSectionCount] = {
{GLColor(255, 0, 0, 255), GLColor(191, 0, 0, 255), GLColor(127, 0, 0, 255),
GLColor(63, 0, 0, 255)},
{GLColor(0, 255, 0, 255), GLColor(0, 191, 0, 255), GLColor(0, 127, 0, 255),
GLColor(0, 63, 0, 255)},
{GLColor(0, 0, 255, 255), GLColor(0, 0, 191, 255), GLColor(0, 0, 127, 255),
GLColor(0, 0, 63, 255)},
{GLColor(255, 63, 0, 255), GLColor(191, 127, 0, 255), GLColor(127, 191, 0, 255),
GLColor(63, 255, 0, 255)},
{GLColor(0, 255, 63, 255), GLColor(0, 191, 127, 255), GLColor(0, 127, 191, 255),
GLColor(0, 63, 255, 255)},
{GLColor(63, 0, 255, 255), GLColor(127, 0, 191, 255), GLColor(191, 0, 127, 255),
GLColor(255, 0, 63, 255)},
};
constexpr GLsizei kTextureSize = 32;
GLTexture tex;
glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
for (GLenum face = 0; face < kCubeFaceCount; face++)
{
std::vector<GLColor> faceData(kTextureSize * kTextureSize);
// Create the face with four sections, each with a solid color from |faceColors|.
for (size_t row = 0; row < kTextureSize / kCubeFaceSectionCountSqrt; ++row)
{
for (size_t col = 0; col < kTextureSize / kCubeFaceSectionCountSqrt; ++col)
{
for (size_t srow = 0; srow < kCubeFaceSectionCountSqrt; ++srow)
{
for (size_t scol = 0; scol < kCubeFaceSectionCountSqrt; ++scol)
{
size_t r = row + srow * kTextureSize / kCubeFaceSectionCountSqrt;
size_t c = col + scol * kTextureSize / kCubeFaceSectionCountSqrt;
size_t s = srow * kCubeFaceSectionCountSqrt + scol;
faceData[r * kTextureSize + c] = faceColors[face][s];
}
}
}
}
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGBA, kTextureSize, kTextureSize,
0, GL_RGBA, GL_UNSIGNED_BYTE, faceData.data());
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
EXPECT_GL_NO_ERROR();
GLTexture fboTex;
glBindTexture(GL_TEXTURE_2D, fboTex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kCubeFaceCount, kCubeFaceSectionCount, 0, GL_RGBA,
GL_UNSIGNED_BYTE, nullptr);
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fboTex, 0);
EXPECT_GL_NO_ERROR();
ANGLE_GL_PROGRAM(program, useES3 ? essl3_shaders::vs::Simple() : essl1_shaders::vs::Simple(),
shader);
glUseProgram(program);
GLint texCubeLocation = glGetUniformLocation(program, "texCube");
ASSERT_NE(-1, texCubeLocation);
glUniform1i(texCubeLocation, 0);
drawQuad(program, useES3 ? essl3_shaders::PositionAttrib() : essl1_shaders::PositionAttrib(),
0.5f);
EXPECT_GL_NO_ERROR();
for (GLenum face = 0; face < kCubeFaceCount; face++)
{
// The following table defines the translation from textureCube coordinates to coordinates
// in each face. The framebuffer has width 6 and height 4. Every column corresponding to
// an x value represents one cube face. The values in rows are samples from the four
// sections of the face.
//
// Major Axis Direction Target sc tc ma
// +rx TEXTURE_CUBE_MAP_POSITIVE_X −rz −ry rx
// −rx TEXTURE_CUBE_MAP_NEGATIVE_X rz −ry rx
// +ry TEXTURE_CUBE_MAP_POSITIVE_Y rx rz ry
// −ry TEXTURE_CUBE_MAP_NEGATIVE_Y rx −rz ry
// +rz TEXTURE_CUBE_MAP_POSITIVE_Z rx −ry rz
// −rz TEXTURE_CUBE_MAP_NEGATIVE_Z −rx −ry rz
//
// This table is used only to determine the direction of growth for s and t. The shader
// always generates (row,col) coordinates (0, 0), (0, 1), (1, 0), (1, 1) which is the order
// the data is uploaded to the faces, but based on the table above, the sample order would
// be different.
constexpr size_t faceSampledSections[kCubeFaceCount][kCubeFaceSectionCount] = {
{3, 2, 1, 0}, {2, 3, 0, 1}, {0, 1, 2, 3}, {2, 3, 0, 1}, {2, 3, 0, 1}, {3, 2, 1, 0},
};
for (size_t section = 0; section < kCubeFaceSectionCount; ++section)
{
const GLColor sectionColor = faceColors[face][faceSampledSections[face][section]];
EXPECT_PIXEL_COLOR_EQ(face, section, sectionColor)
<< "face " << face << ", section " << section;
}
}
EXPECT_GL_NO_ERROR();
}
// Verify that cube map sampling follows the rules that map cubemap coordinates to coordinates
// within each face. See section 3.7.5 of GLES2.0 (Cube Map Texture Selection).
TEST_P(CubeMapTextureTest, SampleCoordinateTransform)
{
// http://anglebug.com/40096654
ANGLE_SKIP_TEST_IF(IsWindows() && IsD3D9());
// Create a program that samples from 6x4 directions of the cubemap, draw and verify that the
// colors match the right color from |faceColors|.
constexpr char kFS[] = R"(precision mediump float;
uniform samplerCube texCube;
const mat4 coordInSection = mat4(
vec4(-0.5, -0.5, 0, 0),
vec4( 0.5, -0.5, 0, 0),
vec4(-0.5, 0.5, 0, 0),
vec4( 0.5, 0.5, 0, 0)
);
void main()
{
vec3 coord;
if (gl_FragCoord.x < 2.0)
{
coord.x = gl_FragCoord.x < 1.0 ? 1.0 : -1.0;
coord.zy = coordInSection[int(gl_FragCoord.y)].xy;
}
else if (gl_FragCoord.x < 4.0)
{
coord.y = gl_FragCoord.x < 3.0 ? 1.0 : -1.0;
coord.xz = coordInSection[int(gl_FragCoord.y)].xy;
}
else
{
coord.z = gl_FragCoord.x < 5.0 ? 1.0 : -1.0;
coord.xy = coordInSection[int(gl_FragCoord.y)].xy;
}
gl_FragColor = textureCube(texCube, coord);
})";
runSampleCoordinateTransformTest(kFS, false);
}
// On Android Vulkan, unequal x and y derivatives cause this test to fail.
TEST_P(CubeMapTextureTest, SampleCoordinateTransformGrad)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_shader_texture_lod"));
constexpr char kFS[] = R"(#extension GL_EXT_shader_texture_lod : require
precision mediump float;
uniform samplerCube texCube;
const mat4 coordInSection = mat4(
vec4(-0.5, -0.5, 0, 0),
vec4( 0.5, -0.5, 0, 0),
vec4(-0.5, 0.5, 0, 0),
vec4( 0.5, 0.5, 0, 0)
);
void main()
{
vec3 coord;
if (gl_FragCoord.x < 2.0)
{
coord.x = gl_FragCoord.x < 1.0 ? 1.0 : -1.0;
coord.zy = coordInSection[int(gl_FragCoord.y)].xy;
}
else if (gl_FragCoord.x < 4.0)
{
coord.y = gl_FragCoord.x < 3.0 ? 1.0 : -1.0;
coord.xz = coordInSection[int(gl_FragCoord.y)].xy;
}
else
{
coord.z = gl_FragCoord.x < 5.0 ? 1.0 : -1.0;
coord.xy = coordInSection[int(gl_FragCoord.y)].xy;
}
gl_FragColor = textureCubeGradEXT(texCube, coord,
vec3(10.0, 10.0, 0.0), vec3(0.0, 10.0, 10.0));
})";
runSampleCoordinateTransformTest(kFS, false);
}
// Same as the previous but uses the ES 3.0 explicit gradient function.
TEST_P(CubeMapTextureTest, SampleCoordinateTransformGrad_ES3)
{
ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3);
constexpr char kFS[] = R"(#version 300 es
precision mediump float;
uniform samplerCube texCube;
out vec4 my_FragColor;
const mat4 coordInSection = mat4(
vec4(-0.5, -0.5, 0, 0),
vec4( 0.5, -0.5, 0, 0),
vec4(-0.5, 0.5, 0, 0),
vec4( 0.5, 0.5, 0, 0)
);
void main()
{
vec3 coord;
if (gl_FragCoord.x < 2.0)
{
coord.x = gl_FragCoord.x < 1.0 ? 1.0 : -1.0;
coord.zy = coordInSection[int(gl_FragCoord.y)].xy;
}
else if (gl_FragCoord.x < 4.0)
{
coord.y = gl_FragCoord.x < 3.0 ? 1.0 : -1.0;
coord.xz = coordInSection[int(gl_FragCoord.y)].xy;
}
else
{
coord.z = gl_FragCoord.x < 5.0 ? 1.0 : -1.0;
coord.xy = coordInSection[int(gl_FragCoord.y)].xy;
}
my_FragColor = textureGrad(texCube, coord,
vec3(10.0, 10.0, 0.0), vec3(0.0, 10.0, 10.0));
})";
runSampleCoordinateTransformTest(kFS, true);
}
// Use this to select which configurations (e.g. which renderer, which GLES major version) these
// tests should be run against.
ANGLE_INSTANTIATE_TEST_ES2_AND_ES3_AND(CubeMapTextureTest, ES2_WEBGPU(), ES3_WEBGPU());