From 54d655c3f7b34bb4ddf36eac95d569a86c9a432b Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 25 Oct 2019 11:36:31 +0200 Subject: [PATCH 1/4] Check bgfx resource creation --- src/Engines/nativeEngine.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Engines/nativeEngine.ts b/src/Engines/nativeEngine.ts index e1b860d2a67..5fd436cf3e5 100644 --- a/src/Engines/nativeEngine.ts +++ b/src/Engines/nativeEngine.ts @@ -71,8 +71,8 @@ interface INativeEngine { setFloat4(uniform: WebGLUniformLocation, x: number, y: number, z: number, w: number): void; createTexture(): WebGLTexture; - loadTexture(texture: WebGLTexture, buffer: ArrayBuffer | ArrayBufferView | Blob, mipMap: boolean): void; - loadCubeTexture(texture: WebGLTexture, data: Array>, flipY : boolean): void; + loadTexture(texture: WebGLTexture, buffer: ArrayBuffer | ArrayBufferView | Blob, mipMap: boolean): any; + loadCubeTexture(texture: WebGLTexture, data: Array>, flipY : boolean): any; getTextureWidth(texture: WebGLTexture): number; getTextureHeight(texture: WebGLTexture): number; setTextureSampling(texture: WebGLTexture, filter: number): void; // filter is a NativeFilter.XXXX value. @@ -175,6 +175,8 @@ declare var _native: any; /** @hidden */ export class NativeEngine extends Engine { private readonly _native: INativeEngine = new _native.Engine(); + /** Defines the invalid handle returned by bgfx when resource creation goes wrong */ + private readonly INVALID_HANDLE = 65535; public getHardwareScalingLevel(): number { return 1.0; @@ -288,6 +290,9 @@ export class NativeEngine extends Engine { buffer.references = 1; buffer.is32Bits = (data.BYTES_PER_ELEMENT === 4); buffer.nativeIndexBuffer = this._native.createIndexBuffer(data); + if (buffer.nativeVertexBuffer == this.INVALID_HANDLE) { + throw new Error("Could not create a native index buffer."); + } return buffer; } @@ -295,6 +300,9 @@ export class NativeEngine extends Engine { const buffer = new NativeDataBuffer(); buffer.references = 1; buffer.nativeVertexBuffer = this._native.createVertexBuffer(ArrayBuffer.isView(data) ? data : new Float32Array(data)); + if (buffer.nativeVertexBuffer == this.INVALID_HANDLE) { + throw new Error("Could not create a native vertex buffer."); + } return buffer; } @@ -946,7 +954,10 @@ export class NativeEngine extends Engine { return; } - this._native.loadTexture(webGLTexture, data, !noMipmap); + let nativeHandle = this._native.loadTexture(webGLTexture, data, !noMipmap); + if (nativeHandle == this.INVALID_HANDLE) { + throw new Error("Could not load a native texture."); + } if (invertY) { throw new Error("Support for textures with inverted Y coordinates not yet implemented."); From 6e62ac71dabcbccca44ee6178662282b66f1f505 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 25 Oct 2019 11:43:25 +0200 Subject: [PATCH 2/4] check for loadcubetexture --- src/Engines/nativeEngine.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Engines/nativeEngine.ts b/src/Engines/nativeEngine.ts index 59f21b008e4..b6611c3d303 100644 --- a/src/Engines/nativeEngine.ts +++ b/src/Engines/nativeEngine.ts @@ -1075,8 +1075,10 @@ export class NativeEngine extends Engine { texture.getEngine().updateTextureSamplingMode(Texture.TRILINEAR_SAMPLINGMODE, texture); texture._isRGBD = true; texture.invertY = true; - this._native.loadCubeTexture(texture._webGLTexture!, imageData, true); - + let nativeHandle = this._native.loadCubeTexture(texture._webGLTexture!, imageData, true); + if (nativeHandle === this.INVALID_HANDLE) { + throw new Error("Could not load a native cube texture."); + } texture.isReady = true; if (onLoad) { onLoad(); From 4882aa1ccccccc28dd30899a1e0f7d3be1e0926a Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 25 Oct 2019 11:44:32 +0200 Subject: [PATCH 3/4] empty line --- src/Engines/nativeEngine.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Engines/nativeEngine.ts b/src/Engines/nativeEngine.ts index b6611c3d303..1f9ba8b0882 100644 --- a/src/Engines/nativeEngine.ts +++ b/src/Engines/nativeEngine.ts @@ -1079,6 +1079,7 @@ export class NativeEngine extends Engine { if (nativeHandle === this.INVALID_HANDLE) { throw new Error("Could not load a native cube texture."); } + texture.isReady = true; if (onLoad) { onLoad(); From d212d3a8b9050bf901aedef95f4f1f476f722ff5 Mon Sep 17 00:00:00 2001 From: Cedric Guillemet Date: Fri, 25 Oct 2019 18:23:18 +0200 Subject: [PATCH 4/4] loadtexture now return boolean --- src/Engines/nativeEngine.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Engines/nativeEngine.ts b/src/Engines/nativeEngine.ts index 1f9ba8b0882..47984bbf518 100644 --- a/src/Engines/nativeEngine.ts +++ b/src/Engines/nativeEngine.ts @@ -71,8 +71,8 @@ interface INativeEngine { setFloat4(uniform: WebGLUniformLocation, x: number, y: number, z: number, w: number): void; createTexture(): WebGLTexture; - loadTexture(texture: WebGLTexture, buffer: ArrayBuffer | ArrayBufferView | Blob, mipMap: boolean, invertY: boolean): any; - loadCubeTexture(texture: WebGLTexture, data: Array>, flipY : boolean): any; + loadTexture(texture: WebGLTexture, buffer: ArrayBuffer | ArrayBufferView | Blob, mipMap: boolean, invertY: boolean): boolean; + loadCubeTexture(texture: WebGLTexture, data: Array>, flipY : boolean): boolean; getTextureWidth(texture: WebGLTexture): number; getTextureHeight(texture: WebGLTexture): number; setTextureSampling(texture: WebGLTexture, filter: number): void; // filter is a NativeFilter.XXXX value. @@ -959,8 +959,7 @@ export class NativeEngine extends Engine { return; } - let nativeHandle = this._native.loadTexture(webGLTexture, data, !noMipmap, invertY); - if (nativeHandle === this.INVALID_HANDLE) { + if (!this._native.loadTexture(webGLTexture, data, !noMipmap, invertY)) { throw new Error("Could not load a native texture."); } @@ -1075,8 +1074,7 @@ export class NativeEngine extends Engine { texture.getEngine().updateTextureSamplingMode(Texture.TRILINEAR_SAMPLINGMODE, texture); texture._isRGBD = true; texture.invertY = true; - let nativeHandle = this._native.loadCubeTexture(texture._webGLTexture!, imageData, true); - if (nativeHandle === this.INVALID_HANDLE) { + if (!this._native.loadCubeTexture(texture._webGLTexture!, imageData, true)) { throw new Error("Could not load a native cube texture."); }