@zappar/zappar-cv 0.3.16 → 0.3.17

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.17] - 2022-07-21
4
+
5
+ ### Fixed
6
+
7
+ - Preserve `TextureAttribDivisor`, `VertexAttribDivisor` and `STENCIL_TEST` WebGL state.
8
+
3
9
  ## [0.3.16] - 2021-02-07
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.16/zappar-cv.js"></script>
21
+ <script src="https://libs.zappar.com/zappar-cv/0.3.17/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.16/zappar-cv.zip](https://libs.zappar.com/zappar-cv/0.3.16/zappar-cv.zip)
25
+ [https://libs.zappar.com/zappar-cv/0.3.17/zappar-cv.zip](https://libs.zappar.com/zappar-cv/0.3.17/zappar-cv.zip)
@@ -30,6 +30,8 @@ export declare class HTMLElementSource extends Source {
30
30
  start(): void;
31
31
  getFrame(currentlyProcessing: boolean): CameraFrameInfo | undefined;
32
32
  private _processFrame;
33
+ private _instancedArraysExtension;
34
+ private _instancedArraysExtensionSet;
33
35
  private _uploadFrame;
34
36
  private _readFrame;
35
37
  private _updateTransforms;
@@ -28,6 +28,7 @@ class HTMLElementSource extends source_1.Source {
28
28
  this._framebufferHeight = 0;
29
29
  this._framebufferId = null;
30
30
  this._renderTexture = null;
31
+ this._instancedArraysExtensionSet = false;
31
32
  let video = this._video;
32
33
  if (this._video instanceof HTMLVideoElement) {
33
34
  video.addEventListener("loadedmetadata", () => { this._hadFrames = true; });
@@ -56,6 +57,7 @@ class HTMLElementSource extends source_1.Source {
56
57
  this._vertexBuffer = undefined;
57
58
  this._indexBuffer = undefined;
58
59
  this._greyscaleShader = undefined;
60
+ this._instancedArraysExtensionSet = false;
59
61
  }
60
62
  destroy() {
61
63
  let p = pipeline_1.Pipeline.get(this._pipeline);
@@ -126,15 +128,20 @@ class HTMLElementSource extends source_1.Source {
126
128
  let pipeline = pipeline_1.Pipeline.get(this._pipeline);
127
129
  if (!pipeline)
128
130
  return;
129
- let gl = pipeline.glContext;
131
+ let { glContext: gl, isWebGL2 } = pipeline;
130
132
  if (!gl)
131
133
  return;
134
+ if (!isWebGL2 && !this._instancedArraysExtensionSet) {
135
+ this._instancedArraysExtension = gl.getExtension("ANGLE_instanced_arrays");
136
+ this._instancedArraysExtensionSet = true;
137
+ }
132
138
  const glStateManager = gl_state_manager_1.GLStateManager.get(gl);
133
139
  glStateManager.push();
134
140
  const reenableScissorTest = gl.isEnabled(gl.SCISSOR_TEST);
135
141
  const reenableDepthTest = gl.isEnabled(gl.DEPTH_TEST);
136
142
  const reenableBlend = gl.isEnabled(gl.BLEND);
137
143
  const reenableCullFace = gl.isEnabled(gl.CULL_FACE);
144
+ const reenableStencilTest = gl.isEnabled(gl.STENCIL_TEST);
138
145
  const previousActiveTexture = gl.getParameter(gl.ACTIVE_TEXTURE);
139
146
  const previousUnpackFlip = gl.getParameter(gl.UNPACK_FLIP_Y_WEBGL);
140
147
  const previousProgram = gl.getParameter(gl.CURRENT_PROGRAM);
@@ -147,6 +154,7 @@ class HTMLElementSource extends source_1.Source {
147
154
  gl.disable(gl.DEPTH_TEST);
148
155
  gl.disable(gl.BLEND);
149
156
  gl.disable(gl.CULL_FACE);
157
+ gl.disable(gl.STENCIL_TEST);
150
158
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
151
159
  gl.bindTexture(gl.TEXTURE_2D, this._currentVideoTexture);
152
160
  const level = 0;
@@ -185,6 +193,20 @@ class HTMLElementSource extends source_1.Source {
185
193
  const previousTextureAttribOffset = gl.getVertexAttribOffset(shader.aTextureCoordLoc, gl.VERTEX_ATTRIB_ARRAY_POINTER);
186
194
  const previousTextureAttribEnabled = gl.getVertexAttrib(shader.aTextureCoordLoc, gl.VERTEX_ATTRIB_ARRAY_ENABLED);
187
195
  const previousTextureAttribBufferBinding = gl.getVertexAttrib(shader.aTextureCoordLoc, gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING);
196
+ let previousVertexAttribDivisor = 0;
197
+ let previousTextureAttribDivisor = 0;
198
+ if (isWebGL2) {
199
+ previousVertexAttribDivisor = gl.getVertexAttrib(shader.aVertexPositionLoc, gl.VERTEX_ATTRIB_ARRAY_DIVISOR);
200
+ previousTextureAttribDivisor = gl.getVertexAttrib(shader.aTextureCoordLoc, gl.VERTEX_ATTRIB_ARRAY_DIVISOR);
201
+ gl.vertexAttribDivisor(shader.aVertexPositionLoc, 0);
202
+ gl.vertexAttribDivisor(shader.aTextureCoordLoc, 0);
203
+ }
204
+ else if (this._instancedArraysExtension) {
205
+ previousVertexAttribDivisor = gl.getVertexAttrib(shader.aVertexPositionLoc, this._instancedArraysExtension.VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE);
206
+ previousTextureAttribDivisor = gl.getVertexAttrib(shader.aTextureCoordLoc, this._instancedArraysExtension.VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE);
207
+ this._instancedArraysExtension.vertexAttribDivisorANGLE(shader.aVertexPositionLoc, 0);
208
+ this._instancedArraysExtension.vertexAttribDivisorANGLE(shader.aTextureCoordLoc, 0);
209
+ }
188
210
  // Rendering to the greyscale conversion buffer - bind the framebuffer
189
211
  gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
190
212
  // const t_before = performance.now();
@@ -223,6 +245,14 @@ class HTMLElementSource extends source_1.Source {
223
245
  gl.disableVertexAttribArray(shader.aVertexPositionLoc);
224
246
  if (!previousTextureAttribEnabled)
225
247
  gl.disableVertexAttribArray(shader.aTextureCoordLoc);
248
+ if (isWebGL2) {
249
+ gl.vertexAttribDivisor(shader.aVertexPositionLoc, previousVertexAttribDivisor);
250
+ gl.vertexAttribDivisor(shader.aTextureCoordLoc, previousTextureAttribDivisor);
251
+ }
252
+ else if (this._instancedArraysExtension) {
253
+ this._instancedArraysExtension.vertexAttribDivisorANGLE(shader.aVertexPositionLoc, previousVertexAttribDivisor);
254
+ this._instancedArraysExtension.vertexAttribDivisorANGLE(shader.aTextureCoordLoc, previousTextureAttribDivisor);
255
+ }
226
256
  gl.bindFramebuffer(gl.FRAMEBUFFER, previousBoundFramebuffer);
227
257
  gl.useProgram(previousProgram);
228
258
  gl.bindTexture(gl.TEXTURE_2D, previousBoundTexture);
package/lib/pipeline.d.ts CHANGED
@@ -15,15 +15,17 @@ export declare class Pipeline {
15
15
  private cameraTokens;
16
16
  private nextCameraToken;
17
17
  private tokensInFlight;
18
- glContext: WebGLRenderingContext | undefined;
18
+ glContext: WebGLRenderingContext | WebGL2RenderingContext | undefined;
19
19
  private videoTextures;
20
20
  cameraPixelArrays: ArrayBuffer[];
21
21
  private _cameraDraw;
22
22
  private _faceDraw;
23
23
  private _faceProjectDraw;
24
+ private _isWebGL2;
24
25
  onGLContextReset: Event;
25
26
  static create(client: zappar_cwrap, mgr: MsgManager): zappar_pipeline_t;
26
27
  static get(p: zappar_pipeline_t): Pipeline | undefined;
28
+ get isWebGL2(): boolean;
27
29
  private constructor();
28
30
  frameUpdate(client: zappar_client): void;
29
31
  cameraTokenReturn(tokenId: number, pixelArray: ArrayBuffer): void;
package/lib/pipeline.js CHANGED
@@ -21,6 +21,7 @@ class Pipeline {
21
21
  this.tokensInFlight = 0;
22
22
  this.videoTextures = [];
23
23
  this.cameraPixelArrays = [];
24
+ this._isWebGL2 = false;
24
25
  this.onGLContextReset = new event_1.Event();
25
26
  }
26
27
  static create(client, mgr) {
@@ -31,6 +32,9 @@ class Pipeline {
31
32
  static get(p) {
32
33
  return byId.get(p);
33
34
  }
35
+ get isWebGL2() {
36
+ return this._isWebGL2;
37
+ }
34
38
  frameUpdate(client) {
35
39
  for (let msg of this.pendingMessages) {
36
40
  client.processMessages(msg);
@@ -106,6 +110,7 @@ class Pipeline {
106
110
  glContextSet(gl, texturePool) {
107
111
  this.glContextLost();
108
112
  this.glContext = gl;
113
+ this._isWebGL2 = gl.getParameter(gl.VERSION).indexOf("WebGL 2") >= 0;
109
114
  texturePool = texturePool || [];
110
115
  for (let i = 0; i < 4; i++) {
111
116
  let tex = texturePool[i] || gl.createTexture();
package/lib/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.3.16";
1
+ export declare const VERSION = "0.3.17";
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.16";
4
+ exports.VERSION = "0.3.17";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zappar/zappar-cv",
3
- "version": "0.3.16",
3
+ "version": "0.3.17",
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",