Test and fix the new features from the Thread API
This commit is contained in:
parent
5ae0479e02
commit
f1a3ca1f2e
12
src/main.mjs
12
src/main.mjs
|
|
@ -8,10 +8,10 @@ import Renderer from "./modules/renderer/renderer.mjs";
|
||||||
globalThis.TIMINGS = DEBUG && localStorage.getItem("timings");
|
globalThis.TIMINGS = DEBUG && localStorage.getItem("timings");
|
||||||
globalThis.RELEASE = !DEBUG;
|
globalThis.RELEASE = !DEBUG;
|
||||||
|
|
||||||
const renderer = new Renderer();
|
/*const renderer = new Renderer();
|
||||||
renderer.render();
|
renderer.render();*/
|
||||||
|
|
||||||
const thread = new Thread("./workers/base.mjs", "backend");
|
const thread = new Thread("./workers/base.mjs", "backend", true);
|
||||||
await thread.setup().complete();
|
await thread.setup();
|
||||||
const grid = await thread.test_fillArr({x: 20, y: 20, depth: 2, seed: 0, jitter: 3 });
|
const grid = await thread.test.fillArr([{x: 20, y: 20, seed: 0, jitter: 3 }]);
|
||||||
})();
|
})();
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
import * as Three from "../../libs/three.mjs";
|
import * as Three from "../../libs/three.mjs";
|
||||||
import Thread from "../../utils/workerUtils.mjs";
|
import { Thread } from "../../utils/workerUtils.mjs";
|
||||||
|
|
||||||
class Renderer
|
class Renderer
|
||||||
{
|
{
|
||||||
static async init()
|
static async init()
|
||||||
{
|
{
|
||||||
const renderer_thread = new Thread("../../workers/renderer.mjs", "renderer");
|
const renderer_thread = new Thread("../../workers/renderer.mjs", "renderer", true);
|
||||||
await renderer_thread.setup().complete();
|
await renderer_thread.setup();
|
||||||
renderer_thread.init(document.createElement("canvas").transferControlToOffscreen(), window.innerWidth, window.innerHeight);
|
const offcanvas = document.createElement("canvas").transferControlToOffscreen();
|
||||||
|
await renderer_thread.render.init([offcanvas, window.innerWidth, window.innerHeight], [offcanvas]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -55,9 +55,12 @@ function wrapper(thread, mod, fn)
|
||||||
{
|
{
|
||||||
return function(args, shared)
|
return function(args, shared)
|
||||||
{
|
{
|
||||||
globalThis.TIMINGS && console.time("Measuring " + mod + "." + fn);
|
if(globalThis.TIMINGS)
|
||||||
globalThis.TIMINGS && thread._calledMod = mod;
|
{
|
||||||
globalThis.TIMINGS && thread._calledFn = fn;
|
console.time("Measuring " + mod + "." + fn);
|
||||||
|
thread._calledMod = mod;
|
||||||
|
thread._calledFn = fn;
|
||||||
|
}
|
||||||
return request(thread, [Constants.REQUEST.CALL, mod, fn, args], shared);
|
return request(thread, [Constants.REQUEST.CALL, mod, fn, args], shared);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -66,12 +69,12 @@ function onmessage(thread)
|
||||||
{
|
{
|
||||||
return function(e) {
|
return function(e) {
|
||||||
const ret = parseReturns(e.data);
|
const ret = parseReturns(e.data);
|
||||||
globalThis.VERBOSE && console.debug(`Received message from ${thread._name} containing ${ret}`);
|
globalThis.VERBOSE && console.debug(`Received message from ${thread._name} containing `, ret);
|
||||||
|
globalThis.TIMINGS && thread._calledFn !== "" && console.timeEnd("Measuring " + thread._calledMod + "." + thread._calledFn);
|
||||||
|
|
||||||
if(thread._waiting && ret)
|
if(thread._waiting && ret)
|
||||||
{
|
{
|
||||||
globalThis.DEBUG && console.log(`Resolving ${thread._name}`);
|
globalThis.DEBUG && console.log(`Resolving ${thread._name}`);
|
||||||
globalThis.TIMINGS && thread._calledFn !== "" && console.timeEnd("Measuring " + thread._calledMod + "." + thread._calledFn);
|
|
||||||
thread._ret = ret;
|
thread._ret = ret;
|
||||||
thread._resolver(ret);
|
thread._resolver(ret);
|
||||||
cleanUp(thread);
|
cleanUp(thread);
|
||||||
|
|
@ -81,7 +84,8 @@ function onmessage(thread)
|
||||||
function onerror(thread)
|
function onerror(thread)
|
||||||
{
|
{
|
||||||
return function(e) {
|
return function(e) {
|
||||||
globalThis.DEBUG && console.error(`Received error from ${thread._name} containing ${e}`);
|
globalThis.DEBUG && console.error(`Received error from ${thread._name} containing `, e);
|
||||||
|
globalThis.TIMINGS && thread._calledFn !== "" && console.timeEnd("Measuring " + thread._calledMod + "." + thread._calledFn);
|
||||||
|
|
||||||
if(thread._waiting)
|
if(thread._waiting)
|
||||||
{
|
{
|
||||||
|
|
@ -97,9 +101,9 @@ const Thread = Object.freeze(class
|
||||||
* Creates a new Thread loading the linked file. The linked file must follow the Thread expectations.
|
* Creates a new Thread loading the linked file. The linked file must follow the Thread expectations.
|
||||||
* You can give it a optionnal name to make the debug easier.
|
* You can give it a optionnal name to make the debug easier.
|
||||||
*/
|
*/
|
||||||
constructor(url, name)
|
constructor(url, name, mod)
|
||||||
{
|
{
|
||||||
this._worker = new Worker(url, {type: "module"});
|
this._worker = new Worker(url, mod ? {type: "module"} : undefined);
|
||||||
this._name = name || url;
|
this._name = name || url;
|
||||||
|
|
||||||
this._worker.onmessage = onmessage(this);
|
this._worker.onmessage = onmessage(this);
|
||||||
|
|
@ -120,23 +124,13 @@ const Thread = Object.freeze(class
|
||||||
this[mod] = {};
|
this[mod] = {};
|
||||||
for(let i = 0; i < result[mod].length; ++i)
|
for(let i = 0; i < result[mod].length; ++i)
|
||||||
{
|
{
|
||||||
|
const fn = result[mod][i];
|
||||||
this[mod][fn] = wrapper(this, mod, fn);
|
this[mod][fn] = wrapper(this, mod, fn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
complete()
|
async terminate()
|
||||||
{
|
|
||||||
if(this._waiting)
|
|
||||||
{
|
|
||||||
return this._promise;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return Promise.resolve(this._ret);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
terminate()
|
|
||||||
{
|
{
|
||||||
await request(this, [Constants.REQUEST.TERMINATE]);
|
await request(this, [Constants.REQUEST.TERMINATE]);
|
||||||
this._worker.terminate();
|
this._worker.terminate();
|
||||||
|
|
@ -147,7 +141,7 @@ const Thread = Object.freeze(class
|
||||||
|
|
||||||
function send(ret)
|
function send(ret)
|
||||||
{
|
{
|
||||||
globalThis.VERBOSE && console.debug(`Sending ${ret} to the main thread`);
|
globalThis.VERBOSE && console.debug(`Sending `, ret, ` back to the main thread`);
|
||||||
postMessage([Constants.STATE.OK, ret]);
|
postMessage([Constants.STATE.OK, ret]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -163,7 +157,7 @@ class Process
|
||||||
}
|
}
|
||||||
static onmessage(e)
|
static onmessage(e)
|
||||||
{
|
{
|
||||||
globalThis.VERBOSE && console.debug(`Received ${e.data}`);
|
globalThis.VERBOSE && console.debug(`Received `, e.data);
|
||||||
if(e && e.data)
|
if(e && e.data)
|
||||||
{
|
{
|
||||||
switch(e.data[0])
|
switch(e.data[0])
|
||||||
|
|
@ -207,21 +201,24 @@ class Process
|
||||||
|
|
||||||
if(!Array.isArray(arr))
|
if(!Array.isArray(arr))
|
||||||
arr = [arr];
|
arr = [arr];
|
||||||
|
|
||||||
|
if(mod in Process._customFn)
|
||||||
|
throw new Error("This module has already been registered in the process.");
|
||||||
|
|
||||||
|
const modObj = {};
|
||||||
for(let i = 0; i < arr.length; ++i)
|
for(let i = 0; i < arr.length; ++i)
|
||||||
{
|
{
|
||||||
if(typeof arr[i] != 'function')
|
if(typeof arr[i] != 'function')
|
||||||
throw new Error("You can only register functions");
|
throw new Error("You can only register functions");
|
||||||
|
|
||||||
if(mod in Process._customFn)
|
|
||||||
throw new Error("This module has already been registered in the process.");
|
|
||||||
|
|
||||||
const modObj = {};
|
|
||||||
const name = arr[i].name;
|
const name = arr[i].name;
|
||||||
if(name in modObj)
|
if(name in modObj)
|
||||||
throw new Error("This function name is already registered in the process. Please use a different one.");
|
throw new Error("This function name is already registered in the process. Please use a different one.");
|
||||||
else
|
else
|
||||||
modObj[name] = arr[i];
|
modObj[name] = arr[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Process._customFn[mod] = modObj;
|
||||||
}
|
}
|
||||||
static cleanUp()
|
static cleanUp()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,11 @@ import Noise from '../libs/alea.mjs'
|
||||||
function fillArr(settings)
|
function fillArr(settings)
|
||||||
{
|
{
|
||||||
const noise = Noise(settings.seed);
|
const noise = Noise(settings.seed);
|
||||||
const arr = new Float32Array(settings.x * settings.y * settings.depth);
|
const arr = new Float32Array(settings.x * settings.y * 2);
|
||||||
for(let i = arr.length - 1; i >= 0; --i)
|
for(let i = arr.length - 1; i >= 0; --i)
|
||||||
{
|
{
|
||||||
const _x = i % settings.x;
|
const _x = i % settings.x;
|
||||||
const _y = (i - _x) / settings.x;
|
const _y = (i - _x) / settings.x;
|
||||||
arr[i] =
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue