Files
OrcaSlicer/src/slic3r/GUI/FrameProfiler.cpp
T
HanifKoh 4895bc03b4 Remove Unused Project Includes and Forward-Declare Where a Type Is Only Referenced (#16099)
* Remove Unused Project Includes and Forward-Declare Where a Type Is Only Referenced

Generated with include-what-you-use and applied conservatively. Only OrcaSlicer's own headers, the ones under src/ and tests/, are removed or forward-declared; standard-library and third-party includes are left alone. An include is removed only when both the Release and the Debug configuration leave it unused, never from inside a conditional block, and never from a file with platform-specific blocks, which only gain includes. Files whose only use of a header sits behind a feature or debug macro (libvgcode's OpenGL ES and marker code, the ARACHNE/TESTS_EXPORT_SVGS debug output) keep their includes.

clonable_ptr.hpp gains #pragma once; it had no include guard and was only safe while Config.hpp was its sole includer.

* Remove Unused Project Includes From Files With Platform-Specific Code

A Linux include-what-you-use run cannot see the code inside _WIN32, __APPLE__ or __linux__ blocks, so its verdict is only taken where nothing the removed header declares, directly or through what it includes, is named inside those blocks. Removals also have to hold in both the Release and Debug configuration and never touch a line inside a conditional block.

* Restore the libslic3r Precompiled Header and Direct Includes Lost in the Platform Pass

The platform-file pass treated pchheader.hpp as an ordinary header and
emptied it, and left GUI_Preview.hpp and 14 other files relying on
headers they no longer reached directly.

* Restore MainFrame.hpp in ParamsDialog.cpp for the Windows-Only Reparent Call

* Include Headers That Files Reached Through Ones the Cleanup Removed

* Drop Includes Duplicated by the Cleanup or by Main's Own Additions

* Leave PreciseSeam.cpp as Main Has It After the Precise Seam Rework
2026-10-05 16:47:17 +08:00

158 lines
4.9 KiB
C++

#include "FrameProfiler.hpp"
#include "3DScene.hpp"
#include <chrono>
#include <array>
#include <glad/gl.h>
#include <algorithm>
#include <cstring>
#include <ratio>
#include <vector>
#include <utility>
namespace Slic3r {
namespace GUI {
void FrameProfiler::begin_frame()
{
m_recording = nullptr;
// GL 3.3 or ARB_timer_query
if (glQueryCounter == nullptr)
return;
collect(false);
Frame& frame = m_frames[m_next];
// Every frame in flight still waits for the GPU: skip this one rather than stall.
if (frame.pending)
return;
if (frame.queries[0] == 0)
glsafe(::glGenQueries(GLsizei(frame.queries.size()), frame.queries.data()));
glsafe(::glQueryCounter(frame.queries[0], GL_TIMESTAMP));
frame.count = 0;
frame.averaged = m_averaging;
m_recording = &frame;
m_last_mark = std::chrono::steady_clock::now();
}
void FrameProfiler::mark(const char* name)
{
if (m_recording == nullptr || m_recording->count == MAX_SECTIONS)
return;
Frame& frame = *m_recording;
const std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
glsafe(::glQueryCounter(frame.queries[2 * frame.count + 1], GL_TIMESTAMP));
// Else the GPU time would include the CPU time spent until the driver flushes on its own.
glsafe(::glFlush());
// Sent with the next section's commands, so the time the GPU waits for them is not billed to it.
if (frame.count + 1 < MAX_SECTIONS)
glsafe(::glQueryCounter(frame.queries[2 * frame.count + 2], GL_TIMESTAMP));
frame.names[frame.count] = name;
frame.cpu_ms[frame.count] = std::chrono::duration<double, std::milli>(now - m_last_mark).count();
++frame.count;
m_last_mark = now;
}
void FrameProfiler::end_frame()
{
if (m_recording == nullptr)
return;
m_recording->pending = m_recording->count > 0;
m_recording = nullptr;
m_next = (m_next + 1) % FRAMES_IN_FLIGHT;
}
void FrameProfiler::collect(bool wait)
{
constexpr double SMOOTHING = 0.1;
// Oldest first: a frame whose results are not in yet holds back the newer ones too.
for (size_t i = 0; i < FRAMES_IN_FLIGHT; ++i) {
Frame& frame = m_frames[(m_next + i) % FRAMES_IN_FLIGHT];
if (!frame.pending)
continue;
if (!wait) {
GLint available = 0;
glsafe(::glGetQueryObjectiv(frame.queries[2 * frame.count - 1], GL_QUERY_RESULT_AVAILABLE, &available));
if (available == 0)
break;
}
std::array<GLuint64, 2 * MAX_SECTIONS> stamps{};
for (size_t j = 0; j < 2 * frame.count; ++j)
glsafe(::glGetQueryObjectui64v(frame.queries[j], GL_QUERY_RESULT, &stamps[j]));
frame.pending = false;
const bool averaged = frame.averaged && m_averaging;
if (averaged)
++m_averaged_frames;
std::vector<Section> sections;
sections.reserve(frame.count);
for (size_t j = 0; j < frame.count; ++j) {
Section section{ frame.names[j], frame.cpu_ms[j], stamps[2 * j + 1] > stamps[2 * j] ? double(stamps[2 * j + 1] - stamps[2 * j]) * 1e-6 : 0.0 };
if (averaged) {
auto sum = std::find_if(m_sums.begin(), m_sums.end(), [&section](const Section& s) { return std::strcmp(s.name, section.name) == 0; });
if (sum == m_sums.end())
m_sums.push_back(section);
else {
sum->cpu_ms += section.cpu_ms;
sum->gpu_ms += section.gpu_ms;
}
}
for (const Section& prev : m_sections) {
if (std::strcmp(prev.name, section.name) == 0) {
section.cpu_ms = prev.cpu_ms + SMOOTHING * (section.cpu_ms - prev.cpu_ms);
section.gpu_ms = prev.gpu_ms + SMOOTHING * (section.gpu_ms - prev.gpu_ms);
break;
}
}
sections.push_back(section);
}
m_sections = std::move(sections);
}
}
void FrameProfiler::start_averaging()
{
m_averaging = true;
m_sums.clear();
m_averaged_frames = 0;
}
std::vector<FrameProfiler::Section> FrameProfiler::finish_averaging(bool wait)
{
if (wait && glQueryCounter != nullptr)
collect(true);
std::vector<Section> averages = std::move(m_sums);
for (Section& section : averages) {
section.cpu_ms /= double(m_averaged_frames);
section.gpu_ms /= double(m_averaged_frames);
}
m_averaging = false;
m_sums.clear();
m_averaged_frames = 0;
return averages;
}
void FrameProfiler::reset()
{
for (Frame& frame : m_frames) {
if (frame.queries[0] != 0)
glsafe(::glDeleteQueries(GLsizei(frame.queries.size()), frame.queries.data()));
frame = Frame();
}
m_recording = nullptr;
m_sections.clear();
m_averaging = false;
m_sums.clear();
m_averaged_frames = 0;
}
} // namespace GUI
} // namespace Slic3r