Cycles: OpenCL tweaks
[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
181                 int major, minor, req_major = 1, req_minor = 1;
182
183                 clGetPlatformInfo(cpPlatform, CL_PLATFORM_VERSION, sizeof(version), &version, NULL);
184
185                 if(sscanf(version, "OpenCL %d.%d", &major, &minor) < 2) {
186                         fprintf(stderr, "OpenCL: failed to parse platform version string (%s).", version);
187                         return false;
188                 }
189
190                 if(!((major == req_major && minor >= req_minor) || (major > req_major))) {
191                         fprintf(stderr, "OpenCL: platform version 1.1 or later required, found %d.%d\n", major, minor);
192                         return false;
193                 }
194
195                 clGetDeviceInfo(cdDevice, CL_DEVICE_OPENCL_C_VERSION, sizeof(version), &version, NULL);
196
197                 if(sscanf(version, "OpenCL C %d.%d", &major, &minor) < 2) {
198                         fprintf(stderr, "OpenCL: failed to parse OpenCL C version string (%s).", version);
199                         return false;
200                 }
201
202                 if(!((major == req_major && minor >= req_minor) || (major > req_major))) {
203                         fprintf(stderr, "OpenCL: C version 1.1 or later required, found %d.%d\n", major, minor);
204                         return false;
205                 }
206
207                 /* we don't check CL_DEVICE_VERSION since for e.g. nvidia sm 1.3 cards this is
208                         1.0 even if the language features are there, just limited shared memory */
209
210                 return true;
211         }
212
213         bool load_binary(const string& kernel_path, const string& clbin)
214         {
215                 /* read binary into memory */
216                 vector<uint8_t> binary;
217
218                 if(!path_read_binary(clbin, binary)) {
219                         fprintf(stderr, "OpenCL failed to read cached binary %s.\n", clbin.c_str());
220                         return false;
221                 }
222
223                 /* create program */
224                 cl_int status;
225                 size_t size = binary.size();
226                 const uint8_t *bytes = &binary[0];
227
228                 cpProgram = clCreateProgramWithBinary(cxContext, 1, &cdDevice,
229                         &size, &bytes, &status, &ciErr);
230
231                 if(opencl_error(status) || opencl_error(ciErr)) {
232                         fprintf(stderr, "OpenCL failed create program from cached binary %s.\n", clbin.c_str());
233                         return false;
234                 }
235
236                 if(!build_kernel(kernel_path))
237                         return false;
238
239                 return true;
240         }
241
242         bool save_binary(const string& clbin)
243         {
244                 size_t size = 0;
245                 clGetProgramInfo(cpProgram, CL_PROGRAM_BINARY_SIZES, sizeof(size_t), &size, NULL);
246
247                 if(!size)
248                         return false;
249
250                 vector<uint8_t> binary(size);
251                 uint8_t *bytes = &binary[0];
252
253                 clGetProgramInfo(cpProgram, CL_PROGRAM_BINARIES, sizeof(uint8_t*), &bytes, NULL);
254
255                 if(!path_write_binary(clbin, binary)) {
256                         fprintf(stderr, "OpenCL failed to write cached binary %s.\n", clbin.c_str());
257                         return false;
258                 }
259
260                 return true;
261         }
262
263         string kernel_build_options()
264         {
265                 string build_options = " -cl-fast-relaxed-math ";
266                 
267                 /* Full Shading only on NVIDIA cards at the moment */
268                 char vendor[256];
269
270                 clGetPlatformInfo(cpPlatform, CL_PLATFORM_NAME, sizeof(vendor), &vendor, NULL);
271                 string name = vendor;
272                 
273                 if(name == "NVIDIA CUDA")
274                         build_options += "-D__KERNEL_SHADING__ -D__MULTI_CLOSURE__ ";
275
276                 return build_options;
277         }
278
279         bool build_kernel(const string& kernel_path)
280         {
281                 string build_options = "";
282
283                 build_options += "-I " + kernel_path + ""; /* todo: escape path, but it doesn't get parsed correct? */
284                 build_options += kernel_build_options();
285         
286                 ciErr = clBuildProgram(cpProgram, 0, NULL, build_options.c_str(), NULL, NULL);
287
288                 if(ciErr != CL_SUCCESS) {
289                         /* show build errors */
290                         char *build_log;
291                         size_t ret_val_size;
292
293                         clGetProgramBuildInfo(cpProgram, cdDevice, CL_PROGRAM_BUILD_LOG, 0, NULL, &ret_val_size);
294
295                         build_log = new char[ret_val_size+1];
296                         clGetProgramBuildInfo(cpProgram, cdDevice, CL_PROGRAM_BUILD_LOG, ret_val_size, build_log, NULL);
297
298                         build_log[ret_val_size] = '\0';
299                         fprintf(stderr, "OpenCL build failed:\n %s\n", build_log);
300
301                         delete[] build_log;
302
303                         return false;
304                 }
305
306                 return true;
307         }
308
309         bool compile_kernel(const string& kernel_path, const string& kernel_md5)
310         {
311                 /* we compile kernels consisting of many files. unfortunately opencl
312                    kernel caches do not seem to recognize changes in included files.
313                    so we force recompile on changes by adding the md5 hash of all files */
314                 string source = "#include \"kernel.cl\" // " + kernel_md5 + "\n";
315                 size_t source_len = source.size();
316                 const char *source_str = source.c_str();
317
318                 cpProgram = clCreateProgramWithSource(cxContext, 1, &source_str, &source_len, &ciErr);
319
320                 if(opencl_error(ciErr))
321                         return false;
322
323                 double starttime = time_dt();
324                 printf("Compiling OpenCL kernel ...\n");
325
326                 if(!build_kernel(kernel_path))
327                         return false;
328
329                 printf("Kernel compilation finished in %.2lfs.\n", time_dt() - starttime);
330
331                 return true;
332         }
333
334         string device_md5_hash()
335         {
336                 MD5Hash md5;
337                 char version[256], driver[256], name[256], vendor[256];
338
339                 clGetPlatformInfo(cpPlatform, CL_PLATFORM_VENDOR, sizeof(vendor), &vendor, NULL);
340                 clGetDeviceInfo(cdDevice, CL_DEVICE_VERSION, sizeof(version), &version, NULL);
341                 clGetDeviceInfo(cdDevice, CL_DEVICE_NAME, sizeof(name), &name, NULL);
342                 clGetDeviceInfo(cdDevice, CL_DRIVER_VERSION, sizeof(driver), &driver, NULL);
343
344                 md5.append((uint8_t*)vendor, strlen(vendor));
345                 md5.append((uint8_t*)version, strlen(version));
346                 md5.append((uint8_t*)name, strlen(name));
347                 md5.append((uint8_t*)driver, strlen(driver));
348
349                 string options = kernel_build_options();
350                 md5.append((uint8_t*)options.c_str(), options.size());
351
352                 return md5.get_hex();
353         }
354
355         bool load_kernels()
356         {
357                 /* verify if device was initialized */
358                 if(!device_initialized) {
359                         fprintf(stderr, "OpenCL: failed to initialize device.\n");
360                         return false;
361                 }
362
363                 /* verify we have right opencl version */
364                 if(!opencl_version_check())
365                         return false;
366
367                 /* md5 hash to detect changes */
368                 string kernel_path = path_get("kernel");
369                 string kernel_md5 = path_files_md5_hash(kernel_path);
370                 string device_md5 = device_md5_hash();
371
372                 /* try to use cache binary */
373                 string clbin = string_printf("cycles_kernel_%s_%s.clbin", device_md5.c_str(), kernel_md5.c_str());;
374                 clbin = path_user_get(path_join("cache", clbin));
375
376                 if(path_exists(clbin)) {
377                         /* if exists already, try use it */
378                         if(!load_binary(kernel_path, clbin))
379                                 return false;
380                 }
381                 else {
382                         /* compile kernel */
383                         if(!compile_kernel(kernel_path, kernel_md5))
384                                 return false;
385
386                         /* save binary for reuse */
387                         save_binary(clbin);
388                 }
389
390                 /* find kernels */
391                 ckPathTraceKernel = clCreateKernel(cpProgram, "kernel_ocl_path_trace", &ciErr);
392                 if(opencl_error(ciErr))
393                         return false;
394
395                 ckFilmConvertKernel = clCreateKernel(cpProgram, "kernel_ocl_tonemap", &ciErr);
396                 if(opencl_error(ciErr))
397                         return false;
398
399                 return true;
400         }
401
402         ~OpenCLDevice()
403         {
404                 if(null_mem)
405                         clReleaseMemObject(CL_MEM_PTR(null_mem));
406
407                 map<string, device_vector<uchar>*>::iterator mt;
408                 for(mt = const_mem_map.begin(); mt != const_mem_map.end(); mt++) {
409                         mem_free(*(mt->second));
410                         delete mt->second;
411                 }
412
413                 if(ckPathTraceKernel)
414                         clReleaseKernel(ckPathTraceKernel);  
415                 if(ckFilmConvertKernel)
416                         clReleaseKernel(ckFilmConvertKernel);  
417                 if(cpProgram)
418                         clReleaseProgram(cpProgram);
419                 if(cqCommandQueue)
420                         clReleaseCommandQueue(cqCommandQueue);
421                 if(cxContext)
422                         clReleaseContext(cxContext);
423         }
424
425         bool support_full_kernel()
426         {
427                 return false;
428         }
429
430         string description()
431         {
432                 char name[1024];
433
434                 clGetDeviceInfo(cdDevice, CL_DEVICE_NAME, sizeof(name), &name, NULL);
435
436                 return string("OpenCL ") + name;
437         }
438
439         void mem_alloc(device_memory& mem, MemoryType type)
440         {
441                 size_t size = mem.memory_size();
442
443                 if(type == MEM_READ_ONLY)
444                         mem.device_pointer = (device_ptr)clCreateBuffer(cxContext, CL_MEM_READ_ONLY, size, NULL, &ciErr);
445                 else if(type == MEM_WRITE_ONLY)
446                         mem.device_pointer = (device_ptr)clCreateBuffer(cxContext, CL_MEM_WRITE_ONLY, size, NULL, &ciErr);
447                 else
448                         mem.device_pointer = (device_ptr)clCreateBuffer(cxContext, CL_MEM_READ_WRITE, size, NULL, &ciErr);
449
450                 opencl_assert(ciErr);
451         }
452
453         void mem_copy_to(device_memory& mem)
454         {
455                 /* this is blocking */
456                 size_t size = mem.memory_size();
457                 ciErr = clEnqueueWriteBuffer(cqCommandQueue, CL_MEM_PTR(mem.device_pointer), CL_TRUE, 0, size, (void*)mem.data_pointer, 0, NULL, NULL);
458                 opencl_assert(ciErr);
459         }
460
461         void mem_copy_from(device_memory& mem, size_t offset, size_t size)
462         {
463                 ciErr = clEnqueueReadBuffer(cqCommandQueue, CL_MEM_PTR(mem.device_pointer), CL_TRUE, offset, size, (uchar*)mem.data_pointer + offset, 0, NULL, NULL);
464                 opencl_assert(ciErr);
465         }
466
467         void mem_zero(device_memory& mem)
468         {
469                 if(mem.device_pointer) {
470                         memset((void*)mem.data_pointer, 0, mem.memory_size());
471                         mem_copy_to(mem);
472                 }
473         }
474
475         void mem_free(device_memory& mem)
476         {
477                 if(mem.device_pointer) {
478                         ciErr = clReleaseMemObject(CL_MEM_PTR(mem.device_pointer));
479                         mem.device_pointer = 0;
480                         opencl_assert(ciErr);
481                 }
482         }
483
484         void const_copy_to(const char *name, void *host, size_t size)
485         {
486                 if(const_mem_map.find(name) == const_mem_map.end()) {
487                         device_vector<uchar> *data = new device_vector<uchar>();
488                         data->copy((uchar*)host, size);
489
490                         mem_alloc(*data, MEM_READ_ONLY);
491                         const_mem_map[name] = data;
492                 }
493                 else {
494                         device_vector<uchar> *data = const_mem_map[name];
495                         data->copy((uchar*)host, size);
496                 }
497
498                 mem_copy_to(*const_mem_map[name]);
499         }
500
501         void tex_alloc(const char *name, device_memory& mem, bool interpolation, bool periodic)
502         {
503                 mem_alloc(mem, MEM_READ_ONLY);
504                 mem_copy_to(mem);
505                 mem_map[name] = &mem;
506         }
507
508         void tex_free(device_memory& mem)
509         {
510                 if(mem.data_pointer)
511                         mem_free(mem);
512         }
513
514         size_t global_size_round_up(int group_size, int global_size)
515         {
516                 int r = global_size % group_size;
517                 return global_size + ((r == 0)? 0: group_size - r);
518         }
519
520         void path_trace(DeviceTask& task)
521         {
522                 /* cast arguments to cl types */
523                 cl_mem d_data = CL_MEM_PTR(const_mem_map["__data"]->device_pointer);
524                 cl_mem d_buffer = CL_MEM_PTR(task.buffer);
525                 cl_mem d_rng_state = CL_MEM_PTR(task.rng_state);
526                 cl_int d_x = task.x;
527                 cl_int d_y = task.y;
528                 cl_int d_w = task.w;
529                 cl_int d_h = task.h;
530                 cl_int d_sample = task.sample;
531
532                 /* sample arguments */
533                 int narg = 0;
534                 ciErr = 0;
535
536                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_data), (void*)&d_data);
537                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_buffer), (void*)&d_buffer);
538                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_rng_state), (void*)&d_rng_state);
539
540 #define KERNEL_TEX(type, ttype, name) \
541         ciErr |= set_kernel_arg_mem(ckPathTraceKernel, &narg, #name);
542 #include "kernel_textures.h"
543
544                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_sample), (void*)&d_sample);
545                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_x), (void*)&d_x);
546                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_y), (void*)&d_y);
547                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_w), (void*)&d_w);
548                 ciErr |= clSetKernelArg(ckPathTraceKernel, narg++, sizeof(d_h), (void*)&d_h);
549
550                 opencl_assert(ciErr);
551
552                 size_t workgroup_size;
553
554                 clGetKernelWorkGroupInfo(ckPathTraceKernel, cdDevice,
555                         CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &workgroup_size, NULL);
556         
557                 workgroup_size = max(sqrt((double)workgroup_size), 1.0);
558
559                 size_t local_size[2] = {workgroup_size, workgroup_size};
560                 size_t global_size[2] = {global_size_round_up(local_size[0], d_w), global_size_round_up(local_size[1], d_h)};
561
562                 /* run kernel */
563                 ciErr = clEnqueueNDRangeKernel(cqCommandQueue, ckPathTraceKernel, 2, NULL, global_size, local_size, 0, NULL, NULL);
564                 opencl_assert(ciErr);
565                 opencl_assert(clFinish(cqCommandQueue));
566         }
567
568         cl_int set_kernel_arg_mem(cl_kernel kernel, int *narg, const char *name)
569         {
570                 cl_mem ptr;
571                 cl_int err = 0;
572
573                 if(mem_map.find(name) != mem_map.end()) {
574                         device_memory *mem = mem_map[name];
575                 
576                         ptr = CL_MEM_PTR(mem->device_pointer);
577                 }
578                 else {
579                         /* work around NULL not working, even though the spec says otherwise */
580                         ptr = CL_MEM_PTR(null_mem);
581                 }
582                 
583                 err |= clSetKernelArg(kernel, (*narg)++, sizeof(ptr), (void*)&ptr);
584                 opencl_assert(err);
585
586                 return err;
587         }
588
589         void tonemap(DeviceTask& task)
590         {
591                 /* cast arguments to cl types */
592                 cl_mem d_data = CL_MEM_PTR(const_mem_map["__data"]->device_pointer);
593                 cl_mem d_rgba = CL_MEM_PTR(task.rgba);
594                 cl_mem d_buffer = CL_MEM_PTR(task.buffer);
595                 cl_int d_x = task.x;
596                 cl_int d_y = task.y;
597                 cl_int d_w = task.w;
598                 cl_int d_h = task.h;
599                 cl_int d_sample = task.sample;
600                 cl_int d_resolution = task.resolution;
601
602                 /* sample arguments */
603                 int narg = 0;
604                 ciErr = 0;
605
606                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_data), (void*)&d_data);
607                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_rgba), (void*)&d_rgba);
608                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_buffer), (void*)&d_buffer);
609
610 #define KERNEL_TEX(type, ttype, name) \
611         ciErr |= set_kernel_arg_mem(ckFilmConvertKernel, &narg, #name);
612 #include "kernel_textures.h"
613
614                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_sample), (void*)&d_sample);
615                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_resolution), (void*)&d_resolution);
616                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_x), (void*)&d_x);
617                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_y), (void*)&d_y);
618                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_w), (void*)&d_w);
619                 ciErr |= clSetKernelArg(ckFilmConvertKernel, narg++, sizeof(d_h), (void*)&d_h);
620
621                 opencl_assert(ciErr);
622
623                 size_t workgroup_size;
624
625                 clGetKernelWorkGroupInfo(ckFilmConvertKernel, cdDevice,
626                         CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &workgroup_size, NULL);
627         
628                 workgroup_size = max(sqrt((double)workgroup_size), 1.0);
629
630                 size_t local_size[2] = {workgroup_size, workgroup_size};
631                 size_t global_size[2] = {global_size_round_up(local_size[0], d_w), global_size_round_up(local_size[1], d_h)};
632
633                 /* run kernel */
634                 ciErr = clEnqueueNDRangeKernel(cqCommandQueue, ckFilmConvertKernel, 2, NULL, global_size, local_size, 0, NULL, NULL);
635                 opencl_assert(ciErr);
636                 opencl_assert(clFinish(cqCommandQueue));
637         }
638
639         void task_add(DeviceTask& task)
640         {
641                 if(task.type == DeviceTask::TONEMAP)
642                         tonemap(task);
643                 else if(task.type == DeviceTask::PATH_TRACE)
644                         path_trace(task);
645         }
646
647         void task_wait()
648         {
649         }
650
651         void task_cancel()
652         {
653         }
654 };
655
656 Device *device_opencl_create(bool background)
657 {
658         return new OpenCLDevice(background);
659 }
660
661 CCL_NAMESPACE_END
662
663 #endif /* WITH_OPENCL */
664