Skip to content

Commit 351737c

Browse files
authored
Implement basic rendering flow (#1322)
* Implement basic rendering flow * Fix naming. * Actually fix rename.
1 parent 1ee012e commit 351737c

File tree

17 files changed

+1204
-139
lines changed

17 files changed

+1204
-139
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import processing.core.PApplet;
2+
3+
public class WebGPU extends PApplet {
4+
public void settings() {
5+
size(600, 400, WEBGPU);
6+
}
7+
8+
public void draw() {
9+
background(200);
10+
11+
noStroke();
12+
fill(255, 0, 0);
13+
rect(50, 50, 80, 80);
14+
15+
fill(0, 255, 0);
16+
rect(150, 50, 80, 80);
17+
18+
fill(0, 0, 255);
19+
rect(250, 50, 80, 80);
20+
21+
fill(255, 0, 0, 128);
22+
rect(50, 150, 80, 80);
23+
24+
fill(0, 255, 0, 128);
25+
rect(150, 150, 80, 80);
26+
27+
fill(0, 0, 255, 128);
28+
rect(250, 150, 80, 80);
29+
30+
stroke(0);
31+
strokeWeight(4);
32+
fill(255, 200, 0);
33+
rect(50, 250, 80, 80);
34+
35+
fill(200, 0, 255);
36+
rect(150, 250, 80, 80);
37+
38+
noFill();
39+
stroke(255, 0, 255);
40+
strokeWeight(6);
41+
rect(250, 250, 80, 80);
42+
43+
noStroke();
44+
fill(255, 0, 0, 100);
45+
rect(400, 100, 100, 100);
46+
47+
fill(0, 255, 0, 100);
48+
rect(440, 140, 100, 100);
49+
50+
fill(0, 0, 255, 100);
51+
rect(420, 180, 100, 100);
52+
}
53+
54+
public static void main(String[] args) {
55+
PApplet.disableAWT = true;
56+
PApplet.main(WebGPU.class.getName());
57+
}
58+
}

core/src/processing/webgpu/PGraphicsWebGPU.java

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,131 @@
44
import processing.core.PSurface;
55

66
public class PGraphicsWebGPU extends PGraphics {
7-
private long windowId = 0;
7+
private long surfaceId = 0;
88

99
@Override
1010
public PSurface createSurface() {
1111
return surface = new PSurfaceGLFW(this);
1212
}
1313

1414
protected void initWebGPUSurface(long windowHandle, int width, int height, float scaleFactor) {
15-
windowId = PWebGPU.createSurface(windowHandle, width, height, scaleFactor);
16-
if (windowId == 0) {
15+
surfaceId = PWebGPU.createSurface(windowHandle, width, height, scaleFactor);
16+
if (surfaceId == 0) {
1717
System.err.println("Failed to create WebGPU surface");
1818
}
1919
}
2020

2121
@Override
2222
public void setSize(int w, int h) {
2323
super.setSize(w, h);
24-
if (windowId != 0) {
25-
PWebGPU.windowResized(windowId, pixelWidth, pixelHeight);
24+
if (surfaceId != 0) {
25+
PWebGPU.windowResized(surfaceId, pixelWidth, pixelHeight);
2626
}
2727
}
2828

2929
@Override
3030
public void beginDraw() {
3131
super.beginDraw();
3232
checkSettings();
33+
System.out.println("Beginning draw on surfaceId: " + surfaceId);
34+
PWebGPU.beginDraw(surfaceId);
35+
}
36+
37+
@Override
38+
public void flush() {
39+
super.flush();
40+
PWebGPU.flush(surfaceId);
3341
}
3442

3543
@Override
3644
public void endDraw() {
3745
super.endDraw();
38-
PWebGPU.update();
46+
PWebGPU.endDraw(surfaceId);
3947
}
4048

4149
@Override
4250
public void dispose() {
4351
super.dispose();
44-
if (windowId != 0) {
45-
PWebGPU.destroySurface(windowId);
46-
windowId = 0;
52+
if (surfaceId != 0) {
53+
PWebGPU.destroySurface(surfaceId);
54+
surfaceId = 0;
4755
}
4856
PWebGPU.exit();
4957
}
5058

5159
@Override
5260
protected void backgroundImpl() {
53-
if (windowId == 0) {
61+
if (surfaceId == 0) {
62+
return;
63+
}
64+
PWebGPU.backgroundColor(surfaceId, backgroundR, backgroundG, backgroundB, backgroundA);
65+
}
66+
67+
@Override
68+
protected void fillFromCalc() {
69+
super.fillFromCalc();
70+
if (surfaceId == 0) {
71+
return;
72+
}
73+
if (fill) {
74+
PWebGPU.setFill(surfaceId, fillR, fillG, fillB, fillA);
75+
} else {
76+
PWebGPU.noFill(surfaceId);
77+
}
78+
}
79+
80+
@Override
81+
protected void strokeFromCalc() {
82+
super.strokeFromCalc();
83+
if (surfaceId == 0) {
84+
return;
85+
}
86+
if (stroke) {
87+
PWebGPU.setStrokeColor(surfaceId, strokeR, strokeG, strokeB, strokeA);
88+
} else {
89+
PWebGPU.noStroke(surfaceId);
90+
}
91+
}
92+
93+
@Override
94+
public void strokeWeight(float weight) {
95+
super.strokeWeight(weight);
96+
if (surfaceId == 0) {
97+
return;
98+
}
99+
PWebGPU.setStrokeWeight(surfaceId, weight);
100+
}
101+
102+
@Override
103+
public void noFill() {
104+
super.noFill();
105+
if (surfaceId == 0) {
106+
return;
107+
}
108+
PWebGPU.noFill(surfaceId);
109+
}
110+
111+
@Override
112+
public void noStroke() {
113+
super.noStroke();
114+
if (surfaceId == 0) {
115+
return;
116+
}
117+
PWebGPU.noStroke(surfaceId);
118+
}
119+
120+
@Override
121+
protected void rectImpl(float x1, float y1, float x2, float y2) {
122+
rectImpl(x1, y1, x2, y2, 0, 0, 0, 0);
123+
}
124+
125+
@Override
126+
protected void rectImpl(float x1, float y1, float x2, float y2,
127+
float tl, float tr, float br, float bl) {
128+
if (surfaceId == 0) {
54129
return;
55130
}
56-
PWebGPU.backgroundColor(windowId, backgroundR, backgroundG, backgroundB, backgroundA);
131+
// rectImpl receives corner coordinates, so let's convert to x,y,w,h
132+
PWebGPU.rect(surfaceId, x1, y1, x2 - x1, y2 - y1, tl, tr, br, bl);
57133
}
58134
}

core/src/processing/webgpu/PWebGPU.java

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,46 @@ public static void init() {
4444
* @return Window ID to use for subsequent operations
4545
*/
4646
public static long createSurface(long windowHandle, int width, int height, float scaleFactor) {
47-
long windowId = processing_create_surface(windowHandle, width, height, scaleFactor);
47+
long surfaceId = processing_create_surface(windowHandle, width, height, scaleFactor);
4848
checkError();
49-
return windowId;
49+
return surfaceId;
5050
}
5151

5252
/**
5353
* Destroys a WebGPU surface.
5454
*
55-
* @param windowId The window ID returned from createSurface
55+
* @param surfaceId The window ID returned from createSurface
5656
*/
57-
public static void destroySurface(long windowId) {
58-
processing_destroy_surface(windowId);
57+
public static void destroySurface(long surfaceId) {
58+
processing_destroy_surface(surfaceId);
5959
checkError();
6060
}
6161

6262
/**
6363
* Updates a window's size.
6464
*
65-
* @param windowId The window ID returned from createSurface
65+
* @param surfaceId The window ID returned from createSurface
6666
* @param width New physical window width in pixels
6767
* @param height New physical window height in pixels
6868
*/
69-
public static void windowResized(long windowId, int width, int height) {
70-
processing_resize_surface(windowId, width, height);
69+
public static void windowResized(long surfaceId, int width, int height) {
70+
processing_resize_surface(surfaceId, width, height);
7171
checkError();
7272
}
7373

74-
/**
75-
* Updates the WebGPU subsystem. Should be called once per frame after all drawing is complete.
76-
*/
77-
public static void update() {
78-
processing_update();
74+
75+
public static void beginDraw(long surfaceId) {
76+
processing_begin_draw(surfaceId);
77+
checkError();
78+
}
79+
80+
public static void flush(long surfaceId) {
81+
processing_flush(surfaceId);
82+
checkError();
83+
}
84+
85+
public static void endDraw(long surfaceId) {
86+
processing_end_draw(surfaceId);
7987
checkError();
8088
}
8189

@@ -87,7 +95,7 @@ public static void exit() {
8795
checkError();
8896
}
8997

90-
public static void backgroundColor(long windowId, float r, float g, float b, float a) {
98+
public static void backgroundColor(long surfaceId, float r, float g, float b, float a) {
9199
try (Arena arena = Arena.ofConfined()) {
92100
MemorySegment color = Color.allocate(arena);
93101

@@ -96,11 +104,60 @@ public static void backgroundColor(long windowId, float r, float g, float b, flo
96104
Color.b(color, b);
97105
Color.a(color, a);
98106

99-
processing_background_color(windowId, color);
107+
processing_background_color(surfaceId, color);
100108
checkError();
101109
}
102110
}
103111

112+
/**
113+
* Set the fill color.
114+
*/
115+
public static void setFill(long surfaceId, float r, float g, float b, float a) {
116+
processing_set_fill(surfaceId, r, g, b, a);
117+
checkError();
118+
}
119+
120+
/**
121+
* Set the stroke color.
122+
*/
123+
public static void setStrokeColor(long surfaceId, float r, float g, float b, float a) {
124+
processing_set_stroke_color(surfaceId, r, g, b, a);
125+
checkError();
126+
}
127+
128+
/**
129+
* Set the stroke weight.
130+
*/
131+
public static void setStrokeWeight(long surfaceId, float weight) {
132+
processing_set_stroke_weight(surfaceId, weight);
133+
checkError();
134+
}
135+
136+
/**
137+
* Disable fill for subsequent shapes.
138+
*/
139+
public static void noFill(long surfaceId) {
140+
processing_no_fill(surfaceId);
141+
checkError();
142+
}
143+
144+
/**
145+
* Disable stroke for subsequent shapes.
146+
*/
147+
public static void noStroke(long surfaceId) {
148+
processing_no_stroke(surfaceId);
149+
checkError();
150+
}
151+
152+
/**
153+
* Draw a rectangle.
154+
*/
155+
public static void rect(long surfaceId, float x, float y, float w, float h,
156+
float tl, float tr, float br, float bl) {
157+
processing_rect(surfaceId, x, y, w, h, tl, tr, br, bl);
158+
checkError();
159+
}
160+
104161
/**
105162
* Checks for errors from the native library and throws a PWebGPUException if an error occurred.
106163
*/

0 commit comments

Comments
 (0)