Compare commits

..

No commits in common. 'main' and 'v1.3' have entirely different histories.
main ... v1.3

  1. 9
      src/GLWidget.cpp
  2. 4
      src/GLWidget.h
  3. 9
      src/MainWindow.cpp
  4. 2
      src/MainWindow.h
  5. 102
      src/Simulation.cpp
  6. 12
      src/Simulation.h
  7. 5
      src/main.cpp

@ -46,7 +46,6 @@ void GLWidget::initializeGL() {
simulation->semaphore.acquire(); simulation->semaphore.acquire();
uploadStaticDataToGPU(); uploadStaticDataToGPU();
makeCurrent();
overlay->init(); overlay->init();
} }
@ -103,8 +102,6 @@ bool GLWidget::AnyDialogOpen() {
} }
void GLWidget::uploadStaticDataToGPU() { void GLWidget::uploadStaticDataToGPU() {
makeCurrent();
auto pendula = &simulation->pendula; auto pendula = &simulation->pendula;
size_t pointCount = std::transform_reduce(pendula->begin(), pendula->end(), 0, [](size_t prev, size_t curr){ size_t pointCount = std::transform_reduce(pendula->begin(), pendula->end(), 0, [](size_t prev, size_t curr){
@ -186,14 +183,10 @@ void GLWidget::uploadStaticDataToGPU() {
glBindVertexArray(0); glBindVertexArray(0);
doneCurrent();
simulation->semaphore.release(); simulation->semaphore.release();
} }
void GLWidget::changePosition() { void GLWidget::changePosition() {
makeCurrent();
auto t1 = system_clock::now(); auto t1 = system_clock::now();
glBindBuffer(GL_ARRAY_BUFFER, positionVBO); glBindBuffer(GL_ARRAY_BUFFER, positionVBO);
auto positions = (GLfloat*) glMapBufferRange(GL_ARRAY_BUFFER, 0, GLsizeiptr(positionCount * sizeof(float)), GL_MAP_WRITE_BIT); auto positions = (GLfloat*) glMapBufferRange(GL_ARRAY_BUFFER, 0, GLsizeiptr(positionCount * sizeof(float)), GL_MAP_WRITE_BIT);
@ -208,8 +201,6 @@ void GLWidget::changePosition() {
} }
glUnmapBuffer(GL_ARRAY_BUFFER); glUnmapBuffer(GL_ARRAY_BUFFER);
// std::cout << duration_cast<microseconds>(system_clock::now() - t1).count() << std::endl; // std::cout << duration_cast<microseconds>(system_clock::now() - t1).count() << std::endl;
doneCurrent();
simulation->semaphore.release(); simulation->semaphore.release();
} }

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <QOpenGLWidget> #include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core> #include <QOpenGLExtraFunctions>
#include <QMatrix3x3> #include <QMatrix3x3>
class Pendulum; class Pendulum;
@ -9,7 +9,7 @@ class Simulation;
class QOpenGLShaderProgram; class QOpenGLShaderProgram;
class Overlay; class Overlay;
class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core { class GLWidget : public QOpenGLWidget, protected QOpenGLExtraFunctions {
public: public:
explicit GLWidget(Simulation *); explicit GLWidget(Simulation *);
Overlay * overlay; Overlay * overlay;

@ -18,14 +18,13 @@
#include <QTimer> #include <QTimer>
#include "Overlay.h" #include "Overlay.h"
#include "Slider.h" #include "Slider.h"
#include <QOffscreenSurface>
MainWindow::MainWindow() { MainWindow::MainWindow() {
simulationThread = new QThread(this); simulationThread = new QThread(this);
simulationThread->setObjectName("Simulation"); simulationThread->setObjectName("Simulation");
simulation = new Simulation; simulation = new Simulation;
simulation->moveToThread(simulationThread); simulation->moveToThread(simulationThread);
connect(simulationThread, &QThread::started, simulation, &Simulation::initialize); simulationThread->start();
masses = std::vector<double>(MaxSegments); masses = std::vector<double>(MaxSegments);
lengths = std::vector<double>(MaxSegments); lengths = std::vector<double>(MaxSegments);
@ -37,9 +36,7 @@ MainWindow::MainWindow() {
normalizeLengths(); normalizeLengths();
auto offSurface = new QOffscreenSurface; QMetaObject::invokeMethod(simulation, &Simulation::initialize);
offSurface->create();
simulation->offSurface = offSurface;
} }
void MainWindow::buildUI() { void MainWindow::buildUI() {
@ -386,13 +383,13 @@ QWidget * MainWindow::buildSimulationUI() {
// GPU-Acceleration // GPU-Acceleration
{ {
auto useGPU = new QCheckBox("Use GPU-Acceleration"); auto useGPU = new QCheckBox("Use GPU-Acceleration");
useGPU->setCheckState(Qt::Checked);
connect(useGPU, &QCheckBox::stateChanged, simulation, &Simulation::useGPUChanged); connect(useGPU, &QCheckBox::stateChanged, simulation, &Simulation::useGPUChanged);
connect(simulation, &Simulation::gpuNotSupported, [useGPU](const std::string &message){ connect(simulation, &Simulation::gpuNotSupported, [useGPU](const std::string &message){
useGPU->setCheckState(Qt::Unchecked); useGPU->setCheckState(Qt::Unchecked);
useGPU->setEnabled(false); useGPU->setEnabled(false);
useGPU->setToolTip(QString::fromStdString(message)); useGPU->setToolTip(QString::fromStdString(message));
}); });
useGPU->setCheckState(Qt::Unchecked);
lyt->addWidget(useGPU); lyt->addWidget(useGPU);
} }

@ -15,12 +15,12 @@ class MainWindow : public QWidget {
public: public:
explicit MainWindow(); explicit MainWindow();
~MainWindow() override; ~MainWindow() override;
QThread * simulationThread;
private: private:
void buildUI(); void buildUI();
QWidget * buildAddUI(); QWidget * buildAddUI();
QWidget * buildSimulationUI(); QWidget * buildSimulationUI();
Simulation * simulation; Simulation * simulation;
QThread * simulationThread;
GLWidget * glWidget = nullptr; GLWidget * glWidget = nullptr;

@ -23,19 +23,15 @@ void Simulation::initialize() {
// OpenGL Offscreen functions // OpenGL Offscreen functions
{ {
context = new QOpenGLContext; auto context = new QOpenGLContext(this);
auto format = QSurfaceFormat::defaultFormat(); context->setFormat(QSurfaceFormat::defaultFormat());
format.setVersion(4, 3);
context->setFormat(format);
context->create(); context->create();
context->makeCurrent(offSurface); auto surface = new QOffscreenSurface(nullptr, this);
f = new QOpenGLFunctions_4_3_Core; surface->setFormat(context->format());
if (!f->initializeOpenGLFunctions()){ surface->create();
std::string message = "OpenGL 4.3 is required for Compute Shaders! Falling back to CPU-Multithreading."; context->makeCurrent(surface);
printf("%s\n", message.c_str()); initializeOpenGLFunctions();
useGPUAcceleration = false; if (!context->hasExtension("GL_ARB_gpu_shader_fp64")){
emit gpuNotSupported(message);
} else if (!context->hasExtension("GL_ARB_gpu_shader_fp64")){
std::string message = "Double precision not supported by OpenGL! Falling back to CPU-Multithreading."; std::string message = "Double precision not supported by OpenGL! Falling back to CPU-Multithreading.";
printf("%s\n", message.c_str()); printf("%s\n", message.c_str());
useGPUAcceleration = false; useGPUAcceleration = false;
@ -43,19 +39,13 @@ void Simulation::initialize() {
} }
} }
if (!useGPUAcceleration){
context->doneCurrent();
return;
}
// Read GPU Limits // Read GPU Limits
{ {
for (int i = 0; i < 3; i++){ for (int i = 0; i < 3; i++){
f->glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, i, gpuLimits.maxWGCount + i); glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, i, gpuLimits.maxWGCount + i);
f->glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, i, gpuLimits.maxWGSize + i); glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, i, gpuLimits.maxWGSize + i);
} }
f->glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &gpuLimits.maxWGInvocations); glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &gpuLimits.maxWGInvocations);
printf("Max work group count: (%d, %d, %d)\n", gpuLimits.maxWGCount[0], gpuLimits.maxWGCount[1], gpuLimits.maxWGCount[2]); printf("Max work group count: (%d, %d, %d)\n", gpuLimits.maxWGCount[0], gpuLimits.maxWGCount[1], gpuLimits.maxWGCount[2]);
printf("Max work group size: (%d, %d, %d)\n", gpuLimits.maxWGSize[0], gpuLimits.maxWGSize[1], gpuLimits.maxWGSize[2]); printf("Max work group size: (%d, %d, %d)\n", gpuLimits.maxWGSize[0], gpuLimits.maxWGSize[1], gpuLimits.maxWGSize[2]);
@ -68,22 +58,19 @@ void Simulation::initialize() {
program->addShaderFromSourceFile(QOpenGLShader::Compute, ":/shaders/compute.glsl"); program->addShaderFromSourceFile(QOpenGLShader::Compute, ":/shaders/compute.glsl");
program->link(); program->link();
f->glGenBuffers(6, &SSBO.positions); glGenBuffers(6, &SSBO.positions);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, SSBO.positions); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, SSBO.positions);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, SSBO.velocities); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, SSBO.velocities);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, SSBO.invMasses); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, SSBO.invMasses);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, SSBO.lengths); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, SSBO.lengths);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, SSBO.indices); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, SSBO.indices);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, SSBO.segmentCounts); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, SSBO.segmentCounts);
} }
context->doneCurrent();
} }
// When the layout changes // When the layout changes
void Simulation::updateGPUData() { void Simulation::updateGPUData() {
context->makeCurrent(offSurface);
std::vector<GLdouble> positions; std::vector<GLdouble> positions;
std::vector<GLdouble> velocities; std::vector<GLdouble> velocities;
std::vector<GLdouble> invMasses; std::vector<GLdouble> invMasses;
@ -113,20 +100,18 @@ void Simulation::updateGPUData() {
auto indicesSize = GLsizeiptr(indices.size() * sizeof(GLuint)); auto indicesSize = GLsizeiptr(indices.size() * sizeof(GLuint));
auto segmentCountsSize = GLsizeiptr(segmentCounts.size() * sizeof(GLuint)); auto segmentCountsSize = GLsizeiptr(segmentCounts.size() * sizeof(GLuint));
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.positions); glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.positions);
f->glBufferData(GL_SHADER_STORAGE_BUFFER, posSize, positions.data(), GL_DYNAMIC_DRAW); glBufferData(GL_SHADER_STORAGE_BUFFER, posSize, positions.data(), GL_DYNAMIC_DRAW);
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.velocities); glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.velocities);
f->glBufferData(GL_SHADER_STORAGE_BUFFER, velSize, velocities.data(), GL_DYNAMIC_DRAW); glBufferData(GL_SHADER_STORAGE_BUFFER, velSize, velocities.data(), GL_DYNAMIC_DRAW);
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.invMasses); glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.invMasses);
f->glBufferData(GL_SHADER_STORAGE_BUFFER, invMassSize, invMasses.data(), GL_STATIC_DRAW); glBufferData(GL_SHADER_STORAGE_BUFFER, invMassSize, invMasses.data(), GL_STATIC_DRAW);
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.lengths); glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.lengths);
f->glBufferData(GL_SHADER_STORAGE_BUFFER, lengthSize, lengths.data(), GL_STATIC_DRAW); glBufferData(GL_SHADER_STORAGE_BUFFER, lengthSize, lengths.data(), GL_STATIC_DRAW);
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.indices); glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.indices);
f->glBufferData(GL_SHADER_STORAGE_BUFFER, indicesSize, indices.data(), GL_STATIC_DRAW); glBufferData(GL_SHADER_STORAGE_BUFFER, indicesSize, indices.data(), GL_STATIC_DRAW);
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.segmentCounts); glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.segmentCounts);
f->glBufferData(GL_SHADER_STORAGE_BUFFER, segmentCountsSize, segmentCounts.data(), GL_STATIC_DRAW); glBufferData(GL_SHADER_STORAGE_BUFFER, segmentCountsSize, segmentCounts.data(), GL_STATIC_DRAW);
context->doneCurrent();
} }
// Every few milliseconds // Every few milliseconds
@ -144,19 +129,18 @@ void Simulation::update() {
double newKineticEnergy = 0; double newKineticEnergy = 0;
if (useGPUAcceleration) { if (useGPUAcceleration) {
context->makeCurrent(offSurface);
program->bind(); program->bind();
f->glUniform1d(program->uniformLocation("h"), h); glUniform1d(program->uniformLocation("h"), h);
f->glUniform1d(program->uniformLocation("gravity"), gravity); glUniform1d(program->uniformLocation("gravity"), gravity);
f->glUniform1ui(program->uniformLocation("substeps"), substeps); glUniform1ui(program->uniformLocation("substeps"), substeps);
f->glDispatchCompute(pendula.size(), 1, 1); glDispatchCompute(pendula.size(), 1, 1);
f->glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
// Read updated positions // Read updated positions
{ {
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.positions); glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.positions);
auto * newPositions = (double*) f->glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY); auto * newPositions = (double*) glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY);
int index = 0; int index = 0;
for (Pendulum * p : pendula){ for (Pendulum * p : pendula){
for (Vector &point : p->X){ for (Vector &point : p->X){
@ -164,13 +148,13 @@ void Simulation::update() {
point.y = newPositions[index++]; point.y = newPositions[index++];
} }
} }
f->glUnmapBuffer(GL_SHADER_STORAGE_BUFFER); glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
} }
// Read updated velocities // Read updated velocities
{ {
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.velocities); glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.velocities);
auto * newVelocities = (double*) f->glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY); auto * newVelocities = (double*) glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY);
int index = 0; int index = 0;
for (Pendulum * p : pendula){ for (Pendulum * p : pendula){
for (Vector &velocity : p->V){ for (Vector &velocity : p->V){
@ -178,7 +162,7 @@ void Simulation::update() {
velocity.y = newVelocities[index++]; velocity.y = newVelocities[index++];
} }
} }
f->glUnmapBuffer(GL_SHADER_STORAGE_BUFFER); glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
} }
#pragma omp parallel for #pragma omp parallel for
@ -190,7 +174,7 @@ void Simulation::update() {
#pragma omp atomic #pragma omp atomic
newKineticEnergy += localKineticEnergy; newKineticEnergy += localKineticEnergy;
} }
context->doneCurrent();
} else { } else {
// CPU-Multithreading // CPU-Multithreading
#pragma omp parallel for #pragma omp parallel for
@ -224,7 +208,6 @@ void Simulation::clearPendula() {
pendula.shrink_to_fit(); pendula.shrink_to_fit();
updateEnergy(); updateEnergy();
if (useGPUAcceleration)
updateGPUData(); updateGPUData();
emit layoutChanged(); emit layoutChanged();
@ -237,7 +220,6 @@ void Simulation::addPendula(const std::vector<Pendulum *> &add) {
pendula.push_back(p); pendula.push_back(p);
updateEnergy(); updateEnergy();
if (useGPUAcceleration)
updateGPUData(); updateGPUData();
emit layoutChanged(); emit layoutChanged();

@ -5,13 +5,11 @@
#include <semaphore> #include <semaphore>
#include <QOpenGLFunctions_4_3_Core> #include <QOpenGLFunctions_4_3_Core>
class QOffscreenSurface;
class QTimer;
class FPS; class FPS;
class Pendulum; class Pendulum;
class QOpenGLShaderProgram; class QOpenGLShaderProgram;
class Simulation : public QObject { class Simulation : public QObject, protected QOpenGLFunctions_4_3_Core {
Q_OBJECT Q_OBJECT
public: public:
explicit Simulation(); explicit Simulation();
@ -31,23 +29,19 @@ public:
std::binary_semaphore semaphore = std::binary_semaphore(1); std::binary_semaphore semaphore = std::binary_semaphore(1);
FPS * ups; FPS * ups;
QOffscreenSurface* offSurface; void initialize();
QOpenGLContext* context;
void updateEnergy(); void updateEnergy();
signals: signals:
void gpuNotSupported(const std::string &message); void gpuNotSupported(const std::string &message);
void layoutChanged(); void layoutChanged();
void positionChanged(); void positionChanged();
public slots: public slots:
void initialize();
void clearPendula(); void clearPendula();
void addPendula(const std::vector<Pendulum *> &add); void addPendula(const std::vector<Pendulum *> &add);
void useGPUChanged(int); void useGPUChanged(int);
private slots: private slots:
void update(); void update();
private: private:
QOpenGLFunctions_4_3_Core * f;
void updateGPUData(); void updateGPUData();
struct GPULimits { struct GPULimits {
int maxWGCount[3]; int maxWGCount[3];
@ -65,5 +59,5 @@ private:
} SSBO; } SSBO;
QTimer * timer; QTimer * timer;
int updateInterval = 16; int updateInterval = 16;
bool useGPUAcceleration = true; bool useGPUAcceleration = false;
}; };

@ -2,14 +2,13 @@
#include <QSurfaceFormat> #include <QSurfaceFormat>
#include "MainWindow.h" #include "MainWindow.h"
#include <QIcon> #include <QIcon>
#include <QThread>
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
QSurfaceFormat fmt = QSurfaceFormat::defaultFormat(); QSurfaceFormat fmt = QSurfaceFormat::defaultFormat();
fmt.setDepthBufferSize(24); fmt.setDepthBufferSize(24);
fmt.setSamples(8); fmt.setSamples(8);
fmt.setSwapInterval(1); fmt.setSwapInterval(1);
fmt.setVersion(3, 3); fmt.setVersion(4, 3);
fmt.setProfile(QSurfaceFormat::CoreProfile); fmt.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(fmt); QSurfaceFormat::setDefaultFormat(fmt);
@ -20,7 +19,5 @@ int main(int argc, char* argv[]) {
w.setWindowIcon(QIcon(":/icons/app_icon.ico")); w.setWindowIcon(QIcon(":/icons/app_icon.ico"));
w.show(); w.show();
w.simulationThread->start();
return QApplication::exec(); return QApplication::exec();
} }

Loading…
Cancel
Save