-
Notifications
You must be signed in to change notification settings - Fork 0
/
OCLRenderer.cpp
291 lines (256 loc) · 9.05 KB
/
OCLRenderer.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
#ifdef USE_DOUBLE
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
#endif
#include <iostream>
#include <fstream>
#include <sstream>
#include "OCLRenderer.hpp"
#include "CLUtils.hpp"
#ifdef __linux__
#include <X11/X.h>
#include <X11/Xlib.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#elif defined(__APPLE__)
#include <OpenGL/OpenGL.h>
#endif
OCLRenderer::OCLRenderer(size_t width, size_t height, size_t gpuNum, const std::string &kernelname,
const std::string &sourceFilename) : texture(Texture(width, height)), zoom(1.0f), pos({0.0f, 0.0f}), iterations(300)
{
try
{
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
size_t gpuCount = 0;
if (platforms.size() == 0)
{
std::cerr << "[OCLRenderer] no opencl platforms available" << std::endl;
exit(EXIT_FAILURE);
}
for (const auto &p : platforms)
{
// Select the platform with the same Vendor as the GL Context
if (std::string(p.getInfo<CL_PLATFORM_VENDOR>()).find(std::string((const char *) glGetString(GL_VENDOR))) !=
std::string::npos)
{
// Get a list of devices on this platform
std::vector<cl::Device> devices;
p.getDevices(CL_DEVICE_TYPE_ALL, &devices);
for (const auto &d : devices)
{
// if the device is a gpu and has the support for sharing a gl context it is a suitable gpu
if (d.getInfo<CL_DEVICE_TYPE>() == CL_DEVICE_TYPE_GPU &&
(std::string(d.getInfo<CL_DEVICE_EXTENSIONS>()).find(std::string("cl_khr_gl_sharing")) != std::string::npos))
{
if (gpuNum == gpuCount)
{
#ifdef __linux__
cl_context_properties properties[] = {
CL_GL_CONTEXT_KHR, (cl_context_properties) glXGetCurrentContext(),
CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay(),
CL_CONTEXT_PLATFORM, (cl_context_properties) (p)(),
0
};
#elif defined(_WIN32)
cl_context_properties properties[] = {
CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(),
CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC(),
CL_CONTEXT_PLATFORM, (cl_context_properties) (mPlatforms[0])(),
0
};
#elif defined(__APPLE__)
CGLContextObj glContext = CGLGetCurrentContext();
CGLShareGroupObj shareGroup = CGLGetShareGroup(glContext);
cl_context_properties properties[] = {
CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
(cl_context_properties) shareGroup,
};
#endif
device = d;
context = cl::Context(device, properties);
queue = cl::CommandQueue(context, device);
// open and compile the program
openProgram(sourceFilename, kernelname);
// setup texture with the correct width and height
reshape(width, height);
return;
}
gpuCount++;
}
}
}
}
std::cerr << "[OCLRenderer] requested gpu not or no gpu with GL context available" << std::endl;
exit(EXIT_FAILURE);
}
catch (cl::Error error)
{
std::cout << error.what() << "(" << cl::errorString(error.err()) << ")" << std::endl;
exit(EXIT_FAILURE);
}
}
bool OCLRenderer::openProgram(const std::string &filename, const std::string &kernelname)
{
try
{
std::ifstream sourcefile(filename);
std::string sourcecode(std::istreambuf_iterator<char>(sourcefile), (std::istreambuf_iterator<char>()));
cl::Program::Sources source(1, std::make_pair(sourcecode.c_str(), sourcecode.length() + 1));
// make program of the source code in the context
program = cl::Program(context, source);
// possibly some definitions for the kernel
std::stringstream kerneloptions;
// build program
std::vector<cl::Device> tmpdevices;
tmpdevices.push_back(device);
program.build(tmpdevices, kerneloptions.str().c_str());
#ifdef USE_DOUBLE
renderKernelFunc.reset(
new cl::make_kernel<cl::ImageGL &, cl::Buffer &, cl::Buffer &, cl_float3, cl_int, cl_int, cl_int, cl_double, cl_double2, cl_int>(
cl::Kernel(program, kernelname.c_str())));
#else
renderKernelFunc.reset(
new cl::make_kernel<cl::ImageGL &, cl::Buffer &, cl::Buffer &, cl_float3, cl_int, cl_int, cl_int, cl_float, cl_float2, cl_int>(
cl::Kernel(program, "mandelbrot")));
#endif
}
catch (cl::Error error)
{
std::cout << error.what() << "(" << cl::errorString(error.err()) << ")" << std::endl;
if (error.err() == CL_BUILD_PROGRAM_FAILURE)
std::cout << "Build log:" << std::endl << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device) << std::endl;
exit(EXIT_FAILURE);
}
}
void OCLRenderer::render(bool refresh)
{
glFinish();
try
{
queue.enqueueAcquireGLObjects(&glObjs);
cl::EnqueueArgs eargs(queue, cl::NDRange(cl::nextDivisible(texture.width, 8),
cl::nextDivisible(texture.height, 8)), cl::NDRange(8, 8));
sampleCount = refresh ? 1 : (sampleCount + 1);
#ifdef USE_DOUBLE
(*renderKernelFunc)(eargs, imageBuffer, imageRawBuffer, randStatesBuffer, color, texture.width,
texture.height, iterations, zoom, pos, sampleCount);
#else
cl_float2 posf = {(cl_float) pos.s[0], (cl_float) pos.s[1]};
(*renderKernelFunc)(eargs, imageBuffer, imageRawBuffer, randStatesBuffer, color, texture.width,
texture.height, iterations, (cl_float) zoom, posf, sampleCount);
#endif
queue.enqueueReleaseGLObjects(&glObjs);
queue.finish();
}
catch (cl::Error error)
{
std::cout << error.what() << "(" << cl::errorString(error.err()) << ")" << std::endl;
exit(EXIT_FAILURE);
}
}
void OCLRenderer::reshape(size_t width, size_t height)
{
texture.width = width;
texture.height = height;
texture.createEmptyTexture();
#ifdef CL_VERSION_1_2
imageBuffer = cl::ImageGL(context, CL_MEM_READ_WRITE, GL_TEXTURE_2D, 0, texture.id);
#else
imageBuffer = cl::Image2DGL(context, CL_MEM_READ_WRITE, GL_TEXTURE_2D, 0, texture.id);
#endif
imageRawBuffer = cl::Buffer(context, CL_MEM_READ_ONLY, width * height * sizeof(cl_float4));
randStatesBuffer = cl::Buffer(context, CL_MEM_READ_ONLY, width * height * sizeof(cl_uint4));
cl_uint *randStatesInitial = new cl_uint[4 * width * height];
for (size_t i = 0; i < 4 * width * height; ++i)
randStatesInitial[i] = i;
queue.enqueueWriteBuffer(randStatesBuffer, CL_TRUE, 0, width * height * sizeof(cl_uint4), randStatesInitial);
delete[] randStatesInitial;
glObjs.clear();
glObjs.push_back(imageBuffer);
}
const Texture &OCLRenderer::getTexture() const
{
return texture;
}
void OCLRenderer::printAllDevices()
{
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
size_t gpuCount = 0;
for (size_t i = 0; i < platforms.size(); ++i)
{
std::cout << "[OCLRenderer] OpenCL Platform " << i << ": " << platforms[i].getInfo<CL_PLATFORM_VENDOR>() <<
std::endl;
// Get the list of devices available on the platform
std::vector<cl::Device> devices;
platforms[i].getDevices(CL_DEVICE_TYPE_ALL, &devices);
for (size_t j = 0; j < devices.size(); ++j)
{
std::cout << "[OCLRenderer] OpenCL device " << j << ": " << devices[j].getInfo<CL_DEVICE_NAME>() <<
std::endl;
std::cout << "[OCLRenderer] Type: " << cl::deviceTypeString(devices[j].getInfo<CL_DEVICE_TYPE>()) <<
std::endl;
std::cout << "[OCLRenderer] Units: " << devices[j].getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>() << std::endl;
std::cout << "[OCLRenderer] Global memory: " <<
devices[j].getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>() / 1024 << "Kbytes" << std::endl;
std::cout << "[OCLRenderer] Local memory: " << devices[j].getInfo<CL_DEVICE_LOCAL_MEM_SIZE>() / 1024 <<
"Kbytes" << std::endl;
std::cout << "[OCLRenderer] Local memory type: " <<
cl::memoryTypeString(devices[j].getInfo<CL_DEVICE_LOCAL_MEM_TYPE>()) << std::endl;
std::cout << "[OCLRenderer] Constant memory: " <<
devices[j].getInfo<CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE>() / 1024 << "Kbytes" << std::endl;
std::cout << "[OCLRenderer] Device extensions: " <<
devices[j].getInfo<CL_DEVICE_EXTENSIONS>() << std::endl;
if (devices[j].getInfo<CL_DEVICE_TYPE>() == CL_DEVICE_TYPE_GPU)
std::cout << "[OCLRenderer] GPU number: " << gpuCount++ << std::endl;
}
}
}
double OCLRenderer::getZoom() const
{
return zoom;
}
void OCLRenderer::setZoom(double zoom)
{
OCLRenderer::zoom = zoom;
}
const cl_double2 &OCLRenderer::getPos() const
{
return pos;
}
void OCLRenderer::setPos(double x, double y)
{
pos = {x, y};
}
const cl_float3 &OCLRenderer::getColor() const
{
return color;
}
void OCLRenderer::setColor(const cl_float3 &color)
{
OCLRenderer::color = color;
}
int OCLRenderer::getSampleCount() const
{
return sampleCount;
}
cl_int OCLRenderer::getIterations() const
{
return iterations;
}
void OCLRenderer::setIterations(cl_int iterations)
{
OCLRenderer::iterations = iterations;
}
std::shared_ptr<std::vector<cl_float>> OCLRenderer::getImage() const
{
std::shared_ptr<std::vector<cl_float>> retVal(new std::vector<cl_float>(texture.width * texture.height * 4));
glFinish();
queue.enqueueReadBuffer(imageRawBuffer, CL_TRUE, 0, texture.width * texture.height * sizeof(cl_float4),
&((*retVal)[0]));
queue.finish();
for (size_t i = 0; i < texture.width * texture.height * 4; ++i)
(*retVal)[i] /= sampleCount;
return retVal;
}