Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,66 @@ This is a workspace of three crates released together: **rsplot** (the plotting
library), **rsdm** (a PyDM-style EPICS display layer built on rsplot), and
**adl2rsdm** (a MEDM `.adl` → RsDM-Rust-source converter).

## [0.5.2] - 2026-07-08

A correctness and robustness pass over the `VolumeRaycaster` shipped in 0.5.1,
from a post-release review, plus two `rsdm` data-engine race fixes surfaced by
the cross-platform CI. `adl2rsdm` is re-released in lockstep (no functional
change).

### Fixed — `rsplot`

- **Dark fringe around opaque regions is gone.** The 3-D volume texture stored
straight-alpha RGBA, so the linear sampler bled a transparent voxel's `rgb=0`
into neighbouring colour at a boundary, darkening it. The volume is now
premultiplied on upload and composited in premultiplied space, so the filter
interpolates colour that keeps its hue. (`premultiply_rgba` is unit-tested.)
- **Opacity no longer depends on the ray step count.** Per-sample coverage was
composited as-is, so raising `set_steps` also made the whole volume more
opaque. Each sample's coverage is now Beer-Lambert–corrected to a 256-step
baseline, so opacity depends only on the data and `set_alpha_scale`; the
default view is unchanged.
- **`set_steps` is clamped to `[1, 4096]`.** A very large value marched every ray
that many times in one frame and could trip the OS GPU-timeout watchdog and
reset the device.
- **Invalid volumes are rejected up front.** `set_volume` now validates the
extent and that the RGBA length matches `depth·height·width·4`, so a bad call
is a no-op (keeping any prior upload) instead of a wgpu validation panic deep
in the render backend.
- **Ctrl/Cmd-pan tracks the pointer one-to-one.** The pan was anchored on the far
plane, so the volume moved faster than the cursor; it is now anchored at the
box-centre depth (the same depth the wheel zoom uses).
- **Same `VolumeId` painted twice in one frame keeps each camera.** The per-id
uniform buffer was shared between `prepare` and `paint`, so two callbacks with
one id both rendered from whichever prepared last (e.g. the same volume in two
panels). Dynamic camera state moved to per-callback buffers.

### Changed — `rsplot`

- **`VolumeRaycaster::remove` / `remove_volume_raycaster(id)`** free a volume's
GPU resources (3-D texture, bind group, uniform buffer); previously an id's
VRAM was held for the app's lifetime with no way to release it.
- Dropped the unused `cam_pos` uniform (the shader un-projects rays through
`inv_mvp`), shrinking the per-frame uniform buffer.

### Fixed — `rsdm`

- **`ca://` no longer leaks a duplicate initial sample.** The connect-time
metadata fetch posted the initial value unconditionally, but the connection
task can run its connect handler more than once (the CA client emits a native
DBR-type "change" on the first connect, re-entering it). The second post
re-emitted a value the value monitor had already delivered, and a strip-chart /
event-plot `subscribe_values` created in between received it as a spurious
sample. The connect-time value is now gated on an actual change, like the
monitor path.
- **`calc://` always publishes its initial derived value.** The poll loop
consumed a child's recompute trigger even on ticks where not every child was
connected yet, so if the last child to connect was not itself a triggering
variable (an `update` list excluding it), the first all-connected tick saw no
change and never ran the initial evaluation — the calc stayed connected but
valueless. Child stamp changes are now folded in only once every child is
connected, so the initial evaluation is order-independent.

## [0.5.1] - 2026-07-07

A feature-bearing patch over the `0.5.0` crate rename: a new volume-rendering
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rsplot"
version = "0.5.1"
version = "0.5.2"
edition = "2024"
rust-version = "1.92"
license = "MIT OR Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion adl2rsdm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "adl2rsdm"
version = "0.5.1"
version = "0.5.2"
edition = "2024"
rust-version = "1.92"
license = "MIT OR Apache-2.0"
Expand Down
4 changes: 2 additions & 2 deletions rsdm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rsdm"
version = "0.5.1"
version = "0.5.2"
edition = "2024"
rust-version = "1.92"
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -53,7 +53,7 @@ calc = ["dep:evalexpr", "dep:epics-base-rs"]
# `version` (alongside `path`) is required so `cargo publish` records a
# crates.io dependency on the published rsplot; the path is used for local
# workspace builds. Keep this in lockstep with rsplot's version.
rsplot = { path = "..", version = "0.5.1" }
rsplot = { path = "..", version = "0.5.2" }
# Async runtime for the data engine. The connection tasks live on a tokio
# runtime the `Engine` owns; widgets read shared state on the GUI thread. `net`
# is enabled so `enable_all()` (and the EPICS backends, later) work.
Expand Down
37 changes: 26 additions & 11 deletions rsdm/src/data_plugins/calc_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,25 +279,40 @@ async fn run_channel(
_ = ticker.tick() => {
let all_connected = children.iter().all(|(_, ch)| ch.is_connected());

if all_connected != connected {
connected = all_connected;
writer.update(move |s| s.connected = all_connected);
}

// Note every child whose value changed; a change to a variable
// in the `update` list (or any variable when no list is given)
// triggers a recompute.
//
// Fold stamp changes into `prev_stamps` ONLY while every child
// is connected — i.e. only while a recompute could actually
// run. A child's initial value often lands a tick or more
// before the last child connects; consuming that change here
// (advancing `prev_stamps` regardless of `all_connected`, the
// old behaviour) dropped its trigger, so the first
// all-connected tick saw no change and the initial value was
// never published — the calc stayed connected but valueless
// until the next child write. Leaving `prev_stamps` at
// `u64::MAX` until the first all-connected tick makes that tick
// observe every child as changed and run the initial eval, with
// no dependence on the child-connect vs poll-tick interleaving.
let mut trigger = false;
for (i, (name, ch)) in children.iter().enumerate() {
let stamp = ch.stamp();
if stamp != prev_stamps[i] {
prev_stamps[i] = stamp;
if update.as_ref().is_none_or(|u| u.iter().any(|n| n == name)) {
trigger = true;
if all_connected {
for (i, (name, ch)) in children.iter().enumerate() {
let stamp = ch.stamp();
if stamp != prev_stamps[i] {
prev_stamps[i] = stamp;
if update.as_ref().is_none_or(|u| u.iter().any(|n| n == name)) {
trigger = true;
}
}
}
}

if all_connected != connected {
connected = all_connected;
writer.update(move |s| s.connected = all_connected);
}

if all_connected && trigger
&& let Some(value) = evaluate(
&id,
Expand Down
30 changes: 23 additions & 7 deletions rsdm/src/data_plugins/epics_plugins/ca_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,30 @@ async fn on_connect(
.filter(|e| !e.strings.is_empty())
.map(|e| latin1_strings(&e.strings));
*enum_cache = strings.clone();
// The connect-time snapshot carries the initial value, so post it as
// a value event (not a bare snapshot update) — the first strip-chart
// sample. Always a value event: PyDM clears its value cache on
// (re)connect, so the first callback after connect emits
// unconditionally (pyepics_plugin_component.py:192-199 → :102).
// The connect-time snapshot carries the initial value. Post it as a
// value event (the first strip-chart sample) — but gate it on the
// same `last_value` change check the monitor branch uses, not
// unconditionally. The value monitor and this metadata fetch both
// deliver the connect-time value, and the runtime fixes neither
// their order nor whether `on_connect` runs once or twice (a
// native-type-change / reconnect event re-enters it). Posting here
// unconditionally re-emits a value the monitor — or a prior
// `on_connect` — already delivered on this connection; if a value
// subscriber was created in between (the strip-chart / event-plot
// `subscribe_values` case) that duplicate leaks to it as a spurious
// sample. Emit only on an actual change; otherwise refresh the CTRL
// metadata (units/precision/enum strings the TIME-class monitor
// lacks) without a sample. PyDM clear_cache parity is preserved: a
// reconnect clears `last_value` (the Disconnected arm), so the first
// value after a reconnect still emits (pyepics_plugin_component.py:
// 192-199 → :102).
let value = epics_to_pv(&snap.value, strings.as_deref());
*last_value = Some(value.clone());
writer.post_value(move |s| apply_metadata(s, &snap, value, strings));
if last_value.as_ref() != Some(&value) {
*last_value = Some(value.clone());
writer.post_value(move |s| apply_metadata(s, &snap, value, strings));
} else {
writer.update(move |s| apply_metadata(s, &snap, value, strings));
}
}
Err(_) => {
// Connected, but the metadata read failed; reflect the connection so
Expand Down
64 changes: 56 additions & 8 deletions rsdm/tests/calc_derived.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ use std::time::{Duration, Instant};

use rsdm::{Engine, PvValue};

/// Poll deadline for the async calc pipeline to settle. Generous on purpose:
/// `wait_for` returns as soon as the condition holds, so a large deadline only
/// bounds the failure path and never slows a passing run — but the `calc://`
/// chain (child connect → value poll → expr eval → republish) can take well
/// over a second on a loaded CI runner, and a 2 s deadline flaked on macOS.
const WAIT: Duration = Duration::from_secs(30);

/// Poll `cond` until it holds or `timeout` elapses; returns the final result.
fn wait_for(mut cond: impl FnMut() -> bool, timeout: Duration) -> bool {
let start = Instant::now();
Expand Down Expand Up @@ -44,15 +51,15 @@ fn calc_sums_two_local_children_and_recomputes_on_write() {

// Both children configure via the direct handles → calc connects.
assert!(
wait_for(|| calc.is_connected(), Duration::from_secs(2)),
wait_for(|| calc.is_connected(), WAIT),
"calc channel never connected (children did not all connect)"
);

// Children default to float 0.0, so the initial derived value is 0.0.
assert!(
wait_for(
|| matches!(calc.read(|s| s.value.clone()), Some(PvValue::Float(v)) if v.abs() < 1e-9),
Duration::from_secs(2)
WAIT
),
"did not observe the initial derived value 0.0 (got {:?})",
calc.read(|s| s.value.clone())
Expand All @@ -64,7 +71,7 @@ fn calc_sums_two_local_children_and_recomputes_on_write() {
assert!(
wait_for(
|| matches!(calc.read(|s| s.value.clone()), Some(PvValue::Float(v)) if (v - 5.0).abs() < 1e-9),
Duration::from_secs(2)
WAIT
),
"did not observe the recomputed sum 5.0 (got {:?})",
calc.read(|s| s.value.clone())
Expand All @@ -87,13 +94,13 @@ fn calc_update_list_restricts_which_child_triggers_recompute() {
.expect("connect child b");

assert!(
wait_for(|| calc.is_connected(), Duration::from_secs(2)),
wait_for(|| calc.is_connected(), WAIT),
"calc channel never connected"
);
assert!(
wait_for(
|| matches!(calc.read(|s| s.value.clone()), Some(PvValue::Float(v)) if v.abs() < 1e-9),
Duration::from_secs(2)
WAIT
),
"did not observe the initial derived value 0.0"
);
Expand All @@ -113,13 +120,54 @@ fn calc_update_list_restricts_which_child_triggers_recompute() {
assert!(
wait_for(
|| matches!(calc.read(|s| s.value.clone()), Some(PvValue::Float(v)) if (v - 6.0).abs() < 1e-9),
Duration::from_secs(2)
WAIT
),
"a (in update list) did not trigger a recompute to 6.0 (got {:?})",
calc.read(|s| s.value.clone())
);
}

/// Regression for the lost initial-evaluation edge: a child whose value lands
/// before *every* child is connected must not have its recompute trigger
/// silently consumed. With `update=a`, connect `a` first and let the 50 ms poll
/// loop observe it (over several ticks) while `b` is still a bare, disconnected
/// child; then connect `b`. `b` is not in the update list, so `b`'s own
/// connection cannot be the trigger — only the pending change to `a` can. The
/// initial evaluation must still run on the first all-connected tick.
///
/// Before the fix the poll loop folded `a`'s change into `prev_stamps` on a
/// not-all-connected tick, so the first all-connected tick saw no triggering
/// change and never published — the calc stayed connected but valueless, and
/// this timed out. This is the deterministic form of the child-connect-order
/// flake that hit `calc://` on CI.
#[test]
fn calc_publishes_initial_value_when_update_var_connects_before_others() {
let engine = Engine::new();
let calc = engine
.connect("calc://early?expr=a+b&a=loc://calc_early_a&b=loc://calc_early_b&update=a")
.expect("connect calc channel");
// Connect the update variable `a` first, then let the poll loop run several
// 50 ms ticks with `a` connected but `b` still bare and disconnected.
let _a = engine
.connect("loc://calc_early_a?type=float&init=2")
.expect("connect child a");
std::thread::sleep(Duration::from_millis(300));
// Now connect `b` (not in the update list): only the pending change to `a`
// can trigger the initial evaluation.
let _b = engine
.connect("loc://calc_early_b?type=float&init=3")
.expect("connect child b");

assert!(
wait_for(
|| matches!(calc.read(|s| s.value.clone()), Some(PvValue::Float(v)) if (v - 5.0).abs() < 1e-9),
WAIT
),
"initial derived value a+b = 2+3 = 5.0 was never published (got {:?})",
calc.read(|s| s.value.clone())
);
}

// ---------------------------------------------------------------------------
// MEDM CALC dialect (`?dialect=medm`) — the adl2rsdm visibility-gate language.
// ---------------------------------------------------------------------------
Expand All @@ -128,7 +176,7 @@ fn calc_update_list_restricts_which_child_triggers_recompute() {
fn wait_for_float(calc: &rsdm::Channel, expected: f64) -> bool {
wait_for(
|| matches!(calc.read(|s| s.value.clone()), Some(PvValue::Float(v)) if (v - expected).abs() < 1e-9),
Duration::from_secs(2),
WAIT,
)
}

Expand Down Expand Up @@ -203,7 +251,7 @@ fn medm_ternary_functions_and_encoded_and_evaluate() {
.connect("loc://medm_t_b?type=float&init=0")
.expect("connect child b");
assert!(
wait_for(|| calc.is_connected(), Duration::from_secs(2)),
wait_for(|| calc.is_connected(), WAIT),
"medm ternary channel never connected"
);

Expand Down
30 changes: 23 additions & 7 deletions src/render/shaders/volume_raycaster.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
//
// A full-screen triangle generates one camera ray per pixel from the inverse
// view-projection matrix. Each ray is clipped to the volume's axis-aligned box
// and marched in fixed steps, sampling straight-alpha RGBA and accumulating
// PREMULTIPLIED colour so the result blends with `PREMULTIPLIED_ALPHA_BLENDING`
// and marched in fixed steps. The 3D texture holds PREMULTIPLIED RGBA (the
// uploader premultiplies straight alpha) so the hardware linear filter
// interpolates premultiplied colour — a colour/transparent boundary stays the
// right hue instead of bleeding toward black. Samples accumulate front-to-back
// into premultiplied colour that blends with `PREMULTIPLIED_ALPHA_BLENDING`
// straight into egui's render pass (no offscreen target, no depth).

struct Uniforms {
// Inverse of the camera clip matrix P·V (OpenGL clip, z in [-1, 1]).
inv_mvp: mat4x4<f32>,
cam_pos: vec4<f32>, // world-space camera position (xyz; w unused)
vol_min: vec4<f32>, // world-space AABB min (xyz)
vol_max: vec4<f32>, // world-space AABB max (xyz)
// x = step count, y = alpha scale, z = sample-alpha cull floor, w unused.
Expand All @@ -20,6 +22,12 @@ struct Uniforms {
@group(0) @binding(1) var vol_tex: texture_3d<f32>;
@group(0) @binding(2) var vol_samp: sampler;

// Reference sample count for the opacity correction: at this many steps a
// sample's coverage is used as-is, and other step counts are corrected so the
// accumulated opacity stays the same. Matches the widget's default `steps`, so
// the default view is unchanged.
const REF_STEPS: f32 = 256.0;

struct VsOut {
@builtin(position) clip: vec4<f32>,
@location(0) ndc: vec2<f32>,
Expand Down Expand Up @@ -80,11 +88,19 @@ fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
for (var i = 0; i < steps; i = i + 1) {
let p = ro + rd * t;
let uvw = (p - u.vol_min.xyz) / extent;
let s = textureSampleLevel(vol_tex, vol_samp, uvw, 0.0);
let s = textureSampleLevel(vol_tex, vol_samp, uvw, 0.0); // premultiplied rgb, coverage a
if s.a > cull_floor {
let a = clamp(s.a * alpha_scale, 0.0, 1.0);
let w = (1.0 - acc.a) * a;
acc = vec4<f32>(acc.rgb + s.rgb * w, acc.a + w);
// Scale coverage by the user opacity knob, then correct it for the
// step count so accumulated opacity is invariant to `steps`
// (transmittance (1-sa)^steps held to the REF_STEPS baseline).
let sa = clamp(s.a * alpha_scale, 0.0, 1.0);
let sa_c = 1.0 - pow(1.0 - sa, REF_STEPS / f32(steps));
// Rescale the premultiplied colour to the corrected coverage
// (gain = sa_c/s.a). Compositing stays in premultiplied space, so no
// dark fringe at boundaries.
let gain = sa_c / max(s.a, 1e-6);
let w = 1.0 - acc.a;
acc = vec4<f32>(acc.rgb + s.rgb * gain * w, acc.a + sa_c * w);
}
if acc.a >= 0.995 {
break;
Expand Down
Loading
Loading