From d721d65052102c1302ccbb8d07e9129c13ffa094 Mon Sep 17 00:00:00 2001 From: James Chen Date: Sun, 29 Dec 2024 21:00:39 +0800 Subject: [PATCH 1/2] [v3.8.6] Optimize code size for webgl-texture.ts/webgl2-texture.ts --- cocos/gfx/webgl/webgl-texture.ts | 128 ++++++++++++++------------ cocos/gfx/webgl2/webgl2-texture.ts | 138 ++++++++++++++++------------- 2 files changed, 143 insertions(+), 123 deletions(-) diff --git a/cocos/gfx/webgl/webgl-texture.ts b/cocos/gfx/webgl/webgl-texture.ts index 3bf6754e8c1..05af19ff62e 100644 --- a/cocos/gfx/webgl/webgl-texture.ts +++ b/cocos/gfx/webgl/webgl-texture.ts @@ -45,39 +45,43 @@ export class WebGLTexture extends Texture { } public initialize (info: Readonly | Readonly, isSwapchainTexture?: boolean): void { + const self = this; + const { instance } = WebGLDeviceManager; let texInfo = info as Readonly; const viewInfo = info as Readonly; if ('texture' in info) { texInfo = viewInfo.texture.info; - this._isTextureView = true; + self._isTextureView = true; } - - this._info.copy(texInfo); - - this._isPowerOf2 = IsPowerOf2(this._info.width) && IsPowerOf2(this._info.height); - this._size = FormatSurfaceSize( - this._info.format, - this.width, - this.height, - this.depth, - this._info.levelCount, - ) * this._info.layerCount; - - if (!this._isTextureView) { - this._gpuTexture = { + const thisTextureInfo = this._info; + thisTextureInfo.copy(texInfo); + + const thisViewInfo = self._viewInfo; + + self._isPowerOf2 = IsPowerOf2(thisTextureInfo.width) && IsPowerOf2(thisTextureInfo.height); + self._size = FormatSurfaceSize( + thisTextureInfo.format, + self.width, + self.height, + self.depth, + thisTextureInfo.levelCount, + ) * thisTextureInfo.layerCount; + + if (!self._isTextureView) { + self._gpuTexture = { type: texInfo.type, format: texInfo.format, usage: texInfo.usage, width: texInfo.width, height: texInfo.height, depth: texInfo.depth, - size: this._size, + size: self._size, arrayLayer: texInfo.layerCount, mipLevel: texInfo.levelCount, samples: texInfo.samples, flags: texInfo.flags, - isPowerOf2: this._isPowerOf2, + isPowerOf2: self._isPowerOf2, glTarget: 0, glInternalFmt: 0, @@ -94,30 +98,32 @@ export class WebGLTexture extends Texture { isSwapchainTexture: isSwapchainTexture || false, }; - if (!this._gpuTexture.isSwapchainTexture) { - WebGLCmdFuncCreateTexture(WebGLDeviceManager.instance, this._gpuTexture); - WebGLDeviceManager.instance.memoryStatus.textureSize += this._size; + if (!self._gpuTexture.isSwapchainTexture) { + WebGLCmdFuncCreateTexture(instance, self._gpuTexture); + instance.memoryStatus.textureSize += self._size; } - this._viewInfo.texture = this; - this._viewInfo.type = info.type; - this._viewInfo.format = info.format; - this._viewInfo.baseLevel = 0; - this._viewInfo.levelCount = info.levelCount; - this._viewInfo.baseLayer = 0; - this._viewInfo.layerCount = info.layerCount; + thisViewInfo.texture = self; + thisViewInfo.type = info.type; + thisViewInfo.format = info.format; + thisViewInfo.baseLevel = 0; + thisViewInfo.levelCount = info.levelCount; + thisViewInfo.baseLayer = 0; + thisViewInfo.layerCount = info.layerCount; } else { - this._viewInfo.copy(viewInfo); - this._lodLevel = viewInfo.baseLevel; - this._gpuTexture = (viewInfo.texture as WebGLTexture)._gpuTexture; + thisViewInfo.copy(viewInfo); + self._lodLevel = viewInfo.baseLevel; + self._gpuTexture = (viewInfo.texture as WebGLTexture)._gpuTexture; } } public destroy (): void { - if (!this._isTextureView && this._gpuTexture) { - WebGLCmdFuncDestroyTexture(WebGLDeviceManager.instance, this._gpuTexture); - WebGLDeviceManager.instance.memoryStatus.textureSize -= this._size; - this._gpuTexture = null; + const self = this; + const { instance } = WebGLDeviceManager; + if (!self._isTextureView && self._gpuTexture) { + WebGLCmdFuncDestroyTexture(instance, self._gpuTexture); + instance.memoryStatus.textureSize -= self._size; + self._gpuTexture = null; } } @@ -137,36 +143,40 @@ export class WebGLTexture extends Texture { } public resize (width: number, height: number): void { - if (this._info.width === width && this._info.height === height) { + const self = this; + const { instance } = WebGLDeviceManager; + const thisTextureInfo = self._info; + if (thisTextureInfo.width === width && thisTextureInfo.height === height) { return; } - if (this._info.levelCount === WebGLTexture.getLevelCount(this._info.width, this._info.height)) { - this._info.levelCount = WebGLTexture.getLevelCount(width, height); - } else if (this._info.levelCount > 1) { - this._info.levelCount = Math.min(this._info.levelCount, WebGLTexture.getLevelCount(width, height)); + if (thisTextureInfo.levelCount === WebGLTexture.getLevelCount(thisTextureInfo.width, thisTextureInfo.height)) { + thisTextureInfo.levelCount = WebGLTexture.getLevelCount(width, height); + } else if (thisTextureInfo.levelCount > 1) { + thisTextureInfo.levelCount = Math.min(thisTextureInfo.levelCount, WebGLTexture.getLevelCount(width, height)); } - const oldSize = this._size; - this._info.width = width; - this._info.height = height; - - this._size = FormatSurfaceSize( - this._info.format, - this.width, - this.height, - this.depth, - this._info.levelCount, - ) * this._info.layerCount; - - if (!this._isTextureView && this._gpuTexture) { - this._gpuTexture.width = width; - this._gpuTexture.height = height; - this._gpuTexture.size = this._size; - if (!this._gpuTexture.isSwapchainTexture) { - WebGLCmdFuncResizeTexture(WebGLDeviceManager.instance, this._gpuTexture); - WebGLDeviceManager.instance.memoryStatus.textureSize -= oldSize; - WebGLDeviceManager.instance.memoryStatus.textureSize += this._size; + const oldSize = self._size; + thisTextureInfo.width = width; + thisTextureInfo.height = height; + + self._size = FormatSurfaceSize( + thisTextureInfo.format, + self.width, + self.height, + self.depth, + thisTextureInfo.levelCount, + ) * thisTextureInfo.layerCount; + + const thisGpuTexture = self._gpuTexture; + if (!self._isTextureView && thisGpuTexture) { + thisGpuTexture.width = width; + thisGpuTexture.height = height; + thisGpuTexture.size = self._size; + if (!thisGpuTexture.isSwapchainTexture) { + WebGLCmdFuncResizeTexture(instance, thisGpuTexture); + instance.memoryStatus.textureSize -= oldSize; + instance.memoryStatus.textureSize += self._size; } } } diff --git a/cocos/gfx/webgl2/webgl2-texture.ts b/cocos/gfx/webgl2/webgl2-texture.ts index 676df803a0c..63b03172028 100644 --- a/cocos/gfx/webgl2/webgl2-texture.ts +++ b/cocos/gfx/webgl2/webgl2-texture.ts @@ -49,39 +49,43 @@ export class WebGL2Texture extends Texture { } public initialize (info: Readonly | Readonly, isSwapchainTexture?: boolean): void { + const self = this; + const { instance } = WebGL2DeviceManager; + const thisTextureInfo = self._info; + const thisViewInfo = self._viewInfo; let texInfo = info as Readonly; const viewInfo = info as Readonly; if ('texture' in info) { texInfo = viewInfo.texture.info; - this._isTextureView = true; + self._isTextureView = true; } - this._info.copy(texInfo); + thisTextureInfo.copy(texInfo); - this._isPowerOf2 = IsPowerOf2(this._info.width) && IsPowerOf2(this._info.height); - this._size = FormatSurfaceSize( - this._info.format, - this.width, - this.height, - this.depth, - this._info.levelCount, - ) * this._info.layerCount; + self._isPowerOf2 = IsPowerOf2(thisTextureInfo.width) && IsPowerOf2(thisTextureInfo.height); + self._size = FormatSurfaceSize( + thisTextureInfo.format, + self.width, + self.height, + self.depth, + thisTextureInfo.levelCount, + ) * thisTextureInfo.layerCount; - if (!this._isTextureView) { - this._gpuTexture = { + if (!self._isTextureView) { + self._gpuTexture = { type: texInfo.type, format: texInfo.format, usage: texInfo.usage, width: texInfo.width, height: texInfo.height, depth: texInfo.depth, - size: this._size, + size: self._size, arrayLayer: texInfo.layerCount, mipLevel: texInfo.levelCount, samples: texInfo.samples, flags: texInfo.flags, - isPowerOf2: this._isPowerOf2, + isPowerOf2: self._isPowerOf2, glTarget: 0, glInternalFmt: 0, @@ -98,37 +102,37 @@ export class WebGL2Texture extends Texture { isSwapchainTexture: isSwapchainTexture || false, }; - if (!this._gpuTexture.isSwapchainTexture && this._gpuTexture) { - WebGL2CmdFuncCreateTexture(WebGL2DeviceManager.instance, this._gpuTexture); - WebGL2DeviceManager.instance.memoryStatus.textureSize += this._size; + if (!self._gpuTexture.isSwapchainTexture) { + WebGL2CmdFuncCreateTexture(instance, self._gpuTexture); + instance.memoryStatus.textureSize += self._size; } - this._viewInfo.texture = this; - this._viewInfo.type = info.type; - this._viewInfo.format = info.format; - this._viewInfo.baseLevel = 0; - this._viewInfo.levelCount = info.levelCount; - this._viewInfo.baseLayer = 0; - this._viewInfo.layerCount = info.layerCount; - - this._gpuTextureView = { - gpuTexture: this._gpuTexture, - type: this._viewInfo.type, - format: this._viewInfo.format, - baseLevel: this._viewInfo.baseLevel, - levelCount: this._viewInfo.levelCount, + thisViewInfo.texture = self; + thisViewInfo.type = info.type; + thisViewInfo.format = info.format; + thisViewInfo.baseLevel = 0; + thisViewInfo.levelCount = info.levelCount; + thisViewInfo.baseLayer = 0; + thisViewInfo.layerCount = info.layerCount; + + self._gpuTextureView = { + gpuTexture: self._gpuTexture, + type: thisViewInfo.type, + format: thisViewInfo.format, + baseLevel: thisViewInfo.baseLevel, + levelCount: thisViewInfo.levelCount, }; } else { - this._viewInfo.copy(viewInfo); - this._gpuTexture = (viewInfo.texture as WebGL2Texture)._gpuTexture; + thisViewInfo.copy(viewInfo); + self._gpuTexture = (viewInfo.texture as WebGL2Texture)._gpuTexture; - if (this._gpuTexture?.format !== texInfo.format) { + if (self._gpuTexture?.format !== texInfo.format) { logID(16403); return; } - this._gpuTextureView = { - gpuTexture: this._gpuTexture, + self._gpuTextureView = { + gpuTexture: self._gpuTexture, type: viewInfo.type, format: viewInfo.format, baseLevel: viewInfo.baseLevel, @@ -138,10 +142,12 @@ export class WebGL2Texture extends Texture { } public destroy (): void { - if (!this._isTextureView && this._gpuTexture) { - WebGL2CmdFuncDestroyTexture(WebGL2DeviceManager.instance, this._gpuTexture); - WebGL2DeviceManager.instance.memoryStatus.textureSize -= this._size; - this._gpuTexture = null; + const self = this; + const { instance } = WebGL2DeviceManager; + if (!self._isTextureView && self._gpuTexture) { + WebGL2CmdFuncDestroyTexture(instance, self._gpuTexture); + instance.memoryStatus.textureSize -= self._size; + self._gpuTexture = null; } } @@ -161,35 +167,39 @@ export class WebGL2Texture extends Texture { } public resize (width: number, height: number): void { - if (this._info.width === width && this._info.height === height) { + const self = this; + const { instance } = WebGL2DeviceManager; + const thisTextureInfo = self._info; + if (thisTextureInfo.width === width && thisTextureInfo.height === height) { return; } - if (this._info.levelCount === WebGL2Texture.getLevelCount(this._info.width, this._info.height)) { - this._info.levelCount = WebGL2Texture.getLevelCount(width, height); - } else if (this._info.levelCount > 1) { - this._info.levelCount = Math.min(this._info.levelCount, WebGL2Texture.getLevelCount(width, height)); + if (thisTextureInfo.levelCount === WebGL2Texture.getLevelCount(thisTextureInfo.width, thisTextureInfo.height)) { + thisTextureInfo.levelCount = WebGL2Texture.getLevelCount(width, height); + } else if (thisTextureInfo.levelCount > 1) { + thisTextureInfo.levelCount = Math.min(thisTextureInfo.levelCount, WebGL2Texture.getLevelCount(width, height)); } - const oldSize = this._size; - this._info.width = width; - this._info.height = height; - this._size = FormatSurfaceSize( - this._info.format, - this.width, - this.height, - this.depth, - this._info.levelCount, - ) * this._info.layerCount; - - if (!this._isTextureView && this._gpuTexture) { - this._gpuTexture.width = width; - this._gpuTexture.height = height; - this._gpuTexture.size = this._size; - if (!this._gpuTexture.isSwapchainTexture) { - WebGL2CmdFuncResizeTexture(WebGL2DeviceManager.instance, this._gpuTexture); - WebGL2DeviceManager.instance.memoryStatus.textureSize -= oldSize; - WebGL2DeviceManager.instance.memoryStatus.textureSize += this._size; + const oldSize = self._size; + thisTextureInfo.width = width; + thisTextureInfo.height = height; + self._size = FormatSurfaceSize( + thisTextureInfo.format, + self.width, + self.height, + self.depth, + thisTextureInfo.levelCount, + ) * thisTextureInfo.layerCount; + const thisGpuTexture = self._gpuTexture; + + if (!self._isTextureView && thisGpuTexture) { + thisGpuTexture.width = width; + thisGpuTexture.height = height; + thisGpuTexture.size = self._size; + if (!thisGpuTexture.isSwapchainTexture) { + WebGL2CmdFuncResizeTexture(instance, thisGpuTexture); + instance.memoryStatus.textureSize -= oldSize; + instance.memoryStatus.textureSize += self._size; } } } From 99b15bc630d4b96cd15cbb4aa9a3238dd34c0fc8 Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 30 Dec 2024 17:14:48 +0800 Subject: [PATCH 2/2] Update webgl-texture.ts --- cocos/gfx/webgl/webgl-texture.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/gfx/webgl/webgl-texture.ts b/cocos/gfx/webgl/webgl-texture.ts index 05af19ff62e..0a59f25550a 100644 --- a/cocos/gfx/webgl/webgl-texture.ts +++ b/cocos/gfx/webgl/webgl-texture.ts @@ -54,7 +54,7 @@ export class WebGLTexture extends Texture { texInfo = viewInfo.texture.info; self._isTextureView = true; } - const thisTextureInfo = this._info; + const thisTextureInfo = self._info; thisTextureInfo.copy(texInfo); const thisViewInfo = self._viewInfo;