Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions lib/graphics_engine/include/ge_vulkan_driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ namespace GE
GVS_2D_RENDER,
GVS_COUNT,
};
enum GEVulkanQueueIndices
{
GVQI_MIN = 0,
GVQI_GRAPHICS = GVQI_MIN,
GVQI_COMPUTE,
GVQI_TRANSFER,
GVQI_COUNT
};
class GEVulkanDriver : public video::CNullDriver
{
public:
Expand Down Expand Up @@ -342,10 +350,9 @@ namespace GE
createSwapChainRelated(false/*handle_surface*/);
}
void updateDriver(bool reload_shaders = false);
uint32_t getGraphicsFamily() const { return m_graphics_family; }
unsigned getGraphicsQueueCount() const
{ return m_graphics_queue_count; }
std::unique_lock<std::mutex> getGraphicsQueue(VkQueue* queue) const;
uint32_t getQueueFamily(uint32_t queue_index) const;
uint32_t getQueueCount(uint32_t queue_index) const;
std::unique_lock<std::mutex> getQueue(VkQueue* queue, uint32_t index) const;
void waitIdle(bool flush_command_loader = false);
void setDisableWaitIdle(bool val) { m_disable_wait_idle = val; }
IrrlichtDevice* getIrrlichtDevice() const { return m_irrlicht_device; }
Expand Down Expand Up @@ -483,13 +490,15 @@ namespace GE
VkSurfaceCapabilitiesKHR m_surface_capabilities;
std::vector<VkSurfaceFormatKHR> m_surface_formats;
std::vector<VkPresentModeKHR> m_present_modes;
std::vector<VkQueue> m_graphics_queue;
VkQueue m_present_queue;
mutable std::vector<std::mutex*> m_graphics_queue_mutexes;

uint32_t m_graphics_family;
std::vector<VkQueue> m_queues[GVQI_COUNT];
uint32_t m_queue_count[GVQI_COUNT];
mutable std::vector<std::mutex*> m_queue_mutexes[GVQI_COUNT];
uint32_t m_queue_family[GVQI_COUNT];
uint32_t m_queue_family_count;
VkQueue m_present_queue;
uint32_t m_present_family;
unsigned m_graphics_queue_count;

VkPhysicalDeviceProperties m_properties;
VkPhysicalDeviceFeatures m_features;

Expand Down Expand Up @@ -522,7 +531,8 @@ namespace GE
void createInstance(SDL_Window* window);
void findPhysicalDevice();
bool checkDeviceExtensions(VkPhysicalDevice device);
bool findQueueFamilies(VkPhysicalDevice device, uint32_t* graphics_family, unsigned* graphics_queue_count, uint32_t* present_family);
bool findQueueFamilies(VkPhysicalDevice device, VkQueueFlags required_queue_flags, VkQueueFlags ignored_queue_flags,
uint32_t* family, unsigned* queue_count, uint32_t* present_family = NULL);
bool updateSurfaceInformation(VkPhysicalDevice device,
VkSurfaceCapabilitiesKHR* surface_capabilities,
std::vector<VkSurfaceFormatKHR>* surface_formats,
Expand Down
4 changes: 2 additions & 2 deletions lib/graphics_engine/src/ge_vulkan_array_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void GEVulkanArrayTexture::reloadInternal(const std::vector<io::path>& list,
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT))
goto destroy;

command_buffer = GEVulkanCommandLoader::beginSingleTimeCommands();
command_buffer = GEVulkanCommandLoader::beginSingleTimeCommands(GVQI_TRANSFER);

transitionImageLayout(command_buffer, VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
Expand All @@ -239,7 +239,7 @@ void GEVulkanArrayTexture::reloadInternal(const std::vector<io::path>& list,
transitionImageLayout(command_buffer, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);

GEVulkanCommandLoader::endSingleTimeCommands(command_buffer);
GEVulkanCommandLoader::endSingleTimeCommands(command_buffer, GVQI_TRANSFER);

createImageView(VK_IMAGE_ASPECT_COLOR_BIT);

Expand Down
93 changes: 51 additions & 42 deletions lib/graphics_engine/src/ge_vulkan_command_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ std::deque<std::function<void()> > g_threaded_commands;
thread_local int g_loader_id = 0;
std::atomic_uint g_loader_count(0);

std::vector<VkCommandPool> g_command_pools;
std::vector<VkFence> g_command_fences;
std::vector<VkCommandPool> g_command_pools[GVQI_COUNT];
std::vector<VkFence> g_command_fences[GVQI_COUNT];
std::vector<std::unique_ptr<std::atomic<bool> > > g_thread_idle;
} // GEVulkanCommandLoader

Expand All @@ -62,29 +62,32 @@ void GEVulkanCommandLoader::init(GEVulkanDriver* vk)
else
thread_count += 3;

g_command_pools.resize(thread_count);
g_command_fences.resize(thread_count);
for (unsigned i = 0; i < thread_count; i++)
for (int i = GVQI_MIN; i < GVQI_COUNT; i++)
{
VkCommandPoolCreateInfo pool_info = {};
pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
pool_info.queueFamilyIndex = g_vk->getGraphicsFamily();
pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
VkResult result = vkCreateCommandPool(g_vk->getDevice(), &pool_info,
NULL, &g_command_pools[i]);
if (result != VK_SUCCESS)
g_command_pools[i].resize(thread_count);
g_command_fences[i].resize(thread_count);
for (unsigned j = 0; j < thread_count; j++)
{
throw std::runtime_error(
"GEVulkanCommandLoader: vkCreateCommandPool failed");
}
VkFenceCreateInfo fence_info = {};
fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
result = vkCreateFence(g_vk->getDevice(), &fence_info, NULL,
&g_command_fences[i]);
if (result != VK_SUCCESS)
{
throw std::runtime_error(
"GEVulkanCommandLoader: vkCreateFence failed");
VkCommandPoolCreateInfo pool_info = {};
pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
pool_info.queueFamilyIndex = g_vk->getQueueFamily(i);
pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
VkResult result = vkCreateCommandPool(g_vk->getDevice(), &pool_info,
NULL, &g_command_pools[i][j]);
if (result != VK_SUCCESS)
{
throw std::runtime_error(
"GEVulkanCommandLoader: vkCreateCommandPool failed");
}
VkFenceCreateInfo fence_info = {};
fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
result = vkCreateFence(g_vk->getDevice(), &fence_info, NULL,
&g_command_fences[i][j]);
if (result != VK_SUCCESS)
{
throw std::runtime_error(
"GEVulkanCommandLoader: vkCreateFence failed");
}
}
}

Expand Down Expand Up @@ -123,7 +126,7 @@ void GEVulkanCommandLoader::init(GEVulkanDriver* vk)

char thread_count_str[40] = {};
snprintf(thread_count_str, 40, "%d threads used, %d graphics queue(s)",
thread_count - 1, vk->getGraphicsQueueCount());
thread_count - 1, vk->getQueueCount(GVQI_GRAPHICS));
os::Printer::log("Vulkan command loader", thread_count_str);
} // init

Expand All @@ -146,12 +149,18 @@ void GEVulkanCommandLoader::destroy()
g_threaded_commands.clear();
g_thread_idle.clear();

for (VkCommandPool& pool : g_command_pools)
vkDestroyCommandPool(g_vk->getDevice(), pool, NULL);
g_command_pools.clear();
for (VkFence& fence : g_command_fences)
vkDestroyFence(g_vk->getDevice(), fence, NULL);
g_command_fences.clear();
for (int i = GVQI_MIN; i < GVQI_COUNT; i++)
{
for (VkCommandPool& pool : g_command_pools[i])
vkDestroyCommandPool(g_vk->getDevice(), pool, NULL);
g_command_pools[i].clear();
}
for (int i = GVQI_MIN; i < GVQI_COUNT; i++)
{
for (VkFence& fence : g_command_fences[i])
vkDestroyFence(g_vk->getDevice(), fence, NULL);
g_command_fences[i].clear();
}
} // destroy

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -179,15 +188,15 @@ int GEVulkanCommandLoader::getLoaderId()
} // getLoaderId

// ----------------------------------------------------------------------------
VkCommandPool GEVulkanCommandLoader::getCurrentCommandPool()
VkCommandPool GEVulkanCommandLoader::getCurrentCommandPool(uint32_t index)
{
return g_command_pools[g_loader_id];
return g_command_pools[index][g_loader_id];
} // getCurrentCommandPool

// ----------------------------------------------------------------------------
VkFence GEVulkanCommandLoader::getCurrentFence()
VkFence GEVulkanCommandLoader::getCurrentFence(uint32_t index)
{
return g_command_fences[g_loader_id];
return g_command_fences[index][g_loader_id];
} // getCurrentFence

// ----------------------------------------------------------------------------
Expand All @@ -201,12 +210,12 @@ void GEVulkanCommandLoader::addMultiThreadingCommand(std::function<void()> cmd)
} // addMultiThreadingCommand

// ----------------------------------------------------------------------------
VkCommandBuffer GEVulkanCommandLoader::beginSingleTimeCommands()
VkCommandBuffer GEVulkanCommandLoader::beginSingleTimeCommands(uint32_t index)
{
VkCommandBufferAllocateInfo alloc_info = {};
alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
alloc_info.commandPool = g_command_pools[g_loader_id];
alloc_info.commandPool = g_command_pools[index][g_loader_id];
alloc_info.commandBufferCount = 1;

VkCommandBuffer command_buffer;
Expand All @@ -222,7 +231,7 @@ VkCommandBuffer GEVulkanCommandLoader::beginSingleTimeCommands()

// ----------------------------------------------------------------------------
void GEVulkanCommandLoader::endSingleTimeCommands(VkCommandBuffer command_buffer,
VkQueueFlagBits bit)
uint32_t index)
{
vkEndCommandBuffer(command_buffer);

Expand All @@ -233,14 +242,14 @@ void GEVulkanCommandLoader::endSingleTimeCommands(VkCommandBuffer command_buffer

const int loader_id = g_loader_id;
VkQueue queue = VK_NULL_HANDLE;
std::unique_lock<std::mutex> lock = g_vk->getGraphicsQueue(&queue);
vkQueueSubmit(queue, 1, &submit_info, g_command_fences[loader_id]);
std::unique_lock<std::mutex> lock = g_vk->getQueue(&queue, GVQI_GRAPHICS);
vkQueueSubmit(queue, 1, &submit_info, g_command_fences[index][loader_id]);
lock.unlock();

vkWaitForFences(g_vk->getDevice(), 1, &g_command_fences[loader_id],
vkWaitForFences(g_vk->getDevice(), 1, &g_command_fences[index][loader_id],
VK_TRUE, std::numeric_limits<uint64_t>::max());
vkResetFences(g_vk->getDevice(), 1, &g_command_fences[loader_id]);
vkFreeCommandBuffers(g_vk->getDevice(), g_command_pools[loader_id], 1,
vkResetFences(g_vk->getDevice(), 1, &g_command_fences[index][loader_id]);
vkFreeCommandBuffers(g_vk->getDevice(), g_command_pools[index][loader_id], 1,
&command_buffer);
} // endSingleTimeCommands

Expand Down
8 changes: 4 additions & 4 deletions lib/graphics_engine/src/ge_vulkan_command_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ unsigned getLoaderCount();
// ----------------------------------------------------------------------------
int getLoaderId();
// ----------------------------------------------------------------------------
VkCommandPool getCurrentCommandPool();
VkCommandPool getCurrentCommandPool(uint32_t index);
// ----------------------------------------------------------------------------
VkFence getCurrentFence();
VkFence getCurrentFence(uint32_t index);
// ----------------------------------------------------------------------------
void addMultiThreadingCommand(std::function<void()> cmd);
// ----------------------------------------------------------------------------
VkCommandBuffer beginSingleTimeCommands();
VkCommandBuffer beginSingleTimeCommands(uint32_t index);
// ----------------------------------------------------------------------------
void endSingleTimeCommands(VkCommandBuffer command_buffer,
VkQueueFlagBits bit = VK_QUEUE_GRAPHICS_BIT);
uint32_t index);
// ----------------------------------------------------------------------------
void waitIdle();
}; // GEVulkanCommandLoader
Expand Down
Loading