@zappar/zappar-cv 0.3.15 → 0.3.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.3.16] - 2021-02-07
4
+
5
+ ### Fixed
6
+
7
+ - External WebGL state is preserved during `process_gl` calls
8
+
3
9
  ## [0.3.15] - 2021-01-25
4
10
 
5
11
  ### Fixed
package/README.md CHANGED
@@ -18,8 +18,8 @@ npm i @zappar/zappar-cv
18
18
 
19
19
  You can use our CDN from within your HTML:
20
20
  ```
21
- <script src="https://libs.zappar.com/zappar-cv/0.3.15/zappar-cv.js"></script>
21
+ <script src="https://libs.zappar.com/zappar-cv/0.3.16/zappar-cv.js"></script>
22
22
  ```
23
23
 
24
24
  Or you can download and host our standalone JavaScript bundle:
25
- [https://libs.zappar.com/zappar-cv/0.3.15/zappar-cv.zip](https://libs.zappar.com/zappar-cv/0.3.15/zappar-cv.zip)
25
+ [https://libs.zappar.com/zappar-cv/0.3.16/zappar-cv.zip](https://libs.zappar.com/zappar-cv/0.3.16/zappar-cv.zip)
@@ -0,0 +1,9 @@
1
+ export declare class GLStateManager {
2
+ private _gl;
3
+ static get(gl: WebGLRenderingContext): GLStateManager;
4
+ private _viewports;
5
+ private _underlyingViewport;
6
+ private constructor();
7
+ push(): void;
8
+ pop(): void;
9
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GLStateManager = void 0;
4
+ const managers = new Map();
5
+ class GLStateManager {
6
+ constructor(_gl) {
7
+ this._gl = _gl;
8
+ this._viewports = [];
9
+ this._underlyingViewport = this._gl.viewport;
10
+ this._viewports.push(this._gl.getParameter(this._gl.VIEWPORT));
11
+ this._gl.viewport = (x, y, width, height) => {
12
+ this._viewports[this._viewports.length - 1] = [x, y, width, height];
13
+ this._underlyingViewport.call(this._gl, x, y, width, height);
14
+ };
15
+ }
16
+ static get(gl) {
17
+ let existing = managers.get(gl);
18
+ if (!existing) {
19
+ existing = new GLStateManager(gl);
20
+ managers.set(gl, existing);
21
+ }
22
+ return existing;
23
+ }
24
+ push() {
25
+ this._viewports.push(this._viewports[this._viewports.length - 1]);
26
+ }
27
+ pop() {
28
+ const current = this._viewports.pop();
29
+ const prev = this._viewports[this._viewports.length - 1];
30
+ if (!current || current[0] !== prev[0] || current[1] !== prev[1] || current[2] !== prev[2] || current[3] !== prev[3]) {
31
+ this._underlyingViewport.call(this._gl, prev[0], prev[1], prev[2], prev[3]);
32
+ }
33
+ }
34
+ }
35
+ exports.GLStateManager = GLStateManager;
@@ -7,6 +7,7 @@ const profile_1 = require("./profile");
7
7
  const shader_1 = require("./shader");
8
8
  const gl_matrix_1 = require("gl-matrix");
9
9
  const loglevel_1 = require("./loglevel");
10
+ const gl_state_manager_1 = require("./gl-state-manager");
10
11
  let latest = 1;
11
12
  let byId = new Map();
12
13
  class HTMLElementSource extends source_1.Source {
@@ -128,12 +129,25 @@ class HTMLElementSource extends source_1.Source {
128
129
  let gl = pipeline.glContext;
129
130
  if (!gl)
130
131
  return;
132
+ const glStateManager = gl_state_manager_1.GLStateManager.get(gl);
133
+ glStateManager.push();
134
+ const reenableScissorTest = gl.isEnabled(gl.SCISSOR_TEST);
135
+ const reenableDepthTest = gl.isEnabled(gl.DEPTH_TEST);
136
+ const reenableBlend = gl.isEnabled(gl.BLEND);
137
+ const reenableCullFace = gl.isEnabled(gl.CULL_FACE);
138
+ const previousActiveTexture = gl.getParameter(gl.ACTIVE_TEXTURE);
139
+ const previousUnpackFlip = gl.getParameter(gl.UNPACK_FLIP_Y_WEBGL);
140
+ const previousProgram = gl.getParameter(gl.CURRENT_PROGRAM);
141
+ gl.activeTexture(gl.TEXTURE0);
142
+ const previousBoundTexture = gl.getParameter(gl.TEXTURE_BINDING_2D);
143
+ const previousBoundFramebuffer = gl.getParameter(gl.FRAMEBUFFER_BINDING);
144
+ const previousBoundArrayBuffer = gl.getParameter(gl.ARRAY_BUFFER_BINDING);
145
+ const previousBoundElementArrayBuffer = gl.getParameter(gl.ELEMENT_ARRAY_BUFFER_BINDING);
131
146
  gl.disable(gl.SCISSOR_TEST);
132
147
  gl.disable(gl.DEPTH_TEST);
133
148
  gl.disable(gl.BLEND);
134
149
  gl.disable(gl.CULL_FACE);
135
150
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
136
- gl.activeTexture(gl.TEXTURE0);
137
151
  gl.bindTexture(gl.TEXTURE_2D, this._currentVideoTexture);
138
152
  const level = 0;
139
153
  const internalFormat = gl.RGBA;
@@ -157,13 +171,29 @@ class HTMLElementSource extends source_1.Source {
157
171
  let vbo = this._getVertexBuffer(gl);
158
172
  let ibo = this._getIndexBuffer(gl);
159
173
  let shader = this._getGreyscaleShader(gl);
160
- // // Rendering to the greyscale conversion buffer - bind the framebuffer
174
+ const previousVertexAttribSize = gl.getVertexAttrib(shader.aVertexPositionLoc, gl.VERTEX_ATTRIB_ARRAY_SIZE);
175
+ const previousVertexAttribType = gl.getVertexAttrib(shader.aVertexPositionLoc, gl.VERTEX_ATTRIB_ARRAY_TYPE);
176
+ const previousVertexAttribNormalized = gl.getVertexAttrib(shader.aVertexPositionLoc, gl.VERTEX_ATTRIB_ARRAY_NORMALIZED);
177
+ const previousVertexAttribStride = gl.getVertexAttrib(shader.aVertexPositionLoc, gl.VERTEX_ATTRIB_ARRAY_STRIDE);
178
+ const previousVertexAttribOffset = gl.getVertexAttribOffset(shader.aVertexPositionLoc, gl.VERTEX_ATTRIB_ARRAY_POINTER);
179
+ const previousVertexAttribEnabled = gl.getVertexAttrib(shader.aVertexPositionLoc, gl.VERTEX_ATTRIB_ARRAY_ENABLED);
180
+ const previousVertexAttribBufferBinding = gl.getVertexAttrib(shader.aVertexPositionLoc, gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING);
181
+ const previousTextureAttribSize = gl.getVertexAttrib(shader.aTextureCoordLoc, gl.VERTEX_ATTRIB_ARRAY_SIZE);
182
+ const previousTextureAttribType = gl.getVertexAttrib(shader.aTextureCoordLoc, gl.VERTEX_ATTRIB_ARRAY_TYPE);
183
+ const previousTextureAttribNormalized = gl.getVertexAttrib(shader.aTextureCoordLoc, gl.VERTEX_ATTRIB_ARRAY_NORMALIZED);
184
+ const previousTextureAttribStride = gl.getVertexAttrib(shader.aTextureCoordLoc, gl.VERTEX_ATTRIB_ARRAY_STRIDE);
185
+ const previousTextureAttribOffset = gl.getVertexAttribOffset(shader.aTextureCoordLoc, gl.VERTEX_ATTRIB_ARRAY_POINTER);
186
+ const previousTextureAttribEnabled = gl.getVertexAttrib(shader.aTextureCoordLoc, gl.VERTEX_ATTRIB_ARRAY_ENABLED);
187
+ const previousTextureAttribBufferBinding = gl.getVertexAttrib(shader.aTextureCoordLoc, gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING);
188
+ // Rendering to the greyscale conversion buffer - bind the framebuffer
161
189
  gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
190
+ // const t_before = performance.now();
191
+ // const viewport = gl.getParameter(gl.VIEWPORT);
192
+ // console.log("Viewport time", performance.now() - t_before, viewport);
162
193
  gl.viewport(0, 0, this._framebufferWidth, this._framebufferHeight);
163
- // // We'll be replacing all the content - clear is a good hint for this on mobile
164
- gl.clearColor(0.0, 0.0, 0.0, 1.0);
194
+ // We'll be replacing all the content - clear is a good hint for this on mobile
165
195
  gl.clear(gl.COLOR_BUFFER_BIT);
166
- // // Set up bindings for vertex attributes
196
+ // Set up bindings for vertex attributes
167
197
  gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
168
198
  gl.vertexAttribPointer(shader.aVertexPositionLoc, 2, gl.FLOAT, false, 4 * 4, 0);
169
199
  gl.enableVertexAttribArray(shader.aVertexPositionLoc);
@@ -183,14 +213,30 @@ class HTMLElementSource extends source_1.Source {
183
213
  gl.uniform1i(shader.uSamplerLoc, 0);
184
214
  // Do the drawing...
185
215
  gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
186
- // Disable attribute arrays
187
- gl.disableVertexAttribArray(shader.aVertexPositionLoc);
188
- gl.disableVertexAttribArray(shader.aTextureCoordLoc);
189
- gl.bindFramebuffer(gl.FRAMEBUFFER, null);
190
- gl.bindBuffer(gl.ARRAY_BUFFER, null);
191
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
192
- gl.useProgram(null);
193
- gl.bindTexture(gl.TEXTURE_2D, null);
216
+ gl.bindBuffer(gl.ARRAY_BUFFER, previousVertexAttribBufferBinding);
217
+ gl.vertexAttribPointer(shader.aVertexPositionLoc, previousVertexAttribSize, previousVertexAttribType, previousVertexAttribNormalized, previousVertexAttribStride, previousVertexAttribOffset);
218
+ gl.bindBuffer(gl.ARRAY_BUFFER, previousTextureAttribBufferBinding);
219
+ gl.vertexAttribPointer(shader.aTextureCoordLoc, previousTextureAttribSize, previousTextureAttribType, previousTextureAttribNormalized, previousTextureAttribStride, previousTextureAttribOffset);
220
+ gl.bindBuffer(gl.ARRAY_BUFFER, previousBoundArrayBuffer);
221
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, previousBoundElementArrayBuffer);
222
+ if (!previousVertexAttribEnabled)
223
+ gl.disableVertexAttribArray(shader.aVertexPositionLoc);
224
+ if (!previousTextureAttribEnabled)
225
+ gl.disableVertexAttribArray(shader.aTextureCoordLoc);
226
+ gl.bindFramebuffer(gl.FRAMEBUFFER, previousBoundFramebuffer);
227
+ gl.useProgram(previousProgram);
228
+ gl.bindTexture(gl.TEXTURE_2D, previousBoundTexture);
229
+ gl.activeTexture(previousActiveTexture);
230
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, previousUnpackFlip);
231
+ if (reenableBlend)
232
+ gl.enable(gl.BLEND);
233
+ if (reenableCullFace)
234
+ gl.enable(gl.CULL_FACE);
235
+ if (reenableDepthTest)
236
+ gl.enable(gl.DEPTH_TEST);
237
+ if (reenableScissorTest)
238
+ gl.enable(gl.SCISSOR_TEST);
239
+ glStateManager.pop();
194
240
  }
195
241
  _readFrame(p, gl) {
196
242
  if (!this._currentVideoTexture)
@@ -208,10 +254,11 @@ class HTMLElementSource extends source_1.Source {
208
254
  pixels = new ArrayBuffer(greySize);
209
255
  }
210
256
  let pixelsView = new Uint8Array(pixels);
257
+ const previousBoundFramebuffer = gl.getParameter(gl.FRAMEBUFFER_BINDING);
211
258
  let framebuffer = this._getFramebuffer(gl, profile_1.profile.dataWidth / 4, profile_1.profile.dataHeight);
212
259
  gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
213
260
  gl.readPixels(0, 0, this._framebufferWidth, this._framebufferHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixelsView);
214
- gl.bindFramebuffer(gl.FRAMEBUFFER, null);
261
+ gl.bindFramebuffer(gl.FRAMEBUFFER, previousBoundFramebuffer);
215
262
  return {
216
263
  uvTransform: this._cameraUvTransform,
217
264
  data: pixels,
package/lib/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.3.15";
1
+ export declare const VERSION = "0.3.16";
package/lib/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
- exports.VERSION = "0.3.15";
4
+ exports.VERSION = "0.3.16";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zappar/zappar-cv",
3
- "version": "0.3.15",
3
+ "version": "0.3.16",
4
4
  "description": "Zappar's core computer vision library, supporting image, face and world tracking.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",