Cycles: enable multi closure sampling and transparent shadows only on CPU and
[blender.git] / intern / cycles / device / device_opencl.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #ifdef WITH_OPENCL
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "device.h"
26 #include "device_intern.h"
27
28 #include "util_map.h"
29 #include "util_math.h"
30 #include "util_md5.h"
31 #include "util_opencl.h"
32 #include "util_opengl.h"
33 #include "util_path.h"
34 #include "util_time.h"
35
36 CCL_NAMESPACE_BEGIN
37
38 #define CL_MEM_PTR(p) ((cl_mem)(unsigned long)(p))
39
40 class OpenCLDevice : public Device
41 {
42 public:
43         cl_context cxContext;
44         cl_command_queue cqCommandQueue;
45         cl_platform_id cpPlatform;
46         cl_device_id cdDevice;
47         cl_program cpProgram;
48         cl_kernel ckPathTraceKernel;
49         cl_kernel ckFilmConvertKernel;
50         cl_int ciErr;
51         map<string, device_vector<uchar>*> const_mem_map;
52         map<string, device_memory*> mem_map;
53         device_ptr null_mem;
54         bool device_initialized;
55
56         const char *opencl_error_string(cl_int err)
57         {
58                 switch (err) {
59                         case CL_SUCCESS: return "Success!";
60                         case CL_DEVICE_NOT_FOUND: return "Device not found.";
61                         case CL_DEVICE_NOT_AVAILABLE: return "Device not available";
62                         case CL_COMPILER_NOT_AVAILABLE: return "Compiler not available";
63                         case CL_MEM_OBJECT_ALLOCATION_FAILURE: return "Memory object allocation failure";
64                         case CL_OUT_OF_RESOURCES: return "Out of resources";
65                         case CL_OUT_OF_HOST_MEMORY: return "Out of host memory";
66                         case CL_PROFILING_INFO_NOT_AVAILABLE: return "Profiling information not available";
67                         case CL_MEM_COPY_OVERLAP: return "Memory copy overlap";
68                         case CL_IMAGE_FORMAT_MISMATCH: return "Image format mismatch";
69                         case CL_IMAGE_FORMAT_NOT_SUPPORTED: return "Image format not supported";
70                         case CL_BUILD_PROGRAM_FAILURE: return "Program build failure";
71                         case CL_MAP_FAILURE: return "Map failure";
72                         case CL_INVALID_VALUE: return "Invalid value";
73                         case CL_INVALID_DEVICE_TYPE: return "Invalid device type";
74                         case CL_INVALID_PLATFORM: return "Invalid platform";
75                         case CL_INVALID_DEVICE: return "Invalid device";
76                         case CL_INVALID_CONTEXT: return "Invalid context";
77                         case CL_INVALID_QUEUE_PROPERTIES: return "Invalid queue properties";
78                         case CL_INVALID_COMMAND_QUEUE: return "Invalid command queue";
79                         case CL_INVALID_HOST_PTR: return "Invalid host pointer";
80                         case CL_INVALID_MEM_OBJECT: return "Invalid memory object";
81                         case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: return "Invalid image format descriptor";
82                         case CL_INVALID_IMAGE_SIZE: return "Invalid image size";
83                         case CL_INVALID_SAMPLER: return "Invalid sampler";
84                         case CL_INVALID_BINARY: return "Invalid binary";
85                         case CL_INVALID_BUILD_OPTIONS: return "Invalid build options";
86                         case CL_INVALID_PROGRAM: return "Invalid program";
87                         case CL_INVALID_PROGRAM_EXECUTABLE: return "Invalid program executable";
88                         case CL_INVALID_KERNEL_NAME: return "Invalid kernel name";
89                         case CL_INVALID_KERNEL_DEFINITION: return "Invalid kernel definition";
90                         case CL_INVALID_KERNEL: return "Invalid kernel";
91                         case CL_INVALID_ARG_INDEX: return "Invalid argument index";
92                         case CL_INVALID_ARG_VALUE: return "Invalid argument value";
93                         case CL_INVALID_ARG_SIZE: return "Invalid argument size";
94                         case CL_INVALID_KERNEL_ARGS: return "Invalid kernel arguments";
95                         case CL_INVALID_WORK_DIMENSION: return "Invalid work dimension";
96                         case CL_INVALID_WORK_GROUP_SIZE: return "Invalid work group size";
97                         case CL_INVALID_WORK_ITEM_SIZE: return "Invalid work item size";
98                         case CL_INVALID_GLOBAL_OFFSET: return "Invalid global offset";
99                         case CL_INVALID_EVENT_WAIT_LIST: return "Invalid event wait list";
100                         case CL_INVALID_EVENT: return "Invalid event";
101                         case CL_INVALID_OPERATION: return "Invalid operation";
102                         case CL_INVALID_GL_OBJECT: return "Invalid OpenGL object";
103                         case CL_INVALID_BUFFER_SIZE: return "Invalid buffer size";
104                         case CL_INVALID_MIP_LEVEL: return "Invalid mip-map level";
105                         default: return "Unknown";
106                 }
107         }
108
109         bool opencl_error(cl_int err)
110         {
111                 if(err != CL_SUCCESS) {
112                         fprintf(stderr, "OpenCL error (%d): %s\n", err, opencl_error_string(err));
113                         return true;
114                 }
115
116                 return false;
117         }
118
119         void opencl_assert(cl_int err)
120         {
121                 if(err != CL_SUCCESS) {
122                         fprintf(stderr, "OpenCL error (%d): %s\n", err, opencl_error_string(err));
123 #ifndef NDEBUG
124                         abort();
125 #endif
126                 }
127         }
128
129         OpenCLDevice(bool background_)
130         {
131                 background = background_;
132                 cpPlatform = NULL;
133                 cxContext = NULL;
134                 cqCommandQueue = NULL;
135                 cpProgram = NULL;
136                 ckPathTraceKernel = NULL;
137                 ckFilmConvertKernel = NULL;
138                 null_mem = 0;
139                 device_initialized = false;
140
141                 vector<cl_platform_id> platform_ids;
142                 cl_uint num_platforms;
143
144                 /* setup device */
145                 ciErr = clGetPlatformIDs(0, NULL, &num_platforms);
146                 if(opencl_error(ciErr))
147                         return;
148
149                 if(num_platforms == 0) {
150                         fprintf(stderr, "OpenCL: no platforms found.\n");
151                         return;
152                 }
153
154                 platform_ids.resize(num_platforms);
155                 ciErr = clGetPlatformIDs(num_platforms, &platform_ids[0], NULL);
156                 if(opencl_error(ciErr))
157                         return;
158
159                 cpPlatform = platform_ids[0]; /* todo: pick specified platform && device */
160
161                 ciErr = clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU|CL_DEVICE_TYPE_ACCELERATOR, 1, &cdDevice, NULL);
162                 if(opencl_error(ciErr))
163                         return;
164
165                 cxContext = clCreateContext(0, 1, &cdDevice, NULL, NULL, &ciErr);
166                 if(opencl_error(ciErr))
167                         return;
168
169                 cqCommandQueue = clCreateCommandQueue(cxContext, cdDevice, 0, &ciErr);
170                 if(opencl_error(ciErr))
171                         return;
172
173                 null_mem = (device_ptr)clCreateBuffer(cxContext, CL_MEM_READ_ONLY, 1, NULL, &ciErr);
174                 device_initialized = true;
175         }
176
177         bool opencl_version_check()
178         {
179                 char version[256];
180                 int major, minor, req_major = 1, req_minor = 1;
181
182                 clGetPlatformInfo(cpPlatform, CL_PLATFORM_VERSION, sizeof(version), &version, NULL);
183
184                 if(sscanf(version, "OpenCL %d.%d", &major, &minor) < 2) {
185                         fprintf(stderr, "OpenCL: failed to parse platform version string (%s).", version);
186                         return false;
187                 }
188
189                 if(!((major == req_major && minor >= req_minor) || (major > req_major))) {
190                         fprintf(stderr, "OpenCL: platform version 1.1 or later required, found %d.%d\n", major, minor);
191                         return false;
192                 }
193
194                 clGetDeviceInfo(cdDevice, CL_DEVICE_OPENCL_C_VERSION, sizeof(version), &version, NULL);
195
196                 if(sscanf(version, "OpenCL C %d.%d", &major, &minor) < 2) {
197                         fprintf(stderr, "OpenCL: failed to parse OpenCL C version string (%s).", version);
198                         return false;
199                 }
200
201                 if(!((major == req_major && minor >= req_minor) || (major > req_major))) {
202                         fprintf(stderr, "OpenCL: C version 1.1 or later required, found %d.%d\n", major, minor);
203                         return false;
204                 }
205
206                 /* we don't check CL_DEVICE_VERSION since for e.g. nvidia sm 1.3 cards this is
207                         1.0 even if the language features are there, just limited shared memory */
208
209                 return true;
210         }
211
212         bool load_binary(const string& kernel_path, const string& clbin)
213         {
214                 /* read binary into memory */
215                 vector<uint8_t> binary;
216
217                 if(!path_read_binary(clbin, binary)) {
218                         fprintf(stderr, "OpenCL failed to read cached binary %s.\n", clbin.c_str());
219                         return false;
220                 }
221
222                 /* create program */
223                 cl_int status;
224                 size_t size = binary.size();
225                 const uint8_t *bytes = &binary[0];
226
227                 cpProgram = clCreateProgramWithBinary(cxContext, 1, &cdDevice,
228                         &size, &bytes, &status, &ciErr);
229
230                 if(opencl_error(status) || opencl_error(ciErr)) {
231                         fprintf(stderr, "OpenCL failed create program from cached binary %s.\n", clbin.c_str());
232                         return false;
233                 }
234
235                 if(!build_kernel(kernel_path))
236                         return false;
237
238                 return true;
239         }
240
241         bool save_binary(const string& clbin)
242         {
243                 size_t size = 0;
244                 clGetProgramInfo(cpProgram, CL_PROGRAM_BINARY_SIZES, sizeof(size_t), &size, NULL);
245
246                 if(!size)
247                         return false;
248
249                 vector<uint8_t> binary(size);
250                 uint8_t *bytes = &binary[0];
251
252                 clGetProgramInfo(cpProgram, CL_PROGRAM_BINARIES, sizeof(uint8_t*), &bytes, NULL);
253
254                 if(!path_write_binary(clbin, binary)) {
255                         fprintf(stderr, "OpenCL failed to write cached binary %s.\n", clbin.c_str());
256                         return false;
257                 }
258
259                 return true;
260         }
261
262         bool build_kernel(const string& kernel_path)
263         {
264                 string build_options = "";
265
266                 build_options += "-I " + kernel_path + ""; /* todo: escape path */
267                 build_options += " -cl-fast-relaxed-math ";
268
269                 ciErr = clBuildProgram(cpProgram, 0, NULL, build_options.c_str(), NULL, NULL);
270
271                 if(ciErr != CL_SUCCESS) {
272                         /* show build errors */
273                         char *build_log;
274                         size_t ret_val_size;
275
276                         clGetProgramBuildInfo(cpProgram, cdDevice, CL_PROGRAM_BUILD_LOG, 0, NULL, &ret_val_size);
277
278                         build_log = new char[ret_val_size+1];
279                         clGetProgramBuildInfo(cpProgram, cdDevice, CL_PROGRAM_BUILD_LOG, ret_val_size, build_log, NULL);
280
281                         build_log[ret_val_size] = '\0';
282                         fprintf(stderr, "OpenCL build failed:\n %s\n", build_log);
283
284                         delete[] build_log;
285
286                         return false;
287                 }
288
289                 return true;
290         }
291
292         bool compile_kernel(const string& kernel_path, const string& kernel_md5)
293         {
294                 /* we compile kernels consisting of many files. unfortunately opencl
295                    kernel caches do not seem to recognize changes in included files.
296                    so we force recompile on changes by adding the md5 hash of all files */
297                 string source = "#include \"kernel.cl\" // " + kernel_md5 + "\n";
298                 size_t source_len = source.size();
299                 const char *source_str = source.c_str();
300
301                 cpProgram = clCreateProgramWithSource(cxContext, 1, &source_str, &source_len, &ciErr);
302
303                 if(opencl_error(ciErr))
304                         return false;
305
306                 double starttime = time_dt();
307                 printf("Compiling OpenCL kernel ...\n");
308
309                 if(!build_kernel(kernel_path))
310                         return false;
311
312                 printf("Kernel compilation finished in %.2lfs.\n", time_dt() - starttime);
313
314                 return true;
315         }
316
317         string device_md5_hash()
318         {
319                 MD5Hash md5;
320                 char version[256], driver[256], name[256], vendor[256];
321
322                 clGetPlatformInfo(cpPlatform, CL_PLATFORM_VENDOR, sizeof(vendor), &vendor, NULL);
323                 clGetDeviceInfo(cdDevice, CL_DEVICE_VERSION, sizeof(version), &version, NULL);
324                 clGetDeviceInfo(cdDevice, CL_DEVICE_NAME, sizeof(name), &name, NULL);
325                 clGetDeviceInfo(cdDevice, CL_DRIVER_VERSION, sizeof(driver), &driver, NULL);
326
327                 md5.append((uint8_t*)vendor, strlen(vendor));
328                 md5.append((uint8_t*)version, strlen(version));
329                 md5.append((uint8_t*)name, strlen(name));
330                 md5.append((uint8_t*)driver, strlen(driver));
331
332                 return md5.get_hex();
333         }
334
335         bool load_kernels()
336         {
337                 /* verify if device was initialized */
338                 if(!device_initialized) {
339                         fprintf(stderr, "OpenCL: failed to initialize device.\n");
340                         return false;
341                 }
342
343                 /* verify we have right opencl version */
344                 if(!opencl_version_check())
345                         return false;
346
347                 /* md5 hash to detect changes */
348                 string kernel_path = path_get("kernel");
349                 string kernel_md5 = path_files_md5_hash(kernel_path);
350                 string device_md5 = device_md5_hash();
351
352                 /* try to use cache binary */
353                 string clbin = string_printf("cycles_kernel_%s_%s.clbin", device_md5.c_str(), kernel_md5.c_str());;
354                 clbin = path_user_get(path_join("cache", clbin));
355
356                 if(path_exists(clbin)) {
357                         /* if exists already, try use it */
358                         if(!load_binary(kernel_path, clbin))
359                                 return false;
360                 }
361                 else {
362                         /* compile kernel */
363                         if(!compile_kernel(kernel_path, kernel_md5))
364                                 return false;
365
366                         /* save binary for reuse */
367                         save_binary(clbin);
368                 }
369
370                 /* find kernels */
371                 ckPathTraceKernel = clCreateKernel(cpProgram, "kernel_ocl_path_trace", &ciErr);
372                 if(opencl_error(ciErr))
373                         return false;
374
375                 ckFilmConvertKernel = clCreateKernel(cpProgram, "kernel_ocl_tonemap", &ciErr);
376                 if(opencl_error(ciErr))
377                         return false;
378
379                 return true;
380         }
381
382         ~OpenCLDevice()
383         {
384                 if(null_mem)
385                         clReleaseMemObject(CL_MEM_PTR(null_mem));
386
387                 map<string, device_vector<uchar>*>::iterator mt;
388                 for(mt = const_mem_map.begin(); mt != const_mem_map.end(); mt++) {
389                         mem_free(*(mt->second));
390                         delete mt->second;
391                 }
392
393                 if(ckPathTraceKernel)
394                         clReleaseKernel(ckPathTraceKernel);  
395                 if(ckFilmConvertKernel)
396                         clReleaseKernel(ckFilmConvertKernel);  
397                 if(cpProgram)
398                         clReleaseProgram(cpProgram);
399                 if(cqCommandQueue)
400                         clReleaseCommandQueue(cqCommandQueue);
401                 if(cxContext)
402                         clReleaseContext(cxContext);
403         }
404
405         bool support_full_kernel()
406         {
407                 return false;
408         }
409
410         string description()
411         {
412                 char name[1024];
413
414                 clGetDeviceInfo(cdDevice, CL_DEVICE_NAME, sizeof(name), &name, NULL);
415
416                 return string("OpenCL ") + name;
417         }
418
419         void mem_alloc(device_memory& mem, MemoryType type)
420         {
421                 size_t size = mem.memory_size();
422
423                 if(type == MEM_READ_ONLY)
424                         mem.device_pointer = (device_ptr)clCreateBuffer(cxContext, CL_MEM_READ_ONLY, size, NULL, &ciErr);
425                 else if(type == MEM_WRITE_ONLY)
426                         mem.device_pointer = (device_ptr)clCreateBuffer(cxContext, CL_MEM_WRITE_ONLY, size, NULL, &ciErr);
427                 else
428                         mem.device_pointer = (device_ptr)clCreateBuffer(cxContext, CL_MEM_READ_WRITE, size, NULL, &ciErr);
429
430                 opencl_assert(ciErr);
431         }
432
433         void mem_copy_to(device_memory& mem)
434         {
435                 /* this is blocking */
436                 size_t size = mem.memory_size();
437                 ciErr = clEnqueueWriteBuffer(cqCommandQueue, CL_MEM_PTR(mem.device_pointer), CL_TRUE, 0, size, (void*)mem.data_pointer, 0, NULL, NULL);
438                 opencl_assert(ciErr);
439         }
440
441         void mem_copy_from(device_memory& mem, size_t offset, size_t size)
442         {
443                 ciErr = clEnqueueReadBuffer(cqCommandQueue, CL_MEM_PTR(mem.device_pointer), CL_TRUE, offset, size, (uchar*)mem.data_pointer + offset, 0, NULL, NULL);
444                 opencl_assert(ciErr);
445         }
446
447         void mem_zero(device_memory& mem)
448         {
449                 if(mem.device_pointer) {
450                         memset((void*)mem.data_pointer, 0, mem.memory_size());
451                         mem_copy_to(mem);
452                 }
453         }
454
455         void mem_free(device_memory& mem)
456         {
457                 if(mem.device_pointer) {
458                         ciErr = clReleaseMemObject(CL_MEM_PTR(mem.device_pointer));
459                         mem.device_pointer = 0;
460                         opencl_assert(ciErr);
461                 }
462         }
463
464         void const_copy_to(const char *name, void *host, size_t size)
465         {
466                 if(const_mem_map.find(name) == const_mem_map.end()) {
467                         device_vector<uchar> *data = new device_vector<uchar>();
468                         data->copy((uchar*)host, size);
469
470                         mem_alloc(*data, MEM_READ_ONLY);
471                         const_mem_map[name] = data;
472                 }
473                 else {
474                         device_vector<uchar> *data = const_mem_map[name];
475                         data->copy((uchar*)host, size);
476                 }
477
478                 mem_copy_to(*const_mem_map[name]);
479         }
480
481         void tex_alloc(const char *name, device_memory& mem, bool interpolation, bool periodic)
482         {
483                 mem_alloc(mem, MEM_READ_ONLY);
484                 mem_copy_to(mem);
485                 mem_map[name] = &mem;
486         }
487
488         void tex_free(device_memory& mem)
489         {
490                 if(mem.data_pointer)
491                         mem_free(mem);
492         }
493
494         size_t global_size_round_up(int group_size, int global_size)
495         {
496                 int r = global_size % group_size;
497                 return global_size + ((r == 0)? 0: group_size - r);
498         }
499
500         void path_trace(DeviceTask& task)
501         {
502                 /* cast arguments to cl types */
503                 cl_mem d_data = CL_MEM_PTR(const_mem_map["__data"]->device_pointer);
504                 cl_mem d_buffer = CL_MEM_PTR(task.buffer);
505                 cl_mem d_rng_state = CL_MEM_PTR(task.rng_state);
506                 cl_int d_x = task.x;
507                 cl_int d_y = task.y;
508                 cl_int d_w = task.w;
509                 cl_int d_h = task.h;
510                 cl_int d_sample = task.sample;
511
512                 /* sample arguments */
513                 int narg = 0;
514                 ciErr = 0;
515
516                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_data), (void*)&d_data);
517                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_buffer), (void*)&d_buffer);
518                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_rng_state), (void*)&d_rng_state);
519
520 #define KERNEL_TEX(type, ttype, name) \
521         ciErr |= set_kernel_arg_mem(ckPathTraceKernel, &narg, #name);
522 #include "kernel_textures.h"
523
524                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_sample), (void*)&d_sample);
525                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_x), (void*)&d_x);
526                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_y), (void*)&d_y);
527                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_w), (void*)&d_w);
528                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_h), (void*)&d_h);
529
530                 opencl_assert(ciErr);
531
532                 size_t workgroup_size;
533
534                 clGetKernelWorkGroupInfo(ckPathTraceKernel, cdDevice,
535                         CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &workgroup_size, NULL);
536         
537                 workgroup_size = max(sqrt((double)workgroup_size), 1.0);
538
539                 size_t local_size[2] = {workgroup_size, workgroup_size};
540                 size_t global_size[2] = {global_size_round_up(local_size[0], d_w), global_size_round_up(local_size[1], d_h)};
541
542                 /* run kernel */
543                 ciErr = clEnqueueNDRangeKernel(cqCommandQueue, ckPathTraceKernel, 2, NULL, global_size, local_size, 0, NULL, NULL);
544                 opencl_assert(ciErr);
545                 opencl_assert(clFinish(cqCommandQueue));
546         }
547
548         cl_int set_kernel_arg_mem(cl_kernel kernel, int *narg, const char *name)
549         {
550                 cl_mem ptr;
551                 cl_int size, err = 0;
552
553                 if(mem_map.find(name) != mem_map.end()) {
554                         device_memory *mem = mem_map[name];
555                 
556                         ptr = CL_MEM_PTR(mem->device_pointer);
557                         size = mem->data_width;
558                 }
559                 else {
560                         /* work around NULL not working, even though the spec says otherwise */
561                         ptr = CL_MEM_PTR(null_mem);
562                         size = 1;
563                 }
564                 
565                 err |= clSetKernelArg(kernel, (*narg)++, sizeof(ptr), (void*)&ptr);
566                 opencl_assert(err);
567                 err |= clSetKernelArg(kernel, (*narg)++, sizeof(size), (void*)&size);
568                 opencl_assert(err);
569
570                 return err;
571         }
572
573         void tonemap(DeviceTask& task)
574         {
575                 /* cast arguments to cl types */
576                 cl_mem d_data = CL_MEM_PTR(const_mem_map["__data"]->device_pointer);
577                 cl_mem d_rgba = CL_MEM_PTR(task.rgba);
578                 cl_mem d_buffer = CL_MEM_PTR(task.buffer);
579                 cl_int d_x = task.x;
580                 cl_int d_y = task.y;
581                 cl_int d_w = task.w;
582                 cl_int d_h = task.h;
583                 cl_int d_sample = task.sample;
584                 cl_int d_resolution = task.resolution;
585
586                 /* sample arguments */
587                 int narg = 0;
588                 ciErr = 0;
589
590                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_data), (void*)&d_data);
591                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_rgba), (void*)&d_rgba);
592                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_buffer), (void*)&d_buffer);
593
594 #define KERNEL_TEX(type, ttype, name) \
595         ciErr |= set_kernel_arg_mem(ckFilmConvertKernel, &narg, #name);
596 #include "kernel_textures.h"
597
598                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_sample), (void*)&d_sample);
599                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_resolution), (void*)&d_resolution);
600                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_x), (void*)&d_x);
601                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_y), (void*)&d_y);
602                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_w), (void*)&d_w);
603                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_h), (void*)&d_h);
604
605                 opencl_assert(ciErr);
606
607                 size_t workgroup_size;
608
609                 clGetKernelWorkGroupInfo(ckFilmConvertKernel, cdDevice,
610                         CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &workgroup_size, NULL);
611         
612                 workgroup_size = max(sqrt((double)workgroup_size), 1.0);
613
614                 size_t local_size[2] = {workgroup_size, workgroup_size};
615                 size_t global_size[2] = {global_size_round_up(local_size[0], d_w), global_size_round_up(local_size[1], d_h)};
616
617                 /* run kernel */
618                 ciErr = clEnqueueNDRangeKernel(cqCommandQueue, ckFilmConvertKernel, 2, NULL, global_size, local_size, 0, NULL, NULL);
619                 opencl_assert(ciErr);
620                 opencl_assert(clFinish(cqCommandQueue));
621         }
622
623         void task_add(DeviceTask& task)
624         {
625                 if(task.type == DeviceTask::TONEMAP)
626                         tonemap(task);
627                 else if(task.type == DeviceTask::PATH_TRACE)
628                         path_trace(task);
629         }
630
631         void task_wait()
632         {
633         }
634
635         void task_cancel()
636         {
637         }
638 };
639
640 Device *device_opencl_create(bool background)
641 {
642         return new OpenCLDevice(background);
643 }
644
645 CCL_NAMESPACE_END
646
647 #endif /* WITH_OPENCL */
648