Skip to content

Commit

Permalink
✨ new core types + refactoring & API improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lamarrr committed Dec 3, 2024
1 parent d81851c commit bbd2a5b
Show file tree
Hide file tree
Showing 22 changed files with 900 additions and 623 deletions.
2 changes: 0 additions & 2 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ CheckOptions:
bugprone-dangling-handle.HandleClasses: >
std::basic_string_view
std::experimental::basic_string_view
ash::Span
ash::Buffer
WarningsAsErrors: ""
HeaderFilterRegex: ""
FormatStyle: file
6 changes: 3 additions & 3 deletions ashura/engine/canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ static inline void flush_batch(Canvas & c)
.params_ssbo = ctx.ngons.descriptor,
.textures = ctx.gpu.texture_views,
.index_counts =
span(ctx.canvas.ngon_index_counts).slice(batch.objects)};
ctx.canvas.ngon_index_counts.span().slice(batch.objects)};
ctx.passes.ngon->encode(ctx.gpu, ctx.enc, params);
});
return;
Expand Down Expand Up @@ -666,7 +666,7 @@ Canvas & Canvas::text(ShapeInfo const & info, TextBlock const & block,
f32 cursor = space_align(block_width, ln.metrics.width, alignment) -
ln.metrics.width * 0.5F;
for (TextRun const & run :
span(layout.runs).slice(ln.first_run, ln.num_runs))
layout.runs.span().slice(ln.first_run, ln.num_runs))
{
FontStyle const & font_style = block.fonts[run.style];
TextStyle const & run_style = style.runs[run.style];
Expand Down Expand Up @@ -822,7 +822,7 @@ Canvas & Canvas::triangles(ShapeInfo const & info, Span<Vec2 const> points,
ngon_vertices.extend_copy(points).unwrap();
ngon_indices.extend_copy(idx).unwrap();

for (u32 & v : span(ngon_indices).slice(first_index))
for (u32 & v : ngon_indices.span().slice(first_index))
{
v += first_vertex;
}
Expand Down
57 changes: 27 additions & 30 deletions ashura/engine/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ EngineCfg EngineCfg::parse(AllocatorImpl allocator, Span<u8 const> json)
auto id = entry.escaped_key().value();
auto path = entry.value().get_string().value();
out.shaders
.insert(vec(allocator, span(id)).unwrap(),
vec(allocator, span(path)).unwrap())
.insert(vec(span(id), allocator).unwrap(),
vec(span(path), allocator).unwrap())
.unwrap();
}

Expand All @@ -86,14 +86,14 @@ EngineCfg EngineCfg::parse(AllocatorImpl allocator, Span<u8 const> json)
auto id = entry.escaped_key().value();
auto path = entry.value().get_string().value();
out.fonts
.insert(vec(allocator, span(id)).unwrap(),
vec(allocator, span(path)).unwrap())
.insert(vec(span(id), allocator).unwrap(),
vec(span(path), allocator).unwrap())
.unwrap();
}

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

// check that it is a valid entry
fonts[default_font_sv].get_string().value();
Expand All @@ -104,8 +104,8 @@ EngineCfg EngineCfg::parse(AllocatorImpl allocator, Span<u8 const> json)
auto id = entry.escaped_key().value();
auto path = entry.value().get_string().value();
out.images
.insert(vec(allocator, span(id)).unwrap(),
vec(allocator, span(path)).unwrap())
.insert(vec(span(id), allocator).unwrap(),
vec(span(path), allocator).unwrap())
.unwrap();
}

Expand Down Expand Up @@ -238,20 +238,19 @@ void Engine::init(AllocatorImpl allocator, void * app,
.unwrap();

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

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

Vec<u8> data{allocator};

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

data_u32.resize_uninit(data.size() >> 2).unwrap();

mem::copy(span(data), span(data_u32).as_u8());
mem::copy(data.span(), data_u32.span().as_u8());

logger->trace("Loaded shader ", span(shader_id), " from file");
logger->trace("Loaded shader ", shader_id, " from file");

async::once(
[shader_id = std::move(shader_id), sem = std::move(sem),
data_u32 = std::move(data_u32)]() mutable {
logger->trace("Sending shader ", span(shader_id), " to GPU");
logger->trace("Sending shader ", shader_id, " to GPU");

gpu::Shader shader =
engine->device
Expand All @@ -290,21 +289,21 @@ 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();
Vec<char> resolved_path = vec(assets_dir, allocator).unwrap();
path_append(resolved_path, path).unwrap();

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

Vec<u8> data{allocator};

Result read_result = read_file(font_path, data);

if (!read_result)
{
logger->error("Unable to load font at ", span(font_path),
logger->error("Unable to load font at ", font_path,
", IO Error: ", read_result.err());
sem->increment(1);
return;
Expand All @@ -314,29 +313,28 @@ void Engine::init(AllocatorImpl allocator, void * app,

if (!decode_result)
{
logger->error("Unable to decode font at ", span(font_path),
logger->error("Unable to decode font at ", font_path,
"Error: ", decode_result.err());
sem->increment(1);
return;
}

Dyn<Font *> font = decode_result.unwrap();

logger->trace("Loaded font ", span(font_id), " from file");
logger->trace("Loaded font ", font_id, " from file");

u32 const font_height = 64;

logger->trace("Rasterizing font ", span(font_id), " @", font_height,
"px ");
logger->trace("Rasterizing font ", font_id, " @", font_height, "px ");

font->rasterize(font_height, allocator).unwrap();

logger->trace("Rasterized font ", span(font_id));
logger->trace("Rasterized font ", font_id);

async::once(
[font_id = std::move(font_id), sem = std::move(sem),
font = std::move(font), allocator]() mutable {
logger->trace("Uploading font ", span(font_id), " to GPU");
logger->trace("Uploading font ", font_id, " to GPU");

font->upload_to_device(engine->gpu_ctx, allocator);

Expand All @@ -354,8 +352,7 @@ void Engine::init(AllocatorImpl allocator, void * app,
scheduler->execute_main_thread_loop(1ms, 2ms);
}

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

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

for (gpu::ColorSpace cp : preferred_color_spaces)
{
Span sel = find_if(span(formats), [&](gpu::SurfaceFormat a) {
Span sel = find_if(formats.span(), [&](gpu::SurfaceFormat a) {
return a.color_space == cp;
});
if (!sel.is_empty())
Expand All @@ -455,7 +452,7 @@ void Engine::recreate_swapchain_()

for (gpu::PresentMode pm : preferred_present_modes)
{
if (!find(span(present_modes), pm).is_empty())
if (!find(present_modes.span(), pm).is_empty())
{
found_present_mode = true;
present_mode = pm;
Expand Down Expand Up @@ -567,7 +564,7 @@ void Engine::run(View & view)
.render_area = {.offset = {},
.extent = gpu_ctx.screen_fb.extent},
.num_layers = 1,
.color_attachments = span(attachments),
.color_attachments = attachments,
.depth_attachment = {},
.stencil_attachment = {}},
.viewport = gpu::Viewport{.offset = {0, 0},
Expand Down
2 changes: 1 addition & 1 deletion ashura/engine/font_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ struct FontImpl : Font
rect_pack::pack_rects(pack_context, rects.data() + num_packed,
num_rasterized_glyphs - num_packed);
auto [just_packed, unpacked] =
partition(span(rects).slice(num_packed),
partition(rects.span().slice(num_packed),
[](rect_pack::rect const & r) { return r.was_packed; });
for (u32 i = num_packed; i < (num_packed + just_packed.span); i++)
{
Expand Down
2 changes: 1 addition & 1 deletion ashura/engine/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "ashura/std/math.h"
#include "ashura/std/option.h"
#include "ashura/std/result.h"
#include "ashura/std/sparse_vec.h"
#include "ashura/std/types.h"
#include "ashura/std/vec.h"

namespace ash
{
Expand Down
8 changes: 4 additions & 4 deletions ashura/engine/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ struct ViewContext
/// @brief makes a zoom transform matrix relative to the center of a viewport.
/// defines the translation and scaling components.
/// @return zoom transform matrix
constexpr Mat3Affine scroll_transform(Vec2 viewport_extent, Vec2 view_extent,
Vec2 t, f32 scale)
constexpr Affine3 scroll_transform(Vec2 viewport_extent, Vec2 view_extent,
Vec2 t, f32 scale)
{
Vec2 const low = -0.5F * viewport_extent + 0.5F * view_extent;
Vec2 const high = 0.5F * viewport_extent - 0.5F * view_extent;
Expand Down Expand Up @@ -439,7 +439,7 @@ struct ViewLayout
{
Vec2 extent = {};
Vec2 viewport_extent = {};
Mat3Affine viewport_transform = Mat3Affine::identity();
Affine3 viewport_transform = Affine3::identity();
Option<Vec2> fixed_position = None;
};

Expand Down Expand Up @@ -584,7 +584,7 @@ struct View
/// @brief Called when the viewport is needed to zoom itself, scaling its
/// inner extent
/// @param zoom zoom to apply to the inner extent
constexpr virtual void zoom(Mat3Affine const & transform)
constexpr virtual void zoom(Affine3 const & transform)
{
(void) transform;
}
Expand Down
56 changes: 28 additions & 28 deletions ashura/engine/view_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@ struct ViewSystem
BitVec<u64> is_esc_input;
BitVec<u64> is_viewport;

Vec<Vec2> centers;
Vec<Vec2> extents;
Vec<Vec2> viewport_extents;
Vec<Mat3Affine> viewport_transforms;
BitVec<u64> is_fixed_positioned;
Vec<Vec2> fixed_positions;
Vec<i32> z_indices;
Vec<i32> stacking_contexts;

Vec<Mat3Affine> transforms;
Vec<CRect> clips;
Vec<u32> z_ordering;
Vec<u32> focus_ordering;
Vec<Vec2> centers;
Vec<Vec2> extents;
Vec<Vec2> viewport_extents;
Vec<Affine3> viewport_transforms;
BitVec<u64> is_fixed_positioned;
Vec<Vec2> fixed_positions;
Vec<i32> z_indices;
Vec<i32> stacking_contexts;

Vec<Affine3> transforms;
Vec<CRect> clips;
Vec<u32> z_ordering;
Vec<u32> focus_ordering;

explicit ViewSystem(AllocatorImpl allocator) :
views{allocator},
Expand Down Expand Up @@ -322,9 +322,9 @@ struct ViewSystem

void focus_order()
{
iota(span(focus_ordering), 0U);
iota(focus_ordering.span(), 0U);

indirect_sort(span(focus_ordering), [&](u32 a, u32 b) {
indirect_sort(focus_ordering.span(), [&](u32 a, u32 b) {
return tab_indices[a] < tab_indices[b];
});

Expand All @@ -349,7 +349,7 @@ struct ViewSystem
{
ViewNode const & node = nodes[i];
views[i]->size(extents[i],
span(extents).slice(node.first_child, node.num_children));
extents.span().slice(node.first_child, node.num_children));
}

centers[0] = Vec2::splat(0);
Expand All @@ -361,8 +361,8 @@ struct ViewSystem
i--;
ViewNode const & node = nodes[i];
ViewLayout layout = views[i]->fit(
extents[i], span(extents).slice(node.first_child, node.num_children),
span(centers).slice(node.first_child, node.num_children));
extents[i], extents.span().slice(node.first_child, node.num_children),
centers.span().slice(node.first_child, node.num_children));
extents[i] = layout.extent;
viewport_extents[i] = layout.viewport_extent;
viewport_transforms[i] = layout.viewport_transform;
Expand All @@ -375,16 +375,16 @@ struct ViewSystem

// transform views to canvas-space

transforms[0] = Mat3Affine::identity();
transforms[0] = Affine3::identity();

for (u32 i = 0; i < n; i++)
{
ViewNode const & node = nodes[i];
ViewNode const & node = nodes[i];
// parent-space to local viewport-space transformation matrix
Mat3Affine const & viewport_transform = viewport_transforms[i];
Affine3 const & viewport_transform = viewport_transforms[i];
// accumulated transform of all ancestors, determines position until this
// parent
Mat3Affine const & ancestor_transform = transforms[i];
Affine3 const & ancestor_transform = transforms[i];
for (u32 c = node.first_child; c < (node.first_child + node.num_children);
c++)
{
Expand All @@ -402,8 +402,8 @@ struct ViewSystem

for (u32 i = 0; i < n; i++)
{
Mat3Affine const & transform = transforms[i];
f32 const zoom = transform[0][0];
Affine3 const & transform = transforms[i];
f32 const zoom = transform[0][0];
centers[i] =
ash::transform(transform, Vec2{0, 0}) + viewport_extent * 0.5F;
extents[i] = extents[i] * zoom;
Expand All @@ -418,7 +418,7 @@ struct ViewSystem
}
}

fill(span(clips), CRect::from_offset({0, 0}, viewport_extent));
fill(clips.span(), CRect::from_offset({0, 0}, viewport_extent));

/// recursive view clipping
for (u32 i = 0; i < n; i++)
Expand Down Expand Up @@ -471,7 +471,7 @@ struct ViewSystem
ViewNode const & node = nodes[i];
z_indices[i] = views[i]->z_index(
z_indices[i],
span(z_indices).slice(node.first_child, node.num_children));
z_indices.span().slice(node.first_child, node.num_children));
}

stacking_contexts[0] = 0;
Expand All @@ -484,10 +484,10 @@ struct ViewSystem
}
}

iota(span(z_ordering), 0U);
iota(z_ordering.span(), 0U);

// sort layers with priority: stacking_context, z_index, node depth
indirect_sort(span(z_ordering), [&](u32 a, u32 b) {
indirect_sort(z_ordering.span(), [&](u32 a, u32 b) {
if (stacking_contexts[a] < stacking_contexts[b])
{
return true;
Expand Down
Loading

0 comments on commit bbd2a5b

Please sign in to comment.