Skip to content

Commit

Permalink
gh-54: cache gfx framebuffers & cache allocated desc sets for speed
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorOrachyov committed Oct 29, 2023
1 parent 3722735 commit b8ca975
Show file tree
Hide file tree
Showing 21 changed files with 279 additions and 169 deletions.
22 changes: 22 additions & 0 deletions engine/gfx/gfx_ctx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ namespace wmoge {
public:
virtual ~GfxCtx() = default;

virtual void update_desc_set(const Ref<GfxDescSet>& set, const GfxDescSetResources& resources) = 0;

virtual void update_vert_buffer(const Ref<GfxVertBuffer>& buffer, int offset, int range, const Ref<Data>& data) = 0;
virtual void update_index_buffer(const Ref<GfxIndexBuffer>& buffer, int offset, int range, const Ref<Data>& data) = 0;
virtual void update_uniform_buffer(const Ref<GfxUniformBuffer>& buffer, int offset, int range, const Ref<Data>& data) = 0;
Expand Down Expand Up @@ -101,10 +103,30 @@ namespace wmoge {
virtual void begin_frame() = 0;
virtual void end_frame() = 0;

virtual void begin_label(const StringId& label) = 0;
virtual void end_label() = 0;

[[nodiscard]] virtual const Mat4x4f& clip_matrix() const = 0;
[[nodiscard]] virtual GfxCtxType ctx_type() const = 0;
};

/**
* @class GfxDebugLabel
* @brief Scope for debug laber
*/
struct GfxDebugLabel {
GfxDebugLabel(GfxCtx* ctx, const StringId& label) : ctx(ctx) { ctx->begin_label(label); }
~GfxDebugLabel() { ctx->end_label(); }
GfxCtx* ctx;
};

#ifndef WMOGE_RELEASE
#define WG_GFX_LABEL(ctx, label) \
GfxDebugLabel __label_guard(ctx, label);
#else
#define WG_GFX_LABEL(ctx, label)
#endif

}// namespace wmoge

#endif//WMOGE_GFX_CTX_H
75 changes: 11 additions & 64 deletions engine/gfx/threaded/gfx_ctx_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,166 +44,110 @@ namespace wmoge {
m_ctx_type = ctx->ctx_type();
}

void GfxCtxWrapper::update_vert_buffer(const Ref<GfxVertBuffer>& buffer, int offset, int range, const Ref<Data>& data) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::update_vert_buffer");
void GfxCtxWrapper::update_desc_set(const Ref<GfxDescSet>& set, const GfxDescSetResources& resources) {
m_stream->push([=]() { m_ctx->update_desc_set(set, resources); });
}

void GfxCtxWrapper::update_vert_buffer(const Ref<GfxVertBuffer>& buffer, int offset, int range, const Ref<Data>& data) {
m_stream->push([=]() { m_ctx->update_vert_buffer(buffer, offset, range, data); });
}
void GfxCtxWrapper::update_index_buffer(const Ref<GfxIndexBuffer>& buffer, int offset, int range, const Ref<Data>& data) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::update_index_buffer");

m_stream->push([=]() { m_ctx->update_index_buffer(buffer, offset, range, data); });
}
void GfxCtxWrapper::update_uniform_buffer(const Ref<GfxUniformBuffer>& buffer, int offset, int range, const Ref<Data>& data) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::update_uniform_buffer");

m_stream->push([=]() { m_ctx->update_uniform_buffer(buffer, offset, range, data); });
}
void GfxCtxWrapper::update_storage_buffer(const Ref<GfxStorageBuffer>& buffer, int offset, int range, const Ref<Data>& data) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::update_storage_buffer");

m_stream->push([=]() { m_ctx->update_storage_buffer(buffer, offset, range, data); });
}
void GfxCtxWrapper::update_texture_2d(const Ref<GfxTexture>& texture, int mip, Rect2i region, const Ref<Data>& data) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::update_texture_2d");

m_stream->push([=]() { m_ctx->update_texture_2d(texture, mip, region, data); });
}
void GfxCtxWrapper::update_texture_2d_array(const Ref<GfxTexture>& texture, int mip, int slice, Rect2i region, const Ref<Data>& data) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::update_texture_2d_array");

m_stream->push([=]() { m_ctx->update_texture_2d_array(texture, mip, slice, region, data); });
}
void GfxCtxWrapper::update_texture_cube(const Ref<GfxTexture>& texture, int mip, int face, Rect2i region, const Ref<Data>& data) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::update_texture_cube");

m_stream->push([=]() { m_ctx->update_texture_cube(texture, mip, face, region, data); });
}

void* GfxCtxWrapper::map_vert_buffer(const Ref<GfxVertBuffer>& buffer) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::map_vert_buffer");

void* mapped_ptr = nullptr;
m_stream->push_and_wait([&]() { mapped_ptr = m_ctx->map_vert_buffer(buffer); });
return mapped_ptr;
}
void* GfxCtxWrapper::map_index_buffer(const Ref<GfxIndexBuffer>& buffer) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::map_index_buffer");

void* mapped_ptr = nullptr;
m_stream->push_and_wait([&]() { mapped_ptr = m_ctx->map_index_buffer(buffer); });
return mapped_ptr;
}
void* GfxCtxWrapper::map_uniform_buffer(const Ref<GfxUniformBuffer>& buffer) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::map_uniform_buffer");

void* mapped_ptr = nullptr;
m_stream->push_and_wait([&]() { mapped_ptr = m_ctx->map_uniform_buffer(buffer); });
return mapped_ptr;
}
void* GfxCtxWrapper::map_storage_buffer(const Ref<GfxStorageBuffer>& buffer) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::map_storage_buffer");

void* mapped_ptr = nullptr;
m_stream->push_and_wait([&]() { mapped_ptr = m_ctx->map_storage_buffer(buffer); });
return mapped_ptr;
}
void GfxCtxWrapper::unmap_vert_buffer(const Ref<GfxVertBuffer>& buffer) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::unmap_vert_buffer");

m_stream->push([=]() { m_ctx->unmap_vert_buffer(buffer); });
}
void GfxCtxWrapper::unmap_index_buffer(const Ref<GfxIndexBuffer>& buffer) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::unmap_index_buffer");

m_stream->push([=]() { m_ctx->unmap_index_buffer(buffer); });
}
void GfxCtxWrapper::unmap_uniform_buffer(const Ref<GfxUniformBuffer>& buffer) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::unmap_uniform_buffer");

m_stream->push([=]() { m_ctx->unmap_uniform_buffer(buffer); });
}
void GfxCtxWrapper::unmap_storage_buffer(const Ref<GfxStorageBuffer>& buffer) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::unmap_storage_buffer");

m_stream->push([=]() { m_ctx->unmap_storage_buffer(buffer); });
}

void GfxCtxWrapper::begin_render_pass(const GfxRenderPassDesc& pass_desc, const StringId& name) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::begin_render_pass");

m_stream->push([=]() { m_ctx->begin_render_pass(pass_desc, name); });
}
void GfxCtxWrapper::bind_target(const Ref<Window>& window) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::bind_target");

m_stream->push([=]() { m_ctx->bind_target(window); });
}
void GfxCtxWrapper::bind_color_target(const Ref<GfxTexture>& texture, int target, int mip, int slice) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::bind_color_target");

m_stream->push([=]() { m_ctx->bind_color_target(texture, target, mip, slice); });
}
void GfxCtxWrapper::bind_depth_target(const Ref<GfxTexture>& texture, int mip, int slice) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::bind_depth_target");

m_stream->push([=]() { m_ctx->bind_depth_target(texture, mip, slice); });
}
void GfxCtxWrapper::viewport(const Rect2i& viewport) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::viewport");

m_stream->push([=]() { m_ctx->viewport(viewport); });
}
void GfxCtxWrapper::clear(int target, const Vec4f& color) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::clear");

m_stream->push([=]() { m_ctx->clear(target, color); });
}
void GfxCtxWrapper::clear(float depth, int stencil) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::clear");

m_stream->push([=]() { m_ctx->clear(depth, stencil); });
}
bool GfxCtxWrapper::bind_pipeline(const Ref<GfxPipeline>& pipeline) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::bind_pipeline");

bool is_bound = false;
m_stream->push_and_wait([&]() { is_bound = m_ctx->bind_pipeline(pipeline); });
return is_bound;
}
void GfxCtxWrapper::bind_vert_buffer(const Ref<GfxVertBuffer>& buffer, int index, int offset) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::bind_vert_buffer");

m_stream->push([=]() { m_ctx->bind_vert_buffer(buffer, index, offset); });
}
void GfxCtxWrapper::bind_index_buffer(const Ref<GfxIndexBuffer>& buffer, GfxIndexType index_type, int offset) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::bind_index_buffer");

m_stream->push([=]() { m_ctx->bind_index_buffer(buffer, index_type, offset); });
}
void GfxCtxWrapper::bind_desc_set(const Ref<GfxDescSet>& set, int index) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::bind_desc_set");

m_stream->push([=]() { m_ctx->bind_desc_set(set, index); });
}
void GfxCtxWrapper::bind_desc_sets(const ArrayView<GfxDescSet*>& sets, int offset) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::bind_desc_sets");

m_stream->push([=]() { m_ctx->bind_desc_sets(sets, offset); });
}
void GfxCtxWrapper::draw(int vertex_count, int base_vertex, int instance_count) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::draw");

m_stream->push([=]() { m_ctx->draw(vertex_count, base_vertex, instance_count); });
}
void GfxCtxWrapper::draw_indexed(int index_count, int base_vertex, int instance_count) {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::draw_indexed");

m_stream->push([=]() { m_ctx->draw_indexed(index_count, base_vertex, instance_count); });
}
void GfxCtxWrapper::end_render_pass() {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::end_render_pass");

m_stream->push([=]() { m_ctx->end_render_pass(); });
}

Expand All @@ -219,16 +163,19 @@ namespace wmoge {
}

void GfxCtxWrapper::begin_frame() {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::begin_frame");

m_stream->push([=]() { m_ctx->begin_frame(); });
}
void GfxCtxWrapper::end_frame() {
WG_AUTO_PROFILE_GFX("GfxCtxWrapper::end_frame");

m_stream->push([=]() { m_ctx->end_frame(); });
}

void GfxCtxWrapper::begin_label(const StringId& label) {
m_stream->push([=]() { m_ctx->begin_label(label); });
}
void GfxCtxWrapper::end_label() {
m_stream->push([=]() { m_ctx->end_label(); });
}

const Mat4x4f& GfxCtxWrapper::clip_matrix() const {
return m_clip_matrix;
}
Expand Down
5 changes: 5 additions & 0 deletions engine/gfx/threaded/gfx_ctx_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ namespace wmoge {

~GfxCtxWrapper() override = default;

void update_desc_set(const Ref<GfxDescSet>& set, const GfxDescSetResources& resources) override;

void update_vert_buffer(const Ref<GfxVertBuffer>& buffer, int offset, int range, const Ref<Data>& data) override;
void update_index_buffer(const Ref<GfxIndexBuffer>& buffer, int offset, int range, const Ref<Data>& data) override;
void update_uniform_buffer(const Ref<GfxUniformBuffer>& buffer, int offset, int range, const Ref<Data>& data) override;
Expand Down Expand Up @@ -92,6 +94,9 @@ namespace wmoge {
void begin_frame() override;
void end_frame() override;

void begin_label(const StringId& label) override;
void end_label() override;

[[nodiscard]] const Mat4x4f& clip_matrix() const override;
[[nodiscard]] GfxCtxType ctx_type() const override;

Expand Down
2 changes: 0 additions & 2 deletions engine/gfx/threaded/gfx_driver_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ namespace wmoge {
}

Ref<GfxVertFormat> GfxDriverWrapper::make_vert_format(const GfxVertElements& elements, const StringId& name) {
WG_AUTO_PROFILE_GFX("GfxDriverWrapper::make_vert_format");

auto cached = m_vert_fmt_cache->get(elements);
if (cached.has_value()) {
return cached.value();
Expand Down
28 changes: 26 additions & 2 deletions engine/gfx/vulkan/vk_ctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ namespace wmoge {
WG_LOG_INFO("shutdown vulkan gfx context");
}

void VKCtx::update_desc_set(const Ref<GfxDescSet>& set, const GfxDescSetResources& resources) {
WG_AUTO_PROFILE_VULKAN("VKCtx::update_desc_set");

assert(check_thread_valid());
assert(set);
assert(!m_in_render_pass);

auto vk_desc_set = dynamic_cast<VKDescSet*>(set.get());

vk_desc_set->update(resources);
vk_desc_set->merge(resources);
}

void VKCtx::update_vert_buffer(const Ref<GfxVertBuffer>& buffer, int offset, int range, const Ref<Data>& data) {
WG_AUTO_PROFILE_VULKAN("VKCtx::update_vert_buffer");

Expand Down Expand Up @@ -372,8 +385,8 @@ namespace wmoge {

assert(m_render_pass_started);

WG_VK_END_LABEL(cmd_current());
vkCmdEndRenderPass(cmd_current());
WG_VK_END_LABEL(cmd_current());

m_render_pass_binder->finish(cmd_current());
m_current_pass.reset();
Expand Down Expand Up @@ -414,6 +427,17 @@ namespace wmoge {
m_cmd_manager->update();
}

void VKCtx::begin_label(const StringId& label) {
assert(!m_in_render_pass);

WG_VK_BEGIN_LABEL(cmd_current(), label);
}
void VKCtx::end_label() {
assert(!m_in_render_pass);

WG_VK_END_LABEL(cmd_current());
}

const Mat4x4f& VKCtx::clip_matrix() const {
return m_clip_matrix;
}
Expand Down Expand Up @@ -467,8 +491,8 @@ namespace wmoge {
render_pass_info.clearValueCount = clear_value_count;
render_pass_info.pClearValues = clear_values.data();

vkCmdBeginRenderPass(cmd_current(), &render_pass_info, VK_SUBPASS_CONTENTS_INLINE);
WG_VK_BEGIN_LABEL(cmd_current(), m_render_pass_name);
vkCmdBeginRenderPass(cmd_current(), &render_pass_info, VK_SUBPASS_CONTENTS_INLINE);

VkViewport viewport;
viewport.x = static_cast<float>(m_viewport.x());
Expand Down
5 changes: 5 additions & 0 deletions engine/gfx/vulkan/vk_ctx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ namespace wmoge {
explicit VKCtx(class VKDriver& driver);
~VKCtx() override;

void update_desc_set(const Ref<GfxDescSet>& set, const GfxDescSetResources& resources) override;

void update_vert_buffer(const Ref<GfxVertBuffer>& buffer, int offset, int range, const Ref<Data>& data) override;
void update_index_buffer(const Ref<GfxIndexBuffer>& buffer, int offset, int range, const Ref<Data>& data) override;
void update_uniform_buffer(const Ref<GfxUniformBuffer>& buffer, int offset, int range, const Ref<Data>& data) override;
Expand Down Expand Up @@ -100,6 +102,9 @@ namespace wmoge {
void begin_frame() override;
void end_frame() override;

void begin_label(const StringId& label) override;
void end_label() override;

[[nodiscard]] const Mat4x4f& clip_matrix() const override;
[[nodiscard]] GfxCtxType ctx_type() const override;
[[nodiscard]] CallbackStream* cmd_stream() override;
Expand Down
1 change: 1 addition & 0 deletions engine/gfx/vulkan/vk_defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#endif
#endif

#include <cinttypes>
#include <functional>
#include <string>
#include <vector>
Expand Down
Loading

0 comments on commit b8ca975

Please sign in to comment.