Skip to content

Commit eeea139

Browse files
committed
Bevy's deferred_raymarch, exact: a GBufferPass, drawn in the camera's viewport; cameras and lights placed by their own rotation
shader_advanced/deferred_raymarch (docs/control.feature FC2d) ports with a <GBufferPass> on the camera (systems/phase gbuffer.rs): a shader file's fullscreen fragment writes Bevy's deferred G-buffer, the lighting pass id and depth after the late deferred prepass, and its shadow entry writes every shadow view the camera's lights draw, so Bevy's deferred lighting lights it and it casts shadows. It matches Bevy to the pixel (SSIM 1.000000, 0 px off at frames 5, 10 and 30); the whole suite (147 ports) passes on pixels. Three gaps closed on the way, each found by probing the shader's inputs on both sides: - The pass sets the camera's viewport, as Bevy's own passes do. raea's target is w x (h + 1) (its tag row), so a fullscreen triangle drawn without it stretched its uv over 721 rows: rays a pixel low at the bottom. - Camera3d sends its rotation, and the engine places the camera by it (rot_qx..qw, exact). It used to send a look-at point the engine turned back into a rotation in f32, about a millionth of a radian off. Bevy's lights do the same (their direction was also rounded to 1e-6). - Transform.lookingAt computes in f32 as glam does (Dir3::new, cross, try_normalize, Quat::from_rotation_axes), the same quaternion to the bit; glam.test.ts holds it to glam's own bits. The port sets Bevy's shadow defaults in a <Shadows> (2048 texels, cascades to 150 m, the first to 10 m, overlap 0.2), as the other shadowed ports do. Mutants (no shadow entry, the first cascade at 8 m, 7000 lux, the cube moved 5 cm, the camera's target 1 cm up) are each thousands of pixels off.
1 parent ce63b7a commit eeea139

20 files changed

Lines changed: 396 additions & 62 deletions

File tree

‎bench/bevy-parity/examples.list‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,4 @@ shader_advanced/custom_vertex_attribute
145145
shader_advanced/fullscreen_material
146146
shader_advanced/custom_render_phase
147147
shader_advanced/custom_shader_instancing
148+
shader_advanced/deferred_raymarch

‎bench/bevy-parity/runs.tsv‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,4 @@ shader_advanced/custom_vertex_attribute - 5,10
144144
shader_advanced/fullscreen_material - 10,30,60
145145
shader_advanced/custom_render_phase - 5,10
146146
shader_advanced/custom_shader_instancing - 5,10
147+
shader_advanced/deferred_raymarch - 5,10,30

‎docs/bevy-parity.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Bevy v0.20.0's examples against their raea TSX ports (docs/control.feature FC0a)
44
don't edit. An example passes when every probed frame passes pixels (SSIM 0.98 and no 32 px tile off by more than 24 of 255) and cost (batch keys and
55
frame time at most 1.25x the example's). Wall time is one machine's, measured in the same run.
66

7-
Examples: **438**, ported **145**, passing **145**, excluded **17** (docs/bevy-parity/excluded.tsv), left **276**.
7+
Examples: **438**, ported **146**, passing **146**, excluded **17** (docs/bevy-parity/excluded.tsv), left **275**.
88

99
raea's frame ms is its update's plus the time React takes to step a frame for its 3D (step), which the example
1010
doesn't pay.
@@ -236,6 +236,9 @@ doesn't pay.
236236
| shader_advanced/custom_shader_instancing | 5 | 1.000000 | 0 | 0 / 0 | 442 / 524 | 6 / 8 | 1 / 1 | 45.771 / 4.077 = 4.077 + 0 | yes | |
237237
| shader_advanced/custom_phase_item | 10 | 1.000000 | 0 | 0 / 0 | 442 / 523 | 5 / 7 | 1 / 1 | 4.21 / 1.445 = 1.445 + 0 | yes | |
238238
| shader_advanced/custom_phase_item | 5 | 1.000000 | 0 | 0 / 0 | 442 / 523 | 5 / 7 | 1 / 1 | 54.956 / 2.448 = 2.448 + 0 | yes | |
239+
| shader_advanced/deferred_raymarch | 10 | 1.000000 | 0 | 2 / 2 | 445 / 530 | 7 / 9 | 3 / 3 | 4.989 / 6.005 = 6.005 + 0 | yes | |
240+
| shader_advanced/deferred_raymarch | 30 | 1.000000 | 0 | 2 / 2 | 443 / 530 | 7 / 9 | 3 / 3 | 5.479 / 3.62 = 3.62 + 0 | yes | |
241+
| shader_advanced/deferred_raymarch | 5 | 1.000000 | 0 | 2 / 2 | 443 / 530 | 7 / 9 | 3 / 3 | 59.743 / 8.363 = 8.363 + 0 | yes | |
239242

240243
## Excluded
241244

‎docs/bevy-parity/results.jsonl‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,6 @@
223223
{"example":"shader_advanced/custom_shader_instancing","frame":5,"ssim":1.000000,"tile":0,"bevy":{"batch_keys":0,"entities":442,"extents":[],"frame":5,"frame_ms":45.771,"materials":1,"mesh_entities":1,"meshes":6,"other_material":1,"visible":1},"raea":{"frame":12,"entities":524,"mesh_entities":1,"visible":1,"meshes":8,"materials":1,"batch_keys":0,"other_material":1,"extents":[],"frame_ms":4.077,"step_ms":null,"cost_ms":4.077},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
224224
{"example":"shader_advanced/custom_phase_item","frame":10,"ssim":1.000000,"tile":0,"bevy":{"batch_keys":0,"entities":442,"extents":[],"frame":10,"frame_ms":4.21,"materials":1,"mesh_entities":0,"meshes":5,"other_material":0,"visible":0},"raea":{"frame":17,"entities":523,"mesh_entities":0,"visible":0,"meshes":7,"materials":1,"batch_keys":0,"other_material":0,"extents":[],"frame_ms":1.445,"step_ms":null,"cost_ms":1.445},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
225225
{"example":"shader_advanced/custom_phase_item","frame":5,"ssim":1.000000,"tile":0,"bevy":{"batch_keys":0,"entities":442,"extents":[],"frame":5,"frame_ms":54.956,"materials":1,"mesh_entities":0,"meshes":5,"other_material":0,"visible":0},"raea":{"frame":12,"entities":523,"mesh_entities":0,"visible":0,"meshes":7,"materials":1,"batch_keys":0,"other_material":0,"extents":[],"frame_ms":2.448,"step_ms":null,"cost_ms":2.448},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
226+
{"example":"shader_advanced/deferred_raymarch","frame":10,"ssim":1.000000,"tile":0,"bevy":{"batch_keys":2,"entities":445,"extents":["0.6000 0.6000 0.6000","10.0000 0.0000 10.0000"],"frame":10,"frame_ms":4.989,"materials":3,"mesh_entities":2,"meshes":7,"other_material":0,"visible":2},"raea":{"frame":17,"entities":530,"mesh_entities":2,"visible":2,"meshes":9,"materials":3,"batch_keys":2,"other_material":0,"extents":["0.6000 0.6000 0.6000","10.0000 0.0000 10.0000"],"frame_ms":6.005,"step_ms":null,"cost_ms":6.005},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
227+
{"example":"shader_advanced/deferred_raymarch","frame":30,"ssim":1.000000,"tile":0,"bevy":{"batch_keys":2,"entities":443,"extents":["0.6000 0.6000 0.6000","10.0000 0.0000 10.0000"],"frame":30,"frame_ms":5.479,"materials":3,"mesh_entities":2,"meshes":7,"other_material":0,"visible":2},"raea":{"frame":37,"entities":530,"mesh_entities":2,"visible":2,"meshes":9,"materials":3,"batch_keys":2,"other_material":0,"extents":["0.6000 0.6000 0.6000","10.0000 0.0000 10.0000"],"frame_ms":3.62,"step_ms":null,"cost_ms":3.62},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
228+
{"example":"shader_advanced/deferred_raymarch","frame":5,"ssim":1.000000,"tile":0,"bevy":{"batch_keys":2,"entities":443,"extents":["0.6000 0.6000 0.6000","10.0000 0.0000 10.0000"],"frame":5,"frame_ms":59.743,"materials":3,"mesh_entities":2,"meshes":7,"other_material":0,"visible":2},"raea":{"frame":12,"entities":530,"mesh_entities":2,"visible":2,"meshes":9,"materials":3,"batch_keys":2,"other_material":0,"extents":["0.6000 0.6000 0.6000","10.0000 0.0000 10.0000"],"frame_ms":8.363,"step_ms":null,"cost_ms":8.363},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}

‎runtime/look/src/camera.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ fn one(all: &[Vec<Item>], p: &mut Program, row: &mut BTreeMap<String, V>, k: usi
6565
}
6666
// which way is up (`up`; +Y by default): a camera that rolls, as a solved one does (raea-render's up_x/y/z, Matchmove)
6767
keyed_vec(p, row, id, &its, "up", &["up_x", "up_y", "up_z"], &[0.0, 1.0, 0.0]);
68+
// a Camera3d's Transform rotation (`rotation`, a quaternion on every frame), exact, which the engine places it by over
69+
// its look-at point
70+
if its.iter().all(|c| c.props.get("rotation").and_then(Value::as_array).is_some_and(|q| q.len() == 4)) {
71+
quat_columns(p, row, id, "rot", &its.iter().map(Some).collect::<Vec<_>>());
72+
}
6873
// a window on a larger view (`view`: its full size and this frame's offset, in frame pixels): a crop of
6974
// footage keeps its lens's centre (raea-render's camera view_*, Matchmove's 9:16 from 16:9)
7075
if its.iter().any(|c| c.props.get("view").is_some_and(Value::is_object)) {

‎runtime/look/tests/camera.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,17 @@ fn each_camera_after_the_first_is_a_row_with_its_order_clear_and_viewport() {
3434
assert_eq!([n(second, "order"), n(second, "vp_x"), n(second, "vp_y")], [Some(1.0), Some(640.0), Some(360.0)]);
3535
assert_eq!(n(second, "x"), Some(150.0));
3636
}
37+
38+
#[test]
39+
fn a_camera_with_a_rotation_keeps_it_exactly_and_one_without_has_none() {
40+
// a Camera3d's quaternion as glam made it (Transform::looking_at from (4, 3, 6) to (0, 0.2, 0)), unrounded: the
41+
// engine places the camera by it, not by its look-at point turned back into a rotation in f32
42+
let q = [-0.17622831463813782, 0.28482943773269653, 0.05335763469338417, 0.9407277703285217];
43+
let mut row: BTreeMap<String, V> = [("id".to_string(), V::S("cam".into()))].into();
44+
camera(&[vec![cam(json!({ "position": [4, 3, 6], "lookAt": [0, 0.2, 0], "rotation": q }))]], &mut Program::default(), &mut row);
45+
let got: Vec<Option<f64>> = ["rot_qx", "rot_qy", "rot_qz", "rot_qw"].iter().map(|k| row.get(*k).map(V::num)).collect();
46+
assert_eq!(got, q.map(Some));
47+
let mut plain: BTreeMap<String, V> = [("id".to_string(), V::S("cam".into()))].into();
48+
camera(&[vec![cam(json!({ "position": [4, 3, 6], "lookAt": [0, 0.2, 0], "rotation": [0.1, 0.2, 0.3] }))]], &mut Program::default(), &mut plain);
49+
assert!(!plain.contains_key("rot_qw"), "a rotation that isn't a quaternion places nothing");
50+
}

‎runtime/scene/src/fast.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ impl LiveScene {
117117
put("dir_x", target[0] - pos[0]);
118118
put("dir_y", target[1] - pos[1]);
119119
put("dir_z", target[2] - pos[2]);
120+
if let Some(q) = crate::parts::light_rotation(it) {
121+
for (name, v) in ["rot_qx", "rot_qy", "rot_qz", "rot_qw"].into_iter().zip(q) {
122+
put(name, v);
123+
}
124+
}
120125
}
121126
if it.kind == "spot" {
122127
// its cone, as parts.rs keys it: a spot whose angle moves (3d/spotlight) kept its first (#26)

‎runtime/scene/src/live.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub(crate) const MORPH_COLS: [&str; 64] = ["morph0", "morph1", "morph2", "morph3
5555
/// The columns a frame may change, per kind (what `rows_into` and `camera` key); everything else is the first frame's.
5656
pub(crate) fn live_dynamic(kind: &str) -> &'static [&'static str] {
5757
match kind {
58-
"light" => &["lux", "lumens", "x", "y", "z", "dir_x", "dir_y", "dir_z", "inner", "outer"],
58+
"light" => &["lux", "lumens", "x", "y", "z", "dir_x", "dir_y", "dir_z", "rot_qx", "rot_qy", "rot_qz", "rot_qw", "inner", "outer"],
5959
"mesh" => &["x", "y", "z", "q_x", "q_y", "q_z", "q_w", "scale_x", "scale_y", "scale_z", "visible", "col_r", "col_g", "col_b", "rough", "metal", "emissive_k", "opacity", "sh_r", "sh_g", "sh_b", "rim_k", "spec_k", "outline_w", "pass"],
6060
"model" => &["x", "y", "z", "q_x", "q_y", "q_z", "q_w", "scale_x", "scale_y", "scale_z", "visible", "anim_t", "mix0_t", "mix0_w", "mix1_t", "mix1_w", "mix2_t", "mix2_w", "mix3_t", "mix3_w", "morph0", "morph1", "morph2", "morph3", "morph4", "morph5", "morph6", "morph7", "morph8", "morph9", "morph10", "morph11", "morph12", "morph13", "morph14", "morph15", "morph16", "morph17", "morph18", "morph19", "morph20", "morph21", "morph22", "morph23", "morph24", "morph25", "morph26", "morph27", "morph28", "morph29", "morph30", "morph31", "morph32", "morph33", "morph34", "morph35", "morph36", "morph37", "morph38", "morph39", "morph40", "morph41", "morph42", "morph43", "morph44", "morph45", "morph46", "morph47", "morph48", "morph49", "morph50", "morph51", "morph52", "morph53", "morph54", "morph55", "morph56", "morph57", "morph58", "morph59", "morph60", "morph61", "morph62", "morph63", "g0_w", "g0_t", "g1_w", "g1_t", "g2_w", "g2_t", "g3_w", "g3_t", "g4_w", "g4_t", "g5_w", "g5_t", "g6_w", "g6_t", "g7_w", "g7_t", "g8_w", "g8_t", "g9_w", "g9_t", "g10_w", "g10_t", "g11_w", "g11_t", "g12_w", "g12_t", "g13_w", "g13_t", "g14_w", "g14_t", "g15_w", "g15_t", "look", "glitch_explode", "glitch_scramble", "glitch_jitter", "glitch_snap", "glitch_smear_x", "glitch_smear_y", "glitch_smear_z", "glitch_seed", "cloth_frame", "screen_power", "pass"],
6161
"panel" => &["x", "y", "z", "q_x", "q_y", "q_z", "q_w", "scale_x", "scale_y", "scale_z", "visible", "screen_power", "screen_bright", "pass"],

‎runtime/scene/src/parts.rs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ pub(crate) fn lights_rows(all: &[Vec<Item>], tr: &Tracks, p: &mut Program) {
6868
for (k, name) in ["dir_x", "dir_y", "dir_z"].iter().enumerate() {
6969
column(p, &mut row, &id, name, &pos.iter().zip(&target).map(|(a, b)| b[k] - a[k]).collect::<Vec<_>>());
7070
}
71+
// a Bevy light's own rotation (`rotation`, its Transform's), exact, which the engine places it by over its
72+
// direction: one rounded to 1e-6 and turned back into a rotation in f32 moves a sun's shadow cascades
73+
if its.iter().all(|i| light_rotation(i).is_some()) {
74+
for (k, name) in ["rot_qx", "rot_qy", "rot_qz", "rot_qw"].iter().enumerate() {
75+
exact(p, &mut row, &id, name, &its.iter().map(|i| light_rotation(i).map_or(0.0, |q| q[k])).collect::<Vec<_>>());
76+
}
77+
}
7178
}
7279
let first = &its[0].props;
7380
if let Some(c) = hex(first.get("color")).filter(|c| c != "#ffffff") {
@@ -386,3 +393,27 @@ pub(crate) fn texts_rows(tr: &Tracks, names: &[String], p: &mut Program) {
386393
}
387394
}
388395
}
396+
397+
/// A light's `rotation` quaternion, when it has one and sits in no turned or moved group (one in a group keeps its
398+
/// direction, which its group's transform doesn't turn either).
399+
pub(crate) fn light_rotation(i: &Item) -> Option<[f64; 4]> {
400+
let q = i.props.get("rotation")?.as_array()?;
401+
(q.len() == 4 && i.world == Mat4::IDENTITY).then(|| [0, 1, 2, 3].map(|k| q[k].as_f64().unwrap_or(0.0)))
402+
}
403+
404+
#[cfg(test)]
405+
mod light_rotation_tests {
406+
use super::*;
407+
408+
fn sun(props: serde_json::Value, world: Mat4) -> Item {
409+
Item { kind: "directional", props: std::sync::Arc::new(props.as_object().unwrap().clone()), shape: None, material: None, world }
410+
}
411+
412+
#[test]
413+
fn a_light_keeps_its_quaternion_unless_a_group_moves_it() {
414+
let q = [-0.42470818758010864, 0.33985114097595215, 0.17591990530490875, 0.8204732537269592];
415+
assert_eq!(light_rotation(&sun(serde_json::json!({ "rotation": q }), Mat4::IDENTITY)), Some(q));
416+
assert_eq!(light_rotation(&sun(serde_json::json!({ "rotation": q }), Mat4::from_translation(Vec3::X))), None);
417+
assert_eq!(light_rotation(&sun(serde_json::json!({ "target": [0, 0, 0] }), Mat4::IDENTITY)), None);
418+
}
419+
}

‎runtime/scene3d/src/sync.rs‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,21 @@ pub fn sync_meshes(
199199
/// once, and a row that moves (or moves back) writes every frame it changes. An entity with no entry always writes,
200200
/// so a render seeking straight to frame N places its camera.
201201
#[allow(clippy::type_complexity)]
202-
pub fn sync_camera(frame: Res<FrameRows>, edit: Option<Res<Edit>>, mut placed: Local<bevy::platform::collections::HashMap<Entity, (Vec3, Vec3)>>, mut q: Query<(Entity, &mut Transform, Option<&mut Projection>, Option<&mut AmbientLight>, Option<&mut bevy::post_process::dof::DepthOfField>, Option<&mut super::lens::Lens>, Option<&mut bevy::post_process::effect_stack::ChromaticAberration>, Option<&mut Camera>), (With<Camera3d>, Without<super::BareEntity>)>) {
202+
pub fn sync_camera(frame: Res<FrameRows>, edit: Option<Res<Edit>>, mut placed: Local<bevy::platform::collections::HashMap<Entity, (Vec3, Vec3, Option<Quat>)>>, mut q: Query<(Entity, &mut Transform, Option<&mut Projection>, Option<&mut AmbientLight>, Option<&mut bevy::post_process::dof::DepthOfField>, Option<&mut super::lens::Lens>, Option<&mut bevy::post_process::effect_stack::ChromaticAberration>, Option<&mut Camera>), (With<Camera3d>, Without<super::BareEntity>)>) {
203203
for (e, mut t, proj, amb, dof, lens, ab, cam) in &mut q {
204204
let Some(v) = frame.vals(e) else { continue };
205205
let v = &v;
206206
let pos = Vec3::new(num(v, "x", 0.0), num(v, "y", 2.0), num(v, "z", 9.0));
207207
let look = Vec3::new(num(v, "look_x", 0.0), num(v, "look_y", 1.0), num(v, "look_z", 0.0));
208-
if (look - pos).length_squared() > 1e-12 && placed.get(&e) != Some(&(pos, look)) {
209-
placed.insert(e, (pos, look));
210-
let nt = Transform::from_translation(pos).looking_at(look, up_for(look - pos));
208+
// a Camera3d's own rotation (rot_qx..qw), exactly as its Transform holds it: its look-at point rebuilt into a
209+
// rotation in f32 is a millionth of a radian off, which a raymarch through the view's matrices shows
210+
let rot = row_rotation(v);
211+
if (rot.is_some() || (look - pos).length_squared() > 1e-12) && placed.get(&e) != Some(&(pos, look, rot)) {
212+
placed.insert(e, (pos, look, rot));
213+
let nt = match rot {
214+
Some(r) => Transform::from_translation(pos).with_rotation(r),
215+
None => Transform::from_translation(pos).looking_at(look, up_for(look - pos)),
216+
};
211217
if *t != nt {
212218
*t = nt;
213219
}
@@ -308,8 +314,12 @@ pub fn sync_lights(frame: Res<FrameRows>, mut commands: Commands, mut q: Query<(
308314
let v = &v;
309315
let pos = Vec3::new(num(v, "x", 0.0), num(v, "y", 0.0), num(v, "z", 0.0));
310316
let dir = Vec3::new(num(v, "dir_x", -0.5), num(v, "dir_y", -1.0), num(v, "dir_z", -0.4));
317+
// a Bevy light's own rotation (rot_qx..qw), exactly as its Transform holds it, over its direction turned back
318+
let rot = row_rotation(v);
311319
let nt = if p.is_some() {
312320
Transform::from_translation(pos)
321+
} else if let Some(r) = rot {
322+
Transform::from_translation(if d.is_some() { Vec3::ZERO } else { pos }).with_rotation(r)
313323
} else if s.is_some() || rect.is_some() {
314324
Transform::from_translation(pos).looking_to(dir, up_for(dir))
315325
} else {
@@ -458,3 +468,10 @@ pub fn sync_bones(
458468
}
459469
}
460470
}
471+
472+
/// A row's own rotation (rot_qx..qw: a Camera3d's or a Bevy light's Transform rotation, exact), normalized only between
473+
/// keyed frames, where its components were interpolated.
474+
fn row_rotation(v: &BTreeMap<String, V>) -> Option<Quat> {
475+
let r = v.contains_key("rot_qw").then(|| Quat::from_xyzw(num(v, "rot_qx", 0.0), num(v, "rot_qy", 0.0), num(v, "rot_qz", 0.0), num(v, "rot_qw", 1.0)))?;
476+
Some(if r.is_normalized() { r } else { r.normalize() })
477+
}

0 commit comments

Comments
 (0)