Final Platform Layer 0.9.8-beta
No Matches

Initialize Vulkan

Initializing Vulkan is different than initializing other graphics APIs such as OpenGL, but FPL helps you in the first steps.

The very first thing you need is a Vulkan Instance (VkInstance).

Secondly you require a Vulkan Surface (VkSurfaceKHR) to present the graphical result to the screen.

FPL can create the Instance and the Surface for you.

The surface creation is mandatory, but an exiting instance can optionally be passed to the fplSettings structure if needed.

Create a Vulkan Instance and a Surface

First you have to create a fplSettings structure and initialize it with default settings using fplSetDefaultSettings().

Second you set the fplVideoSettings::backend to fplVideoBackendType_Vulkan .

We can reference the fplVulkanSettings type stored in fplGraphicsApiSettings::vulkan for convenience usage later.

fplSettings settings;
// Reference of the video settings
fplVideoSettings *videoSettings = &;
// Forcing the video backend to Vulkan
// Reference of the vulkan settings for later usage
fplVulkanSettings *vulkanSettings = &;
fpl_common_api void fplSetDefaultSettings(fplSettings *settings)
Resets the given settings container to default values for window, video, audio, etc.
@ fplVideoBackendType_Vulkan
fplVulkanSettings vulkan
Vulkan settings.
A structure containing settings, such as window, video, etc.
fplVideoSettings video
Video settings.
A structure that contains video settings such as backend, v-sync, API-settings, etc.
fplVideoBackendType backend
video backend type
fplGraphicsApiSettings graphics
Graphics API settings.
A structure that contains Vulkan video settings.

Fill in the application, engine and api versions

fplVulkanSettings *vulkanSettings = &;
vulkanSettings->appName = "The name of your application";
vulkanSettings->appVersion = fplStructInit(fplVersionInfo, "1.0.0", "1", "0", "0"); // Version of your application
vulkanSettings->engineName = "The name of your engine";
vulkanSettings->engineVersion = fplStructInit(fplVersionInfo, "1.0.0", "1", "0", "0"); // Version of your engine
vulkanSettings->apiVersion = fplStructInit(fplVersionInfo, "1.1.0", "1", "1", "0"); // Preferred Vulkan API version (should be greater or equal than 1.1.0)
#define fplStructInit
Initializes a struct by the given type.
A structure that contains version informations.
fplVersionInfo engineVersion
The engine version (Only required if fplVulkanSettings::instanceHandle is fpl_null)
fplVersionInfo apiVersion
The preferred Vulkan api version (Only required if fplVulkanSettings::instanceHandle is fpl_null)
const char * appName
The application name (Only required if fplVulkanSettings::instanceHandle is fpl_null)
fplVersionInfo appVersion
The application version (Only required if fplVulkanSettings::instanceHandle is fpl_null)
const char * engineName
The engine name (Only required if fplVulkanSettings::instanceHandle is fpl_null)

Choose a Vulkan validation mode

static void VulkanValidationLayerCallback(void *userData, const char *message, const VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, const VkDebugUtilsMessageTypeFlagsEXT messageType, const VkDebugUtilsMessengerCallbackDataEXT *debugUtilsMessengerCallbackData) {
fplConsoleFormatOut("Vulkan Validation: %s\n", message);
fplVulkanSettings *vulkanSettings = &;
vulkanSettings->validationLayerMode = fplVulkanValidationLayerMode_Disabled; // Do not use the Vulkan Validation Layer as an instance extension
// or
vulkanSettings->validationLayerMode = fplVulkanValidationLayerMode_Logging; // Enable Vulkan Validation Layer as an instance extension and log everything to FPL directly, using the FPL logging system
// or
vulkanSettings->validationLayerMode = fplVulkanValidationLayerMode_User; // Enable Vulkan Validation Layer as an instance extension and let the user handle the logging
vulkanSettings->validationLayerCallback = VulkanValidationLayerCallback; // The user callback
vulkanSettings->userData = (void *)YourUserData; // The user data passed to the callback
fpl_common_api void fplConsoleFormatOut(const char *format,...)
Writes the given formatted text to the standard output console buffer.
@ fplVulkanValidationLayerMode_Disabled
Do not use the validation.
fplVulkanValidationLayerMode validationLayerMode
The fplVulkanValidationLayerMode.
void * userData
User data passed to any callbacks.
fplVulkanValidationLayerCallback * validationLayerCallback
The validation layer callback fplVulkanValidationLayerCallback.

Continue with Vulkan Usage

Create a Vulkan Surface from an existing Instance

If you already have a Vulkan Instance (VkInstance), you can simply pass that to the fplVulkanSettings::instanceHandle field and initialize FPL with fplPlatformInit():

VkInstance yourVulkanInstance;
fplVulkanSettings *vulkanSettings = &;
vulkanSettings->instance = yourVulkanInstance; // Pass in your vulkan instance to FPL
// ... Vulkan Surface is created now and are ready to use
fpl_common_api bool fplPlatformInit(const fplInitFlags initFlags, const fplSettings *initSettings)
Initializes the platform layer.
@ fplInitFlags_Video
Use a video backbuffer (This flag ensures that fplInitFlags_Window is included always)

Continue with Vulkan Usage

Vulkan Usage

After you have created a Vulkan Instance and a Surface, you can query it by calling fplGetVideoSurface() directly after fplPlatformInit():

// Get video surface
const fplVideoSurface *surface = fplGetVideoSurface();
fplAssert(surface != fpl_null);
// Get the VkInstance and VkSurfaceKHR
VkInstance instanceHandle = (VkInstance)surface->vulkan.instanceHandle;
VkSurfaceKHR surfaceHandle = (VkSurfaceKHR)surface->vulkan.surfaceKHR;
fplAssert(instanceHandle != VK_NULL_HANDLE);
fplAssert(surfaceHandle != VK_NULL_HANDLE);
// ... continue the Vulkan initialization process
#define fpl_null
#define fplAssert(exp)
Breaks with an runtime assertion, when the specified expression evaluates to false.
@ fplInitFlags_All
All init flags.
fpl_common_api const fplVideoSurface * fplGetVideoSurface()
Gets the current fplVideoSurface that stores all handles used for the active video backend.
Stores the surface properties for the active video backend.
fplVideoSurfaceVulkan vulkan
The Vulkan surface properties.

After that, the typical Vulkan process continues:

See the FPL_Vulkan demo for more details or learn more about Vulkan: Vulkan-Tutorials

Memory Allocator

If you have an existing memory allocator for Vulkan, you can pass that to FPL before calling fplPlatformInit():

VkAllocationCallbacks *yourMemoryAllocator;
fplVulkanSettings *vulkanSettings = &;
vulkanSettings->allocator = (void *)yourMemoryAllocator; // Pass in your memory allocator
const void * allocator
The vulkan allocator (VkAllocationCallbacks)