Hash :
d193d51b
Author :
Date :
2024-06-17T22:46:08
Replace issue ids post migration to new issue tracker This change replaces anglebug.com/NNNN links. Bug: None Change-Id: I8ac3aec8d2a8a844b3d7b99fc0a6b2be8da31761 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5637912 Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@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
//
// Copyright 2018 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.
//
// ParallelShaderCompileTest.cpp : Tests of the GL_KHR_parallel_shader_compile extension.
#include "test_utils/ANGLETest.h"
#include "test_utils/gl_raii.h"
#include "util/random_utils.h"
#include "util/test_utils.h"
using namespace angle;
namespace
{
namespace
{
constexpr int kTaskCount = 32;
constexpr unsigned int kPollInterval = 100;
} // anonymous namespace
class ParallelShaderCompileTest : public ANGLETest<>
{
protected:
ParallelShaderCompileTest()
{
setWindowWidth(128);
setWindowHeight(128);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
}
bool ensureParallelShaderCompileExtensionAvailable()
{
if (IsGLExtensionRequestable("GL_KHR_parallel_shader_compile"))
{
glRequestExtensionANGLE("GL_KHR_parallel_shader_compile");
}
if (!IsGLExtensionEnabled("GL_KHR_parallel_shader_compile"))
{
return false;
}
return true;
}
class Task
{
public:
Task(int id) : mID(id) {}
virtual ~Task() {}
virtual bool compile() = 0;
virtual bool isCompileCompleted() = 0;
virtual bool link() = 0;
virtual void postLink() {}
virtual void runAndVerify(ParallelShaderCompileTest *test) = 0;
bool isLinkCompleted()
{
GLint status;
glGetProgramiv(mProgram, GL_COMPLETION_STATUS_KHR, &status);
return (status == GL_TRUE);
}
protected:
static std::string InsertRandomString(const std::string &source)
{
RNG rng;
std::ostringstream ostream;
ostream << source << "\n// Random string to fool program cache: " << rng.randomInt()
<< "\n";
return ostream.str();
}
static GLuint CompileShader(GLenum type, const std::string &source)
{
GLuint shader = glCreateShader(type);
const char *sourceArray[1] = {source.c_str()};
glShaderSource(shader, 1, sourceArray, nullptr);
glCompileShader(shader);
return shader;
}
static void RecompileShader(GLuint shader, const std::string &source)
{
const char *sourceArray[1] = {source.c_str()};
glShaderSource(shader, 1, sourceArray, nullptr);
glCompileShader(shader);
}
static bool CheckShader(GLuint shader)
{
GLint compileResult;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
if (compileResult == 0)
{
GLint infoLogLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
// Info log length includes the null terminator, so 1 means that the info log is an
// empty string.
if (infoLogLength > 1)
{
std::vector<GLchar> infoLog(infoLogLength);
glGetShaderInfoLog(shader, static_cast<GLsizei>(infoLog.size()), nullptr,
&infoLog[0]);
std::cerr << "shader compilation failed: " << &infoLog[0];
}
else
{
std::cerr << "shader compilation failed. <Empty log message>";
}
std::cerr << std::endl;
}
return (compileResult == GL_TRUE);
}
GLuint mProgram;
int mID;
};
template <typename T>
class TaskRunner
{
public:
TaskRunner() {}
~TaskRunner() {}
void run(ParallelShaderCompileTest *test, unsigned int pollInterval)
{
std::vector<std::unique_ptr<T>> compileTasks;
for (int i = 0; i < kTaskCount; ++i)
{
std::unique_ptr<T> task(new T(i));
bool isCompiling = task->compile();
ASSERT_TRUE(isCompiling);
compileTasks.push_back(std::move(task));
}
std::vector<std::unique_ptr<T>> linkTasks;
while (!compileTasks.empty())
{
for (unsigned int i = 0; i < compileTasks.size();)
{
auto &task = compileTasks[i];
if (task->isCompileCompleted())
{
bool isLinking = task->link();
task->postLink();
ASSERT_TRUE(isLinking);
linkTasks.push_back(std::move(task));
compileTasks.erase(compileTasks.begin() + i);
continue;
}
++i;
}
if (pollInterval != 0)
{
angle::Sleep(pollInterval);
}
}
while (!linkTasks.empty())
{
for (unsigned int i = 0; i < linkTasks.size();)
{
auto &task = linkTasks[i];
if (task->isLinkCompleted())
{
task->runAndVerify(test);
linkTasks.erase(linkTasks.begin() + i);
continue;
}
else
{
task->postLink();
}
++i;
}
if (pollInterval != 0)
{
angle::Sleep(pollInterval);
}
}
}
};
class ClearColorWithDraw : public Task
{
public:
ClearColorWithDraw(int taskID) : Task(taskID)
{
auto color = static_cast<GLubyte>(taskID * 255 / kTaskCount);
mColor = {color, color, color, 255};
}
bool compile() override
{
mVertexShader =
CompileShader(GL_VERTEX_SHADER, InsertRandomString(essl1_shaders::vs::Simple()));
mFragmentShader = CompileShader(GL_FRAGMENT_SHADER,
InsertRandomString(essl1_shaders::fs::UniformColor()));
return (mVertexShader != 0 && mFragmentShader != 0);
}
bool isCompileCompleted() override
{
GLint status;
glGetShaderiv(mVertexShader, GL_COMPLETION_STATUS_KHR, &status);
if (status == GL_TRUE)
{
glGetShaderiv(mFragmentShader, GL_COMPLETION_STATUS_KHR, &status);
return (status == GL_TRUE);
}
return false;
}
bool link() override
{
mProgram = 0;
if (CheckShader(mVertexShader) && CheckShader(mFragmentShader))
{
mProgram = glCreateProgram();
glAttachShader(mProgram, mVertexShader);
glAttachShader(mProgram, mFragmentShader);
glLinkProgram(mProgram);
}
glDeleteShader(mVertexShader);
glDeleteShader(mFragmentShader);
return (mProgram != 0);
}
void runAndVerify(ParallelShaderCompileTest *test) override
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glUseProgram(mProgram);
ASSERT_GL_NO_ERROR();
GLint colorUniformLocation =
glGetUniformLocation(mProgram, essl1_shaders::ColorUniform());
ASSERT_NE(colorUniformLocation, -1);
auto normalizeColor = mColor.toNormalizedVector();
glUniform4fv(colorUniformLocation, 1, normalizeColor.data());
test->drawQuad(mProgram, essl1_shaders::PositionAttrib(), 0.5f);
EXPECT_PIXEL_COLOR_EQ(test->getWindowWidth() / 2, test->getWindowHeight() / 2, mColor);
glUseProgram(0);
glDeleteProgram(mProgram);
ASSERT_GL_NO_ERROR();
}
protected:
void recompile()
{
RecompileShader(mVertexShader, essl1_shaders::vs::Simple());
RecompileShader(mFragmentShader, essl1_shaders::fs::UniformColor());
}
private:
GLuint mVertexShader;
GLuint mFragmentShader;
GLColor mColor;
};
class ClearColorWithDrawRecompile : public ClearColorWithDraw
{
public:
ClearColorWithDrawRecompile(int taskID) : ClearColorWithDraw(taskID) {}
void postLink() override { recompile(); }
};
class ImageLoadStore : public Task
{
public:
ImageLoadStore(int taskID) : Task(taskID) {}
~ImageLoadStore() {}
bool compile() override
{
const char kCSSource[] = R"(#version 310 es
layout(local_size_x=1, local_size_y=1, local_size_z=1) in;
layout(r32ui, binding = 0) readonly uniform highp uimage2D uImage_1;
layout(r32ui, binding = 1) writeonly uniform highp uimage2D uImage_2;
void main()
{
uvec4 value = imageLoad(uImage_1, ivec2(gl_LocalInvocationID.xy));
imageStore(uImage_2, ivec2(gl_LocalInvocationID.xy), value);
})";
mShader = CompileShader(GL_COMPUTE_SHADER, InsertRandomString(kCSSource));
return mShader != 0;
}
bool isCompileCompleted() override
{
GLint status;
glGetShaderiv(mShader, GL_COMPLETION_STATUS_KHR, &status);
return status == GL_TRUE;
}
bool link() override
{
mProgram = 0;
if (CheckShader(mShader))
{
mProgram = glCreateProgram();
glAttachShader(mProgram, mShader);
glLinkProgram(mProgram);
}
glDeleteShader(mShader);
return mProgram != 0;
}
void runAndVerify(ParallelShaderCompileTest *test) override
{
// Taken from ComputeShaderTest.StoreImageThenLoad.
constexpr GLuint kInputValues[3][1] = {{300}, {200}, {100}};
GLTexture texture[3];
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, 1, 1);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RED_INTEGER, GL_UNSIGNED_INT,
kInputValues[0]);
EXPECT_GL_NO_ERROR();
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, 1, 1);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RED_INTEGER, GL_UNSIGNED_INT,
kInputValues[1]);
EXPECT_GL_NO_ERROR();
glBindTexture(GL_TEXTURE_2D, texture[2]);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, 1, 1);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RED_INTEGER, GL_UNSIGNED_INT,
kInputValues[2]);
EXPECT_GL_NO_ERROR();
glUseProgram(mProgram);
glBindImageTexture(0, texture[0], 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32UI);
glBindImageTexture(1, texture[1], 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32UI);
glDispatchCompute(1, 1, 1);
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
EXPECT_GL_NO_ERROR();
glBindImageTexture(0, texture[1], 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32UI);
glBindImageTexture(1, texture[2], 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32UI);
glDispatchCompute(1, 1, 1);
glMemoryBarrier(GL_FRAMEBUFFER_BARRIER_BIT);
EXPECT_GL_NO_ERROR();
GLuint outputValue;
GLFramebuffer framebuffer;
glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
texture[2], 0);
glReadPixels(0, 0, 1, 1, GL_RED_INTEGER, GL_UNSIGNED_INT, &outputValue);
EXPECT_GL_NO_ERROR();
EXPECT_EQ(300u, outputValue);
glUseProgram(0);
glDeleteProgram(mProgram);
ASSERT_GL_NO_ERROR();
}
private:
GLuint mShader;
};
};
// Test basic functionality of GL_KHR_parallel_shader_compile
TEST_P(ParallelShaderCompileTest, Basic)
{
ANGLE_SKIP_TEST_IF(!ensureParallelShaderCompileExtensionAvailable());
GLint count = 0;
glMaxShaderCompilerThreadsKHR(8);
EXPECT_GL_NO_ERROR();
glGetIntegerv(GL_MAX_SHADER_COMPILER_THREADS_KHR, &count);
EXPECT_GL_NO_ERROR();
EXPECT_EQ(8, count);
}
// Test to compile and link many programs in parallel.
TEST_P(ParallelShaderCompileTest, LinkAndDrawManyPrograms)
{
ANGLE_SKIP_TEST_IF(!ensureParallelShaderCompileExtensionAvailable());
TaskRunner<ClearColorWithDraw> runner;
runner.run(this, kPollInterval);
}
// Tests no crash in case that the Shader starts another compile while the Program being attached
// to is still linking.
// crbug.com/1317673
TEST_P(ParallelShaderCompileTest, LinkProgramAndRecompileShader)
{
ANGLE_SKIP_TEST_IF(!ensureParallelShaderCompileExtensionAvailable());
TaskRunner<ClearColorWithDrawRecompile> runner;
runner.run(this, 0);
}
class ParallelShaderCompileTestES31 : public ParallelShaderCompileTest
{};
// Test to compile and link many computing programs in parallel.
TEST_P(ParallelShaderCompileTestES31, LinkAndDispatchManyPrograms)
{
// Flaky on Win NVIDIA D3D11. http://anglebug.com/40096580
// Suspectable to the flakyness of http://anglebug.com/40096579.
ANGLE_SKIP_TEST_IF(IsWindows() && IsD3D11());
// TODO(http://anglebug.com/42264192): Fails on Linux+Intel+OpenGL
ANGLE_SKIP_TEST_IF(IsLinux() && IsIntel() && IsOpenGL());
ANGLE_SKIP_TEST_IF(!ensureParallelShaderCompileExtensionAvailable());
TaskRunner<ImageLoadStore> runner;
runner.run(this, kPollInterval);
}
ANGLE_INSTANTIATE_TEST_ES2(ParallelShaderCompileTest);
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ParallelShaderCompileTestES31);
ANGLE_INSTANTIATE_TEST_ES31(ParallelShaderCompileTestES31);
} // namespace