Skip to content

Commit

Permalink
Update wgpu bindings with shader and pipeline layout creation
Browse files Browse the repository at this point in the history
Implemented createShaderModule and createPipelineLayout functions in Device.jvm and Device.js classes under wgpu module. These updates will enhance rendering functionalities by enabling creation of shader modules and pipeline layouts. Mappings for these new functions were also added in ShaderModuleDescriptor for consistency across different platforms.
  • Loading branch information
ygdrasil-io committed Mar 4, 2024
1 parent 07636e0 commit cd7ea5f
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 534 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.ygdrasil.wgpu.examples

import io.ygdrasil.wgpu.PipelineLayoutDescriptor
import io.ygdrasil.wgpu.RenderPipelineDescriptor
import io.ygdrasil.wgpu.ShaderModuleDescriptor

class SimpleTriangleScene : Application.Scene() {
Expand All @@ -10,7 +12,13 @@ class SimpleTriangleScene : Application.Scene() {
)
)

val pipeline = device.createPipelineLayout()
val pipelineLayout = device.createPipelineLayout(PipelineLayoutDescriptor())

val renderPipeline = device.createRenderPipeline(
RenderPipelineDescriptor().apply {
layout = pipelineLayout
}
)

TODO("Not yet implemented")
}
Expand Down
4 changes: 1 addition & 3 deletions bindings/wgpu/examples/src/jvmMain/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import io.ygdrasil.wgpu.internal.jvm.wgpuGetVersion
import kotlinx.coroutines.runBlocking

fun main() {

printVersion()
initSDL()

runBlocking {
jvmApplication()
}
Expand All @@ -26,4 +24,4 @@ fun printVersion() {
SDL_GetVersion(this)
println("SDL version $major.$minor.$patch")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ package io.ygdrasil.wgpu
@OptIn(ExperimentalStdlibApi::class)
expect class Device: AutoCloseable {

val queue: Queue

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

fun createShaderModule(descriptor: ShaderModuleDescriptor): ShaderModule

fun createPipelineLayout(descriptor: PipelineLayoutDescriptor): PipelineLayout

fun createRenderPipeline(descriptor: RenderPipelineDescriptor): RenderPipeline
}

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

expect class PipelineLayout

expect class RenderPipeline

class PipelineLayoutDescriptor
data class RenderPipelineDescriptor(
var fragment: FragmentState? = null,
var layout: PipelineLayout? = null
) {
data class FragmentState(
var module: ShaderModule,
var targets: Array<ColorTargetState?> = arrayOf(),
var entryPoint: String? = null
) {

class ColorTargetState(
/*
var format: String /* "r8unorm" | "r8snorm" | "r8uint" | "r8sint" | "r16uint" | "r16sint" | "r16float" | "rg8unorm" | "rg8snorm" | "rg8uint" | "rg8sint" | "r32uint" | "r32sint" | "r32float" | "rg16uint" | "rg16sint" | "rg16float" | "rgba8unorm" | "rgba8unorm-srgb" | "rgba8snorm" | "rgba8uint" | "rgba8sint" | "bgra8unorm" | "bgra8unorm-srgb" | "rgb9e5ufloat" | "rgb10a2uint" | "rgb10a2unorm" | "rg11b10ufloat" | "rg32uint" | "rg32sint" | "rg32float" | "rgba16uint" | "rgba16sint" | "rgba16float" | "rgba32uint" | "rgba32sint" | "rgba32float" | "stencil8" | "depth16unorm" | "depth24plus" | "depth24plus-stencil8" | "depth32float" | "depth32float-stencil8" | "bc1-rgba-unorm" | "bc1-rgba-unorm-srgb" | "bc2-rgba-unorm" | "bc2-rgba-unorm-srgb" | "bc3-rgba-unorm" | "bc3-rgba-unorm-srgb" | "bc4-r-unorm" | "bc4-r-snorm" | "bc5-rg-unorm" | "bc5-rg-snorm" | "bc6h-rgb-ufloat" | "bc6h-rgb-float" | "bc7-rgba-unorm" | "bc7-rgba-unorm-srgb" | "etc2-rgb8unorm" | "etc2-rgb8unorm-srgb" | "etc2-rgb8a1unorm" | "etc2-rgb8a1unorm-srgb" | "etc2-rgba8unorm" | "etc2-rgba8unorm-srgb" | "eac-r11unorm" | "eac-r11snorm" | "eac-rg11unorm" | "eac-rg11snorm" | "astc-4x4-unorm" | "astc-4x4-unorm-srgb" | "astc-5x4-unorm" | "astc-5x4-unorm-srgb" | "astc-5x5-unorm" | "astc-5x5-unorm-srgb" | "astc-6x5-unorm" | "astc-6x5-unorm-srgb" | "astc-6x6-unorm" | "astc-6x6-unorm-srgb" | "astc-8x5-unorm" | "astc-8x5-unorm-srgb" | "astc-8x6-unorm" | "astc-8x6-unorm-srgb" | "astc-8x8-unorm" | "astc-8x8-unorm-srgb" | "astc-10x5-unorm" | "astc-10x5-unorm-srgb" | "astc-10x6-unorm" | "astc-10x6-unorm-srgb" | "astc-10x8-unorm" | "astc-10x8-unorm-srgb" | "astc-10x10-unorm" | "astc-10x10-unorm-srgb" | "astc-12x10-unorm" | "astc-12x10-unorm-srgb" | "astc-12x12-unorm" | "astc-12x12-unorm-srgb" */
var blend: GPUBlendState?
get() = definedExternally
set(value) = definedExternally
var writeMask: GPUColorWriteFlags?
get() = definedExternally
set(value) = definedExternally
*/
)
}
}
52 changes: 42 additions & 10 deletions bindings/wgpu/wgpu4k/src/jsMain/kotlin/io.ygdrasil.wgpu/Device.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

package io.ygdrasil.wgpu

import io.ygdrasil.wgpu.internal.js.GPUCommandEncoderDescriptor
import io.ygdrasil.wgpu.internal.js.GPUDevice
import io.ygdrasil.wgpu.internal.js.*

actual class Device(val handler: GPUDevice): AutoCloseable {

val queue: Queue by lazy { Queue(handler.queue) }
actual class Device(val handler: GPUDevice) : AutoCloseable {

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

actual fun createCommandEncoder(descriptor: CommandEncoderDescriptor?): CommandEncoder {
return CommandEncoder(
Expand All @@ -24,18 +22,52 @@ actual class Device(val handler: GPUDevice): AutoCloseable {
return ShaderModule(handler.createShaderModule(descriptor.convert()))
}

actual fun createPipelineLayout(): Any {
handler.createPipelineLayout(
actual fun createPipelineLayout(descriptor: PipelineLayoutDescriptor): PipelineLayout = handler
.createPipelineLayout(descriptor.convert())
.let(::PipelineLayout)

actual fun createRenderPipeline(descriptor: RenderPipelineDescriptor): RenderPipeline = handler
.createRenderPipeline(descriptor.convert())
.let(::RenderPipeline)

)
}

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

private fun CommandEncoderDescriptor.convert(): GPUCommandEncoderDescriptor = object : GPUCommandEncoderDescriptor {
private fun RenderPipelineDescriptor.convert(): GPURenderPipelineDescriptor = object : GPURenderPipelineDescriptor {

override var vertex: GPUVertexState = TODO("Not yet implemented")
override var primitive: GPUPrimitiveState? = TODO("Not yet implemented")
override var depthStencil: GPUDepthStencilState? = TODO("Not yet implemented")
override var multisample: GPUMultisampleState? = TODO("Not yet implemented")
override var fragment: GPUFragmentState? = this@convert.fragment?.convert() ?: undefined
override var layout: dynamic = this@convert.layout ?: "auto"
}

private fun RenderPipelineDescriptor.FragmentState.convert(): GPUFragmentState = object : GPUFragmentState {

override var targets: Array<GPUColorTargetState?> = this@convert.targets.map { it?.convert() }.toTypedArray()
override var module: GPUShaderModule = this@convert.module.handler
override var entryPoint: String? = this@convert.entryPoint ?: undefined
// TODO not sure how to map this
//override var constants: Record<String, GPUPipelineConstantValue>? = TODO("Not yet implemented")
}

private fun RenderPipelineDescriptor.FragmentState.ColorTargetState.convert(): GPUColorTargetState =
object : GPUColorTargetState {
override var format: String = TODO("Not yet implemented")
override var blend: GPUBlendState? = TODO("Not yet implemented")
override var writeMask: GPUColorWriteFlags? = TODO("Not yet implemented")
}

private fun PipelineLayoutDescriptor.convert(): GPUPipelineLayoutDescriptor = object : GPUPipelineLayoutDescriptor {
override var label: String? = TODO("Not yet implemented")
override var bindGroupLayouts: Iterable<GPUBindGroupLayout> = TODO("Not yet implemented")
}

private fun CommandEncoderDescriptor.convert(): GPUCommandEncoderDescriptor = object : GPUCommandEncoderDescriptor {
//TODO
}

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

import io.ygdrasil.wgpu.internal.js.GPUPipelineLayout
import io.ygdrasil.wgpu.internal.js.GPURenderPipeline

actual class PipelineLayout(internal var handler: GPUPipelineLayout)
actual class RenderPipeline(internal var handler: GPURenderPipeline)
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ actual class RenderingContext(private val handler: GPUCanvasContext) : AutoClose
}

actual fun present() {
// Nothing to do
// Nothing to do on js
}

fun configure(canvasConfiguration: CanvasConfiguration) {
Expand Down
Loading

0 comments on commit cd7ea5f

Please sign in to comment.