Added `push` and `get` to wasmgl module
This commit is contained in:
parent
2bf6e33350
commit
1fd21affde
|
@ -1,11 +1,3 @@
|
|||
export class WASMGLvalue {
|
||||
ptr;
|
||||
size;
|
||||
constructor(ptr, size) {
|
||||
this.ptr = ptr;
|
||||
this.size = size;
|
||||
}
|
||||
}
|
||||
export async function loadWasmModule(path) {
|
||||
return WebAssembly.instantiateStreaming(fetch(path));
|
||||
}
|
||||
|
@ -34,4 +26,18 @@ export class WASMGL {
|
|||
});
|
||||
return ptr;
|
||||
}
|
||||
push(data) {
|
||||
let ptr = this.alloc(data.length);
|
||||
data.forEach((v, i) => {
|
||||
this.mem[ptr + i] = v;
|
||||
});
|
||||
return ptr;
|
||||
}
|
||||
get(ptr, length) {
|
||||
let a = new Array(length);
|
||||
for (let i = 0; i < length; ++i) {
|
||||
a[i] = this.mem[ptr + i];
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,3 @@
|
|||
export class WASMGLvalue {
|
||||
ptr: number;
|
||||
size: number;
|
||||
|
||||
constructor(ptr: number, size: number) {
|
||||
this.ptr = ptr;
|
||||
this.size = size;
|
||||
}
|
||||
}
|
||||
|
||||
export interface WASMGLEnvironment {
|
||||
exports: any,
|
||||
mem: Float32Array,
|
||||
|
@ -58,4 +48,24 @@ export class WASMGL {
|
|||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
push(data: number[]): number {
|
||||
let ptr = this.alloc(data.length);
|
||||
|
||||
data.forEach((v, i) => {
|
||||
this.mem[ptr + i] = v;
|
||||
});
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
get(ptr: number, length: number): Array<number> {
|
||||
let a = new Array(length);
|
||||
|
||||
for (let i = 0; i < length; ++i) {
|
||||
a[i] = this.mem[ptr + i];
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
#include "wasmgl.h"
|
||||
|
||||
void doubel(WASMGLvalue(0))
|
||||
void doubel(WASMGLvalue(data))
|
||||
{
|
||||
for (int i = 0; i < size_0; ++i)
|
||||
WASMGLset(ptr_0 + i, WASMGLmemory[ptr_0 + i] * 2.0);
|
||||
for (int i = 0; i < size_data; ++i)
|
||||
WASMGLset(ptr_data + i, WASMGLmemory[ptr_data + i] * 2.0);
|
||||
}
|
||||
|
||||
void uppercase(WASMGLvalue(0)) {
|
||||
void uppercase(WASMGLvalue(data)) {
|
||||
int val = 0;
|
||||
|
||||
for (int i = 0; i < size_0; ++i)
|
||||
for (int i = 0; i < size_data; ++i)
|
||||
{
|
||||
val = WASMGLmemory[ptr_0 + i] - 32;
|
||||
val = WASMGLmemory[ptr_data + i] - 32;
|
||||
|
||||
if (val < 65 || val > 122) val = WASMGLmemory[ptr_0 + i];
|
||||
if (val < 65 || val > 122) val = WASMGLmemory[ptr_data + i];
|
||||
|
||||
WASMGLset(ptr_0 + i, val);
|
||||
WASMGLset(ptr_data + i, val);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ extern float WASMGLmemory[WASMGLmemory_size];
|
|||
typedef unsigned int WASMGLptr;
|
||||
typedef unsigned int WASMGLsize;
|
||||
|
||||
#define WASMGLvalue(n) WASMGLptr ptr_##n, WASMGLsize size_##n
|
||||
#define WASMGLvalue(name) WASMGLptr ptr_##name, WASMGLsize size_##name
|
||||
|
||||
WASMGLptr WASMGLmalloc(WASMGLsize size);
|
||||
void WASMGLset(WASMGLptr ptr, int value);
|
||||
|
|
Loading…
Reference in New Issue