Skip to content

Commit

Permalink
listen: Split all_is_cubes::listen into a library, nosy.
Browse files Browse the repository at this point in the history
This commit contains only the minimal migration changes; in particular,
`all_is_cubes::listen` still exists, and contains aliases for the old
item names rather than replacing them everywhere.

Rationale:

* Reduces the build time of `all-is-cubes` (both that crate in
  particular, and the whole project) by moving code into dependencies.
* Makes the functionality more available to other projects (my own and
  others) without taking a dependency on `all-is-cubes`.

A benefit of the migration is that the replacement of `ListenableSource`
with `DynSource` allows for sources that are neither `ListenableCell`
(now `Cell`) nor constants. We do not take advantage of this yet.
  • Loading branch information
kpreid committed Dec 20, 2024
1 parent 940a072 commit c4603c5
Show file tree
Hide file tree
Showing 52 changed files with 281 additions and 1,889 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
- `block::EvalBlockError` is now a `struct` with an inner `ErrorKind` enum, instead of an enum, and contains more information.
- `block::Move`’s means of construction have been changed to be more systematic and orthogonal. In particular, paired moves are constructed from unpaired ones.

- The `listen` module is now a reexport of the separate library [`nosy`](https://docs.rs/nosy).
Many items have changed in name and signature.

- `math::FaceMap::repeat()` has been renamed to `splat()`, for consistency with the same concept in the `euclid` vector types which we use.
* `math::Geometry` is now `math::Wireframe`, and its `translate()` method has been replaced with inherent methods on its implementors.
- `math::GridAab::expand()` now takes unsigned values; use `GridAab::shrink()` instead of negative ones. This allows both versions to never panic.
Expand Down
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ macro_rules_attribute = { version = "0.2.0", default-features = false }
manyfmt = { version = "0.1.0", default-features = false }
mutants = { version = "0.0.3", default-features = false }
noise = { version = "0.9.0", default-features = false }
nosy = { version = "0.1.0", default-features = false }
num-traits = { version = "0.2.19", default-features = false }
ordered-float = { version = "4.2.0", default-features = false }
paste = {version = "1.0.15", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-content/src/city/exhibits/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(super) use all_is_cubes::euclid::{
size3, vec3, Point3D, Rotation2D, Size3D, Vector2D, Vector3D,
};
pub(super) use all_is_cubes::linking::{BlockProvider, InGenError};
pub(super) use all_is_cubes::listen::ListenableSource;
pub(super) use all_is_cubes::listen;
pub(super) use all_is_cubes::math::{
ps32, rgb_const, rgba_const, zo32, Cube, Face6, FaceMap, FreeCoordinate, GridAab,
GridCoordinate, GridPoint, GridRotation, GridSize, GridVector, Gridgid, PositiveSign, Rgb,
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-content/src/city/exhibits/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn UI_PROGRESS_BAR(ctx: Context<'_>) {
vui::LayoutTree::leaf(widgets::ProgressBar::new(
ctx.widget_theme,
Face6::PX,
ListenableSource::constant(widgets::ProgressBarState::new(fraction)),
listen::constant(widgets::ProgressBarState::new(fraction)),
))
};

Expand Down
15 changes: 8 additions & 7 deletions all-is-cubes-desktop/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::time::Duration;

use anyhow::Context;

use all_is_cubes::listen::{self, Listen as _, ListenableSource};
use all_is_cubes::listen::{self, Listen as _};
use all_is_cubes::universe::Universe;
use all_is_cubes_port::gltf::{GltfDataDestination, GltfWriter};
use all_is_cubes_port::{ExportSet, Format};
Expand Down Expand Up @@ -168,16 +168,16 @@ impl Recorder {
let export_set = if options.save_all || export_format == Format::AicJson {
ExportSet::all_of_universe(universe)
} else {
ExportSet::from_spaces(vec![cameras.world_space().get().ok_or_else(
|| match universe.whence.document_name() {
ExportSet::from_spaces(vec![cameras.world_space().get().ok_or_else(|| {
match universe.whence.document_name() {
None => {
anyhow::anyhow!("universe contains no default space to export")
}
Some(name) => anyhow::anyhow!(
"universe {name:?} contains no default space to export",
),
},
)?])
}
})?])
};

RecorderInner::Export {
Expand Down Expand Up @@ -208,7 +208,7 @@ impl Recorder {
let mut renderer = RtRenderer::new(
rec.cameras.clone(),
Box::new(|v| v),
ListenableSource::constant(Arc::new(())),
listen::constant(Arc::new(())),
);
renderer.update(None).unwrap();

Expand Down Expand Up @@ -328,8 +328,9 @@ impl Recorder {

impl listen::Listen for Recorder {
type Msg = Status;
type Listener = <listen::Notifier<Self::Msg> as listen::Listen>::Listener;

fn listen_raw(&self, listener: listen::DynListener<Self::Msg>) {
fn listen_raw(&self, listener: Self::Listener) {
if let Some(notifier) = self.status_notifier.upgrade() {
notifier.listen_raw(listener)
}
Expand Down
6 changes: 3 additions & 3 deletions all-is-cubes-desktop/src/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ pub fn inner_main<Ren: Renderer, Win: Window>(
// Note that this does NOT use the session's viewport_cell, so that the recording can
// have a consistent, as-requested size, regardless of what other rendering might be
// doing. (Of course, the UI will fail to adapt, but there isn't much to do about that.)
let recording_cameras = ctx.create_cameras(
all_is_cubes::listen::ListenableSource::constant(record_options.viewport()),
);
let recording_cameras = ctx.create_cameras(all_is_cubes::listen::constant(
record_options.viewport(),
));

let recorder = ctx.with_universe(|universe| {
record::configure_universe_for_recording(
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-desktop/src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use ratatui::layout::Rect;

use all_is_cubes::arcstr::literal_substr;
use all_is_cubes::euclid::{Point2D, Size2D};
use all_is_cubes::listen::{ListenableCell, ListenableSource};
use all_is_cubes::listen::{self, ListenableCell};
use all_is_cubes::math::Rgba;
use all_is_cubes_render::camera::{self, Camera, StandardCameras, Viewport};
use all_is_cubes_render::raytracer::{
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn create_terminal_session(
.send(RtRenderer::new(
cameras.clone(),
Box::new(|v| v),
ListenableSource::constant(Arc::new(())),
listen::constant(Arc::new(())),
))
.unwrap();
}
Expand Down
1 change: 1 addition & 0 deletions all-is-cubes-desktop/src/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub fn winit_main_loop_and_init<Ren: RendererToWinit + 'static>(
}

/// Creates a [`DesktopSession`] that can be run in an [`winit`] event loop.
#[allow(clippy::large_stack_frames, reason = "wildly overestimated somehow")]
pub async fn create_winit_wgpu_desktop_session(
executor: Arc<crate::Executor>,
session: Session,
Expand Down
6 changes: 3 additions & 3 deletions all-is-cubes-gpu/src/common/reloadable.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! “Hot-reloadable” data sources such as shaders.
//!
//! This module builds on top of the `resource` library to add change notification
//! via a background thread and all-is-cubes's `ListenableSource` mechanism.
//! via a background thread and all-is-cubes's `ListenableCell` mechanism.
use std::sync::{Arc, LazyLock, Mutex, PoisonError};
use std::time::Duration;

use resource::Resource;

use all_is_cubes::listen::{ListenableCell, ListenableSource};
use all_is_cubes::listen::{self, ListenableCell};

#[derive(Clone)]
pub(crate) struct Reloadable(Arc<ReloadableInner>);
Expand Down Expand Up @@ -55,7 +55,7 @@ impl Reloadable {
}
}

pub fn as_source(&self) -> ListenableSource<Arc<str>> {
pub fn as_source(&self) -> listen::DynSource<Arc<str>> {
self.0.cell.as_source()
}
}
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-gpu/src/in_wgpu/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use futures_channel::oneshot;
use futures_core::future::BoxFuture;

use all_is_cubes::character::Cursor;
use all_is_cubes::listen::{DirtyFlag, ListenableSource};
use all_is_cubes::listen::{self, DirtyFlag};
use all_is_cubes::util::Executor;
use all_is_cubes_render::camera::{StandardCameras, Viewport};
use all_is_cubes_render::{Flaws, HeadlessRenderer, RenderError, Rendering};
Expand Down Expand Up @@ -104,7 +104,7 @@ struct RendererImpl {
queue: Arc<wgpu::Queue>,
color_texture: wgpu::Texture,
everything: super::EverythingRenderer<AdaptedInstant>,
viewport_source: ListenableSource<Viewport>,
viewport_source: listen::DynSource<Viewport>,
viewport_dirty: DirtyFlag,
flaws: Flaws,
}
Expand Down
8 changes: 4 additions & 4 deletions all-is-cubes-gpu/src/in_wgpu/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::mem;
use std::sync::Arc;

use all_is_cubes::listen::DirtyFlag;
use all_is_cubes::listen::{Listen, ListenableSource};
use all_is_cubes::listen::{self, Listen as _};
use all_is_cubes_render::camera::{GraphicsOptions, TransparencyOption};

use crate::in_wgpu::frame_texture::FramebufferTextures;
Expand All @@ -26,7 +26,7 @@ pub(crate) struct Pipelines {
/// Tracks whether we need to rebuild pipelines for any reasons.
dirty: DirtyFlag,

graphics_options: ListenableSource<Arc<GraphicsOptions>>,
graphics_options: listen::DynSource<Arc<GraphicsOptions>>,

/// Layout for the camera buffer.
pub(crate) camera_bind_group_layout: wgpu::BindGroupLayout,
Expand Down Expand Up @@ -84,7 +84,7 @@ impl Pipelines {
device: &wgpu::Device,
shaders: &Shaders,
fb: &FramebufferTextures,
graphics_options: ListenableSource<Arc<GraphicsOptions>>,
graphics_options: listen::DynSource<Arc<GraphicsOptions>>,
) -> Self {
// TODO: This is a hazard we should remove. `Pipelines` needs to be consistent with
// other objects (in particular, pipeline versus framebuffer sample_count), and so
Expand Down Expand Up @@ -551,7 +551,7 @@ impl Pipelines {
fb,
mem::replace(
&mut self.graphics_options,
ListenableSource::constant(Default::default()),
listen::Constant::default().into(),
),
);
}
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-gpu/src/in_wgpu/raytrace_to_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use all_is_cubes::character::Cursor;
use all_is_cubes::drawing::embedded_graphics::pixelcolor::PixelColor;
use all_is_cubes::drawing::embedded_graphics::{draw_target::DrawTarget, prelude::Point, Pixel};
use all_is_cubes::euclid::{point2, vec2, Box2D};
use all_is_cubes::listen::ListenableSource;
use all_is_cubes::listen;
use all_is_cubes::math::{Rgb, Rgba, VectorOps as _};
use all_is_cubes_render::camera::{area_usize, Camera, StandardCameras, Viewport};
use all_is_cubes_render::raytracer::{ColorBuf, RtRenderer};
Expand Down Expand Up @@ -51,7 +51,7 @@ impl RaytraceToTexture {
rtr: RtRenderer::new(
cameras,
Box::new(raytracer_size_policy),
ListenableSource::constant(Arc::new(())),
listen::constant(Arc::new(())),
),
pixel_picker: PixelPicker::new(initial_viewport, false),
dirty_pixels: initial_viewport.pixel_count().unwrap(),
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-gpu/src/in_wgpu/shader_testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::sync::Arc;
use wgpu::util::DeviceExt as _;

use all_is_cubes::euclid::{point3, Rotation3D};
use all_is_cubes::listen::ListenableSource;
use all_is_cubes::listen;
use all_is_cubes::math::{ps64, Face6, FreeVector, GridSize, GridVector, Rgba};
use all_is_cubes::time;
use all_is_cubes_mesh::{BlockVertex, Coloring};
Expand Down Expand Up @@ -82,7 +82,7 @@ where
&device,
&shaders,
&fbt,
ListenableSource::constant(Arc::new(GraphicsOptions::default())),
listen::constant(Arc::new(GraphicsOptions::default())),
);

let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
Expand Down
13 changes: 5 additions & 8 deletions all-is-cubes-gpu/src/in_wgpu/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ impl Shaders {

impl listen::Listen for Shaders {
type Msg = ();
fn listen_raw(&self, listener: listen::DynListener<()>) {
self.modules_changed.listen(listener)
type Listener = <listen::Notifier<Self::Msg> as listen::Listen>::Listener;
fn listen_raw(&self, listener: Self::Listener) {
self.modules_changed.listen_raw(listener)
}
}

Expand All @@ -99,18 +100,14 @@ impl listen::Listen for Shaders {
/// The code on initial creation must be valid or creation will panic.
pub(crate) struct ReloadableShader {
label: String,
source: listen::ListenableSource<Arc<str>>,
source: listen::DynSource<Arc<str>>,
dirty: listen::DirtyFlag,
current_module: Identified<wgpu::ShaderModule>,
next_module: Option<BoxFuture<'static, Result<wgpu::ShaderModule, wgpu::Error>>>,
}

impl ReloadableShader {
fn new(
device: &wgpu::Device,
label: String,
wgsl_source: listen::ListenableSource<Arc<str>>,
) -> Self {
fn new(device: &wgpu::Device, label: String, wgsl_source: listen::DynSource<Arc<str>>) -> Self {
let dirty = listen::DirtyFlag::listening(false, &wgsl_source);
let current_module =
Identified::new(device.create_shader_module(wgpu::ShaderModuleDescriptor {
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-gpu/src/in_wgpu/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl<I: time::Instant> SpaceRenderer<I> {
) -> Result<SpaceUpdateInfo, RenderError> {
let start_time = I::now();

let todo = &mut self.todo.lock().unwrap();
let todo = &mut self.todo.lock();

let Some(csm) = &mut self.csm else {
if mem::take(&mut todo.sky) {
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-mesh/src/dynamic/chunked_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ where
let view_chunk_is_different = self.view_chunk != view_chunk;
self.view_chunk = view_chunk;

let todo: &mut CsmTodo<CHUNK_SIZE> = &mut self.todo.lock().unwrap();
let todo: &mut CsmTodo<CHUNK_SIZE> = &mut self.todo.lock();

let space = &*if let Ok(space) = self.space.read() {
space
Expand Down
12 changes: 6 additions & 6 deletions all-is-cubes-render/benches/raytrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use all_is_cubes::character::Character;
use all_is_cubes::content::testing::lighting_bench_space;
use all_is_cubes::euclid::size3;
use all_is_cubes::listen::ListenableSource;
use all_is_cubes::listen;
use all_is_cubes::universe::{Handle, Universe};
use all_is_cubes::util::yield_progress_for_testing;
use all_is_cubes_render::camera::{
Expand Down Expand Up @@ -46,13 +46,13 @@ impl TestData {
options_fn(&mut options);
let mut renderer = RtRenderer::new(
StandardCameras::new(
ListenableSource::constant(Arc::new(options)),
ListenableSource::constant(Viewport::with_scale(1.0, [64, 16])),
ListenableSource::constant(Some(self.character.clone())),
ListenableSource::constant(Arc::new(UiViewState::default())),
listen::constant(Arc::new(options)),
listen::constant(Viewport::with_scale(1.0, [64, 16])),
listen::constant(Some(self.character.clone())),
listen::constant(Arc::new(UiViewState::default())),
),
Box::new(core::convert::identity),
ListenableSource::constant(Arc::new(())),
listen::constant(Arc::new(())),
);
renderer.update(None).unwrap();
renderer
Expand Down
Loading

0 comments on commit c4603c5

Please sign in to comment.