Skip to content

fix(legacy): sync instanced buffer batching optimization to native - #293

Merged
star-e merged 8 commits into
cocos:v4.0.0from
troublemaker52025:sync(legacy)-optimize-instanced-buffer-batching
Aug 20, 2026
Merged

fix(legacy): sync instanced buffer batching optimization to native#293
star-e merged 8 commits into
cocos:v4.0.0from
troublemaker52025:sync(legacy)-optimize-instanced-buffer-batching

Conversation

@troublemaker52025

@troublemaker52025 troublemaker52025 commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Re: #

Changelog


Continuous Integration

This pull request:

  • needs automatic test cases check.

    Manual trigger with @cocos-robot run test cases afterward.

  • does not change any runtime related code or build configuration

    If any reviewer thinks the CI checks are needed, please uncheck this option, then close and reopen the issue.


Compatibility Check

This pull request:

  • changes public API, and have ensured backward compatibility with deprecated features.
  • affects platform compatibility, e.g. system version, browser version, platform sdk version, platform toolchain, language version, hardware compatibility etc.
  • affects file structure of the build package or build configuration which requires user project upgrade.
  • introduces breaking changes, please list all changes, affected features and the scope of violation.

@troublemaker52025
troublemaker52025 requested review from llsansun and star-e and a lite review from Copilot August 19, 2026 02:58
@troublemaker52025

Copy link
Copy Markdown
Contributor Author

@cocos-robot run test cases

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR syncs the instanced-buffer batching optimizations from the engine (TS) side to the native renderer implementation, aiming to reduce CPU overhead when merging instanced draws and align native behavior with the existing instancing path.

Changes:

  • Add a key-based cache (_instancesMap) in native InstancedBuffer to avoid scanning all instance groups on every merge.
  • Refactor native merge logic into _appendInstance() / _createInstance() helpers and expand merge compatibility inputs (lightmap + reflection probe bindings + stride).
  • Update TS instanced-buffer instance creation to persist the additional compatibility fields (and minor buffer-size refactor).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
native/cocos/renderer/pipeline/InstancedBuffer.h Adds helper declarations and an instance-group lookup map keyed by merge-compatibility inputs.
native/cocos/renderer/pipeline/InstancedBuffer.cpp Implements cached merge path, refactors append/create logic, and adjusts upload behavior (needs follow-up per review).
cocos/rendering/instanced-buffer.ts Passes/stores additional merge-compatibility fields in created instance items and minor allocation refactor.
Suppressed comments (1)

native/cocos/renderer/pipeline/InstancedBuffer.h:89

  • Uint8Array buffer is passed by value to _createInstance(), which copies the underlying ArrayBuffer::Ptr. Prefer const Uint8Array& to avoid per-merge refcount traffic (and update the .cpp definition accordingly).
    void _createInstance(const ccstd::string &key,
        gfx::InputAssembler *sourceIA,
        const ccstd::vector<gfx::Attribute> &attributes,
        Uint8Array buffer,
        uint32_t stride,

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread native/cocos/renderer/pipeline/InstancedBuffer.cpp Outdated
Comment thread native/cocos/renderer/pipeline/InstancedBuffer.cpp
Comment thread native/cocos/renderer/pipeline/InstancedBuffer.h Outdated
@github-actions

github-actions Bot commented Aug 19, 2026

Copy link
Copy Markdown

Code Size Check Report

Wechat (WASM) Before After Diff
2D Empty (legacy pipeline) 1015290 bytes 1015435 bytes ⚠️ +145 bytes
2D All (legacy pipeline) 2683021 bytes 2683166 bytes ⚠️ +145 bytes
2D All (new pipeline) 2775280 bytes 2775425 bytes ⚠️ +145 bytes
(2D + 3D) All 10032722 bytes 10032867 bytes ⚠️ +145 bytes
Web (WASM + ASMJS) Before After Diff
(2D + 3D) All 16869140 bytes 16869285 bytes ⚠️ +145 bytes

Interface Check Report

This pull request does not change any public interfaces !

llsansun and others added 2 commits August 19, 2026 11:23
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (2)

native/cocos/renderer/pipeline/InstancedBuffer.cpp:143

  • CC_ASSERT(destination) is ineffective here because destination is computed via pointer arithmetic on instance.data. If instance.data is null (e.g. allocation failure), the pointer arithmetic is already undefined behavior before the assert. Assert the base pointer (or handle allocation failure) before computing destination.
    auto *destination = instance.data + static_cast<size_t>(instance.stride) * instance.drawInfo.instanceCount;
    CC_ASSERT(destination);
    memcpy(destination, buffer.buffer()->getData(), instance.stride);

native/cocos/renderer/pipeline/InstancedBuffer.cpp:213

  • The comment says the upload size is "clamped", but the code only asserts size relationships (and CC_ASSERT compiles out in release). Also dataCapacity is only used in CC_ASSERT, so it becomes an unused variable in non-debug builds. Consider removing the unused variable and making the comment match the actual behavior (uploading only the valid instance data range).
        // `instance.data` is only guaranteed to hold `instance.capacity * instance.stride` bytes.
        // If `instance.vb` was resized to a larger size than the CPU-side `instance.data`
        // buffer (e.g. due to a bug in the resize/append logic), copying `instance.vb->getSize()`
        // bytes from `instance.data` would read out of bounds and crash. Clamp to the smaller
        // of the two to avoid reading past the end of `instance.data`.

@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
web-mobile PASS PASS FAIL graphics-line-join
ios PASS PASS PASS
mac PASS PASS PASS

@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
web-mobile PASS PASS FAIL graphics-continuous-filling
ios PASS PASS PASS
mac PASS PASS PASS

@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

@star-e
star-e merged commit 09b6378 into cocos:v4.0.0 Aug 20, 2026
35 checks passed
star-e pushed a commit that referenced this pull request Aug 23, 2026
Co-authored-by: cocos-robot <cocos-robot@cocos.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants