Commit 99938eab by David 'Digit' Turner Committed by David Turner

[vulkan]: Implement VkSemaphore using Marl primitives.

Proper implementation of a VkSemaphore using an std::mutex and a marl::ConditionVariable. This should make the implementation compatible with both fibers and threads at the same time. A future CL will add platform-specific implementations to cover external semaphores as well. Which explains why the implementation details are hidden from VkSemaphore.hpp. Bug: b/140421726 Change-Id: I1db55493a41db0eb60ce9181fe864253db09f4f8 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/35933Tested-by: 's avatarDavid Turner <digit@google.com> Reviewed-by: 's avatarBen Clayton <bclayton@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
parent 5be09849
......@@ -113,6 +113,7 @@ swiftshader_shared_library("swiftshader_libvulkan") {
"VkQueue.cpp",
"VkRenderPass.cpp",
"VkSampler.cpp",
"VkSemaphore.cpp",
"VkShaderModule.cpp",
"Vulkan.rc",
"libVulkan.cpp",
......
// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "VkSemaphore.hpp"
#include "marl/conditionvariable.h"
#include <mutex>
namespace vk
{
// An implementation of VkSemaphore based on Marl primitives.
class Semaphore::Impl
{
public:
Impl() = default;
void wait()
{
std::unique_lock<std::mutex> lock(mutex);
condition.wait(lock, [this]{ return this->signaled; });
signaled = false; // Vulkan requires resetting after waiting.
}
void signal()
{
std::unique_lock<std::mutex> lock(mutex);
if (!signaled)
{
signaled = true;
condition.notify_one();
}
}
private:
std::mutex mutex;
marl::ConditionVariable condition;
bool signaled = false;
};
Semaphore::Semaphore(const VkSemaphoreCreateInfo* pCreateInfo, void* mem)
{
impl = new (mem) Impl();
}
void Semaphore::destroy(const VkAllocationCallbacks* pAllocator)
{
impl->~Impl();
vk::deallocate(impl, pAllocator);
}
size_t Semaphore::ComputeRequiredAllocationSize(const VkSemaphoreCreateInfo* pCreateInfo)
{
return sizeof(Semaphore::Impl);
}
void Semaphore::wait()
{
impl->wait();
}
void Semaphore::signal()
{
impl->signal();
}
} // namespace vk
......@@ -23,31 +23,24 @@ namespace vk
class Semaphore : public Object<Semaphore, VkSemaphore>
{
public:
Semaphore(const VkSemaphoreCreateInfo* pCreateInfo, void* mem) {}
Semaphore(const VkSemaphoreCreateInfo* pCreateInfo, void* mem);
void destroy(const VkAllocationCallbacks* pAllocator);
static size_t ComputeRequiredAllocationSize(const VkSemaphoreCreateInfo* pCreateInfo)
{
return 0;
}
static size_t ComputeRequiredAllocationSize(const VkSemaphoreCreateInfo* pCreateInfo);
void wait()
{
// Semaphores are noop for now
}
void wait();
void wait(const VkPipelineStageFlags& flag)
{
// VkPipelineStageFlags is the pipeline stage at which the semaphore wait will occur
// Semaphores are noop for now
// NOTE: not sure what else to do here?
wait();
}
void signal()
{
// Semaphores are noop for now
}
void signal();
private:
class Impl;
Impl* impl = nullptr;
};
static inline Semaphore* Cast(VkSemaphore object)
......
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