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

Port to web #1263

Draft
wants to merge 40 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f789735
build: compile to web
luishfonseca Jun 13, 2024
704ddb9
fix(io): disable gamepad on emscripten
luishfonseca Jun 13, 2024
0393501
feat(io): target WebGL 2.0 on web
luishfonseca Jun 13, 2024
1ebabab
refactor(gl): remove Texture1D and CubeMapArray
luishfonseca Jun 13, 2024
3a8b83d
refactor(gl): remove Texture2DArray target type
luishfonseca Jun 13, 2024
d815577
refactor(gl): remove modes unsupported by GLES 3.0
luishfonseca Jun 13, 2024
05a6efb
refactor(gl): remove texture formats unsupported by GLES 3.0
luishfonseca Jun 13, 2024
5072e6c
fix(gl): disable error callback on web
luishfonseca Jun 13, 2024
9793398
fix(gl): disable image binding on web
luishfonseca Jun 13, 2024
86ea743
fix(gl): disable shader storage block on web
luishfonseca Jun 13, 2024
67839ff
fix(gl): disable geometry shaders on web
luishfonseca Jun 13, 2024
8e0c28e
fix(gl): disable compute shaders on web
luishfonseca Jun 13, 2024
32de7ab
fix(gl): add missing fs
luishfonseca Jun 13, 2024
ec88803
fix(gl): disable anisotropic filtering on web
luishfonseca Jun 13, 2024
fd434e3
refactor(gl): replace glGetTexImage with glReadPixels
luishfonseca Jun 13, 2024
b197f10
refactor(gl): replace glMapBuffer with glMapBufferRange
luishfonseca Jun 13, 2024
81cccbf
feat(gl): inject version in shader assets
luishfonseca Jun 13, 2024
6576538
docs(gl): add useful documentation at the top of the file
luishfonseca Jun 13, 2024
32d5744
fix(gl): use GLsizei for OGLTexture2D dimensions
luishfonseca Jun 13, 2024
6e7ab8b
fix(imgui): stop setting raster mode
luishfonseca Jun 13, 2024
13613dd
feat(samples): port gl.quad core sample to web
luishfonseca Jun 13, 2024
c602ed6
feat(ecs): add Cubos move constructor
RiscadoA Jun 14, 2024
2b0818b
fix(telemetry): handle log levels correctly in Emscripten
RiscadoA Jun 14, 2024
02a719e
feat(ecs): use proper main loop on Cubos::run with Emscripten
RiscadoA Jun 14, 2024
d1dcc42
build: embed assets on web releases
luishfonseca Jun 14, 2024
857eebe
fix(gl): remove shader version declarations
luishfonseca Jun 14, 2024
0cac55d
refactor(gl): check OpenGL functions for errors
luishfonseca Jun 15, 2024
bb4b650
feat(gl): add unsigned RenderDevice::clearTargetColor
RiscadoA Jun 15, 2024
b47ce17
fix(rendering): use unsigned clearTargetColor for render picker
RiscadoA Jun 15, 2024
a75efb2
fix(gl): use RGBA instead of RGB for render targets
RiscadoA Jun 15, 2024
6d6dd27
fix(rendering): add missing padding to some std140 structs
RiscadoA Jun 15, 2024
fd08de4
fix(gl): make PixelPackBuffer and Texture2D::copyTo WebGL compatible
RiscadoA Jun 15, 2024
32dd0f1
fix(gl): use WebGL 2 header instead of GLES header
RiscadoA Jun 15, 2024
2d56deb
fix(gl): replace map/unmap by fill
luishfonseca Jun 15, 2024
410854b
build: enable GL_EXT_color_buffer_float extension
luishfonseca Jun 15, 2024
d70db35
fix(gl): make shaders compatible with WebGL 2
luishfonseca Jun 15, 2024
7ead7d7
build: don't minify html
luishfonseca Jun 15, 2024
c707117
fix(gl): disable fill synchronization compilation when building for web
luishfonseca Jun 15, 2024
45f1821
fix(gizmos): use unsigned clearTargetColor for gizmos
luishfonseca Jun 15, 2024
4c97325
refactor(gl): make target type explicit in clearTargetColor commands
luishfonseca Jun 16, 2024
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
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,9 @@ MigrationBackup/
FodyWeavers.xsd

# VS Code files for those working on multiple tools
.vscode/
.vscode/*
*.code-workspace
!.vscode/cmake-kits.json

# Local History for Visual Studio Code
.history/
Expand Down Expand Up @@ -403,4 +404,5 @@ imgui.ini
/.envrc
/.direnv

.DS_store
.DS_store
/.em_cache
6 changes: 6 additions & 0 deletions .vscode/cmake-kits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"name": "Emscripten",
"toolchainFile": "cmake/Emscripten.cmake"
}
]
24 changes: 20 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,30 @@ function(cubos_common_target_options target)
set_target_properties(${target} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}")
endif()
endif()

if (EMSCRIPTEN)
target_link_options(${target} PUBLIC -sSTRICT -sASSERTIONS=0)
target_link_options(${target} PUBLIC -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2)
target_link_options(${target} PUBLIC -pthread -sPTHREAD_POOL_SIZE=navigator.hardwareConcurrency)
target_link_options(${target} PUBLIC -sMINIFY_HTML=0)

if (WITH_GLFW)
target_link_options(${target} PUBLIC -sUSE_GLFW=3)
endif ()

set_target_properties(${target} PROPERTIES SUFFIX ".html")
target_compile_options(${target} PUBLIC -pthread)
endif ()
endfunction()

# Add doctest as a submodule if it is not installed

if (DOCTEST_USE_SUBMODULE)
add_subdirectory(lib/doctest)
else ()
find_package(doctest REQUIRED)
if (BUILD_CORE_TESTS OR BUILD_ENGINE_TESTS)
if (DOCTEST_USE_SUBMODULE)
add_subdirectory(lib/doctest)
else ()
find_package(doctest REQUIRED)
endif ()
endif ()

add_subdirectory(core)
Expand Down
5 changes: 5 additions & 0 deletions api/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
option(BUILD_API_CORE_SAMPLES "Build cubos core c api samples" OFF)
option(BUILD_API_CORE_SHARED "Build cubos core c api as shared library?" ON)

if (EMSCRIPTEN AND BUILD_API_CORE_SHARED)
message(WARNING "Cannot build API core as shared library on Emscripten")
set(BUILD_API_CORE_SHARED OFF)
endif()

message("# Building core c api samples: " ${BUILD_API_CORE_SAMPLES})
message("# Building core c api as shared library: " ${BUILD_API_CORE_SHARED})

Expand Down
5 changes: 5 additions & 0 deletions api/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ set(CUBOS_API_ENGINE_SOURCE
option(BUILD_API_ENGINE_SAMPLES "Build cubos engine c api samples" OFF)
option(BUILD_API_ENGINE_SHARED "Build cubos engine c api as shared library?" ON)

if (EMSCRIPTEN AND BUILD_API_ENGINE_SHARED)
message(WARNING "Cannot build API engine as shared library on Emscripten")
set(BUILD_API_ENGINE_SHARED OFF)
endif()

message("# Building engine c api samples: " ${BUILD_API_ENGINE_SAMPLES})
message("# Building engine c api as shared library: " ${BUILD_API_ENGINE_SHARED})

Expand Down
403 changes: 403 additions & 0 deletions cmake/Emscripten.cmake

Large diffs are not rendered by default.

70 changes: 47 additions & 23 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ option(BUILD_CORE_SAMPLES "Build cubos core samples" OFF)
option(BUILD_CORE_TESTS "Build cubos core tests?" OFF)
option(BUILD_CORE_SHARED "Build cubos core as shared library?" ON)

if (EMSCRIPTEN AND BUILD_CORE_SHARED)
message(WARNING "Cannot build core as shared library on Emscripten")
set(BUILD_CORE_SHARED OFF)
endif()

message("# Building core samples: " ${BUILD_CORE_SAMPLES})
message("# Building core tests: " ${BUILD_CORE_TESTS})
message("# Building core as shared library: " ${BUILD_CORE_SHARED})
Expand Down Expand Up @@ -155,56 +160,75 @@ cubos_common_target_options(cubos-core)
if(WITH_OPENGL)
set(GLAD_SOUURCES_DIR "lib/glad")
add_subdirectory("${GLAD_SOUURCES_DIR}/cmake" glad_cmake SYSTEM)
glad_add_library(glad REPRODUCIBLE API gl:core=3.3)
if(EMSCRIPTEN)
glad_add_library(glad REPRODUCIBLE API gles2=3.0 EXTENSIONS GL_EXT_color_buffer_float)
else()
glad_add_library(glad REPRODUCIBLE API gl:core=3.3)
endif()
if(BUILD_CORE_SHARED)
set_property(TARGET glad PROPERTY POSITION_INDEPENDENT_CODE ON)
endif()
if (EMSCRIPTEN)
target_link_libraries(cubos-core PRIVATE GL)
endif ()
target_link_libraries(cubos-core PRIVATE glad)
target_compile_definitions(cubos-core PRIVATE WITH_OPENGL GLAD_GLAPI_EXPORT GLAD_GLAPI_EXPORT_BUILD)
endif()

if(WITH_GLFW)
if(GLFW_USE_SUBMODULE)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(lib/glfw)
else()
find_package(glfw3 REQUIRED)
endif()
if (NOT EMSCRIPTEN)
if (GLFW_USE_SUBMODULE)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(lib/glfw)
else ()
find_package(glfw3 REQUIRED)
endif ()
endif ()

target_link_libraries(cubos-core PRIVATE glfw)
target_compile_definitions(cubos-core PRIVATE WITH_GLFW)
endif()

if(WITH_OPENAL)
set(ALSOFT_UTILS OFF CACHE BOOL "" FORCE)
set(ALSOFT_NO_CONFIG_UTIL OFF CACHE BOOL "" FORCE)
set(ALSOFT_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(lib/openal-soft)
target_include_directories(cubos-core PRIVATE lib/openal-soft/include)
target_link_libraries(cubos-core PRIVATE OpenAL)
target_compile_definitions(cubos-core PRIVATE WITH_OPENAL)
if (WITH_OPENAL)
if (NOT EMSCRIPTEN)
set(ALSOFT_UTILS OFF CACHE BOOL "" FORCE)
set(ALSOFT_NO_CONFIG_UTIL OFF CACHE BOOL "" FORCE)
set(ALSOFT_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(lib/openal-soft)
target_include_directories(cubos-core PRIVATE lib/openal-soft/include)
target_link_libraries(cubos-core PRIVATE OpenAL)
else ()
target_link_libraries(cubos-core PRIVATE openal)
endif ()

target_compile_definitions(cubos-core PRIVATE WITH_OPENAL)
endif()

if(GLM_USE_SUBMODULE)
if(GLM_USE_SUBMODULE OR EMSCRIPTEN)
add_subdirectory(lib/glm SYSTEM)
else()
find_package(glm REQUIRED)
endif()
target_compile_definitions(cubos-core PUBLIC GLM_FORCE_SILENT_WARNINGS) # Needed for compilation to succeed on MSVC
target_link_libraries(cubos-core PUBLIC glm::glm)

set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(lib/json)
target_link_libraries(cubos-core PUBLIC nlohmann_json::nlohmann_json)

add_subdirectory(lib/stduuid)
target_link_libraries(cubos-core PUBLIC stduuid)

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(cubos-core PUBLIC ${CMAKE_DL_LIBS})

target_compile_definitions(cubos-core PUBLIC GLM_FORCE_SILENT_WARNINGS) # Needed for compilation to succeed on MSVC
target_link_libraries(cubos-core PUBLIC glm::glm nlohmann_json::nlohmann_json ${CMAKE_DL_LIBS})
target_link_libraries(cubos-core PRIVATE Threads::Threads)
set(THREADS_PREFER_PTHREAD_FLAG ON)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
if (NOT EMSCRIPTEN)
find_package(Threads REQUIRED)
target_link_libraries(cubos-core PRIVATE Threads::Threads)
endif ()

# Add core tests
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_CORE_TESTS)
Expand Down
16 changes: 10 additions & 6 deletions core/include/cubos/core/ecs/cubos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ namespace cubos::core::ecs
/// @param argv Argument array.
Cubos(int argc, char** argv);

/// @brief Move constructs.
/// @param other Other application.
Cubos(Cubos&& other) noexcept;

/// @name Forbid copying.
/// @{
Cubos(const Cubos&) = delete;
Expand Down Expand Up @@ -300,7 +304,7 @@ namespace cubos::core::ecs
/// @param plugin Plugin.
void uninstall(Plugin plugin);

World mWorld;
World* mWorld;
SystemRegistry mSystemRegistry;
State* mState{nullptr};

Expand Down Expand Up @@ -370,7 +374,7 @@ namespace cubos::core::ecs
TagBuilder& runIf(auto func)
{
auto conditionId = mCubos.mSystemRegistry.add(mName + "#condition",
System<bool>::make(mCubos.mWorld, std::move(func), {}));
System<bool>::make(*mCubos.mWorld, std::move(func), {}));
mPlanner.onlyIf(mTagId, conditionId);
return *this;
}
Expand All @@ -381,7 +385,7 @@ namespace cubos::core::ecs
TagBuilder& repeatWhile(auto func)
{
auto conditionId =
mCubos.mSystemRegistry.add(mName, System<bool>::make(mCubos.mWorld, std::move(func), {}));
mCubos.mSystemRegistry.add(mName, System<bool>::make(*mCubos.mWorld, std::move(func), {}));
CUBOS_ASSERT(mPlanner.repeatWhile(mTagId, conditionId), "Tag was already set to repeating");
return *this;
}
Expand Down Expand Up @@ -517,15 +521,15 @@ namespace cubos::core::ecs
SystemBuilder&& onlyIf(auto function) &&
{
CUBOS_ASSERT(!mCondition.contains(), "Only one condition can be set per system");
mCondition.replace(System<bool>::make(mCubos.mWorld, std::move(function), {}));
mCondition.replace(System<bool>::make(*mCubos.mWorld, std::move(function), {}));
return std::move(*this);
}

/// @brief Finishes building the system with the given function.
/// @param function System function.
void call(auto function) &&
{
this->finish(System<void>::make(mCubos.mWorld, std::move(function), mOptions));
this->finish(System<void>::make(*mCubos.mWorld, std::move(function), mOptions));
}

private:
Expand Down Expand Up @@ -660,7 +664,7 @@ namespace cubos::core::ecs
/// @param function System function.
void call(auto function) &&
{
this->finish(System<void>::make(mCubos.mWorld, std::move(function), mOptions));
this->finish(System<void>::make(*mCubos.mWorld, std::move(function), mOptions));
}

private:
Expand Down
3 changes: 3 additions & 0 deletions core/include/cubos/core/ecs/system/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ namespace cubos::core::ecs
/// @brief Constructs.
SystemRegistry() = default;

/// @brief Move constructs.
SystemRegistry(SystemRegistry&&) = default;

/// @brief Forbid copying.
SystemRegistry(const SystemRegistry&) = delete;

Expand Down
Loading
Loading