Commit 90cf278c by Ian Elliott Committed by Commit Bot

Add multi-thread/context test for compute shaders

The "Ragnarok M: Eternal Love" game uncovered a bug where 2 contexts shared a VkCommandBuffer. The one context was used by one thread for rendering, and the other context/thread dispatched compute shaders. When the rendering thread destroyed a VkCommandBuffer, future compute dispatches crashed because the new VkCommandBuffer didn't have a compute pipeline bound. Bug: b/181711029 Change-Id: I8bc85150c1c6202e02feb84a7ccc0ad7b9c39258 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2770681 Commit-Queue: Ian Elliott <ianelliott@google.com> Reviewed-by: 's avatarShahbaz Youssefi <syoussefi@chromium.org>
parent 135385a1
......@@ -13,6 +13,8 @@ angle_end2end_tests_sources = [
"egl_tests/EGLContextSharingTest.cpp",
"egl_tests/EGLCreateContextAttribsTest.cpp",
"egl_tests/EGLDebugTest.cpp",
"egl_tests/EGLMultiContextTest.cpp",
"egl_tests/EGLMultiThreadSteps.h",
"egl_tests/EGLNoConfigContextTest.cpp",
"egl_tests/EGLPreRotationTest.cpp",
"egl_tests/EGLPrintEGLinfoTest.cpp",
......
......@@ -8,16 +8,12 @@
#include <gtest/gtest.h>
#include "EGLMultiThreadSteps.h"
#include "test_utils/ANGLETest.h"
#include "test_utils/angle_test_configs.h"
#include "test_utils/gl_raii.h"
#include "util/EGLWindow.h"
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
using namespace angle;
namespace
......@@ -440,65 +436,13 @@ TEST_P(EGLContextSharingTest, DeleteReaderOfSharedTexture)
};
Step currentStep = Step::Start;
// Helper functions to synchronize the threads so that the operations are executed in the
// specific order the test is written for.
auto waitForStep = [&](Step waitStep) -> bool {
std::unique_lock<std::mutex> lock(mutex);
while (currentStep != waitStep)
{
// If necessary, abort execution as the other thread has encountered a GL error.
if (currentStep == Step::Abort)
{
return false;
}
condVar.wait(lock);
}
return true;
};
auto nextStep = [&](Step newStep) {
{
std::unique_lock<std::mutex> lock(mutex);
currentStep = newStep;
}
condVar.notify_one();
};
class AbortOnFailure
{
public:
AbortOnFailure(Step *currentStep, std::mutex *mutex, std::condition_variable *condVar)
: mCurrentStep(currentStep), mMutex(mutex), mCondVar(condVar)
{}
~AbortOnFailure()
{
bool isAborting = false;
{
std::unique_lock<std::mutex> lock(*mMutex);
isAborting = *mCurrentStep != Step::Finish;
if (isAborting)
{
*mCurrentStep = Step::Abort;
}
}
mCondVar->notify_all();
}
private:
Step *mCurrentStep;
std::mutex *mMutex;
std::condition_variable *mCondVar;
};
std::thread deletingThread = std::thread([&]() {
AbortOnFailure abortOnFailure(&currentStep, &mutex, &condVar);
ThreadSynchronization<Step> threadSynchronization(&currentStep, &mutex, &condVar);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface[0], surface[0], ctx[0]));
EXPECT_EGL_SUCCESS();
ASSERT_TRUE(waitForStep(Step::Start));
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
// Draw using the shared texture.
drawQuad(program[0].get(), essl1_shaders::PositionAttrib(), 0.5f);
......@@ -509,8 +453,8 @@ TEST_P(EGLContextSharingTest, DeleteReaderOfSharedTexture)
glFlush();
// Wait for the other thread to also draw using the shared texture.
nextStep(Step::Thread0Draw);
ASSERT_TRUE(waitForStep(Step::Thread1Draw));
threadSynchronization.nextStep(Step::Thread0Draw);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread1Draw));
ASSERT_TRUE(continuingThreadSyncObj != nullptr);
glWaitSync(continuingThreadSyncObj, 0, GL_TIMEOUT_IGNORED);
......@@ -524,8 +468,8 @@ TEST_P(EGLContextSharingTest, DeleteReaderOfSharedTexture)
// Wait for the other thread to use the shared texture again before unbinding the
// context (so no implicit flush happens).
nextStep(Step::Thread0Delete);
ASSERT_TRUE(waitForStep(Step::Finish));
threadSynchronization.nextStep(Step::Thread0Delete);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Finish));
EXPECT_GL_NO_ERROR();
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
......@@ -533,13 +477,13 @@ TEST_P(EGLContextSharingTest, DeleteReaderOfSharedTexture)
});
std::thread continuingThread = std::thread([&]() {
AbortOnFailure abortOnFailure(&currentStep, &mutex, &condVar);
ThreadSynchronization<Step> threadSynchronization(&currentStep, &mutex, &condVar);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface[1], surface[1], ctx[1]));
EXPECT_EGL_SUCCESS();
// Wait for first thread to draw using the shared texture.
ASSERT_TRUE(waitForStep(Step::Thread0Draw));
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0Draw));
ASSERT_TRUE(deletingThreadSyncObj != nullptr);
glWaitSync(deletingThreadSyncObj, 0, GL_TIMEOUT_IGNORED);
......@@ -557,8 +501,8 @@ TEST_P(EGLContextSharingTest, DeleteReaderOfSharedTexture)
glFlush();
// Wait for the other thread to delete its framebuffer.
nextStep(Step::Thread1Draw);
ASSERT_TRUE(waitForStep(Step::Thread0Delete));
threadSynchronization.nextStep(Step::Thread1Draw);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0Delete));
// Write to the shared texture differently, so a dependency is created from the previous
// readers of the shared texture (the two framebuffers of the two threads) to the new
......@@ -572,7 +516,7 @@ TEST_P(EGLContextSharingTest, DeleteReaderOfSharedTexture)
&kTexData2);
drawQuad(program[0].get(), essl1_shaders::PositionAttrib(), 0.5f);
nextStep(Step::Finish);
threadSynchronization.nextStep(Step::Finish);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_SUCCESS();
......
//
// Copyright 2016-2021 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.
//
// EGLMultiContextTest.cpp:
// Tests relating to multiple non-shared Contexts.
#include <gtest/gtest.h>
#include "EGLMultiThreadSteps.h"
#include "test_utils/ANGLETest.h"
#include "test_utils/angle_test_configs.h"
#include "test_utils/gl_raii.h"
#include "util/EGLWindow.h"
using namespace angle;
namespace
{
EGLBoolean SafeDestroyContext(EGLDisplay display, EGLContext &context)
{
EGLBoolean result = EGL_TRUE;
if (context != EGL_NO_CONTEXT)
{
result = eglDestroyContext(display, context);
context = EGL_NO_CONTEXT;
}
return result;
}
class EGLMultiContextTest : public ANGLETest
{
public:
EGLMultiContextTest() : mContexts{EGL_NO_CONTEXT, EGL_NO_CONTEXT}, mTexture(0) {}
void testTearDown() override
{
glDeleteTextures(1, &mTexture);
EGLDisplay display = getEGLWindow()->getDisplay();
if (display != EGL_NO_DISPLAY)
{
for (auto &context : mContexts)
{
SafeDestroyContext(display, context);
}
}
// Set default test state to not give an error on shutdown.
getEGLWindow()->makeCurrent();
}
EGLContext mContexts[2];
GLuint mTexture;
};
// Test that a compute shader running in one thread will still work when rendering is happening in
// another thread (with non-shared contexts). The non-shared context will still share a Vulkan
// command buffer.
TEST_P(EGLMultiContextTest, ComputeShaderOkayWithRendering)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
ANGLE_SKIP_TEST_IF(!isVulkanRenderer());
ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 || getClientMinorVersion() < 1);
// Initialize contexts
EGLWindow *window = getEGLWindow();
EGLDisplay dpy = window->getDisplay();
EGLConfig config = window->getConfig();
constexpr size_t kThreadCount = 2;
EGLSurface surface[kThreadCount] = {EGL_NO_SURFACE, EGL_NO_SURFACE};
EGLContext ctx[kThreadCount] = {EGL_NO_CONTEXT, EGL_NO_CONTEXT};
EGLint pbufferAttributes[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE};
for (size_t t = 0; t < kThreadCount; ++t)
{
surface[t] = eglCreatePbufferSurface(dpy, config, pbufferAttributes);
EXPECT_EGL_SUCCESS();
ctx[t] = window->createContext(EGL_NO_CONTEXT);
EXPECT_NE(EGL_NO_CONTEXT, ctx[t]);
}
// Synchronization tools to ensure the two threads are interleaved as designed by this test.
std::mutex mutex;
std::condition_variable condVar;
enum class Step
{
Thread0Start,
Thread0DispatchedCompute,
Thread1Drew,
Thread0DispatchedComputeAgain,
Finish,
Abort,
};
Step currentStep = Step::Thread0Start;
// This first thread dispatches a compute shader. It immediately starts.
std::thread deletingThread = std::thread([&]() {
ThreadSynchronization<Step> threadSynchronization(&currentStep, &mutex, &condVar);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface[0], surface[0], ctx[0]));
EXPECT_EGL_SUCCESS();
// Potentially wait to be signalled to start.
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0Start));
// Wake up and do next step: Create, detach, and dispatch a compute shader program.
constexpr char kCS[] = R"(#version 310 es
layout(local_size_x=1) in;
void main()
{
})";
GLuint computeProgram = glCreateProgram();
GLuint cs = CompileShader(GL_COMPUTE_SHADER, kCS);
EXPECT_NE(0u, cs);
glAttachShader(computeProgram, cs);
glDeleteShader(cs);
glLinkProgram(computeProgram);
GLint linkStatus;
glGetProgramiv(computeProgram, GL_LINK_STATUS, &linkStatus);
EXPECT_GL_TRUE(linkStatus);
glDetachShader(computeProgram, cs);
EXPECT_GL_NO_ERROR();
glUseProgram(computeProgram);
glDispatchCompute(8, 4, 2);
EXPECT_GL_NO_ERROR();
// Signal the second thread and wait for it to draw and flush.
threadSynchronization.nextStep(Step::Thread0DispatchedCompute);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread1Drew));
// Wake up and do next step: Dispatch the same compute shader again.
glDispatchCompute(8, 4, 2);
// Signal the second thread and wait for it to draw and flush again.
threadSynchronization.nextStep(Step::Thread0DispatchedComputeAgain);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Finish));
// Wake up and do next step: Dispatch the same compute shader again, and force flush the
// underlying command buffer.
glDispatchCompute(8, 4, 2);
glFinish();
// Clean-up and exit this thread.
EXPECT_GL_NO_ERROR();
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_SUCCESS();
});
// This second thread renders. It starts once the other thread does its first nextStep()
std::thread continuingThread = std::thread([&]() {
ThreadSynchronization<Step> threadSynchronization(&currentStep, &mutex, &condVar);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface[1], surface[1], ctx[1]));
EXPECT_EGL_SUCCESS();
// Wait for first thread to create and dispatch a compute shader.
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0DispatchedCompute));
// Wake up and do next step: Create graphics resources, draw, and force flush the
// underlying command buffer.
GLTexture texture;
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
GLRenderbuffer renderbuffer;
GLFramebuffer fbo;
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
constexpr int kRenderbufferSize = 4;
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kRenderbufferSize, kRenderbufferSize);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
renderbuffer);
glBindTexture(GL_TEXTURE_2D, texture);
GLProgram graphicsProgram;
graphicsProgram.makeRaster(essl1_shaders::vs::Texture2D(), essl1_shaders::fs::Texture2D());
ASSERT_TRUE(graphicsProgram.valid());
drawQuad(graphicsProgram.get(), essl1_shaders::PositionAttrib(), 0.5f);
glFinish();
// Signal the first thread and wait for it to dispatch a compute shader again.
threadSynchronization.nextStep(Step::Thread1Drew);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0DispatchedComputeAgain));
// Wake up and do next step: Draw and force flush the underlying command buffer again.
drawQuad(graphicsProgram.get(), essl1_shaders::PositionAttrib(), 0.5f);
glFinish();
// Signal the first thread and wait exit this thread.
threadSynchronization.nextStep(Step::Finish);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_SUCCESS();
});
deletingThread.join();
continuingThread.join();
ASSERT_NE(currentStep, Step::Abort);
// Clean up
for (size_t t = 0; t < kThreadCount; ++t)
{
eglDestroySurface(dpy, surface[t]);
eglDestroyContext(dpy, ctx[t]);
}
}
} // anonymous namespace
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EGLMultiContextTest);
ANGLE_INSTANTIATE_TEST_ES31(EGLMultiContextTest);
//
// Copyright 2016-2021 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.
//
// EGLMultiContextTest.cpp:
// Synchronization help for tests that use multiple threads.
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
namespace
{
// The following class is used by tests that need multiple threads that coordinate their actions
// via an enum of "steps". This enum is the template type E. The enum must have at least the
// following values:
//
// - Finish This value indicates that one thread has finished its last step and is cleaning up.
// The other thread waits for this before it does its last step and cleans up.
// - Abort This value indicates that one thread encountered a GL error and has exited. This
// will cause the other thread (that is waiting for a different step) to also abort.
//
// This class is RAII. It is declared at the top of a thread, and will be deconstructed at the end
// of the thread's outer block. If the thread encounters a GL error, the deconstructor will abort
// the other thread using the E:Abort step.
template <typename E>
class ThreadSynchronization
{
public:
ThreadSynchronization(E *currentStep, std::mutex *mutex, std::condition_variable *condVar)
: mCurrentStep(currentStep), mMutex(mutex), mCondVar(condVar)
{}
~ThreadSynchronization()
{
bool isAborting = false;
{
// If the other thread isn't finished, cause it to abort.
std::unique_lock<std::mutex> lock(*mMutex);
isAborting = *mCurrentStep != E::Finish;
if (isAborting)
{
*mCurrentStep = E::Abort;
}
}
mCondVar->notify_all();
}
// Helper functions to synchronize the threads so that the operations are executed in the
// specific order the test is written for.
bool waitForStep(E waitStep)
{
std::unique_lock<std::mutex> lock(*mMutex);
while (*mCurrentStep != waitStep)
{
// If necessary, abort execution as the other thread has encountered a GL error.
if (*mCurrentStep == E::Abort)
{
return false;
}
mCondVar->wait(lock);
}
return true;
}
void nextStep(E newStep)
{
{
std::unique_lock<std::mutex> lock(*mMutex);
*mCurrentStep = newStep;
}
mCondVar->notify_one();
}
private:
E *mCurrentStep;
std::mutex *mMutex;
std::condition_variable *mCondVar;
};
} // anonymous namespace
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment