forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.cpp
664 lines (582 loc) · 24.9 KB
/
benchmark.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/**************************************************************************/
/* benchmark.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
// Reference:
// ----------
// https://github.com/raysan5/raylib/issues/65
// http://www.codinghorror.com/blog/2011/12/fast-approximate-anti-aliasing-fxaa.html
#include "common/gd_core.h"
#include "common/gd_history_plot.h"
#include "core/error_macros.h"
#include "core/image.h"
#include "core/io/resource_saver.h"
#include "core/math/math_funcs.h"
#include "core/math/transform.h"
#include "core/os/keyboard.h"
#include "main/performance.h"
#include "scene/gui/label.h"
#include "scene/main/viewport.h"
#include "scene/resources/font.h"
#include "scene/resources/texture.h"
#include "benchmark.h"
const String _BULLET1 = String::chr(96);
const String _BULLET2 = String::chr(126);
const String _BULLET3 = String::chr(127);
const String _BULLET4 = String::chr(148);
const String _BULLET5 = String::chr(123);
const String _BULLET6 = String::chr(159);
const String _LT = String::chr(145);
const String _RT = String::chr(133);
const String _LB = String::chr(154);
const String _RB = String::chr(131);
const String _VL = String::chr(124);
const String _HL = String::chr(146);
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
_FORCE_INLINE_ static PoolByteArray create_poolarray(uint8_t *buf_ptr, int buf_size) {
PoolByteArray data;
data.resize(buf_size);
memcpy(data.write().ptr(), buf_ptr, buf_size);
return data;
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
static Ref<BitmapFont> make_font_from_grid(const String &p_characters, int p_grid_width, int p_grid_height, const uint8_t *p_img, size_t p_img_size) {
Ref<BitmapFont> font(memnew(BitmapFont));
Ref<Image> image = memnew(Image(p_img, p_img_size));
if (!image->empty()) {
Ref<ImageTexture> tex = memnew(ImageTexture);
tex->create_from_image(image, 0);
font->add_texture(tex);
const Size2i cell_size = image->get_size() / Size2(p_grid_width, p_grid_height);
for (int x = 0; x < p_grid_width; x++) {
for (int y = 0; y < p_grid_height; y++) {
const int index = x + y * p_grid_width;
if (index < p_characters.length()) {
const int chr = p_characters[index];
Rect2 frect(Point2(x * cell_size.width, y * cell_size.height), cell_size);
font->add_char(chr, 0, frect, Point2(), cell_size.width);
} else {
break;
}
}
}
font->set_height(cell_size.height);
}
return font;
}
static Ref<Texture> make_texture_from_data(const uint8_t *p_data, size_t p_data_len, int p_flags, const String &p_name) {
String fn = "user://__benchmark_" + String::num(p_flags) + "_" + p_name; // cached texture
if (ResourceLoader::exists(fn + ".tex")) {
Ref<Texture> texture = ResourceLoader::load(fn + ".tex", "Texture");
return texture;
} else {
// build texture atlas from resources
Vector<Ref<Image>> images;
Vector<String> names;
Ref<Image> image = memnew(Image(p_data, p_data_len));
Ref<ImageTexture> texture = memnew(ImageTexture);
texture->create_from_image(image, p_flags);
ResourceSaver::save(fn + ".tex", texture);
return texture;
}
}
extern "C" const uint8_t model_cube_dat_data[];
extern "C" const uint8_t texture_cube_png_data[];
extern "C" const size_t texture_cube_png_size;
extern "C" const uint8_t model_frog_dat_data[];
extern "C" const uint8_t texture_frog_png_data[];
extern "C" const size_t texture_frog_png_size;
extern "C" const uint8_t model_kid_dat_data[];
extern "C" const uint8_t texture_kid_png_data[];
extern "C" const size_t texture_kid_png_size;
extern "C" const uint8_t model_trex_dat_data[];
extern "C" const uint8_t texture_trex_png_data[];
extern "C" const size_t texture_trex_png_size;
extern "C" const uint8_t model_robot_dat_data[];
extern "C" const uint8_t texture_robot_png_data[];
extern "C" const size_t texture_robot_png_size;
extern "C" const uint8_t bitmap_font_png_data[];
extern "C" const size_t bitmap_font_png_size;
void Benchmark::_update_viewport() {
if (!Engine::get_singleton()->is_editor_hint()) {
// copy from main.cpp
String stretch_mode = GLOBAL_DEF("display/window/stretch/mode", "disabled");
String stretch_aspect = GLOBAL_DEF("display/window/stretch/aspect", "ignore");
Size2i stretch_size = Size2(GLOBAL_DEF("display/window/size/width", 0), GLOBAL_DEF("display/window/size/height", 0));
SceneTree::StretchMode sml_sm = SceneTree::STRETCH_MODE_DISABLED;
if (stretch_mode == "2d") {
sml_sm = SceneTree::STRETCH_MODE_2D;
} else if (stretch_mode == "viewport") {
sml_sm = SceneTree::STRETCH_MODE_VIEWPORT;
}
SceneTree::StretchAspect sml_aspect = SceneTree::STRETCH_ASPECT_IGNORE;
if (stretch_aspect == "keep") {
sml_aspect = SceneTree::STRETCH_ASPECT_KEEP;
} else if (stretch_aspect == "keep_width") {
sml_aspect = SceneTree::STRETCH_ASPECT_KEEP_WIDTH;
} else if (stretch_aspect == "keep_height") {
sml_aspect = SceneTree::STRETCH_ASPECT_KEEP_HEIGHT;
} else if (stretch_aspect == "expand") {
sml_aspect = SceneTree::STRETCH_ASPECT_EXPAND;
}
get_tree()->set_screen_stretch(sml_sm, sml_aspect, stretch_size, viewport_scale);
}
}
String Benchmark::_get_stats() {
static const char *_msaa[] = { "NONE", "2x", "4x", "8x", "16x" };
static const char *_draw[] = { "NORMAL", "UNSHADED", "OVERDRAW", "WIREFRAME" };
Vector<String> out;
int max_line = 0;
auto add_line = [&out, &max_line](const String &line) {
out.push_back(line);
max_line = MAX(line.size(), max_line);
};
add_line(vformat("%d VERTS/FRAME", 3 * num_objects * models[curr_model].tris));
add_line(vformat("+/-: OBJECT COUNT [%d]", num_objects));
add_line(vformat("X: OBJECT COMPLEXITY [%d TRIS]", models[curr_model].tris));
add_line(vformat("F: VIEWPORT [%d X %d]", int(get_viewport()->get_size().width), int(get_viewport()->get_size().height)));
add_line(vformat(" " + _BULLET3 + " WINDOW [%d X %d]", int(OS::get_singleton()->get_window_size().width), int(OS::get_singleton()->get_window_size().height)));
add_line(vformat(" " + _BULLET3 + " PROJECT [%d X %d]", int(ProjectSettings::get_singleton()->get("display/window/size/width")), int(ProjectSettings::get_singleton()->get("display/window/size/height"))));
add_line(vformat("P: MULTISAMPLING [%s]", _msaa[get_viewport()->get_msaa()]));
add_line(vformat("H: LIGHT MODEL [%s]", "?"));
add_line(vformat("I: TEXTURE FILTER [%s]", opts[OPTION_LINEAR_FILTERING] ? "LINEAR" : "NEAREST"));
add_line(vformat("M: MIPMAPS [%s]", opts[OPTION_MIPMAPING] ? "ON" : "OFF"));
add_line(vformat("C: BACKFACE CULLING [%s]", opts[OPTION_CULLING] ? "ON" : "OFF"));
add_line(vformat("D: DEPTH TEST [%s]", opts[OPTION_DEPTHTEST] ? "ON" : "OFF"));
add_line(vformat("D: DRAW [%s]", _draw[get_viewport()->get_debug_draw()]));
add_line(vformat("Z: CAMERA DISTANCE [%d]", int(camera_distance)));
add_line(vformat("Y: CAMERA YAW [%d]", int(camera_yaw)));
add_line(vformat("L: CAMERA LOOK [%s]", opts[OPTION_CAMERA_LOOKAWAY] ? "AWAY" : "TOWARDS"));
add_line(vformat("A: POSTPROCESS [%s]", "?"));
add_line("S: STATS DISPLAY [ON]");
String ret = _LT + _HL.repeat(max_line) + _RT + "\n";
for (const String &s : out) {
ret += _VL + s.rpad(max_line) + _VL + "\n";
}
ret += _LB + _HL.repeat(max_line) + _RB;
_stats_text_size = Size2(max_line + 2, ret.size());
return ret;
}
RID Benchmark::_create_instance_from_model(const ModelInfo &p_model) {
const int MESH_BASE_LAYER = 16;
const uint32_t layer = 1 << MESH_BASE_LAYER;
RID mesh_instance = RID_PRIME(VS::get_singleton()->instance_create());
VS::get_singleton()->instance_set_base(mesh_instance, p_model.mesh->get_rid());
VS::get_singleton()->instance_set_surface_material(mesh_instance, 0, model_mat->get_rid());
VS::get_singleton()->instance_set_scenario(mesh_instance, get_tree()->get_root()->get_world()->get_scenario());
VS::get_singleton()->instance_set_visible(mesh_instance, true);
VS::get_singleton()->instance_geometry_set_cast_shadows_setting(mesh_instance, VS::SHADOW_CASTING_SETTING_OFF);
VS::get_singleton()->instance_set_layer_mask(mesh_instance, layer);
VS::get_singleton()->instance_set_portal_mode(mesh_instance, VisualServer::INSTANCE_PORTAL_MODE_GLOBAL);
return mesh_instance;
}
int Benchmark::_get_texture_flags() {
int texture_opts = 0;
if (opts[OPTION_MIPMAPING]) {
texture_opts += Texture::FLAG_MIPMAPS;
}
if (opts[OPTION_LINEAR_FILTERING]) {
texture_opts += Texture::FLAG_FILTER;
}
return texture_opts;
}
Benchmark::ModelInfo Benchmark::_make_model_from_data(const uint8_t *p_data, const uint8_t *p_tex_img, size_t p_tex_img_size, const String &p_name) {
const uint32_t verts_num = *(uint32_t *)p_data;
struct _vert_t {
struct {
float x, y, z;
} pos;
struct {
float x, y, z;
} norm;
struct {
float u, v;
} uv;
} *geom = (_vert_t *)(p_data + 4);
const uint32_t verts_size = verts_num * sizeof(_vert_t);
const uint32_t indexes_num = *(uint32_t *)(p_data + 4 + verts_size);
const uint16_t *indexes = (uint16_t *)(p_data + 4 + verts_size + 4);
PoolVector3Array verts, norm;
PoolVector2Array uv;
verts.resize(verts_num);
norm.resize(verts_num);
uv.resize(verts_num);
auto _verts = verts.write();
auto _norm = norm.write();
auto _uv = uv.write();
for (int n = 0; n < verts_num; n++) {
_verts[n] = Vector3(geom[n].pos.x, geom[n].pos.y, geom[n].pos.z);
_norm[n] = Vector3(geom[n].norm.x, geom[n].norm.y, geom[n].norm.z);
_uv[n] = Vector2(geom[n].uv.u, geom[n].uv.v);
}
_uv.release();
_norm.release();
_verts.release();
print_verbose(vformat("Loading model data: %d vertexes, %d indexes, %d bytes of data.", verts_num, indexes_num, verts_size));
Ref<ArrayMesh> mesh = newref(ArrayMesh);
{
PoolIntArray index;
index.resize(indexes_num);
auto _index = index.write();
for (int i = 0; i < indexes_num; i++) {
_index[i] = indexes[i];
}
_index.release();
Array d;
d.resize(VS::ARRAY_MAX);
d[VisualServer::ARRAY_VERTEX] = verts;
d[VisualServer::ARRAY_NORMAL] = norm;
d[VisualServer::ARRAY_TEX_UV] = uv;
d[VisualServer::ARRAY_INDEX] = index;
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, d);
}
#if MODEL_WIREFRAME_SURFACE
// wireframe:
{
PoolIntArray index;
index.resize(indexes_num * 2);
auto _index = index.write();
_index[0] = indexes[0];
for (int i = 0; i < indexes_num - 1; i++) {
_index[i * 2 + 1] = _index[i * 2 + 2] = indexes[i + 1];
}
_index[indexes_num * 2 - 1] = indexes[indexes_num - 1];
_index.release();
Array d;
d.resize(VS::ARRAY_MAX);
d[VisualServer::ARRAY_VERTEX] = verts;
d[VisualServer::ARRAY_INDEX] = index;
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, d); // wireframe
}
#endif
String fn = "user://__benchmark_" + p_name; // cache mesh
ResourceSaver::save(fn + ".mesh", mesh);
return ModelInfo{ p_name, mesh, { p_tex_img, p_tex_img_size }, make_texture_from_data(p_tex_img, p_tex_img_size, _get_texture_flags(), p_name), indexes_num / 3 };
}
void Benchmark::_load_resources() {
static wchar_t _chars[] = {
' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
96, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 123, 124, 125, 126, 127,
128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159
};
screen_font = make_font_from_grid(_chars, 16, 8, bitmap_font_png_data, bitmap_font_png_size);
models[CUBE_MODEL] = _make_model_from_data(model_cube_dat_data, texture_cube_png_data, texture_cube_png_size, "cube");
models[FROG_MODEL] = _make_model_from_data(model_frog_dat_data, texture_frog_png_data, texture_frog_png_size, "frog");
models[KID_MODEL] = _make_model_from_data(model_kid_dat_data, texture_kid_png_data, texture_kid_png_size, "kid");
models[TREX_MODEL] = _make_model_from_data(model_trex_dat_data, texture_trex_png_data, texture_trex_png_size, "trex");
models[ROBOT_MODEL] = _make_model_from_data(model_robot_dat_data, texture_robot_png_data, texture_robot_png_size, "robot");
if (_stats_label) {
_stats_label->set("custom_fonts/font", screen_font);
}
}
void Benchmark::_finalize() {
// remove all objects
for (auto &instance : instances) {
if (instance.first.is_valid()) {
VS::get_singleton()->free(instance.first);
}
}
instances.clear();
}
void Benchmark::_check_instances() {
// check and update instances
while (num_objects > instances.size()) {
instances.push_back({ _create_instance_from_model(models[curr_model]), curr_model });
}
int objects = 0;
for (auto &instance : instances) {
if (instance.second != curr_model && objects < num_objects) {
VS::get_singleton()->instance_set_base(instance.first, models[curr_model].mesh->get_rid());
VS::get_singleton()->instance_set_surface_material(instance.first, 0, model_mat->get_rid());
instance.second = curr_model;
}
VS::get_singleton()->instance_set_visible(instance.first, objects < num_objects);
objects++;
}
}
void Benchmark::_update(float p_delta) {
ERR_FAIL_COND(num_objects > instances.size());
// calculate how many objects to draw per row
int row_size = 1;
while (row_size * row_size < num_objects) {
row_size++;
}
const real_t row_scale = 25;
const real_t row_step = num_objects == 1 ? row_scale : row_scale / (row_size - 1);
// draw the objects in a grid layout
int num_objects_drawn = 0;
for (int x = 0; x < row_size && num_objects_drawn < num_objects; x++) {
const float obj_x = (num_objects == 1) ? 0 : (1.5 * (-row_scale / 2.0 + x * row_step));
for (int y = 0; y < row_size && num_objects_drawn < num_objects; y++) {
const float obj_xs = obj_x + (y % 2) * row_step / 2.0;
const float obj_y = (num_objects == 1) ? 0 : (-row_scale / 2.0 + y * row_step);
// construct a yaw for this object, by combining the global yaw with the grid position
// calculate the model matrix from the yaw
const float yaw = glob_yaw + x * 40 + y * 50;
Transform xform = Transform(Basis(Vector3(0, 1, 0), yaw), Vector3(obj_xs, obj_y, 0));
// update
VisualServer::get_singleton()->instance_set_transform(instances[num_objects_drawn].first, xform);
num_objects_drawn++;
}
}
}
void Benchmark::_input(const Ref<InputEvent> &p_event) {
ERR_FAIL_COND(p_event.is_null());
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
if (!get_tree()) {
return;
}
ERR_FAIL_COND(!is_inside_tree());
if (const InputEventKey *kb = cast_to<InputEventKey>(*p_event)) {
if ((kb->get_scancode() == KEY_PLUS || kb->get_scancode() == KEY_EQUAL) && kb->is_pressed()) {
set_num_objects(num_objects + 1);
return;
}
if (kb->get_scancode() == KEY_MINUS && kb->is_pressed() && num_objects > 0) {
set_num_objects(num_objects - 1);
return;
}
if (kb->get_scancode() == KEY_S && kb->is_pressed()) {
set_display_option(OPTION_STATS, !opts[OPTION_STATS]);
return;
}
if (kb->get_scancode() == KEY_X && kb->is_pressed()) {
if (curr_model + 1 == NUM_MODEL_TYPES) {
set_display_model(FIRST_MODEL_TYPE);
} else {
set_display_model(ModelType(curr_model + 1));
}
return;
}
}
}
void Benchmark::_notification(int p_notification) {
switch (p_notification) {
case NOTIFICATION_READY: {
_load_resources();
} break;
case NOTIFICATION_ENTER_TREE: {
_sky = get_tree()->get_root()->get_world()->get_fallback_environment();
if (!_stats_label) {
_stats_label = memnew(Label);
_stats_label->set_vertical_spacing(-3);
add_child(_stats_label);
}
for (const Performance::Monitor &mon : make_vector(Performance::TIME_FPS, Performance::MEMORY_STATIC)) {
if (!_monitors[mon]) {
_monitors[mon] = memnew(GdHistoryPlot);
_monitors[mon]->set_title_label(Performance::get_singleton()->get_monitor_name(mon));
add_child(_monitors[mon]);
}
}
_monitors[Performance::MEMORY_STATIC]->set_humanize_value(true);
if (active) {
set_process_input(true);
set_process_internal(true);
}
} break;
case NOTIFICATION_EXIT_TREE: {
_finalize();
_stats_label = nullptr;
_monitors[Performance::TIME_FPS] = nullptr;
_monitors.clear();
set_process_input(false);
set_process_internal(true);
} break;
case NOTIFICATION_INTERNAL_PROCESS: {
static float _plot_update = 0;
const float delta = get_process_delta_time();
if (_dirty) {
_update_viewport();
if (!models[curr_model].tex) { // rebuild texture
make_texture_from_data(models[curr_model].tex_data.data, models[curr_model].tex_data.size, _get_texture_flags(), models[curr_model].name);
}
_check_instances();
get_tree()->get_root()->get_world()->set_fallback_environment(opts[OPTION_SKY] ? _sky : Ref<Environment>());
model_mat->set_cull_mode(opts[OPTION_CULLING] ? SpatialMaterial::CULL_BACK : SpatialMaterial::CULL_DISABLED);
model_mat->set_flag(SpatialMaterial::FLAG_DISABLE_DEPTH_TEST, !opts[OPTION_DEPTHTEST]);
model_mat->set_texture(SpatialMaterial::TEXTURE_ALBEDO, opts[OPTION_TEXTURING] ? models[curr_model].tex : Ref<Texture>());
if (_stats_label) {
_stats_label->set_visible(opts[OPTION_STATS]);
}
_dirty = false;
}
glob_yaw += delta;
_update(delta);
if (_stats_label) {
_stats_label->set_text(_get_stats());
}
const Size2 stats_size = _stats_text_size * Size2(16, 16);
const int _monitor_height = 50, _monitor_space = 4;
const Size2 _monitor_size = Size2(
(Engine::get_singleton()->is_editor_hint() ? real_t(ProjectSettings::get_singleton()->get("display/window/size/width")) : get_viewport()->get_size().width) - stats_size.width - 2, _monitor_height);
_plot_update += delta;
if (_monitors[Performance::TIME_FPS]) {
static real_t _accum0 = 0;
static int _accum_count0 = 0;
_monitors[Performance::TIME_FPS]->set_size(_monitor_size);
_monitors[Performance::TIME_FPS]->set_position(Point2(stats_size.width, _monitor_space));
_accum0 += Performance::get_singleton()->get_monitor(Performance::TIME_FPS);
_accum_count0++;
if (_plot_update > 0.5) {
_monitors[Performance::TIME_FPS]->add_sample(_accum0 / _accum_count0);
_accum0 = _accum_count0 = 0;
}
}
if (_monitors[Performance::MEMORY_STATIC]) {
static real_t _accum1 = 0;
static int _accum_count1 = 0;
_monitors[Performance::MEMORY_STATIC]->set_size(_monitor_size);
_monitors[Performance::MEMORY_STATIC]->set_position(Point2(stats_size.width, _monitor_height + 2 * _monitor_space));
_accum1 += Performance::get_singleton()->get_monitor(Performance::MEMORY_STATIC);
_accum_count1++;
if (_plot_update > 0.5) {
_monitors[Performance::MEMORY_STATIC]->add_sample(_accum1 / _accum_count1);
_accum1 = _accum_count1 = 0;
}
}
if (_plot_update > 0.5) {
_plot_update = 0;
}
} break;
}
}
void Benchmark::set_active(bool p_state) {
active = p_state;
if (is_inside_tree()) {
set_process_internal(active);
set_process_input(active);
}
}
bool Benchmark::is_active() const {
return active;
}
void Benchmark::set_num_objects(int p_num) {
ERR_FAIL_COND(p_num < 1);
num_objects = p_num;
_dirty = true;
}
int Benchmark::get_num_objects() const {
return num_objects;
}
void Benchmark::set_display_model(int p_model) {
ERR_FAIL_INDEX(p_model, NUM_MODEL_TYPES);
curr_model = p_model;
_dirty = true;
}
int Benchmark::get_display_model() const {
return curr_model;
}
void Benchmark::set_display_option(int p_opt, bool p_state) {
ERR_FAIL_INDEX(p_opt, NUM_OPTIONS);
if (opts[p_opt] != p_state) {
opts[p_opt] = p_state;
if (p_opt == OPTION_LINEAR_FILTERING || p_opt == OPTION_MIPMAPING) {
// invalidate texture for models
for (int m = 0; m < NUM_MODEL_TYPES; m++) {
models[m].tex = Ref<Texture>();
}
}
_dirty = true;
}
}
bool Benchmark::get_display_option(int p_opt) const {
ERR_FAIL_INDEX_V(p_opt, NUM_OPTIONS, false);
return opts[p_opt];
}
void Benchmark::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_active"), &Benchmark::set_active);
ClassDB::bind_method(D_METHOD("is_active"), &Benchmark::is_active);
ClassDB::bind_method(D_METHOD("set_num_objects"), &Benchmark::set_num_objects);
ClassDB::bind_method(D_METHOD("get_num_objects"), &Benchmark::get_num_objects);
ClassDB::bind_method(D_METHOD("set_display_model"), &Benchmark::set_display_model);
ClassDB::bind_method(D_METHOD("get_display_model"), &Benchmark::get_display_model);
ClassDB::bind_method(D_METHOD("get_display_option"), &Benchmark::get_display_option);
ClassDB::bind_method(D_METHOD("set_display_option"), &Benchmark::set_display_option);
ClassDB::bind_method(D_METHOD("_input"), &Benchmark::_input);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "active"), "set_active", "is_active");
ADD_PROPERTY(PropertyInfo(Variant::INT, "num_objects"), "set_num_objects", "get_num_objects");
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_model", PROPERTY_HINT_ENUM, "Cube,Frog,Kid,TRex,Robot"), "set_display_model", "get_display_model");
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "option_depthtest"), "set_display_option", "get_display_option", OPTION_DEPTHTEST);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "option_fxaa"), "set_display_option", "get_display_option", OPTION_FXAA);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "option_culling"), "set_display_option", "get_display_option", OPTION_CULLING);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "option_mipmaping"), "set_display_option", "get_display_option", OPTION_MIPMAPING);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "option_linear_filtering"), "set_display_option", "get_display_option", OPTION_LINEAR_FILTERING);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "option_texturing"), "set_display_option", "get_display_option", OPTION_TEXTURING);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "option_sky"), "set_display_option", "get_display_option", OPTION_SKY);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "option_stats"), "set_display_option", "get_display_option", OPTION_STATS);
BIND_ENUM_CONSTANT(CUBE_MODEL);
BIND_ENUM_CONSTANT(FROG_MODEL);
BIND_ENUM_CONSTANT(KID_MODEL);
BIND_ENUM_CONSTANT(TREX_MODEL);
BIND_ENUM_CONSTANT(ROBOT_MODEL);
BIND_ENUM_CONSTANT(OPTION_CULLING);
BIND_ENUM_CONSTANT(OPTION_DEPTHTEST);
BIND_ENUM_CONSTANT(OPTION_FXAA);
BIND_ENUM_CONSTANT(OPTION_TEXTURING);
BIND_ENUM_CONSTANT(OPTION_LINEAR_FILTERING);
BIND_ENUM_CONSTANT(OPTION_MIPMAPING);
BIND_ENUM_CONSTANT(OPTION_CAMERA_LOOKAWAY);
BIND_ENUM_CONSTANT(OPTION_STATS);
BIND_ENUM_CONSTANT(OPTION_SKY);
}
Benchmark::Benchmark() {
_dirty = true;
_stats_label = nullptr;
_monitors[Performance::TIME_FPS] = _monitors[Performance::MEMORY_STATIC] = nullptr;
_stats_text_size = Size2(0, 0);
active = false;
plot_hist_size = 100;
plot_buf_index = 0;
num_objects = 20;
curr_model = 0;
glob_yaw = 0;
viewport_scale = GLOBAL_DEF("display/window/stretch/shrink", 1.0);
camera_distance = 0, camera_yaw = 0;
opts[OPTION_STATS] = true;
opts[OPTION_LINEAR_FILTERING] = true;
opts[OPTION_DEPTHTEST] = true;
opts[OPTION_TEXTURING] = true;
opts[OPTION_SKY] = true;
model_mat = newref(SpatialMaterial);
}
Benchmark::~Benchmark() {
_finalize(); // should not be needed
}