Feat/m code (#2933)

* Extract _createRenderBuffer to deduplicate renderbuffer creation in GLRenderTarget (#2897)

* refactor: simplify GLRenderTarget with private _createRenderBuffer method

* fix: text dirty flag set error (#2931)

---------

Co-authored-by: ChenMo <gl3336563@163.com>
This commit is contained in:
AZhan
2026-03-20 15:56:46 +08:00
committed by GitHub
3 changed files with 71 additions and 26 deletions

View File

@@ -245,13 +245,7 @@ export class GLRenderTarget implements IPlatformRenderTarget {
);
} else if (this._target.antiAliasing <= 1) {
const { internalFormat, attachment } = GLTexture._getRenderBufferDepthFormatDetail(_depth, gl, isWebGL2);
const depthRenderBuffer = gl.createRenderbuffer();
this._depthRenderBuffer = depthRenderBuffer;
gl.bindRenderbuffer(gl.RENDERBUFFER, depthRenderBuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, internalFormat, width, height);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment, gl.RENDERBUFFER, depthRenderBuffer);
this._depthRenderBuffer = this._createRenderBuffer(internalFormat, attachment);
}
}
@@ -262,33 +256,23 @@ export class GLRenderTarget implements IPlatformRenderTarget {
private _bindMSAAFBO(): void {
const gl = this._gl;
const isWebGL2 = this._isWebGL2;
const MSAADepthRenderBuffer = gl.createRenderbuffer();
/** @ts-ignore */
const { _depth, colorTextureCount, antiAliasing, width, height } = this._target;
const { _depth, colorTextureCount } = this._target;
this._blitDrawBuffers = new Array(colorTextureCount);
this._MSAADepthRenderBuffer = MSAADepthRenderBuffer;
gl.bindFramebuffer(gl.FRAMEBUFFER, this._MSAAFrameBuffer);
// prepare MRT+MSAA color RBOs
for (let i = 0; i < colorTextureCount; i++) {
const MSAAColorRenderBuffer = gl.createRenderbuffer();
this._MSAAColorRenderBuffers[i] = MSAAColorRenderBuffer;
this._blitDrawBuffers[i] = gl.NONE;
gl.bindRenderbuffer(gl.RENDERBUFFER, MSAAColorRenderBuffer);
gl.renderbufferStorageMultisample(
gl.RENDERBUFFER,
antiAliasing,
const internalFormat =
/** @ts-ignore */
(this._target.getColorTexture(i)._platformTexture as GLTexture)._formatDetail.internalFormat,
width,
height
);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.RENDERBUFFER, MSAAColorRenderBuffer);
(this._target.getColorTexture(i)._platformTexture as GLTexture)._formatDetail.internalFormat;
this._MSAAColorRenderBuffers[i] = this._createRenderBuffer(internalFormat, gl.COLOR_ATTACHMENT0 + i);
}
gl.drawBuffers(this._oriDrawBuffers);
@@ -300,9 +284,7 @@ export class GLRenderTarget implements IPlatformRenderTarget {
(_depth._platformTexture as GLTexture)._formatDetail
: GLTexture._getRenderBufferDepthFormatDetail(_depth, gl, isWebGL2);
gl.bindRenderbuffer(gl.RENDERBUFFER, MSAADepthRenderBuffer);
gl.renderbufferStorageMultisample(gl.RENDERBUFFER, antiAliasing, internalFormat, width, height);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment, gl.RENDERBUFFER, MSAADepthRenderBuffer);
this._MSAADepthRenderBuffer = this._createRenderBuffer(internalFormat, attachment);
}
this._checkFrameBuffer();
@@ -310,6 +292,24 @@ export class GLRenderTarget implements IPlatformRenderTarget {
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
}
private _createRenderBuffer(internalFormat: GLenum, attachment: GLenum): WebGLRenderbuffer {
const gl = this._gl;
const { width, height, antiAliasing } = this._target;
const renderBuffer = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, renderBuffer);
if (antiAliasing > 1) {
gl.renderbufferStorageMultisample(gl.RENDERBUFFER, antiAliasing, internalFormat, width, height);
} else {
gl.renderbufferStorage(gl.RENDERBUFFER, internalFormat, width, height);
}
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment, gl.RENDERBUFFER, renderBuffer);
return renderBuffer;
}
private _checkFrameBuffer(): void {
const gl = this._gl;
const e = gl.checkFramebufferStatus(gl.FRAMEBUFFER);

View File

@@ -336,7 +336,7 @@ export class Text extends UIRenderer implements ITextRenderer {
const canvas = this._getRootCanvas();
if (this._isContainDirtyFlag(DirtyFlag.LocalPositionBounds)) {
this._updateLocalData();
this._setDirtyFlagTrue(DirtyFlag.LocalPositionBounds);
this._setDirtyFlagFalse(DirtyFlag.LocalPositionBounds);
}
if (this._isContainDirtyFlag(DirtyFlag.WorldPosition)) {

View File

@@ -0,0 +1,45 @@
import { RenderTarget, Texture2D, TextureFormat } from "@galacean/engine-core";
import { WebGLEngine } from "@galacean/engine-rhi-webgl";
import { describe, beforeAll, expect, it } from "vitest";
describe("GLRenderTarget", () => {
let engine: WebGLEngine;
beforeAll(async () => {
const canvas = document.createElement("canvas");
engine = await WebGLEngine.create({ canvas });
});
describe("lifecycle", () => {
it("should create and destroy non-MSAA render target", () => {
const colorTexture = new Texture2D(engine, 512, 512);
const renderTarget = new RenderTarget(engine, 512, 512, colorTexture, TextureFormat.Depth16, 1);
// @ts-ignore
const glRenderTarget = renderTarget._platformRenderTarget as any;
expect(() => {
glRenderTarget.activeRenderTarget(0);
glRenderTarget.blitRenderTarget();
}).not.toThrow();
renderTarget.destroy();
});
it("should create and destroy MSAA render target", () => {
const colorTexture = new Texture2D(engine, 512, 512);
const renderTarget = new RenderTarget(engine, 512, 512, colorTexture, TextureFormat.Depth16, 4);
// @ts-ignore
const glRenderTarget = renderTarget._platformRenderTarget as any;
expect(() => {
glRenderTarget.activeRenderTarget(0);
glRenderTarget.blitRenderTarget();
}).not.toThrow();
renderTarget.destroy();
});
});
});