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
175 lines (149 loc) · 4.68 KB
/
PWebGPU.java
File metadata and controls
175 lines (149 loc) · 4.68 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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 surfaceId = processing_create_surface(windowHandle, width, height, scaleFactor);
checkError();
return surfaceId;
}
/**
* Destroys a WebGPU surface.
*
* @param surfaceId The window ID returned from createSurface
*/
public static void destroySurface(long surfaceId) {
processing_destroy_surface(surfaceId);
checkError();
}
/**
* Updates a window's size.
*
* @param surfaceId 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 surfaceId, int width, int height) {
processing_resize_surface(surfaceId, width, height);
checkError();
}
public static void beginDraw(long surfaceId) {
processing_begin_draw(surfaceId);
checkError();
}
public static void flush(long surfaceId) {
processing_flush(surfaceId);
checkError();
}
public static void endDraw(long surfaceId) {
processing_end_draw(surfaceId);
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 surfaceId, 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(surfaceId, color);
checkError();
}
}
/**
* Set the fill color.
*/
public static void setFill(long surfaceId, float r, float g, float b, float a) {
processing_set_fill(surfaceId, r, g, b, a);
checkError();
}
/**
* Set the stroke color.
*/
public static void setStrokeColor(long surfaceId, float r, float g, float b, float a) {
processing_set_stroke_color(surfaceId, r, g, b, a);
checkError();
}
/**
* Set the stroke weight.
*/
public static void setStrokeWeight(long surfaceId, float weight) {
processing_set_stroke_weight(surfaceId, weight);
checkError();
}
/**
* Disable fill for subsequent shapes.
*/
public static void noFill(long surfaceId) {
processing_no_fill(surfaceId);
checkError();
}
/**
* Disable stroke for subsequent shapes.
*/
public static void noStroke(long surfaceId) {
processing_no_stroke(surfaceId);
checkError();
}
/**
* Draw a rectangle.
*/
public static void rect(long surfaceId, float x, float y, float w, float h,
float tl, float tr, float br, float bl) {
processing_rect(surfaceId, x, y, w, h, tl, tr, br, bl);
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);
}
}
}