| 1 | //! Deep Pixel evaluators: `SSAO`, `Shader`, `Texture`. |
| 2 | |
| 3 | use super::*; |
| 4 | |
| 5 | impl Engine { |
| 6 | /// `SSAO`: hemisphere multi-tap ambient occlusion from `aux.z` and `aux.normal` (normals are |
| 7 | /// derived from depth when absent). `Mode = Composite` darkens the image, `AO Only` outputs the occlusion term. |
| 8 | #[allow(clippy::too_many_arguments)] |
| 9 | pub(in crate::eval) fn eval_ssao( |
| 10 | &mut self, |
| 11 | comp: &Comp, |
| 12 | node: &Node, |
| 13 | time: f64, |
| 14 | frame_i: i64, |
| 15 | enc: &mut wgpu::CommandEncoder, |
| 16 | visiting: &mut HashSet<(NodeId, i64)>, |
| 17 | stats: &mut EvalStats, |
| 18 | ) -> CachedTexture { |
| 19 | let Some(src) = self.df_input(comp, node, &INPUT_PORTS, time, enc, visiting, stats) else { |
| 20 | return self.dummy.clone(); |
| 21 | }; |
| 22 | let blend = self.df_blend(comp, node, time); |
| 23 | let aux = src.aux.clone(); |
| 24 | let Some(z) = aux.as_ref().and_then(|a| a.z.as_ref()).map(|z| z.view.clone()) else { |
| 25 | return src; |
| 26 | }; |
| 27 | if blend.0 <= 1e-6 && !blend.1 { |
| 28 | return src; |
| 29 | } |
| 30 | let normal = aux.as_ref().and_then(|a| a.normal.as_ref()).map(|n| n.view.clone()); |
| 31 | let fin = self.df_fin(comp, node, time, blend, src.size, enc, visiting, stats); |
| 32 | |
| 33 | let n = |keys: &[&str], d: f64| num_any(comp, node, keys, time, d, &self.expr) as f32; |
| 34 | let mut u = UniformParams::default(); |
| 35 | u.p[0] = [ |
| 36 | n(&["Radius"], 1.0).max(1e-4), |
| 37 | n(&["Strength", "Amount"], 1.0).max(0.0), |
| 38 | n(&["Bias"], 0.025), |
| 39 | n(&["Samples"], 16.0).round().clamp(4.0, 64.0), |
| 40 | ]; |
| 41 | let mode = enum_idx(comp, node, "Mode", &["Composite", "AO Only"], time, 0, &self.expr); |
| 42 | u.p[1] = [ |
| 43 | self.camera_rig(comp, node, time, false).tan_half, |
| 44 | src.size[0] as f32 / src.size[1].max(1) as f32, |
| 45 | mode as f32, |
| 46 | b2f(normal.is_some()), |
| 47 | ]; |
| 48 | |
| 49 | let pass = DfPass { |
| 50 | tag: "SSAO", |
| 51 | op: DeepFlowOp::Ssao, |
| 52 | srcs: [Some(&src.view), Some(&z), normal.as_deref(), None], |
| 53 | hashes: &[src.content_hash], |
| 54 | size: src.size, |
| 55 | depth: self.node_depth(comp, node, time, Some(src.depth)), |
| 56 | }; |
| 57 | self.df_run(node, frame_i, pass, &fin, &u, aux, enc, stats) |
| 58 | } |
| 59 | |
| 60 | /// `Shader`: relights from `aux.normal` with a directional light (`EquatorAngle` / `PolarHeight`): |
| 61 | /// `Ambient` + `Diffuse` Lambert, `Specular` highlight tinted by `SpecularRed/Green/Blue`, and |
| 62 | /// `Reflection` of `EnvironmentReflectionMap` (`ReflectionType` = Screen / Spherical / Refraction). |
| 63 | /// Without normals only `Ambient` remains. |
| 64 | #[allow(clippy::too_many_arguments)] |
| 65 | pub(in crate::eval) fn eval_deep_shader( |
| 66 | &mut self, |
| 67 | comp: &Comp, |
| 68 | node: &Node, |
| 69 | time: f64, |
| 70 | frame_i: i64, |
| 71 | enc: &mut wgpu::CommandEncoder, |
| 72 | visiting: &mut HashSet<(NodeId, i64)>, |
| 73 | stats: &mut EvalStats, |
| 74 | ) -> CachedTexture { |
| 75 | let Some(src) = self.df_input(comp, node, &INPUT_PORTS, time, enc, visiting, stats) else { |
| 76 | return self.dummy.clone(); |
| 77 | }; |
| 78 | let blend = self.df_blend(comp, node, time); |
| 79 | if blend.0 <= 1e-6 && !blend.1 { |
| 80 | return src; |
| 81 | } |
| 82 | let map = self.df_input(comp, node, &["EnvironmentReflectionMap"], time, enc, visiting, stats); |
| 83 | let aux = src.aux.clone(); |
| 84 | let normal = aux.as_ref().and_then(|a| a.normal.as_ref()).map(|n| n.view.clone()); |
| 85 | let fin = self.df_fin(comp, node, time, blend, src.size, enc, visiting, stats); |
| 86 | |
| 87 | let n = |key: &str, d: f64| num_any(comp, node, &[key], time, d, &self.expr) as f32; |
| 88 | let (equator, polar) = (n("EquatorAngle", 45.0).to_radians(), n("PolarHeight", 45.0).to_radians()); |
| 89 | let mut u = UniformParams::default(); |
| 90 | u.p[0] = [n("Ambient", 0.25), n("Diffuse", 1.0), n("Specular", 1.0), n("Reflection", 1.0)]; |
| 91 | u.p[1] = [b2f(map.is_some()), b2f(normal.is_some()), n("ReflectionType", 0.0).round(), 0.0]; |
| 92 | u.p[2] = [polar.cos() * equator.cos(), polar.cos() * equator.sin(), polar.sin(), 0.0]; |
| 93 | u.p[3] = [n("SpecularRed", 1.0), n("SpecularGreen", 1.0), n("SpecularBlue", 1.0), 0.0]; |
| 94 | |
| 95 | let pass = DfPass { |
| 96 | tag: "Shader", |
| 97 | op: DeepFlowOp::Relight, |
| 98 | srcs: [Some(&src.view), None, normal.as_deref(), map.as_ref().map(|m| &*m.view)], |
| 99 | hashes: &[src.content_hash, map.as_ref().map_or(0, |m| m.content_hash)], |
| 100 | size: src.size, |
| 101 | depth: self.node_depth(comp, node, time, Some(src.depth)), |
| 102 | }; |
| 103 | self.df_run(node, frame_i, pass, &fin, &u, aux, enc, stats) |
| 104 | } |
| 105 | |
| 106 | /// `Texture`: samples `Texture` through the UV channels (RG) of `Input`: swap, `UScale` / `VScale` / |
| 107 | /// `UOffset` / `VOffset`, `Rotate90`, then flips; `Input` alpha is the coverage. Without a `Texture` input |
| 108 | /// the image passes through (Blend, EffectMask and Process* still apply). |
| 109 | #[allow(clippy::too_many_arguments)] |
| 110 | pub(in crate::eval) fn eval_texture_tool( |
| 111 | &mut self, |
| 112 | comp: &Comp, |
| 113 | node: &Node, |
| 114 | time: f64, |
| 115 | frame_i: i64, |
| 116 | enc: &mut wgpu::CommandEncoder, |
| 117 | visiting: &mut HashSet<(NodeId, i64)>, |
| 118 | stats: &mut EvalStats, |
| 119 | ) -> CachedTexture { |
| 120 | let Some(src) = self.df_input(comp, node, &INPUT_PORTS, time, enc, visiting, stats) else { |
| 121 | return self.dummy.clone(); |
| 122 | }; |
| 123 | let blend = self.df_blend(comp, node, time); |
| 124 | if blend.0 <= 1e-6 && !blend.1 { |
| 125 | return src; |
| 126 | } |
| 127 | let tex = self.df_input(comp, node, &["Texture", "TextureImage"], time, enc, visiting, stats); |
| 128 | let fin = self.df_fin(comp, node, time, blend, src.size, enc, visiting, stats); |
| 129 | |
| 130 | let n = |key: &str, d: f64| num_any(comp, node, &[key], time, d, &self.expr) as f32; |
| 131 | let flag = |key: &str| b2f(n(key, 0.0) > 0.5); |
| 132 | let mut u = UniformParams::default(); |
| 133 | u.p[0] = [n("UScale", 1.0), n("VScale", 1.0), n("UOffset", 0.0), n("VOffset", 0.0)]; |
| 134 | let edges = enum_idx(comp, node, "Edges", &EDGES, time, 2, &self.expr) as f32; |
| 135 | u.p[1] = [edges, b2f(tex.is_some()), flag("SwapUV0"), flag("Rotate90")]; |
| 136 | u.p[2] = [flag("FlipHoriz"), flag("FlipVert"), 0.0, 0.0]; |
| 137 | |
| 138 | let pass = DfPass { |
| 139 | tag: "Texture", |
| 140 | op: DeepFlowOp::Texture, |
| 141 | srcs: [Some(&src.view), tex.as_ref().map(|t| &*t.view), None, None], |
| 142 | hashes: &[src.content_hash, tex.as_ref().map_or(0, |t| t.content_hash)], |
| 143 | size: src.size, |
| 144 | depth: self.node_depth(comp, node, time, Some(tex.as_ref().map_or(src.depth, |t| src.depth.max(t.depth)))), |
| 145 | }; |
| 146 | self.df_run(node, frame_i, pass, &fin, &u, src.aux.clone(), enc, stats) |
| 147 | } |
| 148 | } |