Skip to content

shaders: panic-free Mat3/Mat2 column access (ColumnIndex::col_at)#9

Open
haixuanTao wants to merge 2 commits into
dimforge:mainfrom
haixuanTao:fix/panic-free-mat3-col
Open

shaders: panic-free Mat3/Mat2 column access (ColumnIndex::col_at)#9
haixuanTao wants to merge 2 commits into
dimforge:mainfrom
haixuanTao:fix/panic-free-mat3-col

Conversation

@haixuanTao

@haixuanTao haixuanTao commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

glam 0.33's Mat3::col / Mat2::col panic 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-index col() 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 ColBranchless extension trait (col_b) in the shader crate root and uses it at every dynamic-index col() site in the joint constraint builder. The in-range precondition is explicit: a debug_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

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>
@haixuanTao haixuanTao force-pushed the fix/panic-free-mat3-col branch from 958fa06 to 7b5b303 Compare July 8, 2026 10:05

@sebcrozet sebcrozet left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)],

Comment thread src_rbd_shaders/lib.rs Outdated
/// 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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub trait ColBranchless {
pub trait ColumnIndex {

Comment thread src_rbd_shaders/lib.rs Outdated
/// than carrying panic machinery into the kernel.
pub trait ColBranchless {
type Column;
fn col_b(&self, index: usize) -> Self::Column;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn col_b(&self, index: usize) -> Self::Column;
fn col_at(&self, index: usize) -> Self::Column;

@haixuanTao haixuanTao changed the title shaders: panic-free Mat3/Mat2 column access (ColBranchless::col_b) shaders: panic-free Mat3/Mat2 column access (ColumnIndex::col_at) Jul 9, 2026
@haixuanTao

Copy link
Copy Markdown
Contributor Author

Applied all three suggestions in 568a8b4: trait renamed to ColumnIndex, method to col_at, and the constant-index call sites in joint_constraint_builder.rs now use basis.x_axis/basis.y_axis directly.

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