Skip to content

fix(msl): lay out buffer struct vec3 members with WGSL packing - #2403

Merged
houko merged 5 commits into
nsgo-dev:mainfrom
owocc:fix/issue-2340
Nov 26, 2026
Merged

houko merged 5 commits into
nsgo-dev:mainfrom
owocc:fix/issue-2340

Conversation

@owocc

@owocc owocc commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Emit vec3<f32>, vec3<i32>, and vec3<u32> members of uniform and storage buffer structs in msl.CompilePipeline as packed_float3, packed_int3, and packed_uint3, with explicit uchar __padN[...] members and alignas(N) so Metal offsets, trailing padding, and struct strides match WGSL host-shareable layout.
  • Keep array<vec3<T>, N> and array<vec3<T>> elements as unpacked float3/int3/uint3 (preserving WGSL's 16-byte stride), keep mat2x3/mat3x3/mat4x3 as float2x3/float3x3/float4x3 (matching WGSL's 16-byte column stride), and leave local-only structs unpacked.
  • Unwrap rvalue reads of packed vec3 struct members with float3(...)/int3(...)/uint3(...) via shaderir.TypeOf, keep lvalue targets unwrapped for component/index writes, wrap whole-field assignments in packed_*3(...), and emit __make_SN(...) helpers for constructed buffer structs.
  • Add MSL unit tests (internal/shaderir/msl/pipeline_test.go), a Metal API-validated pixel readback test (TestMetalVec3PackedStructLayout in internal/platform/apple/gpu_test.go), a gpu.View uniform+storage pixel readback test (gpu/vec3_layout_test.go), and a changelog.d/msl-vec3-struct-layout.md fragment.

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 skipped
  • go tool nsgo test ./internal/shader/... -> 161 passed, 0 failed, 0 skipped
  • go tool nsgo test ./gpu -> 54 passed, 0 failed, 0 skipped
  • go tool nsgo test -target macos ./gpu -> 54 passed, 0 failed, 0 skipped
  • go tool nsgo test -target macos -run TestMetal ./internal/platform/apple -> 4 passed, 0 failed, 0 skipped (including TestMetalVec3PackedStructLayout with MTL_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, and tui_* vetted; ios, ios_simulator, tvos, tvos_simulator, watchos, watchos_simulator skipped because this host has CommandLineTools without Xcode.app)

Fixes #2340

🤖 Claude Opus 4.6

@github-actions github-actions Bot added documentation Improvements or additions to documentation macOS iOS bug Something isn't working size/XL labels Nov 25, 2026
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation iOS macOS size/XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

msl: pipeline structs lay out vec3<f32> as float3, so a scalar after a vec3 in a uniform or storage buffer is read 4 bytes late on Metal

2 participants