cgv
Loading...
Searching...
No Matches
performance_monitor.cxx
1#include "performance_monitor.h"
2#include <cgv/utils/file.h>
3#include <stack>
4#include <stdio.h>
5
6namespace cgv {
7 namespace render {
8
9performance_monitor::frame_data& performance_monitor::current_frame()
10{
11 return data.back();
12}
13
14void performance_monitor::add_measurement(const performance_measurement& pm)
15{
16 current_frame().push_back(pm);
17}
18
20{
21 fps = -1;
22 fps_alpha = 0.1;
23 time_scale = 60;
24 enabled = true;
25 frame_finished = true;
26 placement.ref_min_pnt().set(10,10);
27 placement.ref_max_pnt().set(310,110);
28 nr_display_cycles = 2;
29 bar_line_width = 5;
30 frame_id = 0;
31 init_tasks();
32 bar_config.push_back(PMB_MAX);
33 bar_config.push_back(PMB_CUR);
34 bar_config.push_back(PMB_MIN);
35}
36
39{
40 enabled = true;
41}
42
44{
45 enabled = false;
46}
47
48
49void performance_monitor::set_file_name(const std::string& _file_name)
50{
51 file_name = _file_name;
52}
53
54
57{
58 tasks.clear();
59 add_task("frame", rgb(0.5f, 0.5f, 0.5f));
60 add_task("main", rgb(0.8f, 0.2f, 0.2f));
61 add_task("stereo", rgb(0.2f, 0.2f, 0.8f));
62 add_task("shadow_map", rgb(0.1f, 0.1f, 0.1f));
63 add_task("shadow_volume", rgb(0.2f, 0.2f, 0.2f));
64 add_task("opaque_surface", rgb(1.0f, 1.0f, 0.0f));
65 add_task("transparent_surfaces", rgb(0.9f, 0.7f, 0.5f));
66 add_task("pick", rgb(0.0f, 0.0f, 1.0f));
67 add_task("user", rgb(0.0f, 1.0f, 0.0f));
68}
69
72{
73 bar_config.clear();
74}
75
77{
78 bar_config.push_back(item);
79}
80
83{
84 placement = rectangle;
85}
86
89{
90 nr_display_cycles = _nr_cycles;
91}
92
94int performance_monitor::add_task(const std::string& name, const cgv::media::color<float>& col)
95{
96 performance_task pt(name,col);
97 tasks.push_back(pt);
98 return (int)tasks.size() - 1;
99}
100
102{
103 if (!enabled)
104 return;
105 if (!frame_finished)
106 finish_frame();
107
108 while (data.size() >= get_buffer_size())
109 data.pop_front();
110 data.push_back(frame_data());
111 watch.restart();
112 performance_measurement pm(watch.get_elapsed_time(), 0, true);
113 add_measurement(pm);
114 frame_finished = false;
115 ++frame_id;
116}
117
120{
121 if (!enabled)
122 return;
123 performance_measurement pm(watch.get_elapsed_time(), task_id, true);
124 add_measurement(pm);
125}
126
128{
129 if (!enabled)
130 return;
131 performance_measurement pm(watch.get_elapsed_time(), task_id, false);
132 add_measurement(pm);
133}
134
136{
137 if (!enabled)
138 return;
139 const char* start_or_finish[] = { "start", "finish" };
140 performance_measurement pm(watch.get_elapsed_time(), 0, false);
141 add_measurement(pm);
142 double new_fps = 1.0 / (data.back().back().time - data.back().front().time);
143 if (fps < 0)
144 fps = new_fps;
145 else
146 fps = fps_alpha * new_fps + (1.0 - fps_alpha)*fps;
147 frame_finished = true;
148 if (file_name.empty())
149 return;
150 bool need_header = cgv::utils::file::exists(file_name);
151 FILE* fp = fopen(file_name.c_str(), "wa");
152 if (!fp)
153 return;
154 int i;
155 if (need_header) {
156 const frame_data& cf = current_frame();
157 for (i=0; i<(int)cf.size(); ++i) {
158 const performance_measurement& pm = cf[i];
159 fprintf(fp, i==0?"%s %s":",%s %s", start_or_finish[pm.start ? 0 : 1], tasks[pm.task_id].name.c_str());
160 }
161 fprintf(fp, "\n");
162 }
163 fprintf(fp, "%d", frame_id);
164 for (i=0; i<(int)current_frame().size(); ++i)
165 fprintf(fp, ", %f", current_frame()[i].time);
166 fprintf(fp, "\n");
167 fclose(fp);
168}
169
170void performance_monitor::compute_colors(const frame_data& fdata)
171{
172 colors.resize(2*fdata.size()-2);
173 std::stack<int> task_stack;
174 task_stack.push(0);
175 for (unsigned t=0; t<fdata.size()-1; ++t) {
176 int task_id = fdata[t].task_id;
177 if (fdata[t].start) {
178 colors[2*t+1] = colors[2*t] = tasks[task_id].col;
179 task_stack.push(task_id);
180 }
181 else {
182 task_stack.pop();
183 colors[2*t+1] = colors[2*t] = tasks[task_stack.top()].col;
184 }
185 }
186}
187
188void performance_monitor::compute_positions(int x0, int y0, int dx, int dy, const frame_data& fdata)
189{
190 positions.resize(2*fdata.size()-2);
191 double scale_x = time_scale*dx;
192 double scale_y = time_scale*dy;
193 int x = x0, y = y0;
194 for (unsigned t=0; t < fdata.size()-1; ++t) {
195 positions[2*t].set(x,y);
196 x = x0 + (int)(fdata[t+1].time*scale_x+0.5);
197 y = y0 + (int)(fdata[t+1].time*scale_y+0.5);
198 positions[2*t+1].set(x,y);
199 }
200}
201
202 }
203}
represent a color with components of given type and color and alpha model as specified.
Definition color.h:575
void disable()
disable performance monitoring
void clear_bar()
removes all items of the bar config and hides the bar
void add_bar_item(PerformanceMonitoringBar item)
add a bar item to the bar config
void finish_frame()
finish measurement of a frame, if this is not called by hand, it is called automatically in the next ...
void set_placement(const ibox2 &rectangle)
place the performance monitor on screen in pixel coordinates
void init_tasks()
initialize the list of tasks to one for the frame and one for each default rendering pass
void start_task(int task_id)
add a measurement for starting given task
performance_monitor()
construct performance monitor with standard configuration
void start_frame()
start performance measurement of a new frame
int add_task(const std::string &name, const rgb &col)
add a new to me monitored item
unsigned get_buffer_size() const
return the size of the buffer to store frame data
void set_nr_display_cycles(unsigned _nr_cycles)
set the number of display cycles to by drawn for the performance monitor
void finish_task(int task_id)
add a measurement for finishing given task
void enable()
enable performance monitoring
namespace for api independent GPU programming
PerformanceMonitoringBar
different items that can be shown in the long bar
this header is dependency free
Definition print.h:11
cgv::media::color< float, cgv::media::RGB > rgb
declare rgb color type with 32 bit components
Definition color.h:896
cgv::media::axis_aligned_box< int32_t, 2 > ibox2
declare type of 2d 32 bit integer axis-aligned boxes