fix the bugs found in PR9854. (#9875)

* Add extra steps to the destroy functions to update the cached resources.
This will make sure that the resources in device.stateCache is valid.

* unbind a resource before destroying it

* remove unnecessary non-null assertion

* update the state after the destruction to make the process robust

* fix the bugs introduced in PR9854.
bug: forgot to update the active texUnit in stateCache.

* reduce the use of dot operator to improve performance.

* fix the bug introduced in the previous commit

* reduce the use of dot operator to improve performance
This commit is contained in:
Yiwen
2022-01-05 23:20:47 +08:00
committed by GitHub
parent ac49b8b6a0
commit 8402289d77
3 changed files with 83 additions and 7 deletions
+35 -3
View File
@@ -950,13 +950,30 @@ export function WebGLCmdFuncCreateTexture (device: WebGLDevice, gpuTexture: IWeb
}
export function WebGLCmdFuncDestroyTexture (device: WebGLDevice, gpuTexture: IWebGLGPUTexture) {
const { gl } = device;
if (gpuTexture.glTexture) {
device.gl.deleteTexture(gpuTexture.glTexture);
const glTexUnits = device.stateCache.glTexUnits;
let texUnit = device.stateCache.texUnit;
gl.deleteTexture(gpuTexture.glTexture);
for (let i = 0; i < glTexUnits.length; i++) {
if (glTexUnits[i].glTexture === gpuTexture.glTexture) {
gl.activeTexture(gl.TEXTURE0 + i);
texUnit = i;
gl.bindTexture(gpuTexture.glTarget, null);
glTexUnits[i].glTexture = null;
}
}
device.stateCache.texUnit = texUnit;
gpuTexture.glTexture = null;
}
if (gpuTexture.glRenderbuffer) {
device.gl.deleteRenderbuffer(gpuTexture.glRenderbuffer);
let glRenderbuffer = device.stateCache.glRenderbuffer;
gl.deleteRenderbuffer(gpuTexture.glRenderbuffer);
if (glRenderbuffer === gpuTexture.glRenderbuffer) {
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
glRenderbuffer = null;
}
gpuTexture.glRenderbuffer = null;
}
}
@@ -1164,6 +1181,10 @@ export function WebGLCmdFuncCreateFramebuffer (device: WebGLDevice, gpuFramebuff
export function WebGLCmdFuncDestroyFramebuffer (device: WebGLDevice, gpuFramebuffer: IWebGLGPUFramebuffer) {
if (gpuFramebuffer.glFramebuffer) {
device.gl.deleteFramebuffer(gpuFramebuffer.glFramebuffer);
if (device.stateCache.glFramebuffer === gpuFramebuffer.glFramebuffer) {
device.gl.bindFramebuffer(device.gl.FRAMEBUFFER, null);
device.stateCache.glFramebuffer = null;
}
gpuFramebuffer.glFramebuffer = null;
}
}
@@ -1517,6 +1538,10 @@ export function WebGLCmdFuncDestroyShader (device: WebGLDevice, gpuShader: IWebG
}
}
gl.deleteProgram(gpuShader.glProgram);
if (device.stateCache.glProgram === gpuShader.glProgram) {
device.gl.useProgram(null);
device.stateCache.glProgram = null;
}
gpuShader.glProgram = null;
}
}
@@ -1558,10 +1583,17 @@ export function WebGLCmdFuncCreateInputAssember (device: WebGLDevice, gpuInputAs
export function WebGLCmdFuncDestroyInputAssembler (device: WebGLDevice, gpuInputAssembler: IWebGLGPUInputAssembler) {
const it = gpuInputAssembler.glVAOs.values();
let res = it.next();
const OES_vertex_array_object = device.extensions.OES_vertex_array_object!;
let glVAO = device.stateCache.glVAO;
while (!res.done) {
device.extensions.OES_vertex_array_object!.deleteVertexArrayOES(res.value);
OES_vertex_array_object.deleteVertexArrayOES(res.value);
if (glVAO === res.value) {
OES_vertex_array_object.bindVertexArrayOES(null);
glVAO = null;
}
res = it.next();
}
device.stateCache.glVAO = glVAO;
gpuInputAssembler.glVAOs.clear();
}
+4
View File
@@ -116,6 +116,10 @@ export class WebGLDevice extends Device {
this._caps.maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
this._caps.maxCubeMapTextureSize = gl.getParameter(gl.MAX_CUBE_MAP_TEXTURE_SIZE);
// WebGL doesn't support UBOs at all, so here we return
// the guaranteed minimum number of available bindings in WebGL2
this._caps.maxUniformBufferBindings = 16;
const extensions = gl.getSupportedExtensions();
let extStr = '';
if (extensions) {
+44 -4
View File
@@ -1119,13 +1119,30 @@ export function WebGL2CmdFuncCreateTexture (device: WebGL2Device, gpuTexture: IW
}
export function WebGL2CmdFuncDestroyTexture (device: WebGL2Device, gpuTexture: IWebGL2GPUTexture) {
const { gl } = device;
if (gpuTexture.glTexture) {
device.gl.deleteTexture(gpuTexture.glTexture);
const glTexUnits = device.stateCache.glTexUnits;
let texUnit = device.stateCache.texUnit;
gl.deleteTexture(gpuTexture.glTexture);
for (let i = 0; i < glTexUnits.length; ++i) {
if (glTexUnits[i].glTexture === gpuTexture.glTexture) {
gl.activeTexture(gl.TEXTURE0 + i);
texUnit = i;
gl.bindTexture(gpuTexture.glTarget, null);
glTexUnits[i].glTexture = null;
}
}
device.stateCache.texUnit = texUnit;
gpuTexture.glTexture = null;
}
if (gpuTexture.glRenderbuffer) {
device.gl.deleteRenderbuffer(gpuTexture.glRenderbuffer);
let glRenderbuffer = device.stateCache.glRenderbuffer;
gl.deleteRenderbuffer(gpuTexture.glRenderbuffer);
if (glRenderbuffer === gpuTexture.glRenderbuffer) {
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
glRenderbuffer = null;
}
gpuTexture.glRenderbuffer = null;
}
}
@@ -1264,8 +1281,16 @@ export function WebGL2CmdFuncCreateSampler (device: WebGL2Device, gpuSampler: IW
}
export function WebGL2CmdFuncDestroySampler (device: WebGL2Device, gpuSampler: IWebGL2GPUSampler) {
const { gl } = device;
if (gpuSampler.glSampler) {
device.gl.deleteSampler(gpuSampler.glSampler);
gl.deleteSampler(gpuSampler.glSampler);
const glSamplerUnits = device.stateCache.glSamplerUnits;
for (let i = 0; i < glSamplerUnits.length; ++i) {
if (glSamplerUnits[i] === gpuSampler.glSampler) {
gl.bindSampler(i, null);
glSamplerUnits[i] = null;
}
}
gpuSampler.glSampler = null;
}
}
@@ -1373,6 +1398,10 @@ export function WebGL2CmdFuncCreateFramebuffer (device: WebGL2Device, gpuFramebu
export function WebGL2CmdFuncDestroyFramebuffer (device: WebGL2Device, gpuFramebuffer: IWebGL2GPUFramebuffer) {
if (gpuFramebuffer.glFramebuffer) {
device.gl.deleteFramebuffer(gpuFramebuffer.glFramebuffer);
if (device.stateCache.glFramebuffer === gpuFramebuffer.glFramebuffer) {
device.gl.bindFramebuffer(device.gl.FRAMEBUFFER, null);
device.stateCache.glFramebuffer = null;
}
gpuFramebuffer.glFramebuffer = null;
}
}
@@ -1657,6 +1686,10 @@ export function WebGL2CmdFuncCreateShader (device: WebGL2Device, gpuShader: IWeb
export function WebGL2CmdFuncDestroyShader (device: WebGL2Device, gpuShader: IWebGL2GPUShader) {
if (gpuShader.glProgram) {
device.gl.deleteProgram(gpuShader.glProgram);
if (device.stateCache.glProgram === gpuShader.glProgram) {
device.gl.useProgram(null);
device.stateCache.glProgram = null;
}
gpuShader.glProgram = null;
}
}
@@ -1699,10 +1732,17 @@ export function WebGL2CmdFuncCreateInputAssember (device: WebGL2Device, gpuInput
export function WebGL2CmdFuncDestroyInputAssembler (device: WebGL2Device, gpuInputAssembler: IWebGL2GPUInputAssembler) {
const it = gpuInputAssembler.glVAOs.values();
let res = it.next();
const { gl } = device;
let glVAO = device.stateCache.glVAO;
while (!res.done) {
device.gl.deleteVertexArray(res.value);
gl.deleteVertexArray(res.value);
if (glVAO === res.value) {
gl.bindVertexArray(null);
glVAO = null;
}
res = it.next();
}
device.stateCache.glVAO = glVAO;
gpuInputAssembler.glVAOs.clear();
}