nsi_intermediate/resolve/outputs.rs
1//! The camera/screen/layer/driver chain, which is what a renderer
2//! reads to know where pixels go.
3
4use super::*;
5
6impl Scene {
7 /// Resolve ɴsɪ's output chain into what a renderer actually needs.
8 ///
9 /// ɴsɪ spreads this over four nodes and three connection classes --
10 /// `outputdriver -> outputlayer -> screen -> camera` -- where both
11 /// targets want it collapsed: Mitsuba into a `Sensor` with a `Film`,
12 /// MoonRay into `RenderOutput`s. The walk is the same either way.
13 ///
14 /// One entry per screen, since a screen is what pairs a camera with
15 /// a resolution. A screen with no layers still yields an entry: the
16 /// camera and resolution are meaningful on their own.
17 ///
18 /// Layers and drivers come back in connection order, which is
19 /// insertion order in `edges`, so AOV order is the order the
20 /// consumer declared.
21 pub fn render_outputs(&self) -> Vec<RenderOutput> {
22 self.edges()
23 .filter(|edge| edge.kind == EdgeKind::Screen)
24 .map(|screen_edge| {
25 let screen = &screen_edge.from;
26 let mut layers: Vec<OutputLayer> = self
27 .edges_to_attribute(
28 screen,
29 EdgeKind::OutputLayer.to_attribute(),
30 )
31 .filter(|edge| edge.kind == EdgeKind::OutputLayer)
32 .map(|layer_edge| self.output_layer(layer_edge.from()))
33 .collect();
34
35 // ɴsɪ: "Layers with the lowest sortkey attribute
36 // appear first" among the layers on one driver. A
37 // stable sort keeps connection order for the layers
38 // that set no key, and `None` sorts before `Some` --
39 // an unkeyed layer is not last, it is unordered, and
40 // connection order is the only thing left to honour.
41 layers.sort_by_key(|layer| layer.sort_key);
42
43 RenderOutput {
44 camera: screen_edge.to().to_string(),
45 screen: screen.to_string(),
46 layers,
47 }
48 })
49 .collect()
50 }
51}
52
53impl Scene {
54 /// One `outputlayer` node, with the specification's defaults
55 /// applied to every attribute it does not set.
56 fn output_layer(&self, handle: &str) -> OutputLayer {
57 let node = self.node(handle);
58 let string = |name: &str| {
59 node.and_then(|node| node.string(name)).map(str::to_string)
60 };
61 let or = |name: &str, default: &str| {
62 string(name).unwrap_or_else(|| default.to_string())
63 };
64
65 OutputLayer {
66 handle: handle.to_string(),
67 variable_name: string(VARIABLE_NAME),
68 variable_source: or(VARIABLE_SOURCE, "shader"),
69 layer_name: string(LAYER_NAME),
70 layer_type: or(LAYER_TYPE, "color"),
71 scalar_format: or(SCALAR_FORMAT, "uint8"),
72 with_alpha: node
73 .and_then(|node| node.flag(WITH_ALPHA))
74 .unwrap_or(false),
75 dithering: node
76 .and_then(|node| node.flag(DITHERING))
77 .unwrap_or(false),
78 filter: or(FILTER, "blackman-harris"),
79 filter_width: node
80 .and_then(|node| node.f64(FILTER_WIDTH))
81 .unwrap_or(3.0),
82 color_profile: string(COLOR_PROFILE),
83 sort_key: node.and_then(|node| node.i32(SORT_KEY)),
84 drivers: self
85 .edges_to_attribute(
86 handle,
87 EdgeKind::OutputDriver.to_attribute(),
88 )
89 .filter(|edge| edge.kind == EdgeKind::OutputDriver)
90 .map(|edge| edge.from().to_string())
91 .collect(),
92 }
93 }
94}