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

use wgpu4k and add Android basic rendering #279

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b428a8e
Update Kotlin version, JVM target, dependencies, and refactor
ygdrasil-io Sep 19, 2024
6b3b391
Update to JDK 22 throughout the project
ygdrasil-io Sep 19, 2024
bdd3c00
Refactor WebGPU imports and simplify texture format determination
ygdrasil-io Sep 19, 2024
0504cee
Switch to ygdrasil.wgpu library and code adjustments
ygdrasil-io Sep 22, 2024
dd1c16b
Refactor GPU buffer handling and add blend states
ygdrasil-io Sep 22, 2024
187c83c
Refactor and clean up WebGPU codebase
ygdrasil-io Sep 22, 2024
3c5aece
Fix typos and update dependencies
ygdrasil-io Sep 23, 2024
82f5c7b
Refactor import paths and use consistent naming conventions.
ygdrasil-io Sep 23, 2024
41b7539
Update WGPU integration and refactor vertex attribute handling
ygdrasil-io Sep 23, 2024
a290910
Remove unused WebGPU-related code and imports
ygdrasil-io Sep 23, 2024
50b9fd2
Refactor SpriteCache WebGPU function calls
ygdrasil-io Sep 25, 2024
102378d
Refactor imports and improve shader handling
ygdrasil-io Sep 25, 2024
923e1d2
Refactor imports and update vertex buffer handling
ygdrasil-io Sep 26, 2024
c611493
Refactor and simplify GPU-related classes and interfaces
ygdrasil-io Sep 26, 2024
74c8d26
Refactor and simplify GPU-related codebase
ygdrasil-io Sep 26, 2024
8ed0e4e
Refactor and simplify GPU-related codebase
ygdrasil-io Sep 26, 2024
1eb09e4
Add WebGPU conversion methods and refactor WebGPU usage.
ygdrasil-io Sep 27, 2024
cde35ec
Update texture handling to comply with new API requirements
ygdrasil-io Sep 27, 2024
f72a19d
Remove WebGPU native bindings and build configurations
ygdrasil-io Sep 27, 2024
85d4163
Refactor TextureDataLayout parameters
ygdrasil-io Sep 27, 2024
866cc37
Update buffer size calculation from Short to Float.
ygdrasil-io Sep 27, 2024
3f5bcd1
Refactor `writeBuffer` method parameter.
ygdrasil-io Sep 27, 2024
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
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Setup Java JDK 21
- name: Setup Java JDK 22
uses: actions/setup-java@v4.2.1
with:
java-version: 21
java-version: 22
distribution: adopt
- name: Grant gradlew execution permissions
run: chmod +x gradlew
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Setup Java JDK 21
- name: Setup Java JDK 22
uses: actions/setup-java@v4.2.1
with:
java-version: 21
java-version: 22
distribution: adopt
- name: Grant gradlew execution permissions
run: chmod +x gradlew
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ jobs:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- uses: actions/checkout@v2
- name: Setup Java JDK 21
- name: Setup Java JDK 22
uses: actions/setup-java@v4.2.1
with:
java-version: 21
java-version: 22
distribution: adopt
- name: Grant gradlew execution permissions
run: chmod +x gradlew
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![build](https://github.com/littlektframework/littlekt/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/littlektframework/littlekt/actions/workflows/build.yml)
[![Download](https://img.shields.io/maven-central/v/com.littlekt/core/0.10.1)](https://search.maven.org/artifact/com.littlekt/core/0.10.1/pom)

Requires JDK 21+ in order to use due to the usage of the new Java FFM API.
Requires JDK 22+ in order to use due to the usage of the new Java FFM API.

**Currently, in development.**

Expand Down Expand Up @@ -36,13 +36,13 @@ repositories {
kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = "21" // littlekt targets jvm 21 so we must target at least 21
kotlinOptions.jvmTarget = "22" // littlekt targets jvm 21 so we must target at least 21
}
}
}

val littleKtVersion = "0.10.1" // get the latest release at the top
val kotlinCoroutinesVersion = "1.9.0-RC" // or whatever version you are using
val kotlinCoroutinesVersion = "1.9.0" // or whatever version you are using

sourceSets {
val commonMain by getting {
Expand Down
12 changes: 8 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ plugins {
val littleKtVersion: String by project

allprojects {
repositories {
google()
mavenCentral()
}
group = "com.littlekt"
version = littleKtVersion
extra["isReleaseVersion"] = !littleKtVersion.endsWith("SNAPSHOT")
Expand All @@ -24,3 +20,11 @@ allprojects {
plugins.withType<YarnPlugin> {
the<YarnRootExtension>().apply { yarnLockMismatchReport = YarnLockMismatchReport.WARNING }
}

// TODO: remove later
configurations.all {
resolutionStrategy {
cacheChangingModulesFor(0, "seconds")
cacheDynamicVersionsFor(0, "minutes")
}
}
30 changes: 18 additions & 12 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType

Expand All @@ -15,12 +17,11 @@ kotlin {
env = System.getProperty("env")
}
systemProperty("env", env ?: "dev")
jvmArgs("--enable-preview")
}
jvm {
compilations.all {
kotlinOptions.jvmTarget = "11"
compileJavaTaskProvider?.get()?.options?.compilerArgs?.add("--enable-preview")
@OptIn(ExperimentalKotlinGradlePluginApi::class)
compilerOptions {
jvmTarget = JvmTarget.JVM_22
}
}
js(KotlinJsCompilerType.IR) {
Expand All @@ -37,6 +38,7 @@ kotlin {
sourceSets {
val commonMain by getting {
dependencies {
api(libs.wgpu4k)
implementation(libs.kotlinx.atomicfu)
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.serialization.json)
Expand All @@ -47,11 +49,13 @@ kotlin {
//noinspection UseTomlInstead
val jvmMain by getting {
dependencies {
implementation(project(":wgpu-ffm"))
implementation(project(":wgpu-natives"))

implementation(libs.mp3.decoder)

// Helper to create surface
implementation(libs.rococoa)
implementation(libs.jnaPlatform)

implementation(libs.lwjgl.core)
implementation(libs.lwjgl.glfw)
implementation(libs.lwjgl.openal)
Expand Down Expand Up @@ -84,16 +88,18 @@ kotlin {

all {
languageSettings.apply {
progressiveMode = true
optIn("kotlin.contracts.ExperimentalContracts")
optIn("kotlin.time.ExperimentalTime")
}
}

targets.configureEach {
compilations.configureEach {
compilerOptions.configure { freeCompilerArgs.add("-Xexpect-actual-classes") }
}
}
}

@OptIn(ExperimentalKotlinGradlePluginApi::class)
compilerOptions {
// Fix warning, then turn this on
allWarningsAsErrors = false
progressiveMode = true
freeCompilerArgs.add("-Xexpect-actual-classes")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,3 @@ abstract class ContextConfiguration {
abstract val title: String
open val loadInternalResources: Boolean = true
}

/** When requesting an adapter, what is the preferred power usage. */
enum class PowerPreference {
/** Lower power adapter. */
LOW_POWER,

/** High power adapter. */
HIGH_POWER
}
17 changes: 10 additions & 7 deletions core/src/commonMain/kotlin/com/littlekt/Graphics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package com.littlekt

import com.littlekt.graphics.Cursor
import com.littlekt.graphics.SystemCursor
import com.littlekt.graphics.webgpu.*
import io.ygdrasil.wgpu.Adapter
import io.ygdrasil.wgpu.Device
import io.ygdrasil.wgpu.PresentMode
import io.ygdrasil.wgpu.TextureFormat
import io.ygdrasil.wgpu.TextureUsage
import io.ygdrasil.wgpu.CompositeAlphaMode
import io.ygdrasil.wgpu.Surface

/**
* Contains graphic related properties and methods.
Expand Down Expand Up @@ -36,18 +42,15 @@ interface Graphics {
/** @return the preferred [TextureFormat] of the underlying [surface]. */
val preferredFormat: TextureFormat

/** @return the capability of the underlying [surface] */
val surfaceCapabilities: SurfaceCapabilities

/**
* Configures the underlying [surface], via [Surface.configure], which initializes the surface
* for presentation.
*/
fun configureSurface(
usage: TextureUsage = TextureUsage.RENDER_ATTACHMENT,
usage: Set<TextureUsage> = setOf(TextureUsage.renderAttachment),
format: TextureFormat = preferredFormat,
presentMode: PresentMode = PresentMode.FIFO,
alphaMode: AlphaMode = surfaceCapabilities.alphaModes[0]
presentMode: PresentMode = PresentMode.fifo,
alphaMode: CompositeAlphaMode = surface.supportedAlphaMode.first()
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.littlekt.graphics.g2d.TextureSlice
import com.littlekt.graphics.g2d.tilemap.ldtk.*
import com.littlekt.graphics.slice
import com.littlekt.graphics.sliceWithBorderToTexture
import com.littlekt.graphics.webgpu.TextureFormat
import com.littlekt.log.Logger
import com.littlekt.math.geom.Point

Expand Down Expand Up @@ -666,10 +665,7 @@ internal class LDtkLevelLoader(
.readPixmap()
.sliceWithBorderToTexture(
device = vfs.vfs.context.graphics.device,
preferredFormat =
if (vfs.vfs.context.graphics.preferredFormat.srgb)
TextureFormat.RGBA8_UNORM_SRGB
else TextureFormat.RGBA8_UNORM,
preferredFormat = vfs.vfs.context.graphics.preferredFormat,
sliceWidth = tilesetDef.tileGridSize,
sliceHeight = tilesetDef.tileGridSize,
border = sliceBorder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import com.littlekt.graphics.g2d.TextureSlice
import com.littlekt.graphics.g2d.font.*
import com.littlekt.graphics.g2d.tilemap.ldtk.LDtkWorld
import com.littlekt.graphics.g2d.tilemap.tiled.TiledMap
import com.littlekt.graphics.webgpu.TextureFormat
import com.littlekt.math.MutableVec4i
import com.littlekt.util.internal.unquote
import io.ygdrasil.wgpu.TextureFormat
import kotlin.math.max

/**
Expand Down Expand Up @@ -308,9 +308,8 @@ expect suspend fun VfsFile.readPixmap(): Pixmap
* @return the loaded texture
*/
expect suspend fun VfsFile.readTexture(
preferredFormat: TextureFormat =
if (vfs.context.graphics.preferredFormat.srgb) TextureFormat.RGBA8_UNORM_SRGB
else TextureFormat.RGBA8_UNORM
preferredFormat: TextureFormat = vfs.context.graphics.preferredFormat

): Texture

/**
Expand Down
1 change: 1 addition & 0 deletions core/src/commonMain/kotlin/com/littlekt/graphics/Color.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ open class Color(r: Float, g: Float, b: Float, a: Float = 1f) : Vec4f(r, g, b, a

/** @return a new [MutableColor] using the color values of this color. */
fun toMutableColor(result: MutableColor = MutableColor()) = result.set(r, g, b, a)
fun toWebGPUColor() = io.ygdrasil.wgpu.Color(r.toDouble(), g.toDouble(), b.toDouble(), a.toDouble())

companion object {
val CLEAR = Color(0f, 0f, 0f, 0f)
Expand Down
32 changes: 20 additions & 12 deletions core/src/commonMain/kotlin/com/littlekt/graphics/EmptyTexture.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package com.littlekt.graphics

import com.littlekt.graphics.Texture.Companion.nextId
import com.littlekt.graphics.webgpu.*
import io.ygdrasil.wgpu.Device
import io.ygdrasil.wgpu.Sampler
import io.ygdrasil.wgpu.SamplerDescriptor
import io.ygdrasil.wgpu.Size3D
import io.ygdrasil.wgpu.TextureDescriptor
import io.ygdrasil.wgpu.TextureDimension
import io.ygdrasil.wgpu.TextureFormat
import io.ygdrasil.wgpu.TextureUsage
import io.ygdrasil.wgpu.TextureView
import io.ygdrasil.wgpu.TextureViewDescriptor

import io.ygdrasil.wgpu.Texture as WebGPUTexture

/**
* A [Texture] that doesn't contain any underlying raw image data, but instead, is intended to be
* used in a render pass as an output. By default, the [textureDescriptor] uses
* `TextureUsage.TEXTURE or TextureUsage.RENDER_ATTACHMENT` as usage.
* `TextureUsage.textureBinding or TextureUsage.renderAttachment` as usage.
*
* @param device the device for underlying GPU buffers creation
* @param preferredFormat the preferred [TextureFormat]
Expand All @@ -19,7 +30,7 @@ class EmptyTexture(val device: Device, preferredFormat: TextureFormat, width: In
* The [Extent3D] size of the texture. Uses the initial width & height from the constructor and
* a depth of `1`.
*/
override var size: Extent3D = Extent3D(width, height, 1)
override var size: Size3D = Size3D(width, height, 1)
private set

override var id: Int = nextId()
Expand All @@ -29,32 +40,29 @@ class EmptyTexture(val device: Device, preferredFormat: TextureFormat, width: In
override var textureDescriptor: TextureDescriptor =
TextureDescriptor(
size,
1,
1,
TextureDimension.D2,
preferredFormat,
TextureUsage.TEXTURE or TextureUsage.RENDER_ATTACHMENT
setOf(TextureUsage.textureBinding, TextureUsage.renderAttachment)
)
set(value) {
field = value
val textureToDestroy = gpuTexture
textureToDestroy.release()
textureToDestroy.close()
gpuTexture = device.createTexture(field)
}

/** The current [WebGPUTexture]. Changing the value of this field WILL change [id]. */
override var gpuTexture: WebGPUTexture = device.createTexture(textureDescriptor)
set(value) {
field = value
view.release()
view.close()
view = field.createView(textureViewDescriptor)
}

/** The current [TextureViewDescriptor]. Changing the value of this field WILL change [id]. */
override var textureViewDescriptor: TextureViewDescriptor? = null
set(value) {
field = value
view.release()
view.close()
view = gpuTexture.createView(value)
}

Expand All @@ -71,7 +79,7 @@ class EmptyTexture(val device: Device, preferredFormat: TextureFormat, width: In
override var samplerDescriptor: SamplerDescriptor = SamplerDescriptor()
set(value) {
field = value
sampler.release()
sampler.close()
sampler = device.createSampler(value)
}

Expand All @@ -89,7 +97,7 @@ class EmptyTexture(val device: Device, preferredFormat: TextureFormat, width: In
* creating a whole new one.
*/
fun resize(width: Int, height: Int) {
size = Extent3D(width, height, 1)
size = Size3D(width, height, 1)
textureDescriptor = textureDescriptor.copy(size = size)
}

Expand Down
Loading