Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 164 lines (122 sloc) 4.699 kb
03e8bc8 @ccgus Plugin for doing fun things with Core Image and OpenCL.
authored
1 #import "JSTOpenCLContext.h"
2 #import "JSTOpenCLProgram.h"
3
4 @interface JSTOpenCLContext ()
5 - (BOOL)setupContext;
6 @end
7
8 @implementation JSTOpenCLContext
9
10 @synthesize computeContext;
11 @synthesize computeCommands;
12 @synthesize computeDeviceId;
13 @synthesize computeDeviceType;
14
15 - (id)init {
16
17 self = [super init];
18 if (!self) {
19 return nil;
20 }
21
22 if ([self setupContext] == NO) {
23 [self release];
24 return nil;
25 }
26
27 return self;
28 }
29
30 - (void)dealloc {
31 clReleaseCommandQueue(computeCommands);
32 [super dealloc];
33 }
34
35 - (BOOL)setupContext {
36
37 int err = 0;
38
39 computeDeviceType = CL_DEVICE_TYPE_CPU;
40
41 err = clGetDeviceIDs(NULL, computeDeviceType, 1, &computeDeviceId, NULL);
42 if (err != CL_SUCCESS) {
43 NSLog( @"Error: Failed to locate compute device!" );
44 return NO;
45 }
46
47 // Create a context containing the compute device(s)
48 computeContext = clCreateContext(0, 1, &computeDeviceId, clLogMessagesToStdoutAPPLE, NULL, &err);
49 if (!computeContext) {
50 NSLog( @"Error: Failed to create a compute context!" );
51 return NO;
52 }
53
54 unsigned int device_count;
55 cl_device_id device_ids[16];
56 size_t returned_size;
57
58 err = clGetContextInfo(computeContext, CL_CONTEXT_DEVICES, sizeof(device_ids), device_ids, &returned_size);
59 if(err) {
60 NSLog( @"Error: Failed to retrieve compute devices for context!" );
61 return NO;
62 }
63
64 device_count = (unsigned int)(returned_size / sizeof(cl_device_id));
65
66 int i = 0;
67 //int device_found = 0;
68 cl_device_type device_type;
69 for(i = 0; i < device_count; i++) {
70 clGetDeviceInfo(device_ids[i], CL_DEVICE_TYPE, sizeof(cl_device_type), &device_type, NULL);
71
72 if(device_type == computeDeviceType) {
73 computeDeviceId = device_ids[i];
74 //device_found = 1;
75 break;
76 }
77 }
78
79 // Create a command queue
80 //
81 computeCommands = clCreateCommandQueue(computeContext, computeDeviceId, 0, &err);
82 if (!computeCommands) {
83 NSLog( @"Error: Failed to create a command queue!" );
84 return NO;
85 }
86
87 // Report the device vendor and device name
88 //
89 cl_char vendor_name[1024] = {0};
90 cl_char device_name[1024] = {0};
91 err = clGetDeviceInfo(computeDeviceId, CL_DEVICE_VENDOR, sizeof(vendor_name), vendor_name, &returned_size);
92 err|= clGetDeviceInfo(computeDeviceId, CL_DEVICE_NAME, sizeof(device_name), device_name, &returned_size);
93 if (err != CL_SUCCESS) {
94 NSLog( @"Error: Failed to retrieve device info!\n" );
95 return NO;
96 }
97
98 //debug( @"Connecting to %s %s...\n", vendor_name, device_name );
99
100 return YES;
101 }
102
103 - (JSTOpenCLProgram*)programWithSource:(NSString*) sourceCode; {
104 JSTOpenCLProgram *program = [[JSTOpenCLProgram alloc] initWithContext:self];
105 program.sourceCode = sourceCode;
106 return [program autorelease];
107 }
108
109 - (JSTOpenCLBuffer*)bufferWithSize:(size_t) size attributes:(cl_bitfield)attribs {
110 JSTOpenCLBuffer *buffer = [[JSTOpenCLBuffer alloc] initWithContext:self memory:NULL size:size attributes:attribs];
111 return [buffer autorelease];
112 }
113
114 - (JSTOpenCLBuffer*)bufferWithMemory:(void*)memory size:(size_t)size attributes:(cl_bitfield)attribs {
115 JSTOpenCLBuffer *buffer = [[JSTOpenCLBuffer alloc] initWithContext: self memory:memory size:size attributes:attribs];
116 return [buffer autorelease];
117 }
118
119 - (void)finish { // waits until all processing has finished
120 clFinish( computeCommands );
121 }
122
123
124 - (NSUInteger)maximumWorkItemDimensions {
125 cl_uint maximumWorkItemDimensions;
126
127 clGetDeviceInfo(computeDeviceId, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(cl_uint), &maximumWorkItemDimensions, NULL);
128
129 return maximumWorkItemDimensions;
130 }
131
132 - (NSUInteger)maximum2DImageWidth {
133 size_t width;
134
135 clGetDeviceInfo(computeDeviceId, CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof(size_t), &width, NULL);
136
137 return width;
138 }
139
140 - (NSUInteger)maximum2DImageHeight {
141 size_t height;
142
143 clGetDeviceInfo(computeDeviceId, CL_DEVICE_IMAGE2D_MAX_HEIGHT, sizeof(size_t), &height, NULL);
144
145 return height;
146 }
147
148 - (NSArray*)maximumWorkItemSizes {
149 NSUInteger numberOfDimensions = [self maximumWorkItemDimensions];
150 size_t *sizes = malloc(numberOfDimensions * sizeof(cl_uint));
151
152 clGetDeviceInfo(computeDeviceId, CL_DEVICE_MAX_WORK_GROUP_SIZE, numberOfDimensions * sizeof( size_t ), sizes, NULL);
153
154 NSMutableArray *sizeArray = [NSMutableArray array];
155 for ( NSInteger i = 0; i < numberOfDimensions; i++ ) {
156 [sizeArray addObject: [NSNumber numberWithInteger: sizes[i]]];
157 }
158
159 free(sizes);
160 return sizeArray;
161 }
162
163 @end
Something went wrong with that request. Please try again.