diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cdd48eb32..121f314b1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -115,13 +115,14 @@ function(cubos_common_target_options target) endif() if (EMSCRIPTEN) - set(LFLAGS "-sSTRICT -sFILESYSTEM=0 -sASSERTIONS=0 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2") + set(LFLAGS "-sSTRICT -sFILESYSTEM=0 -sASSERTIONS=0 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -pthread -sPTHREAD_POOL_SIZE=navigator.hardwareConcurrency") if (WITH_GLFW) set(LFLAGS "${LFLAGS} -sUSE_GLFW=3") endif () set_target_properties(${target} PROPERTIES SUFFIX ".html" LINK_FLAGS ${LFLAGS}) + target_compile_options(${target} PUBLIC -pthread) endif () endfunction() diff --git a/core/include/cubos/core/gl/render_device.hpp b/core/include/cubos/core/gl/render_device.hpp index 472babf4b3..7cb977017e 100644 --- a/core/include/cubos/core/gl/render_device.hpp +++ b/core/include/cubos/core/gl/render_device.hpp @@ -1103,6 +1103,11 @@ namespace cubos::core::gl /// @brief Unmaps the index buffer, updating it with data written to the mapped region. virtual void unmap() = 0; + /// @brief Fills the buffer with the given data. + /// @param data Pointer to data. + /// @param size Data size in bytes. + virtual void fill(const void* data, std::size_t size) = 0; + protected: IndexBuffer() = default; }; @@ -1131,6 +1136,11 @@ namespace cubos::core::gl /// region. virtual void unmap() = 0; + // @brief Fills the buffer with the given data. + /// @param data Pointer to data. + /// @param size Data size in bytes. + virtual void fill(const void* data, std::size_t size) = 0; + protected: VertexBuffer() = default; }; diff --git a/core/src/gl/ogl_render_device.cpp b/core/src/gl/ogl_render_device.cpp index 5595725f6f..65a43615b2 100644 --- a/core/src/gl/ogl_render_device.cpp +++ b/core/src/gl/ogl_render_device.cpp @@ -833,6 +833,12 @@ class OGLIndexBuffer : public impl::IndexBuffer glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); } + void fill(const void* data, std::size_t size) override + { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->id); + glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, static_cast(size), data); + } + std::shared_ptr destroyed; GLuint id; @@ -878,6 +884,12 @@ class OGLVertexBuffer : public impl::VertexBuffer glUnmapBuffer(GL_ARRAY_BUFFER); } + void fill(const void* data, std::size_t size) override + { + glBindBuffer(GL_ARRAY_BUFFER, this->id); + glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast(size), data); + } + std::shared_ptr destroyed; GLuint id; diff --git a/engine/src/imgui/imgui.cpp b/engine/src/imgui/imgui.cpp index a1135e13a1..5a7175eae6 100644 --- a/engine/src/imgui/imgui.cpp +++ b/engine/src/imgui/imgui.cpp @@ -203,8 +203,6 @@ static void createDeviceObjects() // Setup shader pipeline. auto vs = rd.createShaderStage(gl::Stage::Vertex, R"glsl( -#version 330 core - layout (location = 0) in vec2 position; layout (location = 1) in vec2 uv; layout (location = 2) in vec4 color; @@ -225,8 +223,6 @@ void main() )glsl"); auto ps = rd.createShaderStage(gl::Stage::Pixel, R"glsl( -#version 330 core - uniform sampler2D tex; in vec2 fragUv; @@ -289,6 +285,11 @@ void cubos::engine::imguiInitialize(io::Window window, float dpiScale) return; } +// TODO: Fix this +#ifdef __EMSCRIPTEN__ + io.IniFilename = nullptr; +#endif + // Setup back-end capabilities flags auto* bd = IM_NEW(ImGuiData)(); bd->window = window; @@ -423,10 +424,9 @@ void cubos::engine::imguiEndFrame(const gl::Framebuffer& target) auto* drawData = ImGui::GetDrawData(); // Upload projection matrix to constant buffer. - glm::mat4& proj = *(glm::mat4*)bd->cb->map(); - proj = glm::ortho(drawData->DisplayPos.x, drawData->DisplayPos.x + drawData->DisplaySize.x, - drawData->DisplayPos.y + drawData->DisplaySize.y, drawData->DisplayPos.y); - bd->cb->unmap(); + auto proj = glm::ortho(drawData->DisplayPos.x, drawData->DisplayPos.x + drawData->DisplaySize.x, + drawData->DisplayPos.y + drawData->DisplaySize.y, drawData->DisplayPos.y); + bd->cb->fill(&proj, sizeof(proj)); // Set render state. setupRenderState(bd, target); @@ -480,12 +480,8 @@ void cubos::engine::imguiEndFrame(const gl::Framebuffer& target) } // Upload vertex and index data into vertex buffer. - auto* vtxDst = (ImDrawVert*)bd->vb->map(); - auto* idxDst = (ImDrawIdx*)bd->ib->map(); - memcpy(vtxDst, cmdList->VtxBuffer.Data, static_cast(cmdList->VtxBuffer.Size) * sizeof(ImDrawVert)); - memcpy(idxDst, cmdList->IdxBuffer.Data, static_cast(cmdList->IdxBuffer.Size) * sizeof(ImDrawIdx)); - bd->vb->unmap(); - bd->ib->unmap(); + bd->vb->fill(cmdList->VtxBuffer.Data, static_cast(cmdList->VtxBuffer.Size) * sizeof(ImDrawVert)); + bd->ib->fill(cmdList->IdxBuffer.Data, static_cast(cmdList->IdxBuffer.Size) * sizeof(ImDrawIdx)); rd.setVertexArray(bd->va); rd.setIndexBuffer(bd->ib);