Skip to content

Commit

Permalink
Merge pull request BabylonJS#7054 from CedricGuillemet/checkInvalidHa…
Browse files Browse the repository at this point in the history
…ndleNative

Check invalid handle native
  • Loading branch information
bghgary committed Oct 25, 2019
2 parents d3455d2 + d212d3a commit d70091c
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/Engines/nativeEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -314,13 +316,19 @@ 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;
}

public createVertexBuffer(data: DataArray): NativeDataBuffer {
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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit d70091c

Please sign in to comment.