Skip to content
Open
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());
}
}
98 changes: 87 additions & 11 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) {
surfaceId = PWebGPU.createSurface(windowHandle, 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);
}
}
87 changes: 72 additions & 15 deletions core/src/processing/webgpu/PWebGPU.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,46 @@ public static void init() {
* @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);
long surfaceId = processing_create_surface(windowHandle, width, height, scaleFactor);
checkError();
return windowId;
return surfaceId;
}

/**
* Destroys a WebGPU surface.
*
* @param windowId The window ID returned from createSurface
* @param surfaceId The window ID returned from createSurface
*/
public static void destroySurface(long windowId) {
processing_destroy_surface(windowId);
public static void destroySurface(long surfaceId) {
processing_destroy_surface(surfaceId);
checkError();
}

/**
* Updates a window's size.
*
* @param windowId The window ID returned from createSurface
* @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 windowId, int width, int height) {
processing_resize_surface(windowId, width, height);
public static void windowResized(long surfaceId, int width, int height) {
processing_resize_surface(surfaceId, width, height);
checkError();
}

/**
* Updates the WebGPU subsystem. Should be called once per frame after all drawing is complete.
*/
public static void update() {
processing_update();

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();
}

Expand All @@ -87,7 +95,7 @@ public static void exit() {
checkError();
}

public static void backgroundColor(long windowId, float r, float g, float b, float a) {
public static void backgroundColor(long surfaceId, float r, float g, float b, float a) {
try (Arena arena = Arena.ofConfined()) {
MemorySegment color = Color.allocate(arena);

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

processing_background_color(windowId, color);
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.
*/
Expand Down
Loading