3 * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "pluginvclient.h"
25 #include <cuda_runtime.h>
26 #include <cuda_gl_interop.h>
27 #include <helper_cuda.h>
28 #include <helper_functions.h>
30 #include <5_Simulations/nbody/bodysystemcuda.h>
31 #include <5_Simulations/nbody/render_particles.h>
32 #include "nbodycuda.h"
42 float m_timestep, m_clusterScale, m_velocityScale;
43 float m_softening, m_damping, m_pointSize;
45 static const int num_demos;
56 class N_BodyConfig : public N_BodyParams, public N_BodyCamera
65 int equivalent(N_BodyConfig &that);
66 void copy_from(N_BodyConfig &that);
67 void interpolate(N_BodyConfig &prev, N_BodyConfig &next,
68 long prev_frame, long next_frame, long current_frame);
72 class N_BodyMain : public PluginVClient
75 N_BodyMain(PluginServer *server);
77 PLUGIN_CLASS_MEMBERS2(N_BodyConfig)
81 void save_data(KeyFrame *keyframe);
82 void read_data(KeyFrame *keyframe);
83 int process_buffer(VFrame *frame, int64_t start_position, double frame_rate);
88 int color_model, pass;
95 BodySystem<float> *m_nbody;
96 ParticleRenderer *m_renderer;
98 float *m_hPos, *m_hVel;
101 char deviceName[100];
102 enum { M_VIEW = 0, M_MOVE };
106 int64_t curr_position, new_position;
108 void init(int numBodies);
109 void reset(int numBodies, NBodyConfig cfg);
110 void resetRenderer();
111 void selectDemo(int index);
112 void updateParams() {
113 m_nbody->setSoftening(config.m_softening);
114 m_nbody->setDamping(config.m_damping);
117 void updateSimulation() {
118 m_nbody->update(config.m_timestep);
121 void display() { // display particles
122 m_renderer->setSpriteSize(config.m_pointSize);
123 m_renderer->setPBO(m_nbody->getCurrentReadBuffer(),
124 m_nbody->getNumBodies(), (sizeof(float) > 4));
125 m_renderer->display((ParticleRenderer::DisplayMode)config.mode);
129 void getArrays(float *pos, float *vel) {
130 float *_pos = m_nbody->getArray(BODYSYSTEM_POSITION);
131 float *_vel = m_nbody->getArray(BODYSYSTEM_VELOCITY);
132 memcpy(pos, _pos, m_nbody->getNumBodies() * 4 * sizeof(float));
133 memcpy(vel, _vel, m_nbody->getNumBodies() * 4 * sizeof(float));
136 void setArrays(const float *pos, const float *vel) {
137 int sz = config.numBodies * 4 * sizeof(float);
139 memcpy(m_hPos, pos, sz);
141 memcpy(m_hVel, vel, sz);
142 m_nbody->setArray(BODYSYSTEM_POSITION, m_hPos);
143 m_nbody->setArray(BODYSYSTEM_VELOCITY, m_hVel);