Skip to main content

nsh_engine/
shader_off.rs

1//! The native backends' shader API without the renderer (feature `shader` off):
2//! no GPU is ever found ([`Renderer::new`] is `None`), so shader views draw
3//! nothing of their own, and wgpu is not linked. The backends' code stays the
4//! same either way.
5
6use std::{
7    sync::Arc,
8    time::{Duration, Instant},
9};
10
11use nsh_core::{
12    animation::AnimationValue,
13    geometry::{Points, Size},
14    shader::{ShaderInput, ShaderLayer},
15};
16
17#[cfg(windows)]
18pub use crate::shader_frame::SharedTexture;
19pub use crate::shader_frame::{Frame, MAX_SIDE, PixelFormat};
20
21/// This build has no renderer: a platform reports `Environment::shaders`
22/// `false` from the start.
23pub const LINKED: bool = false;
24
25/// No renderer ever looks for a GPU here.
26pub(crate) fn no_gpu() -> bool {
27    false
28}
29
30/// Never made: this build has no renderer.
31pub struct Renderer {
32    format: PixelFormat,
33}
34
35impl Renderer {
36    /// `None`: this build has no renderer.
37    pub fn new(_format: PixelFormat) -> Option<Renderer> {
38        static WARNED: std::sync::Once = std::sync::Once::new();
39        WARNED.call_once(|| {
40            log::warn!(
41                "a shader view draws nothing: this runtime was built without the NSH platform crate's feature `shader`"
42            )
43        });
44        None
45    }
46
47    /// Calls `done(None)` at once: this build has no renderer.
48    pub fn open(_format: PixelFormat, done: impl FnOnce(Option<Renderer>) + Send + 'static) {
49        done(None);
50    }
51
52    /// Does nothing: no pipeline is ever compiled (`f` is dropped).
53    pub fn on_pipeline_ready(&self, f: impl Fn() + Send + Sync + 'static) {
54        drop(f);
55    }
56
57    /// `false`: no pipeline is ever compiled.
58    pub fn compiling(&self) -> bool {
59        false
60    }
61
62    /// `true` at once: no pipeline is ever compiled.
63    pub fn wait_for_pipelines(&self, _timeout: Duration) -> bool {
64        true
65    }
66
67    /// The format of the frames.
68    pub fn format(&self) -> PixelFormat {
69        self.format
70    }
71
72    /// `false`: there are no frames (`f` is dropped).
73    pub fn when_done(&self, f: impl FnOnce() + Send + 'static) -> bool {
74        drop(f);
75        false
76    }
77}
78
79/// One view's state, which never renders.
80#[derive(Default)]
81pub struct ShaderView {
82    layer: Option<Arc<ShaderLayer>>,
83    frame: Frame,
84}
85
86impl ShaderView {
87    /// Keeps `layer`.
88    pub fn set_layer(&mut self, layer: Option<Arc<ShaderLayer>>) {
89        self.layer = layer;
90    }
91
92    /// Does nothing.
93    pub fn set_input(
94        &mut self,
95        _renderer: &Renderer,
96        _input: ShaderInput,
97        _picture: Option<(&[u8], u32, u32, u32)>,
98    ) {
99    }
100
101    /// `false`: there is no frame.
102    pub fn set_input_from_frame(&mut self, _renderer: &Renderer, _input: ShaderInput) -> bool {
103        false
104    }
105
106    /// `false`: nothing is kept.
107    pub fn has_input(&self, _input: ShaderInput) -> bool {
108        false
109    }
110
111    /// Does nothing: a transition ends at once.
112    pub fn start_transition(&mut self, _duration: Duration, _now: Instant) {}
113
114    /// Does nothing: a transition ends at once.
115    pub fn start_transition_backwards(&mut self, _duration: Duration, _now: Instant) {}
116
117    /// `false`: a transition ends at once.
118    pub fn transitioning(&mut self, _now: Instant) -> bool {
119        false
120    }
121
122    /// `false`: the children stay.
123    pub fn replaces_children(&mut self, _now: Instant) -> bool {
124        false
125    }
126
127    /// Nothing is drawn beyond the box.
128    pub fn overflow(&self) -> Points {
129        Points(0.0)
130    }
131
132    /// The layer it was given.
133    pub fn layer(&self) -> Option<&Arc<ShaderLayer>> {
134        self.layer.as_ref()
135    }
136
137    /// Does nothing.
138    pub fn set_slot(&mut self, _slot: u16, _value: AnimationValue) {}
139
140    /// Does nothing.
141    pub fn set_size(&mut self, _size: Size<Points>, _scale: f32) {}
142
143    /// `false`: there are no frames.
144    pub fn wants_frame(&self) -> bool {
145        false
146    }
147
148    /// `false`: nothing compiles.
149    pub fn compiling(&self) -> bool {
150        false
151    }
152
153    /// `false`: there are no frames.
154    pub fn pending(&self) -> bool {
155        false
156    }
157
158    /// `false`: there are no frames.
159    pub fn due(&self) -> bool {
160        false
161    }
162
163    /// `false`: there are no frames.
164    pub fn animates(&self) -> bool {
165        false
166    }
167
168    /// The empty frame.
169    pub fn frame(&self) -> &Frame {
170        &self.frame
171    }
172
173    /// Does nothing: there are no frames.
174    #[cfg(any(target_vendor = "apple", windows))]
175    pub fn show_on_surfaces(&mut self, _on: bool) {}
176
177    /// `None`: there are no frames.
178    #[cfg(windows)]
179    pub fn surface(&self) -> Option<SharedTexture> {
180        None
181    }
182
183    /// `None`: there are no frames.
184    #[cfg(target_vendor = "apple")]
185    pub fn surface(&self) -> Option<&objc2_io_surface::IOSurfaceRef> {
186        None
187    }
188
189    /// `false`: there are no frames.
190    pub fn collect_frame(&mut self, _renderer: &Renderer) -> bool {
191        false
192    }
193
194    /// `false`: there are no frames.
195    pub fn render(&mut self, _renderer: &Renderer, _now: Instant) -> bool {
196        false
197    }
198}