2023-11-01 23:12:30 +01:00
|
|
|
#include "Resources.h"
|
2024-06-09 12:20:04 +02:00
|
|
|
#include "Command.h"
|
2023-11-01 23:12:30 +01:00
|
|
|
#include "Enums.h"
|
|
|
|
|
#include "Graphics.h"
|
2023-11-17 22:31:26 +01:00
|
|
|
#include "Window.h"
|
2020-03-24 21:05:32 +01:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
using namespace Seele::Vulkan;
|
|
|
|
|
|
2025-03-07 21:48:27 +01:00
|
|
|
SemaphoreHandle::SemaphoreHandle(PGraphics graphics, const std::string& name) : CommandBoundResource(graphics, name) {
|
2023-11-15 00:06:00 +01:00
|
|
|
VkSemaphoreCreateInfo info = {
|
|
|
|
|
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
|
|
|
|
|
.pNext = nullptr,
|
|
|
|
|
.flags = 0,
|
|
|
|
|
};
|
2020-03-24 21:05:32 +01:00
|
|
|
VK_CHECK(vkCreateSemaphore(graphics->getDevice(), &info, nullptr, &handle));
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-07 21:48:27 +01:00
|
|
|
SemaphoreHandle::~SemaphoreHandle() { vkDestroySemaphore(graphics->getDevice(), handle, nullptr); }
|
|
|
|
|
|
|
|
|
|
Semaphore::Semaphore(PGraphics graphics) : graphics(graphics) {}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
Semaphore::~Semaphore() {
|
2025-03-07 21:48:27 +01:00
|
|
|
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;
|
|
|
|
|
}
|
2025-03-26 13:38:48 +01:00
|
|
|
currentHandle = (uint32)handles.size();
|
2025-03-07 21:48:27 +01:00
|
|
|
handles.add(new SemaphoreHandle(graphics, "Semaphore"));
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
Fence::Fence(PGraphics graphics) : graphics(graphics), status(Status::Ready) {
|
|
|
|
|
VkFenceCreateInfo info = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = nullptr, .flags = 0};
|
2020-04-12 15:47:19 +02:00
|
|
|
VK_CHECK(vkCreateFence(graphics->getDevice(), &info, nullptr, &fence));
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
Fence::~Fence() {
|
2024-05-30 21:24:21 +02:00
|
|
|
vkWaitForFences(graphics->getDevice(), 1, &fence, true, 10000000);
|
2020-04-12 15:47:19 +02:00
|
|
|
vkDestroyFence(graphics->getDevice(), fence, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void Fence::submit() { status = Status::InUse; }
|
2024-05-30 21:24:21 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
bool Fence::isSignaled() {
|
|
|
|
|
if (status == Status::Signalled) {
|
2020-04-12 15:47:19 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
2023-12-28 21:42:18 +01:00
|
|
|
VkResult r = vkGetFenceStatus(graphics->getDevice(), fence);
|
2024-06-09 12:20:04 +02:00
|
|
|
if (r == VK_SUCCESS) {
|
2024-05-30 21:24:21 +02:00
|
|
|
status = Status::Signalled;
|
2023-11-15 00:06:00 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
if (r == VK_NOT_READY) {
|
2023-11-15 00:06:00 +01:00
|
|
|
return false;
|
2024-06-09 12:20:04 +02:00
|
|
|
} else {
|
2023-12-28 21:42:18 +01:00
|
|
|
VK_CHECK(r);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void Fence::reset() {
|
|
|
|
|
while (status == Status::InUse) {
|
2024-05-30 21:24:21 +02:00
|
|
|
wait(1000000);
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
if (status == Status::Signalled) {
|
2024-05-23 14:58:14 +02:00
|
|
|
VK_CHECK(vkResetFences(graphics->getDevice(), 1, &fence));
|
2024-05-30 21:24:21 +02:00
|
|
|
status = Status::Ready;
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void Fence::wait(uint64 timeout) {
|
2020-04-12 15:47:19 +02:00
|
|
|
VkFence fences[] = {fence};
|
2023-11-15 17:42:57 +01:00
|
|
|
VkResult r = vkWaitForFences(graphics->getDevice(), 1, fences, true, timeout);
|
2024-06-09 12:20:04 +02:00
|
|
|
if (r == VK_SUCCESS) {
|
2024-05-30 21:24:21 +02:00
|
|
|
status = Status::Signalled;
|
2024-06-09 12:20:04 +02:00
|
|
|
} else if (r == VK_TIMEOUT) {
|
2023-11-15 17:42:57 +01:00
|
|
|
return;
|
2024-06-09 12:20:04 +02:00
|
|
|
} else if (r != VK_NOT_READY) {
|
2023-11-15 17:42:57 +01:00
|
|
|
VK_CHECK(r);
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
DestructionManager::DestructionManager(PGraphics graphics) : graphics(graphics) {}
|
2023-11-12 16:11:27 +01:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
DestructionManager::~DestructionManager() {}
|
2023-11-12 16:11:27 +01:00
|
|
|
|
2024-06-20 21:57:26 +02:00
|
|
|
void DestructionManager::queueResourceForDestruction(OCommandBoundResource resource) {
|
2024-07-15 08:32:50 +02:00
|
|
|
if (resource == nullptr)
|
|
|
|
|
return;
|
2024-06-20 21:57:26 +02:00
|
|
|
if (resource->isCurrentlyBound()) {
|
|
|
|
|
resources.add(std::move(resource));
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-12 16:11:27 +01:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
void DestructionManager::notifyCommandComplete() {
|
|
|
|
|
for (size_t i = 0; i < resources.size(); ++i) {
|
|
|
|
|
if (!resources[i]->isCurrentlyBound()) {
|
2024-05-15 15:27:13 +02:00
|
|
|
resources.removeAt(i, false);
|
|
|
|
|
i--;
|
|
|
|
|
}
|
2023-11-12 16:11:27 +01:00
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
}
|
2024-05-18 21:23:59 +02:00
|
|
|
|
2024-08-07 21:19:33 +02:00
|
|
|
SamplerHandle::SamplerHandle(PGraphics graphics, VkSamplerCreateInfo createInfo) : CommandBoundResource(graphics, "Sampler") {
|
2024-05-18 21:23:59 +02:00
|
|
|
vkCreateSampler(graphics->getDevice(), &createInfo, nullptr, &sampler);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
SamplerHandle::~SamplerHandle() { vkDestroySampler(graphics->getDevice(), sampler, nullptr); }
|
2024-05-18 21:23:59 +02:00
|
|
|
|
|
|
|
|
Sampler::Sampler(PGraphics graphics, VkSamplerCreateInfo createInfo)
|
2024-06-09 12:20:04 +02:00
|
|
|
: graphics(graphics), handle(new SamplerHandle(graphics, createInfo)) {}
|
2024-05-18 21:23:59 +02:00
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
Sampler::~Sampler() { graphics->getDestructionManager()->queueResourceForDestruction(std::move(handle)); }
|