Skip to content

Commit

Permalink
gh-44: rework canvas drawing
Browse files Browse the repository at this point in the history
- impl new canvas class (batch textures, cmds, use new gfx vector)
- use new canvas for debug console rendering
- fix ecs ent deletion
- move visibility cull to sep stage
- move cameras fill to set stage
- store visibility items along with render objects in gpu scene
- remove unnecessary sync in vis system
  • Loading branch information
EgorOrachyov committed Oct 27, 2023
1 parent 9eee434 commit d202487
Show file tree
Hide file tree
Showing 103 changed files with 2,839 additions and 1,416 deletions.
6 changes: 4 additions & 2 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,10 @@ add_library(wmoge STATIC
platform/glfw/glfw_window_manager.hpp
render/aux_draw_manager.cpp
render/aux_draw_manager.hpp
render/aux_draw_canvas.cpp
render/aux_draw_canvas.hpp
render/canvas.cpp
render/canvas.hpp
render/graphics_pipeline.cpp
render/graphics_pipeline.hpp
render/render_camera.cpp
render/render_camera.hpp
render/render_engine.cpp
Expand Down
7 changes: 5 additions & 2 deletions engine/core/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ namespace wmoge {
ShaderManager* Engine::shader_manager() {
return m_shader_manager;
}
TextureManager* Engine::texture_manager() {
return m_texture_manager;
}
AuxDrawManager* Engine::aux_draw_manager() {
return m_aux_draw_manager;
}
Expand All @@ -101,8 +104,8 @@ namespace wmoge {
Console* Engine::console() {
return m_console;
}
AuxDrawCanvas* Engine::canvas_2d_debug() {
return m_canvas_2d_debug;
Canvas* Engine::canvas_debug() {
return m_canvas_debug;
}
ScriptSystem* Engine::script_system() {
return m_script_system;
Expand Down
6 changes: 4 additions & 2 deletions engine/core/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ namespace wmoge {
class GfxDriver* gfx_driver();
class GfxCtx* gfx_ctx();
class ShaderManager* shader_manager();
class TextureManager* texture_manager();
class AuxDrawManager* aux_draw_manager();
class SceneManager* scene_manager();
class ActionManager* action_manager();
class GameTokenManager* game_token_manager();
class Profiler* profiler();
class Console* console();
class AuxDrawCanvas* canvas_2d_debug();
class Canvas* canvas_debug();
class ScriptSystem* script_system();
class AudioEngine* audio_engine();
class RenderEngine* render_engine();
Expand Down Expand Up @@ -112,13 +113,14 @@ namespace wmoge {
class GfxDriver* m_gfx_driver = nullptr;
class GfxCtx* m_gfx_ctx = nullptr;
class ShaderManager* m_shader_manager = nullptr;
class TextureManager* m_texture_manager = nullptr;
class AuxDrawManager* m_aux_draw_manager = nullptr;
class SceneManager* m_scene_manager = nullptr;
class ActionManager* m_action_manager = nullptr;
class GameTokenManager* m_game_token_manager = nullptr;
class Profiler* m_profiler = nullptr;
class Console* m_console = nullptr;
class AuxDrawCanvas* m_canvas_2d_debug = nullptr;
class Canvas* m_canvas_debug = nullptr;
class ScriptSystem* m_script_system = nullptr;
class AudioEngine* m_audio_engine = nullptr;
class RenderEngine* m_render_engine = nullptr;
Expand Down
33 changes: 13 additions & 20 deletions engine/debug/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include "math/math_utils.hpp"
#include "platform/window.hpp"
#include "platform/window_manager.hpp"
#include "render/aux_draw_canvas.hpp"
#include "render/canvas.hpp"
#include "resource/config_file.hpp"
#include "resource/resource_manager.hpp"

Expand Down Expand Up @@ -262,42 +262,35 @@ namespace wmoge {

if (m_state == ConsoleState::Closed) return;

auto screen = m_canvas->get_screen_size();
auto screen = Vec2f(1280.0f, 720.0f);
float height = m_size * m_state_open * screen.y();
float width = screen.x();

m_canvas->push(Vec2f(0.0f, screen.y() - height), 0.0f);
m_canvas->set_font(m_console_font);
m_canvas->push_transform(Vec2f(0.0f, screen.y() - height), 0.0f);

m_canvas->set_fill_color(m_color_back);
m_canvas->draw_filled_rect(Vec2f(0, 0), Vec2f(width, height));

m_canvas->set_fill_color(m_color_line);
m_canvas->draw_filled_rect(Vec2f(0, 0), Vec2f(width, m_line_size));

m_canvas->set_font_color(m_color_text);
m_canvas->draw_text(">", Vec2f(m_margin, m_text_line), m_text_size);
m_canvas->add_rect_filled(Vec2f(0, 0), Vec2f(width, height), m_color_back);
m_canvas->add_rect_filled(Vec2f(0, 0), Vec2f(width, m_line_size), m_color_line);
m_canvas->add_text(">", m_console_font, m_text_size, Vec2f(m_margin, m_text_line), m_color_text);

if (!m_line.empty()) {
m_canvas->set_font_color(m_color_text);
m_canvas->draw_text(m_line, Vec2f(m_margin_line, m_text_line), m_text_size);
m_canvas->add_text(m_line, m_console_font, m_text_size, Vec2f(m_margin_line, m_text_line), m_color_text);
}

if (m_state_blink > m_blink_threshold) {
m_canvas->set_fill_color(m_color_cursor);
m_canvas->draw_filled_rect(Vec2f(m_margin_line + m_cursor_offset, m_margin), Vec2f(m_cursor_width, m_cursor_height));
const auto p_min = Vec2f(m_margin_line + m_cursor_offset, m_margin);
const auto p_max = p_min + Vec2f(m_cursor_width, m_cursor_height);
m_canvas->add_rect_filled(p_min, p_max, m_color_cursor);
}

float text_pos = m_line_size + m_margin;
for (int i = static_cast<int>(m_messages.size()) - m_scroll_messages - 1; i >= 0; --i) {
if (text_pos > height) break;
m_canvas->set_font_color(m_messages[i].color);
m_canvas->draw_text(m_messages[i].text, Vec2f(m_margin, text_pos), m_text_size);
m_canvas->add_text(m_messages[i].text, m_console_font, m_text_size, Vec2f(m_margin, text_pos), m_messages[i].color);
text_pos += m_text_size;
}

m_max_to_display = static_cast<int>((height - m_line_size - m_margin) / m_text_size);
m_canvas->pop();
m_canvas->pop_transform();
}

void Console::register_commands() {
Expand Down Expand Up @@ -409,7 +402,7 @@ namespace wmoge {
auto res_man = engine->resource_manager();
auto config = engine->config();

m_canvas = engine->canvas_2d_debug();
m_canvas = engine->canvas_debug();
m_console_font = res_man->load(SID(config->get_string(SID("debug.console.font"), "res://fonts/anonymous_pro"))).cast<Font>();
m_margin_line = m_margin + m_console_font->get_string_size("> ", m_text_size).x();

Expand Down
18 changes: 9 additions & 9 deletions engine/debug/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,15 @@ namespace wmoge {
float m_margin = 3.0f;
float m_margin_line = 14.0f;

class AuxDrawCanvas* m_canvas = nullptr;
std::string m_line;
ConsoleState m_state = ConsoleState::Closed;
float m_state_open = 0.0f;
float m_state_blink = 0.0f;
float m_current_speed = 0.0f;
float m_cursor_offset = 0.0f;
int m_scroll_messages = 0;
int m_max_to_display = 0;
class Canvas* m_canvas = nullptr;
std::string m_line;
ConsoleState m_state = ConsoleState::Closed;
float m_state_open = 0.0f;
float m_state_blink = 0.0f;
float m_current_speed = 0.0f;
float m_cursor_offset = 0.0f;
int m_scroll_messages = 0;
int m_max_to_display = 0;
};

}// namespace wmoge
Expand Down
17 changes: 7 additions & 10 deletions engine/debug/debug_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "debug/profiler.hpp"
#include "platform/window.hpp"
#include "platform/window_manager.hpp"
#include "render/aux_draw_canvas.hpp"
#include "render/aux_draw_manager.hpp"
#include "render/render_engine.hpp"

Expand All @@ -43,30 +42,28 @@ namespace wmoge {

auto engine = Engine::instance();
auto window = engine->window_manager()->primary_window();
auto canvas_debug = engine->canvas_2d_debug();
auto canvas_debug = engine->canvas_debug();
auto aux_draw_manager = engine->aux_draw_manager();

canvas_debug->set_window(window);
canvas_debug->set_viewport(Rect2i{0, 0, window->fbo_width(), window->fbo_height()});
canvas_debug->set_screen_size(Vec2f(1280, 720));
}
void DebugLayer::on_debug_draw() {
WG_AUTO_PROFILE_DEBUG("DebugLayer::on_debug_draw");

auto engine = Engine::instance();
auto render_engine = engine->render_engine();
auto canvas_debug = engine->canvas_2d_debug();
auto canvas_debug = engine->canvas_debug();
auto aux_draw_manager = engine->aux_draw_manager();
auto console = engine->console();

console->update();

canvas_debug->set_fill_color(Color::BLACK4f);
canvas_debug->draw_filled_rect({0, 0}, {1280, 720});
console->render();

aux_draw_manager->flush(engine->get_delta_time_game());
render_engine->render_aux_geom(*aux_draw_manager);

canvas_debug->compile(true);
render_engine->render_canvas(*canvas_debug, Vec4f(0.0f, 0.0f, 1280.0f, 720.0f));

canvas_debug->clear(false);
}

}// namespace wmoge
4 changes: 3 additions & 1 deletion engine/ecs/ecs_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace wmoge {
out_entity_idx = entity_idx;
m_size += 1;
}
void EcsArchStorage::destroy_entity(const std::uint32_t& in_entity_idx) {
void EcsArchStorage::destroy_entity(const std::uint32_t& in_entity_idx, bool& was_swapped) {
WG_AUTO_PROFILE_ECS("EcsArchStorage::destroy_entity");

assert(int(in_entity_idx) < m_size);
Expand All @@ -120,6 +120,8 @@ namespace wmoge {
m_components_info[component_idx]->swap(component_pool.get_element_raw(entity_idx),
component_pool.get_element_raw(last_entity));
});

was_swapped = true;
}

m_arch.for_each_component([&](int component_idx) {
Expand Down
2 changes: 1 addition & 1 deletion engine/ecs/ecs_memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace wmoge {
~EcsArchStorage();

void make_entity(const EcsEntity& entity, std::uint32_t& entity_idx);
void destroy_entity(const std::uint32_t& entity_idx);
void destroy_entity(const std::uint32_t& entity_idx, bool& was_swapped);
void clear();

template<typename Component>
Expand Down
4 changes: 4 additions & 0 deletions engine/ecs/ecs_system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ namespace wmoge {
const int entity_idx = start_entity + i;
const EcsEntity entity = storage.get_entity(entity_idx);

if (entity.is_invalid()) {
continue;
}

#define WG_ECS_ACCESS_COMPONENT(at) \
using TypeComponent##at = typename std::remove_reference<typename std::tuple_element<at, std::tuple<TArgs...>>::type>::type; \
TypeComponent##at* ptr##at = storage.template get_component<TypeComponent##at>(entity_idx);
Expand Down
11 changes: 9 additions & 2 deletions engine/ecs/ecs_world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,18 @@ namespace wmoge {
assert(entity.is_valid());
assert(entity.idx < m_entity_info.size());

bool need_swap = false;

EcsEntityInfo& entity_info = m_entity_info[entity.idx];
m_arch_storage[entity_info.arch]->destroy_entity(entity_info.storage, need_swap);

m_arch_storage[entity_info.arch]->destroy_entity(entity_info.storage);
m_entity_pool.emplace_back(entity.idx, (entity.gen + 1) % EcsLimits::MAX_GENERATIONS_PER_ARC);
if (need_swap) {
const EcsEntity entity_to_swap = m_arch_storage[entity_info.arch]->get_entity(entity_info.storage);
assert(m_entity_info[entity_to_swap.idx].arch == entity_info.arch);
m_entity_info[entity_to_swap.idx].storage = entity_info.storage;
}

m_entity_pool.emplace_back(entity.idx, (entity.gen + 1) % EcsLimits::MAX_GENERATIONS_PER_ARC);
entity_info = EcsEntityInfo{};
}

Expand Down
3 changes: 2 additions & 1 deletion engine/ecs/ecs_world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "core/callback_queue.hpp"
#include "core/fast_map.hpp"
#include "core/synchronization.hpp"
#include "core/task_manager.hpp"
#include "ecs/ecs_component.hpp"
#include "ecs/ecs_core.hpp"
Expand Down Expand Up @@ -128,7 +129,7 @@ namespace wmoge {
CallbackQueue m_queue; // queue for async world operations, flushed on sync
TaskManager* m_task_manager;// manager for parallel system update

mutable std::mutex m_mutex;
SpinMutex m_mutex;
};

template<class Component>
Expand Down
2 changes: 1 addition & 1 deletion engine/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@
#include "platform/window.hpp"
#include "platform/window_manager.hpp"

#include "render/aux_draw_canvas.hpp"
#include "render/aux_draw_manager.hpp"
#include "render/canvas.hpp"
#include "render/render_camera.hpp"
#include "render/render_engine.hpp"
#include "render/render_mesh_skinned.hpp"
Expand Down
4 changes: 2 additions & 2 deletions engine/gfx/gfx_ctx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ namespace wmoge {
virtual void draw_indexed(int index_count, int base_vertex, int instance_count) = 0;
virtual void end_render_pass() = 0;

virtual void execute(const std::function<void()>& functor) = 0;
virtual void shutdown() = 0;
virtual void execute(const std::function<void(GfxCtx* thread_ctx)>& functor) = 0;
virtual void shutdown() = 0;

virtual void begin_frame() = 0;
virtual void end_frame() = 0;
Expand Down
6 changes: 6 additions & 0 deletions engine/gfx/gfx_defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ namespace wmoge {
NegativeZ = 5,
};

/** @brief Tex view channels swizzling */
enum class GfxTexSwizz : int {
None = 0,
RRRRtoRGBA = 1
};

/** @brief Formats used to specify internal storage format */
enum class GfxFormat : int {
Unknown,
Expand Down
Loading

0 comments on commit d202487

Please sign in to comment.