From 45e471bf63621eb8ba176ae91fced00a8c764000 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Fri, 22 Jul 2022 08:08:29 +0300 Subject: [PATCH 1/4] add get/set pixel routines --- .../templates/assemblyscript/src/wasm4.ts | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/cli/assets/templates/assemblyscript/src/wasm4.ts b/cli/assets/templates/assemblyscript/src/wasm4.ts index e29ed870..74e352b8 100644 --- a/cli/assets/templates/assemblyscript/src/wasm4.ts +++ b/cli/assets/templates/assemblyscript/src/wasm4.ts @@ -48,6 +48,47 @@ export const SYSTEM_HIDE_GAMEPAD_OVERLAY = 2; // │ │ // └───────────────────────────────────────────────────────────────────────────┘ +/** Set single pixel to the framebuffer with specific color without bounds checking. */ +// @ts-ignore: decorator +@inline +export function setPixelUnsafe(color: u8, x: i32, y: i32): void { + let idx = y * (SCREEN_SIZE >>> 2) + (x >>> 2); + let shift = ((x & 0x3) << 1); + let mask = 0x3 << shift; + let data = (color << shift) | (load(FRAMEBUFFER + idx) & ~mask); + store(FRAMEBUFFER + idx, data); +} + +/** Set single pixel to the framebuffer with specific color with bounds checking. */ +// @ts-ignore: decorator +@inline +export function setPixel(color: u8, x: i32, y: i32): void { + if ((x >= 0 && x < SCREEN_SIZE) && (y >= 0 && y < SCREEN_SIZE)) { + setPixelUnsafe(color, x, y); + } +} + +/** Get single pixel from the framebuffer without bounds checking. */ +// @ts-ignore: decorator +@inline +export function getPixelUnsafe(x: i32, y: i32): u8 { + let idx = y * (SCREEN_SIZE >>> 2) + (x >>> 2); + let shift = ((x & 0x3) << 1); + let color = load(FRAMEBUFFER + idx); + return (color >> shift) & 0x3; +} + +/** Get single pixel from the framebuffer with bounds checking. */ +// @ts-ignore: decorator +@inline +export function getPixel(x: i32, y: i32): u8 { + if ((x >= 0 && x < SCREEN_SIZE) && (y >= 0 && y < SCREEN_SIZE)) { + return getPixelUnsafe(x, y); + } else { + return 0; + } +} + /** Copies pixels to the framebuffer. */ // @ts-ignore: decorator @external("env", "blit") From 088d48c9be6fe9b80f7f01e83f9c7cc1433d1128 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Fri, 22 Jul 2022 08:20:44 +0300 Subject: [PATCH 2/4] optimize bounds checking --- cli/assets/templates/assemblyscript/src/wasm4.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/assets/templates/assemblyscript/src/wasm4.ts b/cli/assets/templates/assemblyscript/src/wasm4.ts index 74e352b8..04f10c10 100644 --- a/cli/assets/templates/assemblyscript/src/wasm4.ts +++ b/cli/assets/templates/assemblyscript/src/wasm4.ts @@ -63,7 +63,7 @@ export function setPixelUnsafe(color: u8, x: i32, y: i32): void { // @ts-ignore: decorator @inline export function setPixel(color: u8, x: i32, y: i32): void { - if ((x >= 0 && x < SCREEN_SIZE) && (y >= 0 && y < SCREEN_SIZE)) { + if (x < SCREEN_SIZE && y < SCREEN_SIZE) { setPixelUnsafe(color, x, y); } } @@ -82,7 +82,7 @@ export function getPixelUnsafe(x: i32, y: i32): u8 { // @ts-ignore: decorator @inline export function getPixel(x: i32, y: i32): u8 { - if ((x >= 0 && x < SCREEN_SIZE) && (y >= 0 && y < SCREEN_SIZE)) { + if (x < SCREEN_SIZE && y < SCREEN_SIZE) { return getPixelUnsafe(x, y); } else { return 0; From 79d6999c08efedf8862a6b10efd00fb20d1c6a18 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Fri, 22 Jul 2022 08:24:33 +0300 Subject: [PATCH 3/4] remove inlines from safe versions --- cli/assets/templates/assemblyscript/src/wasm4.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cli/assets/templates/assemblyscript/src/wasm4.ts b/cli/assets/templates/assemblyscript/src/wasm4.ts index 04f10c10..02d9055c 100644 --- a/cli/assets/templates/assemblyscript/src/wasm4.ts +++ b/cli/assets/templates/assemblyscript/src/wasm4.ts @@ -60,8 +60,6 @@ export function setPixelUnsafe(color: u8, x: i32, y: i32): void { } /** Set single pixel to the framebuffer with specific color with bounds checking. */ -// @ts-ignore: decorator -@inline export function setPixel(color: u8, x: i32, y: i32): void { if (x < SCREEN_SIZE && y < SCREEN_SIZE) { setPixelUnsafe(color, x, y); @@ -79,8 +77,6 @@ export function getPixelUnsafe(x: i32, y: i32): u8 { } /** Get single pixel from the framebuffer with bounds checking. */ -// @ts-ignore: decorator -@inline export function getPixel(x: i32, y: i32): u8 { if (x < SCREEN_SIZE && y < SCREEN_SIZE) { return getPixelUnsafe(x, y); From 2a7a7d9c025c289ad0f54646306a38a77002fe18 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Fri, 22 Jul 2022 19:46:49 +0300 Subject: [PATCH 4/4] refactor --- cli/assets/templates/assemblyscript/src/wasm4.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cli/assets/templates/assemblyscript/src/wasm4.ts b/cli/assets/templates/assemblyscript/src/wasm4.ts index 02d9055c..968f763b 100644 --- a/cli/assets/templates/assemblyscript/src/wasm4.ts +++ b/cli/assets/templates/assemblyscript/src/wasm4.ts @@ -53,8 +53,8 @@ export const SYSTEM_HIDE_GAMEPAD_OVERLAY = 2; @inline export function setPixelUnsafe(color: u8, x: i32, y: i32): void { let idx = y * (SCREEN_SIZE >>> 2) + (x >>> 2); - let shift = ((x & 0x3) << 1); - let mask = 0x3 << shift; + let shift = ((x & 3) << 1); + let mask = 3 << shift; let data = (color << shift) | (load(FRAMEBUFFER + idx) & ~mask); store(FRAMEBUFFER + idx, data); } @@ -62,7 +62,7 @@ export function setPixelUnsafe(color: u8, x: i32, y: i32): void { /** Set single pixel to the framebuffer with specific color with bounds checking. */ export function setPixel(color: u8, x: i32, y: i32): void { if (x < SCREEN_SIZE && y < SCREEN_SIZE) { - setPixelUnsafe(color, x, y); + setPixelUnsafe(color & 3, x, y); } } @@ -71,9 +71,9 @@ export function setPixel(color: u8, x: i32, y: i32): void { @inline export function getPixelUnsafe(x: i32, y: i32): u8 { let idx = y * (SCREEN_SIZE >>> 2) + (x >>> 2); - let shift = ((x & 0x3) << 1); + let shift = ((x & 3) << 1); let color = load(FRAMEBUFFER + idx); - return (color >> shift) & 0x3; + return (color >> shift) & 3; } /** Get single pixel from the framebuffer with bounds checking. */