-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcl_mandel.cpp
327 lines (233 loc) · 9.02 KB
/
cl_mandel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* The OpenCL way of doing things, all in just one file
Compile using:
g++ cl_mandel.cpp -o a.out -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio -lsfml-network -lOpenCL -lGL
Run using:
sudo ./a.out # libOpenCL.so often requires root access to work
*/
#define DTYPE float
#include <cmath>
#include <string>
#include <CL/cl2.hpp>
#include <iostream>
#ifndef SFML_GRAPHICS_HPP
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#endif
namespace dim {
int width, height;
}
struct pos
{
double offsetX = 0;
double offsetY = 0;
double zoom = 40;
};
sf::Vector2<float> toPixels(pos position, float x, float y) {
/* Cartesian cords to pixels */
float centerX = (dim::width / 2) + position.offsetX;
float centerY = (dim::height / 2) + position.offsetY;
return sf::Vector2f(
centerX + x * position.zoom,
centerY - y * position.zoom
);
}
sf::Vector2f toCartesian(pos position, sf::Vector2i mpos) {
/* pixel cords to cartesian */
float centerX = (dim::width / 2) + position.offsetX;
float centerY = (dim::height / 2) + position.offsetY;
return sf::Vector2f(
(mpos.x - centerX) / position.zoom,
-(mpos.y - centerY) / position.zoom
);
}
std::string KernelSource = R"cl(
struct complex {
double real;
double imag;
};
__constant int columns = $0;
__constant double initialCenterX = $1;
__constant double initialCenterY = $2;
__kernel void mandel(
__global float *iter,
__global float *SMOOTH,
const double offsetX, const double offsetY,
const double zoom, const int iterations)
{
int col = get_global_id(0);
int row = get_global_id(1);
double centerX = initialCenterX + offsetX;
double centerY = initialCenterY + offsetY;
double x = (col - centerX) / zoom;
double y = -(row - centerY) / zoom;
struct complex z, _z;
z.real = 0, _z.real = 0;
z.imag = 0; _z.imag = 0;
float a_2, b_2;
int local_iterations;
for (local_iterations=0; local_iterations < iterations; local_iterations++)
{
a_2 = _z.real * _z.real;
b_2 = _z.imag * _z.imag;
z.real = a_2 - b_2;
z.imag = 2 * _z.real * _z.imag;
z.real += x;
z.imag += y;
_z.real = z.real;
_z.imag = z.imag;
if (a_2 + b_2 > 4)
break;
}
int tid = row * columns + col;
float smooth = local_iterations + 1 - log(log(sqrt(a_2 + b_2)) / 2);
iter[tid] = ((float)local_iterations / (float)iterations);
SMOOTH[tid] = smooth;
}
)cl";
void rplc(std::string& str, std::string word, std::string rplcmt) {
str.replace(str.find(word), sizeof("$1") - 1, rplcmt);
}
std::string loadKernelSource(std::string source_code, std::string width, std::string height, int cord_array[]) {
std::cout << "BEGIN\n";
if (width.length() > 5 || height.length() > 5)
{
printf("Input width and/or height too large to be proccesed.\n");
exit(1);
}
int __width = std::stoi(width.c_str());
int __height = std::stoi(height.c_str());
rplc(source_code, "$0", std::to_string(__width));
rplc(source_code, "$1", std::to_string((double)__width / 2.0));
rplc(source_code, "$2", std::to_string((double)__height / 2.0));
cord_array[0] = __width;
cord_array[1] = __height;
return source_code;
}
bool inRange(int start, int end, int x) {
bool b1 = x >= start;
bool b2 = x <= end;
return b1 && b2;
}
cl::Device get_device(int platform_index=0, int device_index=0) {
using std::vector;
using std::cout;
vector<cl::Platform> platforms; cl::Platform::get(&platforms);
if (platforms.size() == 0) {
cout << ("No Supported OpenCL implementation found");
exit(1);
}
if (!inRange(0, platforms.size(), platform_index)) {
cout << ("Index \"" + std::to_string(platform_index) + "\" out of Bounds (at Platform selection).");
exit(1);
}
cl::Platform selected_platform = platforms[platform_index];
vector<cl::Device> devices;
selected_platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
if (devices.size() == 0) {
cout << "Platform \"" << selected_platform.getInfo<CL_PLATFORM_NAME>() << "\" has no supported devices.\n";
exit(1);
}
if (!inRange(0, devices.size(), device_index)) {
cout << "Out of bounds index for " << "Platform \"" << selected_platform.getInfo<CL_PLATFORM_NAME>() << "";
exit(1);
}
cl::Device default_device = devices[device_index];
cout << "Using device \"" << default_device.getInfo<CL_DEVICE_NAME>() << "\" by " << default_device.getInfo<CL_DEVICE_VENDOR>() << "\n" ;
return default_device;
}
int main() {
fprintf(stdout, "[ Warning ] Running this may require `sudo` permissions.\n");
// int width = 800, height = 600;
dim::width = 800;
dim::height = 600;
int SIZE = dim::width * dim::height;
int BUFFER_SIZE = SIZE * sizeof(DTYPE);
cl::Device device = get_device(0, 0); // Defalut device 0, 0
cl::Context context({device});
cl::CommandQueue queue(context, device);
int cord_array[2];
std::string kernel_source = loadKernelSource(KernelSource, "800", "600", cord_array);
cl::Program::Sources sources; sources.push_back({kernel_source.c_str(), kernel_source.length()});
cl::Program program(context, sources);
if (program.build({device}) != CL_SUCCESS) {
std::cout << "Error building: " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device) << std::endl;
exit(1);
} else printf("Build success\n");
DTYPE CPU_PIXELS[SIZE];
DTYPE CPU_COPY_PIXELS[SIZE];
DTYPE SMOOTH[SIZE];
DTYPE SMOOTH_COPY[SIZE];
cl::Buffer GPU_PIXELS(context, CL_MEM_READ_WRITE, BUFFER_SIZE);
cl::Buffer GPU_SMOOTH(context, CL_MEM_READ_WRITE, BUFFER_SIZE);
queue.enqueueWriteBuffer(GPU_PIXELS, CL_TRUE, 0, BUFFER_SIZE, CPU_PIXELS);
queue.enqueueWriteBuffer(GPU_SMOOTH, CL_TRUE, 0, BUFFER_SIZE, SMOOTH);
pos position;
position.offsetX = 0;
position.offsetY = 0;
position.zoom = 40;
cl::Kernel mandel(program, "mandel");
sf::RenderWindow window(sf::VideoMode(dim::width, dim::height), "test", sf::Style::Default, sf::ContextSettings(32));
sf::Event event;
bool running = true;
glOrtho(0, dim::width, dim::height, 0, -1, 1);
float velocity = 1.0f;
float last_time = 0;
sf::Clock timer;
while (running)
{
while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { running = false; } }
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {
velocity += 1.0f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::E)) {
velocity += 100.0f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) {
sf::Vector2i mpos = sf::Mouse::getPosition();
sf::Vector2f cmpos = toCartesian(position, mpos);
position.zoom += velocity;
}
sf::Vector2i mpos = sf::Mouse::getPosition();
sf::Vector2f cmpos = toCartesian(position, mpos);
// printf("%.2f %.2f\n", cmpos.x, cmpos.y);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::X)) {
position.zoom -= 1.0f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
position.offsetX += 10;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
position.offsetX -= 10;
}
mandel.setArg(0, GPU_PIXELS); // __global float *iter,
mandel.setArg(1, GPU_SMOOTH); // __global float *SMOOTH,
mandel.setArg(2, position.offsetX); // const double offsetX,
mandel.setArg(3, position.offsetY); // const double offsetY,
mandel.setArg(4, position.zoom); // const double zoom,
mandel.setArg(5, 50); // const int iterations)
queue.enqueueNDRangeKernel(mandel, cl::NullRange, cl::NDRange(dim::width, dim::height), cl::NullRange);
queue.enqueueReadBuffer(GPU_PIXELS, CL_TRUE, 0, BUFFER_SIZE, CPU_COPY_PIXELS);
queue.enqueueReadBuffer(GPU_SMOOTH, CL_TRUE, 0, BUFFER_SIZE, SMOOTH_COPY);
glBegin(GL_POINTS);
for (int i=0; i < dim::height; i++) {
for (int j=0; j < dim::width; j++) {
int tid = i * dim::width + j;
float value = CPU_COPY_PIXELS[tid];
float smooth = SMOOTH_COPY[tid];
glColor3f(0, value, 0);
glVertex2f(j, i);
}
}
glEnd();
window.display();
timer.restart();
}
return 0;
}
/*
function run ()
{
g++ $1 -o a.out -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio -lsfml-network -lOpenCL -lGL && sudo ./a.out
}
*/