Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade to wgpu v24 #38

Merged
merged 5 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ jobs:
steps:
- name: install dependencies
if: matrix.os == 'ubuntu-latest'
run: sudo apt install -y libglfw3-dev
run: |
sudo apt update
sudo apt install -y libglfw3-dev
- uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ allprojects {
}

group = "io.ygdrasil"
version = System.getenv("VERSION")?.takeIf { it.isNotBlank() } ?: "v22.0.2-SNAPSHOT"
version = System.getenv("VERSION")?.takeIf { it.isNotBlank() } ?: "v24.0.0-SNAPSHOT"
}


2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repositories {
dependencies {
implementation(libs.download)
implementation(libs.kotlin.multiplatform)
implementation(libs.dokka)
implementation(libs.bundles.dokka)
implementation(libs.jreleaser.plugin)
implementation(libs.android.library)

Expand Down
13 changes: 3 additions & 10 deletions demo/android/src/androidMain/kotlin/WGPUSurfaceView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.util.AttributeSet
import android.view.SurfaceHolder
import android.view.SurfaceView
import com.sun.jna.Pointer
import ffi.NativeAddress
import ffi.memoryScope


Expand All @@ -27,7 +28,7 @@ class WGPUSurfaceView : SurfaceView, SurfaceHolder.Callback2 {
override fun surfaceCreated(surfaceHolder: SurfaceHolder): Unit = memoryScope { scope ->
val instance = wgpuCreateInstance(null) ?: error("fail to create instance")
val surface = getSurface(instance, holder)
val adapter = getAdapter(surface, instance, WGPUBackendType_Vulkan)
val adapter = getAdapter(surface, instance)
val device = getDevice(adapter)
val surfaceCapabilities = surfaceCapabilities(surface, adapter)
configureSurface(device, width, height, surface, surfaceCapabilities.formats.first(), surfaceCapabilities.alphaModes.first(), listOf(surfaceCapabilities.formats.first()))
Expand Down Expand Up @@ -55,13 +56,5 @@ fun getSurface(
surfaceHolder: SurfaceHolder
): WGPUSurface = memoryScope { scope ->
val nativeWindow = io.ygdrasil.nativeHelper.Helper.nativeWindowFromSurface(surfaceHolder.surface)

val surfaceDescriptor = WGPUSurfaceDescriptor.allocate(scope).apply {
nextInChain = WGPUSurfaceDescriptorFromAndroidNativeWindow.allocate(scope).apply {
chain.sType = WGPUSType_SurfaceDescriptorFromAndroidNativeWindow
window = Pointer(nativeWindow)
}.handler
}

wgpuInstanceCreateSurface(instance, surfaceDescriptor) ?: error("fail to create surface")
return getSurfaceAndroidView(instance, nativeWindow.let(::NativeAddress))
}
22 changes: 13 additions & 9 deletions demo/common/src/commonMain/kotlin/HelloTriangle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@ class HelloTriangleScene(val device: WGPUDevice, val renderingContextFormat: UIn
fun initialize() = memoryScope { scope ->
renderPipeline = WGPURenderPipelineDescriptor.allocate(scope).apply {
vertex.module = WGPUShaderModuleDescriptor.allocate(scope).apply {
nextInChain = WGPUShaderModuleWGSLDescriptor.allocate(scope).apply {
code = scope.allocateFrom(triangleVertexShader)
chain.sType = WGPUSType_ShaderModuleWGSLDescriptor
nextInChain = WGPUShaderSourceWGSL.allocate(scope).apply {
code.length = triangleVertexShader.length.toULong()
code.data = scope.allocateFrom(triangleVertexShader)
chain.sType = WGPUSType_ShaderSourceWGSL
}.handler
}.let { wgpuDeviceCreateShaderModule(device, it) } ?: error("fail to create shader module")

vertex.entryPoint = scope.allocateFrom("main")
vertex.entryPoint.data = scope.allocateFrom("main")
vertex.entryPoint.length = "main".length.toULong()

fragment = WGPUFragmentState.allocate(scope).apply {
entryPoint = scope.allocateFrom("main")
entryPoint.data = scope.allocateFrom("main")
entryPoint.length = "main".length.toULong()
module = WGPUShaderModuleDescriptor.allocate(scope).apply {
nextInChain = WGPUShaderModuleWGSLDescriptor.allocate(scope).apply {
code = scope.allocateFrom(redFragmentShader)
chain.sType = WGPUSType_ShaderModuleWGSLDescriptor
nextInChain = WGPUShaderSourceWGSL.allocate(scope).apply {
code.length = redFragmentShader.length.toULong()
code.data = scope.allocateFrom(redFragmentShader)
chain.sType = WGPUSType_ShaderSourceWGSL
}.handler
}.let { wgpuDeviceCreateShaderModule(device, it) } ?: error("fail to create shader module")

Expand All @@ -47,7 +51,7 @@ class HelloTriangleScene(val device: WGPUDevice, val renderingContextFormat: UIn
val surfaceTexture = WGPUSurfaceTexture.allocate(scope)
wgpuSurfaceGetCurrentTexture(surface, surfaceTexture)

if (surfaceTexture.status > WGPUSurfaceGetCurrentTextureStatus_Success) error("surface status is KO with status ${surfaceTexture.status}")
if (surfaceTexture.status > WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal) error("surface status is KO with status ${surfaceTexture.status}")
surfaceTexture.texture ?: error("fail to get texture")

val frame = wgpuTextureCreateView(surfaceTexture.texture, null) ?: error("fail to create view")
Expand Down
94 changes: 79 additions & 15 deletions demo/common/src/commonMain/kotlin/ext.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package io.ygdrasil.wgpu

import ffi.ArrayHolder
import ffi.CString
import ffi.MemoryAllocator
import ffi.NativeAddress
import ffi.memoryScope

val allocator = MemoryAllocator()

fun configureLogs(logLevel: WGPULogLevel = WGPULogLevel_Trace) {
val callback = WGPULogCallback.allocate(allocator) { level: WGPULogLevel, message: CString?, userdata: NativeAddress? ->
val kMessage = message?.toKString()
when (level) {
WGPULogLevel_Error -> println("ERROR : $kMessage}")
WGPULogLevel_Warn -> println("WARN : $kMessage")
WGPULogLevel_Info -> println("INFO : $kMessage")
WGPULogLevel_Debug -> println("DEBUG : $kMessage")
WGPULogLevel_Trace -> println("TRACE : $kMessage")
val callback = WGPULogCallback.allocate(allocator, object : WGPULogCallback {
override fun invoke(level: WGPULogLevel, message: WGPUStringView?, userdata: NativeAddress?) {
val kMessage = message?.data?.toKString(message.length)
when (level) {
WGPULogLevel_Error -> println("ERROR : $kMessage}")
WGPULogLevel_Warn -> println("WARN : $kMessage")
WGPULogLevel_Info -> println("INFO : $kMessage")
WGPULogLevel_Debug -> println("DEBUG : $kMessage")
WGPULogLevel_Trace -> println("TRACE : $kMessage")
}
}
}

})
wgpuSetLogLevel(logLevel)
wgpuSetLogCallback(callback, allocator.bufferOfAddress(callback.handler).handler)
}
Expand Down Expand Up @@ -55,31 +57,93 @@ fun configureSurface(
fun getDevice(adapter: WGPUAdapter): WGPUDevice = memoryScope { scope ->
var fetchedDevice: WGPUDevice? = null

val callback = WGPUAdapterRequestDeviceCallback.allocate(scope) { status, device, message, userdata ->
val callback = WGPURequestDeviceCallback.allocate(scope) { status, device, _, _, _ ->
if (status != WGPURequestDeviceStatus_Success && device == null) error("fail to get device")
fetchedDevice = device
}

wgpuAdapterRequestDevice(adapter, null, callback, scope.bufferOfAddress(callback.handler).handler)
val callbackInfo = WGPURequestDeviceCallbackInfo.allocate(scope).apply {
this.callback = callback
this.userdata2 = scope.bufferOfAddress(callback.handler).handler
}

wgpuAdapterRequestDevice(adapter, null, callbackInfo)

fetchedDevice ?: error("fail to get device")
}

fun getAdapter(surface: WGPUSurface, instance: WGPUInstance, backendType: UInt = WGPUBackendType_Undefined) = memoryScope { scope ->

val callbackInfo = WGPURequestAdapterCallbackInfo.allocate(scope)
val options = WGPURequestAdapterOptions.allocate(scope).apply {
compatibleSurface = surface
this.backendType = backendType
}

var fetchedAdapter: WGPUAdapter? = null

val callback = WGPUInstanceRequestAdapterCallback.allocate(scope) { status, adapter, message, userdata ->
val callback = WGPURequestAdapterCallback.allocate(scope) { status, adapter, _, _, _ ->
if (status != WGPURequestAdapterStatus_Success || adapter == null) error("fail to get adapter")
fetchedAdapter = adapter
}

wgpuInstanceRequestAdapter(instance, options, callback, scope.bufferOfAddress(callback.handler).handler)
callbackInfo.callback = callback
callbackInfo.userdata2 = scope.bufferOfAddress(callback.handler).handler

wgpuInstanceRequestAdapter(instance, options, callbackInfo)

fetchedAdapter ?: error("fail to get adapter")
}

fun getSurfaceFromMetalLayer(instance: WGPUInstance, metalLayer: NativeAddress): WGPUSurface? = memoryScope { scope ->

val surfaceDescriptor = WGPUSurfaceDescriptor.allocate(scope).apply {
nextInChain = WGPUSurfaceSourceMetalLayer.allocate(scope).apply {
chain.sType = WGPUSType_SurfaceSourceMetalLayer
layer = metalLayer
}.handler
}

return wgpuInstanceCreateSurface(instance, surfaceDescriptor)
}

fun getSurfaceAndroidView(
instance: WGPUInstance,
surfaceHolder: NativeAddress
): WGPUSurface = memoryScope { scope ->

val surfaceDescriptor = WGPUSurfaceDescriptor.allocate(scope).apply {
nextInChain = WGPUSurfaceSourceAndroidNativeWindow.allocate(scope).apply {
chain.sType = WGPUSType_SurfaceSourceAndroidNativeWindow
window = surfaceHolder
}.handler
}

wgpuInstanceCreateSurface(instance, surfaceDescriptor) ?: error("fail to create surface")
}


fun getSurfaceFromX11Window(instance: WGPUInstance, display: NativeAddress, window: ULong): WGPUSurface? = memoryScope { scope ->

val surfaceDescriptor = WGPUSurfaceDescriptor.allocate(scope).apply {
nextInChain = WGPUSurfaceSourceXlibWindow.allocate(scope).apply {
chain.sType = WGPUSType_SurfaceSourceXlibWindow
this.display = display
this.window = window
}.handler
}

return wgpuInstanceCreateSurface(instance, surfaceDescriptor)
}

fun getSurfaceFromWindows(instance: WGPUInstance, hinstance: NativeAddress, hwnd: NativeAddress): WGPUSurface? = memoryScope { scope ->

val surfaceDescriptor = WGPUSurfaceDescriptor.allocate(scope).apply {
nextInChain = WGPUSurfaceSourceWindowsHWND.allocate(scope).apply {
chain.sType = WGPUSType_SurfaceSourceWindowsHWND
this.hwnd = hwnd
this.hinstance = hinstance
}.handler
}

return wgpuInstanceCreateSurface(instance, surfaceDescriptor)
}
4 changes: 2 additions & 2 deletions demo/desktop-and-ios/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ kotlin {

binaries {
executable {
entryPoint = "main"
entryPoint = "io.ygdrasil.wgpu.main"
}
}
}
Expand Down Expand Up @@ -102,7 +102,7 @@ tasks.register<JavaExec>("runJvm") {
group = "run"
// TODO: find why the app is crashing sometimes
isIgnoreExitValue = true
mainClass = "MainKt"
mainClass = "io.ygdrasil.wgpu.MainKt"
jvmArgs(
if (Platform.os == Os.MacOs) {
listOf(
Expand Down
19 changes: 5 additions & 14 deletions demo/desktop-and-ios/src/desktopMain/kotlin/main.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@file:OptIn(ExperimentalForeignApi::class)

package io.ygdrasil.wgpu

import cnames.structs.GLFWwindow
import glfw.GLFW_CLIENT_API
import glfw.GLFW_FALSE
Expand All @@ -15,16 +17,6 @@ import glfw.glfwWindowHint
import glfw.glfwWindowShouldClose
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.ExperimentalForeignApi
import io.ygdrasil.wgpu.HelloTriangleScene
import io.ygdrasil.wgpu.WGPUInstance
import io.ygdrasil.wgpu.WGPUSurface
import io.ygdrasil.wgpu.compatibleAlphaMode
import io.ygdrasil.wgpu.compatibleFormat
import io.ygdrasil.wgpu.configureLogs
import io.ygdrasil.wgpu.configureSurface
import io.ygdrasil.wgpu.getAdapter
import io.ygdrasil.wgpu.getDevice
import io.ygdrasil.wgpu.wgpuCreateInstance

fun main() {
val width = 640
Expand All @@ -45,11 +37,10 @@ fun main() {
val surface = getSurface(instance, windowHandler)
val adapter = getAdapter(surface, instance)
val device = getDevice(adapter)
val compatibleFormat = compatibleFormat(surface, adapter)
val alphaMode = compatibleAlphaMode(surface, adapter)
configureSurface(device, width, height, surface, compatibleFormat, alphaMode)
val surfaceCapabilities = surfaceCapabilities(surface, adapter)
configureSurface(device, width, height, surface, surfaceCapabilities.formats.first(), surfaceCapabilities.alphaModes.first(), listOf(surfaceCapabilities.formats.first()))

val scene = HelloTriangleScene(device, compatibleFormat, surface)
val scene = HelloTriangleScene(device, surfaceCapabilities.formats.first(), surface)
scene.initialize()

glfwShowWindow(windowHandler)
Expand Down
27 changes: 2 additions & 25 deletions demo/desktop-and-ios/src/iosMain/kotlin/AppDelegate.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
@file:OptIn(ExperimentalForeignApi::class)

package io.ygdrasil.wgpu

import ffi.NativeAddress
import ffi.memoryScope
import io.ygdrasil.wgpu.HelloTriangleScene
import io.ygdrasil.wgpu.WGPUInstance
import io.ygdrasil.wgpu.WGPUSType_SurfaceDescriptorFromMetalLayer
import io.ygdrasil.wgpu.WGPUSurface
import io.ygdrasil.wgpu.WGPUSurfaceDescriptor
import io.ygdrasil.wgpu.WGPUSurfaceDescriptorFromMetalLayer
import io.ygdrasil.wgpu.compatibleAlphaMode
import io.ygdrasil.wgpu.compatibleFormat
import io.ygdrasil.wgpu.configureSurface
import io.ygdrasil.wgpu.getAdapter
import io.ygdrasil.wgpu.getDevice
import io.ygdrasil.wgpu.wgpuCreateInstance
import io.ygdrasil.wgpu.wgpuInstanceCreateSurface
import kotlinx.cinterop.COpaque
import kotlinx.cinterop.COpaquePointer
import kotlinx.cinterop.CValue
Expand Down Expand Up @@ -102,14 +90,3 @@ class ViewDelegate(

}

private fun getSurfaceFromMetalLayer(instance: WGPUInstance, metalLayer: NativeAddress): WGPUSurface? = memoryScope { scope ->

val surfaceDescriptor = WGPUSurfaceDescriptor.allocate(scope).apply {
nextInChain = WGPUSurfaceDescriptorFromMetalLayer.allocate(scope).apply {
chain.sType = WGPUSType_SurfaceDescriptorFromMetalLayer
layer = metalLayer
}.handler
}

return wgpuInstanceCreateSurface(instance, surfaceDescriptor)
}
2 changes: 2 additions & 0 deletions demo/desktop-and-ios/src/jvmMain/kotlin/Platform.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package io.ygdrasil.wgpu

internal enum class Os {
Linux,
Window,
Expand Down
Loading
Loading