Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions core/examples/src/main/java/WebGPU.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import processing.core.PApplet;

public class WebGPU extends PApplet {
public void settings() {
size(600, 400, WEBGPU);
}

public void draw() {
background(200);

noStroke();
fill(255, 0, 0);
rect(50, 50, 80, 80);

fill(0, 255, 0);
rect(150, 50, 80, 80);

fill(0, 0, 255);
rect(250, 50, 80, 80);

fill(255, 0, 0, 128);
rect(50, 150, 80, 80);

fill(0, 255, 0, 128);
rect(150, 150, 80, 80);

fill(0, 0, 255, 128);
rect(250, 150, 80, 80);

stroke(0);
strokeWeight(4);
fill(255, 200, 0);
rect(50, 250, 80, 80);

fill(200, 0, 255);
rect(150, 250, 80, 80);

noFill();
stroke(255, 0, 255);
strokeWeight(6);
rect(250, 250, 80, 80);

noStroke();
fill(255, 0, 0, 100);
rect(400, 100, 100, 100);

fill(0, 255, 0, 100);
rect(440, 140, 100, 100);

fill(0, 0, 255, 100);
rect(420, 180, 100, 100);
}

public static void main(String[] args) {
PApplet.disableAWT = true;
PApplet.main(WebGPU.class.getName());
}
}
100 changes: 88 additions & 12 deletions core/src/processing/webgpu/PGraphicsWebGPU.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,131 @@
import processing.core.PSurface;

public class PGraphicsWebGPU extends PGraphics {
private long windowId = 0;
private long surfaceId = 0;

@Override
public PSurface createSurface() {
return surface = new PSurfaceGLFW(this);
}

protected void initWebGPUSurface(long windowHandle, int width, int height, float scaleFactor) {
windowId = PWebGPU.createSurface(windowHandle, width, height, scaleFactor);
if (windowId == 0) {
protected void initWebGPUSurface(long windowHandle, long displayHandle, int width, int height, float scaleFactor) {
surfaceId = PWebGPU.createSurface(windowHandle, displayHandle, width, height, scaleFactor);
if (surfaceId == 0) {
System.err.println("Failed to create WebGPU surface");
}
}

@Override
public void setSize(int w, int h) {
super.setSize(w, h);
if (windowId != 0) {
PWebGPU.windowResized(windowId, pixelWidth, pixelHeight);
if (surfaceId != 0) {
PWebGPU.windowResized(surfaceId, pixelWidth, pixelHeight);
}
}

@Override
public void beginDraw() {
super.beginDraw();
checkSettings();
System.out.println("Beginning draw on surfaceId: " + surfaceId);
PWebGPU.beginDraw(surfaceId);
}

@Override
public void flush() {
super.flush();
PWebGPU.flush(surfaceId);
}

@Override
public void endDraw() {
super.endDraw();
PWebGPU.update();
PWebGPU.endDraw(surfaceId);
}

@Override
public void dispose() {
super.dispose();
if (windowId != 0) {
PWebGPU.destroySurface(windowId);
windowId = 0;
if (surfaceId != 0) {
PWebGPU.destroySurface(surfaceId);
surfaceId = 0;
}
PWebGPU.exit();
}

@Override
protected void backgroundImpl() {
if (windowId == 0) {
if (surfaceId == 0) {
return;
}
PWebGPU.backgroundColor(surfaceId, backgroundR, backgroundG, backgroundB, backgroundA);
}

@Override
protected void fillFromCalc() {
super.fillFromCalc();
if (surfaceId == 0) {
return;
}
if (fill) {
PWebGPU.setFill(surfaceId, fillR, fillG, fillB, fillA);
} else {
PWebGPU.noFill(surfaceId);
}
}

@Override
protected void strokeFromCalc() {
super.strokeFromCalc();
if (surfaceId == 0) {
return;
}
if (stroke) {
PWebGPU.setStrokeColor(surfaceId, strokeR, strokeG, strokeB, strokeA);
} else {
PWebGPU.noStroke(surfaceId);
}
}

@Override
public void strokeWeight(float weight) {
super.strokeWeight(weight);
if (surfaceId == 0) {
return;
}
PWebGPU.setStrokeWeight(surfaceId, weight);
}

@Override
public void noFill() {
super.noFill();
if (surfaceId == 0) {
return;
}
PWebGPU.noFill(surfaceId);
}

@Override
public void noStroke() {
super.noStroke();
if (surfaceId == 0) {
return;
}
PWebGPU.noStroke(surfaceId);
}

@Override
protected void rectImpl(float x1, float y1, float x2, float y2) {
rectImpl(x1, y1, x2, y2, 0, 0, 0, 0);
}

@Override
protected void rectImpl(float x1, float y1, float x2, float y2,
float tl, float tr, float br, float bl) {
if (surfaceId == 0) {
return;
}
PWebGPU.backgroundColor(windowId, backgroundR, backgroundG, backgroundB, backgroundA);
// rectImpl receives corner coordinates, so let's convert to x,y,w,h
PWebGPU.rect(surfaceId, x1, y1, x2 - x1, y2 - y1, tl, tr, br, bl);
}
}
31 changes: 24 additions & 7 deletions core/src/processing/webgpu/PSurfaceGLFW.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package processing.webgpu;

import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
import org.lwjgl.glfw.GLFWNativeCocoa;
import org.lwjgl.glfw.GLFWNativeWin32;
import org.lwjgl.glfw.GLFWWindowPosCallback;
import org.lwjgl.glfw.*;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.system.Platform;

Expand All @@ -30,6 +25,7 @@ public class PSurfaceGLFW implements PSurface {
protected PGraphics graphics;

protected long window;
protected long display;
protected boolean running = false;

protected boolean paused;
Expand Down Expand Up @@ -78,6 +74,8 @@ public void initFrame(PApplet sketch) {
throw new RuntimeException("Failed to create GLFW window");
}

display = GLFW.glfwGetPrimaryMonitor();

windowCount.incrementAndGet();

// event callbacks
Expand All @@ -87,11 +85,12 @@ public void initFrame(PApplet sketch) {
PWebGPU.init();

long windowHandle = getWindowHandle();
long displayHandle = getDisplayHandle();
int width = sketch.sketchWidth();
int height = sketch.sketchHeight();
float scaleFactor = sketch.sketchPixelDensity();

webgpu.initWebGPUSurface(windowHandle, width, height, scaleFactor);
webgpu.initWebGPUSurface(windowHandle, displayHandle, width, height, scaleFactor);
}
}

Expand Down Expand Up @@ -123,6 +122,24 @@ public long getWindowHandle() {
return GLFWNativeCocoa.glfwGetCocoaWindow(window);
} else if (Platform.get() == Platform.WINDOWS) {
return GLFWNativeWin32.glfwGetWin32Window(window);
} else if (Platform.get() == Platform.LINUX) {
// TODO: need to check if x11 or wayland
return GLFWNativeWayland.glfwGetWaylandWindow(window);
} else {
throw new UnsupportedOperationException("Window handle retrieval not implemented for this platform");
}
}

public long getDisplayHandle() {
if (Platform.get() == Platform.MACOSX) {
// TODO: Currently unsupported
return 0;
} else if (Platform.get() == Platform.WINDOWS) {
// TODO: Currently unsupported
return 0;
Comment on lines +135 to +139
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasn't sure what to do here since we don't use display handle on MacOS and windows yet.

} else if (Platform.get() == Platform.LINUX) {
// TODO: need to check if x11 or wayland
return GLFWNativeWayland.glfwGetWaylandDisplay();
Comment on lines +141 to +142
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think i'll add x11 to this if we can test in the short term. otherwise it could go in another PR.
but happy to just do it here as well. i think i just have to check for a null pointer for Wayland or X11.

} else {
throw new UnsupportedOperationException("Window handle retrieval not implemented for this platform");
}
Expand Down
Loading
Loading