Skip to content

Conversation

Vrixyz
Copy link
Owner

@Vrixyz Vrixyz commented Apr 10, 2025

This allows to have neat-looking demos disclosing a word:

Screenshot from 2025-03-21 14-23-36

Comment on lines +159 to +195
pub mod read_particles {
use super::*;

use bevy::render::{render_resource::BufferUsages, renderer::RenderQueue};
use nalgebra::Vector4;
use wgcore::tensor::GpuVector;
use wgsparkl3d::solver::GpuParticles;

pub fn read_particles_positions(
render_device: RenderDevice,
render_queue: RenderQueue,
particles: &GpuParticles,
) -> Vec<Vector4<f32>> {
// Get the `render_device` is the `Res<RenderDevice>` bevy resource.
let device = render_device.wgpu_device();
// Create the staging buffer.
// Here `particles` is of type `GpuParticles`, accessible from
// `PhysicsContext::data::particles`.
let positions_staging: GpuVector<Vector4<f32>> = GpuVector::uninit(
device,
particles.len() as u32,
BufferUsages::COPY_DST | BufferUsages::MAP_READ,
);

// Copy the buffer.
// `render_queue` the `Res<RenderQueue>` bevy resource.
let compute_queue = &*render_queue.0;
let mut encoder = device.create_command_encoder(&Default::default());
positions_staging.copy_from(&mut encoder, &particles.positions);
compute_queue.submit(Some(encoder.finish()));

// Run the copy. The fourth component of each entry can be ignored.
let positions: Vec<Vector4<f32>> =
futures::executor::block_on(positions_staging.read(device)).unwrap();
positions
}
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be useful as part of the main library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant