From 0bb13b45fe0b0502368a3fbbf6fc169b79cb7ac5 Mon Sep 17 00:00:00 2001 From: Sang Woo Kim Date: Sat, 17 Oct 2026 21:31:58 +0900 Subject: [PATCH 1/6] fix(render): bind the per-curve params blocks as storage buffers On Windows' DX12 backend with the WARP software adapter every curve in a pass read the last curve's uniform block, so all curves took the last curve's colour; the storage buffers in the same bind groups read per-curve. Vulkan on hardware and on lavapipe never showed it. The blocks hold no arrays or vec3, so the layouts coincide and the repr(C) structs are unchanged. --- CHANGELOG.md | 9 +++ src/render/gpu_curve.rs | 93 +++++++++++++++++-------------- src/render/shaders/curve.wgsl | 2 +- src/render/shaders/errorbars.wgsl | 2 +- src/render/shaders/fill.wgsl | 2 +- src/render/shaders/linecaps.wgsl | 2 +- src/render/shaders/markers.wgsl | 2 +- 7 files changed, 66 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fac8a49..14af2da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,15 @@ library), **rsdm** (a PyDM-style EPICS display layer built on rsplot), and ## [Unreleased] +### Fixed + +- **rsplot**: the per-curve params blocks (line, markers, caps, fill, error + bars) are bound as read-only storage buffers instead of uniforms. On + Windows' DX12 backend with the WARP software adapter (a GPU-less VM) every + curve in a pass read the last curve's uniform, so all curves took the last + curve's colour; storage reads were per-curve correct. The block layouts are + unchanged. + ## [0.6.0] - 2026-10-16 ### Changed diff --git a/src/render/gpu_curve.rs b/src/render/gpu_curve.rs index 9503f9f..42c8837 100644 --- a/src/render/gpu_curve.rs +++ b/src/render/gpu_curve.rs @@ -3,7 +3,7 @@ //! [`CurveData`] is the CPU spec (mirrors silx `addCurve`: x/y arrays + color + //! width + Y-axis binding). [`CurvePipeline`] holds the thick-line pipeline //! shared across curves. [`GpuCurve`] owns one curve's point storage buffer + -//! uniform + bind group and persists across frames in `WgpuResources`. +//! params + bind group and persists across frames in `WgpuResources`. //! //! Each segment of the polyline is expanded in the vertex shader into a //! screen-space quad (two triangles) of the curve's pixel width, so the line is @@ -40,11 +40,20 @@ const IDENTITY: [[f32; 4]; 4] = [ /// arc length) matches the shader's `apply_scale` exactly. const INV_LN10: f32 = std::f32::consts::LOG10_E; -/// Uniform block for the curve shader. Field order is laid out so the natural +/// Params block for the curve shader. Field order is laid out so the natural /// `repr(C)` offsets coincide with WGSL std140 alignment (all `vec4`s first at /// 16-aligned offsets, then `vec2`s at 8-aligned, then scalars): mat4 @0, vec4 /// @64, vec4 @80, vec4 @96, vec2 @112, vec2 @120, f32 @128/132/136/140; total /// 144. Matches `Params` in `curve.wgsl`. +/// +/// Every per-curve params block (this one and the marker/caps/fill/error-bar +/// ones) is bound as a read-only *storage* buffer, not a uniform. On Windows' +/// DX12 backend with the WARP software adapter (a GPU-less VM) every curve in +/// a pass read the last curve's uniform block — all curves took the last +/// curve's colour — while storage reads stayed per-curve correct; Vulkan on +/// hardware and on lavapipe never showed it. The blocks hold only +/// mat4/vec4/vec2/scalars (no arrays, no vec3), so the uniform and storage +/// layouts coincide and the `repr(C)` structs are unchanged. #[repr(C)] #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] struct CurveParams { @@ -113,7 +122,7 @@ impl LineStyle { } } -/// Uniform block for the fill shader. Field order keeps `repr(C)` offsets +/// Params block for the fill shader. Field order keeps `repr(C)` offsets /// std140-aligned: mat4 @0, vec4 @64, vec2 @80, vec2 @88; total 96. Matches /// `Params` in `fill.wgsl`. #[repr(C)] @@ -125,7 +134,7 @@ struct FillParams { _pad: [f32; 2], } -/// Uniform block for the error-bar shader. Same layout as the marker uniform up +/// Params block for the error-bar shader. Same layout as the marker block up /// to the line width: mat4 @0, vec4 @64, vec2 @80, vec2 @88, f32 @96; total 112. /// Matches `Params` in `errorbars.wgsl`. #[repr(C)] @@ -140,7 +149,7 @@ struct ErrorBarParams { _pad: [f32; 3], } -/// Uniform block for the marker shader. Layout matches `Params` in +/// Params block for the marker shader. Layout matches `Params` in /// `markers.wgsl` (std140: mat4 @0, vec4 @64, vec2 @80, vec2 @88, f32 @96, /// u32 @100, f32 @104; padded to 112). `use_vertex_color` consumes one of the /// former `_pad` slots, so the 112-byte std140 size is unchanged (see the @@ -312,18 +321,18 @@ impl CurveData { } /// The render pipelines shared by all curves: the thick-line, marker, fill, and -/// error-bar pipelines. The line layout has the curve uniform at binding 0, the +/// error-bar pipelines. The line layout has the curve params at binding 0, the /// points storage buffer at binding 1, a per-vertex color buffer at binding 2, /// and the dash arc length at binding 3. The marker, fill, and error-bar /// pipelines each have their own minimal layout (so the line layout can grow -/// without affecting them): the marker layout is uniform + points; the fill -/// layout is uniform + points + baseline; the error-bar layout is uniform + +/// without affecting them): the marker layout is params + points; the fill +/// layout is params + points + baseline; the error-bar layout is params + /// segments. pub struct CurvePipeline { pub(crate) pipeline: wgpu::RenderPipeline, pub(crate) marker_pipeline: wgpu::RenderPipeline, /// Round joins/caps: an AA disc of the line width at every vertex. Shares the - /// marker bind-group layout (uniform + points + per-vertex colors). + /// marker bind-group layout (params + points + per-vertex colors). pub(crate) caps_pipeline: wgpu::RenderPipeline, pub(crate) fill_pipeline: wgpu::RenderPipeline, pub(crate) errorbar_pipeline: wgpu::RenderPipeline, @@ -364,7 +373,7 @@ impl CurvePipeline { binding: 0, visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, + ty: wgpu::BufferBindingType::Storage { read_only: true }, has_dynamic_offset: false, min_binding_size: NonZeroU64::new( std::mem::size_of::() as u64 @@ -420,9 +429,9 @@ impl CurvePipeline { immediate_size: 0, }); - // Markers have their own minimal layout (uniform at 0 + points at 1 + - // per-vertex colors at 2) so the curve uniform/layout can grow dashes, - // etc. without forcing the marker uniform to match the curve uniform's + // Markers have their own minimal layout (params at 0 + points at 1 + + // per-vertex colors at 2) so the curve params/layout can grow dashes, + // etc. without forcing the marker params to match the curve params' // size or carry dead curve-only bindings. let marker_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { @@ -432,7 +441,7 @@ impl CurvePipeline { binding: 0, visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, + ty: wgpu::BufferBindingType::Storage { read_only: true }, has_dynamic_offset: false, min_binding_size: NonZeroU64::new( std::mem::size_of::() as u64 @@ -532,7 +541,7 @@ impl CurvePipeline { }); // Round joins/caps pipeline: a disc per vertex. Reuses the marker layout - // (same bindings: uniform + points + per-vertex colors) and pipeline + // (same bindings: params + points + per-vertex colors) and pipeline // layout, but the AA disc shader instead of the symbol shader. let caps_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some("rsplot line caps pipeline"), @@ -560,7 +569,7 @@ impl CurvePipeline { cache: None, }); - // Fill pipeline: its own layout (fill uniform at 0, the shared points at + // Fill pipeline: its own layout (fill params at 0, the shared points at // 1, the baseline at 2). Two data-space triangles per segment. let fill_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { @@ -570,7 +579,7 @@ impl CurvePipeline { binding: 0, visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, + ty: wgpu::BufferBindingType::Storage { read_only: true }, has_dynamic_offset: false, min_binding_size: NonZeroU64::new( std::mem::size_of::() as u64 @@ -634,7 +643,7 @@ impl CurvePipeline { cache: None, }); - // Error-bar pipeline: its own layout (error-bar uniform at 0, the segment + // Error-bar pipeline: its own layout (error-bar params at 0, the segment // endpoints at 1). Two triangles (6 vertices) per segment, like the line. let errorbar_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { @@ -644,7 +653,7 @@ impl CurvePipeline { binding: 0, visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, + ty: wgpu::BufferBindingType::Storage { read_only: true }, has_dynamic_offset: false, min_binding_size: NonZeroU64::new( std::mem::size_of::() as u64, @@ -747,10 +756,10 @@ pub struct GpuCurve { /// for, or `None` when never computed; lets [`Self::ensure_arclen`] skip /// recomputation when the view is unchanged. arclen_key: Option, - /// Marker uniform + bind group (shares the points buffer at binding 1). + /// Marker params + bind group (shares the points buffer at binding 1). marker_params: wgpu::Buffer, marker_bind_group: wgpu::BindGroup, - /// Round joins/caps uniform + bind group (shares the points + per-vertex + /// Round joins/caps params + bind group (shares the points + per-vertex /// color buffers; an AA disc of the line width at every vertex). caps_params: wgpu::Buffer, caps_bind_group: wgpu::BindGroup, @@ -762,7 +771,7 @@ pub struct GpuCurve { /// Baseline entries the `baseline_buf` can hold; `0` for the placeholder /// (unfilled at creation), forcing a realloc when the curve becomes filled. baseline_capacity: u32, - /// Fill uniform + bind group (shares the points buffer at binding 1). + /// Fill params + bind group (shares the points buffer at binding 1). fill_params: wgpu::Buffer, fill_bind_group: wgpu::BindGroup, /// Error-bar segment endpoints (binding 1 of the error-bar layout), two per @@ -777,7 +786,7 @@ pub struct GpuCurve { /// capacity); `0` for the placeholder, forcing a realloc when the curve /// gains error bars. errorbar_capacity: u32, - /// Error-bar uniform + bind group. + /// Error-bar params + bind group. errorbar_params: wgpu::Buffer, errorbar_bind_group: wgpu::BindGroup, color: [f32; 4], @@ -938,7 +947,7 @@ fn build_errorbar_segments( } impl GpuCurve { - /// Upload `curve`'s vertices and build its uniform + bind group. + /// Upload `curve`'s vertices and build its params + bind group. pub fn new( device: &wgpu::Device, queue: &wgpu::Queue, @@ -995,7 +1004,7 @@ impl GpuCurve { let params = device.create_buffer(&wgpu::BufferDescriptor { label: Some("rsplot curve params"), size: std::mem::size_of::() as u64, - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); @@ -1022,11 +1031,11 @@ impl GpuCurve { ], }); - // Marker uniform + bind group: same layout, sharing the points buffer. + // Marker params + bind group: same layout, sharing the points buffer. let marker_params = device.create_buffer(&wgpu::BufferDescriptor { label: Some("rsplot marker params"), size: std::mem::size_of::() as u64, - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); let marker_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { @@ -1051,13 +1060,13 @@ impl GpuCurve { ], }); - // Round joins/caps uniform + bind group: reuses the marker layout, + // Round joins/caps params + bind group: reuses the marker layout, // sharing the points buffer (binding 1) and the per-vertex color buffer // (binding 2, so a join takes its own vertex's color). let caps_params = device.create_buffer(&wgpu::BufferDescriptor { label: Some("rsplot line caps params"), size: std::mem::size_of::() as u64, - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); let caps_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { @@ -1098,7 +1107,7 @@ impl GpuCurve { let fill_params = device.create_buffer(&wgpu::BufferDescriptor { label: Some("rsplot fill params"), size: std::mem::size_of::() as u64, - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); let fill_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { @@ -1143,7 +1152,7 @@ impl GpuCurve { let errorbar_params = device.create_buffer(&wgpu::BufferDescriptor { label: Some("rsplot errorbar params"), size: std::mem::size_of::() as u64, - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); let errorbar_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { @@ -1203,7 +1212,7 @@ impl GpuCurve { src_y: curve.y.clone(), decimate_key: None, }; - // Seed the uniforms; the per-frame transform/viewport overwrite them. + // Seed the params blocks; the per-frame transform/viewport overwrite them. gpu.write_uniforms(queue, IDENTITY, [0.0, 0.0], [1.0, 1.0]); gpu } @@ -1428,7 +1437,7 @@ impl GpuCurve { }; queue.write_buffer(&self.params, 0, bytemuck::bytes_of(¶ms)); - // Marker uniform shares the same transform/viewport; symbol code is the + // Marker params share the same transform/viewport; symbol code is the // sentinel `0` (unused) when no marker is set. The drawn size honors the // silx per-symbol overrides (pixel = 1px, point shrinks, strokes round to // odd) via `render_size_px`. @@ -1449,7 +1458,7 @@ impl GpuCurve { }; queue.write_buffer(&self.marker_params, 0, bytemuck::bytes_of(&marker)); - // Round joins/caps uniform: an AA disc of the line *width* at every + // Round joins/caps params: an AA disc of the line *width* at every // vertex (radius = half the width), the curve color, and the same // per-vertex-color flag so a coloured join takes its vertex's color. // `symbol` is unused by the caps shader. @@ -1465,7 +1474,7 @@ impl GpuCurve { }; queue.write_buffer(&self.caps_params, 0, bytemuck::bytes_of(&caps)); - // Fill uniform shares the transform; the fill takes the curve color. + // Fill params share the transform; the fill takes the curve color. let fill = FillParams { ortho, color: self.color, @@ -1474,7 +1483,7 @@ impl GpuCurve { }; queue.write_buffer(&self.fill_params, 0, bytemuck::bytes_of(&fill)); - // Error-bar uniform shares the transform; bars take the curve color and + // Error-bar params share the transform; bars take the curve color and // a fixed pixel width. let errorbar = ErrorBarParams { ortho, @@ -1646,14 +1655,16 @@ mod tests { } #[test] - fn marker_params_std140_size_unchanged() { - // `use_vertex_color` consumed a former `_pad` slot, so the std140 size + fn marker_params_size_unchanged() { + // `use_vertex_color` consumed a former `_pad` slot, so the block size // must stay 112 bytes (the value the WGSL `Params` struct is laid out // for: mat4 @0, vec4 @64, vec2 @80, vec2 @88, f32 @96, u32 @100, - // f32 @104, padded to 112). A size change here silently desyncs the - // CPU uniform from the shader's binding offsets — fail loudly instead. + // f32 @104, padded to 112 — the same under the uniform and storage + // layout rules, as the block has no arrays or vec3). A size change here + // silently desyncs the CPU block from the shader's offsets — fail + // loudly instead. assert_eq!(std::mem::size_of::(), 112); - // The block must be 16-byte aligned for std140 (the mat4 governs it). + // The block must be 16-byte aligned (the mat4 governs it). assert_eq!(std::mem::size_of::() % 16, 0); } diff --git a/src/render/shaders/curve.wgsl b/src/render/shaders/curve.wgsl index 7d823be..8c34fb3 100644 --- a/src/render/shaders/curve.wgsl +++ b/src/render/shaders/curve.wgsl @@ -36,7 +36,7 @@ struct Params { use_gap_color: f32, // >0.5 to fill gaps with gap_color, else discard }; -@group(0) @binding(0) var params: Params; +@group(0) @binding(0) var params: Params; @group(0) @binding(1) var points: array>; // Per-vertex linear premultiplied RGBA, one per point. A 1-element placeholder // when `use_vertex_color` is 0 (never sampled, but the binding must be present). diff --git a/src/render/shaders/errorbars.wgsl b/src/render/shaders/errorbars.wgsl index 41b1712..871676c 100644 --- a/src/render/shaders/errorbars.wgsl +++ b/src/render/shaders/errorbars.wgsl @@ -16,7 +16,7 @@ struct Params { half_width_px: f32, // half the error-bar line width, in physical pixels }; -@group(0) @binding(0) var params: Params; +@group(0) @binding(0) var params: Params; @group(0) @binding(1) var segs: array>; // 1 / ln(10), to turn the natural log into log10. diff --git a/src/render/shaders/fill.wgsl b/src/render/shaders/fill.wgsl index 6e6cabe..f68c9ba 100644 --- a/src/render/shaders/fill.wgsl +++ b/src/render/shaders/fill.wgsl @@ -13,7 +13,7 @@ struct Params { axis_log: vec2, // 1.0 if that axis is log10, else 0.0 }; -@group(0) @binding(0) var params: Params; +@group(0) @binding(0) var params: Params; @group(0) @binding(1) var points: array>; @group(0) @binding(2) var baseline: array; diff --git a/src/render/shaders/linecaps.wgsl b/src/render/shaders/linecaps.wgsl index 1f7dacf..dfa3761 100644 --- a/src/render/shaders/linecaps.wgsl +++ b/src/render/shaders/linecaps.wgsl @@ -28,7 +28,7 @@ struct Params { use_vertex_color: f32, // >0.5 to take each disc's color from `vcolors` }; -@group(0) @binding(0) var params: Params; +@group(0) @binding(0) var params: Params; @group(0) @binding(1) var points: array>; // Per-vertex linear premultiplied RGBA, one per point (silx per-point line // color). A 1-element placeholder when `use_vertex_color` is 0. Mirrors diff --git a/src/render/shaders/markers.wgsl b/src/render/shaders/markers.wgsl index 8cc42fe..cf400ef 100644 --- a/src/render/shaders/markers.wgsl +++ b/src/render/shaders/markers.wgsl @@ -19,7 +19,7 @@ struct Params { use_vertex_color: f32, // >0.5 to take each marker's color from `vcolors` }; -@group(0) @binding(0) var params: Params; +@group(0) @binding(0) var params: Params; @group(0) @binding(1) var points: array>; // Per-vertex linear premultiplied RGBA, one per point (silx per-point scatter // colormap color). A 1-element placeholder when `use_vertex_color` is 0 (never From 991883d654ad7168afd7eb09fcb4ab86d7970f11 Mon Sep 17 00:00:00 2001 From: Sang Woo Kim Date: Sat, 17 Oct 2026 21:32:43 +0900 Subject: [PATCH 2/6] fix(render): upload colours in the render target's encoding egui-wgpu picks a non-sRGB swapchain format and gamma-encodes in its own shader, while rsplot wrote linearised colours into it, so every curve came out darker than its Color32 and than egui's legend swatch (#4f9bff showed as (20, 84, 255)); PNG export the same. gpu_color now keeps the sRGB bytes for a non-sRGB target and linearises only for an sRGB one, and the clear colour follows the same rule. --- CHANGELOG.md | 6 +++ src/render/backend_wgpu.rs | 22 ++++++-- src/render/gpu_curve.rs | 83 +++++++++++++++++++++++-------- src/render/save.rs | 3 +- src/render/shaders/curve.wgsl | 2 +- src/render/shaders/errorbars.wgsl | 2 +- src/render/shaders/fill.wgsl | 2 +- src/render/shaders/linecaps.wgsl | 2 +- src/render/shaders/markers.wgsl | 2 +- src/widget/plot_widget.rs | 4 +- 10 files changed, 93 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14af2da..858eef6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,12 @@ library), **rsdm** (a PyDM-style EPICS display layer built on rsplot), and curve in a pass read the last curve's uniform, so all curves took the last curve's colour; storage reads were per-curve correct. The block layouts are unchanged. +- **rsplot**: curve, per-vertex, dashed-gap and data-background colours are + uploaded in the render target's encoding: sRGB bytes for a non-sRGB target + (egui-wgpu's swapchain), linear for an sRGB one. They were always + linearised, which on the non-sRGB target drew every colour darker than its + `Color32` and than egui's own legend swatches (`#4f9bff` showed as + `(20, 84, 255)`); PNG export was affected the same way. ## [0.6.0] - 2026-10-16 diff --git a/src/render/backend_wgpu.rs b/src/render/backend_wgpu.rs index e501066..5bcf83a 100644 --- a/src/render/backend_wgpu.rs +++ b/src/render/backend_wgpu.rs @@ -21,7 +21,7 @@ use crate::core::plot::{Plot, PlotId}; use crate::core::shape::{Shape, ShapeKind}; use crate::core::transform::{Margins, Scale, Transform, YAxis}; use crate::core::triangles::Triangles; -use crate::render::gpu_curve::{CurveData, CurvePipeline, GpuCurve}; +use crate::render::gpu_curve::{CurveData, CurvePipeline, GpuCurve, gpu_color}; use crate::render::gpu_image::{ AggregationMode, GpuImage, ImageData, ImagePipeline, ImagePixels, aggregate_blocks, }; @@ -49,6 +49,8 @@ pub struct WgpuResources { curve_pipeline: CurvePipeline, /// Per-plot GPU data, keyed by plot ID. plots: HashMap, + /// Whether the render target is an sRGB format; see [`Self::encode_color`]. + srgb_target: bool, } impl WgpuResources { @@ -112,9 +114,17 @@ impl WgpuResources { image_pipeline, curve_pipeline, plots: HashMap::new(), + srgb_target: target_format.is_srgb(), } } + /// Encode a colour the way the clear pass must write it to the render + /// target (the same rule the curves use; see + /// [`gpu_color`](crate::render::gpu_curve::gpu_color)). + pub(crate) fn encode_color(&self, color: Color32) -> [f32; 4] { + gpu_color(color, self.srgb_target) + } + /// Return the [`PlotGpuData`] for `plot_id`, inserting a fresh entry if it /// does not yet exist. pub(crate) fn get_or_insert_plot( @@ -155,7 +165,8 @@ impl WgpuResources { /// back, and return tightly packed RGBA8 pixels (top row first). Used by /// [`crate::render::save::save_graph`] (`doc/design.md` §13 E1). /// - /// `bg` is the linear, premultiplied clear color; the orthos/axis_log pairs + /// `bg` is the clear color as [`Self::encode_color`] encodes it for + /// `target_format`; the orthos/axis_log pairs /// are the same matrices the on-screen [`CurveCallback`] uses for each Y /// axis. Curves are drawn at whatever resolution they were last decimated /// to, and dashed lines reuse the dash arc length last computed for the @@ -1343,8 +1354,8 @@ pub fn update_curve_at( /// lightweight value re-registered by the egui side every frame; the actual GPU /// resources are looked up from [`WgpuResources`]. pub(crate) struct ClearCallback { - /// Linear color space, premultiplied RGBA. - pub color: [f32; 4], + /// The data-area background; encoded for the target in `prepare`. + pub color: Color32, /// Which plot's GPU data to use. pub plot_id: PlotId, } @@ -1361,8 +1372,9 @@ impl egui_wgpu::CallbackTrait for ClearCallback { let res: &mut WgpuResources = resources .get_mut() .expect("WgpuResources not installed — call rsplot::install() at startup"); + let color = res.encode_color(self.color); let plot_data = res.get_or_insert_plot(device, self.plot_id); - queue.write_buffer(&plot_data.color_uniform, 0, bytemuck::bytes_of(&self.color)); + queue.write_buffer(&plot_data.color_uniform, 0, bytemuck::bytes_of(&color)); Vec::new() } diff --git a/src/render/gpu_curve.rs b/src/render/gpu_curve.rs index 42c8837..ad33a2c 100644 --- a/src/render/gpu_curve.rs +++ b/src/render/gpu_curve.rs @@ -59,8 +59,8 @@ const INV_LN10: f32 = std::f32::consts::LOG10_E; struct CurveParams { ortho: [[f32; 4]; 4], color: [f32; 4], - /// Fill color for dashed-line gaps (linear premultiplied); used only when - /// `use_gap_color` is set. + /// Fill color for dashed-line gaps (premultiplied, in the target's encoding + /// per [`gpu_color`]); used only when `use_gap_color` is set. gap_color: [f32; 4], /// Dash pattern as cumulative boundaries in physical pixels: a fragment at /// phase `p` is "on" when `p < .x` or `.y <= p < .z`; `.w` is the period. @@ -340,6 +340,9 @@ pub struct CurvePipeline { marker_bind_group_layout: wgpu::BindGroupLayout, fill_bind_group_layout: wgpu::BindGroupLayout, errorbar_bind_group_layout: wgpu::BindGroupLayout, + /// Whether the render target is an sRGB format; selects the colour encoding + /// the curves are uploaded in (see [`gpu_color`]). + pub(crate) srgb_target: bool, } impl CurvePipeline { @@ -394,7 +397,7 @@ impl CurvePipeline { }, count: None, }, - // Per-vertex line colors (linear premultiplied RGBA), read in + // Per-vertex line colors (premultiplied RGBA, target encoding), read in // the vertex shader; a 1-element placeholder when unused. wgpu::BindGroupLayoutEntry { binding: 2, @@ -461,7 +464,7 @@ impl CurvePipeline { }, count: None, }, - // Per-vertex marker colors (linear premultiplied RGBA), read + // Per-vertex marker colors (premultiplied RGBA, target encoding), read // in the vertex shader; a 1-element placeholder when unused. wgpu::BindGroupLayoutEntry { binding: 2, @@ -720,6 +723,7 @@ impl CurvePipeline { marker_bind_group_layout, fill_bind_group_layout, errorbar_bind_group_layout, + srgb_target: target_format.is_srgb(), } } } @@ -789,13 +793,17 @@ pub struct GpuCurve { /// Error-bar params + bind group. errorbar_params: wgpu::Buffer, errorbar_bind_group: wgpu::BindGroup, + /// Line color as uploaded (premultiplied, encoded per [`gpu_color`]). color: [f32; 4], /// Line stroke style (selects the dash pattern; [`LineStyle::None`] skips /// the line entirely). line_style: LineStyle, - /// Dashed-gap fill color (linear premultiplied), or `None` to leave gaps + /// Dashed-gap fill color (encoded like `color`), or `None` to leave gaps /// transparent. gap_color: Option<[f32; 4]>, + /// The pipeline's target encoding, copied at creation so an in-place + /// [`Self::update`] encodes new colors the same way. + srgb_target: bool, /// Line width in physical pixels. width: f32, /// Marker symbol, or `None` for a line only. @@ -837,13 +845,30 @@ fn pack(x: &[f64], y: &[f64]) -> Vec<[f32; 2]> { .collect() } -/// Pack sRGB colors into the linear premultiplied RGBA the shader blends with -/// (matches the single-color conversion `egui::Rgba::from`). -fn pack_colors(colors: &[Color32]) -> Vec<[f32; 4]> { - colors - .iter() - .map(|&c| egui::Rgba::from(c).to_array()) - .collect() +/// Encode an sRGB `Color32` as the premultiplied RGBA the curve shaders write +/// straight to the render target, in that target's own encoding. +/// +/// The shaders do no colour conversion: the fragment value is stored as-is +/// and alpha-blended by the target. An sRGB target (`*Srgb` formats) expects +/// linear values and encodes them on store, so the colour is linearised like +/// `egui::Rgba::from` does. A non-sRGB target stores the value verbatim and the +/// display reads it as sRGB, so the colour's own gamma bytes are what must be +/// written — egui-wgpu picks such a target for its swapchain and gamma-encodes +/// in its own shader for the same reason. Writing linear values there made +/// every curve darker than its `Color32` (and than the egui-drawn legend +/// swatch for it): `#4f9bff` showed as `(20, 84, 255)`. +pub(crate) fn gpu_color(c: Color32, srgb_target: bool) -> [f32; 4] { + if srgb_target { + egui::Rgba::from(c).to_array() + } else { + c.to_array().map(|v| f32::from(v) / 255.0) + } +} + +/// Pack sRGB colors into the premultiplied RGBA the shader blends with, +/// encoded for the target like the single-color path ([`gpu_color`]). +fn pack_colors(colors: &[Color32], srgb_target: bool) -> Vec<[f32; 4]> { + colors.iter().map(|&c| gpu_color(c, srgb_target)).collect() } /// Project one data point to the shader's pixel space — NDC (`ortho * point`, @@ -982,7 +1007,7 @@ impl GpuCurve { mapped_at_creation: false, }); if let Some(colors) = &curve.colors { - let packed = pack_colors(colors); + let packed = pack_colors(colors, pipeline.srgb_target); if !packed.is_empty() { queue.write_buffer(&vcolors, 0, bytemuck::cast_slice(&packed)); } @@ -1170,8 +1195,9 @@ impl GpuCurve { ], }); - // sRGB Color32 -> linear, premultiplied RGBA (matches the alpha-blend target). - let color = egui::Rgba::from(curve.color).to_array(); + // sRGB Color32 -> premultiplied RGBA in the target's encoding. + let srgb_target = pipeline.srgb_target; + let color = gpu_color(curve.color, srgb_target); let gpu = Self { points, @@ -1201,7 +1227,8 @@ impl GpuCurve { errorbar_bind_group, color, line_style: curve.line_style.clone(), - gap_color: curve.gap_color.map(|c| egui::Rgba::from(c).to_array()), + gap_color: curve.gap_color.map(|c| gpu_color(c, srgb_target)), + srgb_target, width: curve.width, symbol: curve.symbol, marker_size: curve.marker_size, @@ -1288,7 +1315,7 @@ impl GpuCurve { self.count = positions.len() as u32; self.vertex_color = curve.colors.is_some(); if let Some(colors) = &curve.colors { - let packed = pack_colors(colors); + let packed = pack_colors(colors, self.srgb_target); if !packed.is_empty() { queue.write_buffer(&self.vcolors, 0, bytemuck::cast_slice(&packed)); } @@ -1304,9 +1331,9 @@ impl GpuCurve { if !segs.is_empty() { queue.write_buffer(&self.errorbar_segs, 0, bytemuck::cast_slice(&segs)); } - self.color = egui::Rgba::from(curve.color).to_array(); + self.color = gpu_color(curve.color, self.srgb_target); self.line_style = curve.line_style.clone(); - self.gap_color = curve.gap_color.map(|c| egui::Rgba::from(c).to_array()); + self.gap_color = curve.gap_color.map(|c| gpu_color(c, self.srgb_target)); self.width = curve.width; self.symbol = curve.symbol; self.marker_size = curve.marker_size; @@ -1668,12 +1695,28 @@ mod tests { assert_eq!(std::mem::size_of::() % 16, 0); } + #[test] + fn gpu_color_keeps_gamma_bytes_for_non_srgb_target() { + // A non-sRGB target stores the value verbatim, so the colour's own + // (premultiplied) sRGB bytes go in; an sRGB target linearises on store, + // so the linear value does. + let c = Color32::from_rgb(0x4f, 0x9b, 0xff); + assert_eq!(gpu_color(c, false), [79.0 / 255.0, 155.0 / 255.0, 1.0, 1.0]); + assert_eq!(gpu_color(c, true), egui::Rgba::from(c).to_array()); + // Alpha is never gamma-encoded; the colour channels stay premultiplied. + let t = Color32::from_rgba_unmultiplied(255, 0, 0, 128); + assert_eq!(gpu_color(t, false)[3], 128.0 / 255.0); + assert_eq!(gpu_color(t, false)[0], f32::from(t.r()) / 255.0); + } + #[test] fn pack_colors_matches_single_color_conversion() { // Per-vertex packing must match the single-color path so a uniform // per-vertex list renders identically to a single `color`. let c = Color32::from_rgba_unmultiplied(200, 100, 50, 180); - assert_eq!(pack_colors(&[c])[0], egui::Rgba::from(c).to_array()); + for srgb_target in [false, true] { + assert_eq!(pack_colors(&[c], srgb_target)[0], gpu_color(c, srgb_target)); + } } #[test] diff --git a/src/render/save.rs b/src/render/save.rs index 9dcb608..ff022e2 100644 --- a/src/render/save.rs +++ b/src/render/save.rs @@ -1184,13 +1184,12 @@ pub fn render_plot_rgba( } } } - let bg = egui::Rgba::from(plot.data_background).to_array(); - let renderer = render_state.renderer.read(); let res: &WgpuResources = renderer .callback_resources .get() .expect("WgpuResources not installed — call rsplot::install() first"); + let bg = res.encode_color(plot.data_background); res.render_to_rgba( &render_state.device, &render_state.queue, diff --git a/src/render/shaders/curve.wgsl b/src/render/shaders/curve.wgsl index 8c34fb3..a414f0d 100644 --- a/src/render/shaders/curve.wgsl +++ b/src/render/shaders/curve.wgsl @@ -38,7 +38,7 @@ struct Params { @group(0) @binding(0) var params: Params; @group(0) @binding(1) var points: array>; -// Per-vertex linear premultiplied RGBA, one per point. A 1-element placeholder +// Per-vertex premultiplied RGBA (target encoding), one per point. A 1-element placeholder // when `use_vertex_color` is 0 (never sampled, but the binding must be present). @group(0) @binding(2) var vcolors: array>; // Per-vertex cumulative pixel arc length. A 1-element placeholder when the line diff --git a/src/render/shaders/errorbars.wgsl b/src/render/shaders/errorbars.wgsl index 871676c..1d1ca5c 100644 --- a/src/render/shaders/errorbars.wgsl +++ b/src/render/shaders/errorbars.wgsl @@ -10,7 +10,7 @@ struct Params { ortho: mat4x4, - color: vec4, // linear premultiplied RGBA + color: vec4, // premultiplied RGBA (target encoding) axis_log: vec2, // 1.0 if that axis is log10, else 0.0 viewport_px: vec2, // data-area size in physical pixels half_width_px: f32, // half the error-bar line width, in physical pixels diff --git a/src/render/shaders/fill.wgsl b/src/render/shaders/fill.wgsl index f68c9ba..797c808 100644 --- a/src/render/shaders/fill.wgsl +++ b/src/render/shaders/fill.wgsl @@ -9,7 +9,7 @@ struct Params { ortho: mat4x4, - color: vec4, // linear premultiplied RGBA + color: vec4, // premultiplied RGBA (target encoding) axis_log: vec2, // 1.0 if that axis is log10, else 0.0 }; diff --git a/src/render/shaders/linecaps.wgsl b/src/render/shaders/linecaps.wgsl index dfa3761..f8e28c2 100644 --- a/src/render/shaders/linecaps.wgsl +++ b/src/render/shaders/linecaps.wgsl @@ -30,7 +30,7 @@ struct Params { @group(0) @binding(0) var params: Params; @group(0) @binding(1) var points: array>; -// Per-vertex linear premultiplied RGBA, one per point (silx per-point line +// Per-vertex premultiplied RGBA (target encoding), one per point (silx per-point line // color). A 1-element placeholder when `use_vertex_color` is 0. Mirrors // curve.wgsl's vcolors so a join between two coloured vertices takes its own // vertex's color. diff --git a/src/render/shaders/markers.wgsl b/src/render/shaders/markers.wgsl index cf400ef..61bc871 100644 --- a/src/render/shaders/markers.wgsl +++ b/src/render/shaders/markers.wgsl @@ -21,7 +21,7 @@ struct Params { @group(0) @binding(0) var params: Params; @group(0) @binding(1) var points: array>; -// Per-vertex linear premultiplied RGBA, one per point (silx per-point scatter +// Per-vertex premultiplied RGBA (target encoding), one per point (silx per-point scatter // colormap color). A 1-element placeholder when `use_vertex_color` is 0 (never // sampled, but the binding must be present). Mirrors curve.wgsl's vcolors. @group(0) @binding(2) var vcolors: array>; diff --git a/src/widget/plot_widget.rs b/src/widget/plot_widget.rs index dec172a..42fdab8 100644 --- a/src/widget/plot_widget.rs +++ b/src/widget/plot_widget.rs @@ -391,8 +391,6 @@ impl<'a> PlotView<'a> { let ppp = ui.ctx().pixels_per_point(); let viewport_px = [area.width() * ppp, area.height() * ppp]; - // Convert sRGB Color32 to linear, premultiplied RGBA expected by the shader. - let bg = egui::Rgba::from(plot.data_background).to_array(); let style = chrome::Style::from_visuals(ui.visuals()) .with_overrides(plot.foreground, plot.grid_color); @@ -409,7 +407,7 @@ impl<'a> PlotView<'a> { painter.add(egui_wgpu::Callback::new_paint_callback( area, ClearCallback { - color: bg, + color: plot.data_background, plot_id: plot.id, }, )); From 9834015f19ac9fdd21a8c3d3fe42dfb215d1fef0 Mon Sep 17 00:00:00 2001 From: Sang Woo Kim Date: Sat, 17 Oct 2026 21:45:04 +0900 Subject: [PATCH 3/6] fix(render): bind the remaining params blocks as storage buffers The data-area clear, the image tiles, the three 3D scene pipelines and the volume ray-caster still bound their params as uniforms, the binding type under which DX12/WARP read every draw's block from the last one bound. Same rule for every block now; none holds arrays or nested structs, so the repr(C) layouts and sizes are unchanged. --- CHANGELOG.md | 13 ++--- src/render/backend_wgpu.rs | 4 +- src/render/gpu_image.rs | 36 +++++++------- src/render/gpu_scene3d.rs | 60 +++++++++++++----------- src/render/shaders/clear.wgsl | 7 +-- src/render/shaders/image.wgsl | 2 +- src/render/shaders/image_rgba.wgsl | 2 +- src/render/shaders/scene3d.wgsl | 14 +++--- src/render/shaders/scene3d_image.wgsl | 2 +- src/render/shaders/scene3d_mesh.wgsl | 2 +- src/render/shaders/scene3d_points.wgsl | 2 +- src/render/shaders/volume_raycaster.wgsl | 2 +- src/render/volume_raycaster.rs | 19 ++++---- 13 files changed, 91 insertions(+), 74 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 858eef6..1b23db5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,12 +13,13 @@ library), **rsdm** (a PyDM-style EPICS display layer built on rsplot), and ### Fixed -- **rsplot**: the per-curve params blocks (line, markers, caps, fill, error - bars) are bound as read-only storage buffers instead of uniforms. On - Windows' DX12 backend with the WARP software adapter (a GPU-less VM) every - curve in a pass read the last curve's uniform, so all curves took the last - curve's colour; storage reads were per-curve correct. The block layouts are - unchanged. +- **rsplot**: every per-draw params block — the curve pipelines (line, + markers, caps, fill, error bars), the data-area clear, the image tiles, the + 3D scene pipelines and the volume ray-caster — is bound as a read-only + storage buffer instead of a uniform. On Windows' DX12 backend with the WARP + software adapter (a GPU-less VM) every curve in a pass read the last + curve's uniform, so all curves took the last curve's colour; storage reads + were per-curve correct. The block layouts are unchanged. - **rsplot**: curve, per-vertex, dashed-gap and data-background colours are uploaded in the render target's encoding: sRGB bytes for a non-sRGB target (egui-wgpu's swapchain), linear for an sRGB one. They were always diff --git a/src/render/backend_wgpu.rs b/src/render/backend_wgpu.rs index 5bcf83a..5fb48b0 100644 --- a/src/render/backend_wgpu.rs +++ b/src/render/backend_wgpu.rs @@ -68,7 +68,7 @@ impl WgpuResources { binding: 0, visibility: wgpu::ShaderStages::FRAGMENT, ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, + ty: wgpu::BufferBindingType::Storage { read_only: true }, has_dynamic_offset: false, min_binding_size: NonZeroU64::new(16), }, @@ -136,7 +136,7 @@ impl WgpuResources { let color_uniform = device.create_buffer(&wgpu::BufferDescriptor { label: Some("rsplot clear color"), size: 16, - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { diff --git a/src/render/gpu_image.rs b/src/render/gpu_image.rs index 1c66888..cdb0200 100644 --- a/src/render/gpu_image.rs +++ b/src/render/gpu_image.rs @@ -168,10 +168,13 @@ fn srgb_u8_to_linear_rgba(c: [u8; 4]) -> [f32; 4] { ] } -/// Uniform block for the image shader. Field order keeps `repr(C)` offsets -/// std140-aligned: mat4 @0, vec4 @64, vec2 @80, then scalars f32 @88/92/96/100, -/// u32 @104/108/112, explicit pad @116, then vec4 @128 (16-aligned); total 144. -/// Matches `Params` in `image.wgsl` (where WGSL auto-pads the vec4 to 128). +/// Params block for the image shader, bound as a read-only storage buffer like +/// every rsplot params block (see `CurveParams` in `gpu_curve.rs` for why). +/// Field order keeps `repr(C)` offsets std140-aligned: mat4 @0, vec4 @64, vec2 +/// @80, then scalars f32 @88/92/96/100, u32 @104/108/112, explicit pad @116, +/// then vec4 @128 (16-aligned); total 144 — the same under the storage rules, +/// since there are no arrays or nested structs. Matches `Params` in +/// `image.wgsl` (where WGSL auto-pads the vec4 to 128). #[repr(C)] #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] struct ImageParams { @@ -202,10 +205,11 @@ struct ImageParams { nan_color: [f32; 4], } -/// Uniform block for the RGBA (direct) image shader: just the transform, rect, -/// axis-scale flags, and global alpha — no colormap fields. `repr(C)` offsets -/// stay std140-aligned: mat4 @0, vec4 @64, vec2 @80, f32 @88; padded to 96. -/// Matches `Params` in `image_rgba.wgsl`. +/// Params block for the RGBA (direct) image shader (read-only storage, as +/// [`ImageParams`]): just the transform, rect, axis-scale flags, and global +/// alpha — no colormap fields. `repr(C)` offsets stay std140-aligned: mat4 @0, +/// vec4 @64, vec2 @80, f32 @88; padded to 96. Matches `Params` in +/// `image_rgba.wgsl`. #[repr(C)] #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] struct ImageRgbaParams { @@ -374,12 +378,12 @@ impl ImagePipeline { device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("rsplot image bgl"), entries: &[ - // 0: params uniform (used by both stages) + // 0: params block, read-only storage (used by both stages) wgpu::BindGroupLayoutEntry { binding: 0, visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, + ty: wgpu::BufferBindingType::Storage { read_only: true }, has_dynamic_offset: false, min_binding_size: NonZeroU64::new( std::mem::size_of::() as u64 @@ -470,7 +474,7 @@ impl ImagePipeline { cache: None, }); - // RGBA pipeline: its own minimal layout (uniform + RGBA texture + + // RGBA pipeline: its own minimal layout (params + RGBA texture + // sampler, no LUT). The texture is declared filterable and the sampler // slot is filtering so the bind group can choose the nearest or linear // sampler per image (silx `interpolation`). `Rgba8UnormSrgb` is a @@ -483,7 +487,7 @@ impl ImagePipeline { binding: 0, visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, + ty: wgpu::BufferBindingType::Storage { read_only: true }, has_dynamic_offset: false, min_binding_size: NonZeroU64::new( std::mem::size_of::() as u64, @@ -655,7 +659,7 @@ struct ImageTile { } /// What a [`GpuImage`] renders: a colormapped scalar field (carrying the -/// resolved normalization for the per-frame uniform) or a direct RGBA image +/// resolved normalization for the per-frame params) or a direct RGBA image /// (no colormap fields — they would have no meaning). #[derive(Clone, Copy)] enum GpuImageKind { @@ -830,7 +834,7 @@ impl GpuImage { let params = device.create_buffer(&wgpu::BufferDescriptor { label: Some("rsplot image tile params"), size: std::mem::size_of::() as u64, - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { @@ -941,7 +945,7 @@ impl GpuImage { let params = device.create_buffer(&wgpu::BufferDescriptor { label: Some("rsplot image rgba tile params"), size: std::mem::size_of::() as u64, - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { @@ -985,7 +989,7 @@ impl GpuImage { kind, alpha: image.alpha, }; - // Seed each tile's uniform; the per-frame transform overwrites `ortho`. + // Seed each tile's params; the per-frame transform overwrites `ortho`. gpu.write_uniforms(queue, IDENTITY, [0.0, 0.0]); gpu } diff --git a/src/render/gpu_scene3d.rs b/src/render/gpu_scene3d.rs index fca0c37..834b00e 100644 --- a/src/render/gpu_scene3d.rs +++ b/src/render/gpu_scene3d.rs @@ -264,9 +264,12 @@ pub struct Scene3dTexturedMesh { pub interpolation: ImageInterpolation, } -/// Uniform block for `scene3d.wgsl` **and** `scene3d_image.wgsl` (the image +/// Params block for `scene3d.wgsl` **and** `scene3d_image.wgsl` (the image /// pipeline binds the same buffer at group 0): the column-major, clip-corrected -/// MVP plus the linear-fog datum. +/// MVP plus the linear-fog datum. Bound as a read-only storage buffer like every +/// rsplot params block (see `CurveParams` in `gpu_curve.rs` for why); the three +/// scene blocks hold only mat4/vec4/vec2, so their layout is the same under the +/// uniform and storage rules. #[repr(C)] #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] struct Scene3dParams { @@ -281,9 +284,10 @@ struct Scene3dParams { view_row_z: [f32; 4], } -/// Uniform block for `scene3d_mesh.wgsl`: the clip MVP, the camera-space -/// normal transform (the view matrix, column-major, no depth correction), the -/// fog datum, and the Phong shininess. +/// Params block for `scene3d_mesh.wgsl` (read-only storage, as +/// [`Scene3dParams`]): the clip MVP, the camera-space normal transform (the view +/// matrix, column-major, no depth correction), the fog datum, and the Phong +/// shininess. #[repr(C)] #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] struct Scene3dMeshParams { @@ -296,9 +300,10 @@ struct Scene3dMeshParams { light: [f32; 4], } -/// Uniform block for `scene3d_points.wgsl`: the MVP, the fog datum, plus the -/// offscreen viewport pixel size (the sprite-corner offset is computed in -/// pixels then converted to NDC, so the shader needs the viewport extent). +/// Params block for `scene3d_points.wgsl` (read-only storage, as +/// [`Scene3dParams`]): the MVP, the fog datum, plus the offscreen viewport pixel +/// size (the sprite-corner offset is computed in pixels then converted to NDC, +/// so the shader needs the viewport extent). #[repr(C)] #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] struct Scene3dPointParams { @@ -692,17 +697,17 @@ struct Scene3dPipeline { /// egui's surface format; the offscreen color target uses it too so colors /// round-trip through the blit without an extra color-space conversion. target_format: wgpu::TextureFormat, - /// `group(0)` layout for the MVP uniform (vertex stage). + /// `group(0)` layout for the scene params block (MVP + fog). scene_bgl: wgpu::BindGroupLayout, /// Depth-tested `LineList` pipeline. line_pipeline: wgpu::RenderPipeline, /// Depth-tested `TriangleList` pipeline (no face culling). tri_pipeline: wgpu::RenderPipeline, - /// `group(0)` layout for the point-sprite uniform (MVP + viewport, vertex stage). + /// `group(0)` layout for the point-sprite params block (MVP + fog + viewport). point_bgl: wgpu::BindGroupLayout, /// Depth-tested, alpha-blended billboarded point-sprite pipeline. point_pipeline: wgpu::RenderPipeline, - /// `group(0)` layout for the mesh uniform (MVP + normal matrix, vertex stage). + /// `group(0)` layout for the mesh params block (MVP + normal matrix + fog + light). mesh_bgl: wgpu::BindGroupLayout, /// Depth-tested, headlight-shaded `TriangleList` mesh pipeline (no culling). mesh_pipeline: wgpu::RenderPipeline, @@ -749,11 +754,11 @@ impl Scene3dPipeline { label: Some("rsplot scene3d scene bgl"), entries: &[wgpu::BindGroupLayoutEntry { binding: 0, - // The fragment stage reads the fog uniform (silx applies fog + // The fragment stage reads the fog params (silx applies fog // per-fragment, viewport.py RenderContext scene_post). visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, + ty: wgpu::BufferBindingType::Storage { read_only: true }, has_dynamic_offset: false, min_binding_size: NonZeroU64::new(std::mem::size_of::() as u64), }, @@ -831,17 +836,17 @@ impl Scene3dPipeline { "rsplot scene3d triangles", ); - // Point sprites: their own uniform (MVP + fog + viewport) and an + // Point sprites: their own params block (MVP + fog + viewport) and an // instanced billboard pipeline with premultiplied-alpha blending so the // antialiased marker edges composite over the opaque scene behind them. let point_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("rsplot scene3d point bgl"), entries: &[wgpu::BindGroupLayoutEntry { binding: 0, - // Fragment stage reads the fog uniform. + // Fragment stage reads the fog params. visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, + ty: wgpu::BufferBindingType::Storage { read_only: true }, has_dynamic_offset: false, min_binding_size: NonZeroU64::new( std::mem::size_of::() as u64 @@ -900,17 +905,17 @@ impl Scene3dPipeline { cache: None, }); - // Shaded meshes: their own uniform (MVP + normal matrix + fog + + // Shaded meshes: their own params block (MVP + normal matrix + fog + // shininess) and a depth-tested, premultiplied-alpha-blended, double-sided // triangle pipeline with headlight lighting in the fragment shader. let mesh_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("rsplot scene3d mesh bgl"), entries: &[wgpu::BindGroupLayoutEntry { binding: 0, - // Fragment stage reads the fog + shininess uniforms. + // Fragment stage reads the fog + shininess params. visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, + ty: wgpu::BufferBindingType::Storage { read_only: true }, has_dynamic_offset: false, min_binding_size: NonZeroU64::new( std::mem::size_of::() as u64 @@ -976,7 +981,7 @@ impl Scene3dPipeline { cache: None, }); - // Textured image quads: group 0 reuses the scene MVP uniform; group 1 is + // Textured image quads: group 0 reuses the scene params block; group 1 is // the per-image texture + sampler. Depth-tested, premultiplied-alpha // blended (opaque images write fully; RGBA images composite). let image_tex_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { @@ -1162,10 +1167,11 @@ struct Scene3dImageGpu { vertex_count: u32, } -/// Per-scene GPU data: vertex buffers, the MVP uniform, and the offscreen +/// Per-scene GPU data: vertex buffers, the params blocks, and the offscreen /// color+depth render target (recreated on size change). struct Scene3dGpu { - /// MVP uniform, written each frame in [`Scene3dResources::prepare_scene`]. + /// Scene params (MVP + fog), written each frame in + /// [`Scene3dResources::prepare_scene`]. params_buf: wgpu::Buffer, /// `group(0)` bind group over `params_buf` for the scene pipelines. scene_bind_group: wgpu::BindGroup, @@ -1175,14 +1181,14 @@ struct Scene3dGpu { /// Triangle vertices; `None` while empty (skip the draw). tri_vbuf: Option, tri_count: u32, - /// Point-sprite uniform (MVP + viewport), written each frame. + /// Point-sprite params (MVP + fog + viewport), written each frame. point_params_buf: wgpu::Buffer, /// `group(0)` bind group over `point_params_buf` for the point pipeline. point_bind_group: wgpu::BindGroup, /// Per-instance scatter points; `None` while empty (skip the draw). point_vbuf: Option, point_count: u32, - /// Mesh uniform (MVP + normal matrix), written each frame. + /// Mesh params (MVP + normal matrix + fog + light), written each frame. mesh_params_buf: wgpu::Buffer, /// `group(0)` bind group over `mesh_params_buf` for the mesh pipeline. mesh_bind_group: wgpu::BindGroup, @@ -1206,7 +1212,7 @@ impl Scene3dGpu { let params_buf = device.create_buffer(&wgpu::BufferDescriptor { label: Some("rsplot scene3d params"), size: std::mem::size_of::() as u64, - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); let scene_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { @@ -1220,7 +1226,7 @@ impl Scene3dGpu { let point_params_buf = device.create_buffer(&wgpu::BufferDescriptor { label: Some("rsplot scene3d point params"), size: std::mem::size_of::() as u64, - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); let point_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { @@ -1234,7 +1240,7 @@ impl Scene3dGpu { let mesh_params_buf = device.create_buffer(&wgpu::BufferDescriptor { label: Some("rsplot scene3d mesh params"), size: std::mem::size_of::() as u64, - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); let mesh_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { diff --git a/src/render/shaders/clear.wgsl b/src/render/shaders/clear.wgsl index 76eb95f..aa663f5 100644 --- a/src/render/shaders/clear.wgsl +++ b/src/render/shaders/clear.wgsl @@ -2,15 +2,16 @@ // // egui-wgpu already calls set_viewport with the paint callback's rect, so a // full-screen triangle covering all of NDC paints only the viewport (= the data -// rect). The color comes from a group(0) binding(0) uniform (linear color space, -// premultiplied). +// rect). The color comes from the group(0) binding(0) params block (premultiplied +// RGBA in the target's encoding), bound as a read-only storage buffer like every +// rsplot params block (see `CurveParams` in gpu_curve.rs). struct Params { color: vec4, }; @group(0) @binding(0) -var params: Params; +var params: Params; @vertex fn vs_main(@builtin(vertex_index) vid: u32) -> @builtin(position) vec4 { diff --git a/src/render/shaders/image.wgsl b/src/render/shaders/image.wgsl index a3e179a..9911a99 100644 --- a/src/render/shaders/image.wgsl +++ b/src/render/shaders/image.wgsl @@ -37,7 +37,7 @@ fn apply_scale(p: vec2) -> vec2 { ); } -@group(0) @binding(0) var params: Params; +@group(0) @binding(0) var params: Params; @group(0) @binding(1) var data_tex: texture_2d; // R32Float, unfilterable @group(0) @binding(2) var data_samp: sampler; // unused: data is fetched // via textureLoad below, diff --git a/src/render/shaders/image_rgba.wgsl b/src/render/shaders/image_rgba.wgsl index ab42d69..4bddf79 100644 --- a/src/render/shaders/image_rgba.wgsl +++ b/src/render/shaders/image_rgba.wgsl @@ -25,7 +25,7 @@ fn apply_scale(p: vec2) -> vec2 { ); } -@group(0) @binding(0) var params: Params; +@group(0) @binding(0) var params: Params; @group(0) @binding(1) var rgba_tex: texture_2d; // Rgba8UnormSrgb @group(0) @binding(2) var rgba_samp: sampler; // non-filtering (nearest) diff --git a/src/render/shaders/scene3d.wgsl b/src/render/shaders/scene3d.wgsl index 877cc95..5cd4835 100644 --- a/src/render/shaders/scene3d.wgsl +++ b/src/render/shaders/scene3d.wgsl @@ -4,12 +4,14 @@ // // The MVP is `camera.matrix() × model`, already transposed to column-major and // clip-corrected for wgpu's z∈[0,1] depth range (see `Mat4::to_gpu_clip_cols`); -// it arrives in the group(0) binding(0) uniform. Per-vertex position and color -// (linear, premultiplied) come from the vertex buffer. +// it arrives in the group(0) binding(0) params block (a read-only storage +// buffer, like every rsplot params block — see `CurveParams` in gpu_curve.rs). +// Per-vertex position and color (linear, premultiplied) come from the vertex +// buffer. // -// NOTE: `Params` is the shared group(0) uniform of the line/triangle pipelines -// AND the textured-image pipeline (`scene3d_image.wgsl`) — both bind the same -// buffer, so the two WGSL structs must stay layout-identical. +// NOTE: `Params` is the shared group(0) params block of the line/triangle +// pipelines AND the textured-image pipeline (`scene3d_image.wgsl`) — both bind +// the same buffer, so the two WGSL structs must stay layout-identical. struct Params { mvp: mat4x4, @@ -24,7 +26,7 @@ struct Params { }; @group(0) @binding(0) -var params: Params; +var params: Params; struct VsOut { @builtin(position) clip: vec4, diff --git a/src/render/shaders/scene3d_image.wgsl b/src/render/shaders/scene3d_image.wgsl index b4b03a4..dcba819 100644 --- a/src/render/shaders/scene3d_image.wgsl +++ b/src/render/shaders/scene3d_image.wgsl @@ -22,7 +22,7 @@ struct Params { view_row_z: vec4, }; -@group(0) @binding(0) var params: Params; +@group(0) @binding(0) var params: Params; @group(1) @binding(0) var tex: texture_2d; @group(1) @binding(1) var samp: sampler; diff --git a/src/render/shaders/scene3d_mesh.wgsl b/src/render/shaders/scene3d_mesh.wgsl index e68d3c1..fa44fe9 100644 --- a/src/render/shaders/scene3d_mesh.wgsl +++ b/src/render/shaders/scene3d_mesh.wgsl @@ -26,7 +26,7 @@ struct MeshParams { light: vec4, }; -@group(0) @binding(0) var params: MeshParams; +@group(0) @binding(0) var params: MeshParams; // silx viewport DirectionalLight defaults, camera frame (viewport.py:227-233 // direction (0,0,-1), ambient 0.3, diffuse 0.7; specular defaults to (1,1,1)). diff --git a/src/render/shaders/scene3d_points.wgsl b/src/render/shaders/scene3d_points.wgsl index b24aa13..08f9f48 100644 --- a/src/render/shaders/scene3d_points.wgsl +++ b/src/render/shaders/scene3d_points.wgsl @@ -23,7 +23,7 @@ struct PointParams { _pad: vec2, }; -@group(0) @binding(0) var params: PointParams; +@group(0) @binding(0) var params: PointParams; struct VsOut { @builtin(position) clip: vec4, diff --git a/src/render/shaders/volume_raycaster.wgsl b/src/render/shaders/volume_raycaster.wgsl index 1de4b77..711e285 100644 --- a/src/render/shaders/volume_raycaster.wgsl +++ b/src/render/shaders/volume_raycaster.wgsl @@ -20,7 +20,7 @@ struct Uniforms { params: vec4, }; -@group(0) @binding(0) var u: Uniforms; +@group(0) @binding(0) var u: Uniforms; @group(0) @binding(1) var vol_tex: texture_3d; @group(0) @binding(2) var vol_samp: sampler; diff --git a/src/render/volume_raycaster.rs b/src/render/volume_raycaster.rs index 17f0b67..141a6a5 100644 --- a/src/render/volume_raycaster.rs +++ b/src/render/volume_raycaster.rs @@ -21,7 +21,10 @@ use half::f16; /// like `Scene3dId`). pub type VolumeId = u64; -/// Per-frame camera + rendering uniforms. Column-major matrix for WGSL. +/// Per-frame camera + rendering params, bound as a read-only storage buffer like +/// every rsplot params block (see `CurveParams` in `gpu_curve.rs` for why; the +/// block holds only a mat4 and vec4s, so the layout is the same as the uniform +/// one). Column-major matrix for WGSL. #[repr(C)] #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] struct VolumeUniforms { @@ -110,7 +113,7 @@ impl VolumePipeline { binding: 0, visibility: wgpu::ShaderStages::FRAGMENT, ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, + ty: wgpu::BufferBindingType::Storage { read_only: true }, has_dynamic_offset: false, min_binding_size: wgpu::BufferSize::new( std::mem::size_of::() as u64, @@ -262,10 +265,10 @@ impl VolumeGpu { (self.vol_min, self.vol_max) = volume_bounds(depth, height, width); } - /// Build a bind group for one frame: a fresh uniform buffer holding this + /// Build a bind group for one frame: a fresh params buffer holding this /// frame's camera and this view's box, plus the view's texture and the shared /// sampler. `None` until a volume has been uploaded. Called once per paint in - /// `prepare`, so each callback owns its uniforms rather than sharing one + /// `prepare`, so each callback owns its params rather than sharing one /// per-id buffer. fn frame_bind_group( &self, @@ -284,7 +287,7 @@ impl VolumeGpu { let uniform_buf = device.create_buffer(&wgpu::BufferDescriptor { label: Some("rsplot volume raycaster uniforms"), size: std::mem::size_of::() as u64, - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); queue.write_buffer(&uniform_buf, 0, bytemuck::bytes_of(&u)); @@ -292,7 +295,7 @@ impl VolumeGpu { dimension: Some(wgpu::TextureViewDimension::D3), ..Default::default() }); - // The bind group holds strong refs to the uniform buffer and view, so + // The bind group holds strong refs to the params buffer and view, so // both live as long as the callback keeps the bind group. Some(device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("rsplot volume raycaster bind group"), @@ -423,7 +426,7 @@ pub fn set_volume_raycaster( } /// egui paint callback. `prepare` builds this frame's own bind group (fresh -/// uniform buffer for the camera + the view's texture) and stashes it in `ready`; +/// params buffer for the camera + the view's texture) and stashes it in `ready`; /// `paint` ray-marches with it. Because the bind group is per-callback rather /// than a shared per-id buffer, two callbacks with the same [`VolumeId`] in one /// frame each render with their own camera. @@ -475,7 +478,7 @@ impl egui_wgpu::CallbackTrait for VolumeCallback { /// Register the raycaster paint callback for `frame` over `rect`. A no-op on /// screen until [`set_volume_raycaster`] has uploaded a volume for `frame.id`. /// -/// Each callback builds its own uniform buffer + bind group in `prepare` (keyed +/// Each callback builds its own params buffer + bind group in `prepare` (keyed /// off `frame`, not shared per id), so painting the same [`VolumeId`] twice in /// one frame — two panels of the same volume from different cameras — renders /// each with its own viewpoint. From 4739141f1eed6fc4a606d757b62a2debde60ac7b Mon Sep 17 00:00:00 2001 From: Sang Woo Kim Date: Sat, 17 Oct 2026 21:48:45 +0900 Subject: [PATCH 4/6] fix(render): upload image texels in the render target's encoding The colormap LUT and the direct-RGBA tiles were Rgba8UnormSrgb and the nan colour was linearised, so on egui-wgpu's non-sRGB swapchain the image shaders wrote linear values and every image came out darker than its colormap (viridis mid teal (33, 145, 140) read back as (4, 71, 66)). color_texel_format / nan_color_for_target now pick the encoding from the target, the same rule gpu_color applies to curve colours. --- CHANGELOG.md | 7 ++ src/render/gpu_image.rs | 102 +++++++++++++++++++----- src/render/shaders/image.wgsl | 15 ++-- src/render/shaders/image_rgba.wgsl | 8 +- tests/image_target_encoding_render.rs | 108 ++++++++++++++++++++++++++ 5 files changed, 210 insertions(+), 30 deletions(-) create mode 100644 tests/image_target_encoding_render.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b23db5..49c8306 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,13 @@ library), **rsdm** (a PyDM-style EPICS display layer built on rsplot), and linearised, which on the non-sRGB target drew every colour darker than its `Color32` and than egui's own legend swatches (`#4f9bff` showed as `(20, 84, 255)`); PNG export was affected the same way. +- **rsplot**: the 2D image path uploads its colour texels in the render + target's encoding too: the colormap LUT and direct-RGBA tiles are + `Rgba8Unorm` for a non-sRGB target and `Rgba8UnormSrgb` for an sRGB one, + and the colormap's `nan_color` follows the same rule. They were always + sRGB-decoded to linear, which on egui-wgpu's non-sRGB swapchain drew every + colormapped image and RGBA image darker than its colormap (viridis' mid + teal `(33, 145, 140)` showed as `(4, 71, 66)`). ## [0.6.0] - 2026-10-16 diff --git a/src/render/gpu_image.rs b/src/render/gpu_image.rs index cdb0200..01552a6 100644 --- a/src/render/gpu_image.rs +++ b/src/render/gpu_image.rs @@ -146,11 +146,39 @@ const IDENTITY: [[f32; 4]; 4] = [ [0.0, 0.0, 0.0, 1.0], ]; +/// The texture format for 8-bit sRGB colour texels — the colormap LUT and the +/// direct-RGBA tiles — given the render target's encoding: the texture analogue +/// of [`gpu_color`](super::gpu_curve::gpu_color). `Rgba8UnormSrgb` decodes the +/// bytes to linear for an sRGB target (the hardware re-encodes on write); +/// `Rgba8Unorm` passes them through for a non-sRGB target such as egui-wgpu's +/// swapchain, which holds gamma-encoded values. Either way the sampled texel is +/// in the target's encoding and the shader needs no conversion. Both formats +/// are hardware-filterable. +fn color_texel_format(srgb_target: bool) -> wgpu::TextureFormat { + if srgb_target { + wgpu::TextureFormat::Rgba8UnormSrgb + } else { + wgpu::TextureFormat::Rgba8Unorm + } +} + +/// Encode the colormap's straight-alpha sRGB `nan_color` bytes for the render +/// target, in the same encoding the LUT texels sample as (see +/// [`color_texel_format`]) so the shader composites the two identically: +/// linearised RGB for an sRGB target, the bytes as-is for a non-sRGB one. Alpha +/// is linear (not gamma-encoded) in both. +fn nan_color_for_target(c: [u8; 4], srgb_target: bool) -> [f32; 4] { + if srgb_target { + srgb_u8_to_linear_rgba(c) + } else { + c.map(|b| f32::from(b) / 255.0) + } +} + /// Convert an sRGB-encoded `[u8; 4]` color (the colormap's `nan_color`) to -/// linear-space `[f32; 4]`. The scalar-image LUT is an `Rgba8UnormSrgb` texture, -/// so sampling it returns linear RGB; converting the nan_color the same way lets -/// the shader composite it identically to the colormapped path. The RGB channels -/// use the standard sRGB transfer function; alpha is linear (not gamma-encoded). +/// linear-space `[f32; 4]` — the sRGB-target branch of +/// [`nan_color_for_target`]. The RGB channels use the standard sRGB transfer +/// function; alpha is linear (not gamma-encoded). fn srgb_u8_to_linear_rgba(c: [u8; 4]) -> [f32; 4] { let to_linear = |b: u8| { let s = b as f32 / 255.0; @@ -353,10 +381,13 @@ pub struct ImagePipeline { /// sampling must snap to the nearest texel, not interpolate between entries. lut_sampler: wgpu::Sampler, /// LINEAR sampler for the *direct RGBA* image path's bilinear interpolation - /// (`Rgba8UnormSrgb` is hardware-filterable). Kept separate from + /// (the 8-bit RGBA formats are hardware-filterable). Kept separate from /// [`Self::lut_sampler`] so making the LUT lookup NEAREST does not turn RGBA /// linear interpolation into nearest. linear_sampler: wgpu::Sampler, + /// Whether the render target is an sRGB format. Selects the colour texel + /// format and the nan-colour encoding (see [`color_texel_format`]). + srgb_target: bool, /// A 1×1 `R32Float` texture of value `1.0`, bound at scalar binding 5 for /// images without a per-pixel alpha map so the bind group stays valid; the /// shader never samples it (its `has_alpha_map` flag is 0). @@ -477,8 +508,8 @@ impl ImagePipeline { // RGBA pipeline: its own minimal layout (params + RGBA texture + // sampler, no LUT). The texture is declared filterable and the sampler // slot is filtering so the bind group can choose the nearest or linear - // sampler per image (silx `interpolation`). `Rgba8UnormSrgb` is a - // filterable format, so this needs no extra wgpu feature. + // sampler per image (silx `interpolation`). Both 8-bit RGBA formats are + // filterable, so this needs no extra wgpu feature. let rgba_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("rsplot image rgba bgl"), @@ -599,6 +630,7 @@ impl ImagePipeline { data_sampler, lut_sampler, linear_sampler, + srgb_target: target_format.is_srgb(), dummy_alpha_view, } } @@ -644,7 +676,8 @@ fn extract_subgrid( } /// One tile of a (possibly split) image: its own texture (`R32Float` for a -/// scalar image, `Rgba8UnormSrgb` for an RGBA one), params, bind group, +/// scalar image, 8-bit RGBA in the target's encoding — [`color_texel_format`] +/// — for an RGBA one), params, bind group, /// data-space rect, and pixel bounds within the full image (the bounds route /// [`GpuImage::update_region`] writes to the tiles they overlap). struct ImageTile { @@ -675,8 +708,8 @@ enum GpuImageKind { interp: u32, /// 1 if a per-pixel alpha map is bound at binding 5, else 0. has_alpha_map: u32, - /// Linear RGBA for non-finite samples (silx `Colormap.nan_color`), - /// pre-converted from the colormap's sRGB bytes. + /// RGBA for non-finite samples (silx `Colormap.nan_color`) in the render + /// target's encoding, like the LUT texels (see [`nan_color_for_target`]). nan_color: [f32; 4], }, Rgba, @@ -695,11 +728,12 @@ pub struct GpuImage { impl GpuImage { /// Upload `image` and build per-tile bind groups. A scalar image uploads - /// `R32Float` tiles plus a shared 256x1 sRGB LUT and uses the colormap - /// pipeline; an RGBA image uploads `Rgba8UnormSrgb` tiles and uses the - /// direct pipeline (no LUT). Either way the image is split into tiles no - /// larger than the device's `max_texture_dimension_2d`, so it can exceed the - /// single-texture limit. + /// `R32Float` tiles plus a shared 256x1 colormap LUT and uses the colormap + /// pipeline; an RGBA image uploads RGBA tiles and uses the direct pipeline + /// (no LUT). The LUT and the RGBA tiles take the 8-bit format + /// [`color_texel_format`] picks for the render target's encoding. Either way + /// the image is split into tiles no larger than the device's + /// `max_texture_dimension_2d`, so it can exceed the single-texture limit. pub fn new( device: &wgpu::Device, queue: &wgpu::Queue, @@ -734,7 +768,7 @@ impl GpuImage { mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Rgba8UnormSrgb, + format: color_texel_format(pipeline.srgb_target), usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, view_formats: &[], }); @@ -892,14 +926,15 @@ impl GpuImage { norm: colormap.normalization.code(), interp: image.interpolation.code(), has_alpha_map: u32::from(image.alpha_map.is_some()), - nan_color: srgb_u8_to_linear_rgba(colormap.nan_color), + nan_color: nan_color_for_target(colormap.nan_color, pipeline.srgb_target), }, ) } ImagePixels::Rgba { data } => { - // silx interpolation on a direct RGBA image: `Rgba8UnormSrgb` is - // filterable, so the hardware sampler does it (no manual bilinear - // and no extra wgpu feature). Nearest reuses the data sampler. + // silx interpolation on a direct RGBA image: the 8-bit RGBA formats + // are filterable, so the hardware sampler does it (no manual + // bilinear and no extra wgpu feature). Nearest reuses the data + // sampler. let rgba_sampler = match image.interpolation { InterpolationMode::Nearest => &pipeline.data_sampler, InterpolationMode::Linear => &pipeline.linear_sampler, @@ -918,7 +953,7 @@ impl GpuImage { mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Rgba8UnormSrgb, + format: color_texel_format(pipeline.srgb_target), usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, view_formats: &[], @@ -1166,6 +1201,31 @@ mod tests { assert!((mid[3] - 128.0 / 255.0).abs() < 1e-6, "alpha is linear"); } + #[test] + fn color_texel_format_follows_the_target_encoding() { + // A non-sRGB target (egui-wgpu's swapchain) must sample the sRGB bytes + // as-is; an sRGB target must have them decoded to linear. + assert_eq!(color_texel_format(false), wgpu::TextureFormat::Rgba8Unorm); + assert_eq!( + color_texel_format(true), + wgpu::TextureFormat::Rgba8UnormSrgb + ); + } + + #[test] + fn nan_color_for_target_matches_the_lut_encoding() { + let c = [188, 64, 255, 128]; + // Non-sRGB target: the bytes pass through, like a texel of an + // `Rgba8Unorm` LUT. + let gamma = nan_color_for_target(c, false); + let expected = c.map(|b| f32::from(b) / 255.0); + assert_eq!(gamma, expected); + // sRGB target: linearised RGB, like a texel of an `Rgba8UnormSrgb` LUT; + // alpha linear in both. + assert_eq!(nan_color_for_target(c, true), srgb_u8_to_linear_rgba(c)); + assert_eq!(gamma[3], nan_color_for_target(c, true)[3]); + } + #[test] fn oversize_image_splits_into_grid_covering_every_pixel() { // 5 × 3 image with max_dim 2: columns {0..2, 2..4, 4..5}, rows diff --git a/src/render/shaders/image.wgsl b/src/render/shaders/image.wgsl index 9911a99..e1a4d7c 100644 --- a/src/render/shaders/image.wgsl +++ b/src/render/shaders/image.wgsl @@ -6,7 +6,9 @@ // texture (nearest or bilinear-on-data, mirroring silx interpolation), // maps it to a [0, 1] LUT coordinate under the colormap normalization // (linear / log10 / sqrt / gamma / arcsinh — mirrors silx GLPlotImage), -// and looks up the color in a 256x1 sRGB LUT texture (linear). +// and looks up the color in a 256x1 LUT texture. The LUT's format puts the +// sampled texel in the render target's encoding (gpu_image.rs +// `color_texel_format`), so the color is written out unconverted. struct Params { ortho: mat4x4, @@ -21,7 +23,8 @@ struct Params { has_alpha_map: u32, // 1 if a per-pixel alpha map is bound at binding 5 // WGSL pads the vec4 below to the 16-aligned offset 128 (the three trailing // u32s above end at 116); the Rust ImageParams adds explicit padding to match. - nan_color: vec4, // linear RGBA for non-finite samples (silx nan_color) + nan_color: vec4, // RGBA for non-finite samples (silx nan_color), + // in the target's encoding like the LUT texels }; // 1 / ln(10), to turn the natural log into log10. @@ -42,7 +45,7 @@ fn apply_scale(p: vec2) -> vec2 { @group(0) @binding(2) var data_samp: sampler; // unused: data is fetched // via textureLoad below, // kept for layout parity -@group(0) @binding(3) var lut_tex: texture_2d; // 256x1 sRGB +@group(0) @binding(3) var lut_tex: texture_2d; // 256x1, target encoding @group(0) @binding(4) var lut_samp: sampler; // filtering (linear) @group(0) @binding(5) var alpha_tex: texture_2d; // R32Float per-pixel alpha, // unfilterable; 1x1 dummy @@ -185,9 +188,9 @@ fn fs_main(in: VsOut) -> @location(0) vec4 { // Only NaN gets the colormap's nan_color (silx GLPlotImage.py:202-206 / // _colormap.pyx:362-376: nancolor solely for isnan). +/-inf survive the // normalization clamp and land on the LUT ends (+inf -> top, -inf -> bottom). - // NaN is the only value that fails self-equality. The nan_color is - // pre-converted to linear RGBA on the CPU so it composites identically to the - // sRGB LUT colors, and its alpha is scaled by the image's global alpha. + // NaN is the only value that fails self-equality. The nan_color is encoded + // on the CPU the same way the LUT texels sample (the target's encoding) so it + // composites identically, and its alpha is scaled by the image's global alpha. if (v != v) { return vec4(params.nan_color.rgb, params.nan_color.a * a); } diff --git a/src/render/shaders/image_rgba.wgsl b/src/render/shaders/image_rgba.wgsl index 4bddf79..81fac23 100644 --- a/src/render/shaders/image_rgba.wgsl +++ b/src/render/shaders/image_rgba.wgsl @@ -1,8 +1,10 @@ // Direct RGBA image (silx addImage with an RGBA array): no colormap / LUT. // // Same quad as image.wgsl (the image's data-space rect mapped to NDC by the -// ortho matrix), but the fragment samples a sRGB RGBA texture directly and -// multiplies the global alpha. Used when the image is RGBA rather than a scalar +// ortho matrix), but the fragment samples the RGBA texture directly and +// multiplies the global alpha. The texture's format puts the sampled texel in +// the render target's encoding (gpu_image.rs `color_texel_format`), so no +// conversion happens here. Used when the image is RGBA rather than a scalar // field (doc/design.md §5·§6). struct Params { @@ -26,7 +28,7 @@ fn apply_scale(p: vec2) -> vec2 { } @group(0) @binding(0) var params: Params; -@group(0) @binding(1) var rgba_tex: texture_2d; // Rgba8UnormSrgb +@group(0) @binding(1) var rgba_tex: texture_2d; // 8-bit RGBA, target encoding @group(0) @binding(2) var rgba_samp: sampler; // non-filtering (nearest) struct VsOut { diff --git a/tests/image_target_encoding_render.rs b/tests/image_target_encoding_render.rs new file mode 100644 index 0000000..99eb4cb --- /dev/null +++ b/tests/image_target_encoding_render.rs @@ -0,0 +1,108 @@ +//! Headless wgpu readback proving the 2D image path writes its colours in the +//! render target's encoding: a colormapped texel and a direct-RGBA texel must +//! come back as the colormap's / the image's own sRGB bytes. +//! +//! `egui_kittest`'s renderer targets a non-sRGB `Rgba8Unorm` texture — the same +//! kind of target egui-wgpu's swapchain is — and reads the stored bytes back +//! verbatim. The colormap LUT and the RGBA tiles used to be `Rgba8UnormSrgb` +//! textures, so the shader wrote *linearised* values into that target and every +//! image came out darker than its colormap (viridis' mid teal `(33, 145, 140)` +//! read back as `(4, 71, 66)`). Now the texel formats follow the target +//! (`gpu_image::color_texel_format`), so the bytes round-trip exactly. +//! +//! Boundaries covered: the scalar (LUT) path and the direct-RGBA path, each +//! sampled over a block of pixels around the window centre, which the image +//! fills (view limits pinned to the image extent, no grid, no colorbar). Needs +//! a GPU (real or software). + +use std::cell::RefCell; +use std::rc::Rc; + +use egui_kittest::Harness; +use egui_kittest::wgpu::{WgpuTestRenderer, create_render_state, default_wgpu_setup}; +use rsplot::egui; +use rsplot::egui_wgpu::RendererOptions; +use rsplot::{Colormap, ImageData, Plot, PlotView, install, set_image}; + +const W: usize = 400; +const H: usize = 300; +const WD: u32 = 16; +const HD: u32 = 16; +/// Half-side of the sampled block around the window centre. +const BLOCK: usize = 4; + +/// Render `image` filling the data area and return the frame's RGBA bytes. +fn render(image: &ImageData) -> Vec { + let rs = create_render_state(default_wgpu_setup(), RendererOptions::default()); + install(&rs); + set_image(&rs, 0, image); + + let mut plot = Plot::new(0); + plot.limits = (0.0, f64::from(WD), 0.0, f64::from(HD)); + + let app = Rc::new(RefCell::new(plot)); + let app_ui = app.clone(); + let renderer = WgpuTestRenderer::from_render_state(rs); + let mut harness = Harness::builder() + .with_size(egui::vec2(W as f32, H as f32)) + .with_pixels_per_point(1.0) + .renderer(renderer) + .build_ui(move |ui| { + PlotView::new().show(ui, &mut app_ui.borrow_mut()); + }); + + harness.step(); + harness.step(); + let frame = harness.render().expect("headless wgpu render"); + assert_eq!((frame.width() as usize, frame.height() as usize), (W, H)); + frame.as_raw().to_vec() +} + +/// Every pixel of the `(2·BLOCK)²` block around the window centre must be +/// `expected` to within one 8-bit step per channel. +fn assert_centre_block_is(raw: &[u8], expected: [u8; 3], what: &str) { + let (cx, cy) = (W / 2, H / 2); + for y in cy - BLOCK..cy + BLOCK { + for x in cx - BLOCK..cx + BLOCK { + let i = (y * W + x) * 4; + let got = [raw[i], raw[i + 1], raw[i + 2]]; + let close = got.iter().zip(expected).all(|(&g, e)| g.abs_diff(e) <= 1); + assert!( + close, + "{what}: pixel ({x},{y}) is rgb{got:?}, expected rgb{expected:?} ± 1 \ + (the image's own sRGB bytes on the non-sRGB target)" + ); + } + } +} + +#[test] +fn colormapped_texel_reads_back_as_the_colormap_byte() { + // A uniform field at the colormap's midpoint: every texel is one LUT entry, + // a saturated teal whose linearised bytes would be far darker. + let colormap = Colormap::viridis(0.0, 1.0); + let expected = colormap.color_at(0.5); + assert!( + expected[0] > 20 && expected[1] > 100, + "viridis mid should be a mid-intensity teal; got {expected:?}" + ); + let image = ImageData::new(WD, HD, vec![0.5f32; (WD * HD) as usize], colormap); + + let raw = render(&image); + assert_centre_block_is( + &raw, + [expected[0], expected[1], expected[2]], + "scalar (LUT) image", + ); +} + +#[test] +fn rgba_texel_reads_back_as_its_own_bytes() { + // Three distinct mid-range channels: each would land on a different, darker + // byte if the tile were sRGB-decoded on the way to the non-sRGB target. + let texel = [128u8, 64, 32, 255]; + let image = ImageData::rgba(WD, HD, vec![texel; (WD * HD) as usize]); + + let raw = render(&image); + assert_centre_block_is(&raw, [texel[0], texel[1], texel[2]], "direct RGBA image"); +} From 873a1d78f8bba76a30c68209a51f7e1e621b9bbb Mon Sep 17 00:00:00 2001 From: Sang Woo Kim Date: Sat, 17 Oct 2026 21:56:51 +0900 Subject: [PATCH 5/6] fix(render): encode the 3D scene's linear colour for a non-sRGB target The scene shades in linear light and the blit copied it unconverted onto egui-wgpu's non-sRGB swapchain, so every 3D colour showed darker than its Color32. Scene3dPipeline now renders into the sRGB variant of the surface format and the blit encodes only for a non-sRGB surface; snapshot_scene reads back that sRGB target, so its bytes are gamma-encoded too. --- CHANGELOG.md | 8 ++ src/render/gpu_scene3d.rs | 85 +++++++++--- src/render/shaders/scene3d_blit.wgsl | 26 +++- tests/scene3d_mesh_render.rs | 19 ++- tests/scene3d_target_encoding_render.rs | 166 ++++++++++++++++++++++++ 5 files changed, 282 insertions(+), 22 deletions(-) create mode 100644 tests/scene3d_target_encoding_render.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 49c8306..952c3a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,14 @@ library), **rsdm** (a PyDM-style EPICS display layer built on rsplot), and sRGB-decoded to linear, which on egui-wgpu's non-sRGB swapchain drew every colormapped image and RGBA image darker than its colormap (viridis' mid teal `(33, 145, 140)` showed as `(4, 71, 66)`). +- **rsplot**: the 3D scene (`paint_scene3d`, `SceneWidget`, `ScalarFieldView`, + the volume cut plane and iso-surfaces) is rendered into the sRGB variant of + the surface format and the blit encodes the linear result to sRGB when the + surface is non-sRGB, so geometry, mesh shading, fog, image layers and the + background now show at their `Color32` values on egui-wgpu's swapchain + instead of darker. `snapshot_scene3d` / `SceneWidget::snapshot` read back + the sRGB offscreen target, so their PNGs carry sRGB-encoded bytes whatever + the surface format (they were as dark as the screen). ## [0.6.0] - 2026-10-16 diff --git a/src/render/gpu_scene3d.rs b/src/render/gpu_scene3d.rs index 834b00e..3838888 100644 --- a/src/render/gpu_scene3d.rs +++ b/src/render/gpu_scene3d.rs @@ -694,9 +694,12 @@ impl Scene3dGeometry { /// The shared pipelines + layouts for 3D rendering. Built once in /// [`Scene3dResources::new`]. struct Scene3dPipeline { - /// egui's surface format; the offscreen color target uses it too so colors - /// round-trip through the blit without an extra color-space conversion. - target_format: wgpu::TextureFormat, + /// Format of the offscreen colour target the scene is rendered into — the + /// sRGB variant of egui's surface format (see [`offscreen_format`]); the + /// blit samples it as linear and, for a non-sRGB surface, encodes it + /// ([`blit_entry_point`]). The surface format itself is baked into the + /// blit pipeline's colour target and not kept. + offscreen_format: wgpu::TextureFormat, /// `group(0)` layout for the scene params block (MVP + fog). scene_bgl: wgpu::BindGroupLayout, /// Depth-tested `LineList` pipeline. @@ -727,8 +730,33 @@ struct Scene3dPipeline { sampler: wgpu::Sampler, } +/// The offscreen colour format for a scene blitted onto `target_format`: its +/// sRGB variant when one exists. The scene pipelines write linear, premultiplied +/// colour — lighting, fog and blending all run in linear light — so an sRGB +/// attachment stores it gamma-encoded at full 8-bit precision and the blit reads +/// it back as linear; a format without an sRGB variant (float targets) stores +/// the linear value as is, which the blit reads back the same way. +fn offscreen_format(target_format: wgpu::TextureFormat) -> wgpu::TextureFormat { + target_format.add_srgb_suffix() +} + +/// The blit's fragment entry point for `target_format` (`scene3d_blit.wgsl`): +/// an sRGB surface encodes on write, so the linear sample passes through; a +/// non-sRGB surface — egui-wgpu's default swapchain, which egui's own shader +/// writes gamma-encoded — needs the blit to encode the linear sample to sRGB +/// itself, or the whole scene shows darker than its `Color32`s. The same split +/// as egui-wgpu's `fs_main_linear_framebuffer` / `fs_main_gamma_framebuffer`. +fn blit_entry_point(target_format: wgpu::TextureFormat) -> &'static str { + if target_format.is_srgb() { + "fs_main_linear_target" + } else { + "fs_main_gamma_target" + } +} + impl Scene3dPipeline { fn new(device: &wgpu::Device, target_format: wgpu::TextureFormat) -> Self { + let offscreen_format = offscreen_format(target_format); let scene_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("rsplot scene3d"), source: wgpu::ShaderSource::Wgsl(include_str!("shaders/scene3d.wgsl").into()), @@ -800,7 +828,7 @@ impl Scene3dPipeline { // translucent content (e.g. axis tick lines at 0.6 α, // `axes.py:114`) composites. Depth write stays on, as in silx. targets: &[Some(wgpu::ColorTargetState { - format: target_format, + format: offscreen_format, blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING), write_mask: wgpu::ColorWrites::ALL, })], @@ -879,7 +907,7 @@ impl Scene3dPipeline { module: &point_shader, entry_point: Some("fs_main"), targets: &[Some(wgpu::ColorTargetState { - format: target_format, + format: offscreen_format, blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING), write_mask: wgpu::ColorWrites::ALL, })], @@ -953,7 +981,7 @@ impl Scene3dPipeline { // -level for this, which `ScalarField3D::append_raw` already does); // opaque meshes (α=1) write fully. Depth write stays on, as in silx. targets: &[Some(wgpu::ColorTargetState { - format: target_format, + format: offscreen_format, blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING), write_mask: wgpu::ColorWrites::ALL, })], @@ -1027,7 +1055,7 @@ impl Scene3dPipeline { module: &image_shader, entry_point: Some("fs_main"), targets: &[Some(wgpu::ColorTargetState { - format: target_format, + format: offscreen_format, blend: Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING), write_mask: wgpu::ColorWrites::ALL, })], @@ -1112,7 +1140,9 @@ impl Scene3dPipeline { }, fragment: Some(wgpu::FragmentState { module: &blit_shader, - entry_point: Some("fs_main"), + // Encodes the linear offscreen sample for a non-sRGB surface, + // passes it through for an sRGB one (see `blit_entry_point`). + entry_point: Some(blit_entry_point(target_format)), // blend: None → replace; the scene (opaque background) occludes // whatever egui drew behind the widget rect. targets: &[Some(target_format.into())], @@ -1138,7 +1168,7 @@ impl Scene3dPipeline { }); Self { - target_format, + offscreen_format, scene_bgl, line_pipeline, tri_pipeline, @@ -1199,7 +1229,8 @@ struct Scene3dGpu { images: Vec, /// Pixel size of the current offscreen target (`[0, 0]` until first sized). size: [u32; 2], - /// Offscreen color view (target format); the blit samples this. + /// Offscreen color view (`Scene3dPipeline::offscreen_format`); the blit + /// samples this. color_view: Option, /// Offscreen depth view (`Depth32Float`), for depth testing. depth_view: Option, @@ -1333,7 +1364,7 @@ impl Scene3dGpu { mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D2, - format: pipeline.target_format, + format: pipeline.offscreen_format, usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, view_formats: &[], }); @@ -1834,8 +1865,10 @@ impl Scene3dResources { } /// Render scene `frame.id` into a transient copyable target at `frame.size_px` - /// and read it back as tightly packed RGBA8 (`width * height * 4`). Returns - /// `None` if the scene has no uploaded geometry yet or the GPU readback fails. + /// and read it back as tightly packed RGBA8 (`width * height * 4`). The + /// target is the sRGB offscreen format, so the bytes are sRGB-encoded — what + /// a PNG expects — whatever the surface format. Returns `None` if the scene + /// has no uploaded geometry yet or the GPU readback fails. /// /// The per-scene uniforms are (re)written for `frame`'s camera and size, then /// the same [`Scene3dGpu::encode_offscreen`] draw runs into a fresh @@ -1869,7 +1902,7 @@ impl Scene3dResources { mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D2, - format: pipeline.target_format, + format: pipeline.offscreen_format, usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC, view_formats: &[], }); @@ -1939,7 +1972,7 @@ impl Scene3dResources { let rgba = { let mapped = buffer.slice(..).get_mapped_range().ok()?; - rows_to_rgba8(&mapped, w, h, bpr, pipeline.target_format) + rows_to_rgba8(&mapped, w, h, bpr, pipeline.offscreen_format) }; buffer.unmap(); Some(rgba) @@ -2419,6 +2452,28 @@ mod tests { use crate::core::scene3d::mat4::{mat4_rotate, mat4_scale, mat4_translate}; + #[test] + fn offscreen_format_is_the_srgb_variant_of_the_target() { + use wgpu::TextureFormat as F; + // egui-wgpu's two default swapchain formats gain the sRGB suffix … + assert_eq!(offscreen_format(F::Rgba8Unorm), F::Rgba8UnormSrgb); + assert_eq!(offscreen_format(F::Bgra8Unorm), F::Bgra8UnormSrgb); + // … an sRGB surface is already there … + assert_eq!(offscreen_format(F::Rgba8UnormSrgb), F::Rgba8UnormSrgb); + // … and a format with no sRGB variant stays linear-valued. + assert_eq!(offscreen_format(F::Rgba16Float), F::Rgba16Float); + } + + #[test] + fn blit_entry_point_encodes_only_for_a_non_srgb_target() { + use wgpu::TextureFormat as F; + assert_eq!(blit_entry_point(F::Rgba8Unorm), "fs_main_gamma_target"); + assert_eq!(blit_entry_point(F::Bgra8Unorm), "fs_main_gamma_target"); + assert_eq!(blit_entry_point(F::Rgba16Float), "fs_main_gamma_target"); + assert_eq!(blit_entry_point(F::Rgba8UnormSrgb), "fs_main_linear_target"); + assert_eq!(blit_entry_point(F::Bgra8UnormSrgb), "fs_main_linear_target"); + } + #[test] fn bounding_box_with_axes_has_twelve_lines_and_rgb_axes() { let mut g = Scene3dGeometry::new(); diff --git a/src/render/shaders/scene3d_blit.wgsl b/src/render/shaders/scene3d_blit.wgsl index cdbcaac..f678f19 100644 --- a/src/render/shaders/scene3d_blit.wgsl +++ b/src/render/shaders/scene3d_blit.wgsl @@ -4,6 +4,15 @@ // egui-wgpu already calls set_viewport with the paint callback's rect, so NDC // [-1,1] maps exactly to the widget rect; the offscreen texture is sized to the // same pixel rect (see `Scene3dGpu::ensure_offscreen`), giving a 1:1 copy. +// +// The offscreen target is the sRGB variant of egui's surface format +// (`Scene3dPipeline::offscreen_format`), so the sample is linear colour. Which +// fragment entry point the blit pipeline uses depends on the surface +// (`blit_entry_point`): an sRGB surface encodes on write, so the linear sample +// passes through; egui-wgpu's default non-sRGB surface holds gamma-encoded +// values (egui's own shader writes gamma there), so the blit encodes the sample +// to sRGB itself — the same split as egui-wgpu's `fs_main_linear_framebuffer` / +// `fs_main_gamma_framebuffer`. Alpha is left as is in both. @group(0) @binding(0) var src_tex: texture_2d; @group(0) @binding(1) var src_sampler: sampler; @@ -29,7 +38,22 @@ fn vs_main(@builtin(vertex_index) vid: u32) -> VsOut { return out; } +// Linear → sRGB transfer function (IEC 61966-2-1), per channel. +fn linear_to_srgb(linear: vec3) -> vec3 { + let lower = linear * 12.92; + let higher = 1.055 * pow(linear, vec3(1.0 / 2.4)) - 0.055; + return select(higher, lower, linear <= vec3(0.0031308)); +} + +// sRGB surface: the hardware encodes on write, so pass the linear sample through. @fragment -fn fs_main(in: VsOut) -> @location(0) vec4 { +fn fs_main_linear_target(in: VsOut) -> @location(0) vec4 { return textureSample(src_tex, src_sampler, in.uv); } + +// Non-sRGB surface (egui-wgpu's default): encode the linear sample to sRGB here. +@fragment +fn fs_main_gamma_target(in: VsOut) -> @location(0) vec4 { + let c = textureSample(src_tex, src_sampler, in.uv); + return vec4(linear_to_srgb(c.rgb), c.a); +} diff --git a/tests/scene3d_mesh_render.rs b/tests/scene3d_mesh_render.rs index 7baebf4..14158bc 100644 --- a/tests/scene3d_mesh_render.rs +++ b/tests/scene3d_mesh_render.rs @@ -9,11 +9,16 @@ //! - the quad whose normal faces the headlight (camera-space `(0,0,1)`) is fully //! lit → ambient + diffuse ≈ 1.0 → white; //! - the quad whose normal is perpendicular to the light gets ambient only ≈ 0.3 -//! → dark grey. +//! in linear light → mid grey once encoded. //! //! Asserting the first is far brighter than the second proves the shader shades //! per-normal (not flat), with the right ambient/diffuse split. Using identical //! geometry isolates lighting from foreshortening/visibility. +//! +//! The harness target is a non-sRGB `Rgba8Unorm` read back verbatim, and the +//! scene blit encodes its linear result for such a target (the same bytes a +//! `Color32` carries), so the ambient 0.3 is expected as its sRGB byte ≈ 149, +//! not the linear 76. use egui_kittest::Harness; use egui_kittest::wgpu::{WgpuTestRenderer, create_render_state, default_wgpu_setup}; @@ -117,15 +122,17 @@ fn scene3d_mesh_is_shaded_by_its_normal() { gray(lit) > 200, "normal-toward-light quad should be fully lit (~white); got rgb{lit:?}" ); - // Perpendicular quad: ambient only ≈ 0.3 → ~76/255 grey, clearly not black - // (proves ambient is applied) and clearly not white (diffuse term is off). + // Perpendicular quad: ambient only ≈ 0.3 linear, sRGB-encoded by the blit + // for the non-sRGB target → ~149/255 grey, clearly not black (proves ambient + // is applied) and clearly not white (diffuse term is off). let a = gray(ambient); assert!( - (45..120).contains(&a), - "perpendicular quad should be ambient-only grey (~76); got rgb{ambient:?}" + (130..170).contains(&a), + "perpendicular quad should be ambient-only grey (~149 encoded); got rgb{ambient:?}" ); + // Encoding compresses the 1.0 vs 0.3 gap to 255 vs ~149 in 8 bits. assert!( - gray(lit) > a + 100, + gray(lit) > a + 80, "lit quad must be much brighter than the ambient-only quad; \ lit={} ambient={a}", gray(lit) diff --git a/tests/scene3d_target_encoding_render.rs b/tests/scene3d_target_encoding_render.rs new file mode 100644 index 0000000..2aa5e4f --- /dev/null +++ b/tests/scene3d_target_encoding_render.rs @@ -0,0 +1,166 @@ +//! Headless wgpu readback proving the 3D scene reaches a non-sRGB render target +//! in that target's encoding: an opaque triangle and the background clear must +//! come back as their own `Color32` bytes, both through the on-screen blit and +//! through `snapshot_scene3d`. +//! +//! `egui_kittest`'s renderer targets a non-sRGB `Rgba8Unorm` texture — the same +//! kind of target egui-wgpu's swapchain is — and reads the stored bytes back +//! verbatim. The scene is shaded in linear light and used to be blitted into +//! that target unconverted, so every 3D colour came out darker than its +//! `Color32` (teal `(33, 145, 140)` read back as `(4, 71, 66)`). Now the scene +//! renders into the sRGB variant of the surface format and the blit encodes the +//! linear sample for a non-sRGB surface (`gpu_scene3d::blit_entry_point`), so +//! the bytes round-trip to within one 8-bit step. +//! +//! Boundaries covered: a shaded primitive (triangle) and the clear colour; the +//! blit path and the snapshot readback path. Needs a GPU (real or software). + +use std::cell::RefCell; +use std::rc::Rc; + +use egui_kittest::Harness; +use egui_kittest::wgpu::{WgpuTestRenderer, create_render_state, default_wgpu_setup}; +use rsplot::egui::{self, Color32}; +use rsplot::egui_wgpu::RenderState; +use rsplot::egui_wgpu::RendererOptions; +use rsplot::{ + Camera, Scene3dGeometry, Vec3, install_scene3d, paint_scene3d, set_scene3d, snapshot_scene3d, +}; + +const SCENE_ID: u64 = 0; +const WIN: f32 = 300.0; +/// Half-side of the sampled block. +const BLOCK: usize = 3; + +const TRIANGLE: Color32 = Color32::from_rgb(33, 145, 140); +const BACKGROUND: Color32 = Color32::from_rgb(128, 64, 32); + +/// A camera at `(0,0,5)` looking down `-z`, up `+y`, fovy 30°. +fn camera() -> Camera { + Camera::new( + 30.0, + 0.1, + 100.0, + (1.0, 1.0), + Vec3::new(0.0, 0.0, 5.0), + Vec3::new(0.0, 0.0, -1.0), + Vec3::new(0.0, 1.0, 0.0), + ) +} + +/// Upload one opaque triangle covering the view centre (its corners stay well +/// inside the frame, so the frame corners show the clear colour). +fn upload_scene(rs: &RenderState) { + install_scene3d(rs); + let mut g = Scene3dGeometry::new(); + g.add_triangle( + [-0.5, -0.5, 0.0], + [0.5, -0.5, 0.0], + [0.0, 0.5, 0.0], + TRIANGLE, + ); + set_scene3d(rs, SCENE_ID, &g); +} + +/// Every pixel of the `(2·BLOCK)²` block around `(cx, cy)` must be `expected` +/// to within one 8-bit step per channel. +fn assert_block_is( + raw: &[u8], + width: usize, + (cx, cy): (usize, usize), + expected: Color32, + what: &str, +) { + let expected = [expected.r(), expected.g(), expected.b()]; + for y in cy - BLOCK..cy + BLOCK { + for x in cx - BLOCK..cx + BLOCK { + let i = (y * width + x) * 4; + let got = [raw[i], raw[i + 1], raw[i + 2]]; + let close = got.iter().zip(expected).all(|(&g, e)| g.abs_diff(e) <= 1); + assert!( + close, + "{what}: pixel ({x},{y}) is rgb{got:?}, expected rgb{expected:?} ± 1 \ + (the Color32's own sRGB bytes on the non-sRGB target)" + ); + } + } +} + +struct App { + camera: Camera, + last_rect: Option, +} + +impl App { + fn ui(&mut self, ui: &mut egui::Ui) { + let (rect, _resp) = ui.allocate_exact_size(ui.available_size(), egui::Sense::hover()); + paint_scene3d(ui, rect, SCENE_ID, &self.camera, BACKGROUND); + self.last_rect = Some(rect); + } +} + +#[test] +fn blitted_scene_reads_back_its_color32_bytes() { + let rs = create_render_state(default_wgpu_setup(), RendererOptions::default()); + upload_scene(&rs); + let app = Rc::new(RefCell::new(App { + camera: camera(), + last_rect: None, + })); + let renderer = WgpuTestRenderer::from_render_state(rs); + + let app_ui = app.clone(); + let mut harness = Harness::builder() + .with_size(egui::vec2(WIN, WIN)) + .with_pixels_per_point(1.0) + .renderer(renderer) + .build_ui(move |ui| app_ui.borrow_mut().ui(ui)); + + harness.step(); + let rect = app.borrow().last_rect.expect("scene rect captured"); + let image = harness.render().expect("headless wgpu render"); + let iw = image.width() as usize; + let raw = image.as_raw(); + + let at = |fx: f32, fy: f32| { + ( + (rect.min.x + fx * rect.width()).round() as usize, + (rect.min.y + fy * rect.height()).round() as usize, + ) + }; + assert_block_is(raw, iw, at(0.5, 0.5), TRIANGLE, "blit: triangle at centre"); + assert_block_is(raw, iw, at(0.08, 0.08), BACKGROUND, "blit: clear at corner"); +} + +#[test] +fn snapshot_reads_back_its_color32_bytes() { + let rs = create_render_state(default_wgpu_setup(), RendererOptions::default()); + upload_scene(&rs); + + let (w, h) = (WIN as u32, WIN as u32); + let raw = snapshot_scene3d(&rs, SCENE_ID, &camera(), BACKGROUND, (w, h)) + .expect("snapshot of an uploaded scene"); + assert_eq!(raw.len(), (w * h * 4) as usize); + + let iw = w as usize; + let at = |fx: f32, fy: f32| { + ( + (fx * w as f32).round() as usize, + (fy * h as f32).round() as usize, + ) + }; + assert_block_is( + &raw, + iw, + at(0.5, 0.5), + TRIANGLE, + "snapshot: triangle at centre", + ); + assert_block_is( + &raw, + iw, + at(0.08, 0.08), + BACKGROUND, + "snapshot: clear at corner", + ); +} From cb4d1e0e4b9bda9bb41def52934e3b95893b0229 Mon Sep 17 00:00:00 2001 From: Sang Woo Kim Date: Sat, 17 Oct 2026 22:24:56 +0900 Subject: [PATCH 6/6] fix(render): upload the volume ray-caster's voxels in the target's encoding premultiply_rgba kept the caller's sRGB bytes whatever the colour target: right for egui-wgpu's non-sRGB swapchain, encoded twice on an sRGB one, where an opaque (255, 128, 0) voxel stored as (255, 188, 0). The voxels now go through rgba8_for_target (the image path's nan_color_for_target, shared). --- CHANGELOG.md | 5 + src/render/gpu_image.rs | 28 ++-- src/render/shaders/volume_raycaster.wgsl | 4 + src/render/volume_raycaster.rs | 96 ++++++++++--- src/widget/volume_raycaster.rs | 5 +- tests/volume_target_encoding_render.rs | 170 +++++++++++++++++++++++ 6 files changed, 275 insertions(+), 33 deletions(-) create mode 100644 tests/volume_target_encoding_render.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 952c3a7..cb9d09a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,11 @@ library), **rsdm** (a PyDM-style EPICS display layer built on rsplot), and instead of darker. `snapshot_scene3d` / `SceneWidget::snapshot` read back the sRGB offscreen target, so their PNGs carry sRGB-encoded bytes whatever the surface format (they were as dark as the screen). +- **rsplot**: `VolumeRaycaster` uploads its voxel colours in the render + target's encoding — the sRGB bytes as is for egui-wgpu's non-sRGB swapchain + (unchanged there), linearised for an sRGB target, where the composite used + to be encoded a second time on store and showed washed out (an opaque + `(255, 128, 0)` voxel read back as `(255, 188, 0)`). ## [0.6.0] - 2026-10-16 diff --git a/src/render/gpu_image.rs b/src/render/gpu_image.rs index 01552a6..6e35c88 100644 --- a/src/render/gpu_image.rs +++ b/src/render/gpu_image.rs @@ -162,12 +162,14 @@ fn color_texel_format(srgb_target: bool) -> wgpu::TextureFormat { } } -/// Encode the colormap's straight-alpha sRGB `nan_color` bytes for the render -/// target, in the same encoding the LUT texels sample as (see -/// [`color_texel_format`]) so the shader composites the two identically: -/// linearised RGB for an sRGB target, the bytes as-is for a non-sRGB one. Alpha -/// is linear (not gamma-encoded) in both. -fn nan_color_for_target(c: [u8; 4], srgb_target: bool) -> [f32; 4] { +/// Encode straight-alpha sRGB RGBA8 bytes (a `Color32`'s, a colormap's) as the +/// `[f32; 4]` a shader composites in the render target's encoding: linearised +/// RGB for an sRGB target, the bytes as-is for a non-sRGB one — the same values +/// an 8-bit colour texture of [`color_texel_format`] samples as, so a colour +/// from either source composites identically. Alpha is linear (not +/// gamma-encoded) in both. Used for the image `nan_color` and for the volume +/// ray-caster's voxel texels (`volume_raycaster.rs`). +pub(crate) fn rgba8_for_target(c: [u8; 4], srgb_target: bool) -> [f32; 4] { if srgb_target { srgb_u8_to_linear_rgba(c) } else { @@ -177,7 +179,7 @@ fn nan_color_for_target(c: [u8; 4], srgb_target: bool) -> [f32; 4] { /// Convert an sRGB-encoded `[u8; 4]` color (the colormap's `nan_color`) to /// linear-space `[f32; 4]` — the sRGB-target branch of -/// [`nan_color_for_target`]. The RGB channels use the standard sRGB transfer +/// [`rgba8_for_target`]. The RGB channels use the standard sRGB transfer /// function; alpha is linear (not gamma-encoded). fn srgb_u8_to_linear_rgba(c: [u8; 4]) -> [f32; 4] { let to_linear = |b: u8| { @@ -709,7 +711,7 @@ enum GpuImageKind { /// 1 if a per-pixel alpha map is bound at binding 5, else 0. has_alpha_map: u32, /// RGBA for non-finite samples (silx `Colormap.nan_color`) in the render - /// target's encoding, like the LUT texels (see [`nan_color_for_target`]). + /// target's encoding, like the LUT texels (see [`rgba8_for_target`]). nan_color: [f32; 4], }, Rgba, @@ -926,7 +928,7 @@ impl GpuImage { norm: colormap.normalization.code(), interp: image.interpolation.code(), has_alpha_map: u32::from(image.alpha_map.is_some()), - nan_color: nan_color_for_target(colormap.nan_color, pipeline.srgb_target), + nan_color: rgba8_for_target(colormap.nan_color, pipeline.srgb_target), }, ) } @@ -1213,17 +1215,17 @@ mod tests { } #[test] - fn nan_color_for_target_matches_the_lut_encoding() { + fn rgba8_for_target_matches_the_lut_encoding() { let c = [188, 64, 255, 128]; // Non-sRGB target: the bytes pass through, like a texel of an // `Rgba8Unorm` LUT. - let gamma = nan_color_for_target(c, false); + let gamma = rgba8_for_target(c, false); let expected = c.map(|b| f32::from(b) / 255.0); assert_eq!(gamma, expected); // sRGB target: linearised RGB, like a texel of an `Rgba8UnormSrgb` LUT; // alpha linear in both. - assert_eq!(nan_color_for_target(c, true), srgb_u8_to_linear_rgba(c)); - assert_eq!(gamma[3], nan_color_for_target(c, true)[3]); + assert_eq!(rgba8_for_target(c, true), srgb_u8_to_linear_rgba(c)); + assert_eq!(gamma[3], rgba8_for_target(c, true)[3]); } #[test] diff --git a/src/render/shaders/volume_raycaster.wgsl b/src/render/shaders/volume_raycaster.wgsl index 711e285..4c476e3 100644 --- a/src/render/shaders/volume_raycaster.wgsl +++ b/src/render/shaders/volume_raycaster.wgsl @@ -10,6 +10,10 @@ // colour back out at low coverage. 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). +// +// The texels are already in the render target's encoding (the uploader keeps +// the sRGB bytes for a non-sRGB target and linearises them for an sRGB one), so +// nothing here converts colour: the composite is written out as is. struct Uniforms { // Inverse of the camera clip matrix P·V (OpenGL clip, z in [-1, 1]). diff --git a/src/render/volume_raycaster.rs b/src/render/volume_raycaster.rs index 141a6a5..7a4fecd 100644 --- a/src/render/volume_raycaster.rs +++ b/src/render/volume_raycaster.rs @@ -6,6 +6,12 @@ //! per-view 3D RGBA texture and composites premultiplied colour straight into //! egui's render pass (`shaders/volume_raycaster.wgsl`). //! +//! The voxel colours are uploaded in the render target's encoding +//! (`gpu_image::rgba8_for_target`): the caller's sRGB bytes as is for +//! egui-wgpu's non-sRGB swapchain, linearised for an sRGB target, so the +//! composite the shader writes is already what the target stores — the rule +//! every rsplot colour follows (`gpu_color`, the image LUT, the 3D blit). +//! //! Lifecycle mirrors the scene path: [`install_volume_raycaster`] once at //! startup inserts the shared pipeline into `callback_resources`; //! [`set_volume_raycaster`] uploads a view's volume texture; @@ -17,6 +23,8 @@ use std::sync::Mutex; use egui_wgpu::{RenderState, wgpu}; use half::f16; +use super::gpu_image::rgba8_for_target; + /// Identifies one raycaster view within the shared resources (caller-assigned, /// like `Scene3dId`). pub type VolumeId = u64; @@ -61,8 +69,11 @@ pub fn volume_bounds(depth: usize, height: usize, width: usize) -> ([f32; 3], [f ([-half[0], -half[1], -half[2]], half) } -/// Premultiply a straight-alpha RGBA8 buffer into `Rgba16Float` texels (`rgb *= -/// a`, both normalised to `[0, 1]`), so the linear sampler interpolates +/// Convert a straight-alpha RGBA8 buffer (sRGB bytes, as a `Color32` carries +/// them) into premultiplied `Rgba16Float` texels in the render target's +/// encoding. Each voxel goes through [`rgba8_for_target`] — the bytes as is for +/// a non-sRGB target, linearised RGB for an sRGB one, which encodes on store — +/// and is then premultiplied (`rgb *= a`), so the linear sampler interpolates /// premultiplied colour: a transparent voxel becomes `(0,0,0,0)` and /// interpolating it against an opaque colour keeps the hue instead of dragging /// it toward black. A trailing partial pixel (len not a multiple of 4) is @@ -75,17 +86,19 @@ pub fn volume_bounds(depth: usize, height: usize, width: usize) -> ([f32; 3], [f /// to steps of `1/a`, which for the low coverages a ray-march is built on /// (`a = 3/255`) is three colour levels total. `f16` holds every `c·a/255²` to /// ~0.05% relative, so the recovered RGB is exact to well under one 8-bit step -/// at every coverage down to 1/255. -fn premultiply_rgba(rgba: &[u8]) -> Vec { +/// at every coverage down to 1/255. The linearised dark end (`1/255 → 3.0e-4`) +/// times the lowest coverage lands in f16's subnormals, whose `6e-8` spacing +/// still re-encodes the recovered channel to its own byte (unit test). +fn premultiply_rgba(rgba: &[u8], srgb_target: bool) -> Vec { rgba.as_chunks::<4>() .0 .iter() .flat_map(|px| { - let a = f32::from(px[3]) / 255.0; + let [r, g, b, a] = rgba8_for_target(*px, srgb_target); let texel = [ - f16::from_f32(f32::from(px[0]) / 255.0 * a), - f16::from_f32(f32::from(px[1]) / 255.0 * a), - f16::from_f32(f32::from(px[2]) / 255.0 * a), + f16::from_f32(r * a), + f16::from_f32(g * a), + f16::from_f32(b * a), f16::from_f32(a), ]; texel.map(f16::to_le_bytes) @@ -98,6 +111,9 @@ struct VolumePipeline { pipeline: wgpu::RenderPipeline, bgl: wgpu::BindGroupLayout, sampler: wgpu::Sampler, + /// Whether the colour target is an `*Srgb` format: decides the encoding the + /// voxel texels are uploaded in (see [`premultiply_rgba`]). + srgb_target: bool, } impl VolumePipeline { @@ -187,6 +203,7 @@ impl VolumePipeline { pipeline, bgl, sampler, + srgb_target: target_format.is_srgb(), } } } @@ -216,21 +233,23 @@ impl VolumeGpu { } /// Upload a `(depth, height, width)` RGBA8 volume (row-major, straight - /// alpha), (re)building the 3D texture. The straight alpha is premultiplied - /// into `Rgba16Float` before upload so the linear sampler interpolates - /// premultiplied colour (no dark fringe at colour/transparent boundaries) - /// while the product still carries the straight RGB at low coverage — see - /// [`premultiply_rgba`]. Costs twice the VRAM of an 8-bit texture. + /// alpha, sRGB bytes), (re)building the 3D texture. The voxels are encoded + /// for the target (`srgb_target`) and premultiplied into `Rgba16Float` + /// before upload so the linear sampler interpolates premultiplied colour (no + /// dark fringe at colour/transparent boundaries) while the product still + /// carries the straight RGB at low coverage — see [`premultiply_rgba`]. + /// Costs twice the VRAM of an 8-bit texture. fn upload( &mut self, device: &wgpu::Device, queue: &wgpu::Queue, rgba: &[u8], dims: (usize, usize, usize), + srgb_target: bool, ) { let (depth, height, width) = dims; let (w, h, d) = (width as u32, height as u32, depth as u32); - let premul = premultiply_rgba(rgba); + let premul = premultiply_rgba(rgba, srgb_target); let extent = wgpu::Extent3d { width: w, height: h, @@ -384,7 +403,8 @@ pub fn release_volume_raycaster(render_state: &RenderState, id: VolumeId) { } /// Upload view `id`'s volume as a `(depth, height, width)` RGBA8 texture -/// (row-major, straight alpha). Requires a claim from +/// (row-major, straight alpha; sRGB bytes, as `Color32` carries them — they are +/// encoded for the render target on upload). Requires a claim from /// [`acquire_volume_raycaster`]; without one the call is a no-op, so an upload /// can never resurrect an entry whose last holder released it. pub fn set_volume_raycaster( @@ -413,6 +433,7 @@ pub fn set_volume_raycaster( .callback_resources .get_mut() .expect("VolumeRaycasterResources not installed — call install_volume_raycaster() first"); + let srgb_target = res.pipeline.srgb_target; let Some(scene) = res.scenes.get_mut(&id) else { debug_assert!(false, "set_volume_raycaster: id {id} holds no claim"); return; @@ -422,6 +443,7 @@ pub fn set_volume_raycaster( &render_state.queue, rgba, (depth, height, width), + srgb_target, ); } @@ -514,7 +536,7 @@ mod tests { 200, 100, 50, 0, // transparent → all zero 255, 255, 255, 128, // half coverage → ~half ]; - let out = premultiply_rgba(&src); + let out = premultiply_rgba(&src, false); assert_eq!(texel(&out, 0), [1.0, 0.0, 0.0, 1.0]); assert_eq!(texel(&out, 1), [0.0; 4]); let half = texel(&out, 2); @@ -530,7 +552,7 @@ mod tests { fn premultiply_recovers_the_straight_rgb_at_the_lowest_coverages() { for a in [1u8, 2, 3, 4, 8, 128, 255] { for c in [1u8, 37, 128, 200, 254, 255] { - let out = premultiply_rgba(&[c, 0, 0, a]); + let out = premultiply_rgba(&[c, 0, 0, a], false); let t = texel(&out, 0); let recovered = t[0] / t[3]; let want = f32::from(c) / 255.0; @@ -545,6 +567,44 @@ mod tests { #[test] fn premultiply_drops_trailing_partial_pixel() { let src = [255, 0, 0, 255, 1, 2]; // 6 bytes: one pixel + 2 stragglers - assert_eq!(premultiply_rgba(&src).len(), 8); // one Rgba16Float texel + assert_eq!(premultiply_rgba(&src, false).len(), 8); // one Rgba16Float texel + } + + /// The same opaque orange in both target encodings: the bytes as is for a + /// non-sRGB target, the sRGB transfer function applied for an sRGB one + /// (`128/255 → 0.2158`); alpha is linear in both. + #[test] + fn premultiply_encodes_the_rgb_for_the_target() { + let src = [255, 128, 0, 255]; + let gamma = texel(&premultiply_rgba(&src, false), 0); + let linear = texel(&premultiply_rgba(&src, true), 0); + assert!((gamma[1] - 128.0 / 255.0).abs() < 1e-3, "got {}", gamma[1]); + assert!((linear[1] - 0.2158).abs() < 1e-3, "got {}", linear[1]); + assert_eq!(linear[0], 1.0); + assert_eq!(linear[2], 0.0); + assert_eq!(linear[3], gamma[3]); + } + + /// The sRGB-target branch at the same coverage boundaries: the straight + /// channel the shader recovers (`s.rgb / s.a`), re-encoded, must be the + /// authored byte. Its dark end (`1/255 → 3.0e-4`) times the lowest coverage + /// is an f16 subnormal, so this is where the format's precision is thinnest. + #[test] + fn premultiply_recovers_the_authored_byte_through_the_linear_encoding() { + let encode = |l: f32| -> u8 { + let s = if l <= 0.003_130_8 { + l * 12.92 + } else { + 1.055 * l.powf(1.0 / 2.4) - 0.055 + }; + (s * 255.0).round() as u8 + }; + for a in [1u8, 2, 3, 4, 8, 128, 255] { + for c in [1u8, 37, 128, 200, 254, 255] { + let t = texel(&premultiply_rgba(&[c, 0, 0, a], true), 0); + let got = encode(t[0] / t[3]); + assert!(got.abs_diff(c) <= 1, "a={a} c={c}: re-encoded as {got}"); + } + } } } diff --git a/src/widget/volume_raycaster.rs b/src/widget/volume_raycaster.rs index 1d72a76..2d0f9ca 100644 --- a/src/widget/volume_raycaster.rs +++ b/src/widget/volume_raycaster.rs @@ -92,8 +92,9 @@ impl VolumeRaycaster { } /// Upload a `(depth, height, width)` RGBA8 volume (row-major, straight - /// alpha). The first upload frames the camera to the volume; later uploads - /// keep the viewpoint so a changing field animates in place. + /// alpha; sRGB bytes, as `Color32` carries them). The first upload frames + /// the camera to the volume; later uploads keep the viewpoint so a changing + /// field animates in place. pub fn set_volume( &mut self, render_state: &RenderState, diff --git a/tests/volume_target_encoding_render.rs b/tests/volume_target_encoding_render.rs new file mode 100644 index 0000000..31cd19a --- /dev/null +++ b/tests/volume_target_encoding_render.rs @@ -0,0 +1,170 @@ +//! Headless wgpu readback proving `VolumeRaycaster` writes its composite in the +//! render target's encoding on an *sRGB* target too. +//! +//! The voxel texels used to be the caller's sRGB bytes whatever the target. On +//! egui-wgpu's default non-sRGB swapchain that is right (the stored byte is the +//! colormap byte; `tests/volume_opacity_physics.rs` pins that path). On an +//! `*Srgb` target the hardware encodes the shader's output on store, so the +//! already-encoded bytes were encoded a second time: an opaque `(255, 128, 0)` +//! voxel read back as `(255, 188, 0)`. Now the uploader linearises the voxels +//! for an sRGB target (`gpu_image::rgba8_for_target`), so the stored bytes are +//! the authored ones again. +//! +//! `egui_kittest` renders into a texture of the render state's `target_format`, +//! so the sRGB target is reached by rebuilding the render state with the sRGB +//! variant of its format and an egui renderer for it (`RenderState`'s fields +//! are public). Boundaries covered on that target: an opaque voxel (opacity 1, +//! no blend) and the lowest-coverage voxels (a translucent composite over the +//! background — in linear light, as the hardware blends on an sRGB target); +//! and the opaque result must equal the non-sRGB target's. + +use std::cell::RefCell; +use std::rc::Rc; +use std::sync::{Arc, Mutex}; + +use egui_kittest::Harness; +use egui_kittest::wgpu::{WgpuTestRenderer, create_render_state, default_wgpu_setup}; +use rsplot::VolumeRaycaster; +use rsplot::egui; +use rsplot::egui::mutex::RwLock; +use rsplot::egui_wgpu::{RenderState, Renderer, RendererOptions}; + +const WIN: f32 = 320.0; +const N: usize = 16; + +/// See `tests/volume_raycaster_render.rs`: concurrent wgpu device create/destroy +/// faults on the Windows CI runner's DX12 software adapter. +static GPU: Mutex<()> = Mutex::new(()); + +/// A fresh render state whose colour target is the kittest default format or, +/// for `srgb`, that format's sRGB variant with an egui renderer built for it. +fn render_state(srgb: bool) -> RenderState { + let mut rs = create_render_state(default_wgpu_setup(), RendererOptions::default()); + if srgb { + let format = rs.target_format.add_srgb_suffix(); + assert!( + format.is_srgb(), + "kittest target {:?} has no sRGB variant", + rs.target_format + ); + rs.renderer = Arc::new(RwLock::new(Renderer::new( + &rs.device, + format, + RendererOptions::default(), + ))); + rs.target_format = format; + } + rs +} + +/// An `N³` RGBA8 volume, every voxel `voxel`. +fn cube(voxel: [u8; 4]) -> Vec { + voxel.iter().copied().cycle().take(N * N * N * 4).collect() +} + +struct App { + view: VolumeRaycaster, +} + +/// RGB of the centre pixel after rendering a uniform `voxel` cube on `rs`'s +/// target at the default 256 steps. The centre ray crosses the cube through its +/// middle: a unit chord, by `volume_bounds`. +fn centre_rgb(rs: RenderState, voxel: [u8; 4]) -> [u8; 3] { + let mut view = VolumeRaycaster::new(&rs, 13); + view.set_volume(&rs, &cube(voxel), N, N, N); + let app = Rc::new(RefCell::new(App { view })); + let renderer = WgpuTestRenderer::from_render_state(rs); + let app_ui = app.clone(); + let mut harness = Harness::builder() + .with_size(egui::vec2(WIN, WIN)) + .with_pixels_per_point(1.0) + .renderer(renderer) + .build_ui(move |ui| { + app_ui.borrow_mut().view.show(ui); + }); + harness.step(); + let image = harness.render().expect("headless wgpu render"); + let (iw, ih) = (image.width() as usize, image.height() as usize); + let centre = ((ih / 2) * iw + iw / 2) * 4; + let raw = image.as_raw(); + [raw[centre], raw[centre + 1], raw[centre + 2]] +} + +/// sRGB transfer function, byte → linear. +fn to_linear(b: u8) -> f64 { + let s = f64::from(b) / 255.0; + if s <= 0.04045 { + s / 12.92 + } else { + ((s + 0.055) / 1.055).powf(2.4) + } +} + +/// sRGB transfer function, linear → byte scale (`0..=255`, unrounded). +fn to_srgb_255(l: f64) -> f64 { + let s = if l <= 0.003_130_8 { + l * 12.92 + } else { + 1.055 * l.powf(1.0 / 2.4) - 0.055 + }; + s * 255.0 +} + +/// Analytic accumulated opacity of the centre ray through the unit cube of +/// uniform-`alpha` voxels — `tests/volume_opacity_physics.rs` at depth 16: the +/// chord is 1, the reference spacing `√3 / 256`, so the transmittance is +/// `(1 - alpha)^(256 / √3)`. +fn analytic_opacity(alpha: u8) -> f64 { + let sa = f64::from(alpha) / 255.0; + 1.0 - (1.0 - sa).powf(256.0 / 3f64.sqrt()) +} + +/// Boundary: opacity 1, no blend with the background. The stored bytes on an +/// sRGB target must be the voxel's own, and the same as on a non-sRGB target. +#[test] +fn opaque_voxels_store_their_own_bytes_on_an_srgb_target() { + let _gpu = GPU.lock().unwrap_or_else(|e| e.into_inner()); + const VOXEL: [u8; 4] = [255, 128, 0, 255]; + + let srgb = centre_rgb(render_state(true), VOXEL); + let gamma = centre_rgb(render_state(false), VOXEL); + for c in 0..3 { + assert!( + srgb[c].abs_diff(VOXEL[c]) <= 1, + "sRGB target: channel {c} stored as {} for voxel byte {} (all {srgb:?})", + srgb[c], + VOXEL[c] + ); + assert!( + srgb[c].abs_diff(gamma[c]) <= 1, + "sRGB target {srgb:?} differs from non-sRGB target {gamma:?} at channel {c}" + ); + } +} + +/// Boundary: the lowest coverage a march is built on, composited over the +/// background. On an sRGB target the blend runs in linear light (the hardware +/// decodes the destination), so the stored byte is the encoding of +/// `linear(voxel) · opacity + linear(background) · (1 - opacity)` — the +/// authored hue at the Beer–Lambert opacity, not a double-encoded one. +#[test] +fn low_coverage_voxels_composite_their_authored_hue_on_an_srgb_target() { + let _gpu = GPU.lock().unwrap_or_else(|e| e.into_inner()); + const VOXEL: [u8; 4] = [255, 128, 0, 3]; // faint orange + + // An all-transparent cube leaves the harness background at the centre. + let bg = centre_rgb(render_state(true), [255, 128, 0, 0]); + let got = centre_rgb(render_state(true), VOXEL); + let opacity = analytic_opacity(VOXEL[3]); + + for c in 0..3 { + let expect = + to_srgb_255(to_linear(VOXEL[c]) * opacity + to_linear(bg[c]) * (1.0 - opacity)); + assert!( + (f64::from(got[c]) - expect).abs() <= 2.0, + "channel {c}: expected {expect:.1}, got {} (all {got:?}, background {bg:?}, \ + opacity {opacity:.4})", + got[c] + ); + } +}