1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "action_classes.h"
17
18 #pragma mark -------------------- Base Action Class -------------------------
19
20 const cl_uint BufferSizeReductionFactor = 20;
21
IGetPreferredImageSize2D(cl_device_id device,size_t & outWidth,size_t & outHeight)22 cl_int Action::IGetPreferredImageSize2D( cl_device_id device, size_t &outWidth, size_t &outHeight )
23 {
24 cl_ulong maxAllocSize;
25 size_t maxWidth, maxHeight;
26 cl_int error;
27
28
29 // Get the largest possible buffer we could allocate
30 error = clGetDeviceInfo( device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof( maxAllocSize ), &maxAllocSize, NULL );
31 error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof( maxWidth ), &maxWidth, NULL );
32 error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_HEIGHT, sizeof( maxHeight ), &maxHeight, NULL );
33 test_error( error, "Unable to get device config" );
34
35 // Create something of a decent size
36 if( maxWidth * maxHeight * 4 > maxAllocSize / BufferSizeReductionFactor )
37 {
38 float rootSize = sqrtf( (float)( maxAllocSize / ( BufferSizeReductionFactor * 4 ) ) );
39
40 if( (size_t)rootSize > maxWidth )
41 outWidth = maxWidth;
42 else
43 outWidth = (size_t)rootSize;
44 outHeight = (size_t)( ( maxAllocSize / ( BufferSizeReductionFactor * 4 ) ) / outWidth );
45 if( outHeight > maxHeight )
46 outHeight = maxHeight;
47 }
48 else
49 {
50 outWidth = maxWidth;
51 outHeight = maxHeight;
52 }
53
54 outWidth /=2;
55 outHeight /=2;
56
57 if (outWidth > 2048)
58 outWidth = 2048;
59 if (outHeight > 2048)
60 outHeight = 2048;
61 log_info("\tImage size: %d x %d (%gMB)\n", (int)outWidth, (int)outHeight,
62 (double)((int)outWidth*(int)outHeight*4)/(1024.0*1024.0));
63 return CL_SUCCESS;
64 }
65
IGetPreferredImageSize3D(cl_device_id device,size_t & outWidth,size_t & outHeight,size_t & outDepth)66 cl_int Action::IGetPreferredImageSize3D( cl_device_id device, size_t &outWidth, size_t &outHeight, size_t &outDepth )
67 {
68 cl_ulong maxAllocSize;
69 size_t maxWidth, maxHeight, maxDepth;
70 cl_int error;
71
72
73 // Get the largest possible buffer we could allocate
74 error = clGetDeviceInfo( device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof( maxAllocSize ), &maxAllocSize, NULL );
75 error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE3D_MAX_WIDTH, sizeof( maxWidth ), &maxWidth, NULL );
76 error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE3D_MAX_HEIGHT, sizeof( maxHeight ), &maxHeight, NULL );
77 error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE3D_MAX_DEPTH, sizeof( maxDepth ), &maxDepth, NULL );
78 test_error( error, "Unable to get device config" );
79
80 // Create something of a decent size
81 if( (cl_ulong)maxWidth * maxHeight * maxDepth > maxAllocSize / ( BufferSizeReductionFactor * 4 ) )
82 {
83 float rootSize = cbrtf( (float)( maxAllocSize / ( BufferSizeReductionFactor * 4 ) ) );
84
85 if( (size_t)rootSize > maxWidth )
86 outWidth = maxWidth;
87 else
88 outWidth = (size_t)rootSize;
89 if( (size_t)rootSize > maxHeight )
90 outHeight = maxHeight;
91 else
92 outHeight = (size_t)rootSize;
93 outDepth = (size_t)( ( maxAllocSize / ( BufferSizeReductionFactor * 4 ) ) / ( outWidth * outHeight ) );
94 if( outDepth > maxDepth )
95 outDepth = maxDepth;
96 }
97 else
98 {
99 outWidth = maxWidth;
100 outHeight = maxHeight;
101 outDepth = maxDepth;
102 }
103
104 outWidth /=2;
105 outHeight /=2;
106 outDepth /=2;
107
108 if (outWidth > 512)
109 outWidth = 512;
110 if (outHeight > 512)
111 outHeight = 512;
112 if (outDepth > 512)
113 outDepth = 512;
114 log_info("\tImage size: %d x %d x %d (%gMB)\n", (int)outWidth, (int)outHeight, (int)outDepth,
115 (double)((int)outWidth*(int)outHeight*(int)outDepth*4)/(1024.0*1024.0));
116
117 return CL_SUCCESS;
118 }
119
120 #pragma mark -------------------- Execution Sub-Classes -------------------------
121
Setup(cl_device_id device,cl_context context,cl_command_queue queue)122 cl_int NDRangeKernelAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
123 {
124 const char *long_kernel[] = {
125 "__kernel void sample_test(__global float *src, __global int *dst)\n"
126 "{\n"
127 " int tid = get_global_id(0);\n"
128 " int i;\n"
129 "\n"
130 " for( i = 0; i < 100000; i++ )\n"
131 " {\n"
132 " dst[tid] = (int)src[tid] * 3;\n"
133 " }\n"
134 "\n"
135 "}\n" };
136
137 size_t threads[1] = { 1000 };
138 int error;
139
140 if( create_single_kernel_helper( context, &mProgram, &mKernel, 1, long_kernel, "sample_test" ) )
141 {
142 return -1;
143 }
144
145 error = get_max_common_work_group_size( context, mKernel, threads[0], &mLocalThreads[0] );
146 test_error( error, "Unable to get work group size to use" );
147
148 mStreams[0] = clCreateBuffer( context, (cl_mem_flags)(CL_MEM_READ_WRITE), sizeof(cl_float) * 1000, NULL, &error );
149 test_error( error, "Creating test array failed" );
150 mStreams[1] = clCreateBuffer( context, (cl_mem_flags)(CL_MEM_READ_WRITE), sizeof(cl_int) * 1000, NULL, &error );
151 test_error( error, "Creating test array failed" );
152
153 /* Set the arguments */
154 error = clSetKernelArg( mKernel, 0, sizeof( mStreams[0] ), &mStreams[0] );
155 test_error( error, "Unable to set kernel arguments" );
156 error = clSetKernelArg( mKernel, 1, sizeof( mStreams[1] ), &mStreams[1] );
157 test_error( error, "Unable to set kernel arguments" );
158
159 return CL_SUCCESS;
160 }
161
Execute(cl_command_queue queue,cl_uint numWaits,cl_event * waits,cl_event * outEvent)162 cl_int NDRangeKernelAction::Execute( cl_command_queue queue, cl_uint numWaits, cl_event *waits, cl_event *outEvent )
163 {
164 size_t threads[1] = { 1000 };
165 cl_int error = clEnqueueNDRangeKernel( queue, mKernel, 1, NULL, threads, mLocalThreads, numWaits, waits, outEvent );
166 test_error( error, "Unable to execute kernel" );
167
168 return CL_SUCCESS;
169 }
170
171 #pragma mark -------------------- Buffer Sub-Classes -------------------------
172
Setup(cl_device_id device,cl_context context,cl_command_queue queue,bool allocate)173 cl_int BufferAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue, bool allocate )
174 {
175 cl_int error;
176 cl_ulong maxAllocSize;
177
178
179 // Get the largest possible buffer we could allocate
180 error = clGetDeviceInfo( device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof( maxAllocSize ), &maxAllocSize, NULL );
181
182 // Don't create a buffer quite that big, just so we have some space left over for other work
183 mSize = (size_t)( maxAllocSize / BufferSizeReductionFactor );
184
185 // Cap at 128M so tests complete in a reasonable amount of time.
186 if (mSize > 128 << 20)
187 mSize = 128 << 20;
188
189 mSize /=2;
190
191 log_info("\tBuffer size: %gMB\n", (double)mSize/(1024.0*1024.0));
192
193 mBuffer = clCreateBuffer( context, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, mSize, NULL, &error );
194 test_error( error, "Unable to create buffer to test against" );
195
196 mOutBuffer = malloc( mSize );
197 if( mOutBuffer == NULL )
198 {
199 log_error( "ERROR: Unable to allocate temp buffer (out of memory)\n" );
200 return CL_OUT_OF_RESOURCES;
201 }
202
203 return CL_SUCCESS;
204 }
205
Setup(cl_device_id device,cl_context context,cl_command_queue queue)206 cl_int ReadBufferAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
207 {
208 return BufferAction::Setup( device, context, queue, true );
209 }
210
Execute(cl_command_queue queue,cl_uint numWaits,cl_event * waits,cl_event * outEvent)211 cl_int ReadBufferAction::Execute( cl_command_queue queue, cl_uint numWaits, cl_event *waits, cl_event *outEvent )
212 {
213 cl_int error = clEnqueueReadBuffer( queue, mBuffer, CL_FALSE, 0, mSize, mOutBuffer, numWaits, waits, outEvent );
214 test_error( error, "Unable to enqueue buffer read" );
215
216 return CL_SUCCESS;
217 }
218
Setup(cl_device_id device,cl_context context,cl_command_queue queue)219 cl_int WriteBufferAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
220 {
221 return BufferAction::Setup( device, context, queue, true );
222 }
223
Execute(cl_command_queue queue,cl_uint numWaits,cl_event * waits,cl_event * outEvent)224 cl_int WriteBufferAction::Execute( cl_command_queue queue, cl_uint numWaits, cl_event *waits, cl_event *outEvent )
225 {
226 cl_int error = clEnqueueWriteBuffer( queue, mBuffer, CL_FALSE, 0, mSize, mOutBuffer, numWaits, waits, outEvent );
227 test_error( error, "Unable to enqueue buffer write" );
228
229 return CL_SUCCESS;
230 }
231
~MapBufferAction()232 MapBufferAction::~MapBufferAction()
233 {
234 if (mQueue)
235 clEnqueueUnmapMemObject( mQueue, mBuffer, mMappedPtr, 0, NULL, NULL );
236 }
237
Setup(cl_device_id device,cl_context context,cl_command_queue queue)238 cl_int MapBufferAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
239 {
240 return BufferAction::Setup( device, context, queue, false );
241 }
242
Execute(cl_command_queue queue,cl_uint numWaits,cl_event * waits,cl_event * outEvent)243 cl_int MapBufferAction::Execute( cl_command_queue queue, cl_uint numWaits, cl_event *waits, cl_event *outEvent )
244 {
245 cl_int error;
246 mQueue = queue;
247 mMappedPtr = clEnqueueMapBuffer( queue, mBuffer, CL_FALSE, CL_MAP_READ, 0, mSize, numWaits, waits, outEvent, &error );
248 test_error( error, "Unable to enqueue buffer map" );
249
250 return CL_SUCCESS;
251 }
252
Setup(cl_device_id device,cl_context context,cl_command_queue queue)253 cl_int UnmapBufferAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
254 {
255 cl_int error = BufferAction::Setup( device, context, queue, false );
256 if( error != CL_SUCCESS )
257 return error;
258
259 mMappedPtr = clEnqueueMapBuffer( queue, mBuffer, CL_TRUE, CL_MAP_READ, 0, mSize, 0, NULL, NULL, &error );
260 test_error( error, "Unable to enqueue buffer map" );
261
262 return CL_SUCCESS;
263 }
264
Execute(cl_command_queue queue,cl_uint numWaits,cl_event * waits,cl_event * outEvent)265 cl_int UnmapBufferAction::Execute( cl_command_queue queue, cl_uint numWaits, cl_event *waits, cl_event *outEvent )
266 {
267 cl_int error = clEnqueueUnmapMemObject( queue, mBuffer, mMappedPtr, numWaits, waits, outEvent );
268 test_error( error, "Unable to enqueue buffer unmap" );
269
270 return CL_SUCCESS;
271 }
272
273
274 #pragma mark -------------------- Read/Write Image Classes -------------------------
275
Setup(cl_device_id device,cl_context context,cl_command_queue queue)276 cl_int ReadImage2DAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
277 {
278 cl_int error;
279
280
281 if( ( error = IGetPreferredImageSize2D( device, mWidth, mHeight ) ) )
282 return error;
283
284 cl_image_format format = { CL_RGBA, CL_SIGNED_INT8 };
285 mImage = create_image_2d( context, CL_MEM_READ_ONLY, &format, mWidth, mHeight, 0, NULL, &error );
286
287 test_error( error, "Unable to create image to test against" );
288
289 mOutput = malloc( mWidth * mHeight * 4 );
290 if( mOutput == NULL )
291 {
292 log_error( "ERROR: Unable to allocate buffer: out of memory\n" );
293 return CL_OUT_OF_RESOURCES;
294 }
295
296 return CL_SUCCESS;
297 }
298
Execute(cl_command_queue queue,cl_uint numWaits,cl_event * waits,cl_event * outEvent)299 cl_int ReadImage2DAction::Execute( cl_command_queue queue, cl_uint numWaits, cl_event *waits, cl_event *outEvent )
300 {
301 size_t origin[ 3 ] = { 0, 0, 0 }, region[ 3 ] = { mWidth, mHeight, 1 };
302
303 cl_int error = clEnqueueReadImage( queue, mImage, CL_FALSE, origin, region, 0, 0, mOutput, numWaits, waits, outEvent );
304 test_error( error, "Unable to enqueue image read" );
305
306 return CL_SUCCESS;
307 }
308
Setup(cl_device_id device,cl_context context,cl_command_queue queue)309 cl_int ReadImage3DAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
310 {
311 cl_int error;
312
313
314 if( ( error = IGetPreferredImageSize3D( device, mWidth, mHeight, mDepth ) ) )
315 return error;
316
317 cl_image_format format = { CL_RGBA, CL_SIGNED_INT8 };
318 mImage = create_image_3d( context, CL_MEM_READ_ONLY, &format, mWidth, mHeight, mDepth, 0, 0, NULL, &error );
319 test_error( error, "Unable to create image to test against" );
320
321 mOutput = malloc( mWidth * mHeight * mDepth * 4 );
322 if( mOutput == NULL )
323 {
324 log_error( "ERROR: Unable to allocate buffer: out of memory\n" );
325 return CL_OUT_OF_RESOURCES;
326 }
327
328 return CL_SUCCESS;
329 }
330
Execute(cl_command_queue queue,cl_uint numWaits,cl_event * waits,cl_event * outEvent)331 cl_int ReadImage3DAction::Execute( cl_command_queue queue, cl_uint numWaits, cl_event *waits, cl_event *outEvent )
332 {
333 size_t origin[ 3 ] = { 0, 0, 0 }, region[ 3 ] = { mWidth, mHeight, mDepth };
334
335 cl_int error = clEnqueueReadImage( queue, mImage, CL_FALSE, origin, region, 0, 0, mOutput, numWaits, waits, outEvent );
336 test_error( error, "Unable to enqueue image read" );
337
338 return CL_SUCCESS;
339 }
340
Setup(cl_device_id device,cl_context context,cl_command_queue queue)341 cl_int WriteImage2DAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
342 {
343 cl_int error;
344
345
346 if( ( error = IGetPreferredImageSize2D( device, mWidth, mHeight ) ) )
347 return error;
348
349 cl_image_format format = { CL_RGBA, CL_SIGNED_INT8 };
350 mImage = create_image_2d( context, CL_MEM_WRITE_ONLY, &format, mWidth, mHeight, 0, NULL, &error );
351 test_error( error, "Unable to create image to test against" );
352
353 mOutput = malloc( mWidth * mHeight * 4 );
354 if( mOutput == NULL )
355 {
356 log_error( "ERROR: Unable to allocate buffer: out of memory\n" );
357 return CL_OUT_OF_RESOURCES;
358 }
359
360 return CL_SUCCESS;
361 }
362
Execute(cl_command_queue queue,cl_uint numWaits,cl_event * waits,cl_event * outEvent)363 cl_int WriteImage2DAction::Execute( cl_command_queue queue, cl_uint numWaits, cl_event *waits, cl_event *outEvent )
364 {
365 size_t origin[ 3 ] = { 0, 0, 0 }, region[ 3 ] = { mWidth, mHeight, 1 };
366
367 cl_int error = clEnqueueWriteImage( queue, mImage, CL_FALSE, origin, region, 0, 0, mOutput, numWaits, waits, outEvent );
368 test_error( error, "Unable to enqueue image write" );
369
370 return CL_SUCCESS;
371 }
372
Setup(cl_device_id device,cl_context context,cl_command_queue queue)373 cl_int WriteImage3DAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
374 {
375 cl_int error;
376
377
378 if( ( error = IGetPreferredImageSize3D( device, mWidth, mHeight, mDepth ) ) )
379 return error;
380
381 cl_image_format format = { CL_RGBA, CL_SIGNED_INT8 };
382 mImage = create_image_3d( context, CL_MEM_READ_ONLY, &format, mWidth, mHeight, mDepth, 0, 0, NULL, &error );
383 test_error( error, "Unable to create image to test against" );
384
385 mOutput = malloc( mWidth * mHeight * mDepth * 4 );
386 if( mOutput == NULL )
387 {
388 log_error( "ERROR: Unable to allocate buffer: out of memory\n" );
389 return CL_OUT_OF_RESOURCES;
390 }
391
392 return CL_SUCCESS;
393 }
394
Execute(cl_command_queue queue,cl_uint numWaits,cl_event * waits,cl_event * outEvent)395 cl_int WriteImage3DAction::Execute( cl_command_queue queue, cl_uint numWaits, cl_event *waits, cl_event *outEvent )
396 {
397 size_t origin[ 3 ] = { 0, 0, 0 }, region[ 3 ] = { mWidth, mHeight, mDepth };
398
399 cl_int error = clEnqueueWriteImage( queue, mImage, CL_FALSE, origin, region, 0, 0, mOutput, numWaits, waits, outEvent );
400 test_error( error, "Unable to enqueue image write" );
401
402 return CL_SUCCESS;
403 }
404
405 #pragma mark -------------------- Copy Image Classes -------------------------
406
Execute(cl_command_queue queue,cl_uint numWaits,cl_event * waits,cl_event * outEvent)407 cl_int CopyImageAction::Execute( cl_command_queue queue, cl_uint numWaits, cl_event *waits, cl_event *outEvent )
408 {
409 size_t origin[ 3 ] = { 0, 0, 0 }, region[ 3 ] = { mWidth, mHeight, mDepth };
410
411 cl_int error = clEnqueueCopyImage( queue, mSrcImage, mDstImage, origin, origin, region, numWaits, waits, outEvent );
412 test_error( error, "Unable to enqueue image copy" );
413
414 return CL_SUCCESS;
415 }
416
Setup(cl_device_id device,cl_context context,cl_command_queue queue)417 cl_int CopyImage2Dto2DAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
418 {
419 cl_int error;
420
421
422 if( ( error = IGetPreferredImageSize2D( device, mWidth, mHeight ) ) )
423 return error;
424
425 mWidth /= 2;
426
427 cl_image_format format = { CL_RGBA, CL_SIGNED_INT8 };
428 mSrcImage = create_image_2d( context, CL_MEM_READ_ONLY, &format, mWidth, mHeight, 0, NULL, &error );
429 test_error( error, "Unable to create image to test against" );
430
431 mDstImage = create_image_2d( context, CL_MEM_WRITE_ONLY, &format, mWidth, mHeight, 0, NULL, &error );
432 test_error( error, "Unable to create image to test against" );
433
434 mDepth = 1;
435 return CL_SUCCESS;
436 }
437
Setup(cl_device_id device,cl_context context,cl_command_queue queue)438 cl_int CopyImage2Dto3DAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
439 {
440 cl_int error;
441
442
443 if( ( error = IGetPreferredImageSize3D( device, mWidth, mHeight, mDepth ) ) )
444 return error;
445
446 mDepth /= 2;
447
448 cl_image_format format = { CL_RGBA, CL_SIGNED_INT8 };
449 mSrcImage = create_image_2d( context, CL_MEM_READ_ONLY, &format, mWidth, mHeight, 0, NULL, &error );
450 test_error( error, "Unable to create image to test against" );
451
452 mDstImage = create_image_3d( context, CL_MEM_READ_ONLY, &format, mWidth, mHeight, mDepth, 0, 0, NULL, &error );
453 test_error( error, "Unable to create image to test against" );
454
455 mDepth = 1;
456 return CL_SUCCESS;
457 }
458
Setup(cl_device_id device,cl_context context,cl_command_queue queue)459 cl_int CopyImage3Dto2DAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
460 {
461 cl_int error;
462
463
464 if( ( error = IGetPreferredImageSize3D( device, mWidth, mHeight, mDepth ) ) )
465 return error;
466
467 mDepth /= 2;
468
469 cl_image_format format = { CL_RGBA, CL_SIGNED_INT8 };
470 mSrcImage = create_image_3d( context, CL_MEM_READ_ONLY, &format, mWidth, mHeight, mDepth, 0, 0, NULL, &error );
471 test_error( error, "Unable to create image to test against" );
472
473 mDstImage = create_image_2d( context, CL_MEM_WRITE_ONLY, &format, mWidth, mHeight, 0, NULL, &error );
474 test_error( error, "Unable to create image to test against" );
475
476 mDepth = 1;
477 return CL_SUCCESS;
478 }
479
Setup(cl_device_id device,cl_context context,cl_command_queue queue)480 cl_int CopyImage3Dto3DAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
481 {
482 cl_int error;
483
484
485 if( ( error = IGetPreferredImageSize3D( device, mWidth, mHeight, mDepth ) ) )
486 return error;
487
488 mDepth /= 2;
489
490 cl_image_format format = { CL_RGBA, CL_SIGNED_INT8 };
491 mSrcImage = create_image_3d( context, CL_MEM_READ_ONLY, &format, mWidth, mHeight, mDepth, 0, 0, NULL, &error );
492 test_error( error, "Unable to create image to test against" );
493
494 mDstImage = create_image_3d( context, CL_MEM_READ_ONLY, &format, mWidth, mHeight, mDepth, 0, 0, NULL, &error );
495 test_error( error, "Unable to create image to test against" );
496
497 return CL_SUCCESS;
498 }
499
500 #pragma mark -------------------- Copy Image/Buffer Classes -------------------------
501
Setup(cl_device_id device,cl_context context,cl_command_queue queue)502 cl_int Copy2DImageToBufferAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
503 {
504 cl_int error;
505
506
507 if( ( error = IGetPreferredImageSize2D( device, mWidth, mHeight ) ) )
508 return error;
509
510 mWidth /= 2;
511
512 cl_image_format format = { CL_RGBA, CL_SIGNED_INT8 };
513 mSrcImage = create_image_2d( context, CL_MEM_READ_ONLY, &format, mWidth, mHeight, 0, NULL, &error );
514 test_error( error, "Unable to create image to test against" );
515
516 mDstBuffer = clCreateBuffer( context, CL_MEM_WRITE_ONLY, mWidth * mHeight * 4, NULL, &error );
517 test_error( error, "Unable to create buffer to test against" );
518
519 return CL_SUCCESS;
520 }
521
Execute(cl_command_queue queue,cl_uint numWaits,cl_event * waits,cl_event * outEvent)522 cl_int Copy2DImageToBufferAction::Execute( cl_command_queue queue, cl_uint numWaits, cl_event *waits, cl_event *outEvent )
523 {
524 size_t origin[ 3 ] = { 0, 0, 0 }, region[ 3 ] = { mWidth, mHeight, 1 };
525
526 cl_int error = clEnqueueCopyImageToBuffer( queue, mSrcImage, mDstBuffer, origin, region, 0, numWaits, waits, outEvent );
527 test_error( error, "Unable to enqueue image to buffer copy" );
528
529 return CL_SUCCESS;
530 }
531
Setup(cl_device_id device,cl_context context,cl_command_queue queue)532 cl_int Copy3DImageToBufferAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
533 {
534 cl_int error;
535
536
537 if( ( error = IGetPreferredImageSize3D( device, mWidth, mHeight, mDepth ) ) )
538 return error;
539
540 mDepth /= 2;
541
542 cl_image_format format = { CL_RGBA, CL_SIGNED_INT8 };
543 mSrcImage = create_image_3d( context, CL_MEM_READ_ONLY, &format, mWidth, mHeight, mDepth, 0, 0, NULL, &error );
544 test_error( error, "Unable to create image to test against" );
545
546 mDstBuffer = clCreateBuffer( context, CL_MEM_WRITE_ONLY, mWidth * mHeight * mDepth * 4, NULL, &error );
547 test_error( error, "Unable to create buffer to test against" );
548
549 return CL_SUCCESS;
550 }
551
Execute(cl_command_queue queue,cl_uint numWaits,cl_event * waits,cl_event * outEvent)552 cl_int Copy3DImageToBufferAction::Execute( cl_command_queue queue, cl_uint numWaits, cl_event *waits, cl_event *outEvent )
553 {
554 size_t origin[ 3 ] = { 0, 0, 0 }, region[ 3 ] = { mWidth, mHeight, mDepth };
555
556 cl_int error = clEnqueueCopyImageToBuffer( queue, mSrcImage, mDstBuffer, origin, region, 0, numWaits, waits, outEvent );
557 test_error( error, "Unable to enqueue image to buffer copy" );
558
559 return CL_SUCCESS;
560 }
561
Setup(cl_device_id device,cl_context context,cl_command_queue queue)562 cl_int CopyBufferTo2DImageAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
563 {
564 cl_int error;
565
566
567 if( ( error = IGetPreferredImageSize2D( device, mWidth, mHeight ) ) )
568 return error;
569
570 mWidth /= 2;
571
572 cl_image_format format = { CL_RGBA, CL_SIGNED_INT8 };
573
574 mSrcBuffer = clCreateBuffer( context, CL_MEM_READ_ONLY, mWidth * mHeight * 4, NULL, &error );
575 test_error( error, "Unable to create buffer to test against" );
576
577 mDstImage = create_image_2d( context, CL_MEM_WRITE_ONLY, &format, mWidth, mHeight, 0, NULL, &error );
578 test_error( error, "Unable to create image to test against" );
579
580 return CL_SUCCESS;
581 }
582
Execute(cl_command_queue queue,cl_uint numWaits,cl_event * waits,cl_event * outEvent)583 cl_int CopyBufferTo2DImageAction::Execute( cl_command_queue queue, cl_uint numWaits, cl_event *waits, cl_event *outEvent )
584 {
585 size_t origin[ 3 ] = { 0, 0, 0 }, region[ 3 ] = { mWidth, mHeight, 1 };
586
587 cl_int error = clEnqueueCopyBufferToImage( queue, mSrcBuffer, mDstImage, 0, origin, region, numWaits, waits, outEvent );
588 test_error( error, "Unable to enqueue buffer to image copy" );
589
590 return CL_SUCCESS;
591 }
592
Setup(cl_device_id device,cl_context context,cl_command_queue queue)593 cl_int CopyBufferTo3DImageAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
594 {
595 cl_int error;
596
597
598 if( ( error = IGetPreferredImageSize3D( device, mWidth, mHeight, mDepth ) ) )
599 return error;
600
601 mDepth /= 2;
602
603 mSrcBuffer = clCreateBuffer( context, CL_MEM_READ_ONLY, mWidth * mHeight * mDepth * 4, NULL, &error );
604 test_error( error, "Unable to create buffer to test against" );
605
606 cl_image_format format = { CL_RGBA, CL_SIGNED_INT8 };
607 mDstImage = create_image_3d( context, CL_MEM_READ_ONLY, &format, mWidth, mHeight, mDepth, 0, 0, NULL, &error );
608 test_error( error, "Unable to create image to test against" );
609
610 return CL_SUCCESS;
611 }
612
Execute(cl_command_queue queue,cl_uint numWaits,cl_event * waits,cl_event * outEvent)613 cl_int CopyBufferTo3DImageAction::Execute( cl_command_queue queue, cl_uint numWaits, cl_event *waits, cl_event *outEvent )
614 {
615 size_t origin[ 3 ] = { 0, 0, 0 }, region[ 3 ] = { mWidth, mHeight, mDepth };
616
617 cl_int error = clEnqueueCopyBufferToImage( queue, mSrcBuffer, mDstImage, 0, origin, region, numWaits, waits, outEvent );
618 test_error( error, "Unable to enqueue buffer to image copy" );
619
620 return CL_SUCCESS;
621 }
622
623 #pragma mark -------------------- Map Image Class -------------------------
624
~MapImageAction()625 MapImageAction::~MapImageAction()
626 {
627 if (mQueue)
628 clEnqueueUnmapMemObject( mQueue, mImage, mMappedPtr, 0, NULL, NULL );
629 }
630
Setup(cl_device_id device,cl_context context,cl_command_queue queue)631 cl_int MapImageAction::Setup( cl_device_id device, cl_context context, cl_command_queue queue )
632 {
633 cl_int error;
634
635
636 if( ( error = IGetPreferredImageSize2D( device, mWidth, mHeight ) ) )
637 return error;
638
639 cl_image_format format = { CL_RGBA, CL_SIGNED_INT8 };
640 mImage = create_image_2d( context, CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR, &format, mWidth, mHeight, 0, NULL, &error );
641 test_error( error, "Unable to create image to test against" );
642
643 return CL_SUCCESS;
644 }
645
Execute(cl_command_queue queue,cl_uint numWaits,cl_event * waits,cl_event * outEvent)646 cl_int MapImageAction::Execute( cl_command_queue queue, cl_uint numWaits, cl_event *waits, cl_event *outEvent )
647 {
648 cl_int error;
649
650 size_t origin[ 3 ] = { 0, 0, 0 }, region[ 3 ] = { mWidth, mHeight, 1 };
651 size_t outPitch;
652
653 mQueue = queue;
654 mMappedPtr = clEnqueueMapImage( queue, mImage, CL_FALSE, CL_MAP_READ, origin, region, &outPitch, NULL, numWaits, waits, outEvent, &error );
655 test_error( error, "Unable to enqueue image map" );
656
657 return CL_SUCCESS;
658 }
659