mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
profiler format (#5607)
* profiler format * profiler update * profiler update * profiler scheduler add * profiler update optimize
This commit is contained in:
@@ -466,18 +466,18 @@ export default [
|
||||
{
|
||||
"name": "util/profiler",
|
||||
"techniques": [
|
||||
{"passes":[{"blendState":{"targets":[{"blend":true, "blendSrc":2, "blendDst":4, "blendDstAlpha":4}]}, "rasterizerState":{"cullMode":0}, "program":"util/profiler|profiler-vs:vert|profiler-fs:frag", "depthStencilState":{"depthTest":false, "depthWrite":false}, "properties":{"mainTexture":{"value":"grey", "type":28}, "offset":{"type":16}}}]}
|
||||
{"passes":[{"blendState":{"targets":[{"blend":true, "blendSrc":2, "blendDst":4, "blendDstAlpha":4}]}, "rasterizerState":{"cullMode":0}, "program":"util/profiler|profiler-vs:vert|profiler-fs:frag", "depthStencilState":{"depthTest":false, "depthWrite":false}}]}
|
||||
],
|
||||
"shaders": [
|
||||
{
|
||||
"name": "util/profiler|profiler-vs:vert|profiler-fs:frag",
|
||||
"hash": 3710757766,
|
||||
"hash": 3801970600,
|
||||
"glsl3": {
|
||||
"vert": `\nprecision mediump float;\nuniform CCGlobal {\n vec4 cc_time;\n vec4 cc_screenSize;\n vec4 cc_screenScale;\n vec4 cc_nativeSize;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos;\n vec4 cc_exposure;\n vec4 cc_mainLitDir;\n vec4 cc_mainLitColor;\n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nin vec3 a_position;\nvoid CCDecode (out vec4 position) {\n position = vec4(a_position, 1.0);\n}\nin vec2 a_texCoord;\nout vec2 v_uv;\nuniform Constants {\n vec4 offset;\n};\nvec4 vert () {\n vec4 position;\n CCDecode(position);\n position.x *= cc_screenSize.y * cc_screenSize.z;\n position.xy += offset.xy + abs(position.xy);\n v_uv = a_texCoord;\n return position;\n}\nvoid main() { gl_Position = vert(); }\n`,
|
||||
"vert": `\nprecision mediump float;\nuniform CCGlobal {\n vec4 cc_time;\n vec4 cc_screenSize;\n vec4 cc_screenScale;\n vec4 cc_nativeSize;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos;\n vec4 cc_exposure;\n vec4 cc_mainLitDir;\n vec4 cc_mainLitColor;\n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nin vec3 a_position;\nvoid CCDecode (out vec4 position) {\n position = vec4(a_position, 1.0);\n}\nin vec4 a_color;\nout vec2 v_uv;\nuniform Constants {\n vec4 offset;\n vec4 symbols[4];\n};\nuniform PerFrameInfo {\n vec4 digits[16 * 9 / 4];\n};\nfloat getComponent(vec4 v, float i) {\n if (i < 1.0) { return v[0]; }\n else if (i < 2.0) { return v[1]; }\n else if (i < 3.0) { return v[2]; }\n else { return v[3]; }\n}\nvec4 vert () {\n vec4 position;\n CCDecode(position);\n position.x *= cc_screenSize.y * cc_screenSize.z;\n position.xy += offset.xy + abs(position.xy);\n if(a_color.z < 0.0){\n v_uv = a_color.xy;\n }\n else{\n float n = getComponent(digits[int(a_color.z)], a_color.w);\n float row = floor(n / 4.0), col = n - row * 4.0;\n v_uv = a_color.xy + vec2(getComponent(symbols[int(row)], col), 0.0);\n }\n return position;\n}\nvoid main() { gl_Position = vert(); }\n`,
|
||||
"frag": `\nprecision mediump float;\nuniform CCGlobal {\n vec4 cc_time;\n vec4 cc_screenSize;\n vec4 cc_screenScale;\n vec4 cc_nativeSize;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos;\n vec4 cc_exposure;\n vec4 cc_mainLitDir;\n vec4 cc_mainLitColor;\n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nvec3 SRGBToLinear(vec3 gamma)\n{\n return pow(gamma, vec3(2.2));\n}\nvec3 LinearToSRGB(vec3 linear)\n{\n return pow(linear, vec3(0.454545));\n}\nvec4 CCFragOutput(vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nin vec2 v_uv;\nuniform sampler2D mainTexture;\nvec4 frag () {\n return CCFragOutput(texture(mainTexture, v_uv));\n}\nout vec4 cc_FragColor;\nvoid main() { cc_FragColor = frag(); }\n`
|
||||
},
|
||||
"glsl1": {
|
||||
"vert": `\nprecision mediump float;\nuniform vec4 cc_screenSize;\nattribute vec3 a_position;\nvoid CCDecode (out vec4 position) {\n position = vec4(a_position, 1.0);\n}\nattribute vec2 a_texCoord;\nvarying vec2 v_uv;\nuniform vec4 offset;\nvec4 vert () {\n vec4 position;\n CCDecode(position);\n position.x *= cc_screenSize.y * cc_screenSize.z;\n position.xy += offset.xy + abs(position.xy);\n v_uv = a_texCoord;\n return position;\n}\nvoid main() { gl_Position = vert(); }\n`,
|
||||
"vert": `\nprecision mediump float;\nuniform vec4 cc_screenSize;\nattribute vec3 a_position;\nvoid CCDecode (out vec4 position) {\n position = vec4(a_position, 1.0);\n}\nattribute vec4 a_color;\nvarying vec2 v_uv;\nuniform vec4 offset;\nuniform vec4 symbols[4];\nuniform vec4 digits[36];\nfloat getComponent(vec4 v, float i) {\n if (i < 1.0) { return v[0]; }\n else if (i < 2.0) { return v[1]; }\n else if (i < 3.0) { return v[2]; }\n else { return v[3]; }\n}\nvec4 vert () {\n vec4 position;\n CCDecode(position);\n position.x *= cc_screenSize.y * cc_screenSize.z;\n position.xy += offset.xy + abs(position.xy);\n if(a_color.z < 0.0){\n v_uv = a_color.xy;\n }\n else{\n float n = getComponent(digits[int(a_color.z)], a_color.w);\n float row = floor(n / 4.0), col = n - row * 4.0;\n v_uv = a_color.xy + vec2(getComponent(symbols[int(row)], col), 0.0);\n }\n return position;\n}\nvoid main() { gl_Position = vert(); }\n`,
|
||||
"frag": `\nprecision mediump float;\nuniform vec4 cc_exposure;\nvec3 SRGBToLinear(vec3 gamma)\n{\n return pow(gamma, vec3(2.2));\n}\nvec3 LinearToSRGB(vec3 linear)\n{\n return pow(linear, vec3(0.454545));\n}\nvec4 CCFragOutput(vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nvarying vec2 v_uv;\nuniform sampler2D mainTexture;\nvec4 frag () {\n return CCFragOutput(texture2D(mainTexture, v_uv));\n}\nvoid main() { gl_FragColor = frag(); }\n`
|
||||
},
|
||||
"builtins": {"globals":{"blocks":[{"name":"CCGlobal", "defines":[]}], "samplers":[]}, "locals":{"blocks":[], "samplers":[]}},
|
||||
@@ -486,7 +486,11 @@ export default [
|
||||
],
|
||||
"blocks": [
|
||||
{"name": "Constants", "defines": [], "binding": 0, "members": [
|
||||
{"name":"offset", "type":16, "count":1}
|
||||
{"name":"offset", "type":16, "count":1},
|
||||
{"name":"symbols", "type":16, "count":4}
|
||||
]},
|
||||
{"name": "PerFrameInfo", "defines": [], "binding": 1, "members": [
|
||||
{"name":"digits", "type":16, "count":36}
|
||||
]}
|
||||
],
|
||||
"samplers": [
|
||||
|
||||
@@ -114,7 +114,7 @@ const getBindingFromHandle = (handle: number) => (handle & bindingMask) >>> 14;
|
||||
const getOffsetFromHandle = (handle: number) => (handle & offsetMask);
|
||||
const customizeType = (handle: number, type: GFXType) => (handle & ~typeMask) | ((type << 22) & typeMask);
|
||||
|
||||
interface IBlock {
|
||||
export interface IBlock {
|
||||
buffer: ArrayBuffer;
|
||||
view: Float32Array;
|
||||
dirty: boolean;
|
||||
@@ -676,6 +676,7 @@ export class Pass {
|
||||
Object.assign(this._dss, info.depthStencilState);
|
||||
}
|
||||
|
||||
get blocks () { return this._blocks; }
|
||||
get device () { return this._device; }
|
||||
get idxInTech () { return this._idxInTech; }
|
||||
get programName () { return this._programName; }
|
||||
|
||||
@@ -33,6 +33,7 @@ import { GFXDevice } from '../../gfx/device';
|
||||
import { GFXTexture } from '../../gfx/texture';
|
||||
import { GFXTextureView } from '../../gfx/texture-view';
|
||||
import { Vec4 } from '../../math';
|
||||
import { IBlock } from '../../renderer/core/pass';
|
||||
import { Layers } from '../../scene-graph';
|
||||
import { Node } from '../../scene-graph/node';
|
||||
import { ICounterOption } from './counter';
|
||||
@@ -51,17 +52,17 @@ interface IProfilerState {
|
||||
bufferMemory: ICounterOption;
|
||||
}
|
||||
|
||||
const characters = '0123456789. ';
|
||||
|
||||
export class Profiler {
|
||||
|
||||
public _stats: IProfilerState | null = null;
|
||||
public id = '__Profiler__';
|
||||
|
||||
private _showFPS = false;
|
||||
private readonly _fontSize = 22;
|
||||
private readonly _fontSize = 24;
|
||||
private readonly _lineHeight = this._fontSize + 2;
|
||||
private readonly _left = 10;
|
||||
private readonly _right = 10;
|
||||
private readonly _top = 10;
|
||||
private readonly _bottom = 10;
|
||||
private _wordHeight = 0;
|
||||
|
||||
private _rootNode: Node | null = null;
|
||||
private _device: GFXDevice | null = null;
|
||||
@@ -72,9 +73,37 @@ export class Profiler {
|
||||
private readonly _region: GFXBufferTextureCopy = new GFXBufferTextureCopy();
|
||||
private readonly _canvasArr: HTMLCanvasElement[] = [];
|
||||
private readonly _regionArr = [this._region];
|
||||
private digitsData: IBlock = null!;
|
||||
|
||||
private _canvasDone = false;
|
||||
private _statsDone = false;
|
||||
private _inited = false;
|
||||
|
||||
private _rowNumber = 9;
|
||||
private _columnNumber = 8;
|
||||
|
||||
private _posWordWidth = 0.18; // profiler left side width
|
||||
private _posBaseHeight = 0.18; // profiler left side height
|
||||
private _posNumWidth = 0.09; // profiler right side width
|
||||
private _eachNumWidth = 0; // profiler each number width
|
||||
|
||||
private lastTime = 0; // update use time
|
||||
|
||||
private _string2offset = {
|
||||
'0': 0,
|
||||
'1': 1,
|
||||
'2': 2,
|
||||
'3': 3,
|
||||
'4': 4,
|
||||
'5': 5,
|
||||
'6': 6,
|
||||
'7': 7,
|
||||
'8': 8,
|
||||
'9': 9,
|
||||
'.': 10,
|
||||
};
|
||||
|
||||
private _uvOffset: Vec4[] = [];
|
||||
|
||||
constructor () {
|
||||
if (!CC_TEST) {
|
||||
@@ -141,8 +170,8 @@ export class Profiler {
|
||||
return;
|
||||
}
|
||||
|
||||
const textureWidth = 350;
|
||||
const textureHeight = 200;
|
||||
const textureWidth = 256;
|
||||
const textureHeight = 256;
|
||||
|
||||
if (!this._ctx || !this._canvas) {
|
||||
return;
|
||||
@@ -177,11 +206,12 @@ export class Profiler {
|
||||
}
|
||||
|
||||
public generateStats () {
|
||||
if (this._statsDone || !this._ctx) {
|
||||
if (this._statsDone || !this._ctx || !this._canvas ) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._stats = null;
|
||||
this._inited = false;
|
||||
const now = performance.now();
|
||||
|
||||
const opts = {
|
||||
@@ -198,14 +228,35 @@ export class Profiler {
|
||||
|
||||
this._ctx.textAlign = 'left';
|
||||
let i = 0;
|
||||
for (const id of Object.keys(opts)) {
|
||||
this._ctx.fillText(opts[id].desc, this._left, this._top + i * this._lineHeight);
|
||||
opts[id].counter = new PerfCounter(id, opts[id], now);
|
||||
for (const id in opts) {
|
||||
const element = opts[id];
|
||||
this._ctx.fillText(element.desc, 0, i * this._lineHeight);
|
||||
element.counter = new PerfCounter(id, element, now);
|
||||
i++;
|
||||
}
|
||||
this._ctx.textAlign = 'end';
|
||||
this._wordHeight = i * this._lineHeight / this._canvas.height;
|
||||
|
||||
this._eachNumWidth = this._ctx.measureText('0').width / this._canvas.width; // each number uv width
|
||||
const canvasNumWidth = this._eachNumWidth * this._canvas.width; // each number width in canvas
|
||||
|
||||
const offsets = new Array();
|
||||
let offset = 0;
|
||||
offsets[0] = 0;
|
||||
for (let j = 0; j < characters.length; ++j) {
|
||||
this._ctx.fillText(characters[j], j * canvasNumWidth, i * this._lineHeight);
|
||||
offset += this._eachNumWidth;
|
||||
offsets[j + 1] = offset; // cause offsets[0] = 0
|
||||
}
|
||||
|
||||
const len = Math.ceil(offsets.length / 4);
|
||||
for (let j = 0; j < len; j++) {
|
||||
this._uvOffset.push(new Vec4(offsets[j * 4], offsets[j * 4 + 1], offsets[j * 4 + 2], offsets[j * 4 + 3]));
|
||||
}
|
||||
|
||||
this._stats = opts as IProfilerState;
|
||||
this._canvasArr[0] = this._canvas;
|
||||
this.updateTexture();
|
||||
this._inited = true;
|
||||
}
|
||||
|
||||
public generateNode () {
|
||||
@@ -231,24 +282,48 @@ export class Profiler {
|
||||
const managerNode = new Node('Profiler_Root');
|
||||
managerNode.parent = this._rootNode;
|
||||
|
||||
const columnWidth = this._posNumWidth / this._columnNumber;
|
||||
const rowHeight = this._posBaseHeight / this._rowNumber;
|
||||
const vertexPos: number[] = [
|
||||
0, this._posBaseHeight, 0, // top-left
|
||||
this._posBaseHeight, this._posBaseHeight, 0, // top-right
|
||||
this._posWordWidth, 0, 0, // bottom-right
|
||||
0, 0, 0, // bottom-left
|
||||
];
|
||||
const vertexindices: number[] = [
|
||||
0, 2, 1,
|
||||
0, 3, 2,
|
||||
];
|
||||
const vertexUV: number[] = [
|
||||
0, 0, -1, 0,
|
||||
1, 0, -1, 0,
|
||||
1, this._wordHeight, -1, 0,
|
||||
0, this._wordHeight, -1, 0,
|
||||
];
|
||||
let offset;
|
||||
for (let i = 0; i < this._rowNumber; i++) {
|
||||
for (let j = 0; j < this._columnNumber; j++) {
|
||||
vertexPos.push(this._posWordWidth + j * columnWidth, this._posBaseHeight - i * rowHeight, 0 ); // 0xyz
|
||||
vertexPos.push(this._posWordWidth + (j + 1) * columnWidth, this._posBaseHeight - i * rowHeight, 0); // 1xyz
|
||||
vertexPos.push(this._posWordWidth + (j + 1) * columnWidth, this._posBaseHeight - (i + 1) * rowHeight, 0); // 2xyz
|
||||
vertexPos.push(this._posWordWidth + j * columnWidth, this._posBaseHeight - (i + 1) * rowHeight, 0); // 3xyz
|
||||
offset = (i * this._columnNumber + j + 1) * 4;
|
||||
vertexindices.push(0 + offset, 2 + offset, 1 + offset, 0 + offset, 3 + offset, 2 + offset);
|
||||
const idx = i * this._columnNumber + j;
|
||||
const z = Math.floor(idx / 4);
|
||||
const w = idx - z * 4;
|
||||
vertexUV.push(0, this._wordHeight, z, w ); // 0uvindex
|
||||
vertexUV.push(this._eachNumWidth, this._wordHeight, z, w ); // 1uvindex
|
||||
vertexUV.push(this._eachNumWidth, 1, z, w ); // 2uvindex
|
||||
vertexUV.push(0, 1, z, w ); // 3uvindex
|
||||
}
|
||||
}
|
||||
|
||||
const modelCom = managerNode.addComponent('cc.ModelComponent') as ModelComponent;
|
||||
modelCom.mesh = createMesh({
|
||||
positions: [
|
||||
-0.35, -0.2, 0, // bottom-left
|
||||
-0.35, 0.2, 0, // top-left
|
||||
0.35, 0.2, 0, // top-right
|
||||
0.35, -0.2, 0, // bottom-right
|
||||
],
|
||||
indices: [
|
||||
0, 2, 1,
|
||||
0, 3, 2,
|
||||
],
|
||||
uvs: [
|
||||
0, 1,
|
||||
0, 0,
|
||||
1, 0,
|
||||
1, 1,
|
||||
],
|
||||
positions : vertexPos,
|
||||
indices: vertexindices,
|
||||
colors: vertexUV, // use colors,actually x is u,y is v,z is index
|
||||
});
|
||||
|
||||
const _material = new Material();
|
||||
@@ -256,10 +331,14 @@ export class Profiler {
|
||||
effectName: 'util/profiler',
|
||||
});
|
||||
_material.setProperty('offset', new Vec4(-0.9, -0.9, 0, 0));
|
||||
_material.setProperty('symbols', this._uvOffset);
|
||||
const pass = _material.passes[0];
|
||||
const handle = pass.getBinding('mainTexture');
|
||||
pass.bindTextureView(handle!, this._textureView!);
|
||||
|
||||
const binding = pass.getBinding('digits')!;
|
||||
this.digitsData = pass.blocks[binding];
|
||||
|
||||
modelCom.material = _material;
|
||||
modelCom.node.layer = Layers.Enum.PROFILER;
|
||||
}
|
||||
@@ -318,34 +397,42 @@ export class Profiler {
|
||||
}
|
||||
|
||||
public afterDraw () {
|
||||
if (!this._stats || !this._ctx || !this._canvas) {
|
||||
if (!this._stats || !this._inited) {
|
||||
return;
|
||||
}
|
||||
const now = performance.now();
|
||||
|
||||
this.getCounter('fps').frame(now);
|
||||
this.getCounter('draws').value = this._device!.numDrawCalls;
|
||||
this.getCounter('bufferMemory').value = this._device!.memoryStatus.bufferSize / (1024 * 1024);
|
||||
this.getCounter('textureMemory').value = this._device!.memoryStatus.textureSize / (1024 * 1024);
|
||||
this.getCounter('tricount').value = this._device!.numTris;
|
||||
this.getCounter('render').end(now);
|
||||
|
||||
const x = this._left + this._ctx.measureText('GFX Texture Mem(M)').width;
|
||||
this._ctx.clearRect( x, 0, this._canvas.width - x, this._canvas.height);
|
||||
if (now - this.lastTime < 500) {
|
||||
return;
|
||||
}
|
||||
this.lastTime = now;
|
||||
|
||||
const device = this._device!;
|
||||
this.getCounter('draws').value = device.numDrawCalls;
|
||||
this.getCounter('bufferMemory').value = device.memoryStatus.bufferSize / (1024 * 1024);
|
||||
this.getCounter('textureMemory').value = device.memoryStatus.textureSize / (1024 * 1024);
|
||||
this.getCounter('tricount').value = device.numTris;
|
||||
|
||||
let i = 0;
|
||||
for (const id of Object.keys(this._stats)) {
|
||||
const view = this.digitsData.view;
|
||||
for (const id in this._stats) {
|
||||
const stat = this._stats[id];
|
||||
stat.counter.sample(now);
|
||||
this._ctx.fillText(
|
||||
stat.counter.human(!(stat.desc === 'Framerate (FPS)')),
|
||||
this._canvas.width - this._right,
|
||||
this._top + i * this._lineHeight);
|
||||
const result = stat.counter.human(!(stat.desc === 'Framerate (FPS)')).toString();
|
||||
for (let j = this._columnNumber - 1; j >= 0; j--) {
|
||||
const index = i * this._columnNumber + j;
|
||||
const character = result[result.length - (this._columnNumber - j)];
|
||||
let offset = this._string2offset[character];
|
||||
if (offset === undefined) { offset = 11; }
|
||||
view[index] = offset;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
this._canvasArr[0] = this._canvas;
|
||||
this.updateTexture();
|
||||
this.digitsData.dirty = true;
|
||||
}
|
||||
|
||||
public getCounter (s: string) {
|
||||
|
||||
Reference in New Issue
Block a user