Skip to content

Commit a53d015

Browse files
Olli Etuahogreggman
authored andcommitted
Don't update shaders on every frame in Aquarium
This saves some CPU time - setShaders only needs to be called when some bit of state affecting shading has changed.
1 parent 01cebfc commit a53d015

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

aquarium/aquarium.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ if (isMultiviewSupportEnabled()) {
2626
g.options.useMultiview = { enabled: true, text: 'Use multiview' };
2727
}
2828

29+
function useMultiviewForStereo() {
30+
return multiview && g.options.useMultiview.enabled;
31+
}
32+
2933
// globals
3034
var gl; // the gl context.
3135
var multiview; // multiview extension.
@@ -43,6 +47,7 @@ var g_fog = true;
4347
var g_numFish = [1, 100, 500, 1000, 5000, 10000, 15000, 20000, 25000, 30000];
4448

4549
var g_stereoDemoActive = false;
50+
var g_shadersNeedUpdate = false; // Set to true whenever the state has changed so that shaders may need to be changed.
4651

4752
var g_requestId;
4853
var g_syncManager;
@@ -1246,13 +1251,6 @@ function initialize() {
12461251
var lightRayConstInUse = useMultiview ? lightRayConstMultiview : lightRayConst;
12471252
var fishConstInUse = useMultiview ? fishConstMultiview : fishConst;
12481253

1249-
// If we are running > 40hz then turn on a few more options.
1250-
if (setPretty && g_fpsTimer.averageFPS > 40) {
1251-
setPretty = false;
1252-
if (!g.options.normalMaps.enabled) { g.options.normalMaps.toggle(); }
1253-
if (!g.options.reflection.enabled) { g.options.reflection.toggle(); }
1254-
}
1255-
12561254
ambient[0] = g.globals.ambientRed;
12571255
ambient[1] = g.globals.ambientGreen;
12581256
ambient[2] = g.globals.ambientBlue;
@@ -1635,14 +1633,12 @@ function initialize() {
16351633
}
16361634

16371635
function renderStereo(leftProjectionMatrix, rightProjectionMatrix, viewInverseMatrix, pose) {
1638-
var useMultiview = multiview && g.options.useMultiview.enabled;
1639-
if (useMultiview) {
1636+
if (useMultiviewForStereo()) {
16401637
setupMultiviewFbIfNeeded();
16411638
var halfWidth = Math.floor(canvas.width * 0.5);
16421639
gl.bindFramebuffer(gl.FRAMEBUFFER, g_multiviewFb);
16431640
gl.viewport(0, 0, halfWidth, canvas.height);
16441641
gl.disable(gl.SCISSOR_TEST);
1645-
setShaders(true);
16461642
render([leftProjectionMatrix, rightProjectionMatrix], viewInverseMatrix, true, pose);
16471643

16481644
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, null);
@@ -1653,7 +1649,6 @@ function initialize() {
16531649
} else { // not multiview
16541650
gl.viewport(0, 0, canvas.width * 0.5, canvas.height);
16551651
gl.enable(gl.SCISSOR_TEST);
1656-
setShaders(false);
16571652
gl.scissor(0, 0, canvas.width * 0.5, canvas.height);
16581653
render(leftProjectionMatrix, viewInverseMatrix, false, pose);
16591654

@@ -1703,7 +1698,6 @@ function initialize() {
17031698

17041699
gl.disable(gl.SCISSOR_TEST);
17051700
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
1706-
setShaders(false);
17071701
render(monoProjection, viewInverseTemp);
17081702
}
17091703

@@ -1758,13 +1752,26 @@ function initialize() {
17581752
g_fpsTimer.update(elapsedTime);
17591753
fpsElem.innerHTML = g_fpsTimer.averageFPS;
17601754

1755+
// If we are running > 40hz then turn on a few more options.
1756+
if (setPretty && g_fpsTimer.averageFPS > 40) {
1757+
setPretty = false;
1758+
if (!g.options.normalMaps.enabled) { g.options.normalMaps.toggle(); }
1759+
if (!g.options.reflection.enabled) { g.options.reflection.toggle(); }
1760+
}
1761+
17611762
if (g_vrDisplay) {
17621763
g_requestId = g_vrDisplay.requestAnimationFrame(onAnimationFrame);
17631764
g_vrDisplay.getFrameData(g_frameData);
17641765
} else {
17651766
g_requestId = tdl.webgl.requestAnimationFrame(onAnimationFrame, canvas);
17661767
}
17671768

1769+
if (g_shadersNeedUpdate) {
1770+
var isInStereoMode = (g_vrDisplay && g_vrDisplay.isPresenting) || g_stereoDemoActive;
1771+
setShaders(isInStereoMode && useMultiviewForStereo());
1772+
g_shadersNeedUpdate = false;
1773+
}
1774+
17681775
if (g_vrDisplay && g_vrDisplay.isPresenting) {
17691776
/* VR UI is enabled in VR Mode. VR UI has two mode, menu mode is the mirror of control panel of
17701777
* aquarium and non-menu mode may presents fps(could be turn off) in front of user. These two
@@ -1778,10 +1785,8 @@ function initialize() {
17781785
// Query gamepad button clicked event.
17791786
g_vrUi.queryGamepadStatus();
17801787

1781-
var useMultiview = multiview && g.options.useMultiview.enabled;
1782-
17831788
// TODO: Support VRUI when doing multiview rendering.
1784-
if (!useMultiview && g_vrUi.isMenuMode) {
1789+
if (!useMultiviewForStereo() && g_vrUi.isMenuMode) {
17851790

17861791
// When VR UI in menu mode, UI need a cursor to help user do select operation. Currently, cursor uses
17871792
// head-neck model which means a point in front of user and user could move the point by rotating their head(with HMD).
@@ -1947,6 +1952,7 @@ function initUIStuff() {
19471952
options[name] = {enabled:!option.enabled};
19481953
setSettings({options:options});
19491954
elem.style.color = option.enabled ? "red" : "gray";
1955+
g_shadersNeedUpdate = true;
19501956
}
19511957

19521958
var optionsContainer = document.getElementById("optionsContainer");
@@ -2135,6 +2141,8 @@ $(function(){
21352141
// to the recommended dimensions for the display.
21362142
resize();
21372143

2144+
g_shadersNeedUpdate = true;
2145+
21382146
if (g_vrDisplay.isPresenting) {
21392147
if (g_vrDisplay.capabilities.hasExternalDisplay) {
21402148
removeButton(vrButton);
@@ -2165,6 +2173,7 @@ $(function(){
21652173

21662174
function toggleStereoDemo() {
21672175
g_stereoDemoActive = !g_stereoDemoActive;
2176+
g_shadersNeedUpdate = true;
21682177
}
21692178

21702179
function resize() {
@@ -2224,7 +2233,7 @@ $(function(){
22242233
}
22252234
}
22262235
// Regardless of if we have WebVR support, we can demonstrate stereo rendering inside the window.
2227-
stereoDemoButton = addButton("Toggle Stereo Demo", "E", getCurrentUrl() + "/vr_assets/button.png", toggleStereoDemo);
2236+
stereoDemoButton = addButton("Toggle Stereo Demo", "", getCurrentUrl() + "/vr_assets/button.png", toggleStereoDemo);
22282237
}
22292238
window.addEventListener('resize', function() {onResize();}, false);
22302239
onResize();

0 commit comments

Comments
 (0)