Repository navigation
fix(msl): lay out buffer struct vec3 members with WGSL packing - #2403
Merged
Merged
Conversation
WGSL's uniform address space requires array strides that are a multiple of 16. The frontend accepted array<f32, N> and similar in uniform buffers, and the MSL writer, which keeps natural array strides, then read the elements at offsets other than the std140 ones shaderir reports. Refuse them in the frontend, drop the unused isStorage parameter from the MSL layout helpers, credit the contributor in the changelog fragment, and unwrap the helpers' comments.
…blocks The cross-backend test only checks the IR's offsets and the MSL struct; the other writers are only checked to compile. Rename it and say so. Add a WGSL-sourced case where a buffer struct local declared in the outer block is read and written inside if and for bodies, in a helper and in an entry point.
The HLSL writer declares cbuffer and StructuredBuffer structs with natural members, so Direct3D 11 reads vec3 members at offsets other than WGSL's (nsgo-dev#2340).
This was referenced Nov 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
vec3<f32>,vec3<i32>, andvec3<u32>members of uniform and storage buffer structs inmsl.CompilePipelineaspacked_float3,packed_int3, andpacked_uint3, with explicituchar __padN[...]members andalignas(N)so Metal offsets, trailing padding, and struct strides match WGSL host-shareable layout.array<vec3<T>, N>andarray<vec3<T>>elements as unpackedfloat3/int3/uint3(preserving WGSL's 16-byte stride), keepmat2x3/mat3x3/mat4x3asfloat2x3/float3x3/float4x3(matching WGSL's 16-byte column stride), and leave local-only structs unpacked.vec3struct members withfloat3(...)/int3(...)/uint3(...)viashaderir.TypeOf, keep lvalue targets unwrapped for component/index writes, wrap whole-field assignments inpacked_*3(...), and emit__make_SN(...)helpers for constructed buffer structs.internal/shaderir/msl/pipeline_test.go), a Metal API-validated pixel readback test (TestMetalVec3PackedStructLayoutininternal/platform/apple/gpu_test.go), agpu.Viewuniform+storage pixel readback test (gpu/vec3_layout_test.go), and achangelog.d/msl-vec3-struct-layout.mdfragment.Local Verification
gofmt -l .-> empty (exit 0)go run ./tools/changelog check->2 changelog fragments(exit 0)go tool nsgo test ./internal/shaderir/...-> 14 passed, 0 failed, 0 skippedgo tool nsgo test ./internal/shader/...-> 161 passed, 0 failed, 0 skippedgo tool nsgo test ./gpu-> 54 passed, 0 failed, 0 skippedgo tool nsgo test -target macos ./gpu-> 54 passed, 0 failed, 0 skippedgo tool nsgo test -target macos -run TestMetal ./internal/platform/apple-> 4 passed, 0 failed, 0 skipped (includingTestMetalVec3PackedStructLayoutwithMTL_DEBUG_LAYER=1)go tool nsgo test ./internal/moduletest-> 17 passed, 0 failed, 1 skipped (TestPlatformFunctionDuplicates)go tool nsgo check-> passed (host + dev client + catalogs)go tool nsgo check -target all -jobs 2-> passed (macos_x64,macos_arm64,windows_arm64,windows_x64,linux_arm64,linux_x64,web,android_arm64, andtui_*vetted;ios,ios_simulator,tvos,tvos_simulator,watchos,watchos_simulatorskipped because this host has CommandLineTools withoutXcode.app)Fixes #2340
🤖 Claude Opus 4.6