Skip to content

Commit

Permalink
update Pot
Browse files Browse the repository at this point in the history
  • Loading branch information
saharan committed Jan 15, 2025
1 parent ee896fe commit 9bd9a3f
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Pot/src/pot/core/FrameRateManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class FrameRateManager {
final maxDrawBegin = max(lastDrawBegin + targetInterval * MAX_FRAMERATE_RATIO, currentTime +
MIN_UPDATE_TIME);
final maxUpdateCount = !frameSkipEnabled || count < 10 ? 1 : max(1,
Math.round((maxDrawBegin - currentTime) / estimatedUpdateTime));
Math.floor(targetInterval / estimatedUpdateTime));
final idealUpdateCountFloat = (currentTime - prevTime) / max(targetInterval * 0.01,
targetInterval - estimatedUpdateTime);
final idealUpdateCount = idealUpdateCountFloat > 0.2 && idealUpdateCountFloat < 1.8 ? 1 : Math.round(idealUpdateCountFloat);
Expand Down
2 changes: 1 addition & 1 deletion Pot/src/pot/core/Pot.hx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Pot {
}

public function endUpdate():Void {
assert(asyncUpdate, "enable async update is disabled");
assert(asyncUpdate, "async update is disabled");
assert(onEndUpdate != null, "callback function not set");
onEndUpdate();
onEndUpdate = null;
Expand Down
9 changes: 6 additions & 3 deletions Pot/src/pot/graphics/bitmap/Bitmap.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pot.graphics.bitmap;

import haxe.io.Int32Array;
import js.html.ImageData;
import js.html.CanvasRenderingContext2D;
import haxe.ds.Vector;
Expand All @@ -14,13 +15,15 @@ class Bitmap {
public var width(default, null):Int = -1;
public var height(default, null):Int = -1;
public var numPixels(default, null):Int = -1;
public var pixels(default, null):Vector<Int> = null;
public var pixels(default, null):Int32Array = null;

var imageData:ImageData = null;

public function new(width:Int, height:Int) {
canvas = Browser.document.createCanvasElement();
c2d = canvas.getContext2d();
c2d = canvas.getContext2d({
willReadFrequently: true
});
g = new BitmapGraphics(c2d);
setSize(width, height);
}
Expand All @@ -44,7 +47,7 @@ class Bitmap {

public function loadPixels():Void {
if (pixels == null || pixels.length != numPixels) {
pixels = new Vector<Int>(numPixels);
pixels = new Int32Array(numPixels);
}
imageData = c2d.getImageData(0, 0, width, height);
final data = imageData.data;
Expand Down
70 changes: 61 additions & 9 deletions Pot/src/pot/graphics/bitmap/BitmapGraphics.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pot.graphics.bitmap;

import muun.la.Vec4;
import muun.la.Vec3;
import js.html.CanvasElement;
import js.html.CanvasRenderingContext2D;

Expand All @@ -19,7 +21,23 @@ class BitmapGraphics {
return c2d;
}

public function clear(r:Float = 0, g:Float = 0, b:Float = 0, a:Float = 1):Void {
overload extern public inline function clear(rgb:Vec3, a:Float = 1):Void {
clearImpl(rgb.x, rgb.y, rgb.z, a);
}

overload extern public inline function clear(rgba:Vec4):Void {
clearImpl(rgba.x, rgba.y, rgba.z, rgba.w);
}

overload extern public inline function clear(r:Float, g:Float, b:Float, a:Float = 1):Void {
clearImpl(r, g, b, a);
}

overload extern public inline function clear(rgb:Float, a:Float = 1):Void {
clearImpl(rgb, rgb, rgb, a);
}

function clearImpl(r:Float, g:Float, b:Float, a:Float):Void {
save();
c2d.clearRect(0, 0, canvas.width, canvas.height);
alpha();
Expand All @@ -37,15 +55,48 @@ class BitmapGraphics {
c2d.globalCompositeOperation = mode;
}

extern public inline function fillColor(r:Float, g:Float, b:Float, a:Float = 1.0):Void {
overload extern public inline function fillColor(rgb:Vec3, a:Float = 1):Void {
fillColorImpl(rgb.x, rgb.y, rgb.z, a);
}

overload extern public inline function fillColor(rgba:Vec4):Void {
fillColorImpl(rgba.x, rgba.y, rgba.z, rgba.w);
}

overload extern public inline function fillColor(r:Float, g:Float, b:Float, a:Float = 1):Void {
fillColorImpl(r, g, b, a);
}

overload extern public inline function fillColor(rgb:Float, a:Float = 1):Void {
fillColorImpl(rgb, rgb, rgb, a);
}

function fillColorImpl(r:Float, g:Float, b:Float, a:Float):Void {
c2d.fillStyle = 'rgba(${Std.int(r * 255 + 0.5)}, ${Std.int(g * 255 + 0.5)}, ${Std.int(b * 255 + 0.5)}, $a)';
}

extern public inline function strokeColor(r:Float, g:Float, b:Float, a:Float = 1.0):Void {
overload extern public inline function strokeColor(rgb:Vec3, a:Float = 1):Void {
strokeColorImpl(rgb.x, rgb.y, rgb.z, a);
}

overload extern public inline function strokeColor(rgba:Vec4):Void {
strokeColorImpl(rgba.x, rgba.y, rgba.z, rgba.w);
}

overload extern public inline function strokeColor(r:Float, g:Float, b:Float, a:Float = 1):Void {
strokeColorImpl(r, g, b, a);
}

overload extern public inline function strokeColor(rgb:Float, a:Float = 1):Void {
strokeColorImpl(rgb, rgb, rgb, a);
}

function strokeColorImpl(r:Float, g:Float, b:Float, a:Float):Void {
c2d.strokeStyle = 'rgba(${Std.int(r * 255 + 0.5)}, ${Std.int(g * 255 + 0.5)}, ${Std.int(b * 255 + 0.5)}, $a)';
}

extern public inline function font(name:String, size:Float, weight:FontWeight = Normal, fallback:FontFallback = SansSerif):Void {
extern public inline function font(name:String, size:Float, weight:FontWeight = Normal,
fallback:FontFallback = SansSerif):Void {
c2d.font = '$weight ${size}px "$name", $fallback';
}

Expand Down Expand Up @@ -156,12 +207,13 @@ class BitmapGraphics {
c2d.arcTo(c1x, c1y, c2x, c2y, r);
}

extern public inline function arc(x:Float, y:Float, r:Float, start:Float, end:Float, anticlockwise:Bool = false):Void {
extern public inline function arc(x:Float, y:Float, r:Float, start:Float, end:Float,
anticlockwise:Bool = false):Void {
c2d.arc(x, y, r, start, end, anticlockwise);
}

extern public inline function ellipse(x:Float, y:Float, rx:Float, ry:Float, rotation:Float, start:Float, end:Float,
anticlockwise:Bool = false):Void {
extern public inline function ellipse(x:Float, y:Float, rx:Float, ry:Float, rotation:Float, start:Float,
end:Float, anticlockwise:Bool = false):Void {
c2d.ellipse(x, y, rx, ry, rotation, start, end, anticlockwise);
}

Expand Down Expand Up @@ -219,8 +271,8 @@ class BitmapGraphics {
c2d.drawImage(src.source, dx, dy, dw, dh);
}

extern overload public inline function drawImage(src:BitmapSource, sx:Float, sy:Float, sw:Float, sh:Float, dx:Float, dy:Float,
dw:Float, dh:Float):Void {
extern overload public inline function drawImage(src:BitmapSource, sx:Float, sy:Float, sw:Float, sh:Float,
dx:Float, dy:Float, dw:Float, dh:Float):Void {
c2d.drawImage(src.source, sx, sy, sw, sh, dx, dy, dw, dh);
}
}
11 changes: 7 additions & 4 deletions Pot/src/pot/graphics/gl/Graphics.hx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Graphics {

var floatBlendEnabled:Bool = false;

public function new(canvas:CanvasElement) {
public function new(canvas:CanvasElement, antialias:Bool = true) {
this.canvas = canvas;
// gl = untyped WebGLDebugUtils.makeDebugContext(canvas.getContextWebGL2({
// premultipliedAlpha: false,
Expand All @@ -101,6 +101,7 @@ class Graphics {
gl = canvas.getContextWebGL2({
premultipliedAlpha: false,
preserveDrawingBuffer: true,
antialias: antialias,
stencil: true
});

Expand Down Expand Up @@ -438,21 +439,21 @@ class Graphics {
}

public function createTexture(width:Int, height:Int, format:TextureFormat = RGBA,
?type:TextureType = Int8):Texture {
?type:TextureType = UInt8):Texture {
final tex = new Texture(gl);
tex.init(width, height, format, type);
return tex;
}

public function loadBitmap(source:BitmapSource, format:TextureFormat = RGBA, type:TextureType = Int8,
public function loadBitmap(source:BitmapSource, format:TextureFormat = RGBA, type:TextureType = UInt8,
?flipY:Bool = true):Texture {
final tex = new Texture(gl);
tex.load(source, format, type, flipY);
return tex;
}

public function loadBitmapTo(dst:Texture, source:BitmapSource, format:TextureFormat = RGBA,
?type:TextureType = Int8, flipY:Bool = true):Void {
?type:TextureType = UInt8, flipY:Bool = true):Void {
dst.load(source, format, type, flipY);
}

Expand Down Expand Up @@ -574,6 +575,7 @@ class Graphics {
}

public function pushMatrix():Void {
sceneCheck(true, "begin scene before push/pop matrices");
if (matStackCount > MAT_STACK_SIZE - 16) {
throw new Error("matrix stack overflowed");
}
Expand All @@ -596,6 +598,7 @@ class Graphics {
}

public function popMatrix():Void {
sceneCheck(true, "begin scene before push/pop matrices");
if (matStackCount < 16) {
throw new Error("cannot pop matrix");
}
Expand Down
8 changes: 8 additions & 0 deletions Pot/src/pot/graphics/gl/Material.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ class Material {
public function new(shader:Shader = null) {
this.shader = shader;
}

public function copy():Material {
final res = new Material(shader);
for (key => value in uniformMap) {
res.uniformMap[key] = value;
}
return res;
}
}
2 changes: 1 addition & 1 deletion Pot/src/pot/graphics/gl/Object.hx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Object extends GLObject {

public var mode:ShapeMode = Triangles;

public final material:Material = new Material();
public var material:Material = new Material();

@:allow(pot.graphics.gl.Graphics)
function new(gl:GL2, attributes:Array<Attribute>) {
Expand Down
7 changes: 4 additions & 3 deletions Pot/src/pot/graphics/gl/RenderTexture.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ class RenderTexture {
return textures[srcIndex];
}

public function new(g:Graphics, width:Int, height:Int, type:TextureType, numTextures:Int = 1, filter:TextureFilter = Nearest) {
public function new(g:Graphics, width:Int, height:Int, format:TextureFormat = RGBA, type:TextureType,
numTextures:Int = 1, filter:TextureFilter = Nearest) {
this.g = g;
this.numTextures = numTextures;
for (i in 0...numTextures) {
final t0 = g.createTexture(width, height, type);
final t1 = g.createTexture(width, height, type);
final t0 = g.createTexture(width, height, format, type);
final t1 = g.createTexture(width, height, format, type);
t0.filter(filter);
t1.filter(filter);
textures[0].push(t0);
Expand Down
31 changes: 18 additions & 13 deletions Pot/src/pot/graphics/gl/Texture.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pot.graphics.gl;

import js.html.webgl.Framebuffer;
import js.html.webgl.GL2;
import js.lib.Float32Array;
import js.lib.Int32Array;
Expand Down Expand Up @@ -43,11 +44,11 @@ class Texture extends GLObject {

function internalFormatWrap():Int {
return switch [format, type] {
case [R, Int8]:
case [R, UInt8]:
GL2.R8;
case [RGB, Int8]:
case [RGB, UInt8]:
GL2.RGB8;
case [RGBA, Int8]:
case [RGBA, UInt8]:
GL2.RGBA8;
case [R, Int32]:
GL2.R32I;
Expand Down Expand Up @@ -84,11 +85,11 @@ class Texture extends GLObject {
GL2.RGB_INTEGER;
case [RGBA, Int32 | UInt32]:
GL2.RGBA_INTEGER;
case [R, Int8 | Float16 | Float32]:
case [R, UInt8 | Float16 | Float32]:
GL2.RED;
case [RGB, Int8 | Float16 | Float32]:
case [RGB, UInt8 | Float16 | Float32]:
GL2.RGB;
case [RGBA, Int8 | Float16 | Float32]:
case [RGBA, UInt8 | Float16 | Float32]:
GL2.RGBA;
}
}
Expand Down Expand Up @@ -152,7 +153,7 @@ class Texture extends GLObject {

overload extern public inline function upload(xOffset:Int, yOffset:Int, width:Int, height:Int,
pixelsRGBA:Uint8Array, flipY:Bool = true):Void {
if (type != Int8)
if (type != UInt8)
throw "not an 8-bit integer texture";
if (pixelsRGBA.length != width * height * numChannels())
throw "dimensions mismatch";
Expand Down Expand Up @@ -206,13 +207,14 @@ class Texture extends GLObject {

overload extern public inline function download(xOffset:Int, yOffset:Int, width:Int, height:Int,
pixelsRGBA:Uint8Array):Void {
if (type != Int8)
if (type != UInt8)
throw "not an 8-bit integer texture";
if (pixelsRGBA.length != width * height * numChannels())
throw "dimensions mismatch";
final tmp:Framebuffer = cast gl.getParameter(GL2.FRAMEBUFFER_BINDING);
gl.bindFramebuffer(GL2.FRAMEBUFFER, frameBuffer.getRawFrameBuffer());
gl.readPixels(xOffset, yOffset, width, height, formatWrap(), GL2.UNSIGNED_BYTE, pixelsRGBA);
gl.bindFramebuffer(GL2.FRAMEBUFFER, null);
gl.bindFramebuffer(GL2.FRAMEBUFFER, tmp);
}

overload extern public inline function download(xOffset:Int, yOffset:Int, width:Int, height:Int,
Expand All @@ -221,9 +223,10 @@ class Texture extends GLObject {
throw "not a 32-bit integer texture";
if (pixelsRGBA.length != width * height * numChannels())
throw "dimensions mismatch";
final tmp:Framebuffer = cast gl.getParameter(GL2.FRAMEBUFFER_BINDING);
gl.bindFramebuffer(GL2.FRAMEBUFFER, frameBuffer.getRawFrameBuffer());
gl.readPixels(xOffset, yOffset, width, height, formatWrap(), GL2.INT, pixelsRGBA);
gl.bindFramebuffer(GL2.FRAMEBUFFER, null);
gl.bindFramebuffer(GL2.FRAMEBUFFER, tmp);
}

overload extern public inline function download(xOffset:Int, yOffset:Int, width:Int, height:Int,
Expand All @@ -232,9 +235,10 @@ class Texture extends GLObject {
throw "not an unsigned 32-bit integer texture";
if (pixelsRGBA.length != width * height * numChannels())
throw "dimensions mismatch";
final tmp:Framebuffer = cast gl.getParameter(GL2.FRAMEBUFFER_BINDING);
gl.bindFramebuffer(GL2.FRAMEBUFFER, frameBuffer.getRawFrameBuffer());
gl.readPixels(xOffset, yOffset, width, height, formatWrap(), GL2.UNSIGNED_INT, pixelsRGBA);
gl.bindFramebuffer(GL2.FRAMEBUFFER, null);
gl.bindFramebuffer(GL2.FRAMEBUFFER, tmp);
}

overload extern public inline function download(xOffset:Int, yOffset:Int, width:Int, height:Int,
Expand All @@ -243,16 +247,17 @@ class Texture extends GLObject {
throw "not a 32-bit floating point texture";
if (pixelsRGBA.length != width * height * numChannels())
throw "dimensions mismatch";
final tmp:Framebuffer = cast gl.getParameter(GL2.FRAMEBUFFER_BINDING);
gl.bindFramebuffer(GL2.FRAMEBUFFER, frameBuffer.getRawFrameBuffer());
gl.readPixels(xOffset, yOffset, width, height, formatWrap(), GL2.FLOAT, pixelsRGBA);
gl.bindFramebuffer(GL2.FRAMEBUFFER, null);
gl.bindFramebuffer(GL2.FRAMEBUFFER, tmp);
}

public function sync():Void {
gl.bindFramebuffer(GL2.FRAMEBUFFER, frameBuffer.getRawFrameBuffer());
final nc = numChannels();
gl.readPixels(0, 0, 1, 1, formatWrap(), type, switch type {
case Int8:
case UInt8:
new Uint8Array(nc);
case Int32:
new Int32Array(nc);
Expand Down
2 changes: 1 addition & 1 deletion Pot/src/pot/graphics/gl/TextureType.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ enum abstract TextureType(Int) to Int {
/**
* Represents 8-bit integer type.
*/
var Int8 = GL2.UNSIGNED_BYTE;
var UInt8 = GL2.UNSIGNED_BYTE;

/**
* Represents 32-bit signed integer type.
Expand Down
Loading

0 comments on commit 9bd9a3f

Please sign in to comment.