Kotlin Vulkan Initialize

Submitted by Dickens A S on Fri, 11/15/2019 - 13:27

Kickstart Kotlin Vulkan using below github code

Install MSYS2

This is required for windows to download pre-compiled libraries and its headers

pacman -S mingw-w64-x86_64-vulkan-loader mingw-w64-i686-vulkan-loader mingw-w64-i686-vulkan-headers mingw-w64-x86_64-vulkan-headers

Note: libvulkan-1.dll should be copied to the exe folder to make the windows app work

Kotlin Native Def File

headers = vulkan/vulkan.h vulkan/vulkan_core.h
package = vulkan
libraryPaths = C:/msys64/mingw64/x86_64-w64-mingw32/lib C:/msys64/mingw64/lib
linkerOpts = -DVK_USE_PLATFORM_WIN32_KHR -lvulkan -LC:/msys64/mingw64/lib -LC:/msys64/mingw64/x86_64-w64-mingw32/lib
compilerOpts = -DVK_USE_PLATFORM_WIN32_KHR -lvulkan -LC:/msys64/mingw64/lib -LC:/msys64/mingw64/x86_64-w64-mingw32/lib
Operating System Macro
Windows VK_USE_PLATFORM_WIN32_KHR
Mac OS VK_USE_PLATFORM_MACOS_MVK
IOS VK_USE_PLATFORM_IOS_MVK
Android VK_USE_PLATFORM_ANDROID_KHR

Source code analysis - C code vs Kotlin code

C Code

Kotlin Code

#include <stdio.h>
#include <stdlib.h>
#include <vulkan/vulkan.h>

int main(int argc, char *argv[]) {

    VkInstanceCreateInfo acInfo;
    VkInstance output = 0;
    VkResult res;

    acInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    acInfo.pNext = NULL;
    acInfo.pApplicationInfo = NULL;
    acInfo.enabledLayerCount = 0;
    acInfo.ppEnabledLayerNames = NULL;
    acInfo.enabledExtensionCount = 0;
    acInfo.ppEnabledExtensionNames = NULL;
    res = vkCreateInstance(&acInfo, NULL, &output);

    if (res != VK_SUCCESS) {
        printf("Failed to initialize Vulkan");        
        return 1;
    };

    printf("Vulkan successfully initialized");
import vulkan.*
import kotlinx.cinterop.*

data class Result(var value:VkInstance?,var result:Int)

fun main() {

    val res = memScoped {
        val acInfo = alloc<VkInstanceCreateInfo>().apply {
            sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
            pNext = null
            pApplicationInfo = null
            enabledLayerCount = 0u
            ppEnabledLayerNames = null
            enabledExtensionCount = 0U
            ppEnabledExtensionNames = null
        }
        
        val output = alloc<VkInstanceVar>()
        var res = vkCreateInstance(acInfo.ptr, null, output.ptr)
        Result(output.value,res)
    }
    
    if(res.result != VK_SUCCESS) {
        throw Error("Failed to initialize Vulkan")
    }
    
    println("Vulkan successfully initialized")
}

Source Code

End Of Article

Comments

Add new comment