WebAssembly image dithering example

kinke noone at nowhere.com
Fri Aug 3 21:05:09 UTC 2018


> An LLVM issue, I guess.

Nope, LLVM apparently doesn't like the pixels array starting at 
null (so just pass a null pointer from JS to WebAssembly). With 
this diff and your python2 help, I got it to work locally now 
(849 bytes):

diff --git a/docs/js/worker.js b/docs/js/worker.js
index 57756e9..b512fad 100644
--- a/docs/js/worker.js
+++ b/docs/js/worker.js
@@ -162,7 +162,7 @@
              const heapSize = wasmHeap.length - imageByteSize;
              //dither image
              const performanceResults = 
Timer.megapixelsPerSecond('WASM ordered dithering performance', 
imageWidth * imageHeight, ()=>{
-                wasmExports.dither(imageWidth, imageHeight, 
heapOffset, heapSize);
+                wasmExports.dither(0, imageWidth, imageHeight, 
heapOffset, heapSize);
              });
              performanceResults[2] = ditherId;
              //dithered image is now in the wasmHeap
diff --git a/wasm_src/main.d b/wasm_src/main.d
index 4878a98..6959b7b 100644
--- a/wasm_src/main.d
+++ b/wasm_src/main.d
@@ -11,7 +11,7 @@ template TInitialize(T){
      //sort of halfway between static and dynamic array
      //like dynamic array in that length and offset can be 
runtime values
      //but like static array in that length cannot change after 
initialization without possible causing problems
-    T[] fixedArray(int offset, int length){
+    T[] fixedArray(void* offset, int length){
          //take pointer to (global/heap? not sure correct term) 
memory and convert to array by taking slice
          //(make sure you disable bounds checking in compiler 
since assert is not supported in wasm currently)
          return (cast(T*) offset)[0..length];
@@ -58,16 +58,16 @@ void fillBayerMatrix(float[] bayerMatrix){
         bayerMatrix[3] = -.166666667 * DITHER_R_COEFFICIENT;
  }

-void dither(int imageWidth, int imageHeight, int heapOffset, int 
heapLength){
+void dither(void* pixelsData, int imageWidth, int imageHeight, 
void* heapStart, int heapLength){
         //* 4 since RGBA format
         immutable int pixelsLength = imageWidth * imageHeight * 4;
      //pixels array starts at offset 0 in wasm heap
-    ubyte[] pixels = TInitialize!(ubyte).fixedArray(0, 
pixelsLength);
+    ubyte[] pixels = TInitialize!(ubyte).fixedArray(pixelsData, 
pixelsLength);

      //2x2 bayer matrix
      immutable int bayerDimensions = 2;
      //create array using heap memory
-    float[] bayerMatrix = 
TInitialize!(float).fixedArray(heapOffset, 
bayerDimensions*bayerDimensions);
+    float[] bayerMatrix = 
TInitialize!(float).fixedArray(heapStart, 
bayerDimensions*bayerDimensions);

      /*
      //adjust heapOffset and heapLength, in case we want to use 
them again
@@ -105,5 +105,7 @@ void dither(int imageWidth, int imageHeight, 
int heapOffset, int heapLength){
      }
  }

+void __assert(const(char)* msg, const(char)* file, uint line) {}
+
  // seems to be the required entry point
  void _start() {}


More information about the digitalmars-d-ldc mailing list