Fixing some semaphore things

This commit is contained in:
Dynamitos
2025-03-07 21:48:27 +01:00
parent 4d7c5db2c3
commit 913b8391f8
25 changed files with 236 additions and 139 deletions
+6 -6
View File
@@ -45,6 +45,7 @@ void Command::begin() {
void Command::end() {
VK_CHECK(vkEndCommandBuffer(handle));
signalSemaphore->rotateSemaphore();
state = State::End;
}
@@ -111,15 +112,14 @@ void Command::executeCommands(Array<Gfx::OComputeCommand> commands) {
}
void Command::waitForSemaphore(VkPipelineStageFlags flags, PSemaphore semaphore) {
bindResource(semaphore->getCurrentSemaphore());
waitSemaphores.add(semaphore);
waitFlags.add(flags);
// std::cout << "Cmd " << handle << " wait for " << semaphore->getHandle() << std::endl;
}
void Command::checkFence() {
assert(state == State::Submit || !fence->isSignaled());
if (fence->isSignaled()) {
// std::cout << "Cmd " << handle << " was signaled" << std::endl;
vkResetCommandBuffer(handle, VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT);
fence->reset();
for (auto& command : executingComputes) {
@@ -132,8 +132,8 @@ void Command::checkFence() {
pool->cacheCommands(std::move(executingRenders));
for (auto& descriptor : boundResources) {
descriptor->unbind();
// std::cout << "Cmd " << handle << " unbind " << descriptor->getHandle() << std::endl;
}
signalSemaphore->resolveSignal();
boundResources.clear();
graphics->getDestructionManager()->notifyCommandComplete();
state = State::Init;
@@ -561,11 +561,12 @@ void CommandPool::submitCommands(PSemaphore signalSemaphore) {
assert(command->state == Command::State::Begin); // Not in a renderpass
command->end();
Array<VkSemaphore> semaphores = {command->signalSemaphore->getHandle()};
command->signalSemaphore->encodeSignal();
if (signalSemaphore != nullptr) {
semaphores.add(signalSemaphore->getHandle());
signalSemaphore->encodeSignal();
}
queue->submitCommandBuffer(command, semaphores);
// std::cout << "Cmd " << command->getHandle() << " signalling " << command->signalSemaphore->getHandle() << std::endl;
PSemaphore waitSemaphore = command->signalSemaphore;
for (uint32 i = 0; i < allocatedBuffers.size(); ++i) {
@@ -587,8 +588,7 @@ void CommandPool::submitCommands(PSemaphore signalSemaphore) {
}
void CommandPool::refreshCommands() {
for (uint32 i = 0; i < allocatedBuffers.size(); ++i)
{
for (uint32 i = 0; i < allocatedBuffers.size(); ++i) {
allocatedBuffers[i]->checkFence();
}
}
+58 -25
View File
@@ -53,6 +53,7 @@ void DescriptorLayout::create() {
} else {
mappings[gfxBinding.name] = {
.binding = (uint32)bindings.size(),
.type = cast(gfxBinding.descriptorType),
};
bindings.add({
.binding = (uint32)bindings.size(),
@@ -205,11 +206,11 @@ void DescriptorSet::updateConstants(const std::string& name, uint32 offset, void
void DescriptorSet::updateBuffer(const std::string& name, uint32 index, Gfx::PShaderBuffer shaderBuffer) {
PShaderBuffer vulkanBuffer = shaderBuffer.cast<ShaderBuffer>();
uint32 binding = owner->getLayout()->mappings[name].binding;
const auto& map = owner->getLayout()->mappings[name];
uint32 binding = map.binding;
if (boundResources[binding][index] == vulkanBuffer->getAlloc() || vulkanBuffer->getAlloc() == nullptr) {
return;
}
bufferInfos.add(VkDescriptorBufferInfo{
.buffer = vulkanBuffer->getHandle(),
.offset = 0,
@@ -222,7 +223,7 @@ void DescriptorSet::updateBuffer(const std::string& name, uint32 index, Gfx::PSh
.dstBinding = binding,
.dstArrayElement = index,
.descriptorCount = 1,
.descriptorType = cast(layout->getBindings()[binding].descriptorType),
.descriptorType = map.type,
.pBufferInfo = &bufferInfos.back(),
});
@@ -231,7 +232,8 @@ void DescriptorSet::updateBuffer(const std::string& name, uint32 index, Gfx::PSh
void DescriptorSet::updateBuffer(const std::string& name, uint32 index, Gfx::PVertexBuffer indexBuffer) {
PVertexBuffer vulkanBuffer = indexBuffer.cast<VertexBuffer>();
uint32 binding = owner->getLayout()->mappings[name].binding;
const auto& map = owner->getLayout()->mappings[name];
uint32 binding = map.binding;
if (boundResources[binding][index] == vulkanBuffer->getAlloc() || vulkanBuffer->getAlloc() == nullptr) {
return;
}
@@ -248,7 +250,7 @@ void DescriptorSet::updateBuffer(const std::string& name, uint32 index, Gfx::PVe
.dstBinding = binding,
.dstArrayElement = index,
.descriptorCount = 1,
.descriptorType = cast(layout->getBindings()[binding].descriptorType),
.descriptorType = map.type,
.pBufferInfo = &bufferInfos.back(),
});
@@ -257,7 +259,8 @@ void DescriptorSet::updateBuffer(const std::string& name, uint32 index, Gfx::PVe
void DescriptorSet::updateBuffer(const std::string& name, uint32 index, Gfx::PIndexBuffer indexBuffer) {
PIndexBuffer vulkanBuffer = indexBuffer.cast<IndexBuffer>();
uint32 binding = owner->getLayout()->mappings[name].binding;
const auto& map = owner->getLayout()->mappings[name];
uint32 binding = map.binding;
if (boundResources[binding][index] == vulkanBuffer->getAlloc() || vulkanBuffer->getAlloc() == nullptr) {
return;
}
@@ -274,7 +277,7 @@ void DescriptorSet::updateBuffer(const std::string& name, uint32 index, Gfx::PIn
.dstBinding = binding,
.dstArrayElement = index,
.descriptorCount = 1,
.descriptorType = cast(layout->getBindings()[binding].descriptorType),
.descriptorType = map.type,
.pBufferInfo = &bufferInfos.back(),
});
@@ -283,7 +286,8 @@ void DescriptorSet::updateBuffer(const std::string& name, uint32 index, Gfx::PIn
void DescriptorSet::updateSampler(const std::string& name, uint32 index, Gfx::PSampler samplerState) {
PSampler vulkanSampler = samplerState.cast<Sampler>();
uint32 binding = owner->getLayout()->mappings[name].binding;
const auto& map = owner->getLayout()->mappings[name];
uint32 binding = map.binding;
if (boundResources[binding][index] == vulkanSampler->getHandle()) {
return;
}
@@ -301,7 +305,7 @@ void DescriptorSet::updateSampler(const std::string& name, uint32 index, Gfx::PS
.dstBinding = binding,
.dstArrayElement = index,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER,
.descriptorType = map.type,
.pImageInfo = &imageInfos.back(),
});
@@ -310,7 +314,8 @@ void DescriptorSet::updateSampler(const std::string& name, uint32 index, Gfx::PS
void DescriptorSet::updateTexture(const std::string& name, uint32 index, Gfx::PTexture2D texture) {
TextureBase* vulkanTexture = texture.cast<TextureBase>().getHandle();
uint32 binding = owner->getLayout()->mappings[name].binding;
const auto& map = owner->getLayout()->mappings[name];
uint32 binding = map.binding;
if (boundResources[binding][index] == vulkanTexture->getHandle()) {
return;
}
@@ -328,7 +333,7 @@ void DescriptorSet::updateTexture(const std::string& name, uint32 index, Gfx::PT
.dstBinding = binding,
.dstArrayElement = index,
.descriptorCount = 1,
.descriptorType = cast(layout->getBindings()[binding].descriptorType),
.descriptorType = map.type,
.pImageInfo = &imageInfos.back(),
});
@@ -337,7 +342,8 @@ void DescriptorSet::updateTexture(const std::string& name, uint32 index, Gfx::PT
void DescriptorSet::updateTexture(const std::string& name, uint32 index, Gfx::PTexture3D texture) {
TextureBase* vulkanTexture = texture.cast<TextureBase>().getHandle();
uint32 binding = owner->getLayout()->mappings[name].binding;
const auto& map = owner->getLayout()->mappings[name];
uint32 binding = map.binding;
if (boundResources[binding][index] == vulkanTexture->getHandle()) {
return;
}
@@ -355,7 +361,7 @@ void DescriptorSet::updateTexture(const std::string& name, uint32 index, Gfx::PT
.dstBinding = binding,
.dstArrayElement = index,
.descriptorCount = 1,
.descriptorType = cast(layout->getBindings()[binding].descriptorType),
.descriptorType = map.type,
.pImageInfo = &imageInfos.back(),
});
@@ -364,7 +370,8 @@ void DescriptorSet::updateTexture(const std::string& name, uint32 index, Gfx::PT
void DescriptorSet::updateTexture(const std::string& name, uint32 index, Gfx::PTextureCube texture) {
TextureBase* vulkanTexture = texture.cast<TextureBase>().getHandle();
uint32 binding = owner->getLayout()->mappings[name].binding;
const auto& map = owner->getLayout()->mappings[name];
uint32 binding = map.binding;
if (boundResources[binding][index] == vulkanTexture->getHandle()) {
return;
}
@@ -382,7 +389,7 @@ void DescriptorSet::updateTexture(const std::string& name, uint32 index, Gfx::PT
.dstBinding = binding,
.dstArrayElement = index,
.descriptorCount = 1,
.descriptorType = cast(layout->getBindings()[binding].descriptorType),
.descriptorType = map.type,
.pImageInfo = &imageInfos.back(),
});
@@ -410,6 +417,42 @@ void DescriptorSet::updateAccelerationStructure(const std::string& name, uint32
}
void DescriptorSet::writeChanges() {
if (constantData.size() > 0) {
if (constantsBuffer != nullptr)
{
graphics->getDestructionManager()->queueResourceForDestruction(std::move(constantsBuffer));
}
constantsBuffer = new BufferAllocation(graphics, owner->getLayout()->getName(),
VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.size = constantData.size(),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
},
VmaAllocationCreateInfo{
.usage = VMA_MEMORY_USAGE_AUTO,
},
Gfx::QueueType::GRAPHICS);
constantsBuffer->updateContents(0, constantData.size(), constantData.data());
constantsBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_UNIFORM_READ_BIT, Gfx::SE_PIPELINE_STAGE_ALL_COMMANDS_BIT);
bufferInfos.add(VkDescriptorBufferInfo{
.buffer = constantsBuffer->buffer,
.offset = 0,
.range = constantsBuffer->size,
});
writeDescriptors.add(VkWriteDescriptorSet{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
.dstSet = setHandle,
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.pBufferInfo = &bufferInfos.back(),
});
}
if (writeDescriptors.size() > 0) {
if (isCurrentlyBound()) {
std::cout << "Descriptor currently bound, allocate a new one instead" << std::endl;
@@ -420,16 +463,6 @@ void DescriptorSet::writeChanges() {
imageInfos.clear();
bufferInfos.clear();
}
constantsBuffer = graphics->createUniformBuffer(UniformBufferCreateInfo{
.sourceData =
{
.size = constantData.size(),
.data = constantData.data(),
},
.name = owner->getLayout()->getName(),
});
constantsBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_UNIFORM_READ_BIT, Gfx::SE_PIPELINE_STAGE_ALL_COMMANDS_BIT);
}
PipelineLayout::PipelineLayout(PGraphics graphics, const std::string& name, Gfx::PPipelineLayout baseLayout)
+4 -4
View File
@@ -8,11 +8,11 @@
namespace Seele {
namespace Vulkan {
DECLARE_REF(Graphics)
struct DescriptorMapping
{
struct DescriptorMapping {
uint32 binding;
uint32 constantOffset;
uint32 constantSize;
VkDescriptorType type;
};
class DescriptorLayout : public Gfx::DescriptorLayout {
public:
@@ -23,7 +23,7 @@ class DescriptorLayout : public Gfx::DescriptorLayout {
private:
PGraphics graphics;
uint32 constantsSize;
uint32 constantsSize = 0;
VkShaderStageFlags constantsStages;
Array<VkDescriptorSetLayoutBinding> bindings;
Map<std::string, DescriptorMapping> mappings;
@@ -73,7 +73,7 @@ class DescriptorSet : public Gfx::DescriptorSet, public CommandBoundResource {
private:
std::vector<uint8> constantData;
Gfx::OUniformBuffer constantsBuffer;
OBufferAllocation constantsBuffer;
VkShaderStageFlags constantsStageFlags;
List<VkDescriptorImageInfo> imageInfos;
List<VkDescriptorBufferInfo> bufferInfos;
+9 -1
View File
@@ -142,7 +142,9 @@ Graphics::~Graphics() {
void Graphics::init(GraphicsInitializer initInfo) {
initInstance(initInfo);
//setupDebugCallback();
#ifdef ENABLE_VALIDATION
setupDebugCallback();
#endif
pickPhysicalDevice();
createDevice(initInfo);
VmaAllocatorCreateInfo createInfo = {
@@ -197,6 +199,12 @@ void Graphics::waitDeviceIdle() {
getGraphicsCommands()->refreshCommands();
}
void Graphics::executeCommands(Gfx::ORenderCommand commands) {
Array<Gfx::ORenderCommand> commandArray;
commandArray.add(std::move(commands));
getGraphicsCommands()->getCommands()->executeCommands(std::move(commandArray));
}
void Graphics::executeCommands(Array<Gfx::ORenderCommand> commands) {
getGraphicsCommands()->getCommands()->executeCommands(std::move(commands));
}
+1
View File
@@ -40,6 +40,7 @@ class Graphics : public Gfx::Graphics {
virtual void endRenderPass() override;
virtual void waitDeviceIdle() override;
virtual void executeCommands(Gfx::ORenderCommand commands) override;
virtual void executeCommands(Array<Gfx::ORenderCommand> commands) override;
virtual void executeCommands(Gfx::OComputeCommand commands) override;
virtual void executeCommands(Array<Gfx::OComputeCommand> commands) override;
+2 -1
View File
@@ -25,6 +25,8 @@ void Queue::submitCommandBuffer(PCommand command, const Array<VkSemaphore>& sign
VkCommandBuffer cmdHandle = command->handle;
Array<VkSemaphore> waitSemaphores;
// wait semaphores get bound to the cmd when they are added
// and unbound when the command completes
for (PSemaphore semaphore : command->waitSemaphores) {
waitSemaphores.add(semaphore->getHandle());
}
@@ -40,7 +42,6 @@ void Queue::submitCommandBuffer(PCommand command, const Array<VkSemaphore>& sign
.signalSemaphoreCount = static_cast<uint32>(signalSemaphores.size()),
.pSignalSemaphores = signalSemaphores.data(),
};
VK_CHECK(vkQueueSubmit(queue, 1, &submitInfo, command->fence->getHandle()));
command->fence->submit();
command->state = Command::State::Submit;
+20 -2
View File
@@ -7,7 +7,7 @@
using namespace Seele;
using namespace Seele::Vulkan;
Semaphore::Semaphore(PGraphics graphics) : graphics(graphics) {
SemaphoreHandle::SemaphoreHandle(PGraphics graphics, const std::string& name) : CommandBoundResource(graphics, name) {
VkSemaphoreCreateInfo info = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.pNext = nullptr,
@@ -16,8 +16,26 @@ Semaphore::Semaphore(PGraphics graphics) : graphics(graphics) {
VK_CHECK(vkCreateSemaphore(graphics->getDevice(), &info, nullptr, &handle));
}
SemaphoreHandle::~SemaphoreHandle() { vkDestroySemaphore(graphics->getDevice(), handle, nullptr); }
Semaphore::Semaphore(PGraphics graphics) : graphics(graphics) {}
Semaphore::~Semaphore() {
// graphics->getDestructionManager()->queueSemaphore(graphics->getGraphicsCommands()->getCommands(), handle);
for (auto& h : handles) {
graphics->getDestructionManager()->queueResourceForDestruction(std::move(h));
}
}
void Semaphore::rotateSemaphore() {
for (uint32 i = 0; i < handles.size(); ++i) {
if (handles[i]->isCurrentlyBound()) {
continue;
}
currentHandle = i;
return;
}
currentHandle = handles.size();
handles.add(new SemaphoreHandle(graphics, "Semaphore"));
}
Fence::Fence(PGraphics graphics) : graphics(graphics), status(Status::Ready) {
+50 -23
View File
@@ -4,21 +4,67 @@
#include <vk_mem_alloc.h>
#include <vulkan/vulkan.h>
namespace Seele {
namespace Vulkan {
DECLARE_REF(DescriptorPool)
DECLARE_REF(CommandPool)
DECLARE_REF(Command)
DECLARE_REF(Graphics)
class Semaphore {
class CommandBoundResource {
public:
Semaphore(PGraphics graphics);
virtual ~Semaphore();
CommandBoundResource(PGraphics graphics, const std::string& name) : graphics(graphics), name(name) {}
virtual ~CommandBoundResource() {
if (isCurrentlyBound())
abort();
}
constexpr bool isCurrentlyBound() const { return bindCount > 0; }
constexpr void bind() { bindCount++; }
constexpr void unbind() { bindCount--; }
protected:
PGraphics graphics;
std::string name;
uint64 bindCount = 0;
};
DEFINE_REF(CommandBoundResource)
class SemaphoreHandle : public CommandBoundResource {
public:
SemaphoreHandle(PGraphics graphics, const std::string& name);
virtual ~SemaphoreHandle();
constexpr VkSemaphore getHandle() const { return handle; }
private:
VkSemaphore handle;
};
DEFINE_REF(SemaphoreHandle)
class Semaphore {
public:
Semaphore(PGraphics graphics);
virtual ~Semaphore();
// call when you need a new semaphore
void rotateSemaphore();
// call when the semaphore is to signal something, for example after using it in vkAcquireImage
void encodeSignal() {
if (handles.size() == 0)
return;
handles[currentHandle]->bind();
}
// call when the semaphore has been signalled
void resolveSignal() {
if (handles.size() == 0)
return;
handles[currentHandle]->unbind();
}
constexpr VkSemaphore getHandle() const { return handles[currentHandle]->getHandle(); }
PSemaphoreHandle getCurrentSemaphore() const { return handles[currentHandle]; }
private:
Array<OSemaphoreHandle> handles;
uint32 currentHandle = 0;
PGraphics graphics;
};
DEFINE_REF(Semaphore)
@@ -45,7 +91,6 @@ class Fence {
VkFence fence;
};
DEFINE_REF(Fence)
DECLARE_REF(CommandBoundResource)
class DestructionManager {
public:
DestructionManager(PGraphics graphics);
@@ -59,24 +104,6 @@ class DestructionManager {
};
DEFINE_REF(DestructionManager)
class CommandBoundResource {
public:
CommandBoundResource(PGraphics graphics, const std::string& name) : graphics(graphics), name(name) {}
virtual ~CommandBoundResource() {
if (isCurrentlyBound())
abort();
}
constexpr bool isCurrentlyBound() const { return bindCount > 0; }
constexpr void bind() { bindCount++; }
constexpr void unbind() { bindCount--; }
protected:
PGraphics graphics;
std::string name;
uint64 bindCount = 0;
};
DEFINE_REF(CommandBoundResource)
class SamplerHandle : public CommandBoundResource {
public:
SamplerHandle(PGraphics graphics, VkSamplerCreateInfo createInfo);
+8
View File
@@ -59,6 +59,7 @@ Window::Window(PGraphics graphics, const WindowCreateInfo& createInfo)
: graphics(graphics), preferences(createInfo), instance(graphics->getInstance()), swapchain(VK_NULL_HANDLE) {
glfwGetMonitorContentScale(glfwGetPrimaryMonitor(), &contentScaleX, &contentScaleY);
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
GLFWwindow* handle = glfwCreateWindow(createInfo.width / contentScaleX, createInfo.height / contentScaleY, createInfo.title, nullptr, nullptr);
windowHandle = handle;
glfwSetWindowUserPointer(handle, this);
@@ -92,11 +93,16 @@ Window::~Window() {
void Window::pollInput() { glfwPollEvents(); }
void Window::show() { glfwShowWindow(static_cast<GLFWwindow*>(windowHandle)); }
void Window::beginFrame() {
imageAvailableFences[currentSemaphoreIndex]->reset();
imageAvailableSemaphores[currentSemaphoreIndex]->resolveSignal();
imageAvailableSemaphores[currentSemaphoreIndex]->rotateSemaphore();
VK_CHECK(vkAcquireNextImageKHR(graphics->getDevice(), swapchain, std::numeric_limits<uint64>::max(),
imageAvailableSemaphores[currentSemaphoreIndex]->getHandle(),
imageAvailableFences[currentSemaphoreIndex]->getHandle(), &currentImageIndex));
imageAvailableSemaphores[currentSemaphoreIndex]->encodeSignal();
imageAvailableFences[currentSemaphoreIndex]->submit();
graphics->getGraphicsCommands()->getCommands()->waitForSemaphore(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
imageAvailableSemaphores[currentSemaphoreIndex]);
@@ -111,6 +117,7 @@ void Window::endFrame() {
swapChainTextures[currentImageIndex]->changeLayout(Gfx::SE_IMAGE_LAYOUT_PRESENT_SRC_KHR, Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, Gfx::SE_ACCESS_MEMORY_READ_BIT,
Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
renderingDoneSemaphores[currentSemaphoreIndex]->rotateSemaphore();
graphics->getGraphicsCommands()->submitCommands(renderingDoneSemaphores[currentSemaphoreIndex]);
VkSemaphore renderDoneHandle = renderingDoneSemaphores[currentSemaphoreIndex]->getHandle();
VkPresentInfoKHR presentInfo = {
@@ -130,6 +137,7 @@ void Window::endFrame() {
} else {
VK_CHECK(r);
}
renderingDoneSemaphores[currentSemaphoreIndex]->resolveSignal();
currentSemaphoreIndex = (currentSemaphoreIndex + 1) % Gfx::numFramesBuffered;
currentFrameIndex = currentSemaphoreIndex;
// graphics->waitDeviceIdle();
+1
View File
@@ -12,6 +12,7 @@ class Window : public Gfx::Window {
Window(PGraphics graphics, const WindowCreateInfo& createInfo);
virtual ~Window();
virtual void pollInput() override;
virtual void show() override;
virtual void beginFrame() override;
virtual void endFrame() override;
virtual Gfx::PTexture2D getBackBuffer() const override;