Skip to content

Commit

Permalink
✨ more core system improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lamarrr committed Nov 20, 2024
1 parent e1cf143 commit 9190a34
Show file tree
Hide file tree
Showing 23 changed files with 2,123 additions and 2,606 deletions.
26 changes: 14 additions & 12 deletions ashura/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@
"height": 1080
},
"shaders": {
"Blur:VS": "shaders/blur.vs.spv",
"Blur:FS": "shaders/blur.fs.spv",
"Ngon:VS": "shaders/ngon.vs.spv",
"Ngon:FS": "shaders/ngon.fs.spv",
"Pbr:VS": "shaders/pbr.vs.spv",
"Pbr:FS": "shaders/pbr.fs.spv",
"Rrect:VS": "shaders/rrect.vs.spv",
"Rrect:FS": "shaders/rrect.fs.spv"
"Blur/Downsample:VS": "shaders/blur_downsample.vert.spv",
"Blur/Downsample:FS": "shaders/blur_downsample.frag.spv",
"Blur/Upsample:VS": "shaders/blur_upsample.vert.spv",
"Blur/Upsample:FS": "shaders/blur_upsample.frag.spv",
"Ngon:VS": "shaders/ngon.vert.spv",
"Ngon:FS": "shaders/ngon.frag.spv",
"PBR:VS": "shaders/pbr.vert.spv",
"PBR:FS": "shaders/pbr.frag.spv",
"RRect:VS": "shaders/rrect.vert.spv",
"RRect:FS": "shaders/rrect.frag.spv"
},
"fonts": {
"Amiri": "fonts/Amiri-Regular.ttf",
"MaterialIcons": "fonts/MaterialIcons-Regular.ttf",
"Roboto": "fonts/Roboto-Regular.ttf",
"RobotoMono": "fonts/RobotoMono-Regular.ttf"
"Amiri": "fonts/Amiri/Amiri-Regular.ttf",
"MaterialIcons": "fonts/MaterialIcons/MaterialIcons-Regular.ttf",
"Roboto": "fonts/Roboto/Roboto-Regular.ttf",
"RobotoMono": "fonts/RobotoMono/RobotoMono-Regular.ttf"
},
"default_font": "Amiri",
"images": {}
Expand Down
18 changes: 9 additions & 9 deletions ashura/engine/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ struct Canvas
{
struct RenderContext
{
Canvas &canvas;
GpuContext &gpu;
PassContext &passes;
RenderTarget const &rt;
gpu::CommandEncoderImpl const &enc;
SSBO const &rrects;
SSBO const &ngons;
SSBO const &ngon_vertices;
SSBO const &ngon_indices;
Canvas &canvas;
GpuContext &gpu;
PassContext &passes;
RenderTarget const &rt;
gpu::CommandEncoder &enc;
SSBO const &rrects;
SSBO const &ngons;
SSBO const &ngon_vertices;
SSBO const &ngon_indices;
};

enum class BatchType : u8
Expand Down
54 changes: 23 additions & 31 deletions ashura/engine/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,11 @@ void Engine::init(AllocatorImpl allocator, void *app,

logger->trace("Initializing Engine");

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

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

GpuContext gpu_ctx =
GpuContext::create(allocator, device, cfg.gpu.hdr, cfg.gpu.buffering,
Expand All @@ -160,7 +158,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"_span).unwrap();

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

engine = new (storage) Engine{allocator,
app,
instance,
std::move(instance),
device,
window,
surface,
Expand All @@ -211,6 +209,8 @@ void Engine::init(AllocatorImpl allocator, void *app,

}));

engine->device->begin_frame(nullptr).unwrap();

// [ ] setup window event listeners
// [ ] recreate_swapchain

Expand Down Expand Up @@ -257,17 +257,13 @@ void Engine::init(AllocatorImpl allocator, void *app,

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

engine->assets.shaders.insert(std::move(shader_id), shader)
.unwrap();

logger->trace("Shader", span(shader_id), " loaded to GPU");

sem->increment(1);
},
async::Ready{}, TaskSchedule{.target = TaskTarget::Main});
Expand Down Expand Up @@ -328,8 +324,6 @@ void Engine::init(AllocatorImpl allocator, void *app,
engine->assets.fonts.insert(std::move(font_id), std::move(font))
.unwrap();

logger->trace("Uploaded font ", span(font_id), " to GPU");

sem->increment(1);
},
async::Ready{}, TaskSchedule{.target = TaskTarget::Main});
Expand All @@ -346,6 +340,8 @@ void Engine::init(AllocatorImpl allocator, void *app,

engine->renderer.acquire(engine->gpu_ctx, engine->assets);

engine->device->submit_frame(nullptr).unwrap();

logger->trace("Engine Initialized");
}

Expand All @@ -354,7 +350,7 @@ void Engine::uninit()
// check resources have been purged
// TODO(lamarrr):
// shader_map.iter([&](Span<char const>, gpu::Shader shader) {
// device->uninit_shader(device.self, shader);
// device->uninit_shader(device, shader);
// });
CHECK(engine != nullptr);
logger->trace("Uninitializing Engine");
Expand All @@ -365,39 +361,35 @@ void Engine::uninit()

Engine::~Engine()
{
device->uninit_swapchain(device.self, swapchain);
device->uninit_swapchain(swapchain);
window_system->uninit_window(window);
logger->trace("Uninitializing Window System");
WindowSystem::uninit();
instance->uninit_device(instance.self, device.self);
instance->uninit(instance.self);
instance->uninit_device(device);
}

void Engine::recreate_swapchain_()
{
gpu::SurfaceCapabilities capabilities =
device->get_surface_capabilities(device.self, surface).unwrap();
device->get_surface_capabilities(surface).unwrap();
CHECK(
has_bits(capabilities.image_usage, gpu::ImageUsage::TransferDst |
gpu::ImageUsage::ColorAttachment));

Vec<gpu::SurfaceFormat> formats;
u32 num_formats =
device->get_surface_formats(device.self, surface, {}).unwrap();
u32 num_formats = device->get_surface_formats(surface, {}).unwrap();
CHECK(num_formats != 0);
formats.resize_uninit(num_formats).unwrap();
CHECK(device->get_surface_formats(device.self, surface, span(formats))
.unwrap() == num_formats);
CHECK(device->get_surface_formats(surface, span(formats)).unwrap() ==
num_formats);

Vec<gpu::PresentMode> present_modes;
u32 num_present_modes =
device->get_surface_present_modes(device.self, surface, {}).unwrap();
device->get_surface_present_modes(surface, {}).unwrap();
CHECK(num_present_modes != 0);
present_modes.resize_uninit(num_present_modes).unwrap();
CHECK(
device
->get_surface_present_modes(device.self, surface, span(present_modes))
.unwrap() == num_present_modes);
CHECK(device->get_surface_present_modes(surface, span(present_modes))
.unwrap() == num_present_modes);

Vec2U surface_extent = window_system->get_surface_size(window);
surface_extent.x = max(surface_extent.x, 1U);
Expand Down Expand Up @@ -486,11 +478,11 @@ void Engine::recreate_swapchain_()

if (swapchain == nullptr)
{
swapchain = device->create_swapchain(device.self, surface, info).unwrap();
swapchain = device->create_swapchain(surface, info).unwrap();
}
else
{
device->invalidate_swapchain(device.self, swapchain, info).unwrap();
device->invalidate_swapchain(swapchain, info).unwrap();
}
}

Expand Down
10 changes: 5 additions & 5 deletions ashura/engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ struct Engine
{
void *app;

gpu::InstanceImpl instance;
Dyn<gpu::Instance *> instance;

gpu::DeviceImpl device;
gpu::Device *device;

Window window;

Expand Down Expand Up @@ -102,13 +102,13 @@ struct Engine

bool should_shutdown = false;

Engine(AllocatorImpl allocator, void *app, gpu::InstanceImpl instance,
gpu::DeviceImpl device, Window window, gpu::Surface surface,
Engine(AllocatorImpl allocator, void *app, Dyn<gpu::Instance *> instance,
gpu::Device *device, Window window, gpu::Surface surface,
gpu::PresentMode present_mode_preference, GpuContext gpu_ctx,
Renderer renderer, Canvas canvas, ViewSystem view_system,
ViewContext view_ctx) :
app{app},
instance{instance},
instance{std::move(instance)},
device{device},
window{window},
surface{surface},
Expand Down
90 changes: 43 additions & 47 deletions ashura/engine/font_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ struct FontImpl : Font

virtual void upload_to_device(GpuContext &c, AllocatorImpl allocator) override
{
gpu::CommandEncoderImpl enc = c.encoder();
gpu::DeviceImpl d = c.device;
gpu::CommandEncoder &enc = c.encoder();
gpu::Device &dev = *c.device;

CHECK(cpu_atlas.is_some());
CHECK(gpu_atlas.is_none());
Expand All @@ -328,21 +328,20 @@ struct FontImpl : Font
CHECK(atlas.extent.y > 0);

gpu::Image image =
d->create_image(
d.self,
gpu::ImageInfo{.label = "Font Atlas Image"_span,
.type = gpu::ImageType::Type2D,
.format = gpu::Format::B8G8R8A8_UNORM,
.usage = gpu::ImageUsage::Sampled |
gpu::ImageUsage::InputAttachment |
gpu::ImageUsage::Storage |
gpu::ImageUsage::TransferSrc |
gpu::ImageUsage::TransferDst,
.aspects = gpu::ImageAspects::Color,
.extent = {atlas.extent.x, atlas.extent.y, 1},
.mip_levels = 1,
.array_layers = atlas.num_layers,
.sample_count = gpu::SampleCount::Count1})
dev.create_image(
gpu::ImageInfo{.label = "Font Atlas Image"_span,
.type = gpu::ImageType::Type2D,
.format = gpu::Format::B8G8R8A8_UNORM,
.usage = gpu::ImageUsage::Sampled |
gpu::ImageUsage::InputAttachment |
gpu::ImageUsage::Storage |
gpu::ImageUsage::TransferSrc |
gpu::ImageUsage::TransferDst,
.aspects = gpu::ImageAspects::Color,
.extent = {atlas.extent.x, atlas.extent.y, 1},
.mip_levels = 1,
.array_layers = atlas.num_layers,
.sample_count = gpu::SampleCount::Count1})
.unwrap();

Vec<gpu::ImageView> views;
Expand All @@ -352,32 +351,31 @@ struct FontImpl : Font
for (u32 i = 0; i < atlas.num_layers; i++)
{
views[i] =
d->create_image_view(
d.self,
gpu::ImageViewInfo{.label = "Font Atlas Image View"_span,
.image = image,
.view_type = gpu::ImageViewType::Type2D,
.view_format = gpu::Format::B8G8R8A8_UNORM,
.mapping = {},
.aspects = gpu::ImageAspects::Color,
.first_mip_level = 0,
.num_mip_levels = 1,
.first_array_layer = i,
.num_array_layers = 1})
dev.create_image_view(
gpu::ImageViewInfo{.label = "Font Atlas Image View"_span,
.image = image,
.view_type = gpu::ImageViewType::Type2D,
.view_format = gpu::Format::B8G8R8A8_UNORM,
.mapping = {},
.aspects = gpu::ImageAspects::Color,
.first_mip_level = 0,
.num_mip_levels = 1,
.first_array_layer = i,
.num_array_layers = 1})
.unwrap();
}

u64 const atlas_size = atlas.channels.size() * (u64) 4;
gpu::Buffer staging_buffer =
d->create_buffer(
d.self, gpu::BufferInfo{.label = "Font Atlas Staging Buffer"_span,
.size = atlas_size,
.host_mapped = true,
.usage = gpu::BufferUsage::TransferSrc |
gpu::BufferUsage::TransferDst})
dev.create_buffer(
gpu::BufferInfo{.label = "Font Atlas Staging Buffer"_span,
.size = atlas_size,
.host_mapped = true,
.usage = gpu::BufferUsage::TransferSrc |
gpu::BufferUsage::TransferDst})
.unwrap();

u8 *map = (u8 *) d->map_buffer_memory(d.self, staging_buffer).unwrap();
u8 *map = (u8 *) dev.map_buffer_memory(staging_buffer).unwrap();

ImageLayerSpan<u8, 4> dst{.channels = {map, atlas_size},
.width = atlas.extent.x,
Expand All @@ -390,10 +388,10 @@ struct FontImpl : Font
dst.get_layer(i), U8_MAX, U8_MAX, U8_MAX);
}

d->flush_mapped_buffer_memory(d.self, staging_buffer,
{.offset = 0, .size = gpu::WHOLE_SIZE})
dev.flush_mapped_buffer_memory(staging_buffer,
{.offset = 0, .size = gpu::WHOLE_SIZE})
.unwrap();
d->unmap_buffer_memory(d.self, staging_buffer);
dev.unmap_buffer_memory(staging_buffer);

Vec<gpu::BufferImageCopy> copies{allocator};

Expand All @@ -415,7 +413,7 @@ struct FontImpl : Font
.image_extent = {atlas.extent.x, atlas.extent.y, 1}};
}

enc->copy_buffer_to_image(enc.self, staging_buffer, image, span(copies));
enc.copy_buffer_to_image(staging_buffer, image, span(copies));

gpu::Format format = gpu::Format::B8G8R8A8_UNORM;
c.release(staging_buffer);
Expand All @@ -429,13 +427,11 @@ struct FontImpl : Font
for (u32 i = 0; i < atlas.num_layers; i++)
{
textures[i] = c.alloc_texture_slot();
d->update_descriptor_set(
d.self,
gpu::DescriptorSetUpdate{
.set = c.texture_views,
.binding = 0,
.element = textures[i],
.images = span({gpu::ImageBinding{.image_view = views[i]}})});
dev.update_descriptor_set(gpu::DescriptorSetUpdate{
.set = c.texture_views,
.binding = 0,
.element = textures[i],
.images = span({gpu::ImageBinding{.image_view = views[i]}})});
}

gpu_atlas = Some{GpuFontAtlas{.image = image,
Expand Down
Loading

0 comments on commit 9190a34

Please sign in to comment.