Skip to content

Commit

Permalink
feat: just copy to internalRT when rt.format === internalFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxudong committed Dec 2, 2024
1 parent 1aba368 commit 89a163f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 120 deletions.
49 changes: 29 additions & 20 deletions packages/core/src/RenderPipeline/BasicRenderPipeline.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Vector2 } from "@galacean/engine-math";
import { Background } from "../Background";
import { Camera } from "../Camera";
import { Logger } from "../base/Logger";
import { BackgroundMode } from "../enums/BackgroundMode";
import { BackgroundTextureFillMode } from "../enums/BackgroundTextureFillMode";
import { CameraClearFlags } from "../enums/CameraClearFlags";
Expand Down Expand Up @@ -46,6 +47,7 @@ export class BasicRenderPipeline {
private _grabTexture: Texture2D;
private _canUseBlitFrameBuffer = false;
private _shouldGrabColor = false;
private _canJustCopyToInternalRT = false;

/**
* Create a basic render pipeline.
Expand Down Expand Up @@ -85,6 +87,7 @@ export class BasicRenderPipeline {
const sunlight = scene._lightManager._sunlight;
const depthOnlyPass = this._depthOnlyPass;
const depthPassEnabled = camera.depthTextureMode === DepthTextureMode.PrePass && depthOnlyPass._supportDepthTexture;
const internalFormat = camera._getInternalColorTextureFormat();
const finalClearFlags = camera.clearFlags & ~(ignoreClear ?? CameraClearFlags.None);
const independentCanvasEnabled = camera.independentCanvasEnabled;
this._shouldGrabColor = independentCanvasEnabled && !(finalClearFlags & CameraClearFlags.Color);
Expand All @@ -93,6 +96,7 @@ export class BasicRenderPipeline {
// 3. Can't blit screen FBO to normal FBO in some platform when antialias enabled
this._canUseBlitFrameBuffer =
rhi.isWebGL2 && camera.msaaSamples === 1 && (!!camera.renderTarget || !rhi.context.antialias);
this._canJustCopyToInternalRT = camera.renderTarget?.getColorTexture().format === internalFormat;

if (scene.castShadows && sunlight && sunlight.shadowType !== ShadowType.None) {
this._cascadedShadowCasterPass.onRender(context);
Expand Down Expand Up @@ -126,7 +130,7 @@ export class BasicRenderPipeline {
this._internalColorTarget,
viewport.width,
viewport.height,
camera._getInternalColorTextureFormat(),
internalFormat,
TextureFormat.Depth24Stencil8,
false,
false,
Expand All @@ -135,7 +139,7 @@ export class BasicRenderPipeline {
TextureFilterMode.Bilinear
);

if (!this._canUseBlitFrameBuffer && this._shouldGrabColor) {
if (!this._canUseBlitFrameBuffer && this._shouldGrabColor && !this._canJustCopyToInternalRT) {
const grabTexture = PipelineUtils.recreateTextureIfNeeded(

Check warning on line 143 in packages/core/src/RenderPipeline/BasicRenderPipeline.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/RenderPipeline/BasicRenderPipeline.ts#L143

Added line #L143 was not covered by tests
engine,
this._grabTexture,
Expand Down Expand Up @@ -210,25 +214,30 @@ export class BasicRenderPipeline {
} else {
if (this._shouldGrabColor) {
rhi.clearRenderTarget(engine, CameraClearFlags.ColorDepth);

Check warning on line 216 in packages/core/src/RenderPipeline/BasicRenderPipeline.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/RenderPipeline/BasicRenderPipeline.ts#L216

Added line #L216 was not covered by tests
rhi.copyRenderTargetToSubTexture(
camera.renderTarget,
internalColorTarget,
this._grabTexture,
camera.viewport
);
PipelineUtils.blitTexture(
engine,
this._grabTexture,
internalColorTarget,
0,
undefined,
undefined,
undefined,
// Only flip Y axis in webgl context
!camera.renderTarget
);
if (this._canJustCopyToInternalRT) {
rhi.copyRenderTargetToSubTexture(

Check warning on line 218 in packages/core/src/RenderPipeline/BasicRenderPipeline.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/RenderPipeline/BasicRenderPipeline.ts#L218

Added line #L218 was not covered by tests
camera.renderTarget,
internalColorTarget.getColorTexture(),
camera.viewport
);
} else {
rhi.copyRenderTargetToSubTexture(camera.renderTarget, this._grabTexture, camera.viewport);
PipelineUtils.blitTexture(

Check warning on line 225 in packages/core/src/RenderPipeline/BasicRenderPipeline.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/RenderPipeline/BasicRenderPipeline.ts#L224-L225

Added lines #L224 - L225 were not covered by tests
engine,
this._grabTexture,
internalColorTarget,
0,
undefined,
undefined,
undefined,
// Only flip Y axis in webgl context
!camera.renderTarget
);
}
} else {
// Must clear all cause of the internal color target can't copy depth/stencil buffer from back buffer
Logger.warn(

Check warning on line 238 in packages/core/src/RenderPipeline/BasicRenderPipeline.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/RenderPipeline/BasicRenderPipeline.ts#L238

Added line #L238 was not covered by tests
"We clear all depth/stencil state cause of the internalRT can't copy depth/stencil buffer from back buffer when use copy plan"
);
rhi.clearRenderTarget(engine, CameraClearFlags.All, color);

Check warning on line 241 in packages/core/src/RenderPipeline/BasicRenderPipeline.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/RenderPipeline/BasicRenderPipeline.ts#L241

Added line #L241 was not covered by tests
}
}
Expand Down
25 changes: 0 additions & 25 deletions packages/core/src/renderingHardwareInterface/IPlatformTexture2D.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { RenderTarget } from "../texture";
import { IPlatformTexture } from "./IPlatformTexture";

/**
Expand Down Expand Up @@ -52,28 +51,4 @@ export interface IPlatformTexture2D extends IPlatformTexture {
* @param out - Color buffer
*/
getPixelBuffer(x: number, y: number, width: number, height: number, mipLevel: number, out: ArrayBufferView): void;

/**
* Copy the specified area of the render target to the sub texture.
* @remarks
* If the render target has MSAA FBO, must be resolved before copying.
* @param renderTarget - The render target to copy from
* @param level - Texture mipmapping level
* @param xOffset - Specifying the horizontal offset within the texture image
* @param yOffset - Specifying the vertical offset within the texture image
* @param x - Specifying the x coordinate of the lower left corner where to start copying
* @param y - Specifying the x coordinate of the lower left corner where to start copying
* @param width - The width of the copy area
* @param height - The height of the copy area
*/
copySubFromRenderTarget(
renderTarget: RenderTarget,
level: number,
xOffset: number,
yOffset: number,
x: number,
y: number,
width: number,
height: number
);
}
37 changes: 0 additions & 37 deletions packages/core/src/texture/Texture2D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { TextureFilterMode } from "./enums/TextureFilterMode";
import { TextureFormat } from "./enums/TextureFormat";
import { TextureUsage } from "./enums/TextureUsage";
import { TextureWrapMode } from "./enums/TextureWrapMode";
import { RenderTarget } from "./RenderTarget";
import { Texture } from "./Texture";

/**
Expand Down Expand Up @@ -176,42 +175,6 @@ export class Texture2D extends Texture {
}
}

/**
* Copy the specified area of the render target to the sub texture.
* @remarks
* If the render target has MSAA FBO, must be resolved before copying.
* @param renderTarget - The render target to copy from
* @param level - Texture mipmapping level
* @param xOffset - Specifying the horizontal offset within the texture image
* @param yOffset - Specifying the vertical offset within the texture image
* @param x - Specifying the x coordinate of the lower left corner where to start copying
* @param y - Specifying the x coordinate of the lower left corner where to start copying
* @param width - The width of the copy area
* @param height - The height of the copy area
*/
copySubFromRenderTarget(
renderTarget: RenderTarget,
level: number,
xOffset: number,
yOffset: number,
x: number,
y: number,
width: number,
height: number
) {
(this._platformTexture as IPlatformTexture2D).copySubFromRenderTarget(
renderTarget,
level,
xOffset,
yOffset,
x,
y,
width,
height
);
this._isContentLost = false;
}

/**
* @internal
*/
Expand Down
35 changes: 1 addition & 34 deletions packages/rhi-webgl/src/GLTexture2D.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import {
IPlatformTexture2D,
Logger,
RenderTarget,
Texture2D,
TextureFormat,
TextureUsage
} from "@galacean/engine-core";
import { IPlatformTexture2D, Logger, Texture2D, TextureFormat, TextureUsage } from "@galacean/engine-core";
import { GLTexture } from "./GLTexture";
import { WebGLGraphicDevice } from "./WebGLGraphicDevice";
import { GLRenderTarget } from "./GLRenderTarget";

/**
* Texture 2d in WebGL platform.
Expand Down Expand Up @@ -116,29 +108,4 @@ export class GLTexture2D extends GLTexture implements IPlatformTexture2D {
}
super._getPixelBuffer(null, x, y, width, height, mipLevel, out);
}

/**
* {@inheritDoc IPlatformTexture2D.copySubFromRenderTarget }
*/
copySubFromRenderTarget(
renderTarget: RenderTarget,
level: number,
xOffset: number,
yOffset: number,
x: number,
y: number,
width: number,
height: number
) {
// @ts-ignore
const frameBuffer = renderTarget?._platformRenderTarget._frameBuffer ?? null;
const gl = this._gl;

// @ts-ignore
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer);

this._bind();

gl.copyTexSubImage2D(this._target, level, xOffset, yOffset, x, y, width, height);
}
}
20 changes: 16 additions & 4 deletions packages/rhi-webgl/src/WebGLGraphicDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,19 +425,31 @@ export class WebGLGraphicDevice implements IHardwareRenderer {
gl.blitFramebuffer(xStart, yStart, xEnd, yEnd, 0, 0, blitWidth, blitHeight, blitMask, gl.NEAREST);

Check warning on line 425 in packages/rhi-webgl/src/WebGLGraphicDevice.ts

View check run for this annotation

Codecov / codecov/patch

packages/rhi-webgl/src/WebGLGraphicDevice.ts#L425

Added line #L425 was not covered by tests
}

copyRenderTargetToSubTexture(srcRT: RenderTarget, destRT: RenderTarget, grabTexture: Texture2D, viewport: Vector4) {
copyRenderTargetToSubTexture(srcRT: RenderTarget, grabTexture: Texture2D, viewport: Vector4) {
const gl = this._gl;
const bufferWidth = this.getMainFrameBufferWidth();
const bufferHeight = this.getMainFrameBufferHeight();

Check warning on line 431 in packages/rhi-webgl/src/WebGLGraphicDevice.ts

View check run for this annotation

Codecov / codecov/patch

packages/rhi-webgl/src/WebGLGraphicDevice.ts#L429-L431

Added lines #L429 - L431 were not covered by tests
const srcWidth = srcRT ? srcRT.width : bufferWidth;
const srcHeight = srcRT ? srcRT.height : bufferHeight;
const copyWidth = destRT.width;
const copyHeight = destRT.height;
const copyWidth = grabTexture.width;
const copyHeight = grabTexture.height;
const flipY = !srcRT;

Check warning on line 436 in packages/rhi-webgl/src/WebGLGraphicDevice.ts

View check run for this annotation

Codecov / codecov/patch

packages/rhi-webgl/src/WebGLGraphicDevice.ts#L434-L436

Added lines #L434 - L436 were not covered by tests

const xStart = viewport.x * srcWidth;

Check warning on line 438 in packages/rhi-webgl/src/WebGLGraphicDevice.ts

View check run for this annotation

Codecov / codecov/patch

packages/rhi-webgl/src/WebGLGraphicDevice.ts#L438

Added line #L438 was not covered by tests
const yStart = flipY ? srcHeight - viewport.y * srcHeight - copyHeight : viewport.y * srcHeight;

grabTexture.copySubFromRenderTarget(srcRT, 0, 0, 0, xStart, yStart, copyWidth, copyHeight);
// @ts-ignore
const frameBuffer = srcRT?._platformRenderTarget._frameBuffer ?? null;

// @ts-ignore
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer);

Check warning on line 445 in packages/rhi-webgl/src/WebGLGraphicDevice.ts

View check run for this annotation

Codecov / codecov/patch

packages/rhi-webgl/src/WebGLGraphicDevice.ts#L445

Added line #L445 was not covered by tests

// @ts-ignore
const glTexture = grabTexture._platformTexture;

Check warning on line 448 in packages/rhi-webgl/src/WebGLGraphicDevice.ts

View check run for this annotation

Codecov / codecov/patch

packages/rhi-webgl/src/WebGLGraphicDevice.ts#L448

Added line #L448 was not covered by tests

glTexture._bind();

Check warning on line 450 in packages/rhi-webgl/src/WebGLGraphicDevice.ts

View check run for this annotation

Codecov / codecov/patch

packages/rhi-webgl/src/WebGLGraphicDevice.ts#L450

Added line #L450 was not covered by tests

gl.copyTexSubImage2D(glTexture._target, 0, 0, 0, xStart, yStart, copyWidth, copyHeight);

Check warning on line 452 in packages/rhi-webgl/src/WebGLGraphicDevice.ts

View check run for this annotation

Codecov / codecov/patch

packages/rhi-webgl/src/WebGLGraphicDevice.ts#L452

Added line #L452 was not covered by tests
}

activeTexture(textureID: number): void {
Expand Down

0 comments on commit 89a163f

Please sign in to comment.