Create Vulkan instance
This commit is contained in:
@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
#include "hk21/vulkan_loader/api.hpp"
|
#include "hk21/vulkan_loader/api.hpp"
|
||||||
|
|
||||||
|
#define VK_ALLOCATOR nullptr
|
||||||
|
|
||||||
int SDL_main(int /* argc */, char* /* argv */[])
|
int SDL_main(int /* argc */, char* /* argv */[])
|
||||||
{
|
{
|
||||||
SDL_Init(SDL_INIT_VIDEO);
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
@ -39,14 +41,56 @@ int SDL_main(int /* argc */, char* /* argv */[])
|
|||||||
if (!status.ok())
|
if (!status.ok())
|
||||||
{
|
{
|
||||||
asl::eprint("Couldn't load global Vulkan functions: {}\n", status);
|
asl::eprint("Couldn't load global Vulkan functions: {}\n", status);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t version{};
|
uint32_t version{};
|
||||||
vkEnumerateInstanceVersion(&version);
|
vkEnumerateInstanceVersion(&version);
|
||||||
|
|
||||||
asl::print("Vulkan version: {}.{}\n",
|
uint32_t vk_major = VK_API_VERSION_MAJOR(version); // NOLINT
|
||||||
VK_API_VERSION_MAJOR(version), // NOLINT
|
uint32_t vk_minor = VK_API_VERSION_MINOR(version); // NOLINT
|
||||||
VK_API_VERSION_MINOR(version)); // NOLINT
|
|
||||||
|
if (vk_major != 1 || vk_minor < 3)
|
||||||
|
{
|
||||||
|
asl::eprint("Incompatible Vulkan version: {}.{}\n", vk_major, vk_minor);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkApplicationInfo app_info{
|
||||||
|
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.pApplicationName = "HK-21",
|
||||||
|
.applicationVersion = 0,
|
||||||
|
.pEngineName = "Custom",
|
||||||
|
.engineVersion = 0,
|
||||||
|
.apiVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), // NOLINT
|
||||||
|
};
|
||||||
|
|
||||||
|
VkInstanceCreateInfo instance_create_info{
|
||||||
|
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.pApplicationInfo = &app_info,
|
||||||
|
.enabledLayerCount = static_cast<uint32_t>(layers.size()),
|
||||||
|
.ppEnabledLayerNames = layers.data(),
|
||||||
|
.enabledExtensionCount = static_cast<uint32_t>(instance_extensions.size()),
|
||||||
|
.ppEnabledExtensionNames = instance_extensions.data(),
|
||||||
|
};
|
||||||
|
|
||||||
|
VkInstance instance{};
|
||||||
|
VkResult res = vkCreateInstance(&instance_create_info, VK_ALLOCATOR, &instance);
|
||||||
|
if (res != VK_SUCCESS)
|
||||||
|
{
|
||||||
|
asl::eprint("Couldn't create Vulkan instance: {}\n", res);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
status = vulkan_loader::load_instance(vkGetInstanceProcAddr, instance);
|
||||||
|
if (!status.ok())
|
||||||
|
{
|
||||||
|
asl::eprint("Couldn't load instance Vulkan functions: {}\n", status);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
bool running = true;
|
bool running = true;
|
||||||
while (running)
|
while (running)
|
||||||
@ -63,6 +107,7 @@ int SDL_main(int /* argc */, char* /* argv */[])
|
|||||||
SDL_Delay(16);
|
SDL_Delay(16);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vkDestroyInstance(instance, VK_ALLOCATOR);
|
||||||
SDL_DestroyWindow(window);
|
SDL_DestroyWindow(window);
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
|
|
||||||
|
@ -12,12 +12,14 @@
|
|||||||
|
|
||||||
#define FN(NAME) extern PFN_##NAME NAME;
|
#define FN(NAME) extern PFN_##NAME NAME;
|
||||||
VULKAN_GLOBAL_FNS
|
VULKAN_GLOBAL_FNS
|
||||||
|
VULKAN_INSTANCE_FNS
|
||||||
#undef FN
|
#undef FN
|
||||||
|
|
||||||
namespace vulkan_loader
|
namespace vulkan_loader
|
||||||
{
|
{
|
||||||
|
|
||||||
asl::status load_global(PFN_vkGetInstanceProcAddr load_fn);
|
asl::status load_global(PFN_vkGetInstanceProcAddr load_fn);
|
||||||
|
asl::status load_instance(PFN_vkGetInstanceProcAddr load_fn, VkInstance instance);
|
||||||
|
|
||||||
} // namespace vulkan_loader
|
} // namespace vulkan_loader
|
||||||
|
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
#define VULKAN_GLOBAL_FNS \
|
#define VULKAN_GLOBAL_FNS \
|
||||||
FN(vkEnumerateInstanceVersion) \
|
FN(vkEnumerateInstanceVersion) \
|
||||||
FN(vkCreateInstance)
|
FN(vkCreateInstance)
|
||||||
|
|
||||||
|
#define VULKAN_INSTANCE_FNS \
|
||||||
|
FN(vkDestroyInstance)
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#define FN(NAME) PFN_##NAME NAME;
|
#define FN(NAME) PFN_##NAME NAME;
|
||||||
VULKAN_GLOBAL_FNS
|
VULKAN_GLOBAL_FNS
|
||||||
|
VULKAN_INSTANCE_FNS
|
||||||
#undef FN
|
#undef FN
|
||||||
|
|
||||||
asl::status vulkan_loader::load_global(PFN_vkGetInstanceProcAddr load_fn)
|
asl::status vulkan_loader::load_global(PFN_vkGetInstanceProcAddr load_fn)
|
||||||
@ -23,3 +24,22 @@ VULKAN_GLOBAL_FNS
|
|||||||
return has_errors ? asl::runtime_error() : asl::ok();
|
return has_errors ? asl::runtime_error() : asl::ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
asl::status vulkan_loader::load_instance(PFN_vkGetInstanceProcAddr load_fn, VkInstance instance)
|
||||||
|
{
|
||||||
|
(void)load_fn;
|
||||||
|
|
||||||
|
bool has_errors = false;
|
||||||
|
|
||||||
|
#define FN(NAME) \
|
||||||
|
NAME = asl::bit_cast<PFN_##NAME>(load_fn(instance, #NAME)); \
|
||||||
|
if (NAME == nullptr) \
|
||||||
|
{ \
|
||||||
|
ASL_ASSERT(NAME != nullptr); \
|
||||||
|
has_errors = true; \
|
||||||
|
}
|
||||||
|
VULKAN_INSTANCE_FNS
|
||||||
|
#undef FN
|
||||||
|
|
||||||
|
return has_errors ? asl::runtime_error() : asl::ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user