From d18b48c9dd3e7823e680a30fc7603e2f45134d19 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Fri, 26 Apr 2024 18:05:15 -0400 Subject: [PATCH] emscripten / webasm full support for 2d --- CMakeLists.txt | 10 ++++++---- cmake/link_flags.cmake | 12 ++++++++++++ include/blt/gfx/renderer/resource_manager.h | 9 +++++++++ include/blt/gfx/texture.h | 2 +- src/blt/gfx/renderer/resource_manager.cpp | 2 +- src/blt/gfx/texture.cpp | 11 ++++++++--- src/blt/gfx/window.cpp | 3 +++ 7 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 cmake/link_flags.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index b283787..53458d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.25) -set(BLT_GRAPHICS_VERSION 0.11.0) +set(BLT_GRAPHICS_VERSION 0.11.1) set(BLT_GRAPHICS_TEST_VERSION 0.0.1) project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION}) @@ -130,13 +130,15 @@ endif () project(BLT_WITH_GRAPHICS) if (EMSCRIPTEN) - list(APPEND TARGETS BLT_WITH_GRAPHICS) + #list(APPEND BLT_LINK_FLAG_TARGETS BLT_WITH_GRAPHICS) if (${BUILD_GRAPHICS_TESTS}) - list(APPEND TARGETS BLT_GRAPHICS_TESTS) + list(APPEND BLT_LINK_FLAG_TARGETS BLT_GRAPHICS_TESTS) endif () #set_target_properties(FinalProject PROPERTIES LINK_FLAGS "-sMAX_WEBGL_VERSION=2 -sASSERTIONS=1 -pthread -sPTHREAD_POOL_SIZE=8 -s INITIAL_MEMORY=134217728 -sUSE_GLFW=3 --preload-file 'assets'") - set_target_properties(${TARGETS} PROPERTIES LINK_FLAGS "-sMAX_WEBGL_VERSION=2 -sFULL_ES2=1 -sFULL_ES3 -sASSERTIONS=1 -sUSE_GLFW=3") + include(cmake/link_flags.cmake) + #set_target_properties(${TARGETS} PROPERTIES LINK_FLAGS "-sMAX_WEBGL_VERSION=2 -sFULL_ES2=1 -sFULL_ES3 -sASSERTIONS=1 -sUSE_GLFW=3") + #set_property(GLOBAL PROPERTY LINK_FLAGS "-sMAX_WEBGL_VERSION=2 -sFULL_ES2=1 -sFULL_ES3 -sASSERTIONS=1 -sUSE_GLFW=3") #set_target_properties(BLT_WITH_GRAPHICS PROPERTIES COMPILE_FLAGS "-pthread") else () target_link_libraries(BLT_WITH_GRAPHICS PUBLIC glfw) diff --git a/cmake/link_flags.cmake b/cmake/link_flags.cmake new file mode 100644 index 0000000..0d1ecde --- /dev/null +++ b/cmake/link_flags.cmake @@ -0,0 +1,12 @@ +list(APPEND BLT_LINK_FLAG_TARGETS ${PROJECT_NAME}) +string(ASCII 27 Esc) +set(ColourReset "${Esc}[m") +set(Blue "${Esc}[34m") +message("Setting link flags for targets ${Blue}${BLT_LINK_FLAG_TARGETS}${ColourReset}") +if ("${BLT_PRELOAD_PATH}" STREQUAL "") + message(STATUS "Setting link flags without asset preloading") + set_target_properties(${BLT_LINK_FLAG_TARGETS} PROPERTIES LINK_FLAGS "-sMAX_WEBGL_VERSION=2 -sFULL_ES2=1 -sFULL_ES3 -sASSERTIONS=1 -sUSE_GLFW=3") +else () + message(STATUS "Setting link flags including preload file path ${BLT_PRELOAD_PATH}") + set_target_properties(${BLT_LINK_FLAG_TARGETS} PROPERTIES LINK_FLAGS "-sMAX_WEBGL_VERSION=2 -sFULL_ES2=1 -sFULL_ES3 -sASSERTIONS=1 -sUSE_GLFW=3 --preload-file ${BLT_PRELOAD_PATH}") +endif () \ No newline at end of file diff --git a/include/blt/gfx/renderer/resource_manager.h b/include/blt/gfx/renderer/resource_manager.h index 074ecb2..5d5453c 100644 --- a/include/blt/gfx/renderer/resource_manager.h +++ b/include/blt/gfx/renderer/resource_manager.h @@ -49,6 +49,7 @@ namespace blt::gfx std::vector textures_to_load; std::vector loaded_textures; blt::hashmap_t textures_2d; + std::string resource_prefix; public: resource_manager() = default; @@ -72,6 +73,14 @@ namespace blt::gfx textures_2d[name] = texture; } + /** + * Sets the directory used to prefix all asset loading with + */ + inline void setPrefixDirectory(std::string path) + { + resource_prefix = std::move(path); + } + void cleanup(); }; diff --git a/include/blt/gfx/texture.h b/include/blt/gfx/texture.h index a00cfc6..503174e 100644 --- a/include/blt/gfx/texture.h +++ b/include/blt/gfx/texture.h @@ -185,7 +185,7 @@ namespace blt::gfx struct texture_gl2D : public texture_gl { public: - explicit texture_gl2D(const texture_data& data, GLint colorMode = GL_RGBA8); + explicit texture_gl2D(const texture_data& data); texture_gl2D(int width, int height, GLint colorMode = GL_RGBA8); diff --git a/src/blt/gfx/renderer/resource_manager.cpp b/src/blt/gfx/renderer/resource_manager.cpp index 0cb1875..4146fa0 100644 --- a/src/blt/gfx/renderer/resource_manager.cpp +++ b/src/blt/gfx/renderer/resource_manager.cpp @@ -30,7 +30,7 @@ namespace blt::gfx void resource_manager::enqueue(std::string path, std::string name) { - textures_to_load.emplace_back(std::move(path), std::move(name)); + textures_to_load.emplace_back(resource_prefix + std::move(path), std::move(name)); } void resource_manager::load_resources(std::size_t threads) diff --git a/src/blt/gfx/texture.cpp b/src/blt/gfx/texture.cpp index 226908b..f6b665e 100644 --- a/src/blt/gfx/texture.cpp +++ b/src/blt/gfx/texture.cpp @@ -85,8 +85,13 @@ void blt::gfx::texture_gl::setDefaults() const glTexParameteri(textureBindType, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(textureBindType, GL_TEXTURE_WRAP_T, GL_REPEAT); // nearest preserves the pixely look +#ifdef __EMSCRIPTEN__ + glTexParameteri(textureBindType, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(textureBindType, GL_TEXTURE_MAG_FILTER, GL_NEAREST); +#else glTexParameteri(textureBindType, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR); glTexParameteri(textureBindType, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_LINEAR); +#endif #ifdef GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT // Anisotropy helps preserve textures at oblique angles float a = 0; @@ -140,12 +145,12 @@ blt::gfx::texture_gl2D::texture_gl2D(int width, int height, GLint colorMode): te glTexStorage2D(textureBindType, MIPMAP_LEVELS, colorMode, width, height); } -blt::gfx::texture_gl2D::texture_gl2D(const blt::gfx::texture_data& data, GLint colorMode): - texture_gl(data.width(), data.height(), GL_TEXTURE_2D, colorMode) +blt::gfx::texture_gl2D::texture_gl2D(const blt::gfx::texture_data& data): + texture_gl(data.width(), data.height(), GL_TEXTURE_2D, data.channels() == 4 ? GL_RGBA8 : GL_RGB8) { bind(); setDefaults(); - glTexStorage2D(textureBindType, 4, colorMode, data.width(), data.height()); + glTexStorage2D(textureBindType, 4, textureColorMode, data.width(), data.height()); upload((void*) data.data(), data.channels() == 4 ? GL_RGBA : GL_RGB, 0, 0, 0, data.width(), data.height()); bind(); generateMipmaps(); diff --git a/src/blt/gfx/window.cpp b/src/blt/gfx/window.cpp index 97df63b..aed36d4 100644 --- a/src/blt/gfx/window.cpp +++ b/src/blt/gfx/window.cpp @@ -177,6 +177,9 @@ namespace blt::gfx void init(const window_data& data) { +#ifdef __EMSCRIPTEN__ + blt::logging::setLogOutputFormat("[${{TIME}}] [${{LOG_LEVEL}}] (${{FILE}}:${{LINE}}) ${{STR}}\n"); +#endif /* -- Set up Error Callback -- */ glfwSetErrorCallback(error_callback); BLT_ASSERT(glfwInit() && "Unable to init GLFW. Aborting.");