Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implementing NFT with threading support with emscripten #2

Open
wants to merge 24 commits into
base: fixing-nft
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
162bc15
implementing NFT with threading support
kalwalt Jul 22, 2019
41eb40a
trackingInitGetResult instead of kpmGetResult
kalwalt Jul 22, 2019
ec03ab4
adding quit tracking and other stuf for the transpose matrix
kalwalt Jul 22, 2019
6f46ad1
improved getNFTMarkerInfo with threaded tracking
kalwalt Jul 22, 2019
a3e19c3
tested a different conditional
kalwalt Jul 24, 2019
f5694a9
adding pthreads also in the bytecode libs
kalwalt Jul 26, 2019
218cb8f
simplifying part of the code
kalwalt Jul 26, 2019
0914d82
this solved the issue: arc->videoFrame instead of arc->videoLuma !!!
kalwalt Jul 27, 2019
359292b
cleaning the code
kalwalt Jul 27, 2019
67b0413
restoring the previous conditional
kalwalt Jul 27, 2019
07a85f2
re-adding wasm support
kalwalt Jul 27, 2019
2cb54f9
fix for threejs_from_scratch.html
kalwalt Jul 28, 2019
5918469
new setupAR2Threads function threads initialization only for NFT
kalwalt Jul 28, 2019
966d6d7
optimization in dataHeap transfer, from comment https://github.com/je…
kalwalt Aug 2, 2019
14f67b5
transferDataToHeap also for debug lib version
kalwalt Aug 2, 2019
04233e1
new libjpeg and related changes
kalwalt Aug 30, 2019
f86f165
deleting files ARMarkerNFT
kalwalt Aug 30, 2019
43ecde3
added NFT example with a gltf model
kalwalt Sep 15, 2019
08eecc4
gltf model needs some lights
kalwalt Sep 18, 2019
e0beb69
gltf Duck example for NFT
kalwalt Sep 22, 2019
cb6d29e
scaling and repositioning the model solved the issue view
kalwalt Sep 22, 2019
e9a88e3
fix for CesiumMan example
kalwalt Sep 22, 2019
e50e52b
fix gitmodules
kalwalt Feb 15, 2023
759c846
fix to build with emsdk 3.1.20
kalwalt Feb 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166,008 changes: 93,264 additions & 72,744 deletions build/artoolkitNft.debug.js

Large diffs are not rendered by default.

26 changes: 14 additions & 12 deletions build/artoolkitNft.min.js

Large diffs are not rendered by default.

163 changes: 163 additions & 0 deletions build/artoolkitNft.min.worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Copyright 2015 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

// Pthread Web Worker startup routine:
// This is the entry point file that is loaded first by each Web Worker
// that executes pthreads on the Emscripten application.

// Thread-local:
var threadInfoStruct = 0; // Info area for this thread in Emscripten HEAP (shared). If zero, this worker is not currently hosting an executing pthread.
var selfThreadId = 0; // The ID of this thread. 0 if not hosting a pthread.
var parentThreadId = 0; // The ID of the parent pthread that launched this thread.
var tempDoublePtr = 0; // A temporary memory area for global float and double marshalling operations.

// Thread-local: Each thread has its own allocated stack space.
var STACK_BASE = 0;
var STACKTOP = 0;
var STACK_MAX = 0;

// These are system-wide memory area parameters that are set at main runtime startup in main thread, and stay constant throughout the application.
var buffer; // All pthreads share the same Emscripten HEAP as SharedArrayBuffer with the main execution thread.
var DYNAMICTOP_PTR = 0;
var DYNAMIC_BASE = 0;

var ENVIRONMENT_IS_PTHREAD = true;
var PthreadWorkerInit = {};

// performance.now() is specced to return a wallclock time in msecs since that Web Worker/main thread launched. However for pthreads this can cause
// subtle problems in emscripten_get_now() as this essentially would measure time from pthread_create(), meaning that the clocks between each threads
// would be wildly out of sync. Therefore sync all pthreads to the clock on the main browser thread, so that different threads see a somewhat
// coherent clock across each of them (+/- 0.1msecs in testing)
var __performance_now_clock_drift = 0;

// Cannot use console.log or console.error in a web worker, since that would risk a browser deadlock! https://bugzilla.mozilla.org/show_bug.cgi?id=1049091
// Therefore implement custom logging facility for threads running in a worker, which queue the messages to main thread to print.
var Module = {};


// When error objects propagate from Web Worker to main thread, they lose helpful call stack and thread ID information, so print out errors early here,
// before that happens.
this.addEventListener('error', function(e) {
if (e.message.indexOf('SimulateInfiniteLoop') != -1) return e.preventDefault();

var errorSource = ' in ' + e.filename + ':' + e.lineno + ':' + e.colno;
console.error('Pthread ' + selfThreadId + ' uncaught exception' + (e.filename || e.lineno || e.colno ? errorSource : "") + ': ' + e.message + '. Error object:');
console.error(e.error);
});

function threadPrint() {
var text = Array.prototype.slice.call(arguments).join(' ');
console.log(text);
}
function threadPrintErr() {
var text = Array.prototype.slice.call(arguments).join(' ');
console.error(text);
console.error(new Error().stack);
}
function threadAlert() {
var text = Array.prototype.slice.call(arguments).join(' ');
postMessage({cmd: 'alert', text: text, threadId: selfThreadId});
}
out = threadPrint;
err = threadPrintErr;
this.alert = threadAlert;


var wasmModule;
var wasmMemory;

this.onmessage = function(e) {
try {
if (e.data.cmd === 'load') { // Preload command that is called once per worker to parse and load the Emscripten code.
// Initialize the thread-local field(s):
tempDoublePtr = e.data.tempDoublePtr;

// Initialize the global "process"-wide fields:
DYNAMIC_BASE = e.data.DYNAMIC_BASE;
DYNAMICTOP_PTR = e.data.DYNAMICTOP_PTR;

buffer = e.data.buffer;



PthreadWorkerInit = e.data.PthreadWorkerInit;

if (typeof e.data.urlOrBlob === 'string') {
importScripts(e.data.urlOrBlob);
} else {
var objectUrl = URL.createObjectURL(e.data.urlOrBlob);
importScripts(objectUrl);
URL.revokeObjectURL(objectUrl);
}


if (typeof FS !== 'undefined' && typeof FS.createStandardStreams === 'function') FS.createStandardStreams();
postMessage({ cmd: 'loaded' });
} else if (e.data.cmd === 'objectTransfer') {
PThread.receiveObjectTransfer(e.data);
} else if (e.data.cmd === 'run') { // This worker was idle, and now should start executing its pthread entry point.
__performance_now_clock_drift = performance.now() - e.data.time; // Sync up to the clock of the main thread.
threadInfoStruct = e.data.threadInfoStruct;
__register_pthread_ptr(threadInfoStruct, /*isMainBrowserThread=*/0, /*isMainRuntimeThread=*/0); // Pass the thread address inside the asm.js scope to store it for fast access that avoids the need for a FFI out.
selfThreadId = e.data.selfThreadId;
parentThreadId = e.data.parentThreadId;
// Establish the stack frame for this thread in global scope
STACK_BASE = STACKTOP = e.data.stackBase;
STACK_MAX = STACK_BASE + e.data.stackSize;
// Call inside asm.js/wasm module to set up the stack frame for this pthread in asm.js/wasm module scope
Module['establishStackSpace'](e.data.stackBase, e.data.stackBase + e.data.stackSize);

PThread.receiveObjectTransfer(e.data);
PThread.setThreadStatus(_pthread_self(), 1/*EM_THREAD_STATUS_RUNNING*/);

try {
// pthread entry points are always of signature 'void *ThreadMain(void *arg)'
// Native codebases sometimes spawn threads with other thread entry point signatures,
// such as void ThreadMain(void *arg), void *ThreadMain(), or void ThreadMain().
// That is not acceptable per C/C++ specification, but x86 compiler ABI extensions
// enable that to work. If you find the following line to crash, either change the signature
// to "proper" void *ThreadMain(void *arg) form, or try linking with the Emscripten linker
// flag -s EMULATE_FUNCTION_POINTER_CASTS=1 to add in emulation for this x86 ABI extension.
var result = Module['dynCall_ii'](e.data.start_routine, e.data.arg);


} catch(e) {
if (e === 'Canceled!') {
PThread.threadCancel();
return;
} else if (e === 'SimulateInfiniteLoop' || e === 'pthread_exit') {
return;
} else {
Atomics.store(HEAPU32, (threadInfoStruct + 4 /*C_STRUCTS.pthread.threadExitCode*/ ) >> 2, (e instanceof ExitStatus) ? e.status : -2 /*A custom entry specific to Emscripten denoting that the thread crashed.*/);
Atomics.store(HEAPU32, (threadInfoStruct + 0 /*C_STRUCTS.pthread.threadStatus*/ ) >> 2, 1); // Mark the thread as no longer running.
_emscripten_futex_wake(threadInfoStruct + 0 /*C_STRUCTS.pthread.threadStatus*/, 0x7FFFFFFF/*INT_MAX*/); // Wake all threads waiting on this thread to finish.
if (!(e instanceof ExitStatus)) throw e;
}
}
// The thread might have finished without calling pthread_exit(). If so, then perform the exit operation ourselves.
// (This is a no-op if explicit pthread_exit() had been called prior.)
if (!Module['noExitRuntime']) PThread.threadExit(result);
} else if (e.data.cmd === 'cancel') { // Main thread is asking for a pthread_cancel() on this thread.
if (threadInfoStruct && PThread.thisThreadCancelState == 0/*PTHREAD_CANCEL_ENABLE*/) {
PThread.threadCancel();
}
} else if (e.data.target === 'setimmediate') {
// no-op
} else if (e.data.cmd === 'processThreadQueue') {
if (threadInfoStruct) { // If this thread is actually running?
_emscripten_current_thread_process_queued_calls();
}
} else {
err('worker.js received unknown command ' + e.data.cmd);
console.error(e.data);
}
} catch(e) {
console.error('worker.js onmessage() captured an uncaught exception: ' + e);
console.error(e.stack);
throw e;
}
}


2 changes: 1 addition & 1 deletion build/artoolkitNft_wasm.js

Large diffs are not rendered by default.

Binary file modified build/artoolkitNft_wasm.wasm
Binary file not shown.
209 changes: 209 additions & 0 deletions emscripten/ARMarkerNFT.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/*
* ARMarkerNFT.c
* ARToolKit5
*
* Disclaimer: IMPORTANT: This Daqri software is supplied to you by Daqri
* LLC ("Daqri") in consideration of your agreement to the following
* terms, and your use, installation, modification or redistribution of
* this Daqri software constitutes acceptance of these terms. If you do
* not agree with these terms, please do not use, install, modify or
* redistribute this Daqri software.
*
* In consideration of your agreement to abide by the following terms, and
* subject to these terms, Daqri grants you a personal, non-exclusive
* license, under Daqri's copyrights in this original Daqri software (the
* "Daqri Software"), to use, reproduce, modify and redistribute the Daqri
* Software, with or without modifications, in source and/or binary forms;
* provided that if you redistribute the Daqri Software in its entirety and
* without modifications, you must retain this notice and the following
* text and disclaimers in all such redistributions of the Daqri Software.
* Neither the name, trademarks, service marks or logos of Daqri LLC may
* be used to endorse or promote products derived from the Daqri Software
* without specific prior written permission from Daqri. Except as
* expressly stated in this notice, no other rights or licenses, express or
* implied, are granted by Daqri herein, including but not limited to any
* patent rights that may be infringed by your derivative works or by other
* works in which the Daqri Software may be incorporated.
*
* The Daqri Software is provided by Daqri on an "AS IS" basis. DAQRI
* MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
* THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE, REGARDING THE DAQRI SOFTWARE OR ITS USE AND
* OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
*
* IN NO EVENT SHALL DAQRI BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
* MODIFICATION AND/OR DISTRIBUTION OF THE DAQRI SOFTWARE, HOWEVER CAUSED
* AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
* STRICT LIABILITY OR OTHERWISE, EVEN IF DAQRI HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2015 Daqri LLC. All Rights Reserved.
* Copyright 2013-2015 ARToolworks, Inc. All Rights Reserved.
*
* Author(s): Philip Lamb.
*
*/

#include "ARMarkerNFT.h"

#include <stdio.h>
#include <string.h>
#ifdef _WIN32
# include <windows.h>
# define MAXPATHLEN MAX_PATH
#else
# include <sys/param.h> // MAXPATHLEN
#endif


const ARPose ARPoseUnity = {{1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};

static char *get_buff(char *buf, int n, FILE *fp, int skipblanks)
{
char *ret;
size_t l;

do {
ret = fgets(buf, n, fp);
if (ret == NULL) return (NULL); // EOF or error.

// Remove NLs and CRs from end of string.
l = strlen(buf);
while (l > 0) {
if (buf[l - 1] != '\n' && buf[l - 1] != '\r') break;
l--;
buf[l] = '\0';
}
} while (buf[0] == '#' || (skipblanks && buf[0] == '\0')); // Reject comments and blank lines.

return (ret);
}

void newMarkers(const char *markersConfigDataFilePathC, ARMarkerNFT **markersNFT_out, int *markersNFTCount_out)
{
FILE *fp;
char buf[MAXPATHLEN], buf1[MAXPATHLEN];
int tempI;
ARMarkerNFT *markersNFT;
int markersNFTCount;
ARdouble tempF;
int i;
char markersConfigDataDirC[MAXPATHLEN];
size_t markersConfigDataDirCLen;

if (!markersConfigDataFilePathC || markersConfigDataFilePathC[0] == '\0' || !markersNFT_out || !markersNFTCount_out) return;

// Load the marker data file.
ARLOGd("Opening marker config. data file from path '%s'.\n", markersConfigDataFilePathC);
arUtilGetDirectoryNameFromPath(markersConfigDataDirC, markersConfigDataFilePathC, MAXPATHLEN, 1); // 1 = add '/' at end.
markersConfigDataDirCLen = strlen(markersConfigDataDirC);
if ((fp = fopen(markersConfigDataFilePathC, "r")) == NULL) {
ARLOGe("Error: unable to locate marker config data file '%s'.\n", markersConfigDataFilePathC);
return;
}

// First line is number of markers to read.
get_buff(buf, MAXPATHLEN, fp, 1);
if (sscanf(buf, "%d", &tempI) != 1 ) {
ARLOGe("Error in marker configuration data file; expected marker count.\n");
fclose(fp);
return;
}

arMallocClear(markersNFT, ARMarkerNFT, tempI);
markersNFTCount = tempI;

ARLOGd("Reading %d marker configuration(s).\n", markersNFTCount);

for (i = 0; i < markersNFTCount; i++) {

// Read marker name.
if (!get_buff(buf, MAXPATHLEN, fp, 1)) {
ARLOGe("Error in marker configuration data file; expected marker name.\n");
break;
}

// Read marker type.
if (!get_buff(buf1, MAXPATHLEN, fp, 1)) {
ARLOGe("Error in marker configuration data file; expected marker type.\n");
break;
}

// Interpret marker type, and read more data.
if (strcmp(buf1, "SINGLE") == 0) {
ARLOGe("Error in marker configuration data file; SINGLE markers not supported in this build.\n");
} else if (strcmp(buf1, "MULTI") == 0) {
ARLOGe("Error in marker configuration data file; MULTI markers not supported in this build.\n");
} else if (strcmp(buf1, "NFT") == 0) {
markersNFT[i].valid = markersNFT[i].validPrev = FALSE;
arMalloc(markersNFT[i].datasetPathname, char, markersConfigDataDirCLen + strlen(buf) + 1);
strcpy(markersNFT[i].datasetPathname, markersConfigDataDirC);
strcpy(markersNFT[i].datasetPathname + markersConfigDataDirCLen, buf);
markersNFT[i].pageNo = -1;
} else {
ARLOGe("Error in marker configuration data file; unsupported marker type %s.\n", buf1);
}

// Look for optional tokens. A blank line marks end of options.
while (get_buff(buf, MAXPATHLEN, fp, 0) && (buf[0] != '\0')) {
if (strncmp(buf, "FILTER", 6) == 0) {
markersNFT[i].filterCutoffFrequency = AR_FILTER_TRANS_MAT_CUTOFF_FREQ_DEFAULT;
markersNFT[i].filterSampleRate = AR_FILTER_TRANS_MAT_SAMPLE_RATE_DEFAULT;
if (strlen(buf) != 6) {
if (sscanf(&buf[6],
#ifdef ARDOUBLE_IS_FLOAT
"%f"
#else
"%lf"
#endif
, &tempF) == 1) markersNFT[i].filterCutoffFrequency = tempF;
}
markersNFT[i].ftmi = arFilterTransMatInit(markersNFT[i].filterSampleRate, markersNFT[i].filterCutoffFrequency);
}
// Unknown tokens are ignored.
}
}
fclose(fp);

// If not all markers were read, an error occurred.
if (i < markersNFTCount) {

// Clean up.
for (; i >= 0; i--) {
if (markersNFT[i].datasetPathname) free(markersNFT[i].datasetPathname);
if (markersNFT[i].ftmi) arFilterTransMatFinal(markersNFT[i].ftmi);
}
free(markersNFT);

*markersNFTCount_out = 0;
*markersNFT_out = NULL;
return;
}

*markersNFTCount_out = markersNFTCount;
*markersNFT_out = markersNFT;
}

void deleteMarkers(ARMarkerNFT **markersNFT_p, int *markersNFTCount_p)
{
int i;

if (!markersNFT_p || !*markersNFT_p || !*markersNFTCount_p || *markersNFTCount_p < 1) return;

for (i = 0; i < *markersNFTCount_p; i++) {
if ((*markersNFT_p)[i].datasetPathname) {
free((*markersNFT_p)[i].datasetPathname);
(*markersNFT_p)[i].datasetPathname = NULL;
}
if ((*markersNFT_p)[i].ftmi) {
arFilterTransMatFinal((*markersNFT_p)[i].ftmi);
(*markersNFT_p)[i].ftmi = NULL;
}
}
free(*markersNFT_p);
*markersNFT_p = NULL;
*markersNFTCount_p = 0;
}
Loading