Hash :
02fa7313
Author :
Date :
2020-05-05T17:01:18
Vulkan:Initial worker thread disabled by default Created new CommandProcessor class that can be run as a worker thread. Running CommandProcessor within RendererVk as a worker thread that takes a CommmandBufferHelper (CBH) ptr as the interface and processes that CBH into a primary command buffer. Main thread has a queue of CBH to draw from. After submitting a CBH to the worker, it pulls next CBH from the queue. Worker thread releases CBH back to the main thread queue when done. Synchronization goes two ways: 1. Work submitted to worker thread is managaed with a mutex and condition variable based around the work queue. 2. Available CBH ptrs for the main thread have a mutex and condition variable that manages the CBH queue. The worker thread is disabled by default, and, when enabled, it will currently behave and perform as the non-threaded code. This is because the kNumCommandBuffers const in ContextVk.h is set to 2. With only 2 command buffers, they will be assigned to the inside and outside RenderPass command buffers respectively. Then, as soon as one is submitted, the main thread will stall waiting for it to be completed and put back into the queue mentioned in #2 above. The next step is to move command submission to the worker thread and update the number of command buffers so that processing/submission will occur in parallel with the main thread. Right now there is a race condition issue when attempting to run in parallel because the main thread updates and submits the same primary command buffers that are used in the worker thread, which is in violation of the Vulkan spec. The follow-on CL will fix this issue as the main thread will only touch SecondaryCommandBuffers and the worker thread will be the only thread touching the primary command buffers. Bug: b/154030730 Change-Id: Ib0c518bbd7ca9a3a7e789f4e1f2f7131ddc0509e Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2174719 Commit-Queue: Tobin Ehlis <tobine@google.com> Reviewed-by: Jamie Madill <jmadill@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
//
// Copyright 2020 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.
//
// CommandProcessor.cpp:
// Implements the class methods for CommandProcessor.
//
#include "libANGLE/renderer/vulkan/CommandProcessor.h"
#include "libANGLE/trace.h"
namespace rx
{
CommandProcessor::CommandProcessor() : mWorkerThreadIdle(true) {}
void CommandProcessor::queueCommands(const vk::CommandProcessorTask &commands)
{
ANGLE_TRACE_EVENT0("gpu.angle", "RendererVk::queueCommands");
std::lock_guard<std::mutex> queueLock(mWorkerMutex);
ASSERT(commands.commandBuffer == nullptr || !commands.commandBuffer->empty());
mCommandsQueue.push(commands);
mWorkAvailableCondition.notify_one();
}
angle::Result CommandProcessor::processCommandProcessorTasks()
{
while (true)
{
std::unique_lock<std::mutex> lock(mWorkerMutex);
mWorkerIdleCondition.notify_one();
mWorkerThreadIdle = true;
// Only wake if notified and command queue is not empty
mWorkAvailableCondition.wait(lock, [this] { return !mCommandsQueue.empty(); });
mWorkerThreadIdle = false;
vk::CommandProcessorTask task = mCommandsQueue.front();
mCommandsQueue.pop();
lock.unlock();
// Either both ptrs should be null or non-null
ASSERT((task.commandBuffer != nullptr && task.contextVk != nullptr) ||
(task.commandBuffer == nullptr && task.contextVk == nullptr));
// A work block with null ptrs signals worker thread to exit
if (task.commandBuffer == nullptr && task.contextVk == nullptr)
{
break;
}
ASSERT(!task.commandBuffer->empty());
// TODO: Will need some way to synchronize error reporting between threads
ANGLE_TRY(task.commandBuffer->flushToPrimary(task.contextVk, task.primaryCB));
ASSERT(task.commandBuffer->empty());
task.commandBuffer->releaseToContextQueue(task.contextVk);
}
return angle::Result::Continue;
}
void CommandProcessor::waitForWorkComplete()
{
ANGLE_TRACE_EVENT0("gpu.angle", "CommandProcessor::waitForWorkerThreadIdle");
std::unique_lock<std::mutex> lock(mWorkerMutex);
mWorkerIdleCondition.wait(lock,
[this] { return (mCommandsQueue.empty() && mWorkerThreadIdle); });
// Worker thread is idle and command queue is empty so good to continue
lock.unlock();
}
void CommandProcessor::shutdown(std::thread *commandProcessorThread)
{
waitForWorkComplete();
const vk::CommandProcessorTask endTask = vk::kEndCommandProcessorThread;
queueCommands(endTask);
if (commandProcessorThread->joinable())
{
commandProcessorThread->join();
}
}
} // namespace rx