#include <memory.au3>
;8. November 2011
#AutoIt3Wrapper_usex64=n

$OpenCl_dll = DllOpen("opencl.dll")
If @error Then
    MsgBox(0, "OpenCl_inc.au3", "The file OpenCl.dll is not available. Program end!")
    Exit
EndIf

;***************************intern*****************
Global $CL_DEBUGFLAG = 1                     ; wenn 0, werden keine Debug-Informationen in die Console ausgegeben
Global $platforms = _dllstructcreate16("dword[100]") ; max 10 platforms
Global $errcode_ret = DllStructCreate("int")
Global $param_value_size_ret = _dllstructcreate16("dword")
Global $nullstruct = DllStructCreate("dword")
Global $NULL = DllStructGetPtr($nullstruct, 1);
;**************************************************


Global $CL_CONTEXT_OFFLINE_DEVICES_AMD = 0x403F
; Error Codes
Global $CL_SUCCESS = 0
Global $CL_DEVICE_NOT_FOUND = -1
Global $CL_DEVICE_NOT_AVAILABLE = -2
Global $CL_COMPILER_NOT_AVAILABLE = -3
Global $CL_MEM_OBJECT_ALLOCATION_FAILURE = -4
Global $CL_OUT_OF_RESOURCES = -5
Global $CL_OUT_OF_HOST_MEMORY = -6
Global $CL_PROFILING_INFO_NOT_AVAILABLE = -7
Global $CL_MEM_COPY_OVERLAP = -8
Global $CL_IMAGE_FORMAT_MISMATCH = -9
Global $CL_IMAGE_FORMAT_NOT_SUPPORTED = -10
Global $CL_BUILD_PROGRAM_FAILURE = -11
Global $CL_MAP_FAILURE = -12

Global $CL_INVALID_VALUE = -30
Global $CL_INVALID_DEVICE_TYPE = -31
Global $CL_INVALID_PLATFORM = -32
Global $CL_INVALID_DEVICE = -33
Global $CL_INVALID_CONTEXT = -34
Global $CL_INVALID_QUEUE_PROPERTIES = -35
Global $CL_INVALID_COMMAND_QUEUE = -36
Global $CL_INVALID_HOST_PTR = -37
Global $CL_INVALID_MEM_OBJECT = -38
Global $CL_INVALID_IMAGE_FORMAT_DESCRIPTOR = -39
Global $CL_INVALID_IMAGE_SIZE = -40
Global $CL_INVALID_SAMPLER = -41
Global $CL_INVALID_BINARY = -42
Global $CL_INVALID_BUILD_OPTIONS = -43
Global $CL_INVALID_PROGRAM = -44
Global $CL_INVALID_PROGRAM_EXECUTABLE = -45
Global $CL_INVALID_KERNEL_NAME = -46
Global $CL_INVALID_KERNEL_DEFINITION = -47
Global $CL_INVALID_KERNEL = -48
Global $CL_INVALID_ARG_INDEX = -49
Global $CL_INVALID_ARG_VALUE = -50
Global $CL_INVALID_ARG_SIZE = -51
Global $CL_INVALID_KERNEL_ARGS = -52
Global $CL_INVALID_WORK_DIMENSION = -53
Global $CL_INVALID_WORK_GROUP_SIZE = -54
Global $CL_INVALID_WORK_ITEM_SIZE = -55
Global $CL_INVALID_GLOBAL_OFFSET = -56
Global $CL_INVALID_EVENT_WAIT_LIST = -57
Global $CL_INVALID_EVENT = -58
Global $CL_INVALID_OPERATION = -59
Global $CL_INVALID_GL_OBJECT = -60
Global $CL_INVALID_BUFFER_SIZE = -61
Global $CL_INVALID_MIP_LEVEL = -62
Global $CL_INVALID_GLOBAL_WORK_SIZE = -63

; OpenCL Version
Global $CL_VERSION_1_0 = 1

; $CL_bool
Global $CL_FALSE = 0
Global $CL_TRUE = 1

; $CL_platform_info
Global $CL_PLATFORM_PROFILE = 0x00900
Global $CL_PLATFORM_VERSION = 0x00901
Global $CL_PLATFORM_NAME = 0x00902
Global $CL_PLATFORM_VENDOR = 0x00903
Global $CL_PLATFORM_EXTENSIONS = 0x00904

; $CL_device_type - bitfield
Global $CL_DEVICE_TYPE_DEFAULT = 1           ;(1 << 0)
Global $CL_DEVICE_TYPE_CPU = 2               ;(1 << 1)
Global $CL_DEVICE_TYPE_GPU = 4               ;(1 << 2)
Global $CL_DEVICE_TYPE_ACCELERATOR = 8       ;(1 << 3)
Global $CL_DEVICE_TYPE_ALL = 0xFFFFFFFF

; $CL_device_info
Global $CL_DEVICE_TYPE = 0x01000
Global $CL_DEVICE_VENDOR_ID = 0x01001
Global $CL_DEVICE_MAX_COMPUTE_UNITS = 0x01002
Global $CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS = 0x01003
Global $CL_DEVICE_MAX_WORK_GROUP_SIZE = 0x01004
Global $CL_DEVICE_MAX_WORK_ITEM_SIZES = 0x01005
Global $CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR = 0x01006
Global $CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT = 0x01007
Global $CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT = 0x01008
Global $CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG = 0x01009
Global $CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT = 0x0100A
Global $CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE = 0x0100B
Global $CL_DEVICE_MAX_CLOCK_FREQUENCY = 0x0100C
Global $CL_DEVICE_ADDRESS_BITS = 0x0100D
Global $CL_DEVICE_MAX_READ_IMAGE_ARGS = 0x0100E
Global $CL_DEVICE_MAX_WRITE_IMAGE_ARGS = 0x0100F
Global $CL_DEVICE_MAX_MEM_ALLOC_SIZE = 0x01010
Global $CL_DEVICE_IMAGE2D_MAX_WIDTH = 0x01011
Global $CL_DEVICE_IMAGE2D_MAX_HEIGHT = 0x01012
Global $CL_DEVICE_IMAGE3D_MAX_WIDTH = 0x01013
Global $CL_DEVICE_IMAGE3D_MAX_HEIGHT = 0x01014
Global $CL_DEVICE_IMAGE3D_MAX_DEPTH = 0x01015
Global $CL_DEVICE_IMAGE_SUPPORT = 0x01016
Global $CL_DEVICE_MAX_PARAMETER_SIZE = 0x01017
Global $CL_DEVICE_MAX_SAMPLERS = 0x01018
Global $CL_DEVICE_MEM_BASE_ADDR_ALIGN = 0x01019
Global $CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE = 0x0101A
Global $CL_DEVICE_SINGLE_FP_CONFIG = 0x0101B
Global $CL_DEVICE_GLOBAL_MEM_CACHE_TYPE = 0x0101C
Global $CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE = 0x0101D
Global $CL_DEVICE_GLOBAL_MEM_CACHE_SIZE = 0x0101E
Global $CL_DEVICE_GLOBAL_MEM_SIZE = 0x0101F
Global $CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE = 0x01020
Global $CL_DEVICE_MAX_CONSTANT_ARGS = 0x01021
Global $CL_DEVICE_LOCAL_MEM_TYPE = 0x01022
Global $CL_DEVICE_LOCAL_MEM_SIZE = 0x01023
Global $CL_DEVICE_ERROR_CORRECTION_SUPPORT = 0x01024
Global $CL_DEVICE_PROFILING_TIMER_RESOLUTION = 0x01025
Global $CL_DEVICE_ENDIAN_LITTLE = 0x01026
Global $CL_DEVICE_AVAILABLE = 0x01027
Global $CL_DEVICE_COMPILER_AVAILABLE = 0x01028
Global $CL_DEVICE_EXECUTION_CAPABILITIES = 0x01029
Global $CL_DEVICE_QUEUE_PROPERTIES = 0x0102A
Global $CL_DEVICE_NAME = 0x0102B
Global $CL_DEVICE_VENDOR = 0x0102C
Global $CL_DRIVER_VERSION = 0x0102D
Global $CL_DEVICE_PROFILE = 0x0102E
Global $CL_DEVICE_VERSION = 0x0102F
Global $CL_DEVICE_EXTENSIONS = 0x01030
Global $CL_DEVICE_PLATFORM = 0x01031

; $CL_device_fp_config - bitfield
Global $CL_FP_DENORM = 1                     ;(1 << 0)
Global $CL_FP_INF_NAN = 2                    ;(1 << 1)
Global $CL_FP_ROUND_TO_NEAREST = 4           ;(1 << 2)
Global $CL_FP_ROUND_TO_ZERO = 8              ;(1 << 3)
Global $CL_FP_ROUND_TO_INF = 16              ;(1 << 4)
Global $CL_FP_FMA = 32                       ;(1 << 5)

; $CL_device_mem_cache_type
Global $CL_NONE = 0x00
Global $CL_READ_ONLY_CACHE = 0x01
Global $CL_READ_WRITE_CACHE = 0x02

; $CL_device_local_mem_type
Global $CL_LOCAL = 0x01
Global $CL_GLOBAL = 0x02

; $CL_device_exec_capabilities - bitfield
Global $CL_EXEC_KERNEL = 1                   ;(1 << 0)
Global $CL_EXEC_NATIVE_KERNEL = 2            ;(1 << 1)

; $CL_command_queue_properties - bitfield
Global $CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE = 1 ;(1 << 0)
Global $CL_QUEUE_PROFILING_ENABLE = 2        ;(1 << 1)

; $CL_context_info
Global $CL_CONTEXT_REFERENCE_COUNT = 0x01080
Global $CL_CONTEXT_DEVICES = 0x01081
Global $CL_CONTEXT_PROPERTIES = 0x01082

; $CL_context_properties
Global $CL_CONTEXT_PLATFORM = 0x01084

; $CL_command_queue_info
Global $CL_QUEUE_CONTEXT = 0x01090
Global $CL_QUEUE_DEVICE = 0x01091
Global $CL_QUEUE_REFERENCE_COUNT = 0x01092
Global $CL_QUEUE_PROPERTIES = 0x01093

; $CL_mem_flags - bitfield
Global $CL_MEM_READ_WRITE = 1                ;(1 << 0)
Global $CL_MEM_WRITE_ONLY = 2                ;(1 << 1)
Global $CL_MEM_READ_ONLY = 4                 ;(1 << 2)
Global $CL_MEM_USE_HOST_PTR = 8              ;(1 << 3)
Global $CL_MEM_ALLOC_HOST_PTR = 16           ;(1 << 4)
Global $CL_MEM_COPY_HOST_PTR = 32            ;(1 << 5)

; $CL_channel_order
Global $CL_R = 0x010B0
Global $CL_A = 0x010B1
Global $CL_RG = 0x010B2
Global $CL_RA = 0x010B3
Global $CL_RGB = 0x010B4
Global $CL_RGBA = 0x010B5
Global $CL_BGRA = 0x010B6
Global $CL_ARGB = 0x010B7
Global $CL_INTENSITY = 0x010B8
Global $CL_LUMINANCE = 0x010B9

; $CL_channel_type
Global $CL_SNORM_INT8 = 0x010D0
Global $CL_SNORM_INT16 = 0x010D1
Global $CL_UNORM_INT8 = 0x010D2
Global $CL_UNORM_INT16 = 0x010D3
Global $CL_UNORM_SHORT_565 = 0x010D4
Global $CL_UNORM_SHORT_555 = 0x010D5
Global $CL_UNORM_INT_101010 = 0x010D6
Global $CL_SIGNED_INT8 = 0x010D7
Global $CL_SIGNED_INT16 = 0x010D8
Global $CL_SIGNED_INT32 = 0x010D9
Global $CL_UNSIGNED_INT8 = 0x010DA
Global $CL_UNSIGNED_INT16 = 0x010DB
Global $CL_UNSIGNED_INT32 = 0x010DC
Global $CL_HALF_FLOAT = 0x010DD
Global $CL_FLOAT = 0x010DE

; $CL_mem_object_type
Global $CL_MEM_OBJECT_BUFFER = 0x010F0
Global $CL_MEM_OBJECT_IMAGE2D = 0x010F1
Global $CL_MEM_OBJECT_IMAGE3D = 0x010F2

; $CL_mem_info
Global $CL_MEM_TYPE = 0x01100
Global $CL_MEM_FLAGS = 0x01101
Global $CL_MEM_SIZE = 0x01102
Global $CL_MEM_HOST_PTR = 0x01103
Global $CL_MEM_MAP_COUNT = 0x01104
Global $CL_MEM_REFERENCE_COUNT = 0x01105
Global $CL_MEM_CONTEXT = 0x01106

; $CL_image_info
Global $CL_IMAGE_FORMAT = 0x01110
Global $CL_IMAGE_ELEMENT_SIZE = 0x01111
Global $CL_IMAGE_ROW_PITCH = 0x01112
Global $CL_IMAGE_SLICE_PITCH = 0x01113
Global $CL_IMAGE_WIDTH = 0x01114
Global $CL_IMAGE_HEIGHT = 0x01115
Global $CL_IMAGE_DEPTH = 0x01116

; $CL_addressing_mode
Global $CL_ADDRESS_NONE = 0x01130
Global $CL_ADDRESS_CLAMP_TO_EDGE = 0x01131
Global $CL_ADDRESS_CLAMP = 0x01132
Global $CL_ADDRESS_REPEAT = 0x01133

; $CL_filter_mode
Global $CL_FILTER_NEAREST = 0x01140
Global $CL_FILTER_LINEAR = 0x01141

; $CL_sampler_info
Global $CL_SAMPLER_REFERENCE_COUNT = 0x01150
Global $CL_SAMPLER_CONTEXT = 0x01151
Global $CL_SAMPLER_NORMALIZED_COORDS = 0x01152
Global $CL_SAMPLER_ADDRESSING_MODE = 0x01153
Global $CL_SAMPLER_FILTER_MODE = 0x01154

; $CL_map_flags - bitfield
Global $CL_MAP_READ = 1                      ;(1 << 0)
Global $CL_MAP_WRITE = 2                     ;(1 << 1)

; $CL_program_info
Global $CL_PROGRAM_REFERENCE_COUNT = 0x01160
Global $CL_PROGRAM_CONTEXT = 0x01161
Global $CL_PROGRAM_NUM_DEVICES = 0x01162
Global $CL_PROGRAM_DEVICES = 0x01163
Global $CL_PROGRAM_SOURCE = 0x01164
Global $CL_PROGRAM_BINARY_SIZES = 0x01165
Global $CL_PROGRAM_BINARIES = 0x01166

; $CL_program_build_info
Global $CL_PROGRAM_BUILD_STATUS = 0x01181
Global $CL_PROGRAM_BUILD_OPTIONS = 0x01182
Global $CL_PROGRAM_BUILD_LOG = 0x01183

; $CL_build_status
Global $CL_BUILD_SUCCESS = 0
Global $CL_BUILD_NONE = -1
Global $CL_BUILD_ERROR = -2
Global $CL_BUILD_IN_PROGRESS = -3

; $CL_kernel_info
Global $CL_KERNEL_FUNCTION_NAME = 0x01190
Global $CL_KERNEL_NUM_ARGS = 0x01191
Global $CL_KERNEL_REFERENCE_COUNT = 0x01192
Global $CL_KERNEL_CONTEXT = 0x01193
Global $CL_KERNEL_PROGRAM = 0x01194

; $CL_kernel_work_group_info
Global $CL_KERNEL_WORK_GROUP_SIZE = 0x011B0
Global $CL_KERNEL_COMPILE_WORK_GROUP_SIZE = 0x011B1
Global $CL_KERNEL_LOCAL_MEM_SIZE = 0x011B2

; $CL_event_info
Global $CL_EVENT_COMMAND_QUEUE = 0x011D0
Global $CL_EVENT_COMMAND_TYPE = 0x011D1
Global $CL_EVENT_REFERENCE_COUNT = 0x011D2
Global $CL_EVENT_COMMAND_EXECUTION_STATUS = 0x011D3

; $CL_command_type
Global $CL_COMMAND_NDRANGE_KERNEL = 0x011F0
Global $CL_COMMAND_TASK = 0x011F1
Global $CL_COMMAND_NATIVE_KERNEL = 0x011F2
Global $CL_COMMAND_READ_BUFFER = 0x011F3
Global $CL_COMMAND_WRITE_BUFFER = 0x011F4
Global $CL_COMMAND_COPY_BUFFER = 0x011F5
Global $CL_COMMAND_READ_IMAGE = 0x011F6
Global $CL_COMMAND_WRITE_IMAGE = 0x011F7
Global $CL_COMMAND_COPY_IMAGE = 0x011F8
Global $CL_COMMAND_COPY_IMAGE_TO_BUFFER = 0x011F9
Global $CL_COMMAND_COPY_BUFFER_TO_IMAGE = 0x011FA
Global $CL_COMMAND_MAP_BUFFER = 0x011FB
Global $CL_COMMAND_MAP_IMAGE = 0x011FC
Global $CL_COMMAND_UNMAP_MEM_OBJECT = 0x011FD
Global $CL_COMMAND_MARKER = 0x011FE
Global $CL_COMMAND_ACQUIRE_GL_OBJECTS = 0x011FF
Global $CL_COMMAND_RELEASE_GL_OBJECTS = 0x01200

; command execution status
Global $CL_COMPLETE = 0x00
Global $CL_RUNNING = 0x01
Global $CL_SUBMITTED = 0x02
Global $CL_QUEUED = 0x03

; $CL_profiling_info
Global $CL_PROFILING_COMMAND_QUEUED = 0x01280
Global $CL_PROFILING_COMMAND_SUBMIT = 0x01281
Global $CL_PROFILING_COMMAND_START = 0x01282
Global $CL_PROFILING_COMMAND_END = 0x01283

; ====================================================================================================== ;

Global $CL_CHAR_BIT = 8
Global $CL_SCHAR_MAX = 127
Global $CL_SCHAR_MIN = (-127 - 1)
Global $CL_CHAR_MAX = $CL_SCHAR_MAX
Global $CL_CHAR_MIN = $CL_SCHAR_MIN
Global $CL_UCHAR_MAX = 255
Global $CL_SHRT_MAX = 32767
Global $CL_SHRT_MIN = (-32767 - 1)
Global $CL_USHRT_MAX = 65535
Global $CL_INT_MAX = 2147483647
Global $CL_INT_MIN = (-2147483647 - 1)
Global $CL_UINT_MAX = 0xffffffff
Global $CL_LONG_MAX = 9223372036854775807
Global $CL_LONG_MIN = -9223372036854775807
Global $CL_ULONG_MAX = 18446744073709551615

Global $CL_FLT_DIG = 6
Global $CL_FLT_MANT_DIG = 24
Global $CL_FLT_MAX_10_EXP = 38
Global $CL_FLT_MAX_EXP = 128
Global $CL_FLT_MIN_10_EXP = -37
Global $CL_FLT_MIN_EXP = -125
Global $CL_FLT_RADIX = 2
Global $CL_FLT_MAX = 340282346638528859811704183484516925440.0
Global $CL_FLT_MIN = 1.175494350822287507969 * 10 ^ (-38)
Global $CL_FLT_EPSILON = 10 ^ (-35)

Global $CL_DBL_DIG = 15
Global $CL_DBL_MANT_DIG = 53
Global $CL_DBL_MAX_10_EXP = 308
Global $CL_DBL_MAX_EXP = 1024
Global $CL_DBL_MIN_10_EXP = -307
Global $CL_DBL_MIN_EXP = -1021
Global $CL_DBL_RADIX = 2
;Global $CL_DBL_MAX  =  "(NOT PORTED)"
Global $CL_DBL_MIN = 2.225073858507201383090 * 10 ^ (-308)
Global $CL_DBL_EPSILON = 2.220446049250313080847 * 10 ^ (-16)


;****FUNKTIONEN****************

Func clGetPlatformIDs($num_entries, ByRef $platforms, ByRef $num_platforms)
    $aCall = DllCall($OpenCl_dll, "long", "clGetPlatformIDs", "dword", $num_entries, "dword", DllStructGetPtr($platforms), "long*", $num_platforms)

    ;$aCall = DllCall($OpenCl_dll, "long", "clGetPlatformIDs", "dword", 0, "dword", 0, "long*", $num_platforms)
;~     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $aCall = ' & $aCall[3] & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
;~     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $num_platforms = ' & $num_platforms & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console

;~     for $i=1 to $num_entries
;~         $platf=dllstructgetdata($platforms,1,$i)
;~         ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $platf = ' & $platf & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
;~     next
;~ msgbox(0,0,0)
    If $CL_DEBUGFLAG Then
        If @error Then
            MsgBox(0, 'Error in Function ''clGetPlatformIDs''   DllCall() failed', ' Returncode ' & @error)
            Return SetError(1, 0, 1)
        EndIf
        If OpenCL_DecodeError($aCall[0]) <> 'CL_SUCCESS' Then
            MsgBox(0, 'Error in Function clGetPlatformIDs', '  Returncode: ' & OpenCL_DecodeError($aCall[0]))
        Else
            ConsoleWrite('clGetPlatformIDs  CL_SUCCESS' & @CRLF)
        EndIf


    EndIf
    $num_platforms = $aCall[3]
    Return $aCall[0]
EndFunc                                      ;==>clGetPlatformIDs


Func clCreateContextFromType($context_prop, $device_type, $pfn_notify, $user_data, $errcode_ret)
    $aCall = DllCall($OpenCl_dll, "dword", "clCreateContextFromType", "dword", DllStructGetPtr($context_prop), "int64", $device_type, "dword", $pfn_notify, "dword", $user_data, "dword", DllStructGetPtr($errcode_ret))
    If $CL_DEBUGFLAG Then
        If @error Then
            MsgBox(0, 'Error in Function ''clCreateContextFromType''   DllCall() failed', ' Returncode ' & @error)
            Return SetError(1, 0, -1)
        EndIf
        If OpenCL_DecodeError(DllStructGetData($errcode_ret, 1)) <> 'CL_SUCCESS' Then
            MsgBox(0, 'Error in Function clCreateContextFromType', '  Returncode: ' & OpenCL_DecodeError(DllStructGetData($errcode_ret, 1)))
        Else
            ConsoleWrite('clCreateContextFromType  CL_SUCCESS' & @CRLF)
        EndIf
    EndIf
    Return $aCall[0]
EndFunc                                      ;==>clCreateContextFromType

Func clCreateBuffer($context, $flags, $size, $host_ptr, $errcode_ret)
    $aCall = DllCall($OpenCl_dll, "dword", "clCreateBuffer", "dword", $context, "int64", $flags, "dword", $size, "dword", $host_ptr, "dword", DllStructGetPtr($errcode_ret))
    If $CL_DEBUGFLAG Then
        If @error Then
            MsgBox(0, 'Error in Function ''clCreateBuffer''   DllCall() failed', ' Returncode ' & @error)
            Return SetError(1, 0, -1)
        EndIf
        If OpenCL_DecodeError(DllStructGetData($errcode_ret, 1)) <> 'CL_SUCCESS' Then
            MsgBox(0, 'Error in Function clCreateBuffer', '  Returncode: ' & OpenCL_DecodeError(DllStructGetData($errcode_ret, 1)))
        Else
            ConsoleWrite('clCreateBuffer  CL_SUCCESS' & @CRLF)
        EndIf
    EndIf
    Return $aCall[0]
EndFunc                                      ;==>clCreateBuffer


Func clGetContextInfo($context, $param_name, $param_value_size, $param_value, ByRef $param_value_size_ret)
    $aCall = DllCall($OpenCl_dll, "long", "clGetContextInfo", "dword", $context, "dword", $param_name, "dword", $param_value_size, "dword", $param_value, "dword", DllStructGetPtr($param_value_size_ret))
    If $CL_DEBUGFLAG Then
        If @error Then
            MsgBox(0, 'Error in Function ''clGetContextInfo''   DllCall() failed', ' Returncode ' & @error)
            Return SetError(1, 0, -1)
        EndIf
        If OpenCL_DecodeError($aCall[0]) <> 'CL_SUCCESS' Then
            MsgBox(0, 'Error in Function clGetContextInfo', '  Returncode: ' & OpenCL_DecodeError($aCall[0]))
        Else
            ConsoleWrite('clGetContextInfo  CL_SUCCESS' & @CRLF)
        EndIf
    EndIf
    Return $aCall[5]
EndFunc                                      ;==>clGetContextInfo


Func clGetDeviceInfo($deviceid, $param_name, $param_value_size, $param_value, ByRef $param_value_size_ret)
    $aCall = DllCall($OpenCl_dll, "dword", "clGetDeviceInfo", "dword", $deviceid, "dword", $param_name, "dword", $param_value_size, "dword", $param_value, "dword", DllStructGetPtr($param_value_size_ret))
    If $CL_DEBUGFLAG Then
        If @error Then
            MsgBox(0, 'Error in Function ''clGetDeviceInfo''   DllCall() failed', ' Returncode ' & @error)
            Return SetError(1, 0, -1)
        EndIf
        If OpenCL_DecodeError($aCall[0]) <> 'CL_SUCCESS' Then
            MsgBox(0, 'Error in Function clGetDeviceInfo', '  Returncode: ' & OpenCL_DecodeError($aCall[0]))
        Else
            ConsoleWrite('clGetDeviceInfo  CL_SUCCESS' & @CRLF)
        EndIf
    EndIf
    Return $aCall[0]
EndFunc                                      ;==>clGetDeviceInfo


Func clCreateCommandQueue($context, $device, $properties, $errcode_ret)
    $aCall = DllCall($OpenCl_dll, "dword", "clCreateCommandQueue", "dword", $context, "dword", $device, "int64", DllStructGetPtr($properties), "long", DllStructGetPtr($errcode_ret))
    $errcode_ret = $aCall[4]
    If $CL_DEBUGFLAG Then
        If @error Then
            MsgBox(0, 'Error in Function ''clCreateCommandQueue''   DllCall() failed', ' Returncode ' & @error)
            Return SetError(1, 0, -1)
        EndIf
        If OpenCL_DecodeError(DllStructGetData($errcode_ret, 1)) <> 'CL_SUCCESS' Then
            MsgBox(0, 'Error in Function clCreateCommandQueue', '  Returncode: ' & OpenCL_DecodeError(DllStructGetData($errcode_ret, 1)))
        Else
            ConsoleWrite('clCreateCommandQueue' & @TAB & @TAB & 'CL_SUCCESS' & @CRLF)
        EndIf
    EndIf
    Return $aCall[0]
EndFunc                                      ;==>clCreateCommandQueue

Func clCreateProgramWithSource($context, $count, $KernelSource, $lengths, $errcode_ret)
    $aCall = DllCall($OpenCl_dll, "dword", "clCreateProgramWithSource", "dword", $context, "dword", $count, "str*", $KernelSource, "dword*", $lengths, "long", DllStructGetPtr($errcode_ret))
    If $CL_DEBUGFLAG Then
        If @error Then
            MsgBox(0, 'Error in Function ''clCreateProgramWithSource''   DllCall() failed', ' Returncode ' & @error)
            Return SetError(1, 0, -1)
        EndIf
        If OpenCL_DecodeError(DllStructGetData($errcode_ret, 1)) <> 'CL_SUCCESS' Then
            MsgBox(0, 'Error in Function clCreateProgramWithSource', '  Returncode: ' & OpenCL_DecodeError(DllStructGetData($errcode_ret, 1)))
        Else
            ConsoleWrite('clCreateProgramWithSource  CL_SUCCESS' & @CRLF)
        EndIf
    EndIf
    Return $aCall[0]
EndFunc                                      ;==>clCreateProgramWithSource


Func clBuildProgram($program, $num_devices, $device_lists, $optionsx, $program_list, $user_data)
    $aCall = DllCall($OpenCl_dll, "long", "clBuildProgram", "dword", $program, "dword", $num_devices, "dword", $device_lists, "dword", $optionsx, "dword", $program_list, "dword", DllStructGetPtr($user_data))
    If $CL_DEBUGFLAG Then
        If @error Then
            MsgBox(0, 'Error in Function ''clBuildProgram''   DllCall() failed', ' Returncode ' & @error)
            Return SetError(1, 0, -1)
        EndIf
        If OpenCL_DecodeError($aCall[0]) <> 'CL_SUCCESS' Then
            MsgBox(0, 'Error in Function clBuildProgram', '  Returncode: ' & OpenCL_DecodeError($aCall[0]))
        Else
            ConsoleWrite('clBuildProgram  CL_SUCCESS' & @CRLF)
        EndIf
    EndIf
    Return $aCall[0]
EndFunc                                      ;==>clBuildProgram


Func clCreateKernel($program, $kernel_name, $errcode_ret)
    Local $kernel_name_struct = _dllstructcreate16("char[100]")
    DllStructSetData($kernel_name_struct, 1, $kernel_name) ;name der kernelfunktion
    $aCall = DllCall($OpenCl_dll, "dword", "clCreateKernel", "dword", $program, "ptr", DllStructGetPtr($kernel_name_struct), "long", DllStructGetPtr($errcode_ret))
    If $CL_DEBUGFLAG Then
        If @error Then
            MsgBox(0, 'Error in Function ''clCreateKernel''   DllCall() failed', ' Returncode ' & @error)
            Return SetError(1, 0, -1)
        EndIf
        If OpenCL_DecodeError(DllStructGetData($errcode_ret, 1)) <> 'CL_SUCCESS' Then
            MsgBox(0, 'Error in Function clCreateKernel', '  Returncode: ' & OpenCL_DecodeError(DllStructGetData($errcode_ret, 1)))
        Else
            ConsoleWrite('clCreateKernel  CL_SUCCESS' & @CRLF)
        EndIf
    EndIf
    Return $aCall[0]
EndFunc                                      ;==>clCreateKernel

Func clSetKernelArg($kernel, $arg_index, $arg_size, $arg_type, $arg_value)
    $aCall = DllCall($OpenCl_dll, "long", "clSetKernelArg", "dword", $kernel, "dword", $arg_index, "dword", $arg_size, $arg_type, $arg_value)
    If $CL_DEBUGFLAG Then
        If @error Then
            MsgBox(0, 'Error in Function ''clSetKernelArg''   DllCall() failed', ' Returncode ' & @error)
            Return SetError(1, 0, -1)
        EndIf
        If OpenCL_DecodeError($aCall[0]) <> 'CL_SUCCESS' Then
            MsgBox(0, 'Error in Function clSetKernelArg', '  Returncode: ' & OpenCL_DecodeError($aCall[0]))
        Else
            ConsoleWrite('clSetKernelArg  CL_SUCCESS' & @CRLF)
        EndIf
    EndIf
    Return $aCall[0]
EndFunc                                      ;==>clSetKernelArg


Func clEnqueueNDRangeKernel($command_queue, $kernel, $work_dim, $global_work_offset, $global_work_size, $local_work_size, ByRef $num_events_in_wait_list, $event_wait_list, $eventlist)
    $aCall = DllCall($OpenCl_dll, "long", "clEnqueueNDRangeKernel", "dword", $command_queue, "dword", $kernel, "dword", $work_dim, "dword", $global_work_offset, "dword", $global_work_size, "dword", $local_work_size, "dword", $num_events_in_wait_list, "dword", DllStructGetPtr($event_wait_list), "long", DllStructGetPtr($eventlist))
    If $CL_DEBUGFLAG Then
        If @error Then
            MsgBox(0, 'Error in Function ''clEnqueueNDRangeKernel''   DllCall() failed', ' Returncode ' & @error)
            Return SetError(1, 0, -1)
        EndIf
        If OpenCL_DecodeError($aCall[0]) <> 'CL_SUCCESS' Then
            MsgBox(0, 'Error in Function clEnqueueNDRangeKernel', '  Returncode: ' & OpenCL_DecodeError($aCall[0]))
        Else
            ConsoleWrite('clEnqueueNDRangeKernel  CL_SUCCESS' & @CRLF)
        EndIf
    EndIf
    $num_events_in_wait_list = $aCall[7]
    Return $aCall[0]
EndFunc                                      ;==>clEnqueueNDRangeKernel


Func clEnqueueReadBuffer($command_queue, $buffer, $blocking_read, $offset, $cb, $ptr, ByRef $num_events_in_wait_list, $event_wait_list, $eventlist)
    $aCall = DllCall($OpenCl_dll, "long", "clEnqueueReadBuffer", "dword", $command_queue, "dword", $buffer, "dword", $blocking_read, "dword", $offset, "dword", $cb, "dword", $ptr, "dword", $num_events_in_wait_list, "dword", $event_wait_list, "long", DllStructGetPtr($eventlist))
    If $CL_DEBUGFLAG Then
        If @error Then
            MsgBox(0, 'Error in Function ''clEnqueueReadBuffer''   DllCall() failed', ' Returncode ' & @error)
            Return SetError(1, 0, -1)
        EndIf
        If OpenCL_DecodeError($aCall[0]) <> 'CL_SUCCESS' Then
            MsgBox(0, 'Error in Function clEnqueueReadBuffer', '  Returncode: ' & OpenCL_DecodeError($aCall[0]))
        Else
            ConsoleWrite('clEnqueueReadBuffer  CL_SUCCESS' & @CRLF)
        EndIf
    EndIf
    $num_events_in_wait_list = $aCall[7]
    Return $aCall[0]
EndFunc                                      ;==>clEnqueueReadBuffer


Func clEnqueueWriteBuffer($command_queue, $buffer, $blocking_write, $offset, $cb, $ptr, $num_events_in_wait_list, ByRef $event_wait_list, $eventlist)
    $aCall = DllCall($OpenCl_dll, "long", "clEnqueueWriteBuffer", "dword", $command_queue, "dword", $buffer, "dword", $blocking_write, "dword", $offset, "dword", $cb, "dword", $ptr, "dword", $num_events_in_wait_list, "dword", $event_wait_list, "long", DllStructGetPtr($eventlist))
    If $CL_DEBUGFLAG Then
        If @error Then
            MsgBox(0, 'Error in Function ''clEnqueueWriteBuffer''   DllCall() failed', ' Returncode ' & @error)
            Return SetError(1, 0, -1)
        EndIf
        If OpenCL_DecodeError($aCall[0]) <> 'CL_SUCCESS' Then
            MsgBox(0, 'Error in Function clEnqueueWriteBuffer', '  Returncode: ' & OpenCL_DecodeError($aCall[0]))
        Else
            ConsoleWrite('clEnqueueWriteBuffer  CL_SUCCESS' & @CRLF)
        EndIf
    EndIf
    $num_events_in_wait_list = $aCall[7]
    Return $aCall[0]
EndFunc                                      ;==>clEnqueueWriteBuffer

Func clGetProgramBuildInfo($program, $device, $param_name, $param_value_size, $param_value, ByRef $param_value_size_ret)
    $aCall = DllCall($OpenCl_dll, "long", "clGetProgramBuildInfo", "dword", $program, "dword", $device, "dword", $param_name, "dword", $param_value_size, "dword", $param_value, "dword", DllStructGetPtr($param_value_size_ret))
    If $CL_DEBUGFLAG Then
        If @error Then
            MsgBox(0, 'Error in Function ''clGetProgramBuildInfo''   DllCall() failed', ' Returncode ' & @error)
            Return SetError(1, 0, -1)
        EndIf
        If OpenCL_DecodeError($aCall[0]) <> 'CL_SUCCESS' Then
            MsgBox(0, 'Error in Function clGetProgramBuildInfo', '  Returncode: ' & OpenCL_DecodeError($aCall[0]))
        Else
            ConsoleWrite('clGetProgramBuildInfo  CL_SUCCESS' & @CRLF)
        EndIf
    EndIf
    Return $aCall[0]
EndFunc                                      ;==>clGetProgramBuildInfo


Func OpenCL_DecodeError($eCode)
    Switch $eCode
        Case 0
            Return "CL_SUCCESS"
        Case -1
            Return "CL_DEVICE_NOT_FOUND"
        Case -2
            Return "CL_DEVICE_NOT_AVAILABLE"
        Case -3
            Return "CL_COMPILER_NOT_AVAILABLE"
        Case -4
            Return "CL_MEM_OBJECT_ALLOCATION_FAILURE"
        Case -5
            Return "CL_OUT_OF_RESOURCES"
        Case -6
            Return "CL_OUT_OF_HOST_MEMORY"
        Case -7
            Return "CL_PROFILING_INFO_NOT_AVAILABLE"
        Case -8
            Return "CL_MEM_COPY_OVERLAP"
        Case -9
            Return "CL_IMAGE_FORMAT_MISMATCH"
        Case -10
            Return "CL_IMAGE_FORMAT_NOT_SUPPORTED"
        Case -11
            Return "CL_BUILD_PROGRAM_FAILURE"
        Case -12
            Return "CL_MAP_FAILURE"
        Case -30
            Return "CL_INVALID_VALUE"
        Case -31
            Return "CL_INVALID_DEVICE_TYPE"
        Case -32
            Return "CL_INVALID_PLATFORM"
        Case -33
            Return "CL_INVALID_DEVICE"
        Case -34
            Return "CL_INVALID_CONTEXT"
        Case -35
            Return "CL_INVALID_QUEUE_PROPERTIES"
        Case -36
            Return "CL_INVALID_COMMAND_QUEUE"
        Case -37
            Return "CL_INVALID_HOST_PTR"
        Case -38
            Return "CL_INVALID_MEM_OBJECT"
        Case -39
            Return "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR"
        Case -40
            Return "CL_INVALID_IMAGE_SIZE"
        Case -41
            Return "CL_INVALID_SAMPLER"
        Case -42
            Return "CL_INVALID_BINARY"
        Case -43
            Return "CL_INVALID_BUILD_OPTIONS"
        Case -44
            Return "CL_INVALID_PROGRAM"
        Case -45
            Return "CL_INVALID_PROGRAM_EXECUTABLE"
        Case -46
            Return "CL_INVALID_KERNEL_NAME"
        Case -47
            Return "CL_INVALID_KERNEL_DEFINITION"
        Case -48
            Return "CL_INVALID_KERNEL"
        Case -49
            Return "CL_INVALID_ARG_INDEX"
        Case -50
            Return "CL_INVALID_ARG_VALUE"
        Case -51
            Return "CL_INVALID_ARG_SIZE"
        Case -52
            Return "CL_INVALID_KERNEL_ARGS"
        Case -53
            Return "CL_INVALID_WORK_DIMENSION"
        Case -54
            Return "CL_INVALID_WORK_GROUP_SIZE"
        Case -55
            Return "CL_INVALID_WORK_ITEM_SIZE"
        Case -56
            Return "CL_INVALID_GLOBAL_OFFSET"
        Case -57
            Return "CL_INVALID_EVENT_WAIT_LIST"
        Case -58
            Return "CL_INVALID_EVENT"
        Case -59
            Return "CL_INVALID_OPERATION"
        Case -60
            Return "CL_INVALID_GL_OBJECT"
        Case -61
            Return "CL_INVALID_BUFFER_SIZE"
        Case -62
            Return "CL_INVALID_MIP_LEVEL"
        Case -63
            Return "CL_INVALID_GLOBAL_WORK_SIZE"
        Case Else
            Return "[!] Unknown error:" & $eCode
    EndSwitch

EndFunc                                      ;==>OpenCL_DecodeError


Func _dllstructcreate16($struct)             ;align auf 16-byte adresse
    $temp = DllStructCreate($struct)
    $tempsize = DllStructGetSize($temp) + 16
    $temp = 0
    $mem = _MemGlobalAlloc($tempsize + 16, BitOR($GMEM_FIXED, $GMEM_ZEROINIT)) ;pointer auf speicher
    $a = Mod(Number($mem), 16)               ;rest div 16 adresse = offset
    Return DllStructCreate($struct, (Number($mem) - $a + 16)) ;auf 16 alingned pointer
EndFunc                                      ;==>_dllstructcreate16

















