forked from processing/processing4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPWebGPU.java
More file actions
118 lines (101 loc) · 3.27 KB
/
PWebGPU.java
File metadata and controls
118 lines (101 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package processing.webgpu;
import processing.core.NativeLibrary;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import static java.lang.foreign.MemorySegment.NULL;
import static processing.ffi.processing_h.*;
import processing.ffi.Color;
/**
* PWebGPU provides the native interface layer for libProcessing's WebGPU support.
*/
public class PWebGPU {
static {
ensureLoaded();
}
/**
* Ensure the native library is loaded.
*/
public static void ensureLoaded() {
NativeLibrary.ensureLoaded();
}
/**
* Initializes the WebGPU subsystem. Must be called before any other WebGPU methods.
* This should be called from the same thread that will call update().
*/
public static void init() {
processing_init();
checkError();
}
/**
* Creates a WebGPU surface from a native window handle.
*
* @param windowHandle The native window handle
* @param width Window width in physical pixels
* @param height Window height in phsyical pixels
* @param scaleFactor os provided scale factor
* @return Window ID to use for subsequent operations
*/
public static long createSurface(long windowHandle, int width, int height, float scaleFactor) {
long windowId = processing_create_surface(windowHandle, width, height, scaleFactor);
checkError();
return windowId;
}
/**
* Destroys a WebGPU surface.
*
* @param windowId The window ID returned from createSurface
*/
public static void destroySurface(long windowId) {
processing_destroy_surface(windowId);
checkError();
}
/**
* Updates a window's size.
*
* @param windowId The window ID returned from createSurface
* @param width New physical window width in pixels
* @param height New physical window height in pixels
*/
public static void windowResized(long windowId, int width, int height) {
processing_resize_surface(windowId, width, height);
checkError();
}
/**
* Updates the WebGPU subsystem. Should be called once per frame after all drawing is complete.
*/
public static void update() {
processing_update();
checkError();
}
/**
* Cleans up the WebGPU subsystem. Should be called on application exit.
*/
public static void exit() {
processing_exit((byte) 0);
checkError();
}
public static void backgroundColor(long windowId, float r, float g, float b, float a) {
try (Arena arena = Arena.ofConfined()) {
MemorySegment color = Color.allocate(arena);
Color.r(color, r);
Color.g(color, g);
Color.b(color, b);
Color.a(color, a);
processing_background_color(windowId, color);
checkError();
}
}
/**
* Checks for errors from the native library and throws a PWebGPUException if an error occurred.
*/
private static void checkError() {
MemorySegment ret = processing_check_error();
if (ret.equals(NULL)) {
return;
}
String errorMsg = ret.getString(0);
if (errorMsg != null && !errorMsg.isEmpty()) {
throw new PWebGPUException(errorMsg);
}
}
}