emscripten / webasm full support for 2d

main
Brett 2024-04-26 18:05:15 -04:00
parent 0233ce7549
commit d18b48c9dd
7 changed files with 40 additions and 9 deletions

View File

@ -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)

12
cmake/link_flags.cmake Normal file
View File

@ -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 ()

View File

@ -49,6 +49,7 @@ namespace blt::gfx
std::vector<loadable_texture> textures_to_load;
std::vector<texture_file*> loaded_textures;
blt::hashmap_t<std::string, texture_gl2D*> 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();
};

View File

@ -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);

View File

@ -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)

View File

@ -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();

View File

@ -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.");