Skip to content

wgpu4k/wgpu4k-native

Repository files navigation

wgpu4k native : a Kotlin binding of wgpu library

Tests Static Badge Static Badge Static Badge Static Badge

About

This project is a WebGPU binding compatible with Desktop and Mobile.

If you are looking for an experience that also covers the web, you can use the wgpu4k project.

This library uses the Firefox backend written in Rust, available here.

How to Run the demo

  1. On JVM: ./gradlew demo:desktop-and-ios:runJvm
  2. On native Macos or Linux: ./gradlew demo:desktop-and-ios:runDebugExecutable
  3. On Android, run the subproject android with android studio !
  4. On iOS ./gradlew demo:desktop-and-ios:assembleWgpuAppXCFramework to build the XC Framework, then you can run the subproject iosApp (on demo/desktop-and-ios folder) with XCode on a iOS simulator or real device.

How to use

From a basic multiplatform project, create a common native source set and add the library:

private val hierarchyTemplate = KotlinHierarchyTemplate {
    /* natural hierarchy is only applied to default 'main'/'test' compilations (by default) */
    withSourceSetTree(KotlinSourceSetTree.main, KotlinSourceSetTree.test)

    common {
        /* All compilations shall be added to the common group by default */
        withCompilations { true }

        group("commonNative") {
            group("native") {
                withNative()

                group("apple") {
                    withApple()

                    group("ios") {
                        withIos()
                    }

                    group("tvos") {
                        withTvos()
                    }

                    group("watchos") {
                        withWatchos()
                    }

                    group("macos") {
                        withMacos()
                    }
                }

                group("linux") {
                    withLinux()
                }

                group("mingw") {
                    withMingw()
                }

            }

            withJvm()
            withAndroidTarget()
        }
    }
}

kotlin {
    ...
    jvm {
        compilerOptions {
            jvmTarget = JvmTarget.JVM_22
        }
    }

    iosX64()
    iosArm64()
    iosSimulatorArm64()
    macosArm64()
    macosX64()
    linuxArm64()
    linuxX64()
    mingwX64()

    androidTarget {
        compilerOptions {
            jvmTarget = JvmTarget.JVM_22
        }

        publishLibraryVariants("release", "debug")
    }

    applyHierarchyTemplate(hierarchyTemplate)

    sourceSets {
    
        ...
        val commonNativeMain by getting {
            dependencies { api("io.ygdrasil:wgpu4k-native:<library version>) }
        }
        ...
        
    }
    ...
}