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

Conversation

ygdrasil-io
Copy link

Not yet compiling. Still plenty of work ahead!

I'm keeping this as a draft until the full release on Maven Central. (For now, I'm using snapshot repositories for faster iteration.)

I'm using this pull request to improve the binding by adding missing features. Functions that deviate significantly from the original specifications are implemented as extensions.

I plan to switch enumeration names to CamelCase to align with Kotlin conventions and distinguish them from C/Java styles.

After replacing the original implementation, I'll create the basic setup to get Android working.

While a review isn't necessary at this stage, I'm open to any feedback you may have.

Moved to Kotlin 2.0.0 and updated dependencies including wgpu4k to preview-3. Changed JVM target to 22 in multiple Gradle scripts and replaced deprecated methods with `allocateFrom` in native code handling.
Updated README.md, build scripts, and GitHub workflows to use JDK 22. This ensures compatibility with the latest Java FFM API and keeps project dependencies current. Also updated Kotlin Coroutines version to 1.9.0.
Replaced 'com.littlekt.graphics.webgpu' imports with 'io.ygdrasil.wgpu' throughout the codebase. Simplified the determination of the preferred texture format and improved consistency in the usage of WebGPU methods and properties.
Replaced the webgpu library with ygdrasil.wgpu and made necessary adjustments to accommodate the new library's API. This includes updating import statements, method calls, and fixing code formatting issues to ensure compatibility and improve readability.
Standardize GPU buffer creation functions and refactor WebGPU imports. Added multiple utility blend states to improve sprite rendering flexibility. Simplified shader and render pipeline configurations by using descriptive blend states and updated method names to align with coding standards.
Remove unused imports and redundant code related to WebGPU in LwjglGraphics and VertexBufferLayout. Simplify the Device class by eliminating unnecessary properties and methods. This refactor aims to streamline the code and improve maintainability.
Corrected various typos in enums and class names, improving code readability and consistency. Also updated the `wgpu4k` dependency version to `0.0.0-SNAPSHOT` in the `gradle/libs.versions.toml` file, ensuring compatibility with the latest changes.
Updated import paths to the new `io.ygdrasil.wgpu` package and replaced deprecated method calls. Reformatted code blocks for consistency and applied uniform naming for buffer usages and vertex attributes.
Switch to io.ygdrasil.wgpu for WGPU functionalities, updating import paths and instance creation. Refactor vertex attribute handling to use sizeInByte instead of bytes for stride calculations, and correct usages of texture formats and pipeline states in render passes.
This commit eliminates redundant WebGPU graphics and surface handling code from the `core` and `examples` directories. It also removes unnecessary imports, streamlining the codebase and improving maintainability.
@LeHaine
Copy link
Member

LeHaine commented Sep 25, 2024

This is great! You are a workhorse. Looking good so far but I can get a deeper dive into this when I am back at some point next week.

Nicd work.

Updated `setIndexBuffer` to use the correct enum value and renamed `release` method to `close`. This improves consistency and accuracy in the WebGPU function calls within the SpriteCache class.
Removed unused imports and cleaned up import statements. Corrected inconsistencies in enum names and refined shader update and binding methods to enhance clarity and functionality.
Removed unused imports and updated import statements for better clarity. Replaced `VertexBufferLayout` with `VertexBufferLayoutView` and `VertexAttribute` with `VertexAttributeView` for consistency. Updated buffer usage and shader handling methods for enhanced functionality and clarity.
Updated several GPU-related classes to remove unnecessary comments and simplify constructors. Consolidated import statements and reduced verbosity by removing redundant or obvious documentation. Improved maintainability and readability by streamlining the code structure and usage.
Removed redundant GPU-related classes and interfaces across multiple files to streamline the codebase. Consolidated functions and removed verbose or trivial comments. This improves maintainability and enhances code readability by focusing on essential functionality and reducing clutter.
Removed redundant GPU-related classes and interfaces across multiple files to streamline the codebase. Consolidated functions for surface creation based on OS-specific logic. Enhanced code readability and maintainability by focusing on essential functionality and reducing clutter.
Added `toWebGPUColor` method for converting colors to WebGPU format in `Color.kt`. Refactored multiple classes to replace deprecated WebGPU constructs with updated ones, ensuring code compatibility and modernization. Additionally, disabled `allWarningsAsErrors` in the build script temporarily for smoother development.
Replaced deprecated API calls with the latest standardized methods across texture handling and rendering processes. This includes changes to TextureUsage enums, buffer creation functions, and various layout descriptors to ensure compatibility and optimize performance.
Deleted native binary files, Gradle build scripts, and headers related to WebGPU. This change is made to clean up deprecated or unnecessary dependencies and configurations.
Updated TextureDataLayout initialization in multiple files to set the proper offset and row pitch. This ensures consistency and clarity in the data layout configuration for texture uploads.
This change adjusts the buffer size calculation to use `Float.SIZE_BYTES` instead of `Short.SIZE_BYTES`. It ensures that the buffer size accurately reflects the data type being used, preventing possible memory allocation issues.
Changed the writeBuffer method's parameter to use a named argument for size to improve code readability and maintainability. This small refactor ensures the intention of the size parameter is clear and reduces potential errors.
@ygdrasil-io
Copy link
Author

This is now compiling, but some examples are crashing, and I'm unsure if the colors are always matching the initial results. While promising, there's still work to be done!

@ygdrasil-io
Copy link
Author

I'm making good progress on a generator to easily update the bindings with each native library upgrade. At the current pace, it should be finished within one or two weeks.

I'll leverage the upcoming iteration in gfx-rs/wgpu-native#427 to test the generator and then update this PR with any necessary bug fixes.

I hope the generator will help stabilize support for all platforms and facilitate publishing the entire project on Maven Central.

@LeHaine In the meantime, if you have time, please review the changes and let me know if you find any issues. Based on the current work, only some (potentially many) typos in the binding will be corrected to align more closely with the specification. However, the overall usage should remain consistent.

@LeHaine
Copy link
Member

LeHaine commented Oct 2, 2024

Great news and great work! I will check it out today.

@LeHaine
Copy link
Member

LeHaine commented Oct 2, 2024

@ygdrasil-io I've pulled down your code and have been testing. Overall, working pretty well! But I have run into a couple issues:

1, When the SurfaceTexture is lost (e.g. from resizing the window), we usually close / release the surface texture. It seems that the underlying texture can be invalid / null. In the WGPU native examples, they do show that they check if the texture is not null before attempting to release it.

SurfaceTextureStatus.lost -> {
-   surfaceTexture.texture.close() // this is the cause
+  surfaceTexture.texture?.close() // might need to make the underlying texture nullable
    graphics.configureSurface(
        setOf(TextureUsage.renderAttachment),
        preferredFormat,
        PresentMode.fifo,
        graphics.surface.supportedAlphaMode.first()
    )
    logger.info { "getCurrentTexture status=$status" }
    return@onUpdate
}
  1. A strange one that I am seeing is a device's SupportedLimits not returning the expected value. The error: Dynamic binding index 0 (targeting bind group 0, binding 0) with value 64, does not respect device's requested 'min_uniform_buffer_offset_alignment' limit: 256 when using the value from:
device.limits.minUniformBufferOffsetAlignment

This returns a value of 64 but for some reason is expecting a value of 256 which causes a validation error and crashes.

From a quick glance, it looks like the underlying Device is using the Adapter limits instead of the Device:

val supportedLimits = WGPUSupportedLimits.allocate(arena)
wgpu_h.wgpuAdapterGetLimits(handler, supportedLimits) // <---- this guy
map(WGPUSupportedLimits.limits(supportedLimits))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants