Skip to content

Commit 44ac2ff

Browse files
authored
Merge pull request #9004 from CesiumGS/webgl2-slow-inspector
Fix slow debug inspector
2 parents 5e0cce6 + 6a51c2d commit 44ac2ff

File tree

3 files changed

+163
-125
lines changed

3 files changed

+163
-125
lines changed

Source/Scene/DebugInspector.js

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import Color from "../Core/Color.js";
2+
import DrawCommand from "../Renderer/DrawCommand.js";
3+
import ShaderSource from "../Renderer/ShaderSource.js";
4+
import ShaderProgram from "../Renderer/ShaderProgram.js";
5+
import defined from "../Core/defined.js";
6+
7+
/**
8+
* @private
9+
*/
10+
function DebugInspector() {
11+
this._cachedShowFrustumsShaders = {};
12+
}
13+
14+
function getAttributeLocations(shaderProgram) {
15+
var attributeLocations = {};
16+
var attributes = shaderProgram.vertexAttributes;
17+
for (var a in attributes) {
18+
if (attributes.hasOwnProperty(a)) {
19+
attributeLocations[a] = attributes[a].index;
20+
}
21+
}
22+
23+
return attributeLocations;
24+
}
25+
26+
function createDebugShowFrustumsShaderProgram(scene, shaderProgram) {
27+
var context = scene.context;
28+
var sp = shaderProgram;
29+
var fs = sp.fragmentShaderSource.clone();
30+
31+
var targets = [];
32+
fs.sources = fs.sources.map(function (source) {
33+
source = ShaderSource.replaceMain(source, "czm_Debug_main");
34+
var re = /gl_FragData\[(\d+)\]/g;
35+
var match;
36+
while ((match = re.exec(source)) !== null) {
37+
if (targets.indexOf(match[1]) === -1) {
38+
targets.push(match[1]);
39+
}
40+
}
41+
return source;
42+
});
43+
var length = targets.length;
44+
45+
var newMain = "";
46+
newMain += "uniform vec3 debugShowCommandsColor;\n";
47+
newMain += "uniform vec3 debugShowFrustumsColor;\n";
48+
newMain += "void main() \n" + "{ \n" + " czm_Debug_main(); \n";
49+
50+
// set debugShowCommandsColor to Color(1.0, 1.0, 1.0, 1.0) to stop rendering scene.debugShowCommands
51+
// set debugShowFrustumsColor to Color(1.0, 1.0, 1.0, 1.0) to stop rendering scene.debugShowFrustums
52+
var i;
53+
if (length > 0) {
54+
for (i = 0; i < length; ++i) {
55+
newMain +=
56+
" gl_FragData[" + targets[i] + "].rgb *= debugShowCommandsColor;\n";
57+
newMain +=
58+
" gl_FragData[" + targets[i] + "].rgb *= debugShowFrustumsColor;\n";
59+
}
60+
} else {
61+
newMain += " gl_FragColor.rgb *= debugShowCommandsColor;\n";
62+
newMain += " gl_FragColor.rgb *= debugShowFrustumsColor;\n";
63+
}
64+
newMain += "}";
65+
66+
fs.sources.push(newMain);
67+
68+
var attributeLocations = getAttributeLocations(sp);
69+
70+
return ShaderProgram.fromCache({
71+
context: context,
72+
vertexShaderSource: sp.vertexShaderSource,
73+
fragmentShaderSource: fs,
74+
attributeLocations: attributeLocations,
75+
});
76+
}
77+
78+
var scratchFrustumColor = new Color();
79+
function createDebugShowFrustumsUniformMap(scene, command) {
80+
// setup uniform for the shader
81+
var debugUniformMap;
82+
if (!defined(command.uniformMap)) {
83+
debugUniformMap = {};
84+
} else {
85+
debugUniformMap = command.uniformMap;
86+
}
87+
88+
if (
89+
defined(debugUniformMap.debugShowCommandsColor) ||
90+
defined(debugUniformMap.debugShowFrustumsColor)
91+
) {
92+
return debugUniformMap;
93+
}
94+
95+
debugUniformMap.debugShowCommandsColor = function () {
96+
if (!scene.debugShowCommands) {
97+
return Color.WHITE;
98+
}
99+
100+
if (!defined(command._debugColor)) {
101+
command._debugColor = Color.fromRandom();
102+
}
103+
104+
return command._debugColor;
105+
};
106+
107+
debugUniformMap.debugShowFrustumsColor = function () {
108+
if (!scene.debugShowFrustums) {
109+
return Color.WHITE;
110+
}
111+
112+
// Support up to three frustums. If a command overlaps all
113+
// three, it's code is not changed.
114+
scratchFrustumColor.red =
115+
command.debugOverlappingFrustums & (1 << 0) ? 1.0 : 0.0;
116+
scratchFrustumColor.green =
117+
command.debugOverlappingFrustums & (1 << 1) ? 1.0 : 0.0;
118+
scratchFrustumColor.blue =
119+
command.debugOverlappingFrustums & (1 << 2) ? 1.0 : 0.0;
120+
scratchFrustumColor.alpha = 1.0;
121+
return scratchFrustumColor;
122+
};
123+
124+
return debugUniformMap;
125+
}
126+
127+
var scratchShowFrustumCommand = new DrawCommand();
128+
DebugInspector.prototype.executeDebugShowFrustumsCommand = function (
129+
scene,
130+
command,
131+
passState
132+
) {
133+
// create debug command
134+
var shaderProgramId = command.shaderProgram.id;
135+
var debugShaderProgram = this._cachedShowFrustumsShaders[shaderProgramId];
136+
if (!defined(debugShaderProgram)) {
137+
debugShaderProgram = createDebugShowFrustumsShaderProgram(
138+
scene,
139+
command.shaderProgram
140+
);
141+
142+
this._cachedShowFrustumsShaders[shaderProgramId] = debugShaderProgram;
143+
}
144+
145+
var debugCommand = DrawCommand.shallowClone(
146+
command,
147+
scratchShowFrustumCommand
148+
);
149+
debugCommand.shaderProgram = debugShaderProgram;
150+
debugCommand.uniformMap = createDebugShowFrustumsUniformMap(scene, command);
151+
debugCommand.execute(scene.context, passState);
152+
};
153+
export default DebugInspector;

Source/Scene/Scene.js

+7-124
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ import ClearCommand from "../Renderer/ClearCommand.js";
3434
import ComputeEngine from "../Renderer/ComputeEngine.js";
3535
import Context from "../Renderer/Context.js";
3636
import ContextLimits from "../Renderer/ContextLimits.js";
37-
import DrawCommand from "../Renderer/DrawCommand.js";
3837
import Pass from "../Renderer/Pass.js";
3938
import RenderState from "../Renderer/RenderState.js";
40-
import ShaderProgram from "../Renderer/ShaderProgram.js";
41-
import ShaderSource from "../Renderer/ShaderSource.js";
4239
import BrdfLutGenerator from "./BrdfLutGenerator.js";
4340
import Camera from "./Camera.js";
4441
import Cesium3DTilePass from "./Cesium3DTilePass.js";
@@ -72,6 +69,7 @@ import SunLight from "./SunLight.js";
7269
import SunPostProcess from "./SunPostProcess.js";
7370
import TweenCollection from "./TweenCollection.js";
7471
import View from "./View.js";
72+
import DebugInspector from "./DebugInspector.js";
7573

7674
var requestRenderAfterFrame = function (scene) {
7775
return function () {
@@ -262,6 +260,7 @@ function Scene(options) {
262260
this._postRender = new Event();
263261

264262
this._minimumDisableDepthTestDistance = 0.0;
263+
this._debugInspector = new DebugInspector();
265264

266265
/**
267266
* Exceptions occurring in <code>render</code> are always caught in order to raise the
@@ -1964,126 +1963,6 @@ Scene.prototype.isVisible = function (command, cullingVolume, occluder) {
19641963
);
19651964
};
19661965

1967-
function getAttributeLocations(shaderProgram) {
1968-
var attributeLocations = {};
1969-
var attributes = shaderProgram.vertexAttributes;
1970-
for (var a in attributes) {
1971-
if (attributes.hasOwnProperty(a)) {
1972-
attributeLocations[a] = attributes[a].index;
1973-
}
1974-
}
1975-
1976-
return attributeLocations;
1977-
}
1978-
1979-
function createDebugFragmentShaderProgram(command, scene, shaderProgram) {
1980-
var context = scene.context;
1981-
var sp = defaultValue(shaderProgram, command.shaderProgram);
1982-
var fs = sp.fragmentShaderSource.clone();
1983-
1984-
var targets = [];
1985-
fs.sources = fs.sources.map(function (source) {
1986-
source = ShaderSource.replaceMain(source, "czm_Debug_main");
1987-
var re = /gl_FragData\[(\d+)\]/g;
1988-
var match;
1989-
while ((match = re.exec(source)) !== null) {
1990-
if (targets.indexOf(match[1]) === -1) {
1991-
targets.push(match[1]);
1992-
}
1993-
}
1994-
return source;
1995-
});
1996-
var length = targets.length;
1997-
1998-
var newMain = "void main() \n" + "{ \n" + " czm_Debug_main(); \n";
1999-
2000-
var i;
2001-
if (scene.debugShowCommands) {
2002-
if (!defined(command._debugColor)) {
2003-
command._debugColor = Color.fromRandom();
2004-
}
2005-
var c = command._debugColor;
2006-
if (length > 0) {
2007-
for (i = 0; i < length; ++i) {
2008-
newMain +=
2009-
" gl_FragData[" +
2010-
targets[i] +
2011-
"].rgb *= vec3(" +
2012-
c.red +
2013-
", " +
2014-
c.green +
2015-
", " +
2016-
c.blue +
2017-
"); \n";
2018-
}
2019-
} else {
2020-
newMain +=
2021-
" " +
2022-
"gl_FragColor" +
2023-
".rgb *= vec3(" +
2024-
c.red +
2025-
", " +
2026-
c.green +
2027-
", " +
2028-
c.blue +
2029-
"); \n";
2030-
}
2031-
}
2032-
2033-
if (scene.debugShowFrustums) {
2034-
// Support up to three frustums. If a command overlaps all
2035-
// three, it's code is not changed.
2036-
var r = command.debugOverlappingFrustums & (1 << 0) ? "1.0" : "0.0";
2037-
var g = command.debugOverlappingFrustums & (1 << 1) ? "1.0" : "0.0";
2038-
var b = command.debugOverlappingFrustums & (1 << 2) ? "1.0" : "0.0";
2039-
if (length > 0) {
2040-
for (i = 0; i < length; ++i) {
2041-
newMain +=
2042-
" gl_FragData[" +
2043-
targets[i] +
2044-
"].rgb *= vec3(" +
2045-
r +
2046-
", " +
2047-
g +
2048-
", " +
2049-
b +
2050-
"); \n";
2051-
}
2052-
} else {
2053-
newMain +=
2054-
" " +
2055-
"gl_FragColor" +
2056-
".rgb *= vec3(" +
2057-
r +
2058-
", " +
2059-
g +
2060-
", " +
2061-
b +
2062-
"); \n";
2063-
}
2064-
}
2065-
2066-
newMain += "}";
2067-
2068-
fs.sources.push(newMain);
2069-
2070-
var attributeLocations = getAttributeLocations(sp);
2071-
2072-
return ShaderProgram.fromCache({
2073-
context: context,
2074-
vertexShaderSource: sp.vertexShaderSource,
2075-
fragmentShaderSource: fs,
2076-
attributeLocations: attributeLocations,
2077-
});
2078-
}
2079-
2080-
function executeDebugCommand(command, scene, passState) {
2081-
var debugCommand = DrawCommand.shallowClone(command);
2082-
debugCommand.shaderProgram = createDebugFragmentShaderProgram(command, scene);
2083-
debugCommand.execute(scene.context, passState);
2084-
debugCommand.shaderProgram.destroy();
2085-
}
2086-
20871966
var transformFrom2D = new Matrix4(
20881967
0.0,
20891968
0.0,
@@ -2260,7 +2139,11 @@ function executeCommand(command, scene, context, passState, debugFramebuffer) {
22602139
}
22612140

22622141
if (scene.debugShowCommands || scene.debugShowFrustums) {
2263-
executeDebugCommand(command, scene, passState);
2142+
scene._debugInspector.executeDebugShowFrustumsCommand(
2143+
scene,
2144+
command,
2145+
passState
2146+
);
22642147
return;
22652148
}
22662149

Specs/Scene/SceneSpec.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ describe(
287287
result
288288
) {
289289
result = originalShallowClone(command, result);
290-
result.execute = function () {};
290+
result.execute = function () {
291+
result.uniformMap.debugShowCommandsColor();
292+
};
291293
return result;
292294
});
293295

0 commit comments

Comments
 (0)