|
|
@ -23,15 +23,12 @@ void Simulation::initialize() { |
|
|
|
|
|
|
|
|
|
|
|
// OpenGL Offscreen functions
|
|
|
|
// OpenGL Offscreen functions
|
|
|
|
{ |
|
|
|
{ |
|
|
|
auto context = new QOpenGLContext(this); |
|
|
|
context = new QOpenGLContext; |
|
|
|
auto format = QSurfaceFormat::defaultFormat(); |
|
|
|
auto format = QSurfaceFormat::defaultFormat(); |
|
|
|
format.setVersion(4, 3); |
|
|
|
format.setVersion(4, 3); |
|
|
|
context->setFormat(format); |
|
|
|
context->setFormat(format); |
|
|
|
context->create(); |
|
|
|
context->create(); |
|
|
|
auto surface = new QOffscreenSurface(nullptr, this); |
|
|
|
context->makeCurrent(offSurface); |
|
|
|
surface->setFormat(context->format()); |
|
|
|
|
|
|
|
surface->create(); |
|
|
|
|
|
|
|
context->makeCurrent(surface); |
|
|
|
|
|
|
|
f = new QOpenGLFunctions_4_3_Core; |
|
|
|
f = new QOpenGLFunctions_4_3_Core; |
|
|
|
if (!f->initializeOpenGLFunctions()){ |
|
|
|
if (!f->initializeOpenGLFunctions()){ |
|
|
|
std::string message = "OpenGL 4.3 is required for Compute Shaders! Falling back to CPU-Multithreading."; |
|
|
|
std::string message = "OpenGL 4.3 is required for Compute Shaders! Falling back to CPU-Multithreading."; |
|
|
@ -46,8 +43,11 @@ void Simulation::initialize() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!useGPUAcceleration) |
|
|
|
if (!useGPUAcceleration){ |
|
|
|
return; |
|
|
|
context->doneCurrent(); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Read GPU Limits
|
|
|
|
// Read GPU Limits
|
|
|
|
{ |
|
|
|
{ |
|
|
@ -77,10 +77,13 @@ void Simulation::initialize() { |
|
|
|
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, SSBO.segmentCounts); |
|
|
|
f->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; |
|
|
@ -122,6 +125,8 @@ void Simulation::updateGPUData() { |
|
|
|
f->glBufferData(GL_SHADER_STORAGE_BUFFER, indicesSize, indices.data(), GL_STATIC_DRAW); |
|
|
|
f->glBufferData(GL_SHADER_STORAGE_BUFFER, indicesSize, indices.data(), GL_STATIC_DRAW); |
|
|
|
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.segmentCounts); |
|
|
|
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO.segmentCounts); |
|
|
|
f->glBufferData(GL_SHADER_STORAGE_BUFFER, segmentCountsSize, segmentCounts.data(), GL_STATIC_DRAW); |
|
|
|
f->glBufferData(GL_SHADER_STORAGE_BUFFER, segmentCountsSize, segmentCounts.data(), GL_STATIC_DRAW); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context->doneCurrent(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Every few milliseconds
|
|
|
|
// Every few milliseconds
|
|
|
@ -139,6 +144,7 @@ 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); |
|
|
|
f->glUniform1d(program->uniformLocation("h"), h); |
|
|
|
f->glUniform1d(program->uniformLocation("gravity"), gravity); |
|
|
|
f->glUniform1d(program->uniformLocation("gravity"), gravity); |
|
|
@ -184,7 +190,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 |
|
|
|