Skip to content

Commit 81534d3

Browse files
committed
refactor(transition2d): cache uniform locations
Signed-off-by: codewithvk <vivek.javiya@collabora.com> Change-Id: Ifc20d52fdce320474a169760a25d7490e74b5c9c
1 parent ccfb2a7 commit 81534d3

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

browser/src/slideshow/Transition2d.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ abstract class TransitionBase extends SlideChangeGl {
8383
class Transition2d extends TransitionBase {
8484
private static readonly DefaultFromColor = new Float32Array([0, 0, 0, 0]);
8585
private static readonly DefaultToColor = new Float32Array([0, 0, 0, 0]);
86+
private _uniformCache: Record<string, WebGLUniformLocation> = {};
8687

8788
constructor(transitionParameters: TransitionParameters) {
8889
super(transitionParameters);
@@ -139,6 +140,17 @@ class Transition2d extends TransitionBase {
139140
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
140141
}
141142

143+
private getUniformLocation(value: string) {
144+
if (this.program) {
145+
if (!this._uniformCache[value])
146+
this._uniformCache[value] = this.gl.getUniformLocation(
147+
this.program,
148+
value,
149+
);
150+
return this._uniformCache[value];
151+
}
152+
}
153+
142154
public render(nT: number, properties?: AnimatedElementRenderProperties) {
143155
if (this.context.isDisposed()) return;
144156

@@ -170,7 +182,6 @@ class Transition2d extends TransitionBase {
170182
0,
171183
);
172184
} else {
173-
// jscpd:ignore-start
174185
let bounds: BoundsType = null;
175186
let alpha = 1.0;
176187
let fromFillColor = Transition2d.DefaultFromColor;
@@ -195,27 +206,17 @@ class Transition2d extends TransitionBase {
195206
console.debug(`Transition2d.render: alpha: ${alpha}`);
196207

197208
this.setPositionBuffer(bounds);
198-
this.gl.uniform1f(
199-
this.gl.getUniformLocation(this.program, 'alpha'),
200-
alpha,
201-
);
209+
this.gl.uniform1f(this.getUniformLocation('alpha'), alpha);
202210
this.gl.uniform4fv(
203-
this.gl.getUniformLocation(this.program, 'fromFillColor'),
211+
this.getUniformLocation('fromFillColor'),
204212
fromFillColor,
205213
);
214+
this.gl.uniform4fv(this.getUniformLocation('toFillColor'), toFillColor);
206215
this.gl.uniform4fv(
207-
this.gl.getUniformLocation(this.program, 'toFillColor'),
208-
toFillColor,
209-
);
210-
this.gl.uniform4fv(
211-
this.gl.getUniformLocation(this.program, 'fromLineColor'),
216+
this.getUniformLocation('fromLineColor'),
212217
fromLineColor,
213218
);
214-
this.gl.uniform4fv(
215-
this.gl.getUniformLocation(this.program, 'toLineColor'),
216-
toLineColor,
217-
);
218-
// jscpd:ignore-end
219+
this.gl.uniform4fv(this.getUniformLocation('toLineColor'), toLineColor);
219220
}
220221

221222
gl.activeTexture(gl.TEXTURE1);

0 commit comments

Comments
 (0)