From 59b6e2779fe429d64c012318885c870eb0b78bff Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Tue, 25 Nov 2025 21:25:04 +0100 Subject: [PATCH] better timing --- examples/HelloAnimation/main.cpp | 13 +++++------ examples/HelloAnimation/project.json | 2 +- .../Crafter.Graphics-Window_wayland.cpp | 23 +++++++++++++++---- project.json | 15 ++++++++++++ 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/examples/HelloAnimation/main.cpp b/examples/HelloAnimation/main.cpp index d9624d0..75edede 100644 --- a/examples/HelloAnimation/main.cpp +++ b/examples/HelloAnimation/main.cpp @@ -6,7 +6,7 @@ using namespace Crafter; int main() { WindowWayland window(1280, 720, "Hello Input!"); - UiElementBufferBuffer* element = new UiElementBufferBuffer( + RenderingElement element( 2, //bufferWidth: the width of this elements pixel buffer 1, //bufferHeight: the height of this elements pixel buffer FractionalToMapped(0.5), //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor @@ -19,9 +19,8 @@ int main() { false //ignoreScaling: wether this element ignores the scaling of the window, if true its size will be scaled according to the window scale ); - window.elements.push_back(element); - - element->UpdatePosition(window); + window.elements.push_back(&element); + element.UpdatePosition(window); Animation> anim({ {std::chrono::seconds(0), FractionalToMapped(-0.5)}, @@ -32,14 +31,14 @@ int main() { EventListener updateListener(&window.onUpdate, [&](FrameTime time){ std::tuple value = anim.Play(time.now); - element->transform.anchorX = std::get<0>(value); - element->UpdatePosition(window); + element.anchorX = std::get<0>(value); + element.UpdatePosition(window); if(anim.currentFrame == anim.keyframes.size()-1) { anim.Start(time.now); } }); - element->buffer = {{255, 0, 0 ,255}, {0, 255, 0 ,255}}; + element.buffer = {{255, 0, 0 ,255}, {0, 255, 0 ,255}}; window.StartUpdate(); window.StartSync(); } diff --git a/examples/HelloAnimation/project.json b/examples/HelloAnimation/project.json index 70e3d7a..3762cb1 100644 --- a/examples/HelloAnimation/project.json +++ b/examples/HelloAnimation/project.json @@ -7,7 +7,7 @@ "dependencies": [ { "path":"../../project.json", - "configuration":"lib-wayland" + "configuration":"lib-wayland-timing" } ] } diff --git a/implementations/Crafter.Graphics-Window_wayland.cpp b/implementations/Crafter.Graphics-Window_wayland.cpp index 8e77268..762f3a5 100644 --- a/implementations/Crafter.Graphics-Window_wayland.cpp +++ b/implementations/Crafter.Graphics-Window_wayland.cpp @@ -232,30 +232,45 @@ void WindowWayland::xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base xdg_wm_base_pong(xdg_wm_base, serial); } +#ifdef CRAFTER_TIMING std::chrono::time_point framEnd; +#endif void WindowWayland::wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t time) { + #ifdef CRAFTER_TIMING auto start = std::chrono::high_resolution_clock::now(); auto vblank = duration_cast(start - framEnd); + #endif wl_callback_destroy(cb); WindowWayland* window = reinterpret_cast(data); if(window->updating) { cb = wl_surface_frame(window->surface); wl_callback_add_listener(cb, &WindowWayland::wl_callback_listener, window); - auto startUpdate = std::chrono::high_resolution_clock::now(); window->onUpdate.Invoke({start, start-window->lastFrameBegin}); - auto endUpdate = std::chrono::high_resolution_clock::now(); + #ifdef CRAFTER_TIMING + std::chrono::nanoseconds totalUpdate = std::chrono::nanoseconds(0); + for (const std::pair*, std::chrono::nanoseconds>& entry : window->onUpdate.listenerTimes) { + totalUpdate += entry.second; + } + std::cout << std::format("Update: {}", duration_cast(totalUpdate)) << std::endl; + for (const std::pair*, std::chrono::nanoseconds>& entry : window->onUpdate.listenerTimes) { + std::cout << std::format("\t{} {}", reinterpret_cast(entry.first), entry.second) << std::endl; + } auto startRender = std::chrono::high_resolution_clock::now(); + #endif window->Render(); + #ifdef CRAFTER_TIMING auto endRender = std::chrono::high_resolution_clock::now(); auto end = std::chrono::high_resolution_clock::now(); - std::cout << std::format("Update: {}, Render: {}, Vblank: {}, Total: {}", duration_cast(endUpdate - startUpdate), duration_cast(endRender - startRender), vblank, duration_cast((endUpdate - startUpdate)+(endRender - startRender)+vblank)) << std::endl; + std::cout << std::format("Render: {}, Vblank: {}, Total: {}", duration_cast(endRender - startRender), vblank, duration_cast(totalUpdate+(endRender - startRender)+vblank)) << std::endl; + #endif } - + #ifdef CRAFTER_TIMING framEnd = std::chrono::high_resolution_clock::now(); + #endif window->lastFrameBegin = start; } diff --git a/project.json b/project.json index 4fc6644..4d6880d 100644 --- a/project.json +++ b/project.json @@ -29,6 +29,15 @@ } ] }, + { + "name": "deps-timing", + "dependencies": [ + { + "path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Event.git", + "configuration":"lib-timing" + } + ] + }, { "name": "deps-debug", "debug": true, @@ -44,6 +53,12 @@ "extends": ["wayland", "deps"], "type": "library" }, + { + "name": "lib-wayland-timing", + "extends": ["wayland", "deps-timing"], + "type": "library", + "defines": [{ "name": "CRAFTER_TIMING" }] + }, { "name": "lib-wayland-debug", "type": "library",