configure.ac add with-cuda/nv, update cakewalk theme, add cuda/plugins=mandel+nbody...
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / nbodycuda / nbody.h
1 /*
2  * CINELERRA
3  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
4  * 
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.
9  * 
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.
14  * 
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
18  */
19
20 #ifndef NBODYCUDA_H
21 #define NBODYCUDA_H
22
23 #include "pluginvclient.h"
24
25 #include <cuda_runtime.h>
26 #include <cuda_gl_interop.h>
27 #include <helper_cuda.h>
28 #include <helper_functions.h>
29
30 #include <5_Simulations/nbody/bodysystemcuda.h>
31 #include <5_Simulations/nbody/render_particles.h>
32 #include "nbodycuda.h"
33
34 class N_BodyConfig;
35 class N_BodyMain;
36 class N_BodyCuda;
37
38
39 class N_BodyParams
40 {
41 public: 
42         float m_timestep, m_clusterScale, m_velocityScale;
43         float m_softening, m_damping, m_pointSize;
44         float m_x, m_y, m_z;
45         static const int num_demos;
46 };
47 class N_BodyCamera
48 {
49 public:
50         float trans[3];
51         float rot[3];
52         float trans_lag[3];
53         float rot_lag[3];
54 };
55
56 class N_BodyConfig : public N_BodyParams, public N_BodyCamera
57 {
58 public:
59         N_BodyConfig();
60         void reset(int i=0);
61         int mode;
62         float inertia;
63         int numBodies;
64
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);
69         void limits();
70 };
71
72 class N_BodyMain : public PluginVClient
73 {
74 public:
75         N_BodyMain(PluginServer *server);
76         ~N_BodyMain();
77         PLUGIN_CLASS_MEMBERS2(N_BodyConfig)
78         int is_realtime();
79         int is_synthesis();
80         void update_gui();
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);
84         void initData();
85         void reset();
86         int handle_opengl();
87
88         int color_model, pass;
89         VFrame *output;
90         N_BodyCuda *cuda;
91
92         void init_cuda();
93         void finish_cuda();
94
95         BodySystem<float> *m_nbody;
96         ParticleRenderer *m_renderer;
97
98         float *m_hPos, *m_hVel;
99         float *m_hColor;
100
101         char deviceName[100];
102         enum { M_VIEW = 0, M_MOVE };
103         int blockSize;
104
105         int activeDemo;
106         int64_t curr_position, new_position;
107
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);
115         }
116
117         void updateSimulation() {
118                 m_nbody->update(config.m_timestep);
119         }
120
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);
126         }
127         void draw();
128
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));
134         }
135
136         void setArrays(const float *pos, const float *vel) {
137                 int sz = config.numBodies * 4 * sizeof(float);
138                 if (pos != m_hPos)
139                         memcpy(m_hPos, pos, sz);
140                 if (vel != m_hVel)
141                         memcpy(m_hVel, vel, sz);
142                 m_nbody->setArray(BODYSYSTEM_POSITION, m_hPos);
143                 m_nbody->setArray(BODYSYSTEM_VELOCITY, m_hVel);
144                 resetRenderer();
145         }
146
147         void finalize();
148 };
149
150 #endif