Skip to content
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/src/processing/webgpu/PGraphicsWebGPU.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package processing.webgpu;

public class PGraphicsWebGPU {
}
32 changes: 27 additions & 5 deletions core/src/processing/webgpu/PWebGPU.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package processing.webgpu;

import processing.core.NativeLibrary;
import processing.ffi.processing_h;

import java.lang.foreign.MemorySegment;

import static java.lang.foreign.MemorySegment.NULL;
import static processing.ffi.processing_h.processing_check_error;
import static processing.ffi.processing_h.processing_init;

/**
* PWebGPU provides the native interface layer for libProcessing's WebGPU support.
*/
public class PWebGPU {

static {
NativeLibrary.ensureLoaded();
ensureLoaded();
init();
}

/**
Expand All @@ -20,9 +26,25 @@ public static void ensureLoaded() {
}

/**
* It's just math, silly!
* Initializes the WebGPU subsystem. Must be called before any other WebGPU methods.
*/
public static void init() {
processing_init();
checkError();
}

/**
* Checks for errors from the native library and throws a PWebGPUException if an error occurred.
*/
public static long add(long left, long right) {
return processing_h.processing_add(left, right);
private static void checkError() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this means we would need to poll the rust side for any errors?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, this is a tedious consequence of ffi code. there are a few different c-style error patterns you can use, but they're all pretty manual. rust exceptions (panics) can't cross the ffi boundary (doing so is undefined behavior).

realistically, we'll probably only end up having like 20ish ffi functions. if we had hundreds, i'd suggest writing an annotation processor or some other kind of reflection magic in java to automate this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay and there is no way from Rust to push an event into Java?

MemorySegment ret = processing_check_error();
if (ret.equals(NULL)) {
return;
}

String errorMsg = ret.getString(0);
if (errorMsg != null && !errorMsg.isEmpty()) {
throw new PWebGPUException(errorMsg);
}
}
}
13 changes: 13 additions & 0 deletions core/src/processing/webgpu/PWebGPUException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package processing.webgpu;

/**
* Unchecked exception thrown for WebGPU-related errors.
* <p>
* WebGPU operations can fail for various reasons, such as unsupported hardware, but are not
* expected to be recoverable by the Processing application.
*/
public class PWebGPUException extends RuntimeException {
public PWebGPUException(String message) {
super(message);
}
}
6 changes: 2 additions & 4 deletions core/test/processing/webgpu/PWebGPUTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package processing.webgpu;

import org.junit.Test;
import static org.junit.Assert.*;

/**
* Tests for the PWebGPU native interface.
*/
public class PWebGPUTest {
@Test
public void testAddFunction() {
long result = PWebGPU.add(2, 2);
assertEquals("2 + 2 should equal 4", 4L, result);
public void itLoads() {
PWebGPU.ensureLoaded();
}
}
Loading