Skip to content

Commit 7888fe6

Browse files
Merge pull request #2 from lucascompython/test-tranform-feedback
Test tranform feedback
2 parents 61e3b6d + 13642c3 commit 7888fe6

File tree

11 files changed

+441
-316
lines changed

11 files changed

+441
-316
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ eframe = { version = "0.31", default-features = false, features = [
1313
] }
1414
wgpu = "24"
1515
egui-wgpu = "0.31"
16-
log = "0.4"
16+
log = { version = "0.4", optional = true }
1717
glam = { version = "0.30", features = ["fast-math"] }
1818
bytemuck = "1.22"
1919
rand = { version = "0.9", default-features = false, features = ["small_rng"] }
@@ -27,10 +27,11 @@ mimalloc = { version = "0.1" }
2727
[target.'cfg(target_arch = "wasm32")'.dependencies]
2828
wgpu = { version = "24", features = ["webgl"] }
2929
wasm-bindgen-futures = "0.4"
30+
web-time = "1.1" # TODO: See if I can get rid of this
3031
web-sys = "0.3" # to access the DOM (to hide the loading text)
3132

3233
[features]
33-
logs = ["dep:env_logger"]
34+
logs = ["dep:env_logger", "dep:log"]
3435

3536
[profile.release]
3637
codegen-units = 1 # Allows LLVM to perform better optimization.

src/app.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ use crate::simulation::{ParticleSimulation, SimParams, SimulationMethod};
1010
use egui::epaint::text::{FontInsert, InsertFontFamily};
1111
use glam::Vec3;
1212
use std::collections::HashSet;
13+
#[cfg(not(target_arch = "wasm32"))]
1314
use std::time::Instant;
15+
#[cfg(target_arch = "wasm32")]
16+
use web_time::Instant;
1417

1518
pub struct ParticleApp {
1619
simulation: Box<dyn ParticleSimulation>,
20+
surface_format: wgpu::TextureFormat,
1721
renderer: ParticleRenderer,
1822
camera: Camera,
1923

@@ -94,20 +98,33 @@ impl ParticleApp {
9498
SimulationMethod::TransformFeedback
9599
};
96100

97-
// Create simulation based on method
101+
let surface_format = wgpu_render_state.target_format;
102+
98103
let initial_particles;
99104
let simulation: Box<dyn ParticleSimulation> = match default_method {
100105
SimulationMethod::Cpu => {
101106
initial_particles = 10_000;
102-
Box::new(CpuParticleSimulation::new(device, initial_particles))
107+
Box::new(CpuParticleSimulation::new(
108+
device,
109+
initial_particles,
110+
surface_format,
111+
))
103112
}
104113
SimulationMethod::ComputeShader => {
105114
initial_particles = 1_000_000;
106-
Box::new(ComputeParticleSimulation::new(device, initial_particles))
115+
Box::new(ComputeParticleSimulation::new(
116+
device,
117+
initial_particles,
118+
surface_format,
119+
))
107120
}
108121
SimulationMethod::TransformFeedback => {
109122
initial_particles = 100_000;
110-
Box::new(TransformFeedbackSimulation::new(device, initial_particles))
123+
Box::new(TransformFeedbackSimulation::new(
124+
device,
125+
initial_particles,
126+
surface_format,
127+
))
111128
}
112129
};
113130

@@ -122,6 +139,7 @@ impl ParticleApp {
122139

123140
Self {
124141
simulation,
142+
surface_format,
125143
renderer,
126144
camera,
127145

@@ -161,13 +179,21 @@ impl ParticleApp {
161179

162180
// Create new simulation with the same particle count
163181
self.simulation = match new_method {
164-
SimulationMethod::Cpu => Box::new(CpuParticleSimulation::new(device, current_count)),
165-
SimulationMethod::ComputeShader => {
166-
Box::new(ComputeParticleSimulation::new(device, current_count))
167-
}
168-
SimulationMethod::TransformFeedback => {
169-
Box::new(TransformFeedbackSimulation::new(device, current_count))
170-
}
182+
SimulationMethod::Cpu => Box::new(CpuParticleSimulation::new(
183+
device,
184+
current_count,
185+
self.surface_format,
186+
)),
187+
SimulationMethod::ComputeShader => Box::new(ComputeParticleSimulation::new(
188+
device,
189+
current_count,
190+
self.surface_format,
191+
)),
192+
SimulationMethod::TransformFeedback => Box::new(TransformFeedbackSimulation::new(
193+
device,
194+
current_count,
195+
self.surface_format,
196+
)),
171197
};
172198

173199
self.simulation.set_paused(was_paused);

src/custom_renderer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ impl CallbackTrait for ClonedParticleCallback {
2929
render_pass.set_pipeline(&self.render_pipeline);
3030
render_pass.set_bind_group(0, &self.camera_bind_group, &[]);
3131
render_pass.set_vertex_buffer(0, self.particle_buffer.slice(..));
32+
// TODO: See this
3233
render_pass.draw(0..1, 0..self.num_particles);
3334
}
3435
}

src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
#[global_allocator]
55
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
66

7-
// When compiling natively:
87
#[cfg(not(target_arch = "wasm32"))]
98
fn main() -> eframe::Result {
109
use std::sync::Arc;
1110

1211
#[cfg(feature = "logs")]
13-
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
12+
env_logger::init_from_env(env_logger::Env::new().default_filter_or("debug"));
1413

1514
let native_options = eframe::NativeOptions {
1615
viewport: egui::ViewportBuilder::default()
@@ -60,7 +59,7 @@ fn main() -> eframe::Result {
6059
}),
6160

6261
on_surface_error: Arc::new(|error| {
63-
log::error!("Surface error: {:?}", error);
62+
eprintln!("Surface error: {:?}", error);
6463
egui_wgpu::SurfaceErrorAction::RecreateSurface
6564
}),
6665
},
@@ -75,12 +74,12 @@ fn main() -> eframe::Result {
7574
)
7675
}
7776

78-
// When compiling to web using trunk:
7977
#[cfg(target_arch = "wasm32")]
8078
fn main() {
8179
use eframe::wasm_bindgen::JsCast as _;
8280

8381
// Redirect `log` message to `console.log` and friends:
82+
#[cfg(feature = "logs")]
8483
eframe::WebLogger::init(log::LevelFilter::Debug).ok();
8584

8685
let web_options = eframe::WebOptions::default();

src/renderer.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,19 @@ impl ParticleRenderer {
7272
entry_point: Some("fs_main"),
7373
targets: &[Some(wgpu::ColorTargetState {
7474
format: *surface_format,
75-
blend: Some(wgpu::BlendState {
76-
color: wgpu::BlendComponent {
77-
src_factor: wgpu::BlendFactor::SrcAlpha,
78-
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
79-
operation: wgpu::BlendOperation::Add,
80-
},
81-
alpha: wgpu::BlendComponent {
82-
src_factor: wgpu::BlendFactor::One,
83-
dst_factor: wgpu::BlendFactor::One,
84-
operation: wgpu::BlendOperation::Add,
85-
},
86-
}),
75+
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
8776
write_mask: wgpu::ColorWrites::ALL,
8877
})],
8978
compilation_options: Default::default(),
9079
}),
9180
primitive: wgpu::PrimitiveState {
9281
topology: wgpu::PrimitiveTopology::PointList,
93-
strip_index_format: None,
94-
front_face: wgpu::FrontFace::Ccw,
95-
cull_mode: Some(wgpu::Face::Back),
96-
polygon_mode: wgpu::PolygonMode::Fill,
97-
unclipped_depth: false,
98-
conservative: false,
82+
..Default::default() // strip_index_format: None,
83+
// front_face: wgpu::FrontFace::Ccw,
84+
// cull_mode: Some(wgpu::Face::Back),
85+
// polygon_mode: wgpu::PolygonMode::Fill,
86+
// unclipped_depth: false,
87+
// conservative: false,
9988
},
10089
depth_stencil: None,
10190
multisample: wgpu::MultisampleState {

0 commit comments

Comments
 (0)