1 module dcl.error;
2 
3 public import derelict.opencl.cl;
4 
5 import std.algorithm : equal;
6 import std.format;
7 import core.exception : AssertError;
8 
9 ///
10 class CLException : Exception
11 {
12     ///
13     this( string msg, string file=__FILE__, size_t line=__LINE__ ) @safe pure nothrow
14     { super( msg, file, line ); }
15 }
16 
17 ///
18 class CLCallException : CLException
19 {
20     ///
21     string func;
22     ///
23     string[2][] args;
24     ///
25     CLError error;
26 
27     ///
28     this( string func, string[2][] args, CLError error, string file=__FILE__, size_t line=__LINE__ ) @safe pure
29     {
30         this.func = func;
31         this.args = args.dup;
32         this.error = error;
33         string msg = format( "'%s' fails with error %s", func, error );
34         super( msg, file, line );
35     }
36 }
37 
38 void assertEq(A,B)( A a, B b, string fmt="%s not equal %s", string file=__FILE__, size_t line=__LINE__ )
39 {
40     bool ok;
41     static if( is(typeof(a==b)) ) ok = a==b;
42     else static if( is(typeof(equal(a,b))) ) ok = equal(a,b);
43     else static if( is(typeof(equal!equal(a,b))) ) ok = equal!equal(a,b);
44     else static assert(0, format("not support assertEq for '%s' and '%s' types", typeid(A), typeid(B) ) );
45 
46     if( !ok ) throw new AssertError( format(fmt,a,b), file, line );
47 }
48 
49 void assertNull(A)( A a, string fmt="%s is not a null", string file=__FILE__, size_t line=__LINE__ )
50 {
51     if( a !is null ) throw new AssertError( format(fmt,a), file, line );
52 }
53 
54 //
55 enum CLError
56 {
57     NONE                                      = CL_SUCCESS, ///
58     DEVICE_NOT_FOUND                          = CL_DEVICE_NOT_FOUND, ///
59     PLATFORM_NOT_FOUND                        = CL_PLATFORM_NOT_FOUND_KHR, ///
60     DEVICE_NOT_AVAILABLE                      = CL_DEVICE_NOT_AVAILABLE, ///
61     INVALID_PARTITION_COUNT                   = CL_INVALID_PARTITION_COUNT_EXT, ///
62     INVALID_PARTITION_NAME                    = CL_INVALID_PARTITION_NAME_EXT, ///
63     COMPILER_NOT_AVAILABLE                    = CL_COMPILER_NOT_AVAILABLE, ///
64     MEM_OBJECT_ALLOCATION_FAILURE             = CL_MEM_OBJECT_ALLOCATION_FAILURE, ///
65     OUT_OF_RESOURCES                          = CL_OUT_OF_RESOURCES, ///
66     OUT_OF_HOST_MEMORY                        = CL_OUT_OF_HOST_MEMORY, ///
67     PROFILING_INFO_NOT_AVAILABLE              = CL_PROFILING_INFO_NOT_AVAILABLE, ///
68     MEM_COPY_OVERLAP                          = CL_MEM_COPY_OVERLAP, ///
69     IMAGE_FORMAT_MISMATCH                     = CL_IMAGE_FORMAT_MISMATCH, ///
70     IMAGE_FORMAT_NOT_SUPPORTED                = CL_IMAGE_FORMAT_NOT_SUPPORTED, ///
71     BUILD_PROGRAM_FAILURE                     = CL_BUILD_PROGRAM_FAILURE, ///
72     MAP_FAILURE                               = CL_MAP_FAILURE, ///
73     MISALIGNED_SUB_BUFFER_OFFSET              = CL_MISALIGNED_SUB_BUFFER_OFFSET, ///
74     EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST = CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST, ///
75     COMPILE_PROGRAM_FAILURE                   = CL_COMPILE_PROGRAM_FAILURE, ///
76     LINKER_NOT_AVAILABLE                      = CL_LINKER_NOT_AVAILABLE, ///
77     LINK_PROGRAM_FAILURE                      = CL_LINK_PROGRAM_FAILURE, ///
78     DEVICE_PARTITION_FAILED                   = CL_DEVICE_PARTITION_FAILED, ///
79     KERNEL_ARG_INFO_NOT_AVAILABLE             = CL_KERNEL_ARG_INFO_NOT_AVAILABLE, ///
80     INVALID_VALUE                             = CL_INVALID_VALUE, ///
81     INVALID_DEVICE_TYPE                       = CL_INVALID_DEVICE_TYPE, ///
82     INVALID_PLATFORM                          = CL_INVALID_PLATFORM, ///
83     INVALID_DEVICE                            = CL_INVALID_DEVICE, ///
84     INVALID_CONTEXT                           = CL_INVALID_CONTEXT, ///
85     INVALID_QUEUE_PROPERTIES                  = CL_INVALID_QUEUE_PROPERTIES, ///
86     INVALID_COMMAND_QUEUE                     = CL_INVALID_COMMAND_QUEUE, ///
87     INVALID_HOST_PTR                          = CL_INVALID_HOST_PTR, ///
88     INVALID_MEM_OBJECT                        = CL_INVALID_MEM_OBJECT, ///
89     INVALID_IMAGE_FORMAT_DESCRIPTOR           = CL_INVALID_IMAGE_FORMAT_DESCRIPTOR, ///
90     INVALID_IMAGE_SIZE                        = CL_INVALID_IMAGE_SIZE, ///
91     INVALID_SAMPLER                           = CL_INVALID_SAMPLER, ///
92     INVALID_BINARY                            = CL_INVALID_BINARY, ///
93     INVALID_BUILD_OPTIONS                     = CL_INVALID_BUILD_OPTIONS, ///
94     INVALID_PROGRAM                           = CL_INVALID_PROGRAM, ///
95     INVALID_PROGRAM_EXECUTABLE                = CL_INVALID_PROGRAM_EXECUTABLE, ///
96     INVALID_KERNEL_NAME                       = CL_INVALID_KERNEL_NAME, ///
97     INVALID_KERNEL_DEFINITION                 = CL_INVALID_KERNEL_DEFINITION, ///
98     INVALID_KERNEL                            = CL_INVALID_KERNEL, ///
99     INVALID_ARG_INDEX                         = CL_INVALID_ARG_INDEX, ///
100     INVALID_ARG_VALUE                         = CL_INVALID_ARG_VALUE, ///
101     INVALID_ARG_SIZE                          = CL_INVALID_ARG_SIZE, ///
102     INVALID_KERNEL_ARGS                       = CL_INVALID_KERNEL_ARGS, ///
103     INVALID_WORK_DIMENSION                    = CL_INVALID_WORK_DIMENSION, ///
104     INVALID_WORK_GROUP_SIZE                   = CL_INVALID_WORK_GROUP_SIZE, ///
105     INVALID_WORK_ITEM_SIZE                    = CL_INVALID_WORK_ITEM_SIZE, ///
106     INVALID_GLOBAL_OFFSET                     = CL_INVALID_GLOBAL_OFFSET, ///
107     INVALID_EVENT_WAIT_LIST                   = CL_INVALID_EVENT_WAIT_LIST, ///
108     INVALID_EVENT                             = CL_INVALID_EVENT, ///
109     INVALID_OPERATION                         = CL_INVALID_OPERATION, ///
110     INVALID_GL_OBJECT                         = CL_INVALID_GL_OBJECT, ///
111     INVALID_BUFFER_SIZE                       = CL_INVALID_BUFFER_SIZE, ///
112     INVALID_MIP_LEVEL                         = CL_INVALID_MIP_LEVEL, ///
113     INVALID_GLOBAL_WORK_SIZE                  = CL_INVALID_GLOBAL_WORK_SIZE, ///
114     INVALID_PROPERTY                          = CL_INVALID_PROPERTY, ///
115     INVALID_IMAGE_DESCRIPTOR                  = CL_INVALID_IMAGE_DESCRIPTOR, ///
116     INVALID_COMPILER_OPTIONS                  = CL_INVALID_COMPILER_OPTIONS, ///
117     INVALID_LINKER_OPTIONS                    = CL_INVALID_LINKER_OPTIONS, ///
118     INVALID_DEVICE_PARTITION_COUNT            = CL_INVALID_DEVICE_PARTITION_COUNT, ///
119 }
120