diff --git a/cocos/3d/assets/texture-cube.ts b/cocos/3d/assets/texture-cube.ts index 1ad72c8e7e..9a9ce7ccb3 100644 --- a/cocos/3d/assets/texture-cube.ts +++ b/cocos/3d/assets/texture-cube.ts @@ -163,16 +163,17 @@ export default class TextureCube extends TextureBase { } } - public onLoaded () { - this.mipmaps = this._mipmaps; - } - protected _createTexture () { if (this._mipmaps.length === 0) { return; } - this._texture = this._getGlobalDevice().createTexture({ + const gfxDevice = this._getGlobalDevice(); + if (!gfxDevice) { + return; + } + + this._texture = gfxDevice.createTexture({ type: GFXTextureType.TEX2D, /* tslint:disable:no-bitwise */ usage: GFXTextureUsageBit.SAMPLED | GFXTextureUsageBit.TRANSFER_DST, @@ -196,7 +197,12 @@ export default class TextureCube extends TextureBase { return; } - this._textureView = this._getGlobalDevice().createTextureView({ + const gfxDevice = this._getGlobalDevice(); + if (!gfxDevice) { + return; + } + + this._textureView = gfxDevice.createTextureView({ texture: this._texture, type: GFXTextureViewType.TV2D_ARRAY, format: this._getGfxFormat(), diff --git a/cocos/assets/render-texture.ts b/cocos/assets/render-texture.ts index b83176fe74..72d001bcc6 100644 --- a/cocos/assets/render-texture.ts +++ b/cocos/assets/render-texture.ts @@ -51,6 +51,9 @@ export default class RenderTexture extends Texture2D { this.destroy(); const gfxDevice = this._getGlobalDevice(); + if (!gfxDevice) { + return; + } const w = Math.floor(width || cc.visibleRect.width); const h = Math.floor(height || cc.visibleRect.height); @@ -179,11 +182,16 @@ export default class RenderTexture extends Texture2D { return undefined; } + const gfxDevice = this._getGlobalDevice(); + if (!gfxDevice) { + return; + } + const normalizedRect = normalizeRect2D(rect, this.width, this.height); data = data || new Uint8Array(normalizedRect.width * normalizedRect.height * 4); - this._getGlobalDevice().copyFramebufferToBuffer(this._framebuffer, data.buffer, [{ + gfxDevice.copyFramebufferToBuffer(this._framebuffer, data.buffer, [{ buffOffset: 0, buffStride: 0, buffTexHeight: 0, diff --git a/cocos/assets/texture-2d.ts b/cocos/assets/texture-2d.ts index f52c059f3c..faf0eff38d 100644 --- a/cocos/assets/texture-2d.ts +++ b/cocos/assets/texture-2d.ts @@ -175,16 +175,17 @@ export default class Texture2D extends TextureBase { } } - public onLoaded () { - this.mipmaps = this._mipmaps; - } - protected _createTexture () { if (this._mipmaps.length === 0) { return; } - this._texture = this._getGlobalDevice().createTexture({ + const gfxDevice = this._getGlobalDevice(); + if (!gfxDevice) { + return; + } + + this._texture = gfxDevice.createTexture({ type: GFXTextureType.TEX2D, /* tslint:disable:no-bitwise */ usage: GFXTextureUsageBit.SAMPLED | GFXTextureUsageBit.TRANSFER_DST, @@ -205,7 +206,12 @@ export default class Texture2D extends TextureBase { return; } - this._textureView = this._getGlobalDevice().createTextureView({ + const gfxDevice = this._getGlobalDevice(); + if (!gfxDevice) { + return; + } + + this._textureView = gfxDevice.createTextureView({ texture: this._texture, type: GFXTextureViewType.TV2D, format: this._getGfxFormat(), diff --git a/cocos/assets/texture-base.ts b/cocos/assets/texture-base.ts index 499e304b19..fb0a4cdf93 100644 --- a/cocos/assets/texture-base.ts +++ b/cocos/assets/texture-base.ts @@ -473,7 +473,12 @@ export default class TextureBase extends Asset { } } - protected _getGlobalDevice (): GFXDevice { + public onLoaded () { + this._updateSampler(); + this._recreateTexture(); + } + + protected _getGlobalDevice (): GFXDevice | null { return cc.director.root.device; } @@ -508,6 +513,11 @@ export default class TextureBase extends Asset { return; } + const gfxDevice = this._getGlobalDevice(); + if (!gfxDevice) { + return; + } + const region: GFXBufferTextureCopy = { buffOffset: 0, buffStride: 0, @@ -531,9 +541,9 @@ export default class TextureBase extends Asset { }; if (data instanceof HTMLElement) { - this._getGlobalDevice().copyImageSourceToTexture(data, this._texture, [region]); + gfxDevice.copyImageSourceToTexture(data, this._texture, [region]); } else { - this._getGlobalDevice().copyBufferToTexture(data.buffer, this._texture, [region]); + gfxDevice.copyBufferToTexture(data.buffer, this._texture, [region]); } } @@ -568,7 +578,16 @@ export default class TextureBase extends Asset { } private _updateSampler () { - this._sampler = this._getGlobalDevice().createSampler({ + if (this._sampler) { + this._sampler.destroy(); + } + + const gfxDevice = this._getGlobalDevice(); + if (!gfxDevice) { + return; + } + + this._sampler = gfxDevice.createSampler({ name: `Sampler of ${this.name}`, minFilter: toGfxFilterMode(this._minFilter), magFilter: toGfxFilterMode(this._magFilter),