Skip to content

Commit

Permalink
feat(linux): add dedicated queue for buffer transfer operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Pacheco95 committed Nov 23, 2023
1 parent 558a061 commit 3588e5e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
31 changes: 23 additions & 8 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,18 @@ void Application::drawFrame() {
.setSwapchains(m_swapChain)
.setImageIndices(imageIndex);

result = m_presentQueue.presentKHR(presentInfo);
result = vk::Result(vkQueuePresentKHR(
m_presentQueue, reinterpret_cast<const VkPresentInfoKHR*>(&presentInfo)
));

if (result == vk::Result::eErrorOutOfDateKHR ||
result == vk::Result::eSuboptimalKHR || m_framebufferResized) {
m_framebufferResized = false;
recreateSwapChain();
} else {
vk::resultCheck(result, "Failed to present swap chain image");
}

vk::resultCheck(result, "Failed to present swap chain image");

m_currentFrame = (m_currentFrame + 1) % Config::MAX_FRAMES_IN_FLIGHT;
}

Expand All @@ -142,6 +144,7 @@ void Application::cleanup() {
}
m_device.destroy(m_vertexBuffer);
m_device.free(m_vertexBufferMemory);
m_device.destroy(m_transferCommandPool);
m_device.destroy(m_commandPool);
m_device.destroy(m_graphicsPipeline);
m_device.destroy(m_pipelineLayout);
Expand Down Expand Up @@ -239,6 +242,7 @@ void Application::createLogicalDevice() {

m_graphicsQueue = m_device.getQueue(indices.graphicsFamily.value(), 0);
m_presentQueue = m_device.getQueue(indices.presentFamily.value(), 0);
m_transferQueue = m_device.getQueue(indices.transferFamily.value(), 0);
}

void Application::createSwapChain() {
Expand Down Expand Up @@ -504,7 +508,13 @@ void Application::createCommandPool() {
.setFlags(vk::CommandPoolCreateFlagBits::eResetCommandBuffer)
.setQueueFamilyIndex(queueFamilyIndices.graphicsFamily.value());

const auto transferPoolInfo =
vk::CommandPoolCreateInfo()
.setFlags(vk::CommandPoolCreateFlagBits::eTransient)
.setQueueFamilyIndex(queueFamilyIndices.transferFamily.value());

m_commandPool = m_device.createCommandPool(poolInfo);
m_transferCommandPool = m_device.createCommandPool(transferPoolInfo);
}

void Application::createVertexBuffer() {
Expand Down Expand Up @@ -740,10 +750,15 @@ void Application::createBuffer(
vk::Buffer& buffer,
vk::DeviceMemory& bufferMemory
) {
const auto indices = QueueFamily::findIndices(m_physicalDevice, m_surface);

vk::BufferCreateInfo bufferInfo;
const auto bufferInfoFamilyIndices = {
indices.graphicsFamily.value(), indices.transferFamily.value()};
bufferInfo.size = size;
bufferInfo.usage = usage;
bufferInfo.sharingMode = vk::SharingMode::eExclusive;
bufferInfo.sharingMode = vk::SharingMode::eConcurrent;
bufferInfo.setQueueFamilyIndices(bufferInfoFamilyIndices);

buffer = m_device.createBuffer(bufferInfo);

Expand All @@ -767,7 +782,7 @@ void Application::copyBuffer(
) {
vk::CommandBufferAllocateInfo allocInfo;
allocInfo.level = vk::CommandBufferLevel::ePrimary;
allocInfo.commandPool = m_commandPool;
allocInfo.commandPool = m_transferCommandPool;
allocInfo.commandBufferCount = 1;

vk::CommandBuffer commandBuffer;
Expand All @@ -788,8 +803,8 @@ void Application::copyBuffer(
vk::SubmitInfo submitInfo;
submitInfo.setCommandBuffers(commandBuffer);

m_graphicsQueue.submit(submitInfo, nullptr);
m_graphicsQueue.waitIdle();
m_transferQueue.submit(submitInfo, nullptr);
m_transferQueue.waitIdle();

m_device.freeCommandBuffers(m_commandPool, commandBuffer);
m_device.freeCommandBuffers(m_transferCommandPool, commandBuffer);
}
2 changes: 2 additions & 0 deletions src/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Application {
vk::Device m_device;
vk::Queue m_graphicsQueue; // Destroyed by logical device
vk::Queue m_presentQueue; // Destroyed by logical device
vk::Queue m_transferQueue; // Destroyed by logical device
vk::SwapchainKHR m_swapChain;
std::vector<vk::Image> m_swapChainImages; // Destroyed by swap chain
vk::Format m_swapChainImageFormat;
Expand All @@ -41,6 +42,7 @@ class Application {
vk::Pipeline m_graphicsPipeline;
std::vector<vk::Framebuffer> m_swapChainFrameBuffers;
vk::CommandPool m_commandPool;
vk::CommandPool m_transferCommandPool;
vk::Buffer m_vertexBuffer;
vk::DeviceMemory m_vertexBufferMemory;
std::vector<vk::CommandBuffer> m_commandBuffers; // Destroyed by command pool
Expand Down
19 changes: 16 additions & 3 deletions src/QueueFamily.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
using Indices = QueueFamily::Indices;

bool Indices::isComplete() const {
return graphicsFamily.has_value() && presentFamily.has_value();
return graphicsFamily.has_value() && presentFamily.has_value() &&
transferFamily.has_value();
}

Indices::QueueCreateInfos QueueFamily::Indices::getQueueCreateInfos() const {
std::set<uint32_t> uniqueFamilies = {
graphicsFamily.value(), presentFamily.value()};
graphicsFamily.value(),
presentFamily.value(),
transferFamily.has_value()};

float queuePriority = 1.0f;
QueueCreateInfos queueCreateInfos;
Expand All @@ -36,10 +39,20 @@ Indices QueueFamily::findIndices(
const auto families = device.getQueueFamilyProperties();

for (uint32_t familyIndex = 0; familyIndex < families.size(); ++familyIndex) {
if (families[familyIndex].queueFlags & vk::QueueFlagBits::eGraphics) {
const auto isGraphicsFamily =
families[familyIndex].queueFlags & vk::QueueFlagBits::eGraphics;

const auto isTransferFamily =
families[familyIndex].queueFlags & vk::QueueFlagBits::eTransfer;

if (isGraphicsFamily) {
indices.graphicsFamily = familyIndex;
}

if (isTransferFamily && !isGraphicsFamily) {
indices.transferFamily = familyIndex;
}

if (device.getSurfaceSupportKHR(familyIndex, surface)) {
indices.presentFamily = familyIndex;
}
Expand Down
1 change: 1 addition & 0 deletions src/QueueFamily.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct QueueFamily {

std::optional<uint32_t> graphicsFamily;
std::optional<uint32_t> presentFamily;
std::optional<uint32_t> transferFamily;

[[nodiscard]] bool isComplete() const;

Expand Down

0 comments on commit 3588e5e

Please sign in to comment.