bevyengine / bevyengine/bevy

bevy_solari: expose a per-instance id on ray hits (instance_custom_index is unused)

Open
#24,654 1 comment 0 reactions 0 assignees View on GitHub
A-Rendering C-Feature D-Modest S-Needs-Design
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## What problem does this solve?

`bevy_solari` builds every TLAS instance with `instance_custom_index` hardcoded:

```rust
// crates/bevy_solari/src/scene/binder.rs
TlasInstance::new(blas, tlas_transform(&transform), Default::default(), 0xFF)
```

So a shader resolving a ray hit has no way to tell **which instance/entity** it hit. `ray_hit.instance_index` is solari's internal per-frame counter (render-world query iteration order), which is neither stable across frames nor meaningful to the application. The source already flags the slot as free:

```rust
// TODO: Store material_id in instance_custom_index instead?
```

This blocks any "which object did this ray hit" use case on the RT path:

- object / instance-id passes
- segmentation masks (ground-truth rendering)
- GPU picking
- per-object debug overlays

## Possible solutions

Open to whichever direction maintainers prefer:

- Expose the hit **entity index** (consumers map entity → their own id on the CPU after readback), or
- Pass a user-supplied `u32` through to `instance_custom_index` via a component, e.g. `RaytracingInstanceId(u32)`.

The first keeps solari free of application concepts; the second lets callers inject any id directly.

## Additional context

Still hardcoded on `main`. Happy to open a PR once a direction is agreed.

Contributor guide

Open the contributing guide

Research direction

Start in crates/bevy_solari/src/scene/binder.rs and inspect the TlasInstance::new call that hardcodes instance_custom_index to 0xFF, along with the ray-hit handling described in the issue. First confirm the maintainers' preferred direction—exposing an entity index or accepting a user-supplied u32—then define completion around making that identifier available to shaders without relying on ray_hit.instance_index.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
computer-graphics
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.