Skip to content
Advertisement

WebGPU JsInterop wrapper

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:

JavaScript

if I wrap it like

JavaScript

I get an error saying:

JavaScript

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

JavaScript

like so:

JavaScript

and then use it in the following way:

JavaScript

I can compile fine but get an error at runtime saying

JavaScript

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:

JavaScript

and then use it like:

JavaScript

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:

JavaScript

Can I just map it like

JavaScript

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!

Advertisement

Answer

Firstly – If you want a ready-made jsinterop binding for WebGPU (and all the other browser APIs) that is built by processing the webidl from the specs and building the binding from the webidl then Akasha provides that in both GWT2.9 and J2CL compatible variants (The Java API is the same but the annotations and innerts differ slightly between the two variants). The coordinate for the latest version of akasha variant for GWT is org.realityforge.akasha:akasha-gwt:jar:0.29 and an example of a simple textured spinning cube is available at Main.java

The specific questions:

  • How do you represent a “unsigned long long”? This is either a double or an int … or a Double if the value is optional. None of these representations match exactly what the API expects but as long as the values are mapped consistently and sourced from external sources (i.e. gltf or other formats) then there is no significant penalty. I map it to int when the type is not optional and Double when optional. A bit ugly but the best that works in GWT-land (although J2CL will soon have a way to represent native longs IIRC)
  • How do I wrap a dictionary? As the underlying representation is just a javascript object there are many, many many different representations possible. This differs whether you are targeting j2cl or GWT but for GWT you generally you need something like below. In my generator I go a step further and define a builder as in GPUExtent3DDict (but note that this source is an example of the J2CL variant and has some slight differences from the code described below) and this makes construction of dictionary data much easier. i.e. GPUExtent3DDict.width( 10 ).height( 10 ).depthOrArrayLayers( 1 )
JavaScript
  • How to map an enum? As enums are really just bags of string values, a class with string constants is adequate. However if you want better usability (i.e. tab completion in IDEs such as IntelliJ IDEA) then I typically model them as below. And then annotate the parameters/return values that are typed as the enum with @GPUPowerPreference
JavaScript

For what it is worth. Working with WebGPU in java has proven to be a life saver, particularly as it is evolving rapidly at the moment and when the spec changes, compilation errors help you locate the problems 😉 Good luck!

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement