1 //
2 // Copyright (c) 2022 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 
17 #ifndef TEST_CL_EXT_IMAGE_BUFFER
18 #define TEST_CL_EXT_IMAGE_BUFFER
19 
20 #define TEST_IMAGE_SIZE 20
21 
22 #define GET_EXTENSION_FUNC(platform, function_name)                            \
23     function_name##_fn function_name = reinterpret_cast<function_name##_fn>(   \
24         clGetExtensionFunctionAddressForPlatform(platform, #function_name));   \
25     if (function_name == nullptr)                                              \
26     {                                                                          \
27         return TEST_FAIL;                                                      \
28     }                                                                          \
29     do                                                                         \
30     {                                                                          \
31     } while (false)
32 
aligned_size(size_t size,size_t alignment)33 static inline size_t aligned_size(size_t size, size_t alignment)
34 {
35     return (size + alignment - 1) & ~(alignment - 1);
36 }
37 
aligned_ptr(void * ptr,size_t alignment)38 static inline void* aligned_ptr(void* ptr, size_t alignment)
39 {
40     return (void*)(((uintptr_t)ptr + alignment - 1) & ~(alignment - 1));
41 }
42 
get_format_size(cl_context context,cl_image_format * format,cl_mem_object_type imageType,cl_mem_flags flags)43 static inline size_t get_format_size(cl_context context,
44                                      cl_image_format* format,
45                                      cl_mem_object_type imageType,
46                                      cl_mem_flags flags)
47 {
48     cl_image_desc image_desc = { 0 };
49     image_desc.image_type = imageType;
50 
51     /* We use a width of 4 to query element size, as this is
52        the smallest possible value that satisfies the requirements
53        of all image formats (including extensions). */
54     image_desc.image_width = 4;
55     if (CL_MEM_OBJECT_IMAGE1D_BUFFER != imageType
56         && CL_MEM_OBJECT_IMAGE1D != imageType)
57     {
58         image_desc.image_height = 1;
59     }
60     if (CL_MEM_OBJECT_IMAGE3D == imageType
61         || CL_MEM_OBJECT_IMAGE2D_ARRAY == imageType)
62     {
63         image_desc.image_depth = 1;
64     }
65     if (CL_MEM_OBJECT_IMAGE1D_ARRAY == imageType
66         || CL_MEM_OBJECT_IMAGE2D_ARRAY == imageType)
67     {
68         image_desc.image_array_size = 1;
69     }
70 
71     cl_int error = 0;
72     cl_mem buffer;
73     if (imageType == CL_MEM_OBJECT_IMAGE1D_BUFFER)
74     {
75         buffer = clCreateBuffer(context, flags,
76                                 get_pixel_size(format) * image_desc.image_width,
77                                 NULL, &error);
78         test_error(error, "Unable to create buffer");
79 
80         image_desc.buffer = buffer;
81     }
82 
83     cl_mem image =
84         clCreateImage(context, flags, format, &image_desc, nullptr, &error);
85     test_error(error, "Unable to create image");
86 
87     size_t element_size = 0;
88     error = clGetImageInfo(image, CL_IMAGE_ELEMENT_SIZE, sizeof(element_size),
89                            &element_size, nullptr);
90     test_error(error, "Error clGetImageInfo");
91 
92     error = clReleaseMemObject(image);
93     test_error(error, "Unable to release image");
94 
95     if (imageType == CL_MEM_OBJECT_IMAGE1D_BUFFER)
96     {
97         error = clReleaseMemObject(buffer);
98         test_error(error, "Unable to release buffer");
99     }
100 
101     return element_size;
102 }
103 
image_desc_init(cl_image_desc * desc,cl_mem_object_type imageType)104 static inline void image_desc_init(cl_image_desc* desc,
105                                    cl_mem_object_type imageType)
106 {
107     desc->image_type = imageType;
108     desc->image_width = TEST_IMAGE_SIZE;
109     if (CL_MEM_OBJECT_IMAGE1D_BUFFER != imageType
110         && CL_MEM_OBJECT_IMAGE1D != imageType)
111     {
112         desc->image_height = TEST_IMAGE_SIZE;
113     }
114     if (CL_MEM_OBJECT_IMAGE3D == imageType
115         || CL_MEM_OBJECT_IMAGE2D_ARRAY == imageType)
116     {
117         desc->image_depth = TEST_IMAGE_SIZE;
118     }
119     if (CL_MEM_OBJECT_IMAGE1D_ARRAY == imageType
120         || CL_MEM_OBJECT_IMAGE2D_ARRAY == imageType)
121     {
122         desc->image_array_size = TEST_IMAGE_SIZE;
123     }
124 }
125 
126 #endif // TEST_CL_EXT_IMAGE_BUFFER
127