Skip to content

Commit

Permalink
[v3.8.6] Optimize code size for webgl-texture.ts/webgl2-texture.ts (#…
Browse files Browse the repository at this point in the history
…18098)

* [v3.8.6] Optimize code size for webgl-texture.ts/webgl2-texture.ts

* Update webgl-texture.ts
  • Loading branch information
dumganhar authored Dec 30, 2024
1 parent f3ee5c8 commit 01ebd4a
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 123 deletions.
128 changes: 69 additions & 59 deletions cocos/gfx/webgl/webgl-texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,39 +45,43 @@ export class WebGLTexture extends Texture {
}

public initialize (info: Readonly<TextureInfo> | Readonly<TextureViewInfo>, isSwapchainTexture?: boolean): void {
const self = this;
const { instance } = WebGLDeviceManager;
let texInfo = info as Readonly<TextureInfo>;
const viewInfo = info as Readonly<TextureViewInfo>;

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 = self._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,
Expand All @@ -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;
}
}

Expand All @@ -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;
}
}
}
Expand Down
138 changes: 74 additions & 64 deletions cocos/gfx/webgl2/webgl2-texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,39 +49,43 @@ export class WebGL2Texture extends Texture {
}

public initialize (info: Readonly<TextureInfo> | Readonly<TextureViewInfo>, isSwapchainTexture?: boolean): void {
const self = this;
const { instance } = WebGL2DeviceManager;
const thisTextureInfo = self._info;
const thisViewInfo = self._viewInfo;
let texInfo = info as Readonly<TextureInfo>;
const viewInfo = info as Readonly<TextureViewInfo>;

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,
Expand All @@ -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,
Expand All @@ -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;
}
}

Expand All @@ -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;
}
}
}
Expand Down

0 comments on commit 01ebd4a

Please sign in to comment.