18#ifndef MAGICKCORE_OPENCL_PRIVATE_H
19#define MAGICKCORE_OPENCL_PRIVATE_H
24#include "MagickCore/studio.h"
25#include "MagickCore/opencl.h"
26#include "MagickCore/thread_.h"
28#if defined(MAGICKCORE_HAVE_CL_CL_H)
31#if defined(MAGICKCORE_HAVE_OPENCL_CL_H)
32# include <OpenCL/cl.h>
35#if defined(__cplusplus) || defined(c_plusplus)
39#if !defined(MAGICKCORE_OPENCL_SUPPORT)
40typedef void* MagickCLCacheInfo;
42typedef struct _MagickCLCacheInfo
69#define MAGICKCORE_OPENCL_UNDEFINED_SCORE -1.0
70#define MAGICKCORE_OPENCL_COMMAND_QUEUES 16
73typedef CL_API_ENTRY cl_int
74 (CL_API_CALL *MAGICKpfn_clGetPlatformIDs)(cl_uint num_entries,
75 cl_platform_id *platforms,cl_uint *num_platforms) CL_API_SUFFIX__VERSION_1_0;
77typedef CL_API_ENTRY cl_int
78 (CL_API_CALL *MAGICKpfn_clGetPlatformInfo)(cl_platform_id platform,
79 cl_platform_info param_name,
size_t param_value_size,
void *param_value,
80 size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
84typedef CL_API_ENTRY cl_int
85 (CL_API_CALL *MAGICKpfn_clGetDeviceIDs)(cl_platform_id platform,
86 cl_device_type device_type,cl_uint num_entries,cl_device_id *devices,
87 cl_uint *num_devices) CL_API_SUFFIX__VERSION_1_0;
89typedef CL_API_ENTRY cl_int
90 (CL_API_CALL *MAGICKpfn_clGetDeviceInfo)(cl_device_id device,
91 cl_device_info param_name,
size_t param_value_size,
void *param_value,
92 size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
96typedef CL_API_ENTRY cl_context
97 (CL_API_CALL *MAGICKpfn_clCreateContext)(
98 const cl_context_properties *properties,cl_uint num_devices,
99 const cl_device_id *devices,void (CL_CALLBACK *pfn_notify)(
const char *,
100 const void *,size_t,
void *),
void *user_data,cl_int *errcode_ret)
101 CL_API_SUFFIX__VERSION_1_0;
103typedef CL_API_ENTRY cl_int
104 (CL_API_CALL *MAGICKpfn_clReleaseContext)(cl_context context)
105 CL_API_SUFFIX__VERSION_1_0;
109typedef CL_API_ENTRY cl_command_queue
110 (CL_API_CALL *MAGICKpfn_clCreateCommandQueue)(cl_context context,
111 cl_device_id device,cl_command_queue_properties properties,
112 cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
114typedef CL_API_ENTRY cl_int
115 (CL_API_CALL *MAGICKpfn_clReleaseCommandQueue)(
116 cl_command_queue command_queue) CL_API_SUFFIX__VERSION_1_0;
118typedef CL_API_ENTRY cl_int
119 (CL_API_CALL *MAGICKpfn_clFlush)(cl_command_queue command_queue)
120 CL_API_SUFFIX__VERSION_1_0;
122typedef CL_API_ENTRY cl_int
123 (CL_API_CALL *MAGICKpfn_clFinish)(cl_command_queue command_queue)
124 CL_API_SUFFIX__VERSION_1_0;
128typedef CL_API_ENTRY cl_mem
129 (CL_API_CALL *MAGICKpfn_clCreateBuffer)(cl_context context,
130 cl_mem_flags flags,
size_t size,
void *host_ptr,cl_int *errcode_ret)
131 CL_API_SUFFIX__VERSION_1_0;
133typedef CL_API_ENTRY cl_int
134 (CL_API_CALL *MAGICKpfn_clRetainMemObject)(cl_mem memobj)
135 CL_API_SUFFIX__VERSION_1_0;
137typedef CL_API_ENTRY cl_int
138 (CL_API_CALL *MAGICKpfn_clReleaseMemObject)(cl_mem memobj)
139 CL_API_SUFFIX__VERSION_1_0;
143typedef CL_API_ENTRY cl_program
144 (CL_API_CALL *MAGICKpfn_clCreateProgramWithSource)(cl_context context,
145 cl_uint count,
const char **strings,
const size_t *lengths,
146 cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
148typedef CL_API_ENTRY cl_program
149 (CL_API_CALL *MAGICKpfn_clCreateProgramWithBinary)(cl_context context,
150 cl_uint num_devices,
const cl_device_id *device_list,
const size_t *lengths,
151 const unsigned char **binaries,cl_int *binary_status,cl_int *errcode_ret)
152 CL_API_SUFFIX__VERSION_1_0;
154typedef CL_API_ENTRY cl_int
155 (CL_API_CALL *MAGICKpfn_clReleaseProgram)(cl_program program)
156 CL_API_SUFFIX__VERSION_1_0;
158typedef CL_API_ENTRY cl_int
159 (CL_API_CALL *MAGICKpfn_clBuildProgram)(cl_program program,
160 cl_uint num_devices,
const cl_device_id *device_list,
const char *options,
161 void (CL_CALLBACK *pfn_notify)(cl_program program,
void * user_data),
162 void *user_data) CL_API_SUFFIX__VERSION_1_0;
164typedef CL_API_ENTRY cl_int
165 (CL_API_CALL *MAGICKpfn_clGetProgramBuildInfo)(cl_program program,
166 cl_device_id device,cl_program_build_info param_name,
size_t param_value_size,
167 void *param_value,
size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
169typedef CL_API_ENTRY cl_int
170 (CL_API_CALL *MAGICKpfn_clGetProgramInfo)(cl_program program,
171 cl_program_info param_name,
size_t param_value_size,
void *param_value,
172 size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
176typedef CL_API_ENTRY cl_kernel
177 (CL_API_CALL *MAGICKpfn_clCreateKernel)(cl_program program,
178 const char *kernel_name,cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
180typedef CL_API_ENTRY cl_int
181 (CL_API_CALL *MAGICKpfn_clReleaseKernel)(cl_kernel kernel)
182 CL_API_SUFFIX__VERSION_1_0;
184typedef CL_API_ENTRY cl_int
185 (CL_API_CALL *MAGICKpfn_clSetKernelArg)(cl_kernel kernel,cl_uint arg_index,
186 size_t arg_size,
const void * arg_value) CL_API_SUFFIX__VERSION_1_0;
188typedef CL_API_ENTRY cl_int
189 (CL_API_CALL *MAGICKpfn_clGetKernelInfo)(cl_kernel kernel,
190 cl_kernel_info param_name,
size_t param_value_size,
void *param_value,
191 size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
195typedef CL_API_ENTRY cl_int
196 (CL_API_CALL *MAGICKpfn_clEnqueueReadBuffer)(cl_command_queue command_queue,
197 cl_mem buffer,cl_bool blocking_read,
size_t offset,
size_t cb,
void *ptr,
198 cl_uint num_events_in_wait_list,
const cl_event *event_wait_list,
199 cl_event *event) CL_API_SUFFIX__VERSION_1_0;
201typedef CL_API_ENTRY
void
202 *(CL_API_CALL *MAGICKpfn_clEnqueueMapBuffer)(cl_command_queue command_queue,
203 cl_mem buffer,cl_bool blocking_map,cl_map_flags map_flags,
size_t offset,
204 size_t cb,cl_uint num_events_in_wait_list,
const cl_event *event_wait_list,
205 cl_event *event,cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0;
207typedef CL_API_ENTRY cl_int
208 (CL_API_CALL *MAGICKpfn_clEnqueueUnmapMemObject)(
209 cl_command_queue command_queue,cl_mem memobj,
void *mapped_ptr,
210 cl_uint num_events_in_wait_list,
const cl_event *event_wait_list,
211 cl_event *event) CL_API_SUFFIX__VERSION_1_0;
213typedef CL_API_ENTRY cl_int
214 (CL_API_CALL *MAGICKpfn_clEnqueueNDRangeKernel)(
215 cl_command_queue command_queue,cl_kernel kernel,cl_uint work_dim,
216 const size_t *global_work_offset,
const size_t *global_work_size,
217 const size_t *local_work_size,cl_uint num_events_in_wait_list,
218 const cl_event * event_wait_list,cl_event *event)
219 CL_API_SUFFIX__VERSION_1_0;
223typedef CL_API_ENTRY cl_int
224 (CL_API_CALL *MAGICKpfn_clGetEventInfo)(cl_event event,
225 cl_profiling_info param_name,
size_t param_value_size,
void *param_value,
226 size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
228typedef CL_API_ENTRY cl_int
229 (CL_API_CALL *MAGICKpfn_clWaitForEvents)(cl_uint num_events,
230 const cl_event *event_list) CL_API_SUFFIX__VERSION_1_0;
232typedef CL_API_ENTRY cl_int
233 (CL_API_CALL *MAGICKpfn_clReleaseEvent)(cl_event event)
234 CL_API_SUFFIX__VERSION_1_0;
236typedef CL_API_ENTRY cl_int
237 (CL_API_CALL *MAGICKpfn_clRetainEvent)(cl_event event)
238 CL_API_SUFFIX__VERSION_1_0;
240typedef CL_API_ENTRY cl_int
241 (CL_API_CALL *MAGICKpfn_clSetEventCallback)(cl_event event,
242 cl_int command_exec_callback_type,void (CL_CALLBACK *MAGICKpfn_notify)(
243 cl_event,cl_int,
void *),
void *user_data) CL_API_SUFFIX__VERSION_1_1;
247typedef CL_API_ENTRY cl_int
248 (CL_API_CALL *MAGICKpfn_clGetEventProfilingInfo)(cl_event event,
249 cl_profiling_info param_name,
size_t param_value_size,
void *param_value,
250 size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0;
252typedef struct MagickLibraryRec MagickLibrary;
254struct MagickLibraryRec
258 MAGICKpfn_clGetPlatformIDs clGetPlatformIDs;
259 MAGICKpfn_clGetPlatformInfo clGetPlatformInfo;
261 MAGICKpfn_clGetDeviceIDs clGetDeviceIDs;
262 MAGICKpfn_clGetDeviceInfo clGetDeviceInfo;
264 MAGICKpfn_clCreateContext clCreateContext;
265 MAGICKpfn_clReleaseContext clReleaseContext;
267 MAGICKpfn_clCreateCommandQueue clCreateCommandQueue;
268 MAGICKpfn_clReleaseCommandQueue clReleaseCommandQueue;
269 MAGICKpfn_clFlush clFlush;
270 MAGICKpfn_clFinish clFinish;
272 MAGICKpfn_clCreateBuffer clCreateBuffer;
273 MAGICKpfn_clRetainMemObject clRetainMemObject;
274 MAGICKpfn_clReleaseMemObject clReleaseMemObject;
276 MAGICKpfn_clCreateProgramWithSource clCreateProgramWithSource;
277 MAGICKpfn_clCreateProgramWithBinary clCreateProgramWithBinary;
278 MAGICKpfn_clReleaseProgram clReleaseProgram;
279 MAGICKpfn_clBuildProgram clBuildProgram;
280 MAGICKpfn_clGetProgramBuildInfo clGetProgramBuildInfo;
281 MAGICKpfn_clGetProgramInfo clGetProgramInfo;
283 MAGICKpfn_clCreateKernel clCreateKernel;
284 MAGICKpfn_clReleaseKernel clReleaseKernel;
285 MAGICKpfn_clSetKernelArg clSetKernelArg;
286 MAGICKpfn_clGetKernelInfo clGetKernelInfo;
288 MAGICKpfn_clEnqueueReadBuffer clEnqueueReadBuffer;
289 MAGICKpfn_clEnqueueMapBuffer clEnqueueMapBuffer;
290 MAGICKpfn_clEnqueueUnmapMemObject clEnqueueUnmapMemObject;
291 MAGICKpfn_clEnqueueNDRangeKernel clEnqueueNDRangeKernel;
293 MAGICKpfn_clGetEventInfo clGetEventInfo;
294 MAGICKpfn_clWaitForEvents clWaitForEvents;
295 MAGICKpfn_clReleaseEvent clReleaseEvent;
296 MAGICKpfn_clRetainEvent clRetainEvent;
297 MAGICKpfn_clSetEventCallback clSetEventCallback;
299 MAGICKpfn_clGetEventProfilingInfo clGetEventProfilingInfo;
302struct _MagickCLDevice
310 command_queues[MAGICKCORE_OPENCL_COMMAND_QUEUES];
348 command_queues_index;
354typedef struct _MagickCLEnv
383#if defined(MAGICKCORE_HDRI_SUPPORT)
384#define CLOptions "-cl-single-precision-constant -cl-mad-enable -DMAGICKCORE_HDRI_SUPPORT=1 "\
385 "-DCLQuantum=float -DCLSignedQuantum=float -DCLPixelType=float4 -DQuantumRange=%ff " \
386 "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
387 "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
388#define CLQuantum cl_float
389#define CLPixelPacket cl_float4
390#define CLCharQuantumScale 1.0f
391#elif (MAGICKCORE_QUANTUM_DEPTH == 8)
392#define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
393 "-DCLQuantum=uchar -DCLSignedQuantum=char -DCLPixelType=uchar4 -DQuantumRange=%ff " \
394 "-DQuantumScale=%ff -DCharQuantumScale=%ff -DMagickEpsilon=%ff -DMagickPI=%ff "\
395 "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
396#define CLQuantum cl_uchar
397#define CLPixelPacket cl_uchar4
398#define CLCharQuantumScale 1.0f
399#elif (MAGICKCORE_QUANTUM_DEPTH == 16)
400#define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
401 "-DCLQuantum=ushort -DCLSignedQuantum=short -DCLPixelType=ushort4 -DQuantumRange=%ff "\
402 "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
403 "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
404#define CLQuantum cl_ushort
405#define CLPixelPacket cl_ushort4
406#define CLCharQuantumScale 257.0f
407#elif (MAGICKCORE_QUANTUM_DEPTH == 32)
408#define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
409 "-DCLQuantum=uint -DCLSignedQuantum=int -DCLPixelType=uint4 -DQuantumRange=%ff "\
410 "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
411 "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
412#define CLQuantum cl_uint
413#define CLPixelPacket cl_uint4
414#define CLCharQuantumScale 16843009.0f
415#elif (MAGICKCORE_QUANTUM_DEPTH == 64)
416#define CLOptions "-cl-single-precision-constant -cl-mad-enable " \
417 "-DCLQuantum=ulong -DCLSignedQuantum=long -DCLPixelType=ulong4 -DQuantumRange=%ff "\
418 "-DQuantumScale=%f -DCharQuantumScale=%f -DMagickEpsilon=%f -DMagickPI=%f "\
419 "-DMaxMap=%u -DMAGICKCORE_QUANTUM_DEPTH=%u"
420#define CLQuantum cl_ulong
421#define CLPixelPacket cl_ulong4
422#define CLCharQuantumScale 72340172838076673.0f
425extern MagickPrivate cl_command_queue
426 AcquireOpenCLCommandQueue(MagickCLDevice);
428extern MagickPrivate cl_int
429 SetOpenCLKernelArg(cl_kernel,
size_t,
size_t,
const void *);
431extern MagickPrivate cl_kernel
432 AcquireOpenCLKernel(MagickCLDevice,
const char *);
434extern MagickPrivate cl_mem
435 CreateOpenCLBuffer(MagickCLDevice,cl_mem_flags,
size_t,
void *);
437extern MagickPrivate MagickBooleanType
438 EnqueueOpenCLKernel(cl_command_queue,cl_kernel,cl_uint,
const size_t *,
439 const size_t *,
const size_t *,
const Image *,
const Image *,
443 const char *,
const char *,
const size_t,
const ExceptionType,
const char *,
445 RecordProfileData(MagickCLDevice,cl_kernel,cl_event);
447extern MagickPrivate MagickCLCacheInfo
448 AcquireMagickCLCacheInfo(MagickCLDevice,Quantum *,
const MagickSizeType),
449 CopyMagickCLCacheInfo(MagickCLCacheInfo),
450 RelinquishMagickCLCacheInfo(MagickCLCacheInfo,
const MagickBooleanType);
452extern MagickPrivate MagickCLDevice
453 RequestOpenCLDevice(MagickCLEnv);
455extern MagickPrivate MagickCLEnv
456 GetCurrentOpenCLEnv(
void);
458extern MagickPrivate
unsigned long
459 GetOpenCLDeviceLocalMemorySize(
const MagickCLDevice);
461extern MagickPrivate
void
462 DumpOpenCLProfileData(),
464 ReleaseOpenCLCommandQueue(MagickCLDevice,cl_command_queue),
465 ReleaseOpenCLDevice(MagickCLDevice),
466 ReleaseOpenCLKernel(cl_kernel),
467 ReleaseOpenCLMemObject(cl_mem),
468 RetainOpenCLEvent(cl_event),
469 RetainOpenCLMemObject(cl_mem);
473#if defined(__cplusplus) || defined(c_plusplus)