Vulkan: Add loading of Instance/Device

Former-commit-id: 0184f78824900bd46cff94dbfe829b126b8c984d
This commit is contained in:
Lynix
2016-04-24 02:28:26 +02:00
parent 4a948dfaa9
commit 22a31c72ed
15 changed files with 592 additions and 25 deletions

View File

@@ -0,0 +1,50 @@
// Copyright (C) 2016 Jérôme Leclercq
// This file is part of the "Nazara Engine - Vulkan"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Vulkan/VkDevice.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/Vulkan/Debug.hpp>
namespace Nz
{
namespace Vk
{
bool Device::Create(VkPhysicalDevice device, const VkDeviceCreateInfo& createInfo, const VkAllocationCallbacks* allocator)
{
m_lastErrorCode = m_instance.vkCreateDevice(device, &createInfo, allocator, &m_device);
if (m_lastErrorCode != VkResult::VK_SUCCESS)
{
NazaraError("Failed to create Vulkan device");
return false;
}
// Store the allocator to access them when needed
if (allocator)
m_allocator = *allocator;
else
m_allocator.pfnAllocation = nullptr;
#define NAZARA_VULKAN_LOAD_DEVICE(func) func = reinterpret_cast<PFN_##func>(GetProcAddr(#func))
try
{
ErrorFlags flags(ErrorFlag_ThrowException, true);
NAZARA_VULKAN_LOAD_DEVICE(vkDestroyDevice);
NAZARA_VULKAN_LOAD_DEVICE(vkDeviceWaitIdle);
NAZARA_VULKAN_LOAD_DEVICE(vkGetDeviceQueue);
}
catch (const std::exception& e)
{
NazaraError(String("Failed to query device function: ") + e.what());
return false;
}
#undef NAZARA_VULKAN_LOAD_DEVICE
return true;
}
}
}

View File

@@ -0,0 +1,103 @@
// Copyright (C) 2016 Jérôme Leclercq
// This file is part of the "Nazara Engine - Vulkan"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Vulkan/VkInstance.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/Vulkan/Debug.hpp>
namespace Nz
{
namespace Vk
{
bool Instance::Create(const VkInstanceCreateInfo& createInfo, const VkAllocationCallbacks* allocator)
{
m_lastErrorCode = Loader::vkCreateInstance(&createInfo, allocator, &m_instance);
if (m_lastErrorCode != VkResult::VK_SUCCESS)
{
NazaraError("Failed to create Vulkan instance");
return false;
}
// Store the allocator to access them when needed
if (allocator)
m_allocator = *allocator;
else
m_allocator.pfnAllocation = nullptr;
#define NAZARA_VULKAN_LOAD_INSTANCE(func) func = reinterpret_cast<PFN_##func>(GetProcAddr(#func))
try
{
ErrorFlags flags(ErrorFlag_ThrowException, true);
NAZARA_VULKAN_LOAD_INSTANCE(vkCreateDevice);
NAZARA_VULKAN_LOAD_INSTANCE(vkDestroyInstance);
NAZARA_VULKAN_LOAD_INSTANCE(vkEnumeratePhysicalDevices);
NAZARA_VULKAN_LOAD_INSTANCE(vkGetDeviceProcAddr);
NAZARA_VULKAN_LOAD_INSTANCE(vkGetPhysicalDeviceFeatures);
NAZARA_VULKAN_LOAD_INSTANCE(vkGetPhysicalDeviceFormatProperties);
NAZARA_VULKAN_LOAD_INSTANCE(vkGetPhysicalDeviceImageFormatProperties);
NAZARA_VULKAN_LOAD_INSTANCE(vkGetPhysicalDeviceMemoryProperties);
NAZARA_VULKAN_LOAD_INSTANCE(vkGetPhysicalDeviceProperties);
NAZARA_VULKAN_LOAD_INSTANCE(vkGetPhysicalDeviceQueueFamilyProperties);
}
catch (const std::exception& e)
{
NazaraError(String("Failed to query instance function: ") + e.what());
return false;
}
#undef NAZARA_VULKAN_LOAD_INSTANCE
return true;
}
bool Instance::EnumeratePhysicalDevices(std::vector<VkPhysicalDevice>* devices)
{
NazaraAssert(devices, "Invalid device vector");
// First, query physical device count
UInt32 deviceCount = 0; // Remember, Nz::UInt32 is a typedef on uint32_t
m_lastErrorCode = vkEnumeratePhysicalDevices(m_instance, &deviceCount, nullptr);
if (m_lastErrorCode != VkResult::VK_SUCCESS || deviceCount == 0)
{
NazaraError("Failed to query physical device count");
return false;
}
// Now we can get the list of the available physical device
devices->resize(deviceCount);
m_lastErrorCode = vkEnumeratePhysicalDevices(m_instance, &deviceCount, devices->data());
if (m_lastErrorCode != VkResult::VK_SUCCESS)
{
NazaraError("Failed to query physical devices");
return false;
}
return true;
}
bool Instance::GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice device, std::vector<VkQueueFamilyProperties>* queueFamilyProperties)
{
NazaraAssert(queueFamilyProperties, "Invalid device vector");
// First, query physical device count
UInt32 queueFamiliesCount = 0; // Remember, Nz::UInt32 is a typedef on uint32_t
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamiliesCount, nullptr);
if (queueFamiliesCount == 0)
{
NazaraError("Failed to query physical device count");
return false;
}
// Now we can get the list of the available physical device
queueFamilyProperties->resize(queueFamiliesCount);
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamiliesCount, queueFamilyProperties->data());
return true;
}
}
}

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2016 Jérôme Leclercq
// Copyright (C) 2016 Jérôme Leclercq
// This file is part of the "Nazara Engine - Vulkan"
// For conditions of distribution and use, see copyright notice in Config.hpp
@@ -7,8 +7,58 @@
namespace Nz
{
namespace Vk
{
bool Loader::Initialize()
{
#ifdef NAZARA_PLATFORM_WINDOWS
s_vulkanLib.Load("vulkan-1.dll");
#elif defined(NAZARA_PLATFORM_LINUX)
s_vulkanLib.Load("libvulkan.so");
#else
#error Unhandled platform
#endif
if (!s_vulkanLib.IsLoaded())
{
NazaraError("Failed to open vulkan library: " + s_vulkanLib.GetLastError());
return false;
}
// vkGetInstanceProcAddr is the only function that's garantee to be exported
vkGetInstanceProcAddr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(s_vulkanLib.GetSymbol("vkGetInstanceProcAddr"));
if (!vkGetInstanceProcAddr)
{
NazaraError("Failed to get symbol \"vkGetInstanceProcAddr\": " + s_vulkanLib.GetLastError());
return false;
}
// all other functions should be loaded using vkGetInstanceProcAddr
#define NAZARA_VULKAN_LOAD_GLOBAL(func) func = reinterpret_cast<PFN_##func>(vkGetInstanceProcAddr(nullptr, #func))
NAZARA_VULKAN_LOAD_GLOBAL(vkCreateInstance);
NAZARA_VULKAN_LOAD_GLOBAL(vkEnumerateInstanceExtensionProperties);
NAZARA_VULKAN_LOAD_GLOBAL(vkEnumerateInstanceLayerProperties);
#undef NAZARA_VULKAN_LOAD_GLOBAL
return true;
}
#define NAZARA_VULKAN_GLOBAL_FUNCTION_IMPL(func) PFN_##func Loader::func = nullptr
NAZARA_VULKAN_GLOBAL_FUNCTION_IMPL(vkCreateInstance);
NAZARA_VULKAN_GLOBAL_FUNCTION_IMPL(vkEnumerateInstanceExtensionProperties);
NAZARA_VULKAN_GLOBAL_FUNCTION_IMPL(vkEnumerateInstanceLayerProperties);
NAZARA_VULKAN_GLOBAL_FUNCTION_IMPL(vkGetInstanceProcAddr);
#undef NAZARA_VULKAN_GLOBAL_FUNCTION_IMPL
DynLib Loader::s_vulkanLib;
void Loader::Uninitialize()
{
s_vulkanLib.Unload();
}
}
}
#define NAZARA_VULKAN_EXPORTED_FUNCTION_IMPL(func) PFN_##func func = nullptr;
NAZARA_VULKAN_EXPORTED_FUNCTION_IMPL(vkGetInstanceProcAddr)

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2016 Jérôme Leclercq
// Copyright (C) 2016 Jérôme Leclercq
// This file is part of the "Nazara Engine - Vulkan"
// For conditions of distribution and use, see copyright notice in Config.hpp