Browse Source

Merge pull request #7054 from CedricGuillemet/checkInvalidHandleNative

Check invalid handle native
Gary Hsu 5 years ago
parent
commit
d70091c5e1
1 changed files with 16 additions and 4 deletions
  1. 16 4
      src/Engines/nativeEngine.ts

+ 16 - 4
src/Engines/nativeEngine.ts

@@ -72,8 +72,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): void;
-    loadCubeTexture(texture: WebGLTexture, data: Array<Array<ArrayBufferView>>, flipY : boolean): void;
+    loadTexture(texture: WebGLTexture, buffer: ArrayBuffer | ArrayBufferView | Blob, mipMap: boolean, invertY: boolean): boolean;
+    loadCubeTexture(texture: WebGLTexture, data: Array<Array<ArrayBufferView>>, flipY : boolean): boolean;
     getTextureWidth(texture: WebGLTexture): number;
     getTextureHeight(texture: WebGLTexture): number;
     setTextureSampling(texture: WebGLTexture, filter: number): void; // filter is a NativeFilter.XXXX value.
@@ -188,6 +188,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;
@@ -314,6 +316,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;
     }
 
@@ -321,6 +326,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;
     }
 
@@ -972,7 +980,9 @@ export class NativeEngine extends Engine {
                     return;
                 }
 
-                this._native.loadTexture(webGLTexture, data, !noMipmap, invertY);
+                if (!this._native.loadTexture(webGLTexture, data, !noMipmap, invertY)) {
+                    throw new Error("Could not load a native texture.");
+                }
 
                 texture.baseWidth = this._native.getTextureWidth(webGLTexture);
                 texture.baseHeight = this._native.getTextureHeight(webGLTexture);
@@ -1085,7 +1095,9 @@ 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);
+                if (!this._native.loadCubeTexture(texture._webGLTexture!, imageData, true)) {
+                    throw new Error("Could not load a native cube texture.");
+                }
 
                 texture.isReady = true;
                 if (onLoad) {