fix: add writable mapped-range API across backends - #318
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
kolkov
left a comment
There was a problem hiding this comment.
Clean PR — fills the native backend gap for BytesMut()/Flush() that browser and Rust backends already have. CI 16/16 pass.
Implementation is correct: native BytesMut() = alias of Bytes(), Flush() = no-op. This matches the Rust backend exactly (mapped_range_rust.go uses the same pattern).
Test follows existing conventions (newDevice + requireHAL + MappedAtCreation).
Design note (non-blocking, for awareness)
Flush() as no-op is correct for DX12, Metal, GLES, Software, and Noop — all return IsCoherent: true from MapBuffer. Vulkan CAN return IsCoherent: false for non-coherent memory types, where CPU writes aren't GPU-visible without vkFlushMappedMemoryRanges. However, this is a pre-existing gap — MappedRange doesn't store the coherency flag, and Bytes() already allows writes with no flush mechanism. The Vulkan UnmapBuffer handles the flush. A proper Flush() for non-coherent Vulkan would need plumbing IsCoherent into MappedRange — that's a separate enhancement.
LGTM.
Summary
Add a common writable mapped-range API to the native
wgpubackend:The browser and Rust backends already expose equivalent behavior. This change brings the native backend into API parity so higher-level libraries such as
g3dcan write mapped buffer data without build-tag-specific code.Motivation
MappedRange.Bytes()is currently suitable for reading, but higher-level code that writes mapped buffer contents needs an explicit writable-range API.The browser backend uses a Go-side staging slice for mapped writes and requires
Flush()to copy modified data back to the JavaScriptArrayBuffer. The Rust backend exposes a direct writable mapping. Native mappings also expose direct writable memory.Without a common API, consumers need platform-specific implementations for the same geometry or buffer upload code.
Changes
MappedRange.BytesMut()to the native backend.MappedRange.Flush()to the native backend.BytesMut()as an alias ofBytes().Flush()as a no-op because native mappings are directly device-visible.Related
This change is a prerequisite for the corresponding
gogpu/g3dupdate that switches geometry uploads to the commonBytesMut()andFlush()API.