Normal mapping doesnt work
This commit is contained in:
@@ -4,195 +4,255 @@
|
||||
using namespace Seele;
|
||||
using namespace Seele::Vulkan;
|
||||
|
||||
BufferAllocation::BufferAllocation(PGraphics graphics)
|
||||
: CommandBoundResource(graphics)
|
||||
{
|
||||
}
|
||||
|
||||
BufferAllocation::~BufferAllocation()
|
||||
{
|
||||
if (buffer != VK_NULL_HANDLE)
|
||||
{
|
||||
vmaDestroyBuffer(graphics->getAllocator(), buffer, allocation);
|
||||
}
|
||||
}
|
||||
|
||||
struct PendingBuffer {
|
||||
uint64 offset;
|
||||
uint64 size;
|
||||
VkBuffer stagingBuffer;
|
||||
VmaAllocation allocation;
|
||||
Gfx::QueueType prevQueue;
|
||||
bool writeOnly;
|
||||
OBufferAllocation allocation;
|
||||
uint64 offset;
|
||||
Gfx::QueueType prevQueue;
|
||||
bool writeOnly;
|
||||
};
|
||||
|
||||
static Map<Vulkan::Buffer*, PendingBuffer> pendingBuffers;
|
||||
|
||||
Buffer::Buffer(PGraphics graphics, uint64 size, VkBufferUsageFlags usage, Gfx::QueueType& queueType, bool dynamic,
|
||||
std::string name)
|
||||
: graphics(graphics), currentBuffer(0), size(size), owner(queueType), usage(usage | VK_BUFFER_USAGE_TRANSFER_DST_BIT), name(name) {
|
||||
createBuffer();
|
||||
std::string name)
|
||||
: graphics(graphics), currentBuffer(0), owner(queueType), usage(usage | VK_BUFFER_USAGE_TRANSFER_DST_BIT), dynamic(dynamic), name(name) {
|
||||
createBuffer(size);
|
||||
}
|
||||
|
||||
Buffer::~Buffer() {
|
||||
for (uint32 i = 0; i < buffers.size(); ++i) {
|
||||
graphics->getDestructionManager()->queueBuffer(graphics->getQueueCommands(owner)->getCommands(), buffers[i].buffer,
|
||||
buffers[i].allocation);
|
||||
}
|
||||
for (uint32 i = 0; i < buffers.size(); ++i) {
|
||||
graphics->getDestructionManager()->queueResourceForDestruction(std::move(buffers[i]));
|
||||
}
|
||||
}
|
||||
|
||||
void Buffer::executeOwnershipBarrier(Gfx::QueueType newOwner) {
|
||||
Gfx::QueueFamilyMapping mapping = graphics->getFamilyMapping();
|
||||
VkBufferMemoryBarrier barrier = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
||||
.pNext = nullptr,
|
||||
.srcQueueFamilyIndex = mapping.getQueueTypeFamilyIndex(owner),
|
||||
.dstQueueFamilyIndex = mapping.getQueueTypeFamilyIndex(newOwner),
|
||||
.offset = 0,
|
||||
.size = size,
|
||||
};
|
||||
PCommandPool sourcePool = graphics->getQueueCommands(owner);
|
||||
PCommandPool dstPool = nullptr;
|
||||
VkPipelineStageFlags srcStage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
|
||||
VkPipelineStageFlags dstStage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
|
||||
assert(barrier.srcQueueFamilyIndex != barrier.dstQueueFamilyIndex);
|
||||
if (owner == Gfx::QueueType::TRANSFER) {
|
||||
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||
srcStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
||||
} else if (owner == Gfx::QueueType::COMPUTE) {
|
||||
barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
||||
srcStage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
|
||||
} else if (owner == Gfx::QueueType::GRAPHICS) {
|
||||
barrier.srcAccessMask = getSourceAccessMask();
|
||||
srcStage = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
|
||||
}
|
||||
if (newOwner == Gfx::QueueType::TRANSFER) {
|
||||
barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
|
||||
dstStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
||||
dstPool = graphics->getTransferCommands();
|
||||
} else if (newOwner == Gfx::QueueType::COMPUTE) {
|
||||
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||
dstStage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
|
||||
dstPool = graphics->getComputeCommands();
|
||||
} else if (newOwner == Gfx::QueueType::GRAPHICS) {
|
||||
barrier.dstAccessMask = getDestAccessMask();
|
||||
dstStage = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
|
||||
dstPool = graphics->getGraphicsCommands();
|
||||
}
|
||||
VkCommandBuffer srcCommand = sourcePool->getCommands()->getHandle();
|
||||
VkCommandBuffer dstCommand = dstPool->getCommands()->getHandle();
|
||||
VkBufferMemoryBarrier dynamicBarriers[Gfx::numFramesBuffered];
|
||||
for (uint32 i = 0; i < buffers.size(); ++i) {
|
||||
dynamicBarriers[i] = barrier;
|
||||
dynamicBarriers[i].buffer = buffers[i].buffer;
|
||||
}
|
||||
vkCmdPipelineBarrier(srcCommand, srcStage, srcStage, 0, 0, nullptr, buffers.size(), dynamicBarriers, 0, nullptr);
|
||||
vkCmdPipelineBarrier(dstCommand, dstStage, dstStage, 0, 0, nullptr, buffers.size(), dynamicBarriers, 0, nullptr);
|
||||
sourcePool->submitCommands();
|
||||
if (getSize() == 0)
|
||||
return;
|
||||
Gfx::QueueFamilyMapping mapping = graphics->getFamilyMapping();
|
||||
VkBufferMemoryBarrier barrier = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
||||
.pNext = nullptr,
|
||||
.srcQueueFamilyIndex = mapping.getQueueTypeFamilyIndex(owner),
|
||||
.dstQueueFamilyIndex = mapping.getQueueTypeFamilyIndex(newOwner),
|
||||
.buffer = getHandle(),
|
||||
.offset = 0,
|
||||
.size = getSize(),
|
||||
};
|
||||
PCommandPool sourcePool = graphics->getQueueCommands(owner);
|
||||
PCommandPool dstPool = nullptr;
|
||||
VkPipelineStageFlags srcStage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
|
||||
VkPipelineStageFlags dstStage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
|
||||
assert(barrier.srcQueueFamilyIndex != barrier.dstQueueFamilyIndex);
|
||||
if (owner == Gfx::QueueType::TRANSFER) {
|
||||
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||
srcStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
||||
}
|
||||
else if (owner == Gfx::QueueType::COMPUTE) {
|
||||
barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
||||
srcStage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
|
||||
}
|
||||
else if (owner == Gfx::QueueType::GRAPHICS) {
|
||||
barrier.srcAccessMask = getSourceAccessMask();
|
||||
srcStage = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
|
||||
}
|
||||
if (newOwner == Gfx::QueueType::TRANSFER) {
|
||||
barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
|
||||
dstStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
||||
dstPool = graphics->getTransferCommands();
|
||||
}
|
||||
else if (newOwner == Gfx::QueueType::COMPUTE) {
|
||||
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||
dstStage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
|
||||
dstPool = graphics->getComputeCommands();
|
||||
}
|
||||
else if (newOwner == Gfx::QueueType::GRAPHICS) {
|
||||
barrier.dstAccessMask = getDestAccessMask();
|
||||
dstStage = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
|
||||
dstPool = graphics->getGraphicsCommands();
|
||||
}
|
||||
VkCommandBuffer srcCommand = sourcePool->getCommands()->getHandle();
|
||||
VkCommandBuffer dstCommand = dstPool->getCommands()->getHandle();
|
||||
vkCmdPipelineBarrier(srcCommand, srcStage, srcStage, 0, 0, nullptr, 1, &barrier, 0, nullptr);
|
||||
vkCmdPipelineBarrier(dstCommand, dstStage, dstStage, 0, 0, nullptr, 1, &barrier, 0, nullptr);
|
||||
sourcePool->submitCommands();
|
||||
}
|
||||
|
||||
void Buffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage, VkAccessFlags dstAccess,
|
||||
VkPipelineStageFlags dstStage) {
|
||||
PCommand commandBuffer = graphics->getQueueCommands(owner)->getCommands();
|
||||
VkBufferMemoryBarrier barrier = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
||||
.pNext = nullptr,
|
||||
.srcAccessMask = srcAccess,
|
||||
.dstAccessMask = dstAccess,
|
||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.offset = 0,
|
||||
.size = size,
|
||||
};
|
||||
VkBufferMemoryBarrier dynamicBarriers[Gfx::numFramesBuffered];
|
||||
for (uint32 i = 0; i < buffers.size(); ++i) {
|
||||
dynamicBarriers[i] = barrier;
|
||||
dynamicBarriers[i].buffer = buffers[i].buffer;
|
||||
}
|
||||
vkCmdPipelineBarrier(commandBuffer->getHandle(), srcStage, dstStage, 0, 0, nullptr, buffers.size(), dynamicBarriers, 0,
|
||||
nullptr);
|
||||
VkPipelineStageFlags dstStage) {
|
||||
if (getSize() == 0)
|
||||
return;
|
||||
PCommand commandBuffer = graphics->getQueueCommands(owner)->getCommands();
|
||||
VkBufferMemoryBarrier barrier = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
||||
.pNext = nullptr,
|
||||
.srcAccessMask = srcAccess,
|
||||
.dstAccessMask = dstAccess,
|
||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.buffer = getHandle(),
|
||||
.offset = 0,
|
||||
.size = getSize(),
|
||||
};
|
||||
vkCmdPipelineBarrier(commandBuffer->getHandle(), srcStage, dstStage, 0, 0, nullptr, 1, &barrier, 0,
|
||||
nullptr);
|
||||
}
|
||||
|
||||
void* Buffer::map(bool writeOnly) { return mapRegion(0, size, writeOnly); }
|
||||
void* Buffer::map(bool writeOnly) { return mapRegion(0, getSize(), writeOnly); }
|
||||
|
||||
void* Buffer::mapRegion(uint64 regionOffset, uint64 regionSize, bool writeOnly) {
|
||||
void* data = nullptr;
|
||||
if (regionSize == 0) return nullptr;
|
||||
void* data = nullptr;
|
||||
|
||||
PendingBuffer pending;
|
||||
pending.writeOnly = writeOnly;
|
||||
pending.prevQueue = owner;
|
||||
pending.offset = regionOffset;
|
||||
pending.size = regionSize;
|
||||
if (writeOnly) {
|
||||
if (buffers[currentBuffer].properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
|
||||
VK_CHECK(vmaMapMemory(graphics->getAllocator(), buffers[currentBuffer].allocation, &data));
|
||||
} else {
|
||||
VkBufferCreateInfo stagingInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
||||
.pNext = nullptr,
|
||||
.flags = 0,
|
||||
.size = regionSize,
|
||||
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
||||
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
||||
};
|
||||
VmaAllocationCreateInfo allocInfo = {
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||
.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
|
||||
};
|
||||
VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &stagingInfo, &allocInfo, &pending.stagingBuffer,
|
||||
&pending.allocation, nullptr));
|
||||
vmaMapMemory(graphics->getAllocator(), pending.allocation, &data);
|
||||
PendingBuffer pending;
|
||||
pending.allocation = new BufferAllocation(graphics);
|
||||
pending.allocation->size = regionSize;
|
||||
pending.writeOnly = writeOnly;
|
||||
pending.prevQueue = owner;
|
||||
pending.offset = regionOffset;
|
||||
if (writeOnly) {
|
||||
if (buffers[currentBuffer]->properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
|
||||
VK_CHECK(vmaMapMemory(graphics->getAllocator(), buffers[currentBuffer]->allocation, &data));
|
||||
}
|
||||
else {
|
||||
VkBufferCreateInfo stagingInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
||||
.pNext = nullptr,
|
||||
.flags = 0,
|
||||
.size = regionSize,
|
||||
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
||||
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
||||
};
|
||||
VmaAllocationCreateInfo allocInfo = {
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||
.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
|
||||
};
|
||||
VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &stagingInfo, &allocInfo, &pending.allocation->buffer,
|
||||
&pending.allocation->allocation, nullptr));
|
||||
vmaMapMemory(graphics->getAllocator(), pending.allocation->allocation, &data);
|
||||
vmaSetAllocationName(graphics->getAllocator(), pending.allocation->allocation, "MappingStaging");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
assert(false);
|
||||
}
|
||||
pendingBuffers[this] = std::move(pending);
|
||||
else {
|
||||
assert(false);
|
||||
}
|
||||
pendingBuffers[this] = std::move(pending);
|
||||
|
||||
assert(data);
|
||||
return data;
|
||||
assert(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
void Buffer::unmap() {
|
||||
auto found = pendingBuffers.find(this);
|
||||
if (found != pendingBuffers.end()) {
|
||||
PendingBuffer& pending = found->value;
|
||||
if (pending.writeOnly) {
|
||||
if (buffers[currentBuffer].properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
|
||||
vmaUnmapMemory(graphics->getAllocator(), buffers[currentBuffer].allocation);
|
||||
} else {
|
||||
vmaFlushAllocation(graphics->getAllocator(), pending.allocation, 0, VK_WHOLE_SIZE);
|
||||
vmaUnmapMemory(graphics->getAllocator(), pending.allocation);
|
||||
PCommand command = graphics->getQueueCommands(owner)->getCommands();
|
||||
VkCommandBuffer cmdHandle = command->getHandle();
|
||||
auto found = pendingBuffers.find(this);
|
||||
if (found != pendingBuffers.end()) {
|
||||
PendingBuffer& pending = found->value;
|
||||
if (pending.writeOnly) {
|
||||
if (buffers[currentBuffer]->properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
|
||||
vmaUnmapMemory(graphics->getAllocator(), buffers[currentBuffer]->allocation);
|
||||
}
|
||||
else {
|
||||
vmaFlushAllocation(graphics->getAllocator(), pending.allocation->allocation, 0, VK_WHOLE_SIZE);
|
||||
vmaUnmapMemory(graphics->getAllocator(), pending.allocation->allocation);
|
||||
PCommand command = graphics->getQueueCommands(owner)->getCommands();
|
||||
command->bindResource(PBufferAllocation(pending.allocation));
|
||||
VkCommandBuffer cmdHandle = command->getHandle();
|
||||
|
||||
VkBufferCopy region = {
|
||||
.srcOffset = 0,
|
||||
.dstOffset = pending.offset,
|
||||
.size = pending.size,
|
||||
};
|
||||
VkBufferMemoryBarrier barrier = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
||||
.pNext = nullptr,
|
||||
.srcAccessMask = VK_ACCESS_MEMORY_READ_BIT,
|
||||
.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
|
||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.buffer = buffers[currentBuffer].buffer,
|
||||
.offset = 0,
|
||||
.size = size,
|
||||
};
|
||||
vkCmdPipelineBarrier(cmdHandle, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0,
|
||||
nullptr, 1, &barrier, 0, nullptr);
|
||||
vkCmdCopyBuffer(cmdHandle, pending.stagingBuffer, buffers[currentBuffer].buffer, 1, ®ion);
|
||||
barrier = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
||||
.pNext = nullptr,
|
||||
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
|
||||
.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
|
||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.buffer = buffers[currentBuffer].buffer,
|
||||
.offset = 0,
|
||||
.size = size,
|
||||
};
|
||||
vkCmdPipelineBarrier(cmdHandle, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0,
|
||||
nullptr, 1, &barrier, 0, nullptr);
|
||||
graphics->getDestructionManager()->queueBuffer(command, pending.stagingBuffer, pending.allocation);
|
||||
}
|
||||
VkBufferCopy region = {
|
||||
.srcOffset = 0,
|
||||
.dstOffset = pending.offset,
|
||||
.size = pending.allocation->size,
|
||||
};
|
||||
VkBufferMemoryBarrier barrier = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
||||
.pNext = nullptr,
|
||||
.srcAccessMask = VK_ACCESS_MEMORY_READ_BIT,
|
||||
.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
|
||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.buffer = buffers[currentBuffer]->buffer,
|
||||
.offset = 0,
|
||||
.size = buffers[currentBuffer]->size,
|
||||
};
|
||||
vkCmdPipelineBarrier(cmdHandle, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0,
|
||||
nullptr, 1, &barrier, 0, nullptr);
|
||||
vkCmdCopyBuffer(cmdHandle, pending.allocation->buffer, buffers[currentBuffer]->buffer, 1, ®ion);
|
||||
barrier = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
||||
.pNext = nullptr,
|
||||
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
|
||||
.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
|
||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.buffer = buffers[currentBuffer]->buffer,
|
||||
.offset = 0,
|
||||
.size = buffers[currentBuffer]->size,
|
||||
};
|
||||
vkCmdPipelineBarrier(cmdHandle, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0,
|
||||
nullptr, 1, &barrier, 0, nullptr);
|
||||
graphics->getDestructionManager()->queueResourceForDestruction(std::move(pending.allocation));
|
||||
}
|
||||
}
|
||||
pendingBuffers.erase(this);
|
||||
}
|
||||
pendingBuffers.erase(this);
|
||||
}
|
||||
}
|
||||
|
||||
void Buffer::createBuffer()
|
||||
void Buffer::rotateBuffer(uint64 size)
|
||||
{
|
||||
size = std::max(getSize(), size);
|
||||
for (size_t i = 0; i < buffers.size(); ++i)
|
||||
{
|
||||
if (buffers[i]->isCurrentlyBound())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (buffers[i]->size < size)
|
||||
{
|
||||
vmaDestroyBuffer(graphics->getAllocator(), buffers[i]->buffer, buffers[i]->allocation);
|
||||
VkBufferCreateInfo info = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
||||
.pNext = nullptr,
|
||||
.size = size,
|
||||
.usage = usage,
|
||||
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
||||
};
|
||||
VmaAllocationCreateInfo allocInfo = {
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT |
|
||||
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||
};
|
||||
VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &info, &allocInfo, &buffers[i]->buffer, &buffers[i]->allocation,
|
||||
&buffers[i]->info));
|
||||
vmaGetAllocationMemoryProperties(graphics->getAllocator(), buffers[i]->allocation, &buffers[i]->properties);
|
||||
if (!name.empty()) {
|
||||
VkDebugUtilsObjectNameInfoEXT nameInfo = { .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
|
||||
.pNext = nullptr,
|
||||
.objectType = VK_OBJECT_TYPE_BUFFER,
|
||||
.objectHandle = (uint64)buffers[i]->buffer,
|
||||
.pObjectName = this->name.c_str() };
|
||||
graphics->vkSetDebugUtilsObjectNameEXT(&nameInfo);
|
||||
}
|
||||
buffers[i]->size = size;
|
||||
}
|
||||
currentBuffer = i;
|
||||
return;
|
||||
}
|
||||
createBuffer(size);
|
||||
}
|
||||
|
||||
void Buffer::createBuffer(uint64 size)
|
||||
{
|
||||
VkBufferCreateInfo info = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
||||
@@ -206,17 +266,18 @@ void Buffer::createBuffer()
|
||||
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||
};
|
||||
buffers.add();
|
||||
buffers.add(new BufferAllocation(graphics));
|
||||
if (size > 0)
|
||||
{
|
||||
VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &info, &allocInfo, &buffers.back().buffer, &buffers.back().allocation,
|
||||
&buffers.back().info));
|
||||
vmaGetAllocationMemoryProperties(graphics->getAllocator(), buffers.back().allocation, &buffers.back().properties);
|
||||
VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &info, &allocInfo, &buffers.back()->buffer, &buffers.back()->allocation,
|
||||
&buffers.back()->info));
|
||||
buffers.back()->size = size;
|
||||
vmaGetAllocationMemoryProperties(graphics->getAllocator(), buffers.back()->allocation, &buffers.back()->properties);
|
||||
if (!name.empty()) {
|
||||
VkDebugUtilsObjectNameInfoEXT nameInfo = { .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
|
||||
.pNext = nullptr,
|
||||
.objectType = VK_OBJECT_TYPE_BUFFER,
|
||||
.objectHandle = (uint64)buffers.back().buffer,
|
||||
.objectHandle = (uint64)buffers.back()->buffer,
|
||||
.pObjectName = this->name.c_str() };
|
||||
graphics->vkSetDebugUtilsObjectNameEXT(&nameInfo);
|
||||
}
|
||||
@@ -225,39 +286,39 @@ void Buffer::createBuffer()
|
||||
|
||||
UniformBuffer::UniformBuffer(PGraphics graphics, const UniformBufferCreateInfo& createInfo)
|
||||
: Gfx::UniformBuffer(graphics->getFamilyMapping(), createInfo.sourceData),
|
||||
Vulkan::Buffer(graphics, createInfo.sourceData.size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, currentOwner,
|
||||
createInfo.dynamic, createInfo.name) {
|
||||
if (size > 0 && createInfo.sourceData.data != nullptr) {
|
||||
void* data = map();
|
||||
std::memcpy(data, createInfo.sourceData.data, createInfo.sourceData.size);
|
||||
unmap();
|
||||
}
|
||||
Vulkan::Buffer(graphics, createInfo.sourceData.size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, currentOwner,
|
||||
createInfo.dynamic, createInfo.name) {
|
||||
if (getSize() > 0 && createInfo.sourceData.data != nullptr) {
|
||||
void* data = map();
|
||||
std::memcpy(data, createInfo.sourceData.data, createInfo.sourceData.size);
|
||||
unmap();
|
||||
}
|
||||
}
|
||||
|
||||
UniformBuffer::~UniformBuffer() {}
|
||||
|
||||
bool UniformBuffer::updateContents(const DataSource& sourceData) {
|
||||
if (!Gfx::UniformBuffer::updateContents(sourceData)) {
|
||||
// no update was performed, skip
|
||||
return false;
|
||||
}
|
||||
void* data = map();
|
||||
std::memcpy(data, sourceData.data, sourceData.size);
|
||||
unmap();
|
||||
return true;
|
||||
if (!Gfx::UniformBuffer::updateContents(sourceData)) {
|
||||
// no update was performed, skip
|
||||
return false;
|
||||
}
|
||||
void* data = map();
|
||||
std::memcpy(data, sourceData.data, sourceData.size);
|
||||
unmap();
|
||||
return true;
|
||||
}
|
||||
|
||||
void UniformBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner) {
|
||||
Gfx::QueueOwnedResource::transferOwnership(newOwner);
|
||||
Gfx::QueueOwnedResource::transferOwnership(newOwner);
|
||||
}
|
||||
|
||||
void UniformBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) {
|
||||
Vulkan::Buffer::executeOwnershipBarrier(newOwner);
|
||||
Vulkan::Buffer::executeOwnershipBarrier(newOwner);
|
||||
}
|
||||
|
||||
void UniformBuffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage,
|
||||
VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) {
|
||||
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
|
||||
VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) {
|
||||
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
|
||||
}
|
||||
|
||||
VkAccessFlags UniformBuffer::getSourceAccessMask() { return VK_ACCESS_MEMORY_WRITE_BIT; }
|
||||
@@ -266,38 +327,56 @@ VkAccessFlags UniformBuffer::getDestAccessMask() { return VK_ACCESS_UNIFORM_READ
|
||||
|
||||
ShaderBuffer::ShaderBuffer(PGraphics graphics, const ShaderBufferCreateInfo& sourceData)
|
||||
: Gfx::ShaderBuffer(graphics->getFamilyMapping(), sourceData.numElements, sourceData.sourceData),
|
||||
Vulkan::Buffer(graphics, sourceData.sourceData.size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, currentOwner,
|
||||
sourceData.dynamic, sourceData.name) {
|
||||
if (size > 0 && sourceData.sourceData.data != nullptr) {
|
||||
void* data = map();
|
||||
std::memcpy(data, sourceData.sourceData.data, sourceData.sourceData.size);
|
||||
unmap();
|
||||
}
|
||||
Vulkan::Buffer(graphics, sourceData.sourceData.size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, currentOwner,
|
||||
sourceData.dynamic, sourceData.name) {
|
||||
if (getSize() > 0 && sourceData.sourceData.data != nullptr) {
|
||||
void* data = map();
|
||||
std::memcpy(data, sourceData.sourceData.data, sourceData.sourceData.size);
|
||||
unmap();
|
||||
}
|
||||
}
|
||||
|
||||
ShaderBuffer::~ShaderBuffer() {}
|
||||
|
||||
bool ShaderBuffer::updateContents(const DataSource& sourceData) {
|
||||
assert(sourceData.size <= getSize());
|
||||
Gfx::ShaderBuffer::updateContents(sourceData);
|
||||
// We always want to update, as the contents could be different on the GPU
|
||||
void* data = map();
|
||||
std::memcpy(data, sourceData.data, sourceData.size);
|
||||
unmap();
|
||||
return true;
|
||||
void ShaderBuffer::updateContents(const ShaderBufferCreateInfo& createInfo) {
|
||||
Gfx::ShaderBuffer::updateContents(createInfo);
|
||||
// We always want to update, as the contents could be different on the GPU
|
||||
if (createInfo.sourceData.data == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
void* data = map();
|
||||
std::memcpy(data, createInfo.sourceData.data, createInfo.sourceData.size);
|
||||
unmap();
|
||||
}
|
||||
|
||||
void Seele::Vulkan::ShaderBuffer::rotateBuffer(uint64 size)
|
||||
{
|
||||
assert(dynamic);
|
||||
Vulkan::Buffer::rotateBuffer(size);
|
||||
}
|
||||
|
||||
void* ShaderBuffer::mapRegion(uint64 offset, uint64 size, bool writeOnly)
|
||||
{
|
||||
return Vulkan::Buffer::mapRegion(offset, size, writeOnly);
|
||||
}
|
||||
|
||||
void ShaderBuffer::unmap()
|
||||
{
|
||||
Vulkan::Buffer::unmap();
|
||||
}
|
||||
|
||||
void ShaderBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner) {
|
||||
Gfx::QueueOwnedResource::transferOwnership(newOwner);
|
||||
Gfx::QueueOwnedResource::transferOwnership(newOwner);
|
||||
}
|
||||
|
||||
void ShaderBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) {
|
||||
Vulkan::Buffer::executeOwnershipBarrier(newOwner);
|
||||
Vulkan::Buffer::executeOwnershipBarrier(newOwner);
|
||||
}
|
||||
|
||||
void ShaderBuffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage,
|
||||
VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) {
|
||||
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
|
||||
VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) {
|
||||
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
|
||||
}
|
||||
|
||||
VkAccessFlags ShaderBuffer::getSourceAccessMask() { return VK_ACCESS_MEMORY_WRITE_BIT; }
|
||||
@@ -306,42 +385,42 @@ VkAccessFlags ShaderBuffer::getDestAccessMask() { return VK_ACCESS_MEMORY_READ_B
|
||||
|
||||
VertexBuffer::VertexBuffer(PGraphics graphics, const VertexBufferCreateInfo& sourceData)
|
||||
: Gfx::VertexBuffer(graphics->getFamilyMapping(), sourceData.numVertices, sourceData.vertexSize,
|
||||
sourceData.sourceData.owner),
|
||||
Vulkan::Buffer(graphics, sourceData.sourceData.size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, currentOwner, false,
|
||||
sourceData.name) {
|
||||
if (sourceData.sourceData.data != nullptr) {
|
||||
void* data = map();
|
||||
std::memcpy(data, sourceData.sourceData.data, sourceData.sourceData.size);
|
||||
unmap();
|
||||
}
|
||||
sourceData.sourceData.owner),
|
||||
Vulkan::Buffer(graphics, sourceData.sourceData.size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, currentOwner, false,
|
||||
sourceData.name) {
|
||||
if (sourceData.sourceData.data != nullptr) {
|
||||
void* data = map();
|
||||
std::memcpy(data, sourceData.sourceData.data, sourceData.sourceData.size);
|
||||
unmap();
|
||||
}
|
||||
}
|
||||
|
||||
VertexBuffer::~VertexBuffer() {}
|
||||
|
||||
void VertexBuffer::updateRegion(DataSource update) {
|
||||
void* data = mapRegion(update.offset, update.size);
|
||||
std::memcpy(data, update.data, update.size);
|
||||
unmap();
|
||||
void* data = mapRegion(update.offset, update.size);
|
||||
std::memcpy(data, update.data, update.size);
|
||||
unmap();
|
||||
}
|
||||
|
||||
void VertexBuffer::download(Array<uint8>& buffer) {
|
||||
void* data = map(false);
|
||||
buffer.resize(size);
|
||||
std::memcpy(buffer.data(), data, size);
|
||||
unmap();
|
||||
void* data = map(false);
|
||||
buffer.resize(getSize());
|
||||
std::memcpy(buffer.data(), data, getSize());
|
||||
unmap();
|
||||
}
|
||||
|
||||
void VertexBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner) {
|
||||
Gfx::QueueOwnedResource::transferOwnership(newOwner);
|
||||
Gfx::QueueOwnedResource::transferOwnership(newOwner);
|
||||
}
|
||||
|
||||
void VertexBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) {
|
||||
Vulkan::Buffer::executeOwnershipBarrier(newOwner);
|
||||
Vulkan::Buffer::executeOwnershipBarrier(newOwner);
|
||||
}
|
||||
|
||||
void VertexBuffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage,
|
||||
VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) {
|
||||
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
|
||||
VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) {
|
||||
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
|
||||
}
|
||||
|
||||
VkAccessFlags VertexBuffer::getSourceAccessMask() { return VK_ACCESS_MEMORY_WRITE_BIT; }
|
||||
@@ -350,36 +429,36 @@ VkAccessFlags VertexBuffer::getDestAccessMask() { return VK_ACCESS_VERTEX_ATTRIB
|
||||
|
||||
IndexBuffer::IndexBuffer(PGraphics graphics, const IndexBufferCreateInfo& sourceData)
|
||||
: Gfx::IndexBuffer(graphics->getFamilyMapping(), sourceData.sourceData.size, sourceData.indexType,
|
||||
sourceData.sourceData.owner),
|
||||
Vulkan::Buffer(graphics, sourceData.sourceData.size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, currentOwner, false,
|
||||
sourceData.name) {
|
||||
if (sourceData.sourceData.data != nullptr) {
|
||||
void* data = map();
|
||||
std::memcpy(data, sourceData.sourceData.data, sourceData.sourceData.size);
|
||||
unmap();
|
||||
}
|
||||
sourceData.sourceData.owner),
|
||||
Vulkan::Buffer(graphics, sourceData.sourceData.size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, currentOwner, false,
|
||||
sourceData.name) {
|
||||
if (sourceData.sourceData.data != nullptr) {
|
||||
void* data = map();
|
||||
std::memcpy(data, sourceData.sourceData.data, sourceData.sourceData.size);
|
||||
unmap();
|
||||
}
|
||||
}
|
||||
|
||||
IndexBuffer::~IndexBuffer() {}
|
||||
|
||||
void IndexBuffer::download(Array<uint8>& buffer) {
|
||||
void* data = map(false);
|
||||
buffer.resize(size);
|
||||
std::memcpy(buffer.data(), data, size);
|
||||
unmap();
|
||||
void* data = map(false);
|
||||
buffer.resize(getSize());
|
||||
std::memcpy(buffer.data(), data, getSize());
|
||||
unmap();
|
||||
}
|
||||
|
||||
void IndexBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner) {
|
||||
Gfx::QueueOwnedResource::transferOwnership(newOwner);
|
||||
Gfx::QueueOwnedResource::transferOwnership(newOwner);
|
||||
}
|
||||
|
||||
void IndexBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner) {
|
||||
Vulkan::Buffer::executeOwnershipBarrier(newOwner);
|
||||
Vulkan::Buffer::executeOwnershipBarrier(newOwner);
|
||||
}
|
||||
|
||||
void IndexBuffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage,
|
||||
VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) {
|
||||
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
|
||||
VkAccessFlags dstAccess, VkPipelineStageFlags dstStage) {
|
||||
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
|
||||
}
|
||||
|
||||
VkAccessFlags IndexBuffer::getSourceAccessMask() { return VK_ACCESS_MEMORY_WRITE_BIT; }
|
||||
|
||||
@@ -1,38 +1,46 @@
|
||||
#pragma once
|
||||
#include "Graphics.h"
|
||||
#include "Graphics/Buffer.h"
|
||||
#include "Resources.h"
|
||||
|
||||
namespace Seele {
|
||||
namespace Vulkan {
|
||||
DECLARE_REF(Command)
|
||||
DECLARE_REF(Fence)
|
||||
class BufferAllocation : public CommandBoundResource {
|
||||
public:
|
||||
BufferAllocation(PGraphics graphics);
|
||||
virtual ~BufferAllocation();
|
||||
VkBuffer buffer = VK_NULL_HANDLE;
|
||||
VmaAllocation allocation = VmaAllocation();
|
||||
VmaAllocationInfo info = VmaAllocationInfo();
|
||||
VkMemoryPropertyFlags properties = 0;
|
||||
uint64 size = 0;
|
||||
};
|
||||
DEFINE_REF(BufferAllocation);
|
||||
class Buffer {
|
||||
public:
|
||||
Buffer(PGraphics graphics, uint64 size, VkBufferUsageFlags usage,
|
||||
Gfx::QueueType &queueType, bool dynamic, std::string name);
|
||||
virtual ~Buffer();
|
||||
VkBuffer getHandle() const { return buffers[currentBuffer].buffer; }
|
||||
uint64 getSize() const { return size; }
|
||||
VkBuffer getHandle() const { return buffers[currentBuffer]->buffer; }
|
||||
PBufferAllocation getAlloc() const { return buffers[currentBuffer]; }
|
||||
uint64 getSize() const { return buffers[currentBuffer]->size; }
|
||||
void *map(bool writeOnly = true);
|
||||
void *mapRegion(uint64 regionOffset, uint64 regionSize,
|
||||
bool writeOnly = true);
|
||||
void unmap();
|
||||
|
||||
protected:
|
||||
struct BufferAllocation {
|
||||
VkBuffer buffer;
|
||||
VmaAllocation allocation;
|
||||
VmaAllocationInfo info;
|
||||
VkMemoryPropertyFlags properties;
|
||||
};
|
||||
PGraphics graphics;
|
||||
uint32 currentBuffer;
|
||||
uint64 size;
|
||||
Gfx::QueueType &owner;
|
||||
Array<BufferAllocation> buffers;
|
||||
Array<OBufferAllocation> buffers;
|
||||
VkBufferUsageFlags usage;
|
||||
bool dynamic;
|
||||
std::string name;
|
||||
void createBuffer();
|
||||
void rotateBuffer(uint64 size);
|
||||
void createBuffer(uint64 size);
|
||||
|
||||
void executeOwnershipBarrier(Gfx::QueueType newOwner);
|
||||
void executePipelineBarrier(VkAccessFlags srcAccess,
|
||||
@@ -115,7 +123,10 @@ class ShaderBuffer : public Gfx::ShaderBuffer, public Buffer {
|
||||
public:
|
||||
ShaderBuffer(PGraphics graphics, const ShaderBufferCreateInfo &sourceData);
|
||||
virtual ~ShaderBuffer();
|
||||
virtual bool updateContents(const DataSource &sourceData) override;
|
||||
virtual void updateContents(const ShaderBufferCreateInfo &createInfo) override;
|
||||
virtual void rotateBuffer(uint64 size) override;
|
||||
virtual void* mapRegion(uint64 offset, uint64 size, bool writeOnly) override;
|
||||
virtual void unmap() override;
|
||||
|
||||
protected:
|
||||
// Inherited via Vulkan::Buffer
|
||||
|
||||
@@ -95,9 +95,9 @@ void Command::executeCommands(Array<Gfx::ORenderCommand> commands)
|
||||
{
|
||||
auto command = Gfx::PRenderCommand(commands[i]).cast<RenderCommand>();
|
||||
command->end();
|
||||
for(auto& descriptor : command->boundDescriptors)
|
||||
for(auto& descriptor : command->boundResources)
|
||||
{
|
||||
boundDescriptors.add(descriptor);
|
||||
boundResources.add(descriptor);
|
||||
//std::cout << "Cmd " << handle << " bound descriptor " << descriptor->getHandle() << std::endl;
|
||||
}
|
||||
cmdBuffers[i] = command->getHandle();
|
||||
@@ -117,9 +117,9 @@ void Command::executeCommands(Array<Gfx::OComputeCommand> commands)
|
||||
{
|
||||
auto command = Gfx::PComputeCommand(commands[i]).cast<ComputeCommand>();
|
||||
command->end();
|
||||
for(auto& descriptor : command->boundDescriptors)
|
||||
for(auto& descriptor : command->boundResources)
|
||||
{
|
||||
boundDescriptors.add(descriptor);
|
||||
boundResources.add(descriptor);
|
||||
//std::cout << "Cmd " << handle << " bound descriptor " << descriptor->getHandle() << std::endl;
|
||||
}
|
||||
cmdBuffers[i] = command->getHandle();
|
||||
@@ -153,18 +153,17 @@ void Command::checkFence()
|
||||
command->reset();
|
||||
}
|
||||
executingRenders.clear();
|
||||
for(auto& descriptor : boundDescriptors)
|
||||
for(auto& descriptor : boundResources)
|
||||
{
|
||||
descriptor->unbind();
|
||||
//std::cout << "Cmd " << handle << " unbind " << descriptor->getHandle() << std::endl;
|
||||
}
|
||||
boundDescriptors.clear();
|
||||
graphics->getDestructionManager()->notifyCmdComplete(this);
|
||||
boundResources.clear();
|
||||
graphics->getDestructionManager()->notifyCommandComplete();
|
||||
state = State::Init;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Command::waitForCommand(uint32 timeout)
|
||||
{
|
||||
pool->submitCommands();
|
||||
@@ -177,6 +176,12 @@ void Command::waitForCommand(uint32 timeout)
|
||||
checkFence();
|
||||
}
|
||||
|
||||
void Command::bindResource(PCommandBoundResource resource)
|
||||
{
|
||||
resource->bind();
|
||||
boundResources.add(resource);
|
||||
}
|
||||
|
||||
PFence Command::getFence()
|
||||
{
|
||||
return fence;
|
||||
@@ -237,7 +242,7 @@ void RenderCommand::end()
|
||||
void RenderCommand::reset()
|
||||
{
|
||||
vkResetCommandBuffer(handle, VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT);
|
||||
boundDescriptors.clear();
|
||||
boundResources.clear();
|
||||
ready = true;
|
||||
}
|
||||
|
||||
@@ -275,12 +280,15 @@ void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet, Array<uint
|
||||
assert(threadId == std::this_thread::get_id());
|
||||
auto descriptor = descriptorSet.cast<DescriptorSet>();
|
||||
assert(descriptor->writeDescriptors.size() == 0);
|
||||
boundDescriptors.add(descriptor.getHandle());
|
||||
boundResources.add(descriptor.getHandle());
|
||||
descriptor->boundResources.clear();
|
||||
boundResources.addAll(descriptor->boundResources);
|
||||
descriptor->bind();
|
||||
|
||||
VkDescriptorSet setHandle = descriptor->getHandle();
|
||||
vkCmdBindDescriptorSets(handle, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->getLayout(), pipeline->getPipelineLayout()->findParameter(descriptorSet->getName()), 1, &setHandle, dynamicOffsets.size(), dynamicOffsets.data());
|
||||
}
|
||||
|
||||
void RenderCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorSets, Array<uint32> dynamicOffsets)
|
||||
{
|
||||
assert(threadId == std::this_thread::get_id());
|
||||
@@ -291,11 +299,14 @@ void RenderCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorS
|
||||
assert(descriptorSet->writeDescriptors.size() == 0);
|
||||
descriptorSet->bind();
|
||||
|
||||
boundDescriptors.add(descriptorSet.getHandle());
|
||||
boundResources.add(descriptorSet.getHandle());
|
||||
boundResources.addAll(descriptorSet->boundResources);
|
||||
descriptorSet->boundResources.clear();
|
||||
sets[pipeline->getPipelineLayout()->findParameter(descriptorSet->getName())] = descriptorSet->getHandle();
|
||||
}
|
||||
vkCmdBindDescriptorSets(handle, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->getLayout(), 0, (uint32)descriptorSets.size(), sets, dynamicOffsets.size(), dynamicOffsets.data());
|
||||
delete[] sets;
|
||||
|
||||
}
|
||||
void RenderCommand::bindVertexBuffer(const Array<Gfx::PVertexBuffer>& streams)
|
||||
{
|
||||
@@ -307,6 +318,8 @@ void RenderCommand::bindVertexBuffer(const Array<Gfx::PVertexBuffer>& streams)
|
||||
PVertexBuffer buf = streams[i].cast<VertexBuffer>();
|
||||
buffers[i] = buf->getHandle();
|
||||
offsets[i] = 0;
|
||||
buf->getAlloc()->bind();
|
||||
boundResources.add(buf->getAlloc());
|
||||
};
|
||||
vkCmdBindVertexBuffers(handle, 0, (uint32)streams.size(), buffers.data(), offsets.data());
|
||||
}
|
||||
@@ -314,6 +327,8 @@ void RenderCommand::bindIndexBuffer(Gfx::PIndexBuffer indexBuffer)
|
||||
{
|
||||
assert(threadId == std::this_thread::get_id());
|
||||
PIndexBuffer buf = indexBuffer.cast<IndexBuffer>();
|
||||
buf->getAlloc()->bind();
|
||||
boundResources.add(buf->getAlloc());
|
||||
vkCmdBindIndexBuffer(handle, buf->getHandle(), 0, cast(buf->getIndexType()));
|
||||
}
|
||||
|
||||
@@ -398,7 +413,7 @@ void ComputeCommand::reset()
|
||||
{
|
||||
assert(threadId == std::this_thread::get_id());
|
||||
vkResetCommandBuffer(handle, VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT);
|
||||
boundDescriptors.clear();
|
||||
boundResources.clear();
|
||||
ready = true;
|
||||
}
|
||||
bool ComputeCommand::isReady()
|
||||
@@ -418,7 +433,9 @@ void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet, Array<uin
|
||||
assert(threadId == std::this_thread::get_id());
|
||||
auto descriptor = descriptorSet.cast<DescriptorSet>();
|
||||
assert(descriptor->writeDescriptors.size() == 0);
|
||||
boundDescriptors.add(descriptor.getHandle());
|
||||
boundResources.add(descriptor.getHandle());
|
||||
boundResources.addAll(descriptor->boundResources);
|
||||
descriptor->boundResources.clear();
|
||||
descriptor->bind();
|
||||
|
||||
VkDescriptorSet setHandle = descriptor->getHandle();
|
||||
@@ -436,7 +453,9 @@ void ComputeCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptor
|
||||
descriptorSet->bind();
|
||||
|
||||
//std::cout << "Binding descriptor " << descriptorSet->getHandle() << " to cmd " << handle << std::endl;
|
||||
boundDescriptors.add(descriptorSet.getHandle());
|
||||
boundResources.add(descriptorSet.getHandle());
|
||||
boundResources.addAll(descriptorSet->boundResources);
|
||||
descriptorSet->boundResources.clear();
|
||||
sets[pipeline->getPipelineLayout()->findParameter(descriptorSet->getName())] = descriptorSet->getHandle();
|
||||
}
|
||||
vkCmdBindDescriptorSets(handle, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline->getLayout(), 0, (uint32)descriptorSets.size(), sets, dynamicOffsets.size(), dynamicOffsets.data());
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "Buffer.h"
|
||||
#include "Graphics/Command.h"
|
||||
#include "Queue.h"
|
||||
#include "Resources.h"
|
||||
#include <thread>
|
||||
|
||||
namespace Seele {
|
||||
@@ -25,6 +26,7 @@ public:
|
||||
void executeCommands(Array<Gfx::ORenderCommand> secondaryCommands);
|
||||
void executeCommands(Array<Gfx::OComputeCommand> secondaryCommands);
|
||||
void waitForSemaphore(VkPipelineStageFlags stages, PSemaphore waitSemaphore);
|
||||
void bindResource(PCommandBoundResource resource);
|
||||
void checkFence();
|
||||
void waitForCommand(uint32 timeToWait = 1000000u);
|
||||
PFence getFence();
|
||||
@@ -52,7 +54,7 @@ private:
|
||||
Array<VkPipelineStageFlags> waitFlags;
|
||||
Array<ORenderCommand> executingRenders;
|
||||
Array<OComputeCommand> executingComputes;
|
||||
Array<PDescriptorSet> boundDescriptors;
|
||||
Array<PDescriptorSet> boundResources;
|
||||
friend class RenderCommand;
|
||||
friend class CommandPool;
|
||||
friend class Queue;
|
||||
@@ -87,7 +89,7 @@ public:
|
||||
private:
|
||||
PGraphicsPipeline pipeline;
|
||||
bool ready;
|
||||
Array<PDescriptorSet> boundDescriptors;
|
||||
Array<PCommandBoundResource> boundResources;
|
||||
VkViewport currentViewport;
|
||||
VkRect2D currentScissor;
|
||||
PGraphics graphics;
|
||||
@@ -117,7 +119,7 @@ public:
|
||||
private:
|
||||
PComputePipeline pipeline;
|
||||
bool ready;
|
||||
Array<PDescriptorSet> boundDescriptors;
|
||||
Array<PCommandBoundResource> boundResources;
|
||||
VkViewport currentViewport;
|
||||
VkRect2D currentScissor;
|
||||
PGraphics graphics;
|
||||
|
||||
@@ -11,6 +11,7 @@ DescriptorLayout::DescriptorLayout(PGraphics graphics, const std::string& name)
|
||||
: Gfx::DescriptorLayout(name), graphics(graphics), layoutHandle(VK_NULL_HANDLE) {}
|
||||
|
||||
DescriptorLayout::~DescriptorLayout() {
|
||||
graphics->getDestructionManager()->queueResourceForDestruction(std::move(pool));
|
||||
if (layoutHandle != VK_NULL_HANDLE) {
|
||||
vkDestroyDescriptorSetLayout(graphics->getDevice(), layoutHandle, nullptr);
|
||||
}
|
||||
@@ -53,7 +54,10 @@ void DescriptorLayout::create() {
|
||||
hash = CRC::Calculate(bindings.data(), sizeof(VkDescriptorSetLayoutBinding) * bindings.size(), CRC::CRC_32());
|
||||
}
|
||||
|
||||
DescriptorPool::DescriptorPool(PGraphics graphics, PDescriptorLayout layout) : graphics(graphics), layout(layout) {
|
||||
DescriptorPool::DescriptorPool(PGraphics graphics, PDescriptorLayout layout)
|
||||
: CommandBoundResource(graphics)
|
||||
, graphics(graphics)
|
||||
, layout(layout) {
|
||||
for (uint32 i = 0; i < cachedHandles.size(); ++i) {
|
||||
cachedHandles[i] = nullptr;
|
||||
}
|
||||
@@ -86,10 +90,17 @@ DescriptorPool::DescriptorPool(PGraphics graphics, PDescriptorLayout layout) : g
|
||||
}
|
||||
|
||||
DescriptorPool::~DescriptorPool() {
|
||||
graphics->getDestructionManager()->queueDescriptorPool(graphics->getGraphicsCommands()->getCommands(), poolHandle);
|
||||
if (nextAlloc) {
|
||||
delete nextAlloc;
|
||||
}
|
||||
for (size_t i = 0; i < maxSets; ++i)
|
||||
{
|
||||
if (cachedHandles[i] != nullptr)
|
||||
{
|
||||
cachedHandles[i] = nullptr;
|
||||
}
|
||||
}
|
||||
vkDestroyDescriptorPool(graphics->getDevice(), poolHandle, nullptr);
|
||||
if (nextAlloc) {
|
||||
delete nextAlloc;
|
||||
}
|
||||
}
|
||||
|
||||
Gfx::PDescriptorSet DescriptorPool::allocateDescriptorSet() {
|
||||
@@ -159,10 +170,19 @@ void DescriptorPool::reset() {
|
||||
}
|
||||
|
||||
DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner)
|
||||
: Gfx::DescriptorSet(owner->getLayout()), setHandle(VK_NULL_HANDLE), graphics(graphics), owner(owner), bindCount(0),
|
||||
currentlyInUse(false) {}
|
||||
: Gfx::DescriptorSet(owner->getLayout())
|
||||
, CommandBoundResource(graphics)
|
||||
, setHandle(VK_NULL_HANDLE)
|
||||
, graphics(graphics)
|
||||
, owner(owner)
|
||||
, bindCount(0)
|
||||
, currentlyInUse(false)
|
||||
{}
|
||||
|
||||
DescriptorSet::~DescriptorSet() {}
|
||||
DescriptorSet::~DescriptorSet()
|
||||
{
|
||||
vkFreeDescriptorSets(graphics->getDevice(), owner->getHandle(), 1, &setHandle);
|
||||
}
|
||||
|
||||
void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PUniformBuffer uniformBuffer) {
|
||||
PUniformBuffer vulkanBuffer = uniformBuffer.cast<UniformBuffer>();
|
||||
@@ -189,6 +209,8 @@ void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PUniformBuffer uniformBu
|
||||
});
|
||||
|
||||
cachedData[binding] = vulkanBuffer.getHandle();
|
||||
vulkanBuffer->getAlloc()->bind();
|
||||
boundResources.add(vulkanBuffer->getAlloc());
|
||||
}
|
||||
|
||||
void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PShaderBuffer shaderBuffer) {
|
||||
@@ -215,6 +237,8 @@ void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PShaderBuffer shaderBuff
|
||||
});
|
||||
|
||||
cachedData[binding] = vulkanBuffer.getHandle();
|
||||
vulkanBuffer->getAlloc()->bind();
|
||||
boundResources.add(vulkanBuffer->getAlloc());
|
||||
}
|
||||
|
||||
void DescriptorSet::updateBuffer(uint32_t binding, uint32 index, Gfx::PShaderBuffer shaderBuffer) {
|
||||
@@ -242,6 +266,8 @@ void DescriptorSet::updateBuffer(uint32_t binding, uint32 index, Gfx::PShaderBuf
|
||||
});
|
||||
|
||||
cachedData[binding] = vulkanBuffer.getHandle();
|
||||
vulkanBuffer->getAlloc()->bind();
|
||||
boundResources.add(vulkanBuffer->getAlloc());
|
||||
}
|
||||
|
||||
void DescriptorSet::updateSampler(uint32_t binding, Gfx::PSampler samplerState) {
|
||||
@@ -300,6 +326,8 @@ void DescriptorSet::updateTexture(uint32_t binding, Gfx::PTexture texture, Gfx::
|
||||
});
|
||||
|
||||
cachedData[binding] = vulkanTexture;
|
||||
vulkanTexture->getHandle()->bind();
|
||||
boundResources.add(vulkanTexture->getHandle());
|
||||
}
|
||||
void DescriptorSet::updateTextureArray(uint32_t binding, Array<Gfx::PTexture> textures) {
|
||||
// maybe make this a parameter?
|
||||
@@ -326,6 +354,8 @@ void DescriptorSet::updateTextureArray(uint32_t binding, Array<Gfx::PTexture> te
|
||||
.descriptorType = descriptorType,
|
||||
.pImageInfo = &imageInfos.back(),
|
||||
});
|
||||
vulkanTexture->getHandle()->bind();
|
||||
boundResources.add(vulkanTexture->getHandle());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -366,7 +396,7 @@ void PipelineLayout::create() {
|
||||
for (size_t i = 0; i < pushConstants.size(); i++) {
|
||||
vkPushConstants[i].offset = pushConstants[i].offset;
|
||||
vkPushConstants[i].size = pushConstants[i].size;
|
||||
vkPushConstants[i].stageFlags = (VkShaderStageFlagBits)pushConstants[i].stageFlags;
|
||||
vkPushConstants[i].stageFlags = pushConstants[i].stageFlags;
|
||||
}
|
||||
|
||||
VkPipelineLayoutCreateInfo createInfo = {
|
||||
|
||||
@@ -23,7 +23,7 @@ private:
|
||||
DEFINE_REF(DescriptorLayout)
|
||||
|
||||
DECLARE_REF(DescriptorSet)
|
||||
class DescriptorPool : public Gfx::DescriptorPool {
|
||||
class DescriptorPool : public Gfx::DescriptorPool, public CommandBoundResource {
|
||||
public:
|
||||
DescriptorPool(PGraphics graphics, PDescriptorLayout layout);
|
||||
virtual ~DescriptorPool();
|
||||
@@ -43,7 +43,7 @@ private:
|
||||
};
|
||||
DEFINE_REF(DescriptorPool)
|
||||
|
||||
class DescriptorSet : public Gfx::DescriptorSet {
|
||||
class DescriptorSet : public Gfx::DescriptorSet, public CommandBoundResource {
|
||||
public:
|
||||
DescriptorSet(PGraphics graphics, PDescriptorPool owner);
|
||||
virtual ~DescriptorSet();
|
||||
@@ -55,10 +55,7 @@ public:
|
||||
virtual void updateTexture(uint32_t binding, Gfx::PTexture texture, Gfx::PSampler sampler = nullptr) override;
|
||||
virtual void updateTextureArray(uint32_t binding, Array<Gfx::PTexture> texture) override;
|
||||
|
||||
constexpr bool isCurrentlyBound() const { return bindCount > 0; }
|
||||
constexpr bool isCurrentlyInUse() const { return currentlyInUse; }
|
||||
constexpr void bind() { bindCount++; }
|
||||
constexpr void unbind() { bindCount--; }
|
||||
constexpr void allocate() { currentlyInUse = true; }
|
||||
constexpr void free() { currentlyInUse = false; }
|
||||
constexpr VkDescriptorSet getHandle() const { return setHandle; }
|
||||
@@ -71,6 +68,7 @@ private:
|
||||
// since the layout is fixed, trying to bind a texture to a buffer
|
||||
// would not work anyways, so casts should be safe
|
||||
Array<void*> cachedData;
|
||||
Array<PCommandBoundResource> boundResources;
|
||||
VkDescriptorSet setHandle;
|
||||
PGraphics graphics;
|
||||
PDescriptorPool owner;
|
||||
|
||||
@@ -404,7 +404,6 @@ void Graphics::initInstance(GraphicsInitializer initInfo)
|
||||
.apiVersion = VK_API_VERSION_1_2,
|
||||
};
|
||||
|
||||
|
||||
Array<const char*> extensions = getRequiredExtensions();
|
||||
for (uint32 i = 0; i < initInfo.instanceExtensions.size(); ++i)
|
||||
{
|
||||
@@ -452,11 +451,16 @@ void Graphics::pickPhysicalDevice()
|
||||
VkPhysicalDevice bestDevice = VK_NULL_HANDLE;
|
||||
uint32 deviceRating = 0;
|
||||
features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
|
||||
features.pNext = &features11;
|
||||
features.pNext = &robustness;
|
||||
robustness.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT;
|
||||
robustness.pNext = &features11;
|
||||
robustness.nullDescriptor = true;
|
||||
features11.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
|
||||
features11.pNext = &features12;
|
||||
features12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
|
||||
features12.pNext = nullptr;
|
||||
features12.pNext = &features13;
|
||||
features13.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES;
|
||||
features13.pNext = nullptr;
|
||||
for (auto dev : physicalDevices)
|
||||
{
|
||||
uint32 currentRating = 0;
|
||||
|
||||
@@ -101,6 +101,8 @@ protected:
|
||||
VkPhysicalDeviceFeatures2 features;
|
||||
VkPhysicalDeviceVulkan11Features features11;
|
||||
VkPhysicalDeviceVulkan12Features features12;
|
||||
VkPhysicalDeviceVulkan13Features features13;
|
||||
VkPhysicalDeviceRobustness2FeaturesEXT robustness;
|
||||
VkDebugUtilsMessengerEXT callback;
|
||||
Map<uint32, OFramebuffer> allocatedFramebuffers;
|
||||
VmaAllocator allocator;
|
||||
|
||||
@@ -207,7 +207,8 @@ RenderPass::RenderPass(PGraphics graphics, Gfx::RenderTargetLayout _layout, Arra
|
||||
|
||||
RenderPass::~RenderPass()
|
||||
{
|
||||
graphics->getDestructionManager()->queueRenderPass(graphics->getGraphicsCommands()->getCommands(), renderPass);
|
||||
vkDestroyRenderPass(graphics->getDevice(), renderPass, nullptr);
|
||||
//graphics->getDestructionManager()->queueRenderPass(graphics->getGraphicsCommands()->getCommands(), renderPass);
|
||||
}
|
||||
|
||||
uint32 RenderPass::getFramebufferHash()
|
||||
|
||||
@@ -20,9 +20,10 @@ Semaphore::Semaphore(PGraphics graphics)
|
||||
|
||||
Semaphore::~Semaphore()
|
||||
{
|
||||
graphics->getDestructionManager()->queueSemaphore(graphics->getGraphicsCommands()->getCommands(), handle);
|
||||
//graphics->getDestructionManager()->queueSemaphore(graphics->getGraphicsCommands()->getCommands(), handle);
|
||||
}
|
||||
|
||||
|
||||
Fence::Fence(PGraphics graphics)
|
||||
: graphics(graphics)
|
||||
, signaled(false)
|
||||
@@ -100,66 +101,19 @@ DestructionManager::~DestructionManager()
|
||||
{
|
||||
}
|
||||
|
||||
void DestructionManager::queueBuffer(PCommand cmd, VkBuffer buffer, VmaAllocation alloc)
|
||||
void DestructionManager::queueResourceForDestruction(OCommandBoundResource resource)
|
||||
{
|
||||
buffers[cmd].add({buffer, alloc});
|
||||
resources.add(std::move(resource));
|
||||
}
|
||||
|
||||
void DestructionManager::queueImage(PCommand cmd, VkImage image, VmaAllocation alloc)
|
||||
void DestructionManager::notifyCommandComplete()
|
||||
{
|
||||
images[cmd].add({image, alloc});
|
||||
}
|
||||
|
||||
void DestructionManager::queueImageView(PCommand cmd, VkImageView image)
|
||||
{
|
||||
views[cmd].add(image);
|
||||
}
|
||||
|
||||
void DestructionManager::queueSemaphore(PCommand cmd, VkSemaphore sem)
|
||||
{
|
||||
sems[cmd].add(sem);
|
||||
}
|
||||
|
||||
void DestructionManager::queueRenderPass(PCommand cmd, VkRenderPass renderPass)
|
||||
{
|
||||
renderPasses[cmd].add(renderPass);
|
||||
}
|
||||
|
||||
void DestructionManager::queueDescriptorPool(PCommand cmd, VkDescriptorPool pool)
|
||||
{
|
||||
pools[cmd].add(pool);
|
||||
}
|
||||
|
||||
void DestructionManager::notifyCmdComplete(PCommand cmd)
|
||||
{
|
||||
for(auto [buf, alloc] : buffers[cmd])
|
||||
for (size_t i = 0; i < resources.size(); ++i)
|
||||
{
|
||||
vmaDestroyBuffer(graphics->getAllocator(), buf, alloc);
|
||||
if(!resources[i]->isCurrentlyBound())
|
||||
{
|
||||
resources.removeAt(i, false);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
for(auto view : views[cmd])
|
||||
{
|
||||
vkDestroyImageView(graphics->getDevice(), view, nullptr);
|
||||
}
|
||||
for(auto [img, alloc] : images[cmd])
|
||||
{
|
||||
vmaDestroyImage(graphics->getAllocator(), img, alloc);
|
||||
}
|
||||
for (auto sem : sems[cmd])
|
||||
{
|
||||
vkDestroySemaphore(graphics->getDevice(), sem, nullptr);
|
||||
}
|
||||
for (auto pool : pools[cmd])
|
||||
{
|
||||
vkDestroyDescriptorPool(graphics->getDevice(), pool, nullptr);
|
||||
}
|
||||
for (auto renderPass : renderPasses[cmd])
|
||||
{
|
||||
vkDestroyRenderPass(graphics->getDevice(), renderPass, nullptr);
|
||||
}
|
||||
buffers[cmd].clear();
|
||||
images[cmd].clear();
|
||||
views[cmd].clear();
|
||||
sems[cmd].clear();
|
||||
pools[cmd].clear();
|
||||
renderPasses[cmd].clear();
|
||||
}
|
||||
|
||||
@@ -50,30 +50,41 @@ private:
|
||||
VkFence fence;
|
||||
};
|
||||
DEFINE_REF(Fence)
|
||||
|
||||
DECLARE_REF(CommandBoundResource)
|
||||
class DestructionManager
|
||||
{
|
||||
public:
|
||||
DestructionManager(PGraphics graphics);
|
||||
~DestructionManager();
|
||||
void queueBuffer(PCommand cmd, VkBuffer buffer, VmaAllocation alloc);
|
||||
void queueImage(PCommand cmd, VkImage image, VmaAllocation alloc);
|
||||
void queueImageView(PCommand cmd, VkImageView view);
|
||||
void queueSemaphore(PCommand cmd, VkSemaphore sem);
|
||||
void queueRenderPass(PCommand cmd, VkRenderPass renderPass);
|
||||
void queueDescriptorPool(PCommand cmd, VkDescriptorPool pool);
|
||||
void notifyCmdComplete(PCommand cmdbuffer);
|
||||
void queueResourceForDestruction(OCommandBoundResource resource);
|
||||
void notifyCommandComplete();
|
||||
private:
|
||||
PGraphics graphics;
|
||||
Map<PCommand, List<Pair<VkBuffer, VmaAllocation>>> buffers;
|
||||
Map<PCommand, List<Pair<VkImage, VmaAllocation>>> images;
|
||||
Map<PCommand, List<VkImageView>> views;
|
||||
Map<PCommand, List<VkSemaphore>> sems;
|
||||
Map<PCommand, List<VkRenderPass>> renderPasses;
|
||||
Map<PCommand, List<VkDescriptorPool>> pools;
|
||||
Array<OCommandBoundResource> resources;
|
||||
};
|
||||
DEFINE_REF(DestructionManager)
|
||||
|
||||
class CommandBoundResource
|
||||
{
|
||||
public:
|
||||
CommandBoundResource(PGraphics graphics)
|
||||
: graphics(graphics)
|
||||
{
|
||||
}
|
||||
virtual ~CommandBoundResource()
|
||||
{
|
||||
if (isCurrentlyBound())
|
||||
abort();
|
||||
}
|
||||
constexpr bool isCurrentlyBound() const { return bindCount > 0; }
|
||||
constexpr void bind() { bindCount++; }
|
||||
constexpr void unbind() { bindCount--; }
|
||||
protected:
|
||||
PGraphics graphics;
|
||||
uint64 bindCount = 0;
|
||||
};
|
||||
DEFINE_REF(CommandBoundResource)
|
||||
|
||||
class Sampler : public Gfx::Sampler
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -25,6 +25,21 @@ VkImageAspectFlags getAspectFromFormat(Gfx::SeFormat format)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TextureHandle::TextureHandle(PGraphics graphics)
|
||||
: CommandBoundResource(graphics)
|
||||
{
|
||||
}
|
||||
|
||||
TextureHandle::~TextureHandle()
|
||||
{
|
||||
vkDestroyImageView(graphics->getDevice(), imageView, nullptr);
|
||||
if (ownsImage)
|
||||
{
|
||||
vmaDestroyImage(graphics->getAllocator(), image, allocation);
|
||||
}
|
||||
}
|
||||
|
||||
TextureBase::TextureBase(PGraphics graphics, VkImageViewType viewType,
|
||||
const TextureCreateInfo& createInfo, Gfx::QueueType& owner, VkImage existingImage)
|
||||
: currentOwner(owner)
|
||||
@@ -38,14 +53,15 @@ TextureBase::TextureBase(PGraphics graphics, VkImageViewType viewType,
|
||||
, samples(createInfo.samples)
|
||||
, format(createInfo.format)
|
||||
, usage(createInfo.usage)
|
||||
, image(existingImage)
|
||||
, handle(new TextureHandle(graphics))
|
||||
, aspect(getAspectFromFormat(createInfo.format))
|
||||
, layout(Gfx::SE_IMAGE_LAYOUT_UNDEFINED)
|
||||
, ownsImage(false)
|
||||
{
|
||||
handle->image = existingImage;
|
||||
handle->ownsImage = false;
|
||||
if (existingImage == VK_NULL_HANDLE)
|
||||
{
|
||||
ownsImage = true;
|
||||
handle->ownsImage = true;
|
||||
VkImageType type = VK_IMAGE_TYPE_MAX_ENUM;
|
||||
VkImageCreateFlags flags = 0;
|
||||
switch (viewType)
|
||||
@@ -99,7 +115,7 @@ TextureBase::TextureBase(PGraphics graphics, VkImageViewType viewType,
|
||||
VmaAllocationCreateInfo allocInfo = {
|
||||
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||
};
|
||||
VK_CHECK(vmaCreateImage(graphics->getAllocator(), &info, &allocInfo, &image, &allocation, nullptr));
|
||||
VK_CHECK(vmaCreateImage(graphics->getAllocator(), &info, &allocInfo, &handle->image, &handle->allocation, nullptr));
|
||||
}
|
||||
|
||||
const DataSource& sourceData = createInfo.sourceData;
|
||||
@@ -109,9 +125,7 @@ TextureBase::TextureBase(PGraphics graphics, VkImageViewType viewType,
|
||||
VK_ACCESS_NONE, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
|
||||
VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
|
||||
void* data;
|
||||
VkMemoryPropertyFlags memProps;
|
||||
VkBuffer stagingBuffer = VK_NULL_HANDLE;
|
||||
VmaAllocation stagingAlloc = VK_NULL_HANDLE;
|
||||
OBufferAllocation stagingAlloc = new BufferAllocation(graphics);
|
||||
VkBufferCreateInfo stagingInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
||||
.pNext = nullptr,
|
||||
@@ -124,11 +138,12 @@ TextureBase::TextureBase(PGraphics graphics, VkImageViewType viewType,
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||
};
|
||||
VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &stagingInfo, &alloc, &stagingBuffer, &stagingAlloc, nullptr));
|
||||
vmaMapMemory(graphics->getAllocator(), stagingAlloc, &data);
|
||||
VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &stagingInfo, &alloc, &stagingAlloc->buffer, &stagingAlloc->allocation, nullptr));
|
||||
vmaMapMemory(graphics->getAllocator(), stagingAlloc->allocation, &data);
|
||||
vmaSetAllocationName(graphics->getAllocator(), stagingAlloc->allocation, "TextureStaging");
|
||||
|
||||
std::memcpy(data, sourceData.data, sourceData.size);
|
||||
vmaUnmapMemory(graphics->getAllocator(), stagingAlloc);
|
||||
vmaUnmapMemory(graphics->getAllocator(), stagingAlloc->allocation);
|
||||
|
||||
PCommandPool commandPool = graphics->getQueueCommands(currentOwner);
|
||||
VkBufferImageCopy region = {
|
||||
@@ -154,14 +169,14 @@ TextureBase::TextureBase(PGraphics graphics, VkImageViewType viewType,
|
||||
};
|
||||
|
||||
vkCmdCopyBufferToImage(commandPool->getCommands()->getHandle(),
|
||||
stagingBuffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
|
||||
stagingAlloc->buffer, handle->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
|
||||
|
||||
commandPool->getCommands()->bindResource(PBufferAllocation(stagingAlloc));
|
||||
// When loading a texture from a file, we will almost always use it as a texture map for fragment shaders
|
||||
changeLayout(Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
||||
VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
|
||||
VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
|
||||
|
||||
graphics->getDestructionManager()->queueBuffer(commandPool->getCommands(), stagingBuffer, stagingAlloc);
|
||||
graphics->getDestructionManager()->queueResourceForDestruction(std::move(stagingAlloc));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -188,7 +203,7 @@ TextureBase::TextureBase(PGraphics graphics, VkImageViewType viewType,
|
||||
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
|
||||
.pNext = nullptr,
|
||||
.flags = 0,
|
||||
.image = image,
|
||||
.image = handle->image,
|
||||
.viewType = viewType,
|
||||
.format = cast(format),
|
||||
.subresourceRange = {
|
||||
@@ -198,18 +213,23 @@ TextureBase::TextureBase(PGraphics graphics, VkImageViewType viewType,
|
||||
},
|
||||
};
|
||||
|
||||
VK_CHECK(vkCreateImageView(graphics->getDevice(), &viewInfo, nullptr, &imageView));
|
||||
VK_CHECK(vkCreateImageView(graphics->getDevice(), &viewInfo, nullptr, &handle->imageView));
|
||||
}
|
||||
|
||||
TextureBase::~TextureBase()
|
||||
{
|
||||
if (ownsImage)
|
||||
{
|
||||
graphics->getDestructionManager()->queueImage(graphics->getQueueCommands(currentOwner)->getCommands(), image, allocation);
|
||||
}
|
||||
graphics->getDestructionManager()->queueImageView(graphics->getQueueCommands(currentOwner)->getCommands(), imageView);
|
||||
graphics->getDestructionManager()->queueResourceForDestruction(std::move(handle));
|
||||
}
|
||||
|
||||
VkImage TextureBase::getImage() const
|
||||
{
|
||||
return handle->image;
|
||||
}
|
||||
|
||||
VkImageView TextureBase::getView() const
|
||||
{
|
||||
return handle->imageView;
|
||||
}
|
||||
|
||||
void TextureBase::changeLayout(Gfx::SeImageLayout newLayout,
|
||||
VkAccessFlags srcAccess, VkPipelineStageFlags srcStage,
|
||||
@@ -224,7 +244,7 @@ void TextureBase::changeLayout(Gfx::SeImageLayout newLayout,
|
||||
.newLayout = cast(newLayout),
|
||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.image = image,
|
||||
.image = handle->image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = aspect,
|
||||
.baseMipLevel = 0,
|
||||
@@ -237,6 +257,7 @@ void TextureBase::changeLayout(Gfx::SeImageLayout newLayout,
|
||||
vkCmdPipelineBarrier(commandPool->getCommands()->getHandle(),
|
||||
srcStage, dstStage,
|
||||
0, 0, nullptr, 0, nullptr, 1, &barrier);
|
||||
commandPool->getCommands()->bindResource(PTextureHandle(handle));
|
||||
commandPool->submitCommands();
|
||||
layout = newLayout;
|
||||
}
|
||||
@@ -250,8 +271,7 @@ void TextureBase::download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Arra
|
||||
VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
|
||||
VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
|
||||
void* data;
|
||||
VkBuffer stagingBuffer = VK_NULL_HANDLE;
|
||||
VmaAllocation stagingAlloc;
|
||||
OBufferAllocation stagingAlloc = new BufferAllocation(graphics);
|
||||
// always create a staging buffer since we do buffer -> image copy and the image may be in any tiling format
|
||||
VkBufferCreateInfo stagingInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
||||
@@ -265,7 +285,8 @@ void TextureBase::download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Arra
|
||||
.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||
};
|
||||
VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &stagingInfo, &alloc, &stagingBuffer, &stagingAlloc, nullptr));
|
||||
VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &stagingInfo, &alloc, &stagingAlloc->buffer, &stagingAlloc->allocation, nullptr));
|
||||
vmaSetAllocationName(graphics->getAllocator(), stagingAlloc->allocation, "DownloadBuffer");
|
||||
|
||||
PCommand cmdBuffer = graphics->getQueueCommands(currentOwner)->getCommands();
|
||||
//Gfx::FormatCompatibilityInfo formatInfo = Gfx::getFormatInfo(format);
|
||||
@@ -290,15 +311,16 @@ void TextureBase::download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Arra
|
||||
.depth = depth
|
||||
},
|
||||
};
|
||||
vkCmdCopyImageToBuffer(cmdBuffer->getHandle(), image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, stagingBuffer, 1, ®ion);
|
||||
vkCmdCopyImageToBuffer(cmdBuffer->getHandle(), handle->image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, stagingAlloc->buffer, 1, ®ion);
|
||||
cmdBuffer->bindResource(PBufferAllocation(stagingAlloc));
|
||||
changeLayout(prevLayout,
|
||||
VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
|
||||
VK_ACCESS_MEMORY_READ_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
|
||||
VK_CHECK(vmaMapMemory(graphics->getAllocator(), stagingAlloc, &data));
|
||||
VK_CHECK(vmaMapMemory(graphics->getAllocator(), stagingAlloc->allocation, &data));
|
||||
buffer.resize(imageSize);
|
||||
std::memcpy(buffer.data(), data, buffer.size());
|
||||
vmaUnmapMemory(graphics->getAllocator(), stagingAlloc);
|
||||
graphics->getDestructionManager()->queueBuffer(cmdBuffer, stagingBuffer, stagingAlloc);
|
||||
vmaUnmapMemory(graphics->getAllocator(), stagingAlloc->allocation);
|
||||
graphics->getDestructionManager()->queueResourceForDestruction(std::move(stagingAlloc));
|
||||
}
|
||||
|
||||
void TextureBase::executeOwnershipBarrier(Gfx::QueueType newOwner)
|
||||
@@ -311,7 +333,7 @@ void TextureBase::executeOwnershipBarrier(Gfx::QueueType newOwner)
|
||||
.newLayout = cast(layout),
|
||||
.srcQueueFamilyIndex = mapping.getQueueTypeFamilyIndex(currentOwner),
|
||||
.dstQueueFamilyIndex = mapping.getQueueTypeFamilyIndex(newOwner),
|
||||
.image = image,
|
||||
.image = handle->image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = aspect,
|
||||
.baseMipLevel = 0,
|
||||
@@ -360,6 +382,8 @@ void TextureBase::executeOwnershipBarrier(Gfx::QueueType newOwner)
|
||||
|
||||
VkCommandBuffer sourceCmd = sourcePool->getCommands()->getHandle();
|
||||
VkCommandBuffer destCmd = dstPool->getCommands()->getHandle();
|
||||
sourcePool->getCommands()->bindResource(PTextureHandle(handle));
|
||||
dstPool->getCommands()->bindResource(PTextureHandle(handle));
|
||||
vkCmdPipelineBarrier(sourceCmd, srcStage, srcStage, 0, 0, nullptr, 0, nullptr, 1, &imageBarrier);
|
||||
vkCmdPipelineBarrier(destCmd, dstStage, dstStage, 0, 0, nullptr, 0, nullptr, 1, &imageBarrier);
|
||||
currentOwner = newOwner;
|
||||
@@ -378,7 +402,7 @@ void TextureBase::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStag
|
||||
.newLayout = cast(layout),
|
||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.image = image,
|
||||
.image = handle->image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = aspect,
|
||||
.baseMipLevel = 0,
|
||||
@@ -389,6 +413,7 @@ void TextureBase::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStag
|
||||
};
|
||||
PCommand command = graphics->getQueueCommands(currentOwner)->getCommands();
|
||||
vkCmdPipelineBarrier(command->getHandle(), srcStage, dstStage, 0, 0, nullptr, 0, nullptr, 1, &barrier);
|
||||
command->bindResource(PTextureHandle(handle));
|
||||
}
|
||||
|
||||
Texture2D::Texture2D(PGraphics graphics, const TextureCreateInfo& createInfo, VkImage existingImage)
|
||||
|
||||
@@ -1,11 +1,23 @@
|
||||
#pragma once
|
||||
#include "Graphics/Texture.h"
|
||||
#include "Graphics.h"
|
||||
#include "Resources.h"
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
namespace Vulkan
|
||||
{
|
||||
class TextureHandle : public CommandBoundResource
|
||||
{
|
||||
public:
|
||||
TextureHandle(PGraphics graphics);
|
||||
virtual ~TextureHandle();
|
||||
VkImage image;
|
||||
VkImageView imageView;
|
||||
VmaAllocation allocation;
|
||||
uint8 ownsImage;
|
||||
};
|
||||
DECLARE_REF(TextureHandle)
|
||||
class TextureBase
|
||||
{
|
||||
public:
|
||||
@@ -24,14 +36,12 @@ public:
|
||||
{
|
||||
return depth;
|
||||
}
|
||||
constexpr VkImage getImage() const
|
||||
PTextureHandle getHandle() const
|
||||
{
|
||||
return image;
|
||||
}
|
||||
constexpr VkImageView getView() const
|
||||
{
|
||||
return imageView;
|
||||
return handle;
|
||||
}
|
||||
VkImage getImage() const;
|
||||
VkImageView getView() const;
|
||||
constexpr Gfx::SeImageLayout getLayout() const
|
||||
{
|
||||
return layout;
|
||||
@@ -73,10 +83,10 @@ public:
|
||||
void download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Array<uint8>& buffer);
|
||||
|
||||
protected:
|
||||
OTextureHandle handle;
|
||||
//Updates via reference
|
||||
Gfx::QueueType& currentOwner;
|
||||
PGraphics graphics;
|
||||
VmaAllocation allocation;
|
||||
uint32 width;
|
||||
uint32 height;
|
||||
uint32 depth;
|
||||
@@ -86,11 +96,8 @@ protected:
|
||||
uint32 samples;
|
||||
Gfx::SeFormat format;
|
||||
Gfx::SeImageUsageFlags usage;
|
||||
VkImage image;
|
||||
VkImageView imageView;
|
||||
VkImageAspectFlags aspect;
|
||||
Gfx::SeImageLayout layout;
|
||||
uint8 ownsImage;
|
||||
friend class Graphics;
|
||||
};
|
||||
DEFINE_REF(TextureBase)
|
||||
|
||||
@@ -14,6 +14,12 @@ double Gfx::getCurrentFrameDelta()
|
||||
return currentFrameDelta;
|
||||
}
|
||||
|
||||
uint32 currentFrameIndex = 0;
|
||||
uint32 Gfx::getCurrentFrameIndex()
|
||||
{
|
||||
return currentFrameIndex;
|
||||
}
|
||||
|
||||
void glfwKeyCallback(GLFWwindow* handle, int key, int, int action, int modifier)
|
||||
{
|
||||
if (key == -1)
|
||||
@@ -143,7 +149,8 @@ void Window::endFrame()
|
||||
VK_CHECK(r);
|
||||
}
|
||||
currentSemaphoreIndex = (currentSemaphoreIndex + 1) % Gfx::numFramesBuffered;
|
||||
graphics->waitDeviceIdle();
|
||||
currentFrameIndex = currentSemaphoreIndex;
|
||||
//graphics->waitDeviceIdle();
|
||||
}
|
||||
|
||||
void Window::onWindowCloseEvent()
|
||||
|
||||
Reference in New Issue
Block a user