I am trying to play around with WebGPU in GWT 2.9.0 using JsInterop and I face some problems trying to map all the WebGPU interfaces to Java. The definitions I refer to are located at https://www.w3.org/TR/webgpu/#idl-index
1) How do I map an unsigned long long?
There is a typedef: typedef [EnforceRange] unsigned long long GPUSize64;
that is used for example here:
interface mixin GPURenderEncoderBase {
//...other declarations left out...
undefined drawIndirect(GPUBuffer indirectBuffer, GPUSize64 indirectOffset);
};
if I wrap it like
@JsType(isNative = true, namespace = JsPackage.GLOBAL)
public class GPURenderEncoderBase {
//...other declarations left out...
@JsMethod
public final native void drawIndirect(GPUBuffer indirectBuffer, long indirectOffset);
}
I get an error saying:
Parameter 'sourceOffset': type 'long' is not safe to access in JSNI code
given that my higher level API only exposes an int here for compatibility with other APIs i could probably just use an int, but what is the correct solution to map the GPUSize64?
2) How do I wrap a dictonary?
When I try to translate the following definition
dictionary GPUExtent3DDict {
required GPUIntegerCoordinate width;
GPUIntegerCoordinate height = 1;
GPUIntegerCoordinate depthOrArrayLayers = 1;
};
typedef (sequence<GPUIntegerCoordinate> or GPUExtent3DDict) GPUExtent3D;
like so:
@JsType(isNative = false, namespace = JsPackage.GLOBAL)
public class GPUExtent3D {
public int width;
public int height = 1;
public int depthOrArrayLayers = 1;
}
and then use it in the following way:
...
GPUExtent3D size = new GPUExtent3D();
size.width = canvasWidth;
size.height = canvasHeight;
GPUCanvasConfiguration config = new GPUCanvasConfiguration();
config.size = size;
gpuCanvasContext.configure(config);
I can compile fine but get an error at runtime saying
Uncaught (in promise) TypeError: Failed to execute 'configure' on 'GPUCanvasContext': Failed to read the 'size' property from 'GPUCanvasConfiguration': Failed to read the 'width' property from 'GPUExtent3DDict': Failed to read the 'width' property from 'GPUExtent3DDict': Required member is undefined.
What confuses me is that it says "Failed to read the 'width' property from 'GPUExtent3DDict'" twice which hints that it expects something nested and probably has to do with the last line in the typedef about the "sequence or GPUExtent3DDict" which I dont understand. When I instead define GPUExtent3D that way:
public final class GPUExtent3D extends JavaScriptObject {
public static final native GPUExtent3D createNew() /*-{
return {height: 1, depthOrArrayLayers: 1};
}-*/;
protected GPUExtent3D() {}
public final native void width(int width) /*-{
this["width"] = width;
}-*/;
//...same for height and depthOrArrayLayers
}
and then use it like:
...
GPUExtent3D size = GPUExtent3D.createNew();
size.width(canvasWidth);
size.height(canvasHeight);
size.depthOrArrayLayers(1);
GPUCanvasConfiguration config = new GPUCanvasConfiguration();
config.size = size;
gpuCanvasContext.configure(config);
It works just fine, but I would like to do it the JsInterop way instead of extent JavaScriptObject. How would I go about that?
3) How to map an enum?
I also found a working solution here and I would like to hear if this is recommended or maybe deprecated / old way of doing it Given an enum declaration:
enum GPUPowerPreference {
"low-power",
"high-performance"
};
Can I just map it like
public final class GPUPowerPreference {
public static final String LOW_POWER = "low-power";
public static final String HIGH_POWER = "high-power";
private GPUPowerPreference() {}
}
Or is there a way to use a java enum with @JsEnum for this (I tried but had problems with the dash that is used in the value)
Thanks a lot and have a nice day!