Skip to content

feat: Bind buffer regions - #3489

Open
reczkok wants to merge 3 commits into
feat/buffer-regionsfrom
feat/bind-buffer-regions
Open

reczkok wants to merge 3 commits into
feat/buffer-regionsfrom
feat/bind-buffer-regions

Conversation

@reczkok

@reczkok reczkok commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Regions bind only their own bytes in bind groups and through .as(...), and root.unwrap(region) returns a GPUBufferBinding. Binding a region at an offset the device's min*BufferOffsetAlignment doesn't allow throws an error naming the limit

@reczkok
reczkok added this pull request to stack #3490 December 2, 2026 15:52
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

pkg.pr.new

packages
Ready to be installed by your favorite package manager ⬇️

https://pkg.pr.new/software-mansion/TypeGPU/eslint-plugin-typegpu@4312ee090b6b6f26743678e1610cfe440a607918
https://pkg.pr.new/software-mansion/TypeGPU/tinyest@4312ee090b6b6f26743678e1610cfe440a607918
https://pkg.pr.new/software-mansion/TypeGPU/tinyest-for-wgsl@4312ee090b6b6f26743678e1610cfe440a607918
https://pkg.pr.new/software-mansion/TypeGPU/typegpu@4312ee090b6b6f26743678e1610cfe440a607918
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/gl@4312ee090b6b6f26743678e1610cfe440a607918
https://pkg.pr.new/software-mansion/TypeGPU/unplugin-typegpu@4312ee090b6b6f26743678e1610cfe440a607918

benchmark
view benchmark

commit
view commit

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Resolution Time Benchmark

---
config:
  themeVariables:
    xyChart:
      plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
  title "Random Branching (🔴 PR | 🔵 main | 🟢 release)"
  x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
  y-axis "time (ms)"
  line [0.64, 1.46, 3.24, 4.86, 5.53, 9.96, 20.15, 18.89]
  line [0.76, 1.57, 3.00, 4.42, 5.34, 9.72, 19.70, 23.35]
  line [0.71, 1.42, 2.83, 4.89, 5.76, 8.88, 19.82, 17.84]
Loading
---
config:
  themeVariables:
    xyChart:
      plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
  title "Linear Recursion (🔴 PR | 🔵 main | 🟢 release)"
  x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
  y-axis "time (ms)"
  line [0.18, 0.36, 0.51, 0.58, 0.78, 0.84, 1.02, 1.09]
  line [0.17, 0.38, 0.56, 0.69, 0.97, 0.94, 1.07, 1.26]
  line [0.20, 0.39, 0.49, 0.57, 0.89, 0.86, 1.02, 1.14]
Loading
---
config:
  themeVariables:
    xyChart:
      plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
  title "Full Tree (🔴 PR | 🔵 main | 🟢 release)"
  x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
  y-axis "time (ms)"
  line [0.90, 2.39, 3.93, 5.43, 12.34, 22.12, 45.92, 97.43]
  line [0.90, 1.97, 3.80, 5.20, 10.46, 23.19, 45.23, 95.28]
  line [0.93, 1.86, 2.85, 5.03, 9.12, 21.50, 42.41, 88.67]
Loading

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Bundle size comparison (import * as ... in PR vs import * as ... in target):

🟢 Decreased ➖ Unchanged 🔴 Increased (max 1.82%) ❔ Unknown
0 347 13 1

import * as ... in PR vs import * as ... in target (did bundle size increase?):

Test tsdown
STATIC_tgpu.js 283.43 kB ($${\color{red}+1.8\%}$$)
STATIC_allImports.js 335.54 kB ($${\color{red}+1.5\%}$$)
tgpu_bindGroupLayout.js 73.08 kB ($${\color{red}+0.6\%}$$)
d_memoryLayoutOf.js 37.76 kB ($${\color{red}+0.6\%}$$)
std_bufferArrayView.js 42.71 kB ($${\color{red}+0.5\%}$$)
std_bufferView.js 42.71 kB ($${\color{red}+0.5\%}$$)
std_bufferLength.js 43.86 kB ($${\color{red}+0.5\%}$$)
tgpu_bufferRegion.js 90.42 kB

If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu.

@pullfrog pullfrog Bot 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.

ℹ️ No blocking issues — three non-blocking findings and one doc nit below.

Approach: sound — a region becomes a { buffer, offset, size } binding at the single place bind groups are created, and alignment failures surface synchronously with the offending limit named.

Reviewed changes

  • Region binding — bufferBindingOf maps regions to offset/size GPUBufferBindings and validates minUniformBufferOffsetAlignment / minStorageBufferOffsetAlignment; it backs both bind groups and the new region.as(...).
  • Usage-flag inheritance — InheritedUsageFlags ties buffer.region()'s flags to the parent buffer and schema, and BindableBuffer threads the buffer type through TgpuMutable / TgpuReadonly / TgpuUniform and LayoutEntryToInput.
  • Unwrap and serialization — root.unwrap(region) returns the exact GPUBufferBinding; unwrapping a binding of a region now throws a named error; region bindings survive the soul round-trip.
  • Docs and tests — new "Binding a region" docs section with a twoslash example, alignment and usage-scope cautions, plus tests for bound bytes, alignment errors, unwrap and serialization.

Non-blocking

  • Regions carry vertex/index/indirect flags that nothing consumes. InheritedUsageFlags adds VertexFlag, IndexFlag and IndirectFlag, and TgpuBufferRegionImpl exposes usableAsVertex / usableAsIndex / usableAsIndirect, but setVertexBuffer, setIndexBuffer and the indirect draw/dispatch paths still require TgpuBuffer (and isUsableAsVertex / isUsableAsIndex are unchanged). Either drop those flags and getters until the pass APIs accept regions, or wire the APIs up; today this suggests support that does not exist.
  • The skills reference was not updated for regions. packages/typegpu/skills/typegpu/references/buffers.md still has no buffer-region section, and line 159 says a binding's .buffer is its TgpuBuffer — false for a binding of a region. Add region(...), .as(...), the offset alignment rule and root.unwrap(region) there (and mention regions in types.md's .as(...) line) so the skill matches the API.
  • The new bind-group test builds a group WebGPU would reject in a pass. bufferRegion.test.ts:187 binds the same region as read-only storage (boids) and writable storage (mutableBoids) in one bind group; the usage-scope caution added in this PR says that combination is invalid within a dispatch or render pass. The mock device does not enforce usage scopes, so the test passes, but it models a pattern readers may copy. Use a second region/buffer, or assert the entries without presenting the group as usable.
  • Doc typo. buffers.mdx:786 reads "Both limits are at most 256 bytes, and 256 bytes on many devices" — missing "are".

Pullfrog  | Fix it ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@reczkok
reczkok force-pushed the feat/bind-buffer-regions branch from 6fc6602 to e5900ae Compare December 2, 2026 16:04
@reczkok
reczkok force-pushed the feat/bind-buffer-regions branch from e5900ae to c641162 Compare December 3, 2026 11:32
Copilot AI balanced review requested due to automatic review settings December 3, 2026 11:32

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@pullfrog pullfrog Bot 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.

ℹ️ No blocking issues — the new commit resolves all four prior non-blocking findings; two doc nits remain.

Reviewed changes

Since the prior review, the delta drops the inert vertex/index/indirect usage flags from regions, updates the skill references to describe region binding, and fixes the earlier docs and test issues.

  • Usage-flag removal. Dropped the inert VertexFlag / IndexFlag / IndirectFlag inheritance and the usableAsVertex / usableAsIndex / usableAsIndirect getters, so InheritedUsageFlags now covers only uniform and storage; the type assertions were narrowed to match.
  • Skills updated. Documented region binding, .as(...), offset alignment, usage scope and root.unwrap(region) in references/buffers.md, and widened .as(...) to regions plus isBufferRegion in references/types.md.
  • Prior test finding fixed. The bind-group test now binds the read-only and mutable regions from two separate buffers, so it no longer models a usage scope WebGPU would reject.
  • Docs typo fixed. "Both limits are at most 256 bytes, and are 256 bytes on many devices."

Non-blocking

  • Skills say a region binding's .buffer supports destroy. references/buffers.md:178 lists copyFrom, clear and destroy as the reason to reach .buffer, but for a region binding .buffer is a TgpuBufferRegion, which has copyFrom and clear but no destroy (only destroyed). Split the sentence so destroy is described as whole-buffer only.
  • Docs and JSDoc still claim a region works "wherever a buffer can". buffers.mdx:680 and the TgpuBufferRegion doc comment promise more than the removed flags now allow: the supported surfaces are bind groups and .as(...), not vertex/index/indirect. Reword to "wherever a buffer binding can" so readers do not expect setVertexBuffer to accept a region.

Pullfrog  | Fix it ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@reczkok
reczkok force-pushed the feat/bind-buffer-regions branch from c641162 to 4312ee0 Compare December 4, 2026 14:59

@pullfrog pullfrog Bot 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.

✅ No new issues found.

Approach: sound — regions now hand their own aligned { offset, size } binding to the bind-group layer, and .as(...) bindings carry the region through instead of collapsing to the whole buffer.

Reviewed changes

The delta since the prior Pullfrog review (c641162) replaces the region API, splits the type module, and routes binding through region internals.

  • Factory API. Replaced the buffer.region(...) methods with tgpu.bufferRegion(buffer, schema, accessor | options), with the region's public types moved to bufferRegionTypes.ts (which also breaks the runtime cycle with bufferBinding.ts).
  • Region-owned binding. TgpuBufferRegion[$internal] now exposes binding(usage), which validates the offset against the device's min*BufferOffsetAlignment, clamps and rounds the size, and returns the GPUBufferBinding. bufferBindingOf moved into tgpuBindGroupLayout.ts and delegates to it for both direct regions and .as(...) bindings.
  • Binding types. TgpuBufferBinding, TgpuMutable, TgpuReadonly and TgpuUniform gained a second TBuffer parameter defaulting to the whole buffer, BindableBuffer is exported, TgpuBufferRegion inherits usage flags from its buffer, and isUsableAsUniform is widened to any carrier.
  • Partial writes and unwrap. writePartial on a binding now patches through its buffer or region (so region bindings work), simulation of a region binding throws a named error, and root.unwrap of a binding-of-region throws instead of returning the wrong GPUBuffer.
  • Docs and skills. Both now use tgpu.bufferRegion, cover binding, alignment and usage scope, document root.unwrap(region), and state that destroy exists only on whole buffers — which also resolves the last review's two doc nits.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

This branch has not been deployed

No deployments
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.

2 participants