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