Split KRDevice::initialize into multiple helper functions
This commit is contained in:
@@ -118,8 +118,9 @@ void KRDevice::destroy()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool KRDevice::initialize(const std::vector<const char*>& deviceExtensions)
|
bool KRDevice::getAndCheckDeviceCapabilities(const std::vector<const char*>& deviceExtensions)
|
||||||
{
|
{
|
||||||
|
|
||||||
vkGetPhysicalDeviceProperties(m_device, &m_deviceProperties);
|
vkGetPhysicalDeviceProperties(m_device, &m_deviceProperties);
|
||||||
vkGetPhysicalDeviceFeatures(m_device, &m_deviceFeatures);
|
vkGetPhysicalDeviceFeatures(m_device, &m_deviceFeatures);
|
||||||
uint32_t extensionCount;
|
uint32_t extensionCount;
|
||||||
@@ -141,7 +142,11 @@ bool KRDevice::initialize(const std::vector<const char*>& deviceExtensions)
|
|||||||
// Anisotropy feature required
|
// Anisotropy feature required
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool KRDevice::selectQueueFamilies()
|
||||||
|
{
|
||||||
uint32_t queueFamilyCount = 0;
|
uint32_t queueFamilyCount = 0;
|
||||||
vkGetPhysicalDeviceQueueFamilyProperties(m_device, &queueFamilyCount, nullptr);
|
vkGetPhysicalDeviceQueueFamilyProperties(m_device, &queueFamilyCount, nullptr);
|
||||||
|
|
||||||
@@ -244,8 +249,11 @@ bool KRDevice::initialize(const std::vector<const char*>& deviceExtensions)
|
|||||||
m_computeFamilyQueueIndex = computeFamilyQueue;
|
m_computeFamilyQueueIndex = computeFamilyQueue;
|
||||||
m_transferFamilyQueueIndex = transferFamilyQueue;
|
m_transferFamilyQueueIndex = transferFamilyQueue;
|
||||||
|
|
||||||
// ----
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool KRDevice::initDeviceAndQueues(const std::vector<const char*>& deviceExtensions)
|
||||||
|
{
|
||||||
VkDeviceQueueCreateInfo queueCreateInfo[3]{};
|
VkDeviceQueueCreateInfo queueCreateInfo[3]{};
|
||||||
int queueCount = 1;
|
int queueCount = 1;
|
||||||
float queuePriority = 1.0f;
|
float queuePriority = 1.0f;
|
||||||
@@ -285,31 +293,34 @@ bool KRDevice::initialize(const std::vector<const char*>& deviceExtensions)
|
|||||||
vkGetDeviceQueue(m_logicalDevice, m_computeFamilyQueueIndex, 0, &m_computeQueue);
|
vkGetDeviceQueue(m_logicalDevice, m_computeFamilyQueueIndex, 0, &m_computeQueue);
|
||||||
vkGetDeviceQueue(m_logicalDevice, m_transferFamilyQueueIndex, 0, &m_transferQueue);
|
vkGetDeviceQueue(m_logicalDevice, m_transferFamilyQueueIndex, 0, &m_transferQueue);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool KRDevice::initCommandPools()
|
||||||
|
{
|
||||||
VkCommandPoolCreateInfo poolInfo{};
|
VkCommandPoolCreateInfo poolInfo{};
|
||||||
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
||||||
poolInfo.queueFamilyIndex = m_graphicsFamilyQueueIndex;
|
poolInfo.queueFamilyIndex = m_graphicsFamilyQueueIndex;
|
||||||
poolInfo.flags = 0;
|
poolInfo.flags = 0;
|
||||||
|
|
||||||
if (vkCreateCommandPool(m_logicalDevice, &poolInfo, nullptr, &m_graphicsCommandPool) != VK_SUCCESS) {
|
if (vkCreateCommandPool(m_logicalDevice, &poolInfo, nullptr, &m_graphicsCommandPool) != VK_SUCCESS) {
|
||||||
destroy();
|
|
||||||
// TODO - Log a warning...
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
poolInfo.queueFamilyIndex = m_computeFamilyQueueIndex;
|
poolInfo.queueFamilyIndex = m_computeFamilyQueueIndex;
|
||||||
if (vkCreateCommandPool(m_logicalDevice, &poolInfo, nullptr, &m_computeCommandPool) != VK_SUCCESS) {
|
if (vkCreateCommandPool(m_logicalDevice, &poolInfo, nullptr, &m_computeCommandPool) != VK_SUCCESS) {
|
||||||
destroy();
|
|
||||||
// TODO - Log a warning...
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
poolInfo.queueFamilyIndex = m_transferFamilyQueueIndex;
|
poolInfo.queueFamilyIndex = m_transferFamilyQueueIndex;
|
||||||
if (vkCreateCommandPool(m_logicalDevice, &poolInfo, nullptr, &m_transferCommandPool) != VK_SUCCESS) {
|
if (vkCreateCommandPool(m_logicalDevice, &poolInfo, nullptr, &m_transferCommandPool) != VK_SUCCESS) {
|
||||||
destroy();
|
|
||||||
// TODO - Log a warning...
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool KRDevice::initCommandBuffers()
|
||||||
|
{
|
||||||
const int kMaxGraphicsCommandBuffers = 10; // TODO - This needs to be dynamic?
|
const int kMaxGraphicsCommandBuffers = 10; // TODO - This needs to be dynamic?
|
||||||
m_graphicsCommandBuffers.resize(kMaxGraphicsCommandBuffers);
|
m_graphicsCommandBuffers.resize(kMaxGraphicsCommandBuffers);
|
||||||
|
|
||||||
@@ -326,27 +337,26 @@ bool KRDevice::initialize(const std::vector<const char*>& deviceExtensions)
|
|||||||
allocInfo.commandBufferCount = (uint32_t)m_graphicsCommandBuffers.size();
|
allocInfo.commandBufferCount = (uint32_t)m_graphicsCommandBuffers.size();
|
||||||
|
|
||||||
if (vkAllocateCommandBuffers(m_logicalDevice, &allocInfo, m_graphicsCommandBuffers.data()) != VK_SUCCESS) {
|
if (vkAllocateCommandBuffers(m_logicalDevice, &allocInfo, m_graphicsCommandBuffers.data()) != VK_SUCCESS) {
|
||||||
destroy();
|
|
||||||
// TODO - Log a warning
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
allocInfo.commandPool = m_computeCommandPool;
|
allocInfo.commandPool = m_computeCommandPool;
|
||||||
allocInfo.commandBufferCount = (uint32_t)m_computeCommandBuffers.size();
|
allocInfo.commandBufferCount = (uint32_t)m_computeCommandBuffers.size();
|
||||||
if (vkAllocateCommandBuffers(m_logicalDevice, &allocInfo, m_computeCommandBuffers.data()) != VK_SUCCESS) {
|
if (vkAllocateCommandBuffers(m_logicalDevice, &allocInfo, m_computeCommandBuffers.data()) != VK_SUCCESS) {
|
||||||
destroy();
|
|
||||||
// TODO - Log a warning
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
allocInfo.commandPool = m_transferCommandPool;
|
allocInfo.commandPool = m_transferCommandPool;
|
||||||
allocInfo.commandBufferCount = (uint32_t)m_transferCommandBuffers.size();
|
allocInfo.commandBufferCount = (uint32_t)m_transferCommandBuffers.size();
|
||||||
if (vkAllocateCommandBuffers(m_logicalDevice, &allocInfo, m_transferCommandBuffers.data()) != VK_SUCCESS) {
|
if (vkAllocateCommandBuffers(m_logicalDevice, &allocInfo, m_transferCommandBuffers.data()) != VK_SUCCESS) {
|
||||||
destroy();
|
|
||||||
// TODO - Log a warning
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool KRDevice::initAllocator()
|
||||||
|
{
|
||||||
// Create Vulkan Memory Allocator instance for this device
|
// Create Vulkan Memory Allocator instance for this device
|
||||||
|
|
||||||
// We are dynamically linking Vulkan, so we need to give VMA some hints
|
// We are dynamically linking Vulkan, so we need to give VMA some hints
|
||||||
@@ -365,36 +375,26 @@ bool KRDevice::initialize(const std::vector<const char*>& deviceExtensions)
|
|||||||
vmaCreateInfo.vulkanApiVersion = VK_API_VERSION_1_2;
|
vmaCreateInfo.vulkanApiVersion = VK_API_VERSION_1_2;
|
||||||
vmaCreateInfo.pVulkanFunctions = &vmaVulkanFunctions;
|
vmaCreateInfo.pVulkanFunctions = &vmaVulkanFunctions;
|
||||||
if (vmaCreateAllocator(&vmaCreateInfo, &m_allocator) != VK_SUCCESS) {
|
if (vmaCreateAllocator(&vmaCreateInfo, &m_allocator) != VK_SUCCESS) {
|
||||||
destroy();
|
|
||||||
// TODO - Log a warning
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool KRDevice::initStagingBuffers()
|
||||||
|
{
|
||||||
// Create Staging Buffer for the transfer queue.
|
// Create Staging Buffer for the transfer queue.
|
||||||
// This will be used for asynchronous asset streaming in the streamer thread.
|
// This will be used for asynchronous asset streaming in the streamer thread.
|
||||||
// Start with a 256MB staging buffer.
|
// Start with a 256MB staging buffer.
|
||||||
// TODO - Dynamically size staging buffer using heuristics
|
// TODO - Dynamically size staging buffer using heuristics
|
||||||
m_streamingStagingBufferSize = size_t(256) * 1024 * 1024;
|
m_streamingStagingBufferSize = size_t(256) * 1024 * 1024;
|
||||||
|
if (!createStagingBuffer(m_streamingStagingBufferSize,
|
||||||
if (!createBuffer(
|
|
||||||
m_streamingStagingBufferSize,
|
|
||||||
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
|
||||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
|
||||||
&m_streamingStagingBuffer,
|
&m_streamingStagingBuffer,
|
||||||
&m_streamingStagingBufferAllocation
|
&m_streamingStagingBufferAllocation,
|
||||||
|
&m_streamingStagingBufferData
|
||||||
#if KRENGINE_DEBUG_GPU_LABELS
|
#if KRENGINE_DEBUG_GPU_LABELS
|
||||||
, "Streaming Staging Buffer"
|
, "Streaming Staging Buffer"
|
||||||
#endif // KRENGINE_DEBUG_GPU_LABELS
|
#endif // KRENGINE_DEBUG_GPU_LABELS
|
||||||
)) {
|
)) {
|
||||||
destroy();
|
|
||||||
// TODO - Log a warning
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
VkResult res = vmaMapMemory(m_allocator, m_streamingStagingBufferAllocation, &m_streamingStagingBufferData);
|
|
||||||
if (res != VK_SUCCESS) {
|
|
||||||
destroy();
|
|
||||||
// TODO - Log a warning
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -403,25 +403,75 @@ bool KRDevice::initialize(const std::vector<const char*>& deviceExtensions)
|
|||||||
// Start with a 256MB staging buffer.
|
// Start with a 256MB staging buffer.
|
||||||
// TODO - Dynamically size staging buffer using heuristics
|
// TODO - Dynamically size staging buffer using heuristics
|
||||||
m_graphicsStagingBufferSize = size_t(256) * 1024 * 1024;
|
m_graphicsStagingBufferSize = size_t(256) * 1024 * 1024;
|
||||||
|
if (!createStagingBuffer(m_graphicsStagingBufferSize,
|
||||||
if (!createBuffer(
|
|
||||||
m_graphicsStagingBufferSize,
|
|
||||||
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
|
||||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
|
||||||
&m_graphicsStagingBuffer,
|
&m_graphicsStagingBuffer,
|
||||||
&m_graphicsStagingBufferAllocation
|
&m_graphicsStagingBufferAllocation,
|
||||||
|
&m_graphicsStagingBufferData
|
||||||
#if KRENGINE_DEBUG_GPU_LABELS
|
#if KRENGINE_DEBUG_GPU_LABELS
|
||||||
, "Streaming Staging Buffer"
|
, "Graphics Staging Buffer"
|
||||||
#endif // KRENGINE_DEBUG_GPU_LABELS
|
#endif // KRENGINE_DEBUG_GPU_LABELS
|
||||||
)) {
|
)) {
|
||||||
destroy();
|
|
||||||
// TODO - Log a warning
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
res = vmaMapMemory(m_allocator, m_graphicsStagingBufferAllocation, &m_graphicsStagingBufferData);
|
return true;
|
||||||
if (res != VK_SUCCESS) {
|
}
|
||||||
|
|
||||||
|
bool KRDevice::createStagingBuffer(VkDeviceSize size, VkBuffer* buffer, VmaAllocation* allocation, void** data
|
||||||
|
#if KRENGINE_DEBUG_GPU_LABELS
|
||||||
|
, const char* debug_label
|
||||||
|
#endif // KRENGINE_DEBUG_GPU_LABELS
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (!createBuffer(
|
||||||
|
size,
|
||||||
|
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
||||||
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||||
|
buffer,
|
||||||
|
allocation
|
||||||
|
#if KRENGINE_DEBUG_GPU_LABELS
|
||||||
|
, debug_label
|
||||||
|
#endif // KRENGINE_DEBUG_GPU_LABELS
|
||||||
|
)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (vmaMapMemory(m_allocator, *allocation, data) != VK_SUCCESS) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool KRDevice::initialize(const std::vector<const char*>& deviceExtensions)
|
||||||
|
{
|
||||||
|
// TODO - Return discrete failure codes
|
||||||
|
if (!getAndCheckDeviceCapabilities(deviceExtensions)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!selectQueueFamilies()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!initDeviceAndQueues(deviceExtensions)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!initCommandPools()) {
|
||||||
|
destroy();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!initCommandBuffers()) {
|
||||||
|
destroy();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!initAllocator()) {
|
||||||
|
destroy();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!initStagingBuffers()) {
|
||||||
destroy();
|
destroy();
|
||||||
// TODO - Log a warning
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -106,4 +106,19 @@ public:
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
void getQueueFamiliesForSharing(uint32_t* queueFamilyIndices, uint32_t* familyCount);
|
void getQueueFamiliesForSharing(uint32_t* queueFamilyIndices, uint32_t* familyCount);
|
||||||
|
|
||||||
|
// Initialization helper functions
|
||||||
|
bool getAndCheckDeviceCapabilities(const std::vector<const char*>& deviceExtensions);
|
||||||
|
bool selectQueueFamilies();
|
||||||
|
bool initDeviceAndQueues(const std::vector<const char*>& deviceExtensions);
|
||||||
|
bool initCommandPools();
|
||||||
|
bool initCommandBuffers();
|
||||||
|
bool initAllocator();
|
||||||
|
bool initStagingBuffers();
|
||||||
|
|
||||||
|
bool createStagingBuffer(VkDeviceSize size, VkBuffer* buffer, VmaAllocation* allocation, void** data
|
||||||
|
#if KRENGINE_DEBUG_GPU_LABELS
|
||||||
|
, const char* debug_label
|
||||||
|
#endif // KRENGINE_DEBUG_GPU_LABELS
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user