shaders: panic-free Mat3/Mat2 column access (ColumnIndex::col_at)#9
Open
haixuanTao wants to merge 2 commits into
Open
shaders: panic-free Mat3/Mat2 column access (ColumnIndex::col_at)#9haixuanTao wants to merge 2 commits into
haixuanTao wants to merge 2 commits into
Conversation
glam 0.33's Mat3/Mat2::col panics with a message on an out-of-range index, which the cuda-oxide backend rejects (no panic message formatting on the GPU). Add a ColBranchless::col_b extension (out-of-range folds to the last column) and use it at every dynamic-index col() site in the joint constraint builders. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
958fa06 to
7b5b303
Compare
sebcrozet
requested changes
Jul 8, 2026
sebcrozet
left a comment
Member
There was a problem hiding this comment.
Thank you! Just had a few name suggestions since col_b isn’t very explicit.
Comment on lines
+144
to
+145
| cmat1_basis: [cmat1.dot(basis.col_b(0)), cmat1.dot(basis.col_b(1))], | ||
| cmat2_basis: [cmat2.dot(basis.col_b(0)), cmat2.dot(basis.col_b(1))], |
Member
There was a problem hiding this comment.
Suggested change
| cmat1_basis: [cmat1.dot(basis.col_b(0)), cmat1.dot(basis.col_b(1))], | |
| cmat2_basis: [cmat2.dot(basis.col_b(0)), cmat2.dot(basis.col_b(1))], | |
| cmat1_basis: [cmat1.dot(basis.x_axis), cmat1.dot(basis.y_axis)], | |
| cmat2_basis: [cmat2.dot(basis.x_axis), cmat2.dot(basis.y_axis)], |
| /// An out-of-range index is a bug: debug (host) builds catch it with a | ||
| /// `debug_assert!`, release/device builds fold it to the last column rather | ||
| /// than carrying panic machinery into the kernel. | ||
| pub trait ColBranchless { |
Member
There was a problem hiding this comment.
Suggested change
| pub trait ColBranchless { | |
| pub trait ColumnIndex { |
| /// than carrying panic machinery into the kernel. | ||
| pub trait ColBranchless { | ||
| type Column; | ||
| fn col_b(&self, index: usize) -> Self::Column; |
Member
There was a problem hiding this comment.
Suggested change
| fn col_b(&self, index: usize) -> Self::Column; | |
| fn col_at(&self, index: usize) -> Self::Column; |
…ss where the index is constant
Contributor
Author
|
Applied all three suggestions in 568a8b4: trait renamed to |
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.
glam 0.33's
Mat3::col/Mat2::colpanic with a formatted message on an out-of-range index. On GPU targets that cannot lower panic-message formatting (e.g. compiling these shaders to PTX via cuda-oxide), any dynamic-indexcol()call makes the whole kernel fail device codegen — even though the index is always in range at runtime (it comes from the locked/limited/motor axis masks,0..DIM).This adds a small
ColBranchlessextension trait (col_b) in the shader crate root and uses it at every dynamic-indexcol()site in the joint constraint builder. The in-range precondition is explicit: adebug_assert!catches an out-of-range index in host/debug builds (so a future bug fails loudly in tests rather than being masked), while release/device builds compile it out and fold to the last column instead of carrying panic machinery into the kernel. Constant-index behavior is unchanged, and SPIR-V output is unaffected apart from dropping the (unreachable) panic branch.Verified: shader crate compiles for SPIR-V and nvptx64 (cuda-oxide); the joint solve on the native-CUDA path produces results matching WebGPU on a pendulum + multibody biped training run.
🤖 Generated with Claude Code