Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3.8.6] Optimize code size for webgl-swapchain.ts & webgl2-swapchain.ts #18097

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 39 additions & 36 deletions cocos/gfx/webgl/webgl-swapchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,18 @@ export class WebGLSwapchain extends Swapchain {
}

public initialize (info: Readonly<SwapchainInfo>): void {
this._canvas = info.windowHandle;
const self = this;
self._canvas = info.windowHandle;

this._webGLContextLostHandler = this._onWebGLContextLost.bind(this);
this._canvas.addEventListener(eventWebGLContextLost, this._webGLContextLostHandler);
self._webGLContextLostHandler = self._onWebGLContextLost.bind(self);
self._canvas.addEventListener(eventWebGLContextLost, self._webGLContextLostHandler);

const { instance } = WebGLDeviceManager;
const { gl, capabilities } = instance;

this.stateCache.initialize(capabilities.maxTextureUnits, capabilities.maxVertexAttributes);
self.stateCache.initialize(capabilities.maxTextureUnits, capabilities.maxVertexAttributes);

this._extensions = getExtensions(gl);
self._extensions = getExtensions(gl);

// init states
initStates(gl);
Expand All @@ -266,24 +267,24 @@ export class WebGLSwapchain extends Swapchain {
if (depthBits && stencilBits) depthStencilFmt = Format.DEPTH_STENCIL;
else if (depthBits) depthStencilFmt = Format.DEPTH;

this._colorTexture = new WebGLTexture();
this._colorTexture.initAsSwapchainTexture({
swapchain: this,
self._colorTexture = new WebGLTexture();
self._colorTexture.initAsSwapchainTexture({
swapchain: self,
format: colorFmt,
width: info.width,
height: info.height,
});

this._depthStencilTexture = new WebGLTexture();
this._depthStencilTexture.initAsSwapchainTexture({
swapchain: this,
self._depthStencilTexture = new WebGLTexture();
self._depthStencilTexture.initAsSwapchainTexture({
swapchain: self,
format: depthStencilFmt,
width: info.width,
height: info.height,
});

// create default null texture
this.nullTex2D = instance.createTexture(new TextureInfo(
self.nullTex2D = instance.createTexture(new TextureInfo(
TextureType.TEX2D,
TextureUsageBit.SAMPLED,
Format.RGBA8,
Expand All @@ -292,7 +293,7 @@ export class WebGLSwapchain extends Swapchain {
TextureFlagBit.GEN_MIPMAP,
)) as WebGLTexture;

this.nullTexCube = instance.createTexture(new TextureInfo(
self.nullTexCube = instance.createTexture(new TextureInfo(
TextureType.CUBE,
TextureUsageBit.SAMPLED,
Format.RGBA8,
Expand All @@ -306,51 +307,53 @@ export class WebGLSwapchain extends Swapchain {
nullTexRegion.texExtent.width = 2;
nullTexRegion.texExtent.height = 2;

const nullTexBuff = new Uint8Array(this.nullTex2D.size);
const nullTexBuff = new Uint8Array(self.nullTex2D.size);
nullTexBuff.fill(0);
instance.copyBuffersToTexture([nullTexBuff], this.nullTex2D, [nullTexRegion]);
instance.copyBuffersToTexture([nullTexBuff], self.nullTex2D, [nullTexRegion]);

nullTexRegion.texSubres.layerCount = 6;
instance.copyBuffersToTexture(
[nullTexBuff, nullTexBuff, nullTexBuff, nullTexBuff, nullTexBuff, nullTexBuff],
this.nullTexCube,
self.nullTexCube,
[nullTexRegion],
);
this._blitManager = new IWebGLBlitManager();
self._blitManager = new IWebGLBlitManager();
}

public destroy (): void {
if (this._canvas && this._webGLContextLostHandler) {
this._canvas.removeEventListener(eventWebGLContextLost, this._webGLContextLostHandler);
this._webGLContextLostHandler = null;
const self = this;
if (self._canvas && self._webGLContextLostHandler) {
self._canvas.removeEventListener(eventWebGLContextLost, self._webGLContextLostHandler);
self._webGLContextLostHandler = null;
}

if (this.nullTex2D) {
this.nullTex2D.destroy();
this.nullTex2D = null!;
if (self.nullTex2D) {
self.nullTex2D.destroy();
self.nullTex2D = null!;
}

if (this.nullTexCube) {
this.nullTexCube.destroy();
this.nullTexCube = null!;
if (self.nullTexCube) {
self.nullTexCube.destroy();
self.nullTexCube = null!;
}

if (this._blitManager) {
this._blitManager.destroy();
this._blitManager = null!;
if (self._blitManager) {
self._blitManager.destroy();
self._blitManager = null!;
}

this._extensions = null;
this._canvas = null;
self._extensions = null;
self._canvas = null;
}

public resize (width: number, height: number, surfaceTransform: SurfaceTransform): void {
if (this._colorTexture.width !== width || this._colorTexture.height !== height) {
const self = this;
if (self._colorTexture.width !== width || self._colorTexture.height !== height) {
debug(`Resizing swapchain: ${width}x${height}`);
this._canvas!.width = width;
this._canvas!.height = height;
this._colorTexture.resize(width, height);
this._depthStencilTexture.resize(width, height);
self._canvas!.width = width;
self._canvas!.height = height;
self._colorTexture.resize(width, height);
self._depthStencilTexture.resize(width, height);
}
}

Expand Down
86 changes: 45 additions & 41 deletions cocos/gfx/webgl2/webgl2-swapchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,22 @@ export class WebGL2Swapchain extends Swapchain {
private _blitManager: IWebGL2BlitManager | null = null;

public initialize (info: Readonly<SwapchainInfo>): void {
this._canvas = info.windowHandle;
const self = this;
self._canvas = info.windowHandle;

this._webGL2ContextLostHandler = this._onWebGLContextLost.bind(this);
this._canvas.addEventListener(eventWebGLContextLost, this._onWebGLContextLost);
self._webGL2ContextLostHandler = self._onWebGLContextLost.bind(self);
self._canvas.addEventListener(eventWebGLContextLost, self._onWebGLContextLost);

const gl = WebGL2DeviceManager.instance.gl;
const { instance } = WebGL2DeviceManager;
const { gl, capabilities } = instance;

this.stateCache.initialize(
WebGL2DeviceManager.instance.capabilities.maxTextureUnits,
WebGL2DeviceManager.instance.capabilities.maxUniformBufferBindings,
WebGL2DeviceManager.instance.capabilities.maxVertexAttributes,
self.stateCache.initialize(
capabilities.maxTextureUnits,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid to invoke WebGL2DeviceManager.instance.capabilities many times which could not be mangled.

capabilities.maxUniformBufferBindings,
capabilities.maxVertexAttributes,
);

this._extensions = getExtensions(gl);
self._extensions = getExtensions(gl);

// init states
initStates(gl);
Expand All @@ -207,24 +209,24 @@ export class WebGL2Swapchain extends Swapchain {
if (depthBits && stencilBits) depthStencilFmt = Format.DEPTH_STENCIL;
else if (depthBits) depthStencilFmt = Format.DEPTH;

this._colorTexture = new WebGL2Texture();
this._colorTexture.initAsSwapchainTexture({
swapchain: this,
self._colorTexture = new WebGL2Texture();
self._colorTexture.initAsSwapchainTexture({
swapchain: self,
format: colorFmt,
width: info.width,
height: info.height,
});

this._depthStencilTexture = new WebGL2Texture();
this._depthStencilTexture.initAsSwapchainTexture({
swapchain: this,
self._depthStencilTexture = new WebGL2Texture();
self._depthStencilTexture.initAsSwapchainTexture({
swapchain: self,
format: depthStencilFmt,
width: info.width,
height: info.height,
});

// create default null texture
this.nullTex2D = WebGL2DeviceManager.instance.createTexture(new TextureInfo(
self.nullTex2D = instance.createTexture(new TextureInfo(
TextureType.TEX2D,
TextureUsageBit.SAMPLED,
Format.RGBA8,
Expand All @@ -233,7 +235,7 @@ export class WebGL2Swapchain extends Swapchain {
TextureFlagBit.NONE,
)) as WebGL2Texture;

this.nullTexCube = WebGL2DeviceManager.instance.createTexture(new TextureInfo(
self.nullTexCube = instance.createTexture(new TextureInfo(
TextureType.CUBE,
TextureUsageBit.SAMPLED,
Format.RGBA8,
Expand All @@ -247,52 +249,54 @@ export class WebGL2Swapchain extends Swapchain {
nullTexRegion.texExtent.width = 2;
nullTexRegion.texExtent.height = 2;

const nullTexBuff = new Uint8Array(this.nullTex2D.size);
const nullTexBuff = new Uint8Array(self.nullTex2D.size);
nullTexBuff.fill(0);
WebGL2DeviceManager.instance.copyBuffersToTexture([nullTexBuff], this.nullTex2D, [nullTexRegion]);
instance.copyBuffersToTexture([nullTexBuff], self.nullTex2D, [nullTexRegion]);

nullTexRegion.texSubres.layerCount = 6;
WebGL2DeviceManager.instance.copyBuffersToTexture(
instance.copyBuffersToTexture(
[nullTexBuff, nullTexBuff, nullTexBuff, nullTexBuff, nullTexBuff, nullTexBuff],
this.nullTexCube,
self.nullTexCube,
[nullTexRegion],
);

this._blitManager = new IWebGL2BlitManager();
self._blitManager = new IWebGL2BlitManager();
}

public destroy (): void {
if (this._canvas && this._webGL2ContextLostHandler) {
this._canvas.removeEventListener(eventWebGLContextLost, this._webGL2ContextLostHandler);
this._webGL2ContextLostHandler = null;
const self = this;
if (self._canvas && self._webGL2ContextLostHandler) {
self._canvas.removeEventListener(eventWebGLContextLost, self._webGL2ContextLostHandler);
self._webGL2ContextLostHandler = null;
}

if (this.nullTex2D) {
this.nullTex2D.destroy();
this.nullTex2D = null!;
if (self.nullTex2D) {
self.nullTex2D.destroy();
self.nullTex2D = null!;
}

if (this.nullTexCube) {
this.nullTexCube.destroy();
this.nullTexCube = null!;
if (self.nullTexCube) {
self.nullTexCube.destroy();
self.nullTexCube = null!;
}

if (this._blitManager) {
this._blitManager.destroy();
this._blitManager = null;
if (self._blitManager) {
self._blitManager.destroy();
self._blitManager = null;
}

this._extensions = null;
this._canvas = null;
self._extensions = null;
self._canvas = null;
}

public resize (width: number, height: number, surfaceTransform: SurfaceTransform): void {
if (this._colorTexture.width !== width || this._colorTexture.height !== height) {
const self = this;
if (self._colorTexture.width !== width || self._colorTexture.height !== height) {
debug(`Resizing swapchain: ${width}x${height}`);
this._canvas!.width = width;
this._canvas!.height = height;
this._colorTexture.resize(width, height);
this._depthStencilTexture.resize(width, height);
self._canvas!.width = width;
self._canvas!.height = height;
self._colorTexture.resize(width, height);
self._depthStencilTexture.resize(width, height);
}
}

Expand Down
Loading