Skip to content

Commit 4d80ade

Browse files
committed
Throttling operation for resizing in a custom pipeline.
1 parent b01c3ce commit 4d80ade

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

cocos/rendering/custom/framework.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,29 @@ export function defaultWindowResize (ppl: BasicPipeline, window: RenderWindow, w
6666
ppl.addDepthStencil(`ShadowDepth${id}`, Format.DEPTH_STENCIL, shadowSize.x, shadowSize.y);
6767
}
6868

69-
const _resizedWindows: RenderWindow[] = [];
69+
// Map to store throttle timers by cameraId
70+
const throttleTimers = new Map();
71+
// Map to store last resize time by cameraId
72+
let lastResizeTimes = 0;
7073

74+
// Throttle delay in milliseconds (when detected frequent resizing)
75+
const THROTTLE_DELAY = 500;
76+
// Window time in milliseconds to detect frequent resizing
77+
const FREQUENCY_WINDOW = 200;
78+
79+
// Adaptive throttled version of dispatchResizeEvents function using cameraId
7180
export function dispatchResizeEvents (cameras: Camera[], builder: PipelineBuilder, ppl: BasicPipeline): void {
72-
if (!builder.windowResize) {
81+
if (!builder.windowResize || !cameras.some((camera) => camera.window.isRenderWindowResized())) {
7382
// No game window resize handler defined.
74-
// Following old prodecure, do nothing
83+
// Following old procedure, do nothing
7584
return;
7685
}
77-
86+
const now = Date.now();
87+
let isFrequentResizing = false;
88+
if (now - lastResizeTimes <= FREQUENCY_WINDOW) {
89+
isFrequentResizing = true;
90+
}
91+
lastResizeTimes = now;
7892
// Resize all windows.
7993
// Notice: A window might be resized multiple times with different cameras.
8094
// User should avoid resource collision between different cameras.
@@ -83,22 +97,29 @@ export function dispatchResizeEvents (cameras: Camera[], builder: PipelineBuilde
8397
continue;
8498
}
8599

86-
const width = Math.max(Math.floor(camera.window.width), 1);
87-
const height = Math.max(Math.floor(camera.window.height), 1);
100+
const cameraId = camera.cameraId; // Unique identifier for each camera
88101

89-
builder.windowResize(ppl, camera.window, camera, width, height);
102+
// Skip execution if a timer exists and we're in frequent resizing mode
103+
if (isFrequentResizing && throttleTimers.has(cameraId)) {
104+
continue;
105+
}
90106

91-
_resizedWindows.push(camera.window);
92-
}
107+
const width = Math.max(Math.floor(camera.window.width), 1);
108+
const height = Math.max(Math.floor(camera.window.height), 1);
93109

94-
// Reset resize flags
95-
for (const window of _resizedWindows) {
96-
window.setRenderWindowResizeHandled();
110+
if (isFrequentResizing) {
111+
// Apply throttle only when frequent resizing is detected
112+
throttleTimers.set(cameraId, setTimeout(() => {
113+
builder.windowResize!(ppl, camera.window, camera, width, height);
114+
camera.window.setRenderWindowResizeHandled();
115+
throttleTimers.delete(cameraId);
116+
}, THROTTLE_DELAY));
117+
} else {
118+
// Normal resize - execute immediately
119+
builder.windowResize(ppl, camera.window, camera, width, height);
120+
camera.window.setRenderWindowResizeHandled();
121+
}
97122
}
98-
99-
// Clear resized windows
100-
_resizedWindows.length = 0;
101-
102123
// For editor preview
103124
forceResize = false;
104125
}

0 commit comments

Comments
 (0)