diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..43511cb38 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "stdint.h": "c" + } +} diff --git a/frontend/resources/public/wasm/Makefile b/frontend/resources/public/wasm/Makefile new file mode 100644 index 000000000..5cc6b69bd --- /dev/null +++ b/frontend/resources/public/wasm/Makefile @@ -0,0 +1,2 @@ +all: + clang -target wasm32 -Wl,--no-entry -Wl,--export-all -nostdlib -O3 add.c -o add.wasm diff --git a/frontend/resources/public/wasm/add.c b/frontend/resources/public/wasm/add.c new file mode 100644 index 000000000..db7a8a6cd --- /dev/null +++ b/frontend/resources/public/wasm/add.c @@ -0,0 +1,19 @@ +#include "int.h" + +#define MAX_OPERATIONS 2048 + +typedef struct _operations { + int32_t a, b, r; +} operations_t; + +operations_t operations[MAX_OPERATIONS]; + +int32_t add(int32_t a, int32_t b) { + return a + b; +} + +void compute() { + for (int32_t i = 0; i < MAX_OPERATIONS; i++) { + operations[i].r = add(operations[i].a, operations[i].b); + } +} diff --git a/frontend/resources/public/wasm/add.js b/frontend/resources/public/wasm/add.js new file mode 100644 index 000000000..a87d16e25 --- /dev/null +++ b/frontend/resources/public/wasm/add.js @@ -0,0 +1,5 @@ +function add(a, b) { + return a + b +} + +add(5, 5) diff --git a/frontend/resources/public/wasm/add.wasm b/frontend/resources/public/wasm/add.wasm new file mode 100755 index 000000000..9fe76a24d Binary files /dev/null and b/frontend/resources/public/wasm/add.wasm differ diff --git a/frontend/resources/public/wasm/int.h b/frontend/resources/public/wasm/int.h new file mode 100644 index 000000000..6080266dd --- /dev/null +++ b/frontend/resources/public/wasm/int.h @@ -0,0 +1,10 @@ +#pragma once + +typedef __INT8_TYPE__ int8_t; +typedef __UINT8_TYPE__ uint8_t; + +typedef __INT16_TYPE__ int16_t; +typedef __UINT16_TYPE__ uint16_t; + +typedef __INT32_TYPE__ int32_t; +typedef __UINT32_TYPE__ uint32_t; diff --git a/frontend/src/app/main.cljs b/frontend/src/app/main.cljs index b81ccc63c..2f5fe0af0 100644 --- a/frontend/src/app/main.cljs +++ b/frontend/src/app/main.cljs @@ -24,6 +24,7 @@ [app.util.dom :as dom] [app.util.i18n :as i18n] [app.util.theme :as theme] + [app.wasm :as wasm] [beicon.core :as rx] [debug] [features] @@ -73,6 +74,7 @@ (defn ^:export init [] + (wasm/init!) (worker/init!) (i18n/init! cf/translations) (theme/init! cf/themes) diff --git a/frontend/src/app/main/data/workspace/transforms.cljs b/frontend/src/app/main/data/workspace/transforms.cljs index daaf0f0ad..ed43feba5 100644 --- a/frontend/src/app/main/data/workspace/transforms.cljs +++ b/frontend/src/app/main/data/workspace/transforms.cljs @@ -199,6 +199,7 @@ (normalize-proportion-lock [[point shift? alt?]] (let [proportion-lock? (:proportion-lock shape)] [point (or proportion-lock? shift?) alt?]))] + (reify ptk/UpdateEvent (update [_ state] diff --git a/frontend/src/app/wasm.cljs b/frontend/src/app/wasm.cljs new file mode 100644 index 000000000..c5419858f --- /dev/null +++ b/frontend/src/app/wasm.cljs @@ -0,0 +1,35 @@ +(ns app.wasm + (:require + [promesa.core :as p])) + +(defonce instance (atom nil)) + +(defn load-wasm + "Loads a WebAssembly module" + [uri] + (-> + (p/let [response (js/fetch uri) + array-buffer (.arrayBuffer response) + assembly (.instantiate js/WebAssembly array-buffer)] + assembly) + (p/catch (fn [error] (prn "error: " error))))) + +(defn init! + "Initializes WebAssembly module" + [] + (p/then + (load-wasm "wasm/add.wasm") + (fn [assembly] + (let [operations (js/Int32Array. + assembly.instance.exports.memory.buffer ;; buffer we want to use + assembly.instance.exports.operations.value ;; offset of pointer 'operations' + (* 2048 12))] + (aset operations 0 2) + (aset operations 1 2) + (.set operations #js [4 5 -1] 3) + (js/console.time "compute") + (assembly.instance.exports.compute) + (js/console.timeEnd "compute") + (js/console.log assembly) + ) + (reset! instance assembly.instance))))