Improve emscripten support

This commit is contained in:
SirLynix 2022-12-06 08:05:28 +01:00 committed by Jérôme Leclercq
parent 7cb90782de
commit ea5c5240fc
10 changed files with 53 additions and 32 deletions

View File

@ -1,7 +1,3 @@
target("PBR") target("PBR")
add_deps("NazaraGraphics") add_deps("NazaraGraphics")
add_files("main.cpp") add_files("main.cpp")
if is_plat("wasm") then
add_ldflags("--preload-file assets/", {force = true})
end

View File

@ -3,5 +3,9 @@ option("examples", { description = "Build examples", default = true })
if has_config("examples") then if has_config("examples") then
set_group("Examples") set_group("Examples")
if is_plat("wasm") then
add_ldflags("--preload-file assets/", { force = true })
end
includes("*/xmake.lua") includes("*/xmake.lua")
end end

View File

@ -7,12 +7,12 @@
#ifndef NAZARA_OPENGLRENDERER_WRAPPER_WEB_WEBCONTEXT_HPP #ifndef NAZARA_OPENGLRENDERER_WRAPPER_WEB_WEBCONTEXT_HPP
#define NAZARA_OPENGLRENDERER_WRAPPER_WEB_WEBCONTEXT_HPP #define NAZARA_OPENGLRENDERER_WRAPPER_WEB_WEBCONTEXT_HPP
#include <emscripten/html5.h>
#include <Nazara/Prerequisites.hpp> #include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/DynLib.hpp> #include <Nazara/Core/DynLib.hpp>
#include <Nazara/OpenGLRenderer/Config.hpp> #include <Nazara/OpenGLRenderer/Config.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Context.hpp> #include <Nazara/OpenGLRenderer/Wrapper/Context.hpp>
#include <Nazara/Platform/WindowHandle.hpp> #include <Nazara/Platform/WindowHandle.hpp>
#include <emscripten/html5.h>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <unordered_set> #include <unordered_set>

View File

@ -4,10 +4,10 @@
#pragma once #pragma once
#ifndef NAZARA_BASIC_MAINLOOP_HPP #ifndef NAZARA_UTILITY_BASICMAINLOOP_HPP
#define NAZARA_BASIC_MAINLOOP_HPP #define NAZARA_UTILITY_BASICMAINLOOP_HPP
#include <Nazara/Renderer.hpp> #include <Nazara/Platform/Window.hpp>
#ifdef NAZARA_PLATFORM_WEB #ifdef NAZARA_PLATFORM_WEB
#include <emscripten/html5.h> #include <emscripten/html5.h>
@ -16,7 +16,7 @@
namespace Nz namespace Nz
{ {
template <typename CallbackT> template <typename CallbackT>
void BasicMainloop(RenderWindow& window, CallbackT&& callback) void BasicMainloop(Window& window, CallbackT&& callback)
{ {
#ifndef NAZARA_PLATFORM_WEB #ifndef NAZARA_PLATFORM_WEB
while (window.IsOpen()) while (window.IsOpen())
@ -40,4 +40,4 @@ namespace Nz
} }
#endif // NAZARA_BASIC_MAINLOOP_HPP #endif // NAZARA_UTILITY_BASICMAINLOOP_HPP

View File

@ -174,6 +174,10 @@ namespace Nz
m_states = states; m_states = states;
m_states.sanitized = true; //< Shader is always sanitized (because of keywords) m_states.sanitized = true; //< Shader is always sanitized (because of keywords)
#ifdef NAZARA_PLATFORM_WEB
m_states.optimize = true; //< Always remove unused code with emscripten (prevents errors on draw calls when no buffer is bound on a unused binding)
#endif
nzsl::Ast::SanitizeVisitor::Options options = nzsl::GlslWriter::GetSanitizeOptions(); nzsl::Ast::SanitizeVisitor::Options options = nzsl::GlslWriter::GetSanitizeOptions();
options.optionValues = states.optionValues; options.optionValues = states.optionValues;
options.moduleResolver = states.shaderModuleResolver; options.moduleResolver = states.shaderModuleResolver;

View File

@ -147,11 +147,14 @@ namespace Nz::GL
GLenum Context::BindFramebuffer(GLuint fbo) const GLenum Context::BindFramebuffer(GLuint fbo) const
{ {
// it looks like emscripten wants us to rebind the FBO everytime
#ifndef NAZARA_PLATFORM_WEB
if (m_state.boundDrawFBO == fbo) if (m_state.boundDrawFBO == fbo)
return GL_DRAW_FRAMEBUFFER; return GL_DRAW_FRAMEBUFFER;
else if (m_state.boundReadFBO == fbo) else if (m_state.boundReadFBO == fbo)
return GL_READ_FRAMEBUFFER; return GL_READ_FRAMEBUFFER;
else else
#endif
{ {
if (!SetCurrentContext(this)) if (!SetCurrentContext(this))
throw std::runtime_error("failed to activate context"); throw std::runtime_error("failed to activate context");
@ -166,12 +169,10 @@ namespace Nz::GL
void Context::BindFramebuffer(FramebufferTarget target, GLuint fbo) const void Context::BindFramebuffer(FramebufferTarget target, GLuint fbo) const
{ {
auto& currentFbo = (target == FramebufferTarget::Draw) ? m_state.boundDrawFBO : m_state.boundReadFBO; auto& currentFbo = (target == FramebufferTarget::Draw) ? m_state.boundDrawFBO : m_state.boundReadFBO;
#ifdef NAZARA_PLATFORM_WEB // it looks like emscripten wants us to rebind the FBO everytime
constexpr bool isWeb = true; #ifndef NAZARA_PLATFORM_WEB
#else if (currentFbo != fbo)
constexpr bool isWeb = false;
#endif #endif
if (currentFbo != fbo || isWeb);
{ {
if (!SetCurrentContext(this)) if (!SetCurrentContext(this))
throw std::runtime_error("failed to activate context"); throw std::runtime_error("failed to activate context");

View File

@ -194,6 +194,10 @@ namespace Nz
WindowHandle WindowImpl::GetSystemHandle() const WindowHandle WindowImpl::GetSystemHandle() const
{ {
#ifdef NAZARA_PLATFORM_WEB
WindowHandle handle;
return handle;
#else
SDL_SysWMinfo wmInfo; SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version); SDL_VERSION(&wmInfo.version);
@ -256,6 +260,7 @@ namespace Nz
} }
return handle; return handle;
#endif
} }
bool WindowImpl::HasFocus() const bool WindowImpl::HasFocus() const

View File

@ -1,7 +1,3 @@
target("GraphicsTest") target("GraphicsTest")
add_deps("NazaraGraphics") add_deps("NazaraGraphics")
add_files("main.cpp") add_files("main.cpp")
if is_plat("wasm") then
add_ldflags("--preload-file assets/", {force = true})
end

View File

@ -176,9 +176,6 @@ if not has_config("embed_rendererbackends") then
end end
end end
add_ldflags("-g -s NO_DISABLE_EXCEPTION_CATCHING -s ALLOW_MEMORY_GROWTH=1")
add_cxflags("-g -s NO_DISABLE_EXCEPTION_CATCHING")
NazaraModules = modules NazaraModules = modules
includes("xmake/**.lua") includes("xmake/**.lua")
@ -194,12 +191,27 @@ option("unitybuild", { description = "Build the engine using unity build", defau
set_project("NazaraEngine") set_project("NazaraEngine")
set_xmakever("2.7.3") set_xmakever("2.7.3")
add_requires("dr_wav", "entt 3.10.1", "fmt", "frozen", "kiwisolver", "minimp3", "ordered_map", "stb") add_requires("chipmunk2d", "dr_wav", "entt 3.10.1", "fmt", "frozen", "kiwisolver", "libflac", "minimp3", "ordered_map", "stb")
if not is_plat("wasm") then add_requires("libvorbis", { configs = { with_vorbisenc = false } })
add_requires("chipmunk2d", "libsdl", "libflac", "efsw")
if is_plat("wasm") then
-- Enable some flags for emscripten
add_cxflags("-g", "-sNO_DISABLE_EXCEPTION_CATCHING")
add_ldflags("-g", "-sNO_DISABLE_EXCEPTION_CATCHING", "-sALLOW_MEMORY_GROWTH", "-sWASM_BIGINT")
add_ldflags("-sERROR_ON_WASM_CHANGES_AFTER_LINK", { force = true })
-- we can't use wasm nzsl to compile shaders
if has_config("compile_shaders") then
add_requires("nzsl~host", { kind = "binary" })
end
else
-- these libraries have ports in emscripten
add_requires("libsdl")
add_requires("freetype", { configs = { bzip2 = true, png = true, woff2 = true, zlib = true, debug = is_mode("debug") } }) add_requires("freetype", { configs = { bzip2 = true, png = true, woff2 = true, zlib = true, debug = is_mode("debug") } })
add_requires("libvorbis", { configs = { with_vorbisenc = false } })
add_requires("openal-soft", { configs = { shared = true }}) add_requires("openal-soft", { configs = { shared = true }})
-- these libraries aren't supported on emscripten
add_requires("efsw")
add_requires("newtondynamics3", { debug = is_plat("windows") and is_mode("debug") }) -- Newton doesn't like compiling in Debug on Linux add_requires("newtondynamics3", { debug = is_plat("windows") and is_mode("debug") }) -- Newton doesn't like compiling in Debug on Linux
end end

View File

@ -1,16 +1,19 @@
-- Compile shaders to includables headers -- Compile shaders to includables headers
rule("nzsl.compile.shaders") rule("nzsl.compile.shaders")
on_load(function (target) set_extensions(".nzsl", ".nzslb")
target:add("packages", "nzsl")
end)
before_buildcmd_file(function (target, batchcmds, shaderfile, opt) before_buildcmd_file(function (target, batchcmds, shaderfile, opt)
import("core.project.project")
import("core.tool.toolchain") import("core.tool.toolchain")
local nzslc = path.join(target:pkg("nzsl"):installdir(), "bin", "nzslc") local nzsl = project.required_package("nzsl~host") or project.required_package("nzsl")
assert(nzsl, "nzsl package not found")
-- warning: project.required_package is not a stable interface, this may break in the future
local nzslc = path.join(nzsl:installdir(), "bin", "nzslc")
-- add commands -- add commands
batchcmds:show_progress(opt.progress, "${color.build.object}compiling shader %s", shaderfile) batchcmds:show_progress(opt.progress, "${color.build.object}compiling.shader %s", shaderfile)
local argv = { "--compile=nzslb-header", "--partial", "--optimize" } local argv = { "--compile=nzslb-header", "--partial", "--optimize" }
-- handle --log-format -- handle --log-format
@ -21,6 +24,7 @@ rule("nzsl.compile.shaders")
table.insert(argv, shaderfile) table.insert(argv, shaderfile)
-- on mingw we need run envs because of .dll dependencies which may be not part of the PATH
local envs local envs
if is_plat("mingw") then if is_plat("mingw") then
local mingw = toolchain.load("mingw") local mingw = toolchain.load("mingw")
@ -28,7 +32,6 @@ rule("nzsl.compile.shaders")
envs = mingw:runenvs() envs = mingw:runenvs()
end end
end end
batchcmds:vrunv(nzslc, argv, { curdir = ".", envs = envs }) batchcmds:vrunv(nzslc, argv, { curdir = ".", envs = envs })
local outputFile = path.join(path.directory(shaderfile), path.basename(shaderfile) .. ".nzslb.h") local outputFile = path.join(path.directory(shaderfile), path.basename(shaderfile) .. ".nzslb.h")