Skip to content

Commit

Permalink
🐛 Various fixes
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
karnkaul committed Oct 26, 2023
1 parent 14a93d7 commit 5f2a722
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion engine/include/le/console/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct Entry {
};

struct Autocomplete {
std::string_view common_suffix{};
std::string common_suffix{};
std::vector<std::string_view> candidates{};
};

Expand Down
7 changes: 4 additions & 3 deletions engine/include/le/graphics/primitive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <le/graphics/defer.hpp>
#include <le/graphics/geometry.hpp>
#include <le/graphics/resource.hpp>
#include <optional>

namespace le::graphics {
class Primitive {
Expand Down Expand Up @@ -34,7 +35,7 @@ class Primitive {
vk::Buffer vertices{};
vk::Buffer indices{};
vk::Buffer bones{};
vk::DeviceSize index_offset{};
std::optional<vk::DeviceSize> index_offset{};
};

auto draw(Buffers const& buffers, std::uint32_t instances, vk::CommandBuffer cmd) const -> void;
Expand All @@ -51,7 +52,7 @@ class StaticPrimitive : public Primitive {
struct Data {
std::unique_ptr<DeviceBuffer> vertices_indices{};
std::unique_ptr<DeviceBuffer> bones{};
vk::DeviceSize index_offset{};
std::optional<vk::DeviceSize> index_offset{};
};

Defer<Data> m_data{};
Expand All @@ -70,6 +71,6 @@ class DynamicPrimitive : public Primitive {

Geometry m_geometry{};
Buffered<std::shared_ptr<HostBuffer>> m_vertices_indices{};
mutable vk::DeviceSize m_index_offset{};
mutable std::optional<vk::DeviceSize> m_index_offset{};
};
} // namespace le::graphics
2 changes: 1 addition & 1 deletion engine/include/le/resources/asset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::byte>;
Expand Down
7 changes: 7 additions & 0 deletions engine/src/core/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include <mutex>
#include <thread>

#if defined(_WIN32)
#include <Windows.h>
#endif

namespace le {
namespace {
namespace fs = std::filesystem;
Expand Down Expand Up @@ -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<File> {
Expand Down
7 changes: 5 additions & 2 deletions engine/src/graphics/primitive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<vk::DeviceSize> {
auto const vertices = std::span{geometry.vertices};
auto const indices = std::span{geometry.indices};
auto const vibo_size = vertices.size_bytes() + indices.size_bytes();
Expand All @@ -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();
}

Expand All @@ -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{});
Expand Down
2 changes: 1 addition & 1 deletion engine/src/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions engine/src/resources/asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5f2a722

Please sign in to comment.