From 5f2a7220a4f6145f23537f8cee91e2605d59121e Mon Sep 17 00:00:00 2001 From: Karn Kaul Date: Thu, 26 Oct 2023 09:34:04 +0530 Subject: [PATCH] :bug: Various fixes - Fix dangling string views by using strings. - Fix index buffers: don't bind if geometry has no indices. - Fix non-constexpr `ui_camera_v`. - Also log to VS console. --- engine/include/le/console/console.hpp | 2 +- engine/include/le/graphics/primitive.hpp | 7 ++++--- engine/include/le/resources/asset.hpp | 2 +- engine/src/core/logger.cpp | 7 +++++++ engine/src/graphics/primitive.cpp | 7 +++++-- engine/src/graphics/renderer.cpp | 2 +- engine/src/resources/asset.cpp | 4 ++-- 7 files changed, 21 insertions(+), 10 deletions(-) diff --git a/engine/include/le/console/console.hpp b/engine/include/le/console/console.hpp index 70771b08..829487b0 100644 --- a/engine/include/le/console/console.hpp +++ b/engine/include/le/console/console.hpp @@ -12,7 +12,7 @@ struct Entry { }; struct Autocomplete { - std::string_view common_suffix{}; + std::string common_suffix{}; std::vector candidates{}; }; diff --git a/engine/include/le/graphics/primitive.hpp b/engine/include/le/graphics/primitive.hpp index 6dd7e632..b3684641 100644 --- a/engine/include/le/graphics/primitive.hpp +++ b/engine/include/le/graphics/primitive.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace le::graphics { class Primitive { @@ -34,7 +35,7 @@ class Primitive { vk::Buffer vertices{}; vk::Buffer indices{}; vk::Buffer bones{}; - vk::DeviceSize index_offset{}; + std::optional index_offset{}; }; auto draw(Buffers const& buffers, std::uint32_t instances, vk::CommandBuffer cmd) const -> void; @@ -51,7 +52,7 @@ class StaticPrimitive : public Primitive { struct Data { std::unique_ptr vertices_indices{}; std::unique_ptr bones{}; - vk::DeviceSize index_offset{}; + std::optional index_offset{}; }; Defer m_data{}; @@ -70,6 +71,6 @@ class DynamicPrimitive : public Primitive { Geometry m_geometry{}; Buffered> m_vertices_indices{}; - mutable vk::DeviceSize m_index_offset{}; + mutable std::optional m_index_offset{}; }; } // namespace le::graphics diff --git a/engine/include/le/resources/asset.hpp b/engine/include/le/resources/asset.hpp index 8b7fc95f..4ec10b79 100644 --- a/engine/include/le/resources/asset.hpp +++ b/engine/include/le/resources/asset.hpp @@ -12,7 +12,7 @@ class Asset : public NamedType { [[nodiscard]] virtual auto try_load(Uri const& uri) -> bool = 0; [[nodiscard]] static auto get_asset_type(dj::Json const& json) -> std::string_view { return json["asset_type"].as_string(); } - [[nodiscard]] static auto get_asset_type(Uri const& uri) -> std::string_view; + [[nodiscard]] static auto get_asset_type(Uri const& uri) -> std::string; protected: [[nodiscard]] auto read_bytes(Uri const& uri) const -> std::vector; diff --git a/engine/src/core/logger.cpp b/engine/src/core/logger.cpp index bf71e24f..d07e0277 100644 --- a/engine/src/core/logger.cpp +++ b/engine/src/core/logger.cpp @@ -10,6 +10,10 @@ #include #include +#if defined(_WIN32) +#include +#endif + namespace le { namespace { namespace fs = std::filesystem; @@ -88,6 +92,9 @@ auto logger::print(std::string_view const domain, std::string_view const message if (auto file = g_file.lock()) { file->push(line); } auto& stream = level == error_v ? std::cerr : std::cout; stream << line; +#if defined(_WIN32) + OutputDebugStringA(line.c_str()); +#endif } auto logger::log_to_file(std::string path) -> std::shared_ptr { diff --git a/engine/src/graphics/primitive.cpp b/engine/src/graphics/primitive.cpp index 94abcbe8..315053f0 100644 --- a/engine/src/graphics/primitive.cpp +++ b/engine/src/graphics/primitive.cpp @@ -5,7 +5,8 @@ namespace le::graphics { namespace { -auto write_vertices_indices(Buffer& out, Geometry const& geometry) -> vk::DeviceSize { +// returns index offset if geometry has indices, else nullopt +auto write_vertices_indices(Buffer& out, Geometry const& geometry) -> std::optional { auto const vertices = std::span{geometry.vertices}; auto const indices = std::span{geometry.indices}; auto const vibo_size = vertices.size_bytes() + indices.size_bytes(); @@ -19,6 +20,8 @@ auto write_vertices_indices(Buffer& out, Geometry const& geometry) -> vk::Device out.write(bytes.data(), bytes.size()); + if (geometry.indices.empty()) { return std::nullopt; } + return vertices.size_bytes(); } @@ -40,7 +43,7 @@ auto Primitive::draw(Buffers const& buffers, std::uint32_t const instances, vk:: assert(buffers.vertices); cmd.bindVertexBuffers(bindings.vertex.buffer, buffers.vertices, vk::DeviceSize{}); - if (buffers.indices != nullptr) { cmd.bindIndexBuffer(buffers.indices, buffers.index_offset, vk::IndexType::eUint32); } + if (buffers.indices != nullptr && buffers.index_offset) { cmd.bindIndexBuffer(buffers.indices, *buffers.index_offset, vk::IndexType::eUint32); } if (buffers.bones != nullptr) { cmd.bindVertexBuffers(bindings.skeleton.buffer, buffers.bones, vk::DeviceSize{}); diff --git a/engine/src/graphics/renderer.cpp b/engine/src/graphics/renderer.cpp index 7422855b..41d9abe5 100644 --- a/engine/src/graphics/renderer.cpp +++ b/engine/src/graphics/renderer.cpp @@ -294,7 +294,7 @@ auto Renderer::wait_for_frame(glm::uvec2 const framebuffer_extent) -> std::optio } auto Renderer::render(RenderFrame const& render_frame, std::uint32_t const image_index) -> std::uint32_t { - static constexpr auto ui_camera_v{Camera{.type = Camera::Orthographic{}}}; + static auto const ui_camera_v{Camera{.type = Camera::Orthographic{}}}; auto const shadow_view_plane = ViewPlane{.near = -0.5f * shadow_frustum.z, .far = 0.5f * shadow_frustum.z}; auto shadow_camera = Camera{.type = Camera::Orthographic{.view_plane = shadow_view_plane}, .face = Camera::Face::ePositiveZ}; diff --git a/engine/src/resources/asset.cpp b/engine/src/resources/asset.cpp index 5e5fbc0e..b81ba6a5 100644 --- a/engine/src/resources/asset.cpp +++ b/engine/src/resources/asset.cpp @@ -7,9 +7,9 @@ namespace { auto const g_log{logger::Logger{"Asset"}}; } -auto Asset::get_asset_type(Uri const& uri) -> std::string_view { +auto Asset::get_asset_type(Uri const& uri) -> std::string { auto const& json = dj::Json::parse(vfs::read_string(uri)); - return get_asset_type(json); + return std::string{get_asset_type(json)}; } // NOLINTNEXTLINE