~ WIP port emscripen (WebGL)

This commit is contained in:
REMqb
2022-08-14 21:46:16 +02:00
committed by Jérôme Leclercq
parent 304bf35c08
commit f172330aaf
27 changed files with 721 additions and 36 deletions

View File

@@ -130,6 +130,10 @@ namespace Nz::GL
void Context::BindBuffer(BufferTarget target, GLuint buffer, bool force) const
{
#ifdef NAZARA_PLATFORM_WEB
force = true;
#endif
if (m_state.bufferTargets[UnderlyingCast(target)] != buffer || force)
{
if (!SetCurrentContext(this))
@@ -162,7 +166,12 @@ namespace Nz::GL
void Context::BindFramebuffer(FramebufferTarget target, GLuint fbo) const
{
auto& currentFbo = (target == FramebufferTarget::Draw) ? m_state.boundDrawFBO : m_state.boundReadFBO;
if (currentFbo != fbo)
#ifdef NAZARA_PLATFORM_WEB
constexpr bool isWeb = true;
#else
constexpr bool isWeb = false;
#endif
if (currentFbo != fbo || isWeb);
{
if (!SetCurrentContext(this))
throw std::runtime_error("failed to activate context");
@@ -427,7 +436,7 @@ namespace Nz::GL
try
{
#define NAZARA_OPENGLRENDERER_FUNC(name, sig) loader.Load<sig, UnderlyingCast(FunctionIndex:: name)>(name, #name, true, true);
#define NAZARA_OPENGLRENDERER_FUNC(name, sig) loader.Load<sig, UnderlyingCast(FunctionIndex:: name)>(name, #name, false, true);
#define NAZARA_OPENGLRENDERER_EXT_FUNC(name, sig) //< Do nothing
NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_EXT_FUNC)
#undef NAZARA_OPENGLRENDERER_EXT_FUNC

View File

@@ -0,0 +1,255 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - OpenGL renderer"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/OpenGLRenderer/Wrapper/Web/WebContext.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Web/WebLoader.hpp>
#include <Nazara/Utils/CallOnExit.hpp>
#include <array>
#include <cassert>
#include <cstdlib>
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz::GL
{
WebContext::~WebContext()
{
Destroy();
}
bool WebContext::Create(const ContextParams& params, const WebContext* shareContext)
{
Destroy(); //< In case a previous display or surface hasn't been released
m_params = params;
std::size_t configCount;
std::array<EmscriptenWebGLContextAttributes, 0xFF> configs;
if (!ChooseConfig(configs.data(), configs.size(), &configCount))
return false;
std::size_t configIndex = 0;
return CreateInternal(configs[configIndex], shareContext);
}
bool WebContext::Create(const ContextParams& params, WindowHandle /*window*/, const WebContext* /*shareContext*/)
{
/*NazaraError("Unexpected context creation call");
return false;*/
return Create(params, nullptr);
}
void WebContext::Destroy()
{
if (s_handle > 0)
{
assert(s_handle > 0);
OnContextRelease();
NotifyContextDestruction(this);
s_handleCounter--;
if(s_handleCounter == 0)
{
emscripten_webgl_destroy_context(s_handle);
s_handle = 0;
}
}
}
void WebContext::EnableVerticalSync(bool /*enabled*/)
{
// TODO
}
void WebContext::SwapBuffers()
{
emscripten_webgl_commit_frame();
}
bool WebContext::ChooseConfig(EmscriptenWebGLContextAttributes* configs, std::size_t maxConfigCount, std::size_t* configCount)
{
EmscriptenWebGLContextAttributes configAttributes;
emscripten_webgl_init_context_attributes(&configAttributes);
// https://emscripten.org/docs/api_reference/html5.h.html#c.EmscriptenWebGLContextAttributes
configAttributes.alpha = true;
configAttributes.depth = m_params.depthBits > 0;
configAttributes.stencil = m_params.stencilBits > 0;
configAttributes.antialias = false;
configAttributes.premultipliedAlpha = true;
configAttributes.preserveDrawingBuffer = false;
configAttributes.powerPreference = EM_WEBGL_POWER_PREFERENCE_DEFAULT;
configAttributes.failIfMajorPerformanceCaveat = false;
configAttributes.majorVersion = m_params.glMajorVersion;
configAttributes.minorVersion = m_params.glMinorVersion;
configAttributes.enableExtensionsByDefault = true;
configAttributes.explicitSwapControl = false; // todo
configAttributes.renderViaOffscreenBackBuffer = false; // todo
configAttributes.proxyContextToMainThread = false; // todo
size_t numConfig = 1;
configs[0] = configAttributes;
*configCount = numConfig;
return true;
}
bool WebContext::CreateInternal(EmscriptenWebGLContextAttributes config, const WebContext* shareContext)
{
if(s_handleCounter > 0)
{
s_handleCounter++;
return true;
}
if (shareContext)
{
NazaraWarning(std::string("shared contexes are not supported by WebGL but shareContext is not null"));
}
struct Version
{
unsigned int major;
unsigned int minor;
};
if (m_params.type == ContextType::OpenGL_ES)
{
// Create OpenGL ES context
std::array<Version, 3> supportedGL_ESVersions = {
{
{ 2, 0 },
{ 1, 0 }
}
};
for (const Version& version : supportedGL_ESVersions)
{
if (m_params.glMajorVersion != 0)
{
if (version.major > m_params.glMajorVersion)
continue;
if (m_params.glMinorVersion != 0 && version.minor > m_params.glMinorVersion)
continue;
}
config.majorVersion = version.major;
config.minorVersion = version.minor;
s_handle = emscripten_webgl_create_context("canvas", &config);
if (s_handle > 0)
{
break;
}
}
}
else
{
NazaraError(std::string("failed to create WebGL context: OpenGL is not supported"));
return false;
}
if (s_handle <= 0)
{
NazaraError(std::string("failed to create Web context: ") + WebLoader::TranslateError(static_cast<EMSCRIPTEN_RESULT>(s_handle)));
return false;
}
LoadExt();
s_handleCounter++;
return true;
}
bool WebContext::ImplementFallback(const std::string_view& function)
{
if (Context::ImplementFallback(function))
return true;
if (m_params.type == ContextType::OpenGL_ES)
return false; //< Implement fallback only for OpenGL (when emulating OpenGL ES)
if (function == "glClearDepthf")
{
fallbacks.glClearDepth = reinterpret_cast<Fallback::glClearDepthProc>(m_loader.LoadFunction("glClearDepth"));
if (!fallbacks.glClearDepth)
return false;
glClearDepthf = [](GLfloat depth)
{
const WebContext* context = static_cast<const WebContext*>(GetCurrentContext());
assert(context);
context->fallbacks.glClearDepth(depth);
};
}
return true;
}
bool WebContext::Activate() const
{
EMSCRIPTEN_RESULT succeeded = emscripten_webgl_make_context_current(s_handle);
if (succeeded != EMSCRIPTEN_RESULT_SUCCESS)
{
NazaraError("failed to activate context");
return false;
}
return true;
}
void WebContext::Desactivate() const
{
assert(GetCurrentContext() == this);
EMSCRIPTEN_RESULT succeeded = emscripten_webgl_make_context_current(0);
if (succeeded != EMSCRIPTEN_RESULT_SUCCESS)
NazaraError("failed to desactivate context");
}
const Loader& WebContext::GetLoader()
{
return m_loader;
}
bool WebContext::LoadExt()
{
if (!SetCurrentContext(this))
return false;
char* extensionString = emscripten_webgl_get_supported_extensions();
if (extensionString)
{
SplitString(extensionString, " ", [&](std::string_view extension)
{
m_supportedPlatformExtensions.emplace(extension);
std::string ext(extension);
emscripten_webgl_enable_extension(s_handle, ext.c_str());
return true;
});
}
free(extensionString);
return true;
}
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE WebContext::s_handle = 0;
size_t WebContext::s_handleCounter = 0;
}
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Core/AntiWindows.hpp>
#endif

View File

@@ -0,0 +1,142 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - OpenGL renderer"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/OpenGLRenderer/Wrapper/Web/WebLoader.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Web/WebContext.hpp>
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz::GL
{
struct WebLoader::SymbolLoader
{
SymbolLoader(WebLoader& parent) :
loader(parent)
{
}
template<typename FuncType, typename Func>
bool Load(Func& func, const char* funcName, bool mandatory, bool implementFallback = true)
{
func = LoadRaw<FuncType>(funcName);
if (!func)
{
if (!implementFallback || (!loader.ImplementFallback(funcName) && !func)) //< double-check
{
if (mandatory)
throw std::runtime_error("failed to load core function " + std::string(funcName));
}
}
return func != nullptr;
}
template<typename FuncType>
FuncType LoadRaw(const char* funcName)
{
return reinterpret_cast<FuncType>(loader.LoadFunction(funcName));
}
WebLoader& loader;
};
std::unique_ptr<Context> WebLoader::CreateContext(const OpenGLDevice* device, const ContextParams& params, Context* shareContext) const
{
std::unique_ptr<WebContext> context = std::make_unique<WebContext>(device, *this);
if (!context->Create(params, static_cast<WebContext*>(shareContext)))
{
NazaraError("failed to create context");
return {};
}
if (!context->Initialize(params))
{
NazaraError("failed to initialize context");
return {};
}
return context;
}
std::unique_ptr<Context> WebLoader::CreateContext(const OpenGLDevice* device, const ContextParams& params, WindowHandle handle, Context* shareContext) const
{
std::unique_ptr<WebContext> context;
switch (handle.type)
{
case WindowBackend::Invalid:
break;
case WindowBackend::Web:
context = std::make_unique<WebContext>(device, *this);
break;
}
if (!context)
{
NazaraError("unsupported window type");
return {};
}
if (!context->Create(params, handle, static_cast<WebContext*>(shareContext)))
{
NazaraError("failed to create context");
return {};
}
if (!context->Initialize(params))
{
NazaraError("failed to initialize context");
return {};
}
return context;
}
ContextType WebLoader::GetPreferredContextType() const
{
return ContextType::OpenGL_ES;
}
GLFunction WebLoader::LoadFunction(const char* name) const
{
/*GLFunction func = reinterpret_cast<GLFunction>(m_WebLib.GetSymbol(name));
if (!func && WebGetProcAddress)
func = reinterpret_cast<GLFunction>(WebGetProcAddress(name));
return func;*/
return reinterpret_cast<GLFunction>(emscripten_webgl_get_proc_address(name));
//return nullptr;
}
const char* WebLoader::TranslateError(EMSCRIPTEN_RESULT errorId)
{
switch (errorId)
{
case EMSCRIPTEN_RESULT_SUCCESS: return "The last function succeeded without error.";
case EMSCRIPTEN_RESULT_DEFERRED: return "The requested operation cannot be completed now for web security reasons, and has been deferred for completion in the next event handler.";
case EMSCRIPTEN_RESULT_NOT_SUPPORTED: return "The given operation is not supported by this browser or the target element. This value will be returned at the time the callback is registered if the operation is not supported.";
case EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED: return "The requested operation could not be completed now for web security reasons. It failed because the user requested the operation not be deferred.";
case EMSCRIPTEN_RESULT_INVALID_TARGET: return "The operation failed because the specified target element is invalid.";
case EMSCRIPTEN_RESULT_UNKNOWN_TARGET: return "The operation failed because the specified target element was not found.";
case EMSCRIPTEN_RESULT_INVALID_PARAM: return "The operation failed because an invalid parameter was passed to the function.";
case EMSCRIPTEN_RESULT_FAILED: return "Unknown error (EMSCRIPTEN_RESULT_FAILED).";
case EMSCRIPTEN_RESULT_NO_DATA: return "The operation failed because no data is currently available.";
default: return "Invalid or unknown error.";
}
}
bool WebLoader::ImplementFallback(const std::string_view& /*function*/)
{
return false;
}
}
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Core/AntiWindows.hpp>
#endif