Skip to content

Commit

Permalink
Implement createShaderModule and createPipelineLayout functions in wgpu
Browse files Browse the repository at this point in the history
The functions createShaderModule and createPipelineLayout were added to classes Device.jvm and Device.js, allowing for the creation of shader modules and pipeline layouts. A simple triangle scene was initialized to demonstrate these new functionalities. Corresponding changes were made in the ShaderModule files across the main and JVM platforms.
  • Loading branch information
ygdrasil-io committed Mar 3, 2024
1 parent 66627aa commit 07636e0
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.ygdrasil.wgpu.examples

import io.ygdrasil.wgpu.ShaderModuleDescriptor

class SimpleTriangleScene : Application.Scene() {
override fun Application.initialiaze() {
val shaderModule = device.createShaderModule(
ShaderModuleDescriptor(
code = shader
)
)

val pipeline = device.createPipelineLayout()

TODO("Not yet implemented")
}

override fun Application.render() {
TODO("Not yet implemented")
}
}

private val shader = """
@vertex
fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4<f32> {
let x = f32(i32(in_vertex_index) - 1);
let y = f32(i32(in_vertex_index & 1u) * 2 - 1);
return vec4<f32>(x, y, 0.0, 1.0);
}
@fragment
fn fs_main() -> @location(0) vec4<f32> {
return vec4<f32>(1.0, 0.0, 0.0, 1.0);
}
""".trimIndent()
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package io.ygdrasil.wgpu
expect class Device: AutoCloseable {

fun createCommandEncoder(descriptor: CommandEncoderDescriptor? = null): CommandEncoder
fun createShaderModule(descriptor: ShaderModuleDescriptor)
fun createPipelineLayout(): Any

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.ygdrasil.wgpu

expect class ShaderModule {
}

data class ShaderModuleDescriptor(
var code: String,
var label: String? = null,
var sourceMap: Any? = null,
var compilationHints: Array<CompilationHint>? = null
) {
data class CompilationHint(
var entryPoint: String,
// TODO
//var layout: dynamic /* GPUPipelineLayout? | "auto" */
)
}
17 changes: 14 additions & 3 deletions bindings/wgpu/wgpu4k/src/jsMain/kotlin/io.ygdrasil.wgpu/Device.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ actual class Device(val handler: GPUDevice): AutoCloseable {

val queue: Queue by lazy { Queue(handler.queue) }

override fun close() {
// Nothing on JS
}

actual fun createCommandEncoder(descriptor: CommandEncoderDescriptor?): CommandEncoder {
return CommandEncoder(
Expand All @@ -22,6 +19,20 @@ actual class Device(val handler: GPUDevice): AutoCloseable {

)
}

actual fun createShaderModule(descriptor: ShaderModuleDescriptor): ShaderModule {
return ShaderModule(handler.createShaderModule(descriptor.convert()))
}

actual fun createPipelineLayout(): Any {
handler.createPipelineLayout(

)
}

override fun close() {
// Nothing on JS
}
}

private fun CommandEncoderDescriptor.convert(): GPUCommandEncoderDescriptor = object : GPUCommandEncoderDescriptor {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.ygdrasil.wgpu

import io.ygdrasil.wgpu.internal.js.GPUShaderModule
import io.ygdrasil.wgpu.internal.js.GPUShaderModuleCompilationHint
import io.ygdrasil.wgpu.internal.js.GPUShaderModuleDescriptor

actual class ShaderModule(internal val handler: GPUShaderModule) {
}

fun ShaderModuleDescriptor.convert(): GPUShaderModuleDescriptor = object : GPUShaderModuleDescriptor {
override var code: String = this@convert.code
override var sourceMap: Any? = this@convert.sourceMap ?: undefined
override var compilationHints: Array<GPUShaderModuleCompilationHint>? = this@convert
.compilationHints
?.map { it.convert() }
?.toTypedArray()
?: undefined
override var label: String? = this@convert.label ?: undefined

}

private fun ShaderModuleDescriptor.CompilationHint.convert() = object : GPUShaderModuleCompilationHint {
override var entryPoint: String = this@convert.entryPoint
override var layout: dynamic = TODO("no yet implemented")//[email protected] ?: undefined
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ actual class Device(internal val handler: WGPUDeviceImpl) : AutoCloseable {
)
}

actual fun createShaderModule(descriptor: ShaderModuleDescriptor): ShaderModule {
return ShaderModule(
wgpuDeviceCreateShaderModule(handler, descriptor.convert())
)
}


override fun close() {
wgpuDeviceRelease(handler)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.ygdrasil.wgpu

import com.sun.jna.NativeLong
import io.ygdrasil.wgpu.internal.jvm.*

actual class ShaderModule(internal val handler: WGPUShaderModule?) : AutoCloseable {
override fun close() {
wgpuShaderModuleRelease(handler)
}
}


internal fun ShaderModuleDescriptor.convert(): WGPUShaderModuleDescriptor = WGPUShaderModuleDescriptor().also {
it.label = label
it.nextInChain = WGPUShaderModuleWGSLDescriptor.ByReference().also {
it.code = code
it.chain.apply {
sType = WGPUSType.WGPUSType_ShaderModuleWGSLDescriptor.value
}
}
it.hintCount = NativeLong(compilationHints.size)
it.hints = compilationHints.map { it.convert() }

}

private fun ShaderModuleDescriptor.CompilationHint.convert() = WGPUShaderModuleCompilationHint.ByReference().also {
TODO("no yet implemented")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3021,7 +3021,7 @@ public open class WGPUShaderModuleDescriptor : Structure {
* Declared([a8(next):[*:b1]i4(sType)x4](WGPUChainedStruct)))*
*/
@JvmField
public var nextInChain: Pointer? = null
public var nextInChain: WGPUShaderModuleWGSLDescriptor.ByReference? = null

/**
* mapped from (Char(layout = b1))*
Expand Down

0 comments on commit 07636e0

Please sign in to comment.