-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
297 lines (237 loc) · 7.76 KB
/
main.cpp
File metadata and controls
297 lines (237 loc) · 7.76 KB
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
#include <CL/cl.h>
#include <vector>
#include <iostream>
#include <string>
#include <fstream>
#include <streambuf>
#include <Windows.h>
#include <atlbase.h>
#include <atlconv.h>
#define IMG_WIDTH 1280
#define IMG_HEIGHT 720
#define KERNEL_FILE "raytracer.cl"
//#define IMG_WIDTH 752
//#define IMG_HEIGHT 480
const TCHAR g_szClassName[] = L"(╯°□°)╯︵ ┻━┻";
cl_int *result;
bool running = true;
cl_int iteration = 0;
std::string loadKernel(const char *name) {
std::ifstream in(name);
std::string result((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
return result;
}
cl_program createProgram(const std::string *source, cl_context context, cl_int *error) {
size_t lengths[1] = {source->size()};
const char *sources[1] = {source->data()};
cl_program program = clCreateProgramWithSource(context, 1, sources, lengths, error);
return program;
}
void display_error(HWND hwnd, TCHAR *message) {
MessageBox(
hwnd,
(LPCWCHAR) message,
L"Error!",
MB_ICONEXCLAMATION | MB_OK);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_QUIT:
running = false;
break;
case WM_CLOSE:
running = false;
DestroyWindow(hwnd);
break;
case WM_DESTROY:
running = false;
PostQuitMessage(0);
break;
case WM_ERASEBKGND:
break;
case WM_PAINT: {
if (result) {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HBITMAP bmp = CreateBitmap(IMG_WIDTH, IMG_HEIGHT, 1, 32, (void*)result);
HDC src = CreateCompatibleDC(hdc);
SelectObject(src, bmp);
BitBlt(hdc, 0, 0, IMG_WIDTH, IMG_HEIGHT, src, 0, 0, SRCCOPY);
DeleteDC(src);
DeleteObject(bmp);
EndPaint(hwnd, &ps);
}
return DefWindowProc(hwnd, msg, wParam, lParam);
} break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = (LPCWCHAR) g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc)) {
display_error(hwnd, L"Window Registration Failed!");
return 1;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
(LPCWCHAR) g_szClassName,
(LPCWCHAR) L"¯\\_(ツ)_/¯",//L"💩💩💩💩💩💩💩",//L"(╯°□°)╯︵ ┻━┻",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, IMG_WIDTH, IMG_HEIGHT,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
display_error(hwnd, L"Window Creation Failed!");
return 1;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
cl_uint platform_id_count = 0;
clGetPlatformIDs(0, nullptr, &platform_id_count);
if (0 == platform_id_count) {
display_error(hwnd, L"No platforms found.");
return 1;
}
std::vector<cl_platform_id> platform_ids(platform_id_count);
clGetPlatformIDs(platform_id_count, platform_ids.data(), nullptr);
for (auto platform : platform_ids) {
size_t platform_name_length = 0;
clGetPlatformInfo(platform, CL_PLATFORM_NAME, 0, nullptr, &platform_name_length);
char *platform_name = (char*)malloc(sizeof(char) * platform_name_length);
clGetPlatformInfo(platform, CL_PLATFORM_NAME, platform_name_length, platform_name, nullptr);
//display_error(hwnd, CA2W(platform_name));
free(platform_name);
}
cl_uint device_id_count = 0;
clGetDeviceIDs(platform_ids[0], CL_DEVICE_TYPE_ALL, 0, nullptr, &device_id_count);
if (0 == device_id_count) {
display_error(hwnd, L"No devices found.");
return 1;
}
std::vector<cl_device_id> device_ids(device_id_count);
clGetDeviceIDs(platform_ids[0], CL_DEVICE_TYPE_ALL, device_id_count, device_ids.data(), nullptr);
for (auto device : device_ids) {
size_t device_name_length = 0;
clGetDeviceInfo(device, CL_DEVICE_NAME, 0, nullptr, &device_name_length);
char *device_name = (char*)malloc(sizeof(char) * device_name_length);
clGetDeviceInfo(device, CL_DEVICE_NAME, device_name_length, device_name, nullptr);
//display_error(hwnd, CA2W(device_name));
free(device_name);
}
const cl_context_properties context_properties[] = {
CL_CONTEXT_PLATFORM,
reinterpret_cast<cl_context_properties> (platform_ids[0]),
0, 0
};
cl_int error = 0;
cl_context context = clCreateContext(
context_properties,
device_id_count,
device_ids.data(),
nullptr,
nullptr,
&error);
if (error != CL_SUCCESS) {
display_error(hwnd, L"Error creating a context.");
return 1;
}
std::string source = loadKernel(KERNEL_FILE);
if (0 == source.size()) {
display_error(hwnd, L"Error reading kernel file.");
return 1;
}
cl_program program = createProgram(&source, context, &error);
if (error != CL_SUCCESS) {
display_error(hwnd, L"Error creating a program.");
return 1;
}
error = clBuildProgram(program, 1, device_ids.data(), nullptr, nullptr, nullptr);
if (error != CL_SUCCESS) {
if (error == CL_BUILD_PROGRAM_FAILURE) {
size_t log_size;
clGetProgramBuildInfo(program, device_ids[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
char *log = (char *)malloc(log_size);
clGetProgramBuildInfo(program, device_ids[0], CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
display_error(hwnd, CA2W(log));
}
display_error(hwnd, L"Error building the program.");
return 1;
}
cl_kernel kernel = clCreateKernel(program, "MAIN", &error);
if (error != CL_SUCCESS) {
display_error(hwnd, L"Error creating a kernel.");
return 1;
}
const cl_int img_width = IMG_WIDTH;
const cl_int img_height = IMG_HEIGHT;
const cl_int num_pixels = img_width * img_height;
result = (cl_int*)malloc(sizeof(cl_int) * num_pixels);
if (nullptr == result) {
display_error(hwnd, L"Failed to allocate result array.");
return 1;
}
cl_mem buffer_integration = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, sizeof(cl_float3) * num_pixels, nullptr, &error);
if (error != CL_SUCCESS) {
display_error(hwnd, L"Error creating integration buffer.");
return 1;
}
cl_mem buffer_result = clCreateBuffer(context, CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(cl_int) * num_pixels, result, &error);
if (error != CL_SUCCESS) {
display_error(hwnd, L"Error creating result buffer.");
return 1;
}
cl_command_queue queue = clCreateCommandQueue(context, device_ids[0], 0, &error);
if (error != CL_SUCCESS) {
display_error(hwnd, L"Error creating a queue.");
return 1;
}
clSetKernelArg(kernel, 0, sizeof(cl_int), &img_width);
clSetKernelArg(kernel, 1, sizeof(cl_int), &img_height);
clSetKernelArg(kernel, 2, sizeof(cl_mem), &buffer_integration);
clSetKernelArg(kernel, 3, sizeof(cl_mem), &buffer_result);
const size_t globalWorkSize[] = {num_pixels, 0, 0};
while (running) {
iteration++;
while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
clSetKernelArg(kernel, 4, sizeof(cl_int), &iteration);
error = clEnqueueNDRangeKernel(queue, kernel, 1, nullptr, globalWorkSize, nullptr, 0, nullptr, nullptr);
if (error != CL_SUCCESS) {
display_error(hwnd, L"Error pushing kernel into the queue.");
return 1;
}
error = clEnqueueReadBuffer(queue, buffer_result, CL_TRUE, 0, sizeof(cl_int) * num_pixels, result, 0, nullptr, nullptr);
if (error != CL_SUCCESS) {
display_error(hwnd, L"Error retrieving results.");
return 1;
}
InvalidateRect(hwnd, nullptr, true);
SetWindowText(hwnd, CA2W(std::to_string(iteration).c_str()));
}
clReleaseCommandQueue(queue);
clReleaseMemObject(buffer_result);
clReleaseMemObject(buffer_integration);
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseContext(context);
return Msg.wParam;
}