Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
luishfonseca committed Jun 14, 2024
1 parent 9eacfdc commit 7b34b73
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
10 changes: 10 additions & 0 deletions core/include/cubos/core/gl/render_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down
15 changes: 15 additions & 0 deletions core/src/ecs/cubos.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <utility>

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

#include <cubos/core/ecs/command_buffer.hpp>
#include <cubos/core/ecs/cubos.hpp>
#include <cubos/core/ecs/name.hpp>
Expand Down Expand Up @@ -289,9 +293,20 @@ bool Cubos::update()
void Cubos::run()
{
this->start();
#ifndef __EMSCRIPTEN__
while (this->update())
{
}
#else
emscripten_set_main_loop_arg(
[](void* cubos) {
if (static_cast<Cubos*>(cubos)->update() == 0)
{
emscripten_cancel_main_loop();
}
},
this, 0, 1);
#endif
}

bool Cubos::isRegistered(const reflection::Type& type) const
Expand Down
12 changes: 12 additions & 0 deletions core/src/gl/ogl_render_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,12 @@ class OGLIndexBuffer : public impl::IndexBuffer
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
}

void fill(const void* data, std::size_t size) override

Check warning on line 836 in core/src/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/gl/ogl_render_device.cpp#L836

Added line #L836 was not covered by tests
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->id);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, static_cast<GLsizeiptr>(size), data);

Check warning on line 839 in core/src/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/gl/ogl_render_device.cpp#L838-L839

Added lines #L838 - L839 were not covered by tests
}

std::shared_ptr<bool> destroyed;

GLuint id;
Expand Down Expand Up @@ -878,6 +884,12 @@ class OGLVertexBuffer : public impl::VertexBuffer
glUnmapBuffer(GL_ARRAY_BUFFER);
}

void fill(const void* data, std::size_t size) override

Check warning on line 887 in core/src/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/gl/ogl_render_device.cpp#L887

Added line #L887 was not covered by tests
{
glBindBuffer(GL_ARRAY_BUFFER, this->id);
glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast<GLsizeiptr>(size), data);

Check warning on line 890 in core/src/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/gl/ogl_render_device.cpp#L889-L890

Added lines #L889 - L890 were not covered by tests
}

std::shared_ptr<bool> destroyed;

GLuint id;
Expand Down
24 changes: 10 additions & 14 deletions engine/src/imgui/imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));

Check warning on line 429 in engine/src/imgui/imgui.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/imgui/imgui.cpp#L427-L429

Added lines #L427 - L429 were not covered by tests

// Set render state.
setupRenderState(bd, target);
Expand Down Expand Up @@ -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<std::size_t>(cmdList->VtxBuffer.Size) * sizeof(ImDrawVert));
memcpy(idxDst, cmdList->IdxBuffer.Data, static_cast<std::size_t>(cmdList->IdxBuffer.Size) * sizeof(ImDrawIdx));
bd->vb->unmap();
bd->ib->unmap();
bd->vb->fill(cmdList->VtxBuffer.Data, static_cast<std::size_t>(cmdList->VtxBuffer.Size) * sizeof(ImDrawVert));
bd->ib->fill(cmdList->IdxBuffer.Data, static_cast<std::size_t>(cmdList->IdxBuffer.Size) * sizeof(ImDrawIdx));

Check warning on line 484 in engine/src/imgui/imgui.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/imgui/imgui.cpp#L483-L484

Added lines #L483 - L484 were not covered by tests

rd.setVertexArray(bd->va);
rd.setIndexBuffer(bd->ib);
Expand Down

0 comments on commit 7b34b73

Please sign in to comment.