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.

Documentation

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 or IntelliJ IDEA!
  4. On Android Native, run ./gradlew build, then run the subproject android-native with android studio or IntelliJ IDEA!
  5. 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 Run the headless render tests

The desktop demo also has headless render paths that do not create a GLFW window or a WebGPU surface. They render the triangle into an offscreen texture, copy it back to CPU memory, and write an image file.

On JVM:

./gradlew :demo:desktop-and-ios:runJvmHeadless

This writes:

demo/desktop-and-ios/build/headless/triangle.png

On Kotlin/Native desktop, first link the executable for the host target, then run it with --headless. For example, on macOS arm64:

./gradlew :demo:desktop-and-ios:linkDebugExecutableMacosArm64
cd demo/desktop-and-ios
./build/bin/macosArm64/debugExecutable/desktop-and-ios.kexe --headless

This writes:

demo/desktop-and-ios/build/headless/triangle.ppm

How to use

View the API reference

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_24
        }
    }

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

    androidTarget {
        compilerOptions {
            jvmTarget = JvmTarget.JVM_24
        }

        publishLibraryVariants("release", "debug")
    }

    applyHierarchyTemplate(hierarchyTemplate)

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

About

wgpu binding for kotlin multi platform

Topics

Resources

License

Stars

34 stars

Watchers

2 watching

Forks

Packages

 
 
 

Contributors