Skip to content

Commit

Permalink
Ability to pause sim (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasMagnus authored Sep 15, 2022
1 parent 06e7176 commit ad7e6b9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 45 deletions.
68 changes: 40 additions & 28 deletions samples/extensions/khr/conway/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,18 @@ class Conway : public cl::sdk::InteropWindow {
explicit Conway(int width, int height, bool fullscreen,
cl_uint platform_id = 0, cl_uint device_id = 0,
cl_bitfield device_type = CL_DEVICE_TYPE_DEFAULT)
: InteropWindow{
sf::VideoMode(width, height),
"Conway's Game of Life",
fullscreen ? sf::Style::Fullscreen : sf::Style::Default,
sf::ContextSettings{ 0, 0, 0, // Depth, Stencil, AA
3, 3, // OpenGL version
sf::ContextSettings::Attribute::Core },
platform_id,
device_id,
device_type
}
: InteropWindow{ sf::VideoMode(width, height),
"Conway's Game of Life",
fullscreen ? sf::Style::Fullscreen
: sf::Style::Default,
sf::ContextSettings{
0, 0, 0, // Depth, Stencil, AA
3, 3, // OpenGL version
sf::ContextSettings::Attribute::Core },
platform_id,
device_id,
device_type },
animating(true)
{}

protected:
Expand Down Expand Up @@ -88,6 +89,7 @@ class Conway : public cl::sdk::InteropWindow {

DoubleBuffer<cl::ImageGL> cl_images;
cl::vector<cl::Memory> interop_resources;
bool animating;
};

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

void Conway::updateScene()
{
auto conway =
cl::KernelFunctor<cl::ImageGL, cl::ImageGL, cl_float2>{ cl_program,
"conway" };
cl::Event acquire, release;
if (animating)
{
auto conway =
cl::KernelFunctor<cl::ImageGL, cl::ImageGL, cl_float2>{ cl_program,
"conway" };
cl::Event acquire, release;

queue.enqueueAcquireGLObjects(&interop_resources, nullptr, &acquire);
queue.enqueueAcquireGLObjects(&interop_resources, nullptr, &acquire);

conway(cl::EnqueueArgs{ queue, cl::NDRange{ getSize().x, getSize().y } },
cl_images.front, cl_images.back,
cl_float2{ 1.f / getSize().x, 1.f / getSize().y });
conway(
cl::EnqueueArgs{ queue, cl::NDRange{ getSize().x, getSize().y } },
cl_images.front, cl_images.back,
cl_float2{ 1.f / getSize().x, 1.f / getSize().y });

queue.enqueueReleaseGLObjects(&interop_resources, nullptr, &release);
queue.enqueueReleaseGLObjects(&interop_resources, nullptr, &release);

// Wait for all OpenCL commands to finish
if (!cl_khr_gl_event_supported)
cl::finish();
else
release.wait();
// Wait for all OpenCL commands to finish
if (!cl_khr_gl_event_supported)
cl::finish();
else
release.wait();

// Swap front and back buffer handles
std::swap(cl_images.front, cl_images.back);
std::swap(gl_images.front, gl_images.back);
// Swap front and back buffer handles
std::swap(cl_images.front, cl_images.back);
std::swap(gl_images.front, gl_images.back);
}
}

void Conway::render()
Expand Down Expand Up @@ -351,6 +357,12 @@ void Conway::event(const sf::Event& event)
switch (event.type)
{
case sf::Event::Closed: close(); break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Key::Space)
{
animating = !animating;
}
break;
}
}

Expand Down
45 changes: 28 additions & 17 deletions samples/extensions/khr/nbody/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class NBody : public cl::sdk::InteropWindow {
z_abs_range(32.f), mass_min(100.f), mass_max(500.f),
RMB_pressed(false),
dist(std::max({ x_abs_range, y_abs_range, z_abs_range }) * 3), phi(0),
theta(0), needMatrixReset(true)
theta(0), needMatrixReset(true), animating(true)
{}

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

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

void NBody::updateScene()
{
auto nbody = cl::KernelFunctor<cl::BufferGL, cl::BufferGL, cl::Buffer,
cl_uint, cl_float>{ cl_program, "nbody" };
cl::Event acquire, release;
if (animating)
{
auto nbody =
cl::KernelFunctor<cl::BufferGL, cl::BufferGL, cl::Buffer, cl_uint,
cl_float>{ cl_program, "nbody" };
cl::Event acquire, release;

queue.enqueueAcquireGLObjects(&interop_resources, nullptr, &acquire);
queue.enqueueAcquireGLObjects(&interop_resources, nullptr, &acquire);

nbody(cl::EnqueueArgs{ queue, cl::NDRange{ particle_count } },
cl_pos_mass.front, cl_pos_mass.back, velocity_buffer,
static_cast<cl_uint>(particle_count), 0.0001f);
nbody(cl::EnqueueArgs{ queue, cl::NDRange{ particle_count } },
cl_pos_mass.front, cl_pos_mass.back, velocity_buffer,
static_cast<cl_uint>(particle_count), 0.0001f);

queue.enqueueReleaseGLObjects(&interop_resources, nullptr, &release);
queue.enqueueReleaseGLObjects(&interop_resources, nullptr, &release);

// Wait for all OpenCL commands to finish
if (!cl_khr_gl_event_supported)
cl::finish();
else
release.wait();
// Wait for all OpenCL commands to finish
if (!cl_khr_gl_event_supported)
cl::finish();
else
release.wait();

// Swap front and back buffer handles
cl_pos_mass.swap();
gl_pos_mass.swap();
// Swap front and back buffer handles
cl_pos_mass.swap();
gl_pos_mass.swap();
}
}

void NBody::render()
Expand Down Expand Up @@ -388,6 +393,12 @@ void NBody::event(const sf::Event& event)
checkError("glViewport(0, 0, getSize().x, getSize().y)");
needMatrixReset = true; // projection matrix need to be recalculated
break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Key::Space)
{
animating = !animating;
}
break;
case sf::Event::MouseButtonPressed:
if (event.mouseButton.button == sf::Mouse::Button::Right)
{
Expand Down

0 comments on commit ad7e6b9

Please sign in to comment.