diff --git a/cocos/gfx/webgl/webgl-commands.ts b/cocos/gfx/webgl/webgl-commands.ts index a9a96d41d1..088c9a8e77 100644 --- a/cocos/gfx/webgl/webgl-commands.ts +++ b/cocos/gfx/webgl/webgl-commands.ts @@ -1958,7 +1958,9 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web let glTexUnit: IWebGLTexUnit | null = null; - if (gpuBinding.gpuTexView && gpuBinding.gpuTexView.gpuTexture.size > 0) { + if (gpuBinding.gpuTexView && + gpuBinding.gpuTexView.gpuTexture.glTexture !== 0 && + gpuBinding.gpuTexView.gpuTexture.size > 0) { const gpuTexture = gpuBinding.gpuTexView.gpuTexture; switch (glSampler.glType) { case WebGLRenderingContext.SAMPLER_2D: { diff --git a/cocos/gfx/webgl/webgl-device.ts b/cocos/gfx/webgl/webgl-device.ts index f9c2242a63..22c88840a4 100644 --- a/cocos/gfx/webgl/webgl-device.ts +++ b/cocos/gfx/webgl/webgl-device.ts @@ -363,7 +363,7 @@ export class WebGLGFXDevice extends GFXDevice { } public resize (width: number, height: number) { - if (this._width !== width && this._height !== height) { + if (this._width !== width || this._height !== height) { console.log('RESIZING DEVICE: ' + width + 'x' + height); this._canvas!.width = width; this._canvas!.height = height; diff --git a/cocos/gfx/webgl2/webgl2-commands.ts b/cocos/gfx/webgl2/webgl2-commands.ts index 13d61501ef..f65da22e97 100644 --- a/cocos/gfx/webgl2/webgl2-commands.ts +++ b/cocos/gfx/webgl2/webgl2-commands.ts @@ -1998,7 +1998,7 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W for (const glBlock of gpuShader.glBlocks) { if (glBlock.binding === gpuBinding.binding) { if (cache.glBindUBOs[glBlock.binding] !== gpuBinding.gpuBuffer.glBuffer) { - gl.bindBufferBase(WebGL2RenderingContext.UNIFORM_BUFFER, glBlock.binding, gpuBinding.gpuBuffer.glBuffer); + gl.bindBufferBase(gl.UNIFORM_BUFFER, glBlock.binding, gpuBinding.gpuBuffer.glBuffer); // gl.bindBufferRange(WebGL2RenderingContext.UNIFORM_BUFFER, glBlock.binding, gpuBinding.gpuBuffer.glBuffer, 0, gpuBinding.gpuBuffer.size); cache.glBindUBOs[glBlock.binding] = gpuBinding.gpuBuffer.glBuffer; cache.glUniformBuffer = gpuBinding.gpuBuffer.glBuffer; @@ -2026,29 +2026,36 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W if (glSampler) { for (const texUnit of glSampler.units) { - if (cache.texUnit !== texUnit) { - gl.activeTexture(WebGL2RenderingContext.TEXTURE0 + texUnit); - cache.texUnit = texUnit; - } - let glTexUnit: IWebGL2TexUnit | null = null; - if (gpuBinding.gpuTexView && gpuBinding.gpuTexView.gpuTexture.size > 0) { + if (gpuBinding.gpuTexView && + gpuBinding.gpuTexView.gpuTexture.size > 0) { + + if (cache.texUnit !== texUnit) { + gl.activeTexture(gl.TEXTURE0 + texUnit); + cache.texUnit = texUnit; + } + const gpuTexture = gpuBinding.gpuTexView.gpuTexture; switch (glSampler.glType) { - case WebGL2RenderingContext.SAMPLER_2D: { + case gl.SAMPLER_2D: { glTexUnit = cache.glTex2DUnits[texUnit]; + if (glTexUnit.glTexture !== gpuTexture.glTexture) { - gl.bindTexture(WebGL2RenderingContext.TEXTURE_2D, gpuTexture.glTexture); + if (gpuTexture.glTexture) { + gl.bindTexture(gl.TEXTURE_2D, gpuTexture.glTexture); + } else { + gl.bindTexture(gl.TEXTURE_2D, device.nullTex2D!.gpuTexture.glTexture); + } glTexUnit.glTexture = gpuTexture.glTexture; } break; } - case WebGL2RenderingContext.SAMPLER_CUBE: { + case gl.SAMPLER_CUBE: { glTexUnit = cache.glTexCubeUnits[texUnit]; if (glTexUnit.glTexture !== gpuTexture.glTexture) { - gl.bindTexture(WebGL2RenderingContext.TEXTURE_CUBE_MAP, gpuTexture.glTexture); + gl.bindTexture(gl.TEXTURE_CUBE_MAP, gpuTexture.glTexture); glTexUnit.glTexture = gpuTexture.glTexture; } break; @@ -2092,8 +2099,8 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W gl.bindVertexArray(_glVAO); cache.glVAO = _glVAO; - gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, null); - gl.bindBuffer(WebGLRenderingContext.ELEMENT_ARRAY_BUFFER, null); + gl.bindBuffer(gl.ARRAY_BUFFER, null); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null); cache.glArrayBuffer = 0; cache.glElementArrayBuffer = 0; @@ -2110,7 +2117,7 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W if (glAttrib) { if (cache.glArrayBuffer !== glAttrib.glBuffer) { - gl.bindBuffer(WebGL2RenderingContext.ARRAY_BUFFER, glAttrib.glBuffer); + gl.bindBuffer(gl.ARRAY_BUFFER, glAttrib.glBuffer); cache.glArrayBuffer = glAttrib.glBuffer; } @@ -2130,7 +2137,7 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W const gpuBuffer = gpuInputAssembler.gpuIndexBuffer; if (gpuBuffer) { if (cache.glElementArrayBuffer !== gpuBuffer.glBuffer) { - gl.bindBuffer(WebGL2RenderingContext.ELEMENT_ARRAY_BUFFER, gpuBuffer.glBuffer); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, gpuBuffer.glBuffer); cache.glElementArrayBuffer = gpuBuffer.glBuffer; } } diff --git a/cocos/gfx/webgl2/webgl2-device.ts b/cocos/gfx/webgl2/webgl2-device.ts index 50c7b7d3b0..ff762e4735 100644 --- a/cocos/gfx/webgl2/webgl2-device.ts +++ b/cocos/gfx/webgl2/webgl2-device.ts @@ -2,7 +2,7 @@ import { GFXBindingLayout, IGFXBindingLayoutInfo } from '../binding-layout'; import { GFXBuffer, IGFXBufferInfo } from '../buffer'; import { GFXCommandAllocator, IGFXCommandAllocatorInfo } from '../command-allocator'; import { GFXCommandBuffer, IGFXCommandBufferInfo } from '../command-buffer'; -import { GFXBufferTextureCopy, GFXFilter, GFXFormat, GFXFormatInfos, GFXFormatSize, GFXQueueType, IGFXRect } from '../define'; +import { GFXBufferTextureCopy, GFXFilter, GFXFormat, GFXFormatInfos, GFXFormatSize, GFXQueueType, IGFXRect, GFXTextureType, GFXTextureUsageBit, GFXTextureFlagBit, GFXTextureViewType } from '../define'; import { GFXAPI, GFXDevice, GFXFeature, IGFXDeviceInfo } from '../device'; import { GFXFramebuffer, IGFXFramebufferInfo } from '../framebuffer'; import { GFXInputAssembler, IGFXInputAssemblerInfo } from '../input-assembler'; @@ -76,6 +76,8 @@ export class WebGL2GFXDevice extends GFXDevice { } public stateCache: WebGL2StateCache = new WebGL2StateCache(); + public nullTex2D: WebGL2GFXTexture | null = null; + public nullTexCube: WebGL2GFXTexture | null = null; private _webGL2RC: WebGL2RenderingContext | null = null; private _isAntialias: boolean = true; @@ -257,11 +259,41 @@ export class WebGL2GFXDevice extends GFXDevice { this._cmdAllocator = this.createCommandAllocator({}); + this.nullTex2D = new WebGL2GFXTexture(this); + this.nullTex2D.initialize({ + type: GFXTextureType.TEX2D, + usage: GFXTextureUsageBit.SAMPLED, + format: GFXFormat.RGBA8, + width: 2, + height: 2, + }); + + this.nullTexCube = new WebGL2GFXTexture(this); + this.nullTexCube.initialize({ + type: GFXTextureType.TEX2D, + usage: GFXTextureUsageBit.SAMPLED, + format: GFXFormat.RGBA8, + width: 2, + height: 2, + arrayLayer: 6, + flags: GFXTextureFlagBit.CUBEMAP, + }); + return true; } public destroy (): void { + if (this.nullTex2D) { + this.nullTex2D.destroy(); + this.nullTex2D = null; + } + + if (this.nullTexCube) { + this.nullTexCube.destroy(); + this.nullTexCube = null; + } + if (this._mainWindow) { this._mainWindow.destroy(); this._mainWindow = null; @@ -281,7 +313,7 @@ export class WebGL2GFXDevice extends GFXDevice { } public resize (width: number, height: number) { - if (this._width !== width && this._height !== height) { + if (this._width !== width || this._height !== height) { console.log('RESIZING DEVICE: ' + width + 'x' + height); this._canvas!.width = width; this._canvas!.height = height; diff --git a/cocos/gfx/webgl2/webgl2-gpu-objects.ts b/cocos/gfx/webgl2/webgl2-gpu-objects.ts index 90cdff9de3..ff18ddfb35 100644 --- a/cocos/gfx/webgl2/webgl2-gpu-objects.ts +++ b/cocos/gfx/webgl2/webgl2-gpu-objects.ts @@ -66,7 +66,7 @@ export class WebGL2GPUTexture { public glFormat: GLenum = 0; public glType: GLenum = 0; public glUsage: GLenum = 0; - public glTexture: WebGLTexture = 0; + public glTexture: WebGLTexture | null = null; public glRenderbuffer: WebGLRenderbuffer = 0; public glWrapS: GLenum = 0; public glWrapT: GLenum = 0; diff --git a/cocos/gfx/webgl2/webgl2-state-cache.ts b/cocos/gfx/webgl2/webgl2-state-cache.ts index 209de3d736..fd614a7ac5 100644 --- a/cocos/gfx/webgl2/webgl2-state-cache.ts +++ b/cocos/gfx/webgl2/webgl2-state-cache.ts @@ -2,7 +2,7 @@ import { GFX_MAX_BUFFER_BINDINGS, GFX_MAX_TEXTURE_UNITS, GFX_MAX_VERTEX_ATTRIBUT import { GFXBlendState, GFXDepthStencilState, GFXRasterizerState } from '../pipeline-state'; export interface IWebGL2TexUnit { - glTexture: WebGLTexture; + glTexture: WebGLTexture | null; } export class WebGL2StateCache { @@ -44,10 +44,10 @@ export class WebGL2StateCache { for (let i = 0; i < GFX_MAX_TEXTURE_UNITS; ++i) { this.glTex2DUnits[i] = { - glTexture: 0, + glTexture: null, }; this.glTexCubeUnits[i] = { - glTexture: 0, + glTexture: null, }; this.glSamplerUnits[i] = 0; } diff --git a/cocos/gfx/webgl2/webgl2-texture.ts b/cocos/gfx/webgl2/webgl2-texture.ts index 06ff31559c..bcd4aca3de 100644 --- a/cocos/gfx/webgl2/webgl2-texture.ts +++ b/cocos/gfx/webgl2/webgl2-texture.ts @@ -120,7 +120,7 @@ export class WebGL2GFXTexture extends GFXTexture { glFormat: 0, glType: 0, glUsage: 0, - glTexture: 0, + glTexture: null, glRenderbuffer: 0, glWrapS: 0, glWrapT: 0,