Skip to content

Fix WebGL readTextureAsync allocating too much memory for outputData#8472

Merged
slimbuck merged 1 commit into
playcanvas:mainfrom
slimbuck:alloc-dev
Feb 23, 2026
Merged

Fix WebGL readTextureAsync allocating too much memory for outputData#8472
slimbuck merged 1 commit into
playcanvas:mainfrom
slimbuck:alloc-dev

Conversation

@slimbuck

Copy link
Copy Markdown
Member

readTextureAsync passes the result of TextureUtils.calcLevelGpuSize() (which returns bytes) directly as the element count to a typed array constructor. For multi-byte element types this over-allocates by a factor of BYTES_PER_ELEMENT (e.g. 4× for Float32Array/Uint32Array formats, 2× for Uint16Array formats).

The fix divides the byte size by ArrayType.BYTES_PER_ELEMENT to produce the correct element count.

@slimbuck slimbuck requested review from a team and Copilot February 23, 2026 15:52
@slimbuck slimbuck self-assigned this Feb 23, 2026
@slimbuck slimbuck added the area: graphics Graphics related issue label Feb 23, 2026
Comment thread src/platform/graphics/webgl/webgl-graphics-device.js

Copilot AI 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.

Pull request overview

This pull request fixes a memory over-allocation bug in WebGL's readTextureAsync method. The method was incorrectly passing byte sizes directly to typed array constructors, which interpret the argument as element count, resulting in over-allocation by factors of 2× to 4× depending on the pixel format.

Changes:

  • Fixed readTextureAsync to divide byte size by BYTES_PER_ELEMENT when allocating typed arrays
  • Improved code readability by extracting ArrayType to a variable

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mvaligursky mvaligursky 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.

looks good. glad webgpu is unaffected.

@slimbuck slimbuck merged commit 64f8d02 into playcanvas:main Feb 23, 2026
10 of 12 checks passed
@slimbuck slimbuck deleted the alloc-dev branch February 23, 2026 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: graphics Graphics related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants