Skip to content

Commit

Permalink
Upgrade ktl
Browse files Browse the repository at this point in the history
  • Loading branch information
karnkaul committed Jun 18, 2022
1 parent edd9401 commit 509fd4c
Show file tree
Hide file tree
Showing 23 changed files with 100 additions and 67 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ set(FETCHCONTENT_QUIET OFF)
FetchContent_Declare(
ktl
GIT_REPOSITORY https://github.com/karnkaul/ktl
GIT_TAG a952dbc
GIT_TAG 0f9370e
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libs/ktl"
)
FetchContent_MakeAvailable(ktl)
Expand Down
11 changes: 4 additions & 7 deletions demo/lib/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include <levk/gameplay/gui/widgets/dropdown.hpp>

#include <ktl/async/kasync.hpp>
#include <levk/core/kassert/assert_instance.hpp>
#include <levk/core/utils/shell.hpp>
#include <levk/core/utils/tween.hpp>
Expand Down Expand Up @@ -772,17 +771,15 @@ bool run(io::Media const& media) {
scenes.attach<App>("app", engine.service());
scenes.open("app");
DeltaTime dt;
ktl::kfuture<void> bf;
ktl::kasync async;
while (!engine.service().closing()) {
engine.service().poll(scenes.sceneView(), &poll);
if (flags.any(Flags(Flag::eQuit, Flag::eReboot))) { break; }
scenes.tick(++dt);
scenes.render(RGBA(0x777777ff, RGBA::Type::eAbsolute));
if (flags.test(Flag::eDebug0) && (!bf.valid() || !bf.busy())) {
// app.sched().enqueue([]() { KASSERT(false, "test"); });
// app.sched().enqueue([]() { KASSERT(false, "test2"); });
auto& ctx = engine.service().context();
// app.sched().enqueue([]() { KASSERT(false, "test"); });
// app.sched().enqueue([]() { KASSERT(false, "test2"); });
auto& ctx = engine.service().context();
if (flags.test(Flag::eDebug0)) {
if (auto img = graphics::utils::makeStorage(&ctx.vram(), ctx.lastDrawn().ref())) {
if (auto file = std::ofstream("shot.ppm", std::ios::out | std::ios::binary)) {
auto const written = graphics::utils::writePPM(ctx.vram().m_device, *img, file);
Expand Down
2 changes: 1 addition & 1 deletion ext/freetype
Submodule freetype updated 137 files
39 changes: 39 additions & 0 deletions levk/core/include/levk/core/stack_string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once
#include <fmt/format.h>
#include <cassert>

namespace le {
template <std::size_t Capacity>
struct StackString {
static constexpr auto capacity_v = Capacity;

char buf[Capacity + 1]{};
std::size_t size{};

StackString() = default;

template <std::size_t N>
constexpr StackString(char const (&str)[N]) {
static_assert(N < Capacity);
for (std::size_t i = 0; i < N; ++i) { buf[i] = str[i]; }
size = N;
}

template <typename... Args>
constexpr StackString(std::string_view const fmt, Args const&... args) {
size = fmt::format_to(buf, fmt::runtime(fmt), args...) - buf;
assert(size < Capacity);
}

constexpr std::string_view get() const { return buf; }
constexpr operator std::string_view() const { return get(); }
constexpr char const* data() const { return buf; }

template <std::size_t N>
constexpr StackString<Capacity>& operator+=(StackString<N> const& rhs) {
size = fmt::format_to(buf + size, "{}", rhs.get()) - buf;
assert(size < Capacity);
return *this;
}
};
} // namespace le
2 changes: 1 addition & 1 deletion levk/gameplay/include/levk/gameplay/editor/inspector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ bool Inspector::TGadget<T>::inspect(std::string_view id, dens::entity entity, de
} else {
if (auto t = registry.find<T>(entity)) {
auto tn = TreeNode(id);
auto const detach = ktl::stack_string<64>("x##{}", id);
Styler s(getWindowWidth() - 30.0f);
auto const detach = StackString<64>("x##{}", id);
if (Button(detach, 0.0f, true)) { registry.detach<T>(entity); }
if (tn) { inspect_(Inspect<T>{*t, registry, entity, store}); }
s = Styler(Style::eSeparator);
Expand Down
13 changes: 5 additions & 8 deletions levk/gameplay/include/levk/gameplay/editor/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <ktl/async/kfunction.hpp>
#include <ktl/enum_flags/enum_flags.hpp>
#include <ktl/n_tree.hpp>
#include <ktl/stack_string.hpp>
#include <levk/core/colour.hpp>
#include <levk/core/span.hpp>
#include <levk/core/stack_string.hpp>
#include <levk/core/utils/string.hpp>
#include <levk/gameplay/editor/scene_ref.hpp>
#include <levk/gameplay/scene/scene_node.hpp>
Expand Down Expand Up @@ -36,14 +36,11 @@ using StyleFlags = ktl::enum_flags<Style, u8>;

enum class WType { eInput, eDrag };

template <std::size_t N = 64>
using CStr = ktl::stack_string<N>;

f32 getWindowWidth();

struct MenuList {
struct Menu {
CStr<64> id;
StackString<64> id{};
ktl::kfunction<void()> callback;
bool separator = false;
};
Expand Down Expand Up @@ -207,7 +204,7 @@ struct TWidgetWrap {

template <typename T>
struct TInspector {
CStr<128> id;
StackString<64> id{};
std::optional<TreeNode> node;
dens::registry* pReg = nullptr;
dens::entity entity;
Expand Down Expand Up @@ -345,7 +342,7 @@ template <typename T>
TInspector<T>& TInspector<T>::operator=(TInspector<T>&& rhs) {
if (&rhs != this) {
node = std::move(rhs.node);
id = std::exchange(rhs.id, CStr<128>());
id = std::move(rhs.id);
pReg = std::exchange(rhs.pReg, nullptr);
bNew = std::exchange(rhs.bNew, false);
bOpen = std::exchange(rhs.bOpen, false);
Expand All @@ -356,7 +353,7 @@ TInspector<T>& TInspector<T>::operator=(TInspector<T>&& rhs) {
template <typename T>
TInspector<T>::~TInspector() {
if (bNew && pReg) {
if (auto add = TreeNode(CStr<16>("[Add {}]", id.data()), false, true, true, false); add.test(GUI::eLeftClicked)) {
if (auto add = TreeNode(StackString<64>("[Add {}]", id.get()), false, true, true, false); add.test(GUI::eLeftClicked)) {
dens::registry& registry = *pReg;
registry.attach<T>(entity);
}
Expand Down
2 changes: 1 addition & 1 deletion levk/gameplay/src/ecs/components/spring_arm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void SpringArm::inspect(editor::Inspect<SpringArm> inspect) {
editor::TWidget<f32>("b##springarm", spring.b, 0.001f);
editor::TWidget<glm::vec3>("offset##springarm", spring.offset, false);
auto const name = inspect.registry.contains(spring.target) ? inspect.registry.name(spring.target) : "[None]";
auto const label = ktl::stack_string<128>("{} [{}]", name.data(), inspect.entity.id);
auto const label = fmt::format("{} [{}]", name.data(), inspect.entity.id);
editor::TWidget<std::string_view> select("target##springarm", label);
if (auto target = editor::DragDrop::Target()) {
if (auto e = target.payload<dens::entity>("ENTITY")) { spring.target = *e; }
Expand Down
12 changes: 6 additions & 6 deletions levk/gameplay/src/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ void displayScale([[maybe_unused]] f32 renderScale) {
template <typename T>
std::string_view inspectAsset(AssetStore const& store, std::string_view name, std::string_view uri) {
Text title(uri);
auto const id = CStr<128>("{}##inspect_{}", name, name);
auto const id = StackString<128>("{}##inspect_{}", name, name);
std::string_view ret;
if (auto popup = Popup(id)) {
static ktl::stack_string<128> s_search;
TWidget<char*> search(CStr<128>("Search##inspect_{}", name), s_search.c_str(), s_search.capacity());
static StackString<128> s_search{};
TWidget<char*> search(fmt::format("Search##inspect_{}", name), s_search.buf, s_search.capacity_v);
if (auto select = AssetIndex::list<T>(store, s_search)) {
ret = select.item;
popup.close();
Expand All @@ -72,19 +72,19 @@ void inspectPrimitiveP(Inspect<PrimitiveProvider> primitive) {
auto mesh = primitive.get().meshPrimitiveURI();
auto mat = primitive.get().materialURI();
auto tex = primitive.get().textureRefsURI();
if (auto tn = TreeNode(CStr<64>("MeshPrimitive"))) {
if (auto tn = TreeNode("MeshPrimitive")) {
auto const uri = primitive.store.uri<graphics::MeshPrimitive>(mesh);
if (auto select = inspectAsset<graphics::MeshPrimitive>(primitive.store, "MeshPrimitive", uri); !select.empty()) {
primitive.get() = PrimitiveProvider(select, mat, tex);
}
}
if (auto tn = TreeNode(CStr<64>("BPMaterial"))) {
if (auto tn = TreeNode("BPMaterial")) {
auto const uri = primitive.store.uri<graphics::BPMaterialData>(mat);
if (auto select = inspectAsset<graphics::BPMaterialData>(primitive.store, "BPMaterial", uri); !select.empty()) {
primitive.get() = PrimitiveProvider(mesh, select, tex);
}
}
if (auto tn = TreeNode(CStr<64>("TextureRefs"))) {
if (auto tn = TreeNode("TextureRefs")) {
auto const uri = primitive.store.uri<TextureRefs>(tex);
if (auto select = inspectAsset<TextureRefs>(primitive.store, "TextureRefs", uri); !select.empty()) {
primitive.get() = PrimitiveProvider(mesh, mat, select);
Expand Down
10 changes: 5 additions & 5 deletions levk/gameplay/src/editor/inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void Inspector::update([[maybe_unused]] SceneRef const& scene) {
auto& reg = *Sudo::registry(scene);
if (!inspect->tree) {
Text(reg.name(inspect->entity));
Text(CStr<16>("id: {}", inspect->entity.id));
Text(StackString<64>("id: {}", inspect->entity.id));
if (reg.attached<RenderPipeline>(inspect->entity) || reg.attached<RenderPipeProvider>(inspect->entity)) {
TWidgetWrap<bool> draw;
if (draw(shouldDraw(inspect->entity, reg), "Draw", draw.out)) { shouldDraw(inspect->entity, reg, draw.out); }
Expand All @@ -99,7 +99,7 @@ void Inspector::update([[maybe_unused]] SceneRef const& scene) {
if (auto transform = reg.find<Transform>(inspect->entity)) { TransformWidget{}(*transform); }
attach(inspect->entity, reg, *store);
} else {
auto const name = CStr<128>("{} -> [GUI node]", reg.name(inspect->entity));
auto const name = StackString<64>("{} -> [GUI node]", reg.name(inspect->entity));
Text txt(name);
GuiRect{}(inspect->tree->m_rect);
if (auto view = dynamic_cast<gui::View*>(inspect->tree)) {
Expand Down Expand Up @@ -131,9 +131,9 @@ void Inspector::attach(dens::entity entity, dens::registry& reg, AssetStore cons
Styler(glm::vec2{0.0f, 30.0f});
if (Button("Attach")) { Popup::open("attach_component"); }
if (auto attach = Popup("attach_component")) {
static CStr<128> s_filter;
editor::TWidget<char*>("Search##component_filter", s_filter.c_str(), s_filter.capacity());
auto filter = s_filter.get();
static StackString<64> s_filter{};
editor::TWidget<char*>("Search##component_filter", s_filter.buf, s_filter.capacity_v);
auto filter = std::string_view(s_filter);
for (auto const& kvp : attachable) {
auto const& [id, gadget] = *kvp;
Pane sub("component_list", {0.0f, 80.0f});
Expand Down
8 changes: 4 additions & 4 deletions levk/gameplay/src/editor/log_stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ void drawLog(glm::vec2 fbSize, f32 logHeight, FrameTime ft) {
{
static constexpr s64 s_minTCounter = 1, s_maxTCounter = 20, scale = 25;
s64 ftCount = (s64)LogStats::s_frameTimeCount / scale;
TWidget<std::pair<s64, s64>> st(CStr<32>("ft count: {}", ftCount * scale), ftCount, s_minTCounter, s_maxTCounter, 1);
TWidget<std::pair<s64, s64>> st(StackString<128>("ft count: {}", ftCount * scale), ftCount, s_minTCounter, s_maxTCounter, 1);
LogStats::s_frameTimeCount = (std::size_t)ftCount * scale;
f32 const ftime = ft.samples.empty() ? 0.0f : ft.samples.back();
auto const overlay = CStr<32>("{.3f}ms (avg of {})", ft.average, ft.samples.size());
auto const title = CStr<32>("[{.2f}ms] [{}] FPS", ftime, ft.rate);
auto const overlay = StackString<128>("{:.3f}ms (avg of {})", ft.average, ft.samples.size());
auto const title = StackString<128>("[{:.2f}ms] [{}] FPS", ftime, ft.rate);
Styler s(Style::eSameLine);
ImGui::PlotLines(title.data(), ft.samples.data(), (s32)ft.samples.size(), 0, overlay.data());
s(Style::eSeparator);
Expand Down Expand Up @@ -92,7 +92,7 @@ void drawLog(glm::vec2 fbSize, f32 logHeight, FrameTime ft) {
Styler s(Style::eSameLine);
static constexpr s64 s_minTCounter = 1, s_maxTCounter = 20, scale = 100;
s64 lineCount = (s64)LogStats::s_lineCount / scale;
TWidget<std::pair<s64, s64>> st(CStr<32>("line count: {}", lineCount * scale), lineCount, s_minTCounter, s_maxTCounter, 1);
TWidget<std::pair<s64, s64>> st(StackString<128>("line count: {}", lineCount * scale), lineCount, s_minTCounter, s_maxTCounter, 1);
LogStats::s_lineCount = (std::size_t)lineCount * scale;
}
{
Expand Down
18 changes: 9 additions & 9 deletions levk/gameplay/src/editor/main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ Panes g_panes;
void Panes::showStats(Engine::Service const& eng) const {
if (auto p = Pane("Engine Stats", {200.0f, 250.0f}, {200.0f, 200.0f}, &g_panes.flag(Flag::eStats))) {
auto const& s = eng.stats();
auto t = Text(CStr<32>("FPS: {}", s.frame.rate));
t = Text(CStr<32>("Frame #: {}", s.frame.count));
t = Text(CStr<32>("Uptime: {}", time::format(s.upTime).data()));
t = Text(CStr<32>("Draw calls: {}", s.gfx.drawCalls));
t = Text(CStr<32>("Triangles: {}", s.gfx.triCount));
t = Text(CStr<32>("Window: {}x{}", s.gfx.extents.window.x, s.gfx.extents.window.y));
t = Text(CStr<32>("Swapchain: {}x{}", s.gfx.extents.swapchain.x, s.gfx.extents.swapchain.y));
t = Text(CStr<32>("Renderer: {}x{}", s.gfx.extents.renderer.x, s.gfx.extents.renderer.y));
auto t = Text(StackString<64>("FPS: {}", s.frame.rate));
t = Text(StackString<64>("Frame #: {}", s.frame.count));
t = Text(StackString<64>("Uptime: {}", time::format(s.upTime).data()));
t = Text(StackString<64>("Draw calls: {}", s.gfx.drawCalls));
t = Text(StackString<64>("Triangles: {}", s.gfx.triCount));
t = Text(StackString<64>("Window: {}x{}", s.gfx.extents.window.x, s.gfx.extents.window.y));
t = Text(StackString<64>("Swapchain: {}x{}", s.gfx.extents.swapchain.x, s.gfx.extents.swapchain.y));
t = Text(StackString<64>("Renderer: {}x{}", s.gfx.extents.renderer.x, s.gfx.extents.renderer.y));
Styler st(Style::eSeparator);
auto& renderer = eng.context().renderer();
f32 rs = renderer.renderScale();
Expand All @@ -65,7 +65,7 @@ void Panes::showProfiler(Engine::Service const& engine) const {
for (auto const& entry : record.entries) {
Text t(entry.id);
ImGui::SameLine(idLength + 20.0f);
ImGui::ProgressBar(entry.dt / total, ImVec2{-1.0f, 0.0f}, CStr<16>("{1.2f}ms", entry.dt.count() * 1000.0f).data());
ImGui::ProgressBar(entry.dt / total, ImVec2{-1.0f, 0.0f}, StackString<16>("{:1.2f}ms", entry.dt.count() * 1000.0f).data());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion levk/gameplay/src/editor/palettes/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void context(graphics::RenderContext& rc) {
void renderer(graphics::Renderer& rd) {
Text txt("Renderer");
static f32 s_newScale = rd.renderScale();
if (Button(ktl::stack_string<16>("Scale: {.2f}", rd.renderScale())) && rd.canScale()) { Popup::open("settings_renderer_scale"); }
if (Button(StackString<32>("Scale: {:.2f}", rd.renderScale())) && rd.canScale()) { Popup::open("settings_renderer_scale"); }
if (auto popup = Popup("settings_renderer_scale")) {
TWidget<f32> rsw("Render Scale", s_newScale, 0.03f, 75.0f, {0.5f, 4.0f});
if (Button("Set")) {
Expand Down
12 changes: 6 additions & 6 deletions levk/gameplay/src/editor/scene_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ void inspect(InspectVerifier& iv, editor::TreeNode& tn, dens::entity entity, gui
}

template <typename T>
CStr<128> uniqueGuiName(T const& t) {
CStr<128> str;
StackString<64> uniqueGuiName(T const& t) {
StackString<64> str;
if constexpr (std::is_base_of_v<gui::View, T>) {
str = t.m_name;
str = std::string_view(t.m_name);
} else {
str = utils::tName(&t);
}
str += CStr<2>("##");
str += CStr<16>("{x}", &t);
str += StackString<4>("##");
str += StackString<32>("{}", static_cast<void const*>(&t));
return str;
}

Expand All @@ -78,7 +78,7 @@ void walk(SceneNode& node, InspectVerifier& iv, dens::registry const& reg) {
auto tn = makeNode(reg.name(entity), iv(entity), node.nodes().empty());
if (auto source = DragDrop::Source()) {
source.payload("ENTITY", entity);
Text(CStr<128>("{} [{}]", reg.name(entity), entity.id));
Text(StackString<128>("{} [{}]", reg.name(entity), entity.id));
}
if (auto target = DragDrop::Target()) {
if (auto e = target.payload<dens::entity>("ENTITY")) {
Expand Down
4 changes: 2 additions & 2 deletions levk/gameplay/src/editor/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,12 @@ TWidget<Transform>::TWidget(MU sv idPos, MU sv idOrn, MU sv idScl, MU Transform&
TWidget<std::pair<s64, s64>>::TWidget(MU sv id, MU s64& out_t, MU s64 min, MU s64 max, MU s64 dt) {
#if defined(LEVK_USE_IMGUI)
ImGui::PushButtonRepeat(true);
if (ImGui::ArrowButton(CStr<64>("##{}_left", id).data(), ImGuiDir_Left) && out_t > min) {
if (ImGui::ArrowButton(StackString<64>("##{}_left", id).data(), ImGuiDir_Left) && out_t > min) {
out_t -= dt;
changed = true;
}
ImGui::SameLine(0.0f, 3.0f);
if (ImGui::ArrowButton(CStr<64>("##{}_right", id).data(), ImGuiDir_Right) && out_t < max) {
if (ImGui::ArrowButton(StackString<64>("##{}_right", id).data(), ImGuiDir_Right) && out_t < max) {
out_t += dt;
changed = true;
}
Expand Down
1 change: 1 addition & 0 deletions levk/graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ if(LEVK_USE_PCH)
<unordered_map>
<vector>
<vulkan/vulkan.hpp>
<vulkan/vulkan_hash.hpp>

<ktl/fixed_vector.hpp>
<ktl/hash_table.hpp>
Expand Down
1 change: 0 additions & 1 deletion levk/graphics/include/levk/graphics/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ inline ktl::hash_table<vk::Result, std::string_view> g_vkResultStr = {
{vk::Result::eSuboptimalKHR, "SubmoptimalSurface"},
{vk::Result::eErrorDeviceLost, "DeviceLost"},
{vk::Result::eErrorSurfaceLostKHR, "SurfaceLost"},
{vk::Result::eErrorFullScreenExclusiveModeLostEXT, "FullScreenExclusiveModeLost"},
{vk::Result::eErrorOutOfDateKHR, "OutOfDateSurface"},
};
} // namespace le::graphics
1 change: 1 addition & 0 deletions levk/graphics/include/levk/graphics/utils/layout_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <ktl/hash_table.hpp>
#include <levk/core/not_null.hpp>
#include <levk/graphics/common.hpp>
#include <vulkan/vulkan_hash.hpp>

namespace le::graphics {
using StageAccess = TPair<vk::PipelineStageFlags, vk::AccessFlags>;
Expand Down
14 changes: 7 additions & 7 deletions levk/graphics/src/device/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,9 @@ bool Device::isBusy(vk::Fence fence) const {
}

void Device::waitFor(Span<vk::Fence const> fences, stdch::nanoseconds const wait) const {
if constexpr (levk_debug) {
auto const result = m_device->waitForFences(u32(fences.size()), fences.data(), true, static_cast<u64>(wait.count()));
KASSERT(result != vk::Result::eTimeout && result != vk::Result::eErrorDeviceLost, "Fence wait failure!");
} else {
m_device->waitForFences(u32(fences.size()), fences.data(), true, maths::max<u64>());
}
u64 const count = levk_debug ? static_cast<u64>(wait.count()) : maths::max<u64>();
auto const result = m_device->waitForFences(u32(fences.size()), fences.data(), true, count);
KASSERT(result != vk::Result::eTimeout && result != vk::Result::eErrorDeviceLost, "Fence wait failure!");
}

void Device::resetFence(vk::Fence optional, bool wait) const {
Expand All @@ -321,7 +318,10 @@ void Device::resetFence(vk::Fence optional, bool wait) const {
}

void Device::resetAll(Span<vk::Fence const> fences) const {
if (!fences.empty()) { m_device->resetFences(u32(fences.size()), fences.data()); }
if (!fences.empty()) {
auto const result = m_device->resetFences(u32(fences.size()), fences.data());
KASSERT(result != vk::Result::eTimeout && result != vk::Result::eErrorDeviceLost, "Fence reset failure!");
}
}

void Device::resetCommandPool(vk::CommandPool pool) const {
Expand Down
Loading

0 comments on commit 509fd4c

Please sign in to comment.