Repository: AmbientRun/Ambient
File: crates/renderer/src/tree_renderer.rs (Rust)

Code before:
                    // not yet finished and we are still using the previous frame's material
                    // counts.
                    let count = count_state
                        .counts()
                        .get(mat.material_index as usize)
                        .copied()
                        .unwrap_or(0);

                    // NOTE: this issues 1 draw call *for every single visible primitive* in the scene
                    tracing::trace!(?count, ?offset, "draw node primitives");
                    for i in 0..count {
                        render_pass.draw_indexed_indirect(
                            collect_state.commands.buffer(),
                            (offset + i as u64) * std::mem::size_of::<DrawIndexedIndirect>() as u64,
                        );
                    }
                } else if self.config.render_mode == RenderMode::Direct {
                    for (i, &(id, primitive_idx)) in mat.primitives.iter().enumerate() {
                        let primitive = &world.get_ref(id, primitives()).unwrap()[primitive_idx];

                        let mesh = mesh_buffer.get_mesh_metadata(&primitive.mesh);
                        let index = offset + i as u64;

                        render_pass.draw_indexed(
                            mesh.index_offset..(mesh.index_offset + mesh.index_count),
                            // mesh.base_offset as i32,
                            0,
                            index as u32..(index as u32 + 1),
                        )
                    }
                }
            }
        }
    }

    pub fn n_entities(&self) -> usize {
        self.tree.values().fold(0, |p, n| p + n.n_entities())
    }

    pub fn n_nodes(&self) -> usize {
        self.tree.values().fold(0, |p, n| p + n.n_nodes())
    }

    pub fn dump(&self, f: &mut dyn std::io::Write) {
        for (key, node) in self.tree.iter() {
            writeln!(f, "    shader {key:?}").unwrap();
            node.dump(f);
        }
    }
}
struct ShaderNode {

Review comment (suggestion):
Strange.

Adding `mesh.base_offset` makes the direct render mode work on the web, but stops it working on native (gray screen).

Doing 0 yields the other way around

🤔 

Code after:
                    // not yet finished and we are still using the previous frame's material
                    // counts.
                    let count = count_state
                        .counts()
                        .get(mat.material_index as usize)
                        .copied()
                        .unwrap_or(0);

                    // NOTE: this issues 1 draw call *for every single visible primitive* in the scene
                    tracing::trace!(?count, ?offset, "draw node primitives");
                    for i in 0..count {
                        render_pass.draw_indexed_indirect(
                            collect_state.commands.buffer(),
                            (offset + i as u64) * std::mem::size_of::<DrawIndexedIndirect>() as u64,
                        );
                    }
                } else if self.config.render_mode == RenderMode::Direct {
                    tracing::info!("Using direct renderer");
                    for (i, &(id, primitive_idx)) in mat.primitives.iter().enumerate() {
                        let primitive = &world.get_ref(id, primitives()).unwrap()[primitive_idx];

                        let mesh = mesh_buffer.get_mesh_metadata(&primitive.mesh);
                        let index = offset + i as u64;

                        render_pass.draw_indexed(
                            mesh.index_offset..(mesh.index_offset + mesh.index_count),
                            // This was previously `0` and worked for Windows, MacOs, and MacOS + Chrome.
                            // Changing this to `mesh.base_offset` makes it works on the web using Windows + Chrome, but breaks it for MacOs + Chrome
                            // The indirect commands in `collect.wgsl` use an offset of 0
                            //
                            // The correct value should be `0`, but as said, breaks Windows +
                            // Chrome
                            mesh.base_offset as i32,
                            // 0,
                            index as u32..(index as u32 + 1),
                        )
                    }
                }
            }
        }
    }

    pub fn n_entities(&self) -> usize {
        self.tree.values().fold(0, |p, n| p + n.n_entities())
    }

    pub fn n_nodes(&self) -> usize {
        self.tree.values().fold(0, |p, n| p + n.n_nodes())
    }

    pub fn dump(&self, f: &mut dyn std::io::Write) {