Skip to content

Commit ad7e6b9

Browse files
Ability to pause sim (#52)
1 parent 06e7176 commit ad7e6b9

2 files changed

Lines changed: 68 additions & 45 deletions

File tree

samples/extensions/khr/conway/main.cpp

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,18 @@ class Conway : public cl::sdk::InteropWindow {
4646
explicit Conway(int width, int height, bool fullscreen,
4747
cl_uint platform_id = 0, cl_uint device_id = 0,
4848
cl_bitfield device_type = CL_DEVICE_TYPE_DEFAULT)
49-
: InteropWindow{
50-
sf::VideoMode(width, height),
51-
"Conway's Game of Life",
52-
fullscreen ? sf::Style::Fullscreen : sf::Style::Default,
53-
sf::ContextSettings{ 0, 0, 0, // Depth, Stencil, AA
54-
3, 3, // OpenGL version
55-
sf::ContextSettings::Attribute::Core },
56-
platform_id,
57-
device_id,
58-
device_type
59-
}
49+
: InteropWindow{ sf::VideoMode(width, height),
50+
"Conway's Game of Life",
51+
fullscreen ? sf::Style::Fullscreen
52+
: sf::Style::Default,
53+
sf::ContextSettings{
54+
0, 0, 0, // Depth, Stencil, AA
55+
3, 3, // OpenGL version
56+
sf::ContextSettings::Attribute::Core },
57+
platform_id,
58+
device_id,
59+
device_type },
60+
animating(true)
6061
{}
6162

6263
protected:
@@ -88,6 +89,7 @@ class Conway : public cl::sdk::InteropWindow {
8889

8990
DoubleBuffer<cl::ImageGL> cl_images;
9091
cl::vector<cl::Memory> interop_resources;
92+
bool animating;
9193
};
9294

9395
inline bool checkError(const char* Title)
@@ -301,28 +303,32 @@ void Conway::initializeCL()
301303

302304
void Conway::updateScene()
303305
{
304-
auto conway =
305-
cl::KernelFunctor<cl::ImageGL, cl::ImageGL, cl_float2>{ cl_program,
306-
"conway" };
307-
cl::Event acquire, release;
306+
if (animating)
307+
{
308+
auto conway =
309+
cl::KernelFunctor<cl::ImageGL, cl::ImageGL, cl_float2>{ cl_program,
310+
"conway" };
311+
cl::Event acquire, release;
308312

309-
queue.enqueueAcquireGLObjects(&interop_resources, nullptr, &acquire);
313+
queue.enqueueAcquireGLObjects(&interop_resources, nullptr, &acquire);
310314

311-
conway(cl::EnqueueArgs{ queue, cl::NDRange{ getSize().x, getSize().y } },
312-
cl_images.front, cl_images.back,
313-
cl_float2{ 1.f / getSize().x, 1.f / getSize().y });
315+
conway(
316+
cl::EnqueueArgs{ queue, cl::NDRange{ getSize().x, getSize().y } },
317+
cl_images.front, cl_images.back,
318+
cl_float2{ 1.f / getSize().x, 1.f / getSize().y });
314319

315-
queue.enqueueReleaseGLObjects(&interop_resources, nullptr, &release);
320+
queue.enqueueReleaseGLObjects(&interop_resources, nullptr, &release);
316321

317-
// Wait for all OpenCL commands to finish
318-
if (!cl_khr_gl_event_supported)
319-
cl::finish();
320-
else
321-
release.wait();
322+
// Wait for all OpenCL commands to finish
323+
if (!cl_khr_gl_event_supported)
324+
cl::finish();
325+
else
326+
release.wait();
322327

323-
// Swap front and back buffer handles
324-
std::swap(cl_images.front, cl_images.back);
325-
std::swap(gl_images.front, gl_images.back);
328+
// Swap front and back buffer handles
329+
std::swap(cl_images.front, cl_images.back);
330+
std::swap(gl_images.front, gl_images.back);
331+
}
326332
}
327333

328334
void Conway::render()
@@ -351,6 +357,12 @@ void Conway::event(const sf::Event& event)
351357
switch (event.type)
352358
{
353359
case sf::Event::Closed: close(); break;
360+
case sf::Event::KeyPressed:
361+
if (event.key.code == sf::Keyboard::Key::Space)
362+
{
363+
animating = !animating;
364+
}
365+
break;
354366
}
355367
}
356368

samples/extensions/khr/nbody/main.cpp

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class NBody : public cl::sdk::InteropWindow {
6464
z_abs_range(32.f), mass_min(100.f), mass_max(500.f),
6565
RMB_pressed(false),
6666
dist(std::max({ x_abs_range, y_abs_range, z_abs_range }) * 3), phi(0),
67-
theta(0), needMatrixReset(true)
67+
theta(0), needMatrixReset(true), animating(true)
6868
{}
6969

7070
protected:
@@ -115,6 +115,7 @@ class NBody : public cl::sdk::InteropWindow {
115115
sf::Vector2<int> mousePos; // Variables to enable dragging
116116
float dist, phi, theta; // Mouse polar coordinates
117117
bool needMatrixReset; // Whether matrices need to be reset in shaders
118+
bool animating;
118119

119120
void
120121
mouseDrag(const sf::Event::MouseMoveEvent& event); // Handle mouse dragging
@@ -324,27 +325,31 @@ void NBody::initializeCL()
324325

325326
void NBody::updateScene()
326327
{
327-
auto nbody = cl::KernelFunctor<cl::BufferGL, cl::BufferGL, cl::Buffer,
328-
cl_uint, cl_float>{ cl_program, "nbody" };
329-
cl::Event acquire, release;
328+
if (animating)
329+
{
330+
auto nbody =
331+
cl::KernelFunctor<cl::BufferGL, cl::BufferGL, cl::Buffer, cl_uint,
332+
cl_float>{ cl_program, "nbody" };
333+
cl::Event acquire, release;
330334

331-
queue.enqueueAcquireGLObjects(&interop_resources, nullptr, &acquire);
335+
queue.enqueueAcquireGLObjects(&interop_resources, nullptr, &acquire);
332336

333-
nbody(cl::EnqueueArgs{ queue, cl::NDRange{ particle_count } },
334-
cl_pos_mass.front, cl_pos_mass.back, velocity_buffer,
335-
static_cast<cl_uint>(particle_count), 0.0001f);
337+
nbody(cl::EnqueueArgs{ queue, cl::NDRange{ particle_count } },
338+
cl_pos_mass.front, cl_pos_mass.back, velocity_buffer,
339+
static_cast<cl_uint>(particle_count), 0.0001f);
336340

337-
queue.enqueueReleaseGLObjects(&interop_resources, nullptr, &release);
341+
queue.enqueueReleaseGLObjects(&interop_resources, nullptr, &release);
338342

339-
// Wait for all OpenCL commands to finish
340-
if (!cl_khr_gl_event_supported)
341-
cl::finish();
342-
else
343-
release.wait();
343+
// Wait for all OpenCL commands to finish
344+
if (!cl_khr_gl_event_supported)
345+
cl::finish();
346+
else
347+
release.wait();
344348

345-
// Swap front and back buffer handles
346-
cl_pos_mass.swap();
347-
gl_pos_mass.swap();
349+
// Swap front and back buffer handles
350+
cl_pos_mass.swap();
351+
gl_pos_mass.swap();
352+
}
348353
}
349354

350355
void NBody::render()
@@ -388,6 +393,12 @@ void NBody::event(const sf::Event& event)
388393
checkError("glViewport(0, 0, getSize().x, getSize().y)");
389394
needMatrixReset = true; // projection matrix need to be recalculated
390395
break;
396+
case sf::Event::KeyPressed:
397+
if (event.key.code == sf::Keyboard::Key::Space)
398+
{
399+
animating = !animating;
400+
}
401+
break;
391402
case sf::Event::MouseButtonPressed:
392403
if (event.mouseButton.button == sf::Mouse::Button::Right)
393404
{

0 commit comments

Comments
 (0)