Skip to content

Commit 2aeecc7

Browse files
committed
fix multiple (redundant) calls to gl.viewport() when using multiple compositors
`viewport` applies to the global gl context, so it should be defined at the [WebGL]Renderer level now, and not anymore in the Compositor class. this follows #1172
1 parent 18defb8 commit 2aeecc7

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

src/video/webgl/compositors/compositor.js

-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as event from "../../../system/event.js";
21
import VertexArrayBuffer from "../buffer/vertex.js";
32
import GLShader from "../glshader.js";
43

@@ -105,12 +104,6 @@ import GLShader from "../glshader.js";
105104
} else {
106105
throw new Error("shader definition missing");
107106
}
108-
109-
// register to the CANVAS resize channel
110-
event.on(event.CANVAS_ONRESIZE, (width, height) => {
111-
this.flush();
112-
this.setViewport(0, 0, width, height);
113-
});
114107
}
115108

116109
/**
@@ -123,13 +116,6 @@ import GLShader from "../glshader.js";
123116

124117
// clear the vertex data buffer
125118
this.vertexData.clear();
126-
127-
// initial viewport size
128-
this.setViewport(
129-
0, 0,
130-
this.renderer.getCanvas().width,
131-
this.renderer.getCanvas().height
132-
);
133119
}
134120

135121
/**
@@ -204,17 +190,6 @@ import GLShader from "../glshader.js";
204190
this.vertexSize = this.vertexByteSize / Float32Array.BYTES_PER_ELEMENT;
205191
}
206192

207-
/**
208-
* Sets the viewport
209-
* @param {number} x - x position of viewport
210-
* @param {number} y - y position of viewport
211-
* @param {number} w - width of viewport
212-
* @param {number} h - height of viewport
213-
*/
214-
setViewport(x, y, w, h) {
215-
this.gl.viewport(x, y, w, h);
216-
}
217-
218193
/**
219194
* set/change the current projection matrix
220195
* @param {Matrix3d} matrix

src/video/webgl/webgl_renderer.js

+20
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ import { isPowerOfTwo } from "./../../math/math.js";
176176
event.on(event.GAME_RESET, () => {
177177
this.reset();
178178
});
179+
180+
// register to the CANVAS resize channel
181+
event.on(event.CANVAS_ONRESIZE, (width, height) => {
182+
this.flush();
183+
this.setViewport(0, 0, width, height);
184+
});
179185
}
180186

181187
/**
@@ -187,6 +193,9 @@ import { isPowerOfTwo } from "./../../math/math.js";
187193
// clear gl context
188194
this.clear();
189195

196+
// initial viewport size
197+
this.setViewport();
198+
190199
// rebind the vertex buffer if required (e.g in case of context loss)
191200
if (this.gl.getParameter(this.gl.ARRAY_BUFFER_BINDING) !== this.vertexBuffer) {
192201
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vertexBuffer);
@@ -318,6 +327,17 @@ import { isPowerOfTwo } from "./../../math/math.js";
318327
this.currentCompositor.setProjection(matrix);
319328
}
320329

330+
/**
331+
* Sets the WebGL viewport, which specifies the affine transformation of x and y from normalized device coordinates to window coordinates
332+
* @param {number} [x = 0] - x the horizontal coordinate for the lower left corner of the viewport origin
333+
* @param {number} [y = 0] - y the vertical coordinate for the lower left corner of the viewport origin
334+
* @param {number} [w = width of the canvas] - the width of viewport
335+
* @param {number} [h = height of the canvas] - the height of viewport
336+
*/
337+
setViewport(x = 0, y = 0, w = this.getCanvas().width, h = this.getCanvas().height) {
338+
this.gl.viewport(x, y, w, h);
339+
}
340+
321341
/**
322342
* Clear the frame buffer
323343
*/

0 commit comments

Comments
 (0)