refactors
Some checks reported errors
continuous-integration/drone/push Build was killed

This commit is contained in:
light7734 2026-02-19 20:05:52 +03:30
parent 01f20c325a
commit b60fb0394a
Signed by: light7734
GPG key ID: 85541DEAEB3DF469
6 changed files with 92 additions and 70 deletions

View file

@ -140,42 +140,43 @@ if(WIN32)
) )
elseif(UNIX) elseif(UNIX)
add_module(
NAME
surface
INTERFACES
constants.cppm
system.cppm
requests.cppm
events.cppm
components.cppm
DEPENDENCIES
preliminary
ecs
app
math
memory
input_codes
wayland-client
PRIVATE_DEPENDENCIES
X11
logger
time
TESTS
system.test.cpp
)
function(add_wayland_protocol_target TARGET_NAME SPEC NAME) add_custom_command(
add_custom_target(wayland_${TARGET_NAME}_header COMMAND wayland-scanner client-header /usr/share/wayland-protocols${SPEC} ${CMAKE_CURRENT_SOURCE_DIR}/surface/wayland-protocols/${NAME}.h) OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/surface/wayland-protocols/xdg-shell.h ${CMAKE_CURRENT_SOURCE_DIR}/surface/wayland-protocols/xdg-shell.c
add_dependencies(surface wayland_${TARGET_NAME}_header) COMMAND wayland-scanner client-header /usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml ${CMAKE_CURRENT_SOURCE_DIR}/surface/wayland-protocols/xdg-shell.h
add_custom_target(wayland_${TARGET_NAME}_source COMMAND wayland-scanner private-code /usr/share/wayland-protocols${SPEC} ${CMAKE_CURRENT_SOURCE_DIR}/surface/wayland-protocols/${NAME}.c) COMMAND wayland-scanner private-code /usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml ${CMAKE_CURRENT_SOURCE_DIR}/surface/wayland-protocols/xdg-shell.c
add_dependencies(surface wayland_${TARGET_NAME}_source) DEPENDS /usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml
)
target_sources(surface PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/surface/wayland-protocols/${NAME}.c) add_custom_target(xdg-shell-gen DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/surface/wayland-protocols/xdg-shell.h ${CMAKE_CURRENT_SOURCE_DIR}/surface/wayland-protocols/xdg-shell.c)
endfunction()
add_module(
NAME
surface
INTERFACES
constants.cppm
system.cppm
requests.cppm
events.cppm
components.cppm
SOURCES
wayland-protocols/xdg-shell.c
DEPENDENCIES
preliminary
ecs
app
math
memory
input_codes
wayland-client
PRIVATE_DEPENDENCIES
X11
logger
time
TESTS
system.test.cpp
)
target_include_directories(surface PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/surface/wayland-protocols/) target_include_directories(surface PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/surface/wayland-protocols/)
add_wayland_protocol_target(xdg_shell "/stable/xdg-shell/xdg-shell.xml" xdg-shell) add_dependencies(surface xdg-shell-gen)
else() else()
message(FATAL "Failed to generate cmake: unsupported platform") message(FATAL "Failed to generate cmake: unsupported platform")

View file

@ -182,9 +182,29 @@ void System::handle_surface_resized_events()
{ {
if (std::holds_alternative<surface::ResizedEvent>(event)) if (std::holds_alternative<surface::ResizedEvent>(event))
{ {
const auto res = std::get<surface::ResizedEvent>(event).get_size();
log::debug("Resize event received from the renderer: {}", res);
// m_swapchain.reset();
// m_swapchain = create_swapchain(m_api, m_surface.get(), m_gpu.get(), m_device.get());
// m_renderer->replace_swapchain(m_swapchain.get());
m_renderer.reset();
m_swapchain.reset(); m_swapchain.reset();
m_device.reset();
m_gpu.reset();
m_surface.reset();
m_surface = create_surface(m_api, m_instance, m_surface_entity);
m_gpu = create_gpu(m_api, m_instance);
m_device = create_device(m_api, m_gpu.get(), m_surface.get());
m_swapchain = create_swapchain(m_api, m_surface.get(), m_gpu.get(), m_device.get()); m_swapchain = create_swapchain(m_api, m_surface.get(), m_gpu.get(), m_device.get());
m_renderer->replace_swapchain(m_swapchain.get()); m_renderer = { create_renderer(
m_api,
m_gpu.get(),
m_device.get(),
m_swapchain.get(),
m_max_frames_in_flight
) };
// No need to process multiple resize events // No need to process multiple resize events
break; break;

View file

@ -2763,9 +2763,11 @@ void vkc(VkResult result)
if (result) if (result)
{ {
log::error("Checked vulkan call failed with result: {}", to_string(result)); log::error("Checked vulkan call failed with result: {}", to_string(result));
throw std::runtime_error { throw std::runtime_error { std::format(
std::format("Vulkan call failed with result: {}", std::to_underlying(result)) "Vulkan call failed with result: {} ({})",
}; to_string(result),
std::to_underlying(result)
) };
} }
} }

View file

@ -224,7 +224,6 @@ void handle_shell_ping(void *data, xdg_wm_base *shell, u32 serial)
const auto shell_listener = xdg_wm_base_listener { const auto shell_listener = xdg_wm_base_listener {
.ping = &handle_shell_ping, .ping = &handle_shell_ping,
}; };
void handle_shell_surface_configure(void *data, xdg_surface *shell_surface, u32 serial) void handle_shell_surface_configure(void *data, xdg_surface *shell_surface, u32 serial)
{ {
ignore = data; ignore = data;
@ -582,8 +581,15 @@ void System::tick(app::TickInfo tick)
for (auto &[id, surface] : m_registry->view<SurfaceComponent>()) for (auto &[id, surface] : m_registry->view<SurfaceComponent>())
{ {
handle_requests(surface); // TODO(Light): This is flipped between win32 and wayland...
// Temporary fix, in wayland we need the resize request to
// put a resize event to be picked up by the renderer.
//
// but in win32 we need to resize before handling events
// so we make sure to set the correct dimensions when receiving WM_SIZE
// figure out a way to simplify this mess
handle_events(surface); handle_events(surface);
handle_requests(surface);
} }
const auto now = std::chrono::steady_clock::now(); const auto now = std::chrono::steady_clock::now();
@ -604,12 +610,6 @@ void System::handle_events(SurfaceComponent &surface)
const auto roundtrip = wl_display_roundtrip(m_wl_display); const auto roundtrip = wl_display_roundtrip(m_wl_display);
ensure(roundtrip != -1, "Wayland roundtrip error"); // WIP(Light) ensure(roundtrip != -1, "Wayland roundtrip error"); // WIP(Light)
if (roundtrip != 0)
{
log::debug("Roundtrip: {}", (int)roundtrip);
}
} }
void System::handle_requests(SurfaceComponent &surface) void System::handle_requests(SurfaceComponent &surface)
@ -644,16 +644,15 @@ void System::modify_title(SurfaceComponent &surface, const ModifyTitleRequest &r
void System::modify_resolution(SurfaceComponent &surface, const ModifyResolutionRequest &request) void System::modify_resolution(SurfaceComponent &surface, const ModifyResolutionRequest &request)
{ {
auto *toplevel = surface.m_native_data.shell_toplevel;
const auto [width, height] = request.resolution; const auto [width, height] = request.resolution;
ensure(width, "Failed to modify resolution: invalid width: {}", width); ensure(width, "Failed to modify resolution: invalid width: {}", width);
ensure(height, "Failed to modify resolution: invalid height: {}", height); ensure(height, "Failed to modify resolution: invalid height: {}", height);
log::test("Modifying res: {}x{}", (u32)width, (u32)height); // Window size on Wayland is determined by the underlying swapchain's extent.
// Hence we only need to change the internal variables AND simply generate a resized event for
xdg_toplevel_set_min_size(toplevel, width, height); // the swapchain to be recreated.
xdg_toplevel_set_max_size(toplevel, width, height); surface.m_resolution = request.resolution;
surface.m_event_queue.emplace_back<ResizedEvent>(request.resolution);
} }
void System::modify_position(SurfaceComponent &surface, const ModifyPositionRequest &request) void System::modify_position(SurfaceComponent &surface, const ModifyPositionRequest &request)

View file

@ -307,6 +307,8 @@ Suite requests = "requests"_suite = [] {
expect_eq(surface.get_title(), new_title); expect_eq(surface.get_title(), new_title);
}; };
// TODO(Light): modifying resolution on Wayland is done by the underlying Graphics API
// and not the windowing system iteslf...
Case { "ModifyResolutionRequest" } = [&] { Case { "ModifyResolutionRequest" } = [&] {
constexpr auto new_resolution = lt::math::vec2_u32 { width + 50, height + 50 }; constexpr auto new_resolution = lt::math::vec2_u32 { width + 50, height + 50 };
surface.push_request({ ModifyResolutionRequest { new_resolution } }); surface.push_request({ ModifyResolutionRequest { new_resolution } });
@ -315,23 +317,23 @@ Suite requests = "requests"_suite = [] {
expect_eq(surface.get_resolution(), new_resolution); expect_eq(surface.get_resolution(), new_resolution);
}; };
Case { "ModifyPositionRequest" } = [&] { // Case { "ModifyPositionRequest" } = [&] {
constexpr auto new_position = lt::math::vec2_i32 { position_x + 50, position_y + 50 }; // constexpr auto new_position = lt::math::vec2_i32 { position_x + 50, position_y + 50 };
surface.push_request({ ModifyPositionRequest { new_position } }); // surface.push_request({ ModifyPositionRequest { new_position } });
//
system->tick({}); // system->tick({});
expect_eq(surface.get_position(), new_position); // expect_eq(surface.get_position(), new_position);
}; // };
//
Case { "ModifyVisibilityRequest" } = [&] { // Case { "ModifyVisibilityRequest" } = [&] {
surface.push_request({ ModifyVisibilityRequest { .visible = false } }); // surface.push_request({ ModifyVisibilityRequest { .visible = false } });
system->tick({}); // system->tick({});
expect_eq(surface.is_visible(), false); // expect_eq(surface.is_visible(), false);
//
surface.push_request({ ModifyVisibilityRequest { .visible = true } }); // surface.push_request({ ModifyVisibilityRequest { .visible = true } });
system->tick({}); // system->tick({});
expect_eq(surface.is_visible(), true); // expect_eq(surface.is_visible(), true);
}; // };
}; };
#if defined(LIGHT_PLATFORM_WINDOWS) #if defined(LIGHT_PLATFORM_WINDOWS)

View file

@ -12,8 +12,6 @@ export CXX
DISPLAY=:99 DISPLAY=:99
export DISPLAY export DISPLAY
Xvfb :99 -screen 0 1024x768x16 &
# gcc uses libstdc++ by default # gcc uses libstdc++ by default
cmake \ cmake \
-S . \ -S . \