Skip to content

Commit

Permalink
♻️ long-overdue span refactoring + QoL improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lamarrr committed Nov 29, 2024
1 parent c523cda commit 4764dde
Show file tree
Hide file tree
Showing 38 changed files with 1,469 additions and 1,338 deletions.
20 changes: 10 additions & 10 deletions ashura/editor/editor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ int main()

Engine::init(
default_allocator, nullptr,
R"(C:\Users\rlama\Documents\workspace\oss\ashura\ashura\config.json)"_span,
R"(C:\Users\rlama\Documents\workspace\oss\ashura\assets)"_span);
R"(C:\Users\rlama\Documents\workspace\oss\ashura\ashura\config.json)"_str,
R"(C:\Users\rlama\Documents\workspace\oss\ashura\assets)"_str);

defer engine_{[&] { Engine::uninit(); }};

Expand All @@ -35,36 +35,36 @@ scalar.frame(250, 100);

// Don't use whole font metrics for line metrics

btn.text(U"replay RELOAD"_span)
btn.text(U"replay RELOAD"_str)
.style(TextStyle{.foreground = colors::WHITE, .background = colors::BLUE},
FontStyle{.font = engine->assets.fonts["RobotoMono"_span].get(),
FontStyle{.font = engine->assets.fonts["RobotoMono"_str].get(),
.font_height = 50,
.line_height = 1.2F})
.style(TextStyle{.foreground = colors::WHITE, .background = colors::BLUE},
FontStyle{.font = engine->assets.fonts["MaterialIcons"_span].get(),
FontStyle{.font = engine->assets.fonts["MaterialIcons"_str].get(),
.font_height = 40,
.line_height = 1},
0, 6)
.padding(10, 10)
.color(colors::CYAN);
btn_home.text(U"home HOME"_span)
btn_home.text(U"home HOME"_str)
.style(TextStyle{.foreground = colors::WHITE, .background = colors::BLUE},
FontStyle{.font = engine->assets.fonts["RobotoMono"_span].get(),
FontStyle{.font = engine->assets.fonts["RobotoMono"_str].get(),
.font_height = 50,
.line_height = 1.2F})
.style(TextStyle{.foreground = colors::WHITE, .background = colors::BLUE},
FontStyle{.font = engine->assets.fonts["MaterialIcons"_span].get(),
FontStyle{.font = engine->assets.fonts["MaterialIcons"_str].get(),
.font_height = 40,
.line_height = 1},
0, 4)
.frame(200, 200)
.color(colors::CYAN);

btn2.text(
U"بِسْمِ ٱللَّهِ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ ١ ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَـٰلَمِينَ ٢ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ ٣ مَـٰلِكِ يَوْمِ ٱلدِّينِ ٤ إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ ٥ ٱهْدِنَا ٱلصِّرَٰطَ ٱلْمُسْتَقِيمَ ٦ صِرَٰطَ ٱلَّذِينَ أَنْعَمْتَ عَلَيْهِمْ غَيْرِ ٱلْمَغْضُوبِ عَلَيْهِمْ وَلَا ٱلضَّآلِّينَ ٧"_span)
U"بِسْمِ ٱللَّهِ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ ١ ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَـٰلَمِينَ ٢ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ ٣ مَـٰلِكِ يَوْمِ ٱلدِّينِ ٤ إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ ٥ ٱهْدِنَا ٱلصِّرَٰطَ ٱلْمُسْتَقِيمَ ٦ صِرَٰطَ ٱلَّذِينَ أَنْعَمْتَ عَلَيْهِمْ غَيْرِ ٱلْمَغْضُوبِ عَلَيْهِمْ وَلَا ٱلضَّآلِّينَ ٧"_str)
.style(
TextStyle{.foreground = colors::WHITE, .background = colors::YELLOW},
FontStyle{.font = engine->assets.fonts["Amiri"_span].get(),
FontStyle{.font = engine->assets.fonts["Amiri"_str].get(),
.font_height = 50,
.line_height = 1.2F})
.frame(200, 200)
Expand Down
4 changes: 2 additions & 2 deletions ashura/engine/assets.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
namespace ash
{

typedef StrMap<gpu::Shader> ShaderMap;
typedef StrVecMap<gpu::Shader> ShaderMap;

typedef StrMap<Dyn<Font *>> FontMap;
typedef StrVecMap<Dyn<Font *>> FontMap;

// [ ] images

Expand Down
12 changes: 6 additions & 6 deletions ashura/engine/canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void path::brect(Vec<Vec2> &vtx, Vec4 slant)
{1 - slant.z, 1}, {-1 + slant.w, 1},
{-1, 1 - slant.w}, {-1, -1 + slant.x}};

vtx.extend_copy(span(vertices)).unwrap();
vtx.extend_copy(vertices).unwrap();
}

void path::bezier(Vec<Vec2> &vtx, Vec2 cp0, Vec2 cp1, Vec2 cp2, u32 segments)
Expand Down Expand Up @@ -385,8 +385,8 @@ static inline void flush_batch(Canvas &c)
switch (batch.type)
{
case Canvas::BatchType::RRect:
c.add_pass("RRect"_span, [batch, world_to_view = c.world_to_view](
Canvas::RenderContext const &ctx) {
c.add_pass("RRect"_str, [batch, world_to_view = c.world_to_view](
Canvas::RenderContext const &ctx) {
RRectPassParams params{.rendering_info = ctx.rt.info,
.scissor =
clip_to_scissor(ctx.rt.viewport, batch.clip),
Expand All @@ -402,8 +402,8 @@ static inline void flush_batch(Canvas &c)
return;

case Canvas::BatchType::Ngon:
c.add_pass("Ngon"_span, [batch, world_to_view = c.world_to_view](
Canvas::RenderContext const &ctx) {
c.add_pass("Ngon"_str, [batch, world_to_view = c.world_to_view](
Canvas::RenderContext const &ctx) {
NgonPassParams params{
.rendering_info = ctx.rt.info,
.scissor = clip_to_scissor(ctx.rt.viewport, batch.clip),
Expand Down Expand Up @@ -843,7 +843,7 @@ Canvas &Canvas::blur(CRect const &area, u32 num_passes)
{
flush_batch(*this);

add_pass("Blur"_span, [num_passes, area](Canvas::RenderContext const &ctx) {
add_pass("Blur"_str, [num_passes, area](Canvas::RenderContext const &ctx) {
BlurPassParams params{.image_view = ctx.rt.info.color_attachments[0].view,
.extent = ctx.rt.extent,
.texture_view = ctx.rt.color_descriptor,
Expand Down
31 changes: 16 additions & 15 deletions ashura/engine/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ EngineCfg EngineCfg::parse(AllocatorImpl allocator, Span<u8 const> json)

std::string_view default_font_sv =
config["default_font"].get_string().value();
out.default_font = vec(allocator, span(default_font_sv)).unwrap();
out.default_font = vec<char>(allocator, default_font_sv).unwrap();

// check that it is a valid entry
fonts[default_font_sv].get_string().value();
Expand Down Expand Up @@ -132,15 +132,15 @@ void Engine::init(AllocatorImpl allocator, void *app,

read_file(config_path, json).unwrap();

EngineCfg cfg = EngineCfg::parse(allocator, span(json));
EngineCfg cfg = EngineCfg::parse(allocator, json);

logger->trace("Initializing Engine");

Dyn<gpu::Instance *> instance =
gpu::create_vulkan_instance(allocator, cfg.gpu.validation).unwrap();

gpu::Device *device =
instance->create_device(allocator, span(cfg.gpu.preferences), 2).unwrap();
instance->create_device(allocator, cfg.gpu.preferences, 2).unwrap();

GpuContext gpu_ctx =
GpuContext::create(allocator, device, cfg.gpu.hdr, cfg.gpu.buffering,
Expand All @@ -159,7 +159,7 @@ void Engine::init(AllocatorImpl allocator, void *app,
logger->trace("Creating Root Window");

Window window =
window_system->create_window(*instance, "Ashura"_span).unwrap();
window_system->create_window(*instance, "Ashura"_str).unwrap();

if (cfg.window.maximized)
{
Expand Down Expand Up @@ -237,17 +237,17 @@ void Engine::init(AllocatorImpl allocator, void *app,

cfg.shaders.iter([&](Vec<char> &id, Vec<char> &path) {
Vec<char> resolved_path = vec(allocator, assets_dir).unwrap();
path_append(resolved_path, span(path)).unwrap();
path_append(resolved_path, path).unwrap();

async::once([shader_id = vec(allocator, span(id)).unwrap(),
async::once([shader_id = vec<char>(allocator, span(id)).unwrap(),
shader_path = std::move(resolved_path), sem = sem.alias(),
allocator]() mutable {
logger->trace("Loading shader ", span(shader_id), " from ",
span(shader_path));

Vec<u8> data{allocator};

if (Result result = read_file(span(shader_path), data); !result)
if (Result result = read_file(shader_path, data); !result)
{
logger->error("Unable to load shader at ", span(shader_path),
", IO Error: ", result.err());
Expand All @@ -274,8 +274,8 @@ void Engine::init(AllocatorImpl allocator, void *app,

gpu::Shader shader =
engine->device
->create_shader(gpu::ShaderInfo{
.label = "Shader"_span, .spirv_code = span(data_u32)})
->create_shader(gpu::ShaderInfo{.label = "Shader"_str,
.spirv_code = data_u32})
.unwrap();

engine->assets.shaders.insert(std::move(shader_id), shader)
Expand All @@ -289,16 +289,16 @@ void Engine::init(AllocatorImpl allocator, void *app,

cfg.fonts.iter([&](Vec<char> &id, Vec<char> &path) {
Vec<char> resolved_path = vec(allocator, assets_dir).unwrap();
path_append(resolved_path, span(path)).unwrap();
path_append(resolved_path, path).unwrap();

async::once([font_id = vec(allocator, span(id)).unwrap(),
async::once([font_id = vec<char>(allocator, span(id)).unwrap(),
font_path = std::move(resolved_path), sem = sem.alias(),
allocator]() mutable {
logger->trace("Loading font ", span(font_id), " from ", span(font_path));

Vec<u8> data{allocator};

Result read_result = read_file(span(font_path), data);
Result read_result = read_file(font_path, data);

if (!read_result)
{
Expand All @@ -308,7 +308,7 @@ void Engine::init(AllocatorImpl allocator, void *app,
return;
}

Result decode_result = Font::decode(span(data), 0, allocator);
Result decode_result = Font::decode(data, 0, allocator);

if (!decode_result)
{
Expand Down Expand Up @@ -352,7 +352,8 @@ void Engine::init(AllocatorImpl allocator, void *app,
scheduler->execute_main_thread_loop(1ms, 2ms);
}

engine->default_font_name = vec(allocator, span(cfg.default_font)).unwrap();
engine->default_font_name =
vec<char>(allocator, span(cfg.default_font)).unwrap();
engine->default_font = engine->assets.fonts[engine->default_font_name].get();

engine->renderer.acquire(engine->gpu_ctx, engine->assets);
Expand Down Expand Up @@ -479,7 +480,7 @@ void Engine::recreate_swapchain_()
}
}

gpu::SwapchainInfo info{.label = "Window Swapchain"_span,
gpu::SwapchainInfo info{.label = "Window Swapchain"_str,
.format = format,
.usage = gpu::ImageUsage::TransferDst |
gpu::ImageUsage::ColorAttachment,
Expand Down
14 changes: 7 additions & 7 deletions ashura/engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ struct EngineCfg

struct Window
{
bool resizable = true;
bool maximized = false;
bool resizable = true;
bool maximized = false;
bool full_screen = false;
u32 width = 1920;
u32 height = 1080;
u32 width = 1920;
u32 height = 1080;
};

Gpu gpu{};
Expand All @@ -37,11 +37,11 @@ struct EngineCfg

Vec<char> default_font{};

StrMap<Vec<char>> shaders{};
StrVecMap<Vec<char>> shaders{};

StrMap<Vec<char>> fonts{};
StrVecMap<Vec<char>> fonts{};

StrMap<Vec<char>> images{};
StrVecMap<Vec<char>> images{};

static EngineCfg parse(AllocatorImpl allocator, Span<u8 const> json);
};
Expand Down
10 changes: 5 additions & 5 deletions ashura/engine/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ constexpr Span<char const> to_string(FontErr err)
switch (err)
{
case FontErr::None:
return "None"_span;
return "None"_str;
case FontErr::DecodingFailed:
return "DecodingFailed"_span;
return "DecodingFailed"_str;
case FontErr::FaceNotFound:
return "FaceNotFound"_span;
return "FaceNotFound"_str;
case FontErr::OutOfMemory:
return "OutOfMemory"_span;
return "OutOfMemory"_str;
default:
return "Unidentified"_span;
return "Unidentified"_str;
}
}

Expand Down
2 changes: 1 addition & 1 deletion ashura/engine/font_atlas.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct CpuFontAtlas

ImageLayerSpan<u8, 1> span() const
{
return ImageLayerSpan<u8, 1>{.channels = ash::span(channels),
return ImageLayerSpan<u8, 1>{.channels = channels,
.width = extent.x,
.height = extent.y,
.layers = num_layers};
Expand Down
20 changes: 10 additions & 10 deletions ashura/engine/font_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ struct FontImpl : Font

virtual FontInfo info() override
{
FontInfo info{.postscript_name = span(postscript_name),
.family_name = span(family_name),
.style_name = span(style_name),
.glyphs = span(glyphs),
FontInfo info{.postscript_name = postscript_name,
.family_name = family_name,
.style_name = style_name,
.glyphs = glyphs,
.replacement_glyph = replacement_glyph,
.space_glyph = space_glyph,
.ellipsis_glyph = ellipsis_glyph,
Expand Down Expand Up @@ -269,7 +269,7 @@ struct FontImpl : Font
return Err{};
}

ImageLayerSpan<u8, 1> atlas_span{.channels = span(atlas.channels),
ImageLayerSpan<u8, 1> atlas_span{.channels = atlas.channels,
.width = atlas_extent.x,
.height = atlas_extent.y,
.layers = num_layers};
Expand Down Expand Up @@ -329,7 +329,7 @@ struct FontImpl : Font

gpu::Image image =
dev.create_image(
gpu::ImageInfo{.label = "Font Atlas Image"_span,
gpu::ImageInfo{.label = "Font Atlas Image"_str,
.type = gpu::ImageType::Type2D,
.format = gpu::Format::B8G8R8A8_UNORM,
.usage = gpu::ImageUsage::Sampled |
Expand All @@ -352,7 +352,7 @@ struct FontImpl : Font
{
views[i] =
dev.create_image_view(
gpu::ImageViewInfo{.label = "Font Atlas Image View"_span,
gpu::ImageViewInfo{.label = "Font Atlas Image View"_str,
.image = image,
.view_type = gpu::ImageViewType::Type2D,
.view_format = gpu::Format::B8G8R8A8_UNORM,
Expand All @@ -368,7 +368,7 @@ struct FontImpl : Font
u64 const atlas_size = atlas.channels.size() * (u64) 4;
gpu::Buffer staging_buffer =
dev.create_buffer(
gpu::BufferInfo{.label = "Font Atlas Staging Buffer"_span,
gpu::BufferInfo{.label = "Font Atlas Staging Buffer"_str,
.size = atlas_size,
.host_mapped = true,
.usage = gpu::BufferUsage::TransferSrc |
Expand Down Expand Up @@ -413,7 +413,7 @@ struct FontImpl : Font
.image_extent = {atlas.extent.x, atlas.extent.y, 1}};
}

enc.copy_buffer_to_image(staging_buffer, image, span(copies));
enc.copy_buffer_to_image(staging_buffer, image, copies);

gpu::Format format = gpu::Format::B8G8R8A8_UNORM;
c.release(staging_buffer);
Expand All @@ -422,7 +422,7 @@ struct FontImpl : Font
Vec<AtlasGlyph> glyphs;

textures.resize_defaulted(atlas.num_layers).unwrap();
glyphs.extend_copy(span(atlas.glyphs)).unwrap();
glyphs.extend_copy(atlas.glyphs).unwrap();

for (u32 i = 0; i < atlas.num_layers; i++)
{
Expand Down
Loading

0 comments on commit 4764dde

Please sign in to comment.