Commit 89899748 by jchen10 Committed by Commit Bot

ParallelCompile: D3D compute

This parallelizes the compiling and linking for compute shaders on the D3D backend. Bug: chromium:849576 Change-Id: Idd6b418cb9c2448209c15eab2756599f8ff7af4c Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1415725Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org> Commit-Queue: Jie A Chen <jie.a.chen@intel.com>
parent 2889dff6
...@@ -1758,6 +1758,21 @@ class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTa ...@@ -1758,6 +1758,21 @@ class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTa
const gl::State &mState; const gl::State &mState;
}; };
class ProgramD3D::GetComputeExecutableTask : public ProgramD3D::GetExecutableTask
{
public:
GetComputeExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
angle::Result run() override
{
mProgram->updateCachedImage2DBindLayoutFromComputeShader();
ShaderExecutableD3D *computeExecutable = nullptr;
ANGLE_TRY(mProgram->getComputeExecutableForImage2DBindLayout(this, &computeExecutable,
&mInfoLog));
return computeExecutable ? angle::Result::Continue : angle::Result::Incomplete;
}
};
// The LinkEvent implementation for linking a rendering(VS, FS, GS) program. // The LinkEvent implementation for linking a rendering(VS, FS, GS) program.
class ProgramD3D::GraphicsProgramLinkEvent final : public LinkEvent class ProgramD3D::GraphicsProgramLinkEvent final : public LinkEvent
{ {
...@@ -1793,9 +1808,9 @@ class ProgramD3D::GraphicsProgramLinkEvent final : public LinkEvent ...@@ -1793,9 +1808,9 @@ class ProgramD3D::GraphicsProgramLinkEvent final : public LinkEvent
ANGLE_TRY(checkTask(context, mPixelTask.get())); ANGLE_TRY(checkTask(context, mPixelTask.get()));
ANGLE_TRY(checkTask(context, mGeometryTask.get())); ANGLE_TRY(checkTask(context, mGeometryTask.get()));
if (mVertexTask.get()->getResult() == angle::Result::Incomplete || if (mVertexTask->getResult() == angle::Result::Incomplete ||
mPixelTask.get()->getResult() == angle::Result::Incomplete || mPixelTask->getResult() == angle::Result::Incomplete ||
mGeometryTask.get()->getResult() == angle::Result::Incomplete) mGeometryTask->getResult() == angle::Result::Incomplete)
{ {
return angle::Result::Incomplete; return angle::Result::Incomplete;
} }
...@@ -1873,6 +1888,36 @@ class ProgramD3D::GraphicsProgramLinkEvent final : public LinkEvent ...@@ -1873,6 +1888,36 @@ class ProgramD3D::GraphicsProgramLinkEvent final : public LinkEvent
const ShaderD3D *mFragmentShader; const ShaderD3D *mFragmentShader;
}; };
// The LinkEvent implementation for linking a computing program.
class ProgramD3D::ComputeProgramLinkEvent final : public LinkEvent
{
public:
ComputeProgramLinkEvent(gl::InfoLog &infoLog,
std::shared_ptr<ProgramD3D::GetComputeExecutableTask> computeTask,
std::shared_ptr<WaitableEvent> event)
: mInfoLog(infoLog), mComputeTask(computeTask), mWaitEvent(event)
{}
bool isLinking() override { return !mWaitEvent->isReady(); }
angle::Result wait(const gl::Context *context) override
{
mWaitEvent->wait();
angle::Result result = mComputeTask->getResult();
if (result != angle::Result::Continue)
{
mInfoLog << "Failed to create D3D compute shader.";
}
return result;
}
private:
gl::InfoLog &mInfoLog;
std::shared_ptr<ProgramD3D::GetComputeExecutableTask> mComputeTask;
std::shared_ptr<WaitableEvent> mWaitEvent;
};
std::unique_ptr<LinkEvent> ProgramD3D::compileProgramExecutables(const gl::Context *context, std::unique_ptr<LinkEvent> ProgramD3D::compileProgramExecutables(const gl::Context *context,
gl::InfoLog &infoLog) gl::InfoLog &infoLog)
{ {
...@@ -1897,6 +1942,21 @@ std::unique_ptr<LinkEvent> ProgramD3D::compileProgramExecutables(const gl::Conte ...@@ -1897,6 +1942,21 @@ std::unique_ptr<LinkEvent> ProgramD3D::compileProgramExecutables(const gl::Conte
vertexShaderD3D, fragmentShaderD3D); vertexShaderD3D, fragmentShaderD3D);
} }
std::unique_ptr<LinkEvent> ProgramD3D::compileComputeExecutable(const gl::Context *context,
gl::InfoLog &infoLog)
{
// Ensure the compiler is initialized to avoid race conditions.
angle::Result result = mRenderer->ensureHLSLCompilerInitialized(GetImplAs<ContextD3D>(context));
if (result != angle::Result::Continue)
{
return std::make_unique<LinkEventDone>(result);
}
auto computeTask = std::make_shared<GetComputeExecutableTask>(this);
return std::make_unique<ComputeProgramLinkEvent>(
infoLog, computeTask,
WorkerThreadPool::PostWorkerTask(context->getWorkerThreadPool(), computeTask));
}
angle::Result ProgramD3D::getComputeExecutableForImage2DBindLayout( angle::Result ProgramD3D::getComputeExecutableForImage2DBindLayout(
d3d::Context *context, d3d::Context *context,
ShaderExecutableD3D **outExecutable, ShaderExecutableD3D **outExecutable,
...@@ -1939,19 +1999,6 @@ angle::Result ProgramD3D::getComputeExecutableForImage2DBindLayout( ...@@ -1939,19 +1999,6 @@ angle::Result ProgramD3D::getComputeExecutableForImage2DBindLayout(
return angle::Result::Continue; return angle::Result::Continue;
} }
angle::Result ProgramD3D::compileComputeExecutable(d3d::Context *context, gl::InfoLog &infoLog)
{
// Ensure the compiler is initialized to avoid race conditions.
ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized(context));
updateCachedImage2DBindLayoutFromComputeShader();
ShaderExecutableD3D *computeExecutable = nullptr;
ANGLE_TRY(getComputeExecutableForImage2DBindLayout(context, &computeExecutable, &infoLog));
return computeExecutable ? angle::Result::Continue : angle::Result::Incomplete;
}
std::unique_ptr<LinkEvent> ProgramD3D::link(const gl::Context *context, std::unique_ptr<LinkEvent> ProgramD3D::link(const gl::Context *context,
const gl::ProgramLinkedResources &resources, const gl::ProgramLinkedResources &resources,
gl::InfoLog &infoLog) gl::InfoLog &infoLog)
...@@ -1982,12 +2029,7 @@ std::unique_ptr<LinkEvent> ProgramD3D::link(const gl::Context *context, ...@@ -1982,12 +2029,7 @@ std::unique_ptr<LinkEvent> ProgramD3D::link(const gl::Context *context,
defineUniformsAndAssignRegisters(); defineUniformsAndAssignRegisters();
angle::Result result = compileComputeExecutable(GetImplAs<ContextD3D>(context), infoLog); return compileComputeExecutable(context, infoLog);
if (result != angle::Result::Continue)
{
infoLog << "Failed to create D3D compute shader.";
}
return std::make_unique<LinkEventDone>(result);
} }
else else
{ {
......
...@@ -333,7 +333,9 @@ class ProgramD3D : public ProgramImpl ...@@ -333,7 +333,9 @@ class ProgramD3D : public ProgramImpl
class GetVertexExecutableTask; class GetVertexExecutableTask;
class GetPixelExecutableTask; class GetPixelExecutableTask;
class GetGeometryExecutableTask; class GetGeometryExecutableTask;
class GetComputeExecutableTask;
class GraphicsProgramLinkEvent; class GraphicsProgramLinkEvent;
class ComputeProgramLinkEvent;
class LoadBinaryTask; class LoadBinaryTask;
class LoadBinaryLinkEvent; class LoadBinaryLinkEvent;
...@@ -475,7 +477,8 @@ class ProgramD3D : public ProgramImpl ...@@ -475,7 +477,8 @@ class ProgramD3D : public ProgramImpl
std::unique_ptr<LinkEvent> compileProgramExecutables(const gl::Context *context, std::unique_ptr<LinkEvent> compileProgramExecutables(const gl::Context *context,
gl::InfoLog &infoLog); gl::InfoLog &infoLog);
angle::Result compileComputeExecutable(d3d::Context *context, gl::InfoLog &infoLog); std::unique_ptr<LinkEvent> compileComputeExecutable(const gl::Context *context,
gl::InfoLog &infoLog);
angle::Result loadBinaryShaderExecutables(const gl::Context *context, angle::Result loadBinaryShaderExecutables(const gl::Context *context,
gl::BinaryInputStream *stream, gl::BinaryInputStream *stream,
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
// ParallelShaderCompileTest.cpp : Tests of the GL_KHR_parallel_shader_compile extension. // ParallelShaderCompileTest.cpp : Tests of the GL_KHR_parallel_shader_compile extension.
#include "test_utils/ANGLETest.h" #include "test_utils/ANGLETest.h"
#include "test_utils/gl_raii.h"
#include "util/random_utils.h" #include "util/random_utils.h"
...@@ -15,6 +16,14 @@ using namespace angle; ...@@ -15,6 +16,14 @@ using namespace angle;
namespace namespace
{ {
namespace
{
constexpr int kTaskCount = 32;
constexpr unsigned int kPollInterval = 100;
} // anonymous namespace
class ParallelShaderCompileTest : public ANGLETest class ParallelShaderCompileTest : public ANGLETest
{ {
protected: protected:
...@@ -46,12 +55,144 @@ class ParallelShaderCompileTest : public ANGLETest ...@@ -46,12 +55,144 @@ class ParallelShaderCompileTest : public ANGLETest
return true; return true;
} }
class ClearColorWithDraw class Task
{
public:
Task(int id) : mID(id) {}
virtual ~Task() {}
virtual bool compile() = 0;
virtual bool isCompileCompleted() = 0;
virtual bool link() = 0;
virtual void runAndVerify(ParallelShaderCompileTest *test) = 0;
bool isLinkCompleted()
{
GLint status;
glGetProgramiv(mProgram, GL_COMPLETION_STATUS_KHR, &status);
return (status == GL_TRUE);
}
protected:
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();
}
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;
}
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: public:
ClearColorWithDraw(GLubyte color) : mColor(color, color, color, 255) {} TaskRunner() {}
~TaskRunner() {}
bool compile() void run(ParallelShaderCompileTest *test)
{
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();
ASSERT_TRUE(isLinking);
linkTasks.push_back(std::move(task));
compileTasks.erase(compileTasks.begin() + i);
continue;
}
++i;
}
Sleep(kPollInterval);
}
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;
}
++i;
}
Sleep(kPollInterval);
}
}
};
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 = mVertexShader =
CompileShader(GL_VERTEX_SHADER, insertRandomString(essl1_shaders::vs::Simple())); CompileShader(GL_VERTEX_SHADER, insertRandomString(essl1_shaders::vs::Simple()));
...@@ -60,7 +201,7 @@ class ParallelShaderCompileTest : public ANGLETest ...@@ -60,7 +201,7 @@ class ParallelShaderCompileTest : public ANGLETest
return (mVertexShader != 0 && mFragmentShader != 0); return (mVertexShader != 0 && mFragmentShader != 0);
} }
bool isCompileCompleted() bool isCompileCompleted() override
{ {
GLint status; GLint status;
glGetShaderiv(mVertexShader, GL_COMPLETION_STATUS_KHR, &status); glGetShaderiv(mVertexShader, GL_COMPLETION_STATUS_KHR, &status);
...@@ -72,7 +213,7 @@ class ParallelShaderCompileTest : public ANGLETest ...@@ -72,7 +213,7 @@ class ParallelShaderCompileTest : public ANGLETest
return false; return false;
} }
bool link() bool link() override
{ {
mProgram = 0; mProgram = 0;
if (checkShader(mVertexShader) && checkShader(mFragmentShader)) if (checkShader(mVertexShader) && checkShader(mFragmentShader))
...@@ -87,14 +228,7 @@ class ParallelShaderCompileTest : public ANGLETest ...@@ -87,14 +228,7 @@ class ParallelShaderCompileTest : public ANGLETest
return (mProgram != 0); return (mProgram != 0);
} }
bool isLinkCompleted() void runAndVerify(ParallelShaderCompileTest *test) override
{
GLint status;
glGetProgramiv(mProgram, GL_COMPLETION_STATUS_KHR, &status);
return (status == GL_TRUE);
}
void drawAndVerify(ParallelShaderCompileTest *test)
{ {
glClearColor(0, 0, 0, 0); glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
...@@ -114,57 +248,109 @@ class ParallelShaderCompileTest : public ANGLETest ...@@ -114,57 +248,109 @@ class ParallelShaderCompileTest : public ANGLETest
} }
private: private:
std::string insertRandomString(const std::string &source) GLColor mColor;
GLuint mVertexShader;
GLuint mFragmentShader;
};
class ImageLoadStore : public Task
{
public:
ImageLoadStore(int taskID) : Task(taskID) {}
~ImageLoadStore() {}
bool compile() override
{ {
RNG rng; const char kCSSource[] = R"(#version 310 es
std::ostringstream ostream; layout(local_size_x=1, local_size_y=1, local_size_z=1) in;
ostream << "// Random string to fool program cache: " << rng.randomInt() << "\n" layout(r32ui, binding = 0) readonly uniform highp uimage2D uImage_1;
<< source; layout(r32ui, binding = 1) writeonly uniform highp uimage2D uImage_2;
return ostream.str(); 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;
} }
GLuint CompileShader(GLenum type, const std::string &source) bool isCompileCompleted() override
{ {
GLuint shader = glCreateShader(type); GLint status;
glGetShaderiv(mShader, GL_COMPLETION_STATUS_KHR, &status);
return status == GL_TRUE;
}
const char *sourceArray[1] = {source.c_str()}; bool link() override
glShaderSource(shader, 1, sourceArray, nullptr); {
glCompileShader(shader); mProgram = 0;
return shader; if (checkShader(mShader))
{
mProgram = glCreateProgram();
glAttachShader(mProgram, mShader);
glLinkProgram(mProgram);
}
glDeleteShader(mShader);
return mProgram != 0;
} }
bool checkShader(GLuint shader) void runAndVerify(ParallelShaderCompileTest *test) override
{ {
GLint compileResult; // Taken from ComputeShaderTest.StoreImageThenLoad.
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult); 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();
if (compileResult == 0) glUseProgram(mProgram);
{
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 glBindImageTexture(0, texture[0], 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32UI);
// empty string. glBindImageTexture(1, texture[1], 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32UI);
if (infoLogLength > 1)
{ glDispatchCompute(1, 1, 1);
std::vector<GLchar> infoLog(infoLogLength); glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
glGetShaderInfoLog(shader, static_cast<GLsizei>(infoLog.size()), nullptr, EXPECT_GL_NO_ERROR();
&infoLog[0]);
std::cerr << "shader compilation failed: " << &infoLog[0]; 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);
else
{ glDispatchCompute(1, 1, 1);
std::cerr << "shader compilation failed. <Empty log message>"; glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
} EXPECT_GL_NO_ERROR();
std::cerr << std::endl;
} GLuint outputValue;
return (compileResult == GL_TRUE); 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();
} }
GLColor mColor; private:
GLuint mVertexShader; GLuint mShader;
GLuint mFragmentShader;
GLuint mProgram;
}; };
}; };
...@@ -186,55 +372,20 @@ TEST_P(ParallelShaderCompileTest, LinkAndDrawManyPrograms) ...@@ -186,55 +372,20 @@ TEST_P(ParallelShaderCompileTest, LinkAndDrawManyPrograms)
{ {
ANGLE_SKIP_TEST_IF(!ensureParallelShaderCompileExtensionAvailable()); ANGLE_SKIP_TEST_IF(!ensureParallelShaderCompileExtensionAvailable());
std::vector<std::unique_ptr<ClearColorWithDraw>> compileTasks; TaskRunner<ClearColorWithDraw> runner;
constexpr int kTaskCount = 32; runner.run(this);
for (int i = 0; i < kTaskCount; ++i) }
{
std::unique_ptr<ClearColorWithDraw> task(
new ClearColorWithDraw(static_cast<GLubyte>(i * 255 / kTaskCount)));
bool isCompiling = task->compile();
ASSERT_TRUE(isCompiling);
compileTasks.push_back(std::move(task));
}
constexpr unsigned int kPollInterval = 100;
std::vector<std::unique_ptr<ClearColorWithDraw>> linkTasks;
while (!compileTasks.empty())
{
for (unsigned int i = 0; i < compileTasks.size();)
{
auto &task = compileTasks[i];
if (task->isCompileCompleted()) class ParallelShaderCompileTestES31 : public ParallelShaderCompileTest
{ {};
bool isLinking = task->link();
ASSERT_TRUE(isLinking);
linkTasks.push_back(std::move(task));
compileTasks.erase(compileTasks.begin() + i);
continue;
}
++i;
}
Sleep(kPollInterval);
}
while (!linkTasks.empty()) // Test to compile and link many computing programs in parallel.
{ TEST_P(ParallelShaderCompileTestES31, LinkAndDispatchManyPrograms)
for (unsigned int i = 0; i < linkTasks.size();) {
{ ANGLE_SKIP_TEST_IF(!ensureParallelShaderCompileExtensionAvailable());
auto &task = linkTasks[i];
if (task->isLinkCompleted()) TaskRunner<ImageLoadStore> runner;
{ runner.run(this);
task->drawAndVerify(this);
linkTasks.erase(linkTasks.begin() + i);
continue;
}
++i;
}
Sleep(kPollInterval);
}
} }
ANGLE_INSTANTIATE_TEST(ParallelShaderCompileTest, ANGLE_INSTANTIATE_TEST(ParallelShaderCompileTest,
...@@ -245,4 +396,6 @@ ANGLE_INSTANTIATE_TEST(ParallelShaderCompileTest, ...@@ -245,4 +396,6 @@ ANGLE_INSTANTIATE_TEST(ParallelShaderCompileTest,
ES2_OPENGLES(), ES2_OPENGLES(),
ES2_VULKAN()); ES2_VULKAN());
ANGLE_INSTANTIATE_TEST(ParallelShaderCompileTestES31, ES31_OPENGL(), ES31_OPENGLES(), ES31_D3D11());
} // namespace } // 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