[engine] 增加高级性能指示器

This commit is contained in:
SmallMain 2024-12-12 20:59:21 +08:00
parent a4ec2b24dd
commit 865e992cb7
No known key found for this signature in database
2 changed files with 95 additions and 2 deletions

View File

@ -386,7 +386,19 @@ cc.macro = {
* @property {Boolean} ENABLE_NATIVE_TTF_RENDERER * @property {Boolean} ENABLE_NATIVE_TTF_RENDERER
* @default false * @default false
*/ */
ENABLE_NATIVE_TTF_RENDERER: false ENABLE_NATIVE_TTF_RENDERER: false,
/**
* !#en
* Enable advanced performance indicators, allowing for customization and display of more performance metrics.
*
* !#zh
* 启用高级性能指示器允许自定义和显示更多性能指标
*
* @property {Boolean} ENABLE_CUSTOM_PROFILER
* @default false
*/
ENABLE_CUSTOM_PROFILER: false,
}; };

View File

@ -36,7 +36,7 @@ let _rootNode = null;
let _label = null; let _label = null;
function generateStats () { function generateStats () {
if (_stats) return; if (_stats && !cc.macro.ENABLE_CUSTOM_PROFILER) return;
_stats = { _stats = {
fps: { desc: 'Framerate (FPS)', below: 30, average: 500 }, fps: { desc: 'Framerate (FPS)', below: 30, average: 500 },
@ -47,10 +47,87 @@ function generateStats () {
mode: { desc: cc.game.renderType === cc.game.RENDER_TYPE_WEBGL ? 'WebGL' : 'Canvas', min: 1 } mode: { desc: cc.game.renderType === cc.game.RENDER_TYPE_WEBGL ? 'WebGL' : 'Canvas', min: 1 }
}; };
if (cc.macro.ENABLE_CUSTOM_PROFILER) {
delete _stats["mode"];
}
let now = performance.now(); let now = performance.now();
for (let id in _stats) { for (let id in _stats) {
_stats[id]._counter = new PerfCounter(id, _stats[id], now); _stats[id]._counter = new PerfCounter(id, _stats[id], now);
} }
if (cc.macro.ENABLE_CUSTOM_PROFILER) {
if (cc.Label) {
if (cc.profiler.showLabelCanvasCounter) {
_stats.label_canvas = {
desc: 'Label Canvas',
_counter: {
sample(now) {
},
human() {
const used = cc.Label._canvasPool.used;
const surplus = cc.Label._canvasPool.pool.length;
return `${used} / ${surplus + used}`;
}
},
};
}
if (cc.profiler.showLabelCharAtlasCounter) {
_stats.label_atlas = {
desc: 'Char Atlas',
_counter: {
sample(now) {
},
human() {
const atlases = cc.Label._shareAtlas.atlases;
let used = 0;
let usedLess = 0;
for (const atlas of atlases) {
const max = atlas._width * atlas._height;
let _used = atlas._width * atlas._nexty;
for (const area of atlas.frees) {
_used -= area._width * area._height;
}
let _usedLess = _used;
for (const area of atlas.waitCleans) {
if (area.ref === 0) {
_usedLess -= area._width * area._height;
}
}
used += _used / max;
usedLess += _usedLess / max;
}
return `${(usedLess / atlases.length).toFixed(2)} / ${(used / atlases.length).toFixed(2)} / ${atlases.length}`;
}
},
};
}
}
if (cc.profiler.showDynamicAtlasCounter) {
_stats.dynamic_atlas = {
desc: 'Dynamic Atlas',
_counter: {
sample(now) {
},
human() {
const atlases = cc.dynamicAtlasManager.atlases;
const max = cc.dynamicAtlasManager.maxAtlasCount;
const curLess = cc.dynamicAtlasManager.atlasCount;
const oneOfMax = cc.dynamicAtlasManager.textureSize * cc.dynamicAtlasManager.textureSize;
let cur = 0;
for (const key in cc.dynamicAtlasManager.rects) {
const rect = cc.dynamicAtlasManager.rects[key];
cur += rect.sizes;
}
return `${(cur / oneOfMax).toFixed(2)} / ${curLess} / ${max}`;
}
},
};
}
}
} }
function generateNode () { function generateNode () {
@ -144,6 +221,10 @@ function afterDraw () {
} }
cc.profiler = module.exports = { cc.profiler = module.exports = {
showLabelCanvasCounter: true,
showLabelCharAtlasCounter: true,
showDynamicAtlasCounter: true,
isShowingStats () { isShowingStats () {
return _showFPS; return _showFPS;
}, },