40 string filename =
"assets/input.bmp";
42 cl_context context = 0;
43 cl_command_queue commandQueue = 0;
44 cl_program program = 0;
45 cl_device_id device = 0;
47 const unsigned int numberOfMemoryObjects = 3;
48 cl_mem memoryObjects[numberOfMemoryObjects] = {0, 0, 0};
53 cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numberOfMemoryObjects);
54 cerr <<
"Failed to create an OpenCL context. " << __FILE__ <<
":"<< __LINE__ << endl;
60 cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numberOfMemoryObjects);
61 cerr <<
"Failed to create the OpenCL command queue. " << __FILE__ <<
":"<< __LINE__ << endl;
65 if (!
createProgram(context, device,
"assets/sobel.cl", &program))
67 cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numberOfMemoryObjects);
68 cerr <<
"Failed to create OpenCL program." << __FILE__ <<
":"<< __LINE__ << endl;
72 kernel = clCreateKernel(program,
"sobel", &errorNumber);
75 cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numberOfMemoryObjects);
76 cerr <<
"Failed to create OpenCL kernel. " << __FILE__ <<
":"<< __LINE__ << endl;
83 unsigned char* imageData = NULL;
86 cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numberOfMemoryObjects);
87 cerr <<
"Failed loading bitmap. " << __FILE__ <<
":"<< __LINE__ << endl;
92 size_t bufferSize = width * height *
sizeof(cl_uchar);
94 bool createMemoryObjectsSuccess =
true;
96 memoryObjects[0] = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR, bufferSize, NULL, &errorNumber);
98 memoryObjects[1] = clCreateBuffer(context, CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR, bufferSize, NULL, &errorNumber);
100 memoryObjects[2] = clCreateBuffer(context, CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR, bufferSize, NULL, &errorNumber);
101 createMemoryObjectsSuccess &=
checkSuccess(errorNumber);
102 if (!createMemoryObjectsSuccess)
104 cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numberOfMemoryObjects);
105 cerr <<
"Failed to create OpenCL buffers. " << __FILE__ <<
":"<< __LINE__ << endl;
110 cl_uchar* luminance = (cl_uchar*)clEnqueueMapBuffer(commandQueue, memoryObjects[0], CL_TRUE, CL_MAP_WRITE, 0, bufferSize, 0, NULL, NULL, &errorNumber);
113 cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numberOfMemoryObjects);
114 cerr <<
"Mapping memory objects failed " << __FILE__ <<
":"<< __LINE__ << endl;
124 if (!
checkSuccess(clEnqueueUnmapMemObject(commandQueue, memoryObjects[0], luminance, 0, NULL, NULL)))
126 cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numberOfMemoryObjects);
127 cerr <<
"Unmapping memory objects failed " << __FILE__ <<
":"<< __LINE__ << endl;
131 bool setKernelArgumentsSuccess =
true;
133 setKernelArgumentsSuccess &=
checkSuccess(clSetKernelArg(kernel, 0,
sizeof(cl_mem), &memoryObjects[0]));
134 setKernelArgumentsSuccess &=
checkSuccess(clSetKernelArg(kernel, 1,
sizeof(cl_int), &width));
135 setKernelArgumentsSuccess &=
checkSuccess(clSetKernelArg(kernel, 2,
sizeof(cl_mem), &memoryObjects[1]));
136 setKernelArgumentsSuccess &=
checkSuccess(clSetKernelArg(kernel, 3,
sizeof(cl_mem), &memoryObjects[2]));
137 if (!setKernelArgumentsSuccess)
139 cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numberOfMemoryObjects);
140 cerr <<
"Failed setting OpenCL kernel arguments. " << __FILE__ <<
":"<< __LINE__ << endl;
152 size_t globalWorksize[2] = {width / 16, height / 1};
156 if (!
checkSuccess(clEnqueueNDRangeKernel(commandQueue, kernel, 2, NULL, globalWorksize, NULL, 0, NULL, &event)))
158 cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numberOfMemoryObjects);
159 cerr <<
"Failed enqueuing the kernel. " << __FILE__ <<
":"<< __LINE__ << endl;
166 cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numberOfMemoryObjects);
167 cerr <<
"Failed waiting for kernel execution to finish. " << __FILE__ <<
":"<< __LINE__ << endl;
176 cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numberOfMemoryObjects);
177 cerr <<
"Failed releasing the event object. " << __FILE__ <<
":"<< __LINE__ << endl;
182 bool mapMemoryObjectsSuccess =
true;
183 cl_char* outputDx = (cl_char*)clEnqueueMapBuffer(commandQueue, memoryObjects[1], CL_TRUE, CL_MAP_READ, 0, bufferSize, 0, NULL, NULL, &errorNumber);
185 cl_char* outputDy = (cl_char*)clEnqueueMapBuffer(commandQueue, memoryObjects[2], CL_TRUE, CL_MAP_READ, 0, bufferSize, 0, NULL, NULL, &errorNumber);
187 if (!mapMemoryObjectsSuccess)
189 cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numberOfMemoryObjects);
190 cerr <<
"Mapping memory objects failed " << __FILE__ <<
":"<< __LINE__ << endl;
202 unsigned char *absDX =
new unsigned char[width * height];
203 unsigned char *absDY =
new unsigned char[width * height];
204 for (
int i = 0; i < width * height; i++)
206 absDX[i] = abs(outputDx[i]);
207 absDY[i] = abs(outputDy[i]);
211 bool unmapMemoryObjectsSuccess =
true;
212 unmapMemoryObjectsSuccess &=
checkSuccess(clEnqueueUnmapMemObject(commandQueue, memoryObjects[1], outputDx, 0, NULL, NULL));
213 unmapMemoryObjectsSuccess &=
checkSuccess(clEnqueueUnmapMemObject(commandQueue, memoryObjects[2], outputDy, 0, NULL, NULL));
214 if (!unmapMemoryObjectsSuccess)
216 cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numberOfMemoryObjects);
217 cerr <<
"Unmapping memory objects failed " << __FILE__ <<
":"<< __LINE__ << endl;
222 cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numberOfMemoryObjects);
225 unsigned char* rgbOut =
new unsigned char[width * height * 3];
233 unsigned char* totalOutput =
new unsigned char[width * height];
234 for (
int index = 0; index < width * height; index++)
236 totalOutput[index] = sqrt(pow(absDX[index], 2) + pow(absDY[index], 2));
244 delete [] totalOutput;