Skip to content

Commit

Permalink
working on async pixel stats, and emscripten threads
Browse files Browse the repository at this point in the history
  • Loading branch information
wkjarosz committed Feb 25, 2024
1 parent dbb7925 commit b970fe5
Show file tree
Hide file tree
Showing 11 changed files with 593 additions and 257 deletions.
165 changes: 89 additions & 76 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,80 +128,6 @@ endif()
# ============================================================================
include(cmake/CPM.cmake)

CPMAddPackage("gh:sgorsten/linalg@2.2")
if(linalg_ADDED)
message(STATUS "linalg library added")
add_library(linalg INTERFACE IMPORTED)
target_include_directories(linalg INTERFACE "${linalg_SOURCE_DIR}")
endif()

CPMAddPackage("gh:fmtlib/fmt#10.1.1")
if(fmt_ADDED)
message(STATUS "fmt library added")
endif()

CPMAddPackage(
NAME spdlog
URL https://github.com/gabime/spdlog/archive/refs/tags/v1.13.0.zip
OPTIONS "SPDLOG_INSTALL NO" # don't create an installable target
"SPDLOG_FMT_EXTERNAL YES" # use the fmt library we added above instead of the bundled one
)
if(spdlog_ADDED)
message(STATUS "spdlog library added")
endif()

CPMAddPackage("gh:nothings/stb#a0a939058c579ddefd4c5671b046f29d12aeae01")
if(stb_ADDED)
message(STATUS "stb library added")
add_library(stb INTERFACE IMPORTED)
target_include_directories(stb INTERFACE "${stb_SOURCE_DIR}")
endif()

CPMAddPackage("gh:Armchair-Software/emscripten-browser-file#2ff8f8f791c0e7177b40e621ee3f758e03b31bf5")
if(emscripten-browser-file_ADDED)
message(STATUS "emscripten-browser-file library added")
add_library(emscripten-browser-file INTERFACE IMPORTED)
target_include_directories(emscripten-browser-file INTERFACE "${emscripten-browser-file_SOURCE_DIR}")
endif()

# CPMAddPackage("gh:samhocevar/portable-file-dialogs#7f852d88a480020d7f91957cbcefe514fc95000c")
# if(portable-file-dialogs_ADDED) message(STATUS "portable-file-dialogs library added")
# add_library(portable-file-dialogs INTERFACE IMPORTED) target_include_directories(portable-file-dialogs INTERFACE
# "${portable-file-dialogs_SOURCE_DIR}") endif()

CPMAddPackage(
NAME Imath
VERSION 3.1.6 # the git repo is incredibly large, so we download the archived include directory
URL https://github.com/AcademySoftwareFoundation/Imath/archive/refs/tags/v3.1.6.zip
OPTIONS "BUILD_SHARED_LIBS OFF" "BUILD_TESTING OFF" "EXCLUDE_FROM_ALL YES"
)

set(OPENEXR_OPTIONS "OPENEXR_BUILD_TOOLS OFF" "BUILD_SHARED_LIBS OFF" "OPENEXR_INSTALL_EXAMPLES OFF"
"BUILD_TESTING OFF" "EXCLUDE_FROM_ALL YES"
)

if(EMSCRIPTEN)
list(APPEND OPENEXR_OPTIONS "OPENEXR_ENABLE_THREADING OFF")
else()
list(APPEND OPENEXR_OPTIONS "OPENEXR_ENABLE_THREADING ON")
endif()

CPMAddPackage(
NAME openexr
GITHUB_REPOSITORY AcademySoftwareFoundation/openexr
GIT_TAG 71bffa3d7d89e46a37108732f5e63d554c10e018
OPTIONS ${OPENEXR_OPTIONS}
)

# CPMAddPackage("gh:epezent/implot#f156599faefe316f7dd20fe6c783bf87c8bb6fd9") if(implot_ADDED) message(STATUS "implot
# library added")

# # implot has no CMake support, so we create our own target set( implot STATIC ${implot_SOURCE_DIR}/implot.h #
# ${implot_SOURCE_DIR}/implot_internal.h ${implot_SOURCE_DIR}/implot.cpp ${implot_SOURCE_DIR}/implot_items.cpp ) #
# target_include_directories(implot PUBLIC ${implot_SOURCE_DIR}) set_target_properties(implot PROPERTIES CXX_STANDARD #
# 17) list(APPEND EXTRA_SOURCES ${implot_SOURCE_DIR}/implot.h ${implot_SOURCE_DIR}/implot_internal.h
# ${implot_SOURCE_DIR}/implot.cpp ${implot_SOURCE_DIR}/implot_items.cpp ) endif()

if(APPLE AND NOT EMSCRIPTEN)
set(HELLOIMGUI_USE_GLFW_METAL ON)

Expand Down Expand Up @@ -261,7 +187,9 @@ foreach(MY_RESOURCE_FILE ${MY_RESOURCE_FILES})
)
endforeach()

# CPMAddPackage("gh:pthom/imgui_bundle#6d6cfe8d68c0a57f62149e6ed85e8d2b905bca23")
set(HELLOIMGUI_EMSCRIPTEN_PTHREAD ON)
set(HELLOIMGUI_EMSCRIPTEN_PTHREAD_ALLOW_MEMORY_GROWTH OFF)

CPMAddPackage(
NAME imgui_bundle
GITHUB_REPOSITORY pthom/imgui_bundle
Expand All @@ -274,14 +202,95 @@ CPMAddPackage(
"IMGUI_BUNDLE_DISABLE_IMGUI_NODE_EDITOR ON"
# "IMGUI_BUNDLE_DISABLE_IMPLOT ON"
"IMGUI_BUNDLE_DISABLE_IMMVISION ON"
# "HELLOIMGUI_EMSCRIPTEN_PTHREAD ON" "HELLOIMGUI_EMSCRIPTEN_PTHREAD_ALLOW_MEMORY_GROWTH OFF"
# "IMGUI_BUNDLE_BUILD_DEMOS ON"
)
if(imgui_bundle_ADDED)
message(STATUS "imgui_bundle library added")
if(HELLOIMGUI_EMSCRIPTEN_PTHREAD)
add_compile_options(-pthread)
endif()
endif()

# CPMAddPackage("gh:pthom/hello_imgui#ed91f72da875b32ba96ef36d6633b9d7c0d95bee") if(hello_imgui_ADDED) message(STATUS
# "hello_imgui library added") endif()

CPMAddPackage("gh:sgorsten/linalg@2.2")
if(linalg_ADDED)
message(STATUS "linalg library added")
add_library(linalg INTERFACE IMPORTED)
target_include_directories(linalg INTERFACE "${linalg_SOURCE_DIR}")
endif()

CPMAddPackage("gh:fmtlib/fmt#10.1.1")
if(fmt_ADDED)
message(STATUS "fmt library added")
endif()

CPMAddPackage(
NAME spdlog
URL https://github.com/gabime/spdlog/archive/refs/tags/v1.13.0.zip
OPTIONS "SPDLOG_INSTALL NO" # don't create an installable target
"SPDLOG_FMT_EXTERNAL YES" # use the fmt library we added above instead of the bundled one
)
if(spdlog_ADDED)
message(STATUS "spdlog library added")
endif()

CPMAddPackage("gh:nothings/stb#a0a939058c579ddefd4c5671b046f29d12aeae01")
if(stb_ADDED)
message(STATUS "stb library added")
add_library(stb INTERFACE IMPORTED)
target_include_directories(stb INTERFACE "${stb_SOURCE_DIR}")
endif()

CPMAddPackage("gh:Armchair-Software/emscripten-browser-file#2ff8f8f791c0e7177b40e621ee3f758e03b31bf5")
if(emscripten-browser-file_ADDED)
message(STATUS "emscripten-browser-file library added")
add_library(emscripten-browser-file INTERFACE IMPORTED)
target_include_directories(emscripten-browser-file INTERFACE "${emscripten-browser-file_SOURCE_DIR}")
endif()

# # this is part of Dear ImGui Bundle, so no need to add it separately
# CPMAddPackage("gh:samhocevar/portable-file-dialogs#7f852d88a480020d7f91957cbcefe514fc95000c")
# if(portable-file-dialogs_ADDED) message(STATUS "portable-file-dialogs library added")
# add_library(portable-file-dialogs INTERFACE IMPORTED) target_include_directories(portable-file-dialogs INTERFACE
# "${portable-file-dialogs_SOURCE_DIR}") endif()

CPMAddPackage(
NAME Imath
VERSION 3.1.6 # the git repo is incredibly large, so we download the archived include directory
URL https://github.com/AcademySoftwareFoundation/Imath/archive/refs/tags/v3.1.6.zip
OPTIONS "BUILD_SHARED_LIBS OFF" "BUILD_TESTING OFF" "EXCLUDE_FROM_ALL YES"
)

set(OPENEXR_OPTIONS "OPENEXR_BUILD_TOOLS OFF" "BUILD_SHARED_LIBS OFF" "OPENEXR_INSTALL_EXAMPLES OFF"
"BUILD_TESTING OFF" "EXCLUDE_FROM_ALL YES"
)

if(EMSCRIPTEN AND NOT HELLOIMGUI_EMSCRIPTEN_PTHREAD)
list(APPEND OPENEXR_OPTIONS "OPENEXR_ENABLE_THREADING OFF")
else()
list(APPEND OPENEXR_OPTIONS "OPENEXR_ENABLE_THREADING ON")
endif()

CPMAddPackage(
NAME openexr
GITHUB_REPOSITORY AcademySoftwareFoundation/openexr
GIT_TAG 71bffa3d7d89e46a37108732f5e63d554c10e018
OPTIONS ${OPENEXR_OPTIONS}
)

# # this is part of Dear ImGui Bundle, so no need to add it separately
# CPMAddPackage("gh:epezent/implot#f156599faefe316f7dd20fe6c783bf87c8bb6fd9") if(implot_ADDED) message(STATUS "implot
# library added")

# # implot has no CMake support, so we create our own target set( implot STATIC ${implot_SOURCE_DIR}/implot.h #
# ${implot_SOURCE_DIR}/implot_internal.h ${implot_SOURCE_DIR}/implot.cpp ${implot_SOURCE_DIR}/implot_items.cpp ) #
# target_include_directories(implot PUBLIC ${implot_SOURCE_DIR}) set_target_properties(implot PROPERTIES CXX_STANDARD #
# 17) list(APPEND EXTRA_SOURCES ${implot_SOURCE_DIR}/implot.h ${implot_SOURCE_DIR}/implot_internal.h
# ${implot_SOURCE_DIR}/implot.cpp ${implot_SOURCE_DIR}/implot_items.cpp ) endif()

# ============================================================================
# Compile remainder of the codebase with compiler warnings turned on
# ============================================================================
Expand Down Expand Up @@ -369,7 +378,11 @@ if(EMSCRIPTEN)
-sMAX_WEBGL_VERSION=2
-sMIN_WEBGL_VERSION=2
)
hello_imgui_set_emscripten_target_initial_memory_megabytes(HDRView 240)
if(HELLOIMGUI_EMSCRIPTEN_PTHREAD)
target_link_options(HDRView PRIVATE -pthread)
target_link_options(HDRView PRIVATE -sPTHREAD_POOL_SIZE=2)
endif()
hello_imgui_set_emscripten_target_initial_memory_megabytes(HDRView 300)
else()
# target_link_libraries(HDRView PRIVATE portable-file-dialogs)
endif()
Expand Down
119 changes: 119 additions & 0 deletions assets/app_settings/emscripten/coi-serviceworker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*! coi-serviceworker v0.1.7 - Guido Zuidhof and contributors, licensed under MIT */
let coepCredentialless = false;
if (typeof window === 'undefined') {
self.addEventListener("install", () => self.skipWaiting());
self.addEventListener("activate", (event) => event.waitUntil(self.clients.claim()));

self.addEventListener("message", (ev) => {
if (!ev.data) {
return;
} else if (ev.data.type === "deregister") {
self.registration
.unregister()
.then(() => {
return self.clients.matchAll();
})
.then(clients => {
clients.forEach((client) => client.navigate(client.url));
});
} else if (ev.data.type === "coepCredentialless") {
coepCredentialless = ev.data.value;
}
});

self.addEventListener("fetch", function (event) {
const r = event.request;
if (r.cache === "only-if-cached" && r.mode !== "same-origin") {
return;
}

const request = (coepCredentialless && r.mode === "no-cors")
? new Request(r, {
credentials: "omit",
})
: r;
event.respondWith(
fetch(request)
.then((response) => {
if (response.status === 0) {
return response;
}

const newHeaders = new Headers(response.headers);
newHeaders.set("Cross-Origin-Embedder-Policy",
coepCredentialless ? "credentialless" : "require-corp"
);
if (!coepCredentialless) {
newHeaders.set("Cross-Origin-Resource-Policy", "cross-origin");
}
newHeaders.set("Cross-Origin-Opener-Policy", "same-origin");

return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newHeaders,
});
})
.catch((e) => console.error(e))
);
});

} else {
(() => {
// You can customize the behavior of this script through a global `coi` variable.
const coi = {
shouldRegister: () => true,
shouldDeregister: () => false,
//coepCredentialless: () => (window.chrome !== undefined || window.netscape !== undefined),
coepCredentialless: () => navigator.userAgentData?.brands?.some((b) => b.brand == "Chromium"),
doReload: () => window.location.reload(),
quiet: false,
...window.coi
};

const n = navigator;

if (n.serviceWorker && n.serviceWorker.controller) {
n.serviceWorker.controller.postMessage({
type: "coepCredentialless",
value: coi.coepCredentialless(),
});

if (coi.shouldDeregister()) {
n.serviceWorker.controller.postMessage({ type: "deregister" });
}
}

// If we're already coi: do nothing. Perhaps it's due to this script doing its job, or COOP/COEP are
// already set from the origin server. Also if the browser has no notion of crossOriginIsolated, just give up here.
if (window.crossOriginIsolated !== false || !coi.shouldRegister()) return;

if (!window.isSecureContext) {
!coi.quiet && console.log("COOP/COEP Service Worker not registered, a secure context is required.");
return;
}

// In some environments (e.g. Chrome incognito mode) this won't be available
if (n.serviceWorker) {
n.serviceWorker.register(window.document.currentScript.src).then(
(registration) => {
!coi.quiet && console.log("COOP/COEP Service Worker registered", registration.scope);

registration.addEventListener("updatefound", () => {
!coi.quiet && console.log("Reloading page to make use of updated COOP/COEP Service Worker.");
coi.doReload();
});

// If the registration is active, but it's not controlling the page
if (registration.active && !n.serviceWorker.controller) {
!coi.quiet && console.log("Reloading page to make use of COOP/COEP Service Worker.");
coi.doReload();
}
},
(err) => {
!coi.quiet && console.error("COOP/COEP Service Worker failed to register:", err);
}
);
}
})();
}
25 changes: 23 additions & 2 deletions assets/app_settings/emscripten/shell.emscripten.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@

<body>
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()"></canvas>

<!-- Call coi-serviceworker at startup -->
<script src="coi-serviceworker.js"></script>

<script type='text/javascript'>
var Module = {
preRun: [],
Expand All @@ -72,8 +76,25 @@
},
canvas: (function () {
var canvas = document.getElementById('canvas');
//canvas.addEventListener("webglcontextlost", function(e) { alert('FIXME: WebGL context lost, please reload the page'); e.preventDefault(); }, false);
return canvas;
canvas.addEventListener("webglcontextlost", function (e) {
alert('WebGL context lost, please reload the page');
e.preventDefault();
}, false);

// Explicitly create a WebGL 2 context with stencil buffer enabled.
if (typeof WebGL2RenderingContext !== 'undefined') {
var gl = canvas.getContext('webgl2', { stencil: true });
if (!gl) {
console.error('WebGL 2 not available, falling back to WebGL');
gl = canvas.getContext('webgl', { stencil: true });
}
if (!gl) {
alert('WebGL not available with stencil buffer');
}
return canvas;
} else {
alert('WebGL 2 not supported by this browser');
}
})(),
setStatus: function (text) {
console.log("status: " + text);
Expand Down
Loading

0 comments on commit b970fe5

Please sign in to comment.