14
14
#undef min
15
15
#undef max
16
16
#define CAMERA_FAR_PLANE 1000 .0f
17
- #define MAX_PARTICLES 100
17
+ #define MAX_PARTICLES 128
18
18
#define GRADIENT_SAMPLES 32
19
19
#define LOCAL_SIZE 32
20
20
@@ -86,6 +86,9 @@ class GPUParticleSystem : public dw::Application
86
86
m_debug_draw.grid (m_main_camera->m_view_projection , 1 .0f , 10 .0f );
87
87
88
88
m_debug_draw.render (nullptr , m_width, m_height, m_main_camera->m_view_projection , m_main_camera->m_position );
89
+
90
+ m_pre_sim_idx = m_pre_sim_idx == 0 ? 1 : 0 ;
91
+ m_post_sim_idx = m_post_sim_idx == 0 ? 1 : 0 ;
89
92
}
90
93
91
94
// -----------------------------------------------------------------------------------------------------------------------------------
@@ -183,6 +186,7 @@ class GPUParticleSystem : public dw::Application
183
186
{
184
187
ImGui::InputFloat (" Rotation" , &m_rotation);
185
188
ImGui::InputFloat3 (" Position" , &m_position.x );
189
+ ImGui::SliderFloat3 (" Velocity" , &m_max_velocity.x , 0 .1f , 5 .0f );
186
190
}
187
191
188
192
// -----------------------------------------------------------------------------------------------------------------------------------
@@ -198,7 +202,7 @@ class GPUParticleSystem : public dw::Application
198
202
m_particle_program->set_uniform (" u_Proj" , m_main_camera->m_projection );
199
203
200
204
m_particle_data_ssbo->bind_base (0 );
201
- m_alive_indices_post_sim_ssbo ->bind_base (1 );
205
+ m_alive_indices_ssbo[m_post_sim_idx] ->bind_base (1 );
202
206
203
207
glBindBuffer (GL_DRAW_INDIRECT_BUFFER, m_draw_indirect_args_ssbo->handle ());
204
208
@@ -212,8 +216,7 @@ class GPUParticleSystem : public dw::Application
212
216
m_particle_initialize_program->use ();
213
217
214
218
m_dead_indices_ssbo->bind_base (0 );
215
- m_alive_indices_pre_sim_ssbo->bind_base (1 );
216
- m_counters_ssbo->bind_base (2 );
219
+ m_counters_ssbo->bind_base (1 );
217
220
218
221
m_particle_initialize_program->set_uniform (" u_MaxParticles" , MAX_PARTICLES);
219
222
@@ -230,7 +233,9 @@ class GPUParticleSystem : public dw::Application
230
233
231
234
int32_t rate = glm::max (1 , int32_t (float (m_emission_rate) * float (m_delta_seconds)));
232
235
m_particle_update_kickoff_program->set_uniform (" u_ParticlesPerFrame" , rate);
233
-
236
+ m_particle_update_kickoff_program->set_uniform (" u_PreSimIdx" , m_pre_sim_idx);
237
+ m_particle_update_kickoff_program->set_uniform (" u_PostSimIdx" , m_post_sim_idx);
238
+
234
239
m_particle_data_ssbo->bind_base (0 );
235
240
m_dispatch_emission_indirect_args_ssbo->bind_base (1 );
236
241
m_dispatch_simulation_indirect_args_ssbo->bind_base (2 );
@@ -251,10 +256,11 @@ class GPUParticleSystem : public dw::Application
251
256
m_particle_emission_program->set_uniform (" u_EmitterPosition" , m_position);
252
257
m_particle_emission_program->set_uniform (" u_EmitterVelocity" , m_max_velocity);
253
258
m_particle_emission_program->set_uniform (" u_EmitterLifetime" , m_max_lifetime);
259
+ m_particle_emission_program->set_uniform (" u_PreSimIdx" , m_pre_sim_idx);
254
260
255
261
m_particle_data_ssbo->bind_base (0 );
256
262
m_dead_indices_ssbo->bind_base (1 );
257
- m_alive_indices_pre_sim_ssbo ->bind_base (2 );
263
+ m_alive_indices_ssbo[m_pre_sim_idx] ->bind_base (2 );
258
264
m_counters_ssbo->bind_base (3 );
259
265
260
266
glBindBuffer (GL_DISPATCH_INDIRECT_BUFFER, m_dispatch_emission_indirect_args_ssbo->handle ());
@@ -271,19 +277,21 @@ class GPUParticleSystem : public dw::Application
271
277
m_particle_simulation_program->use ();
272
278
273
279
m_particle_simulation_program->set_uniform (" u_DeltaTime" , float (m_delta_seconds));
280
+ m_particle_simulation_program->set_uniform (" u_PreSimIdx" , m_pre_sim_idx);
281
+ m_particle_simulation_program->set_uniform (" u_PostSimIdx" , m_post_sim_idx);
274
282
275
283
m_particle_data_ssbo->bind_base (0 );
276
284
m_dead_indices_ssbo->bind_base (1 );
277
- m_alive_indices_pre_sim_ssbo ->bind_base (2 );
278
- m_alive_indices_post_sim_ssbo ->bind_base (3 );
285
+ m_alive_indices_ssbo[m_pre_sim_idx] ->bind_base (2 );
286
+ m_alive_indices_ssbo[m_post_sim_idx] ->bind_base (3 );
279
287
m_draw_indirect_args_ssbo->bind_base (4 );
280
288
m_counters_ssbo->bind_base (5 );
281
289
282
290
glBindBuffer (GL_DISPATCH_INDIRECT_BUFFER, m_dispatch_simulation_indirect_args_ssbo->handle ());
283
291
284
292
glDispatchComputeIndirect (0 );
285
293
286
- glMemoryBarrier (GL_SHADER_STORAGE_BARRIER_BIT );
294
+ glMemoryBarrier (GL_ALL_BARRIER_BITS );
287
295
}
288
296
289
297
// -----------------------------------------------------------------------------------------------------------------------------------
@@ -325,7 +333,7 @@ class GPUParticleSystem : public dw::Application
325
333
}
326
334
327
335
// Create general shader program
328
- dw::gl::Shader* shaders[] = { m_particle_initialize_cs.get () };
336
+ dw::gl::Shader* shaders[] = { m_particle_initialize_cs.get () };
329
337
m_particle_initialize_program = std::make_unique<dw::gl::Program>(1 , shaders);
330
338
331
339
if (!m_particle_initialize_program)
@@ -405,14 +413,14 @@ class GPUParticleSystem : public dw::Application
405
413
glm::vec4 color;
406
414
};
407
415
408
- m_draw_indirect_args_ssbo = std::make_unique<dw::gl::ShaderStorageBuffer>(GL_STATIC_DRAW, sizeof (int32_t ) * 4 , nullptr );
409
- m_dispatch_emission_indirect_args_ssbo = std::make_unique<dw::gl::ShaderStorageBuffer>(GL_STATIC_DRAW, sizeof (int32_t ) * 3 , nullptr );
416
+ m_draw_indirect_args_ssbo = std::make_unique<dw::gl::ShaderStorageBuffer>(GL_STATIC_DRAW, sizeof (int32_t ) * 4 , nullptr );
417
+ m_dispatch_emission_indirect_args_ssbo = std::make_unique<dw::gl::ShaderStorageBuffer>(GL_STATIC_DRAW, sizeof (int32_t ) * 3 , nullptr );
410
418
m_dispatch_simulation_indirect_args_ssbo = std::make_unique<dw::gl::ShaderStorageBuffer>(GL_STATIC_DRAW, sizeof (int32_t ) * 3 , nullptr );
411
- m_particle_data_ssbo = std::make_unique<dw::gl::ShaderStorageBuffer>(GL_STATIC_DRAW, sizeof (Particle) * MAX_PARTICLES, nullptr );
412
- m_alive_indices_pre_sim_ssbo = std::make_unique<dw::gl::ShaderStorageBuffer>(GL_STATIC_DRAW, sizeof (int32_t ) * MAX_PARTICLES, nullptr );
413
- m_alive_indices_post_sim_ssbo = std::make_unique<dw::gl::ShaderStorageBuffer>(GL_STATIC_DRAW, sizeof (int32_t ) * MAX_PARTICLES, nullptr );
414
- m_dead_indices_ssbo = std::make_unique<dw::gl::ShaderStorageBuffer>(GL_STATIC_DRAW, sizeof (int32_t ) * MAX_PARTICLES, nullptr );
415
- m_counters_ssbo = std::make_unique<dw::gl::ShaderStorageBuffer>(GL_STATIC_DRAW, sizeof (int32_t ) * 5 , nullptr );
419
+ m_particle_data_ssbo = std::make_unique<dw::gl::ShaderStorageBuffer>(GL_STATIC_DRAW, sizeof (Particle) * MAX_PARTICLES, nullptr );
420
+ m_alive_indices_ssbo[ 0 ] = std::make_unique<dw::gl::ShaderStorageBuffer>(GL_STATIC_DRAW, sizeof (int32_t ) * MAX_PARTICLES, nullptr );
421
+ m_alive_indices_ssbo[ 1 ] = std::make_unique<dw::gl::ShaderStorageBuffer>(GL_STATIC_DRAW, sizeof (int32_t ) * MAX_PARTICLES, nullptr );
422
+ m_dead_indices_ssbo = std::make_unique<dw::gl::ShaderStorageBuffer>(GL_STATIC_DRAW, sizeof (int32_t ) * MAX_PARTICLES, nullptr );
423
+ m_counters_ssbo = std::make_unique<dw::gl::ShaderStorageBuffer>(GL_STATIC_DRAW, sizeof (int32_t ) * 5 , nullptr );
416
424
417
425
return true ;
418
426
}
@@ -487,8 +495,7 @@ class GPUParticleSystem : public dw::Application
487
495
std::unique_ptr<dw::gl::ShaderStorageBuffer> m_dispatch_emission_indirect_args_ssbo;
488
496
std::unique_ptr<dw::gl::ShaderStorageBuffer> m_dispatch_simulation_indirect_args_ssbo;
489
497
std::unique_ptr<dw::gl::ShaderStorageBuffer> m_particle_data_ssbo;
490
- std::unique_ptr<dw::gl::ShaderStorageBuffer> m_alive_indices_pre_sim_ssbo;
491
- std::unique_ptr<dw::gl::ShaderStorageBuffer> m_alive_indices_post_sim_ssbo;
498
+ std::unique_ptr<dw::gl::ShaderStorageBuffer> m_alive_indices_ssbo[2 ];
492
499
std::unique_ptr<dw::gl::ShaderStorageBuffer> m_dead_indices_ssbo;
493
500
std::unique_ptr<dw::gl::ShaderStorageBuffer> m_counters_ssbo;
494
501
@@ -514,16 +521,18 @@ class GPUParticleSystem : public dw::Application
514
521
515
522
// Particle settings
516
523
uint32_t m_max_active_particles = 0 ; // Max Lifetime * Emission Rate
517
- uint32_t m_emission_rate = 100 ; // Particles per second
524
+ uint32_t m_emission_rate = 100 ; // Particles per second
518
525
float m_min_lifetime = 0 .0f ; // Seconds
519
526
float m_max_lifetime = 3 .0f ; // Seconds
520
- glm::vec3 m_min_velocity = glm::vec3(0 .0f );
521
- glm::vec3 m_max_velocity = glm::vec3(0 .0f , 1 .0f , 0 .0f );
527
+ glm::vec3 m_min_velocity = glm::vec3(0 .0f );
528
+ glm::vec3 m_max_velocity = glm::vec3(0 .0f , 2 .0f , 0 .0f );
522
529
bool m_affected_by_gravity = false ;
523
530
PropertyChangeType m_color_mode = PROPERTY_CONSTANT;
524
531
PropertyChangeType m_scale_mode = PROPERTY_CONSTANT;
525
532
glm::vec3 m_position = glm::vec3(0 .0f );
526
533
float m_rotation = 0 .0f ;
534
+ int32_t m_pre_sim_idx = 0 ;
535
+ int32_t m_post_sim_idx = 1 ;
527
536
};
528
537
529
538
DW_DECLARE_MAIN (GPUParticleSystem)
0 commit comments