Skip to content

fix(legacy): the memory misalignment issue with customAttribute - #313

Open
troublemaker52025 wants to merge 3 commits into
cocos:v4.0.0from
troublemaker52025:fix(legacy)-the-memory-misalignment-issue-with-customAttribute
Open

fix(legacy): the memory misalignment issue with customAttribute#313
troublemaker52025 wants to merge 3 commits into
cocos:v4.0.0from
troublemaker52025:fix(legacy)-the-memory-misalignment-issue-with-customAttribute

Conversation

@troublemaker52025

@troublemaker52025 troublemaker52025 commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Re: #

Changelog

  • attribute 数据类型本来就不是统一的,原文使用了统一的 Float32Array[]内存来存储顶点属性。但是在 customAttribute 却定义成了 RGBA16UI,强行以 Float32 格式填入(memcpy 不区分类型,按 Float32强行写入),GPU 以 RGBA16UI 去解读,导致数据错乱
  • 原文错上加错, 计算 vertexCount 时,既然已经把 RGBA16UI,强行以 Float32 格式填入,那就应该用 Float32 的字节宽度计算顶点数量,反而还用预定义好的 RGBA16UI 来计算,导致顶点数量多了一倍,触发了断言

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

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 addresses dynamic-mesh customAttributes vertex data misalignment by allowing custom attribute values to be provided as non-float TypedArrays (matching the declared GPU format) and adding debug-time validation to catch mismatches early.

Changes:

  • Broadened dynamic custom-attribute values from Float32Array to a TypedArray union (TS) / TypedArray (native) to support integer formats without float round-trips.
  • Updated Mesh.updateSubMesh() (TS + native) to copy/update vertex buffers from arbitrary TypedArrays and validate element-size vs GPU format size in debug builds.
  • Added documentation/comments explaining the rationale and expected usage.

Reviewed changes

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

File Description
native/cocos/primitive/PrimitiveDefine.h Changes dynamic custom attribute storage to TypedArray and documents intended usage.
native/cocos/3d/assets/Mesh.cpp Updates native dynamic submesh update path to accept TypedArray and adds debug validation/typed-array handling.
cocos/primitive/define.ts Introduces DynamicAttributeValues union and updates IDynamicGeometry.customAttributes[].values type.
cocos/3d/assets/mesh.ts Updates TS dynamic submesh update path to accept DynamicAttributeValues and adds debug validation.

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

Comment on lines +1212 to 1215
ccstd::vector<TypedArray> buffers;
if (!geometry.positions.empty()) {
buffers.push_back(&geometry.positions);
buffers.push_back(wrapAsTypedArray(geometry.positions));
}
Comment thread cocos/3d/assets/mesh.ts
Comment on lines 581 to 587
return;
}

const buffers: Float32Array[] = [];
const buffers: DynamicAttributeValues[] = [];
if (dynamicGeometry.positions.length > 0) {
buffers.push(dynamicGeometry.positions);
}
Comment thread native/cocos/3d/assets/Mesh.cpp
Comment thread cocos/3d/assets/mesh.ts

@jk20012001 jk20012001 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

原理上是对的,但是改动代码太底层了,不确定有没有其他地方按照这里错误的样子配合写的,需要非常全面的测试

@github-actions

Copy link
Copy Markdown

Code Size Check Report

Wechat (WASM) Before After Diff
2D Empty (legacy pipeline) 1015436 bytes 1015550 bytes ⚠️ +114 bytes
2D All (legacy pipeline) 2683579 bytes 2683693 bytes ⚠️ +114 bytes
2D All (new pipeline) 2775838 bytes 2775952 bytes ⚠️ +114 bytes
(2D + 3D) All 10033280 bytes 10033394 bytes ⚠️ +114 bytes
Web (WASM + ASMJS) Before After Diff
(2D + 3D) All 16869698 bytes 16869812 bytes ⚠️ +114 bytes

Interface Check Report

! WARNING this pull request has changed these public interfaces:

@@ -51446,8 +51446,25 @@
          */
         export function applyDefaultGeometryOptions<GeometryOptions = IGeometryOptions>(options?: __private.__types_globals__RecursivePartial<IGeometryOptions>): GeometryOptions;
         /**
          * @en
+         * The set of concrete TypedArray types accepted for a dynamic mesh's custom attribute values.
+         * Unlike the standard attributes (positions/normals/uvs/tangents/colors), which are always
+         * float-based, a custom attribute's physical GPU format (`attr.format`) can be any integer or
+         * float format (e.g. RGBA16UI for joint indices). Accepting any concrete TypedArray lets callers
+         * supply data whose element type already matches the target GPU format (fast copy path),
+         * avoiding an unnecessary and potentially lossy "integer -> float -> integer" round trip.
+         * Float32Array remains valid for FLOAT-typed formats (backward compatible).
+         * @zh
+         * 动态网格定制属性 values 字段接受的具体 TypedArray 类型集合。与始终为 float 的标准属性
+         * (positions/normals/uvs/tangents/colors)不同,定制属性的物理 GPU 格式(`attr.format`)可以是
+         * 任意整型或浮点格式(例如 RGBA16UI 用于骨骼关节索引)。接受任意具体 TypedArray 类型,使调用者可以
+         * 直接传入与目标 GPU 格式匹配的原生类型数组(快速拷贝路径),避免不必要甚至有损的
+         * “整数 -> 浮点 -> 整数”往返转换。Float32Array 对于 FLOAT 类型格式依然有效(向后兼容)。
+         */
+        export type DynamicAttributeValues = Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array;
+        /**
+         * @en
          * The definition of the parameter for building a primitive geometry.
          * @zh
          * 几何体参数选项。
          */
@@ -51620,9 +51637,9 @@
              * 定制属性列表。
              */
             customAttributes?: {
                 attr: gfx.Attribute;
-                values: Float32Array;
+                values: DynamicAttributeValues;
             }[];
             /**
              * @en
              * Min position.

@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

troublemaker52025 and others added 2 commits August 28, 2026 15:48
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@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-draw-before-init
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

@troublemaker52025

troublemaker52025 commented Aug 29, 2026

Copy link
Copy Markdown
Contributor Author

测试方法

wave 00_00_00-00_00_30

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.

3 participants