• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef test_conformance_checker_Image_MEM_HOST_READ_ONLY_h
17 #define test_conformance_checker_Image_MEM_HOST_READ_ONLY_h
18 
19 #include "checker.h"
20 
21 template < class T> class cImage_check_mem_host_read_only : public cBuffer_checker<T>
22 {
23 public:
cImage_check_mem_host_read_only(cl_device_id deviceID,cl_context context,cl_command_queue queue)24   cImage_check_mem_host_read_only(cl_device_id deviceID, cl_context context, cl_command_queue queue)
25   : cBuffer_checker <T> (deviceID, context, queue)
26   {
27     m_cl_image_format.image_channel_order = CL_RGBA;
28     m_cl_image_format.image_channel_data_type = CL_UNSIGNED_INT8;
29 
30     m_cl_Image_desc.image_type = CL_MEM_OBJECT_IMAGE1D;
31     m_cl_Image_desc.image_width = 0;
32     m_cl_Image_desc.image_height = 0;
33     m_cl_Image_desc.image_depth = 0;
34     m_cl_Image_desc.image_array_size = 0;
35     m_cl_Image_desc.image_row_pitch = 0;
36     m_cl_Image_desc.image_slice_pitch = 0;
37     m_cl_Image_desc.num_mip_levels = 0;
38     m_cl_Image_desc.num_samples = 0;
39     m_cl_Image_desc.mem_object = NULL;
40 
41     m_Image = NULL;
42   };
43 
~cImage_check_mem_host_read_only()44   ~cImage_check_mem_host_read_only()
45   {
46   };
47 
48   cl_int get_image_elements();
49 
50   cl_image_format m_cl_image_format;
51   cl_image_desc m_cl_Image_desc;
52   clMemWrapper m_Image;
53 
54   virtual cl_int SetupImage();
55   virtual cl_int SetupBuffer();
56   virtual cl_int verify_RW_Image();
57 
58   virtual cl_int verify_RW_Image_Mapping();
59   virtual cl_int verify_data(T *pdtaIn);
60   virtual cl_int verify_data_with_offset(T *pdtaIn, size_t *offset);
61 
62   cl_int get_image_content_size();
63   cl_int get_image_data_size();
64 
65   virtual cl_int verify_RW_Buffer();
66   virtual cl_int verify_RW_Buffer_rect();
67   virtual cl_int verify_RW_Buffer_mapping();
68   cl_int verify_mapping_ptr(T *ptr);
69 };
70 
71 template < class T >
verify_mapping_ptr(T * dataPtr)72 cl_int cImage_check_mem_host_read_only< T >::verify_mapping_ptr( T* dataPtr)
73 {
74   int offset_pixel = (int)(this->buffer_origin[0] + this->buffer_origin[1] *
75                            this->buffer_row_pitch_bytes/ sizeof(T) + this->buffer_origin[2] *
76                            this->buffer_slice_pitch_bytes/sizeof(T));
77 
78   dataPtr = dataPtr - offset_pixel;
79 
80   cl_int err = CL_SUCCESS;
81 
82   if (this->buffer_mem_flag & CL_MEM_USE_HOST_PTR)
83   {
84     if (this->pHost_ptr != this->host_m_1.pData)
85     {
86       log_error("Host memory pointer difference found\n");
87       return FAILURE;
88     }
89 
90     if(dataPtr != this->host_m_1.pData)
91     {
92       log_error("Mapped host pointer difference found\n");
93       return FAILURE;
94     }
95   }
96 
97   return err;
98 }
99 
100 template < class T >
verify_RW_Buffer()101 cl_int cImage_check_mem_host_read_only< T >::verify_RW_Buffer() { return CL_SUCCESS; };
102 
103 template < class T >
verify_RW_Buffer_rect()104 cl_int cImage_check_mem_host_read_only< T >::verify_RW_Buffer_rect()  { return CL_SUCCESS; };
105 
106 template < class T >
verify_RW_Buffer_mapping()107 cl_int cImage_check_mem_host_read_only< T >::verify_RW_Buffer_mapping() { return CL_SUCCESS; };
108 
109 template < class T >
SetupBuffer()110 cl_int cImage_check_mem_host_read_only< T >::SetupBuffer()
111 {
112   return cBuffer_checker< T >::SetupBuffer();
113 }
114 
115 template < class T >
get_image_content_size()116 cl_int cImage_check_mem_host_read_only< T >::get_image_content_size()
117 {
118   return ((cl_int)(m_cl_Image_desc.image_width*m_cl_Image_desc.image_height *
119                    m_cl_Image_desc.image_depth * m_cl_Image_desc.image_array_size));
120 }
121 
122 template < class T >
get_image_data_size()123 cl_int cImage_check_mem_host_read_only< T >::get_image_data_size()
124 {
125   size_t slice_pitch = m_cl_Image_desc.image_slice_pitch ? m_cl_Image_desc.image_slice_pitch :
126     (m_cl_Image_desc.image_height *  m_cl_Image_desc.image_width);
127   return (slice_pitch * m_cl_Image_desc.image_depth * m_cl_Image_desc.image_array_size);
128 }
129 
130 template < class T >
get_image_elements()131 cl_int cImage_check_mem_host_read_only< T >::get_image_elements()
132 {
133   return ((cl_int)(m_cl_Image_desc.image_width*m_cl_Image_desc.image_height *
134                    m_cl_Image_desc.image_depth * m_cl_Image_desc.image_array_size));
135 }
136 
137 template < class T >
SetupImage()138 cl_int cImage_check_mem_host_read_only< T >::SetupImage()
139 {
140   int all = (int)(m_cl_Image_desc.image_width * m_cl_Image_desc.image_height *
141                   m_cl_Image_desc.image_depth * m_cl_Image_desc.image_array_size);
142 
143   T v = TEST_VALUE;
144   this->host_m_1.Init(all, v);
145 
146   cl_int err = CL_SUCCESS;
147   this-> m_Image = clCreateImage(this->m_context, this->buffer_mem_flag,
148                                  &( this-> m_cl_image_format), &(this-> m_cl_Image_desc),
149                                  this->host_m_1.pData, &err);
150   test_error(err , "clCreateImage error");
151 
152   this-> pHost_ptr = (void *) (this->host_m_1.pData);
153 
154   return err;
155 }
156 
157 template < class T >
verify_data(T * pDataIN)158 cl_int cImage_check_mem_host_read_only< T >::verify_data(T *pDataIN)
159 {
160   cl_int err = CL_SUCCESS;
161   if (!this->host_m_1.Equal_rect_from_orig(pDataIN, this->buffer_origin,
162                                            this->region, this->host_row_pitch,
163                                            this->host_slice_pitch)) {
164     log_error("Buffer data difference found\n");
165     return FAILURE;
166   }
167 
168   return err;
169 }
170 
171 template < class T >
verify_data_with_offset(T * pDataIN,size_t * offset)172 cl_int cImage_check_mem_host_read_only< T >::verify_data_with_offset(T *pDataIN,
173                                                                      size_t *offset)
174 {
175   cl_int err = CL_SUCCESS;
176   if (!this->host_m_2.Equal_rect_from_orig(pDataIN, offset, this->region,
177                                            this->host_row_pitch,
178                                            this->host_slice_pitch)) {
179     log_error("Buffer data difference found\n");
180     return FAILURE;
181   }
182 
183   return err;
184 }
185 
186 template < class T >
verify_RW_Image()187 cl_int cImage_check_mem_host_read_only< T >::verify_RW_Image()
188 {
189   this->Init_rect();
190 
191   int imge_content_size = this->get_image_content_size();
192   T v = 0;
193   this->host_m_2.Init( imge_content_size, v);
194 
195   cl_event event;
196   cl_int err = CL_SUCCESS;
197   err = clEnqueueReadImage(this->m_queue, this->m_Image, this->m_blocking,
198                            this->buffer_origin, this->region,
199                            this-> buffer_row_pitch_bytes, this->buffer_slice_pitch_bytes,
200                            this->host_m_2.pData, 0, NULL, &event);
201 
202   test_error(err, "clEnqueueReadImage error");
203 
204   if ( !this->m_blocking) {
205     err = clWaitForEvents(1, &event);
206     test_error(err, "clWaitForEvents error");
207   }
208 
209   err = clReleaseEvent(event);
210   test_error(err, "clReleaseEvent error");
211 
212   err = this->verify_data(this->host_m_2.pData);
213   test_error(err, "verify_data error");
214 
215   err = clEnqueueWriteImage(this->m_queue, this->m_Image, this->m_blocking,
216                             this->buffer_origin, this->region,
217                             this->buffer_row_pitch_bytes, this->buffer_slice_pitch_bytes,
218                             this->host_m_2.pData, 0, NULL, &event);
219 
220   if (err == CL_SUCCESS) {
221     log_error("Calling clEnqueueWriteImage on a memory object created with the CL_MEM_HOST_READ_ONLY flag should not return CL_SUCCESS\n");
222     err = FAILURE;
223     return FAILURE;
224 
225   } else {
226     log_info("Test succeeded\n\n");
227     err = CL_SUCCESS;
228   }
229 
230   return err;
231 }
232 
233 template < class T >
verify_RW_Image_Mapping()234 cl_int cImage_check_mem_host_read_only< T >::verify_RW_Image_Mapping()
235 {
236   cl_event event;
237   cl_int err = CL_SUCCESS;
238 
239   T * dataPtr = (T*) clEnqueueMapImage(this->m_queue, this->m_Image, this->m_blocking,
240                                        CL_MAP_READ,
241                                        this->buffer_origin, this->region,
242                                        &(this-> buffer_row_pitch_bytes),
243                                        &(this->buffer_slice_pitch_bytes),
244                                        0, NULL, &event, &err);
245 
246   if (!this->m_blocking) {
247     err = clWaitForEvents(1, &event);
248     test_error(err, "clWaitForEvents error");
249   }
250 
251   err = clReleaseEvent(event);
252   test_error(err, "clReleaseEvent error");
253 
254   err= this->verify_mapping_ptr(dataPtr);
255   test_error(err, "clEnqueueMapImage error");
256 
257   err = this->verify_data(dataPtr);
258   test_error(err, "verify_data error");
259 
260   err= clEnqueueUnmapMemObject (this->m_queue, this->m_Image, dataPtr, 0, NULL, &event);
261   test_error(err, "clEnqueueUnmapMemObject error");
262 
263   err = clWaitForEvents(1, &event);
264   test_error(err, "clWaitForEvents error");
265 
266   err = clReleaseEvent(event);
267   test_error(err, "clReleaseEvent error");
268 
269   dataPtr = (T*) clEnqueueMapImage(this->m_queue, this->m_Image, this->m_blocking,
270                                    CL_MAP_WRITE,
271                                    this->buffer_origin,
272                                    this->region,
273                                    &(this-> buffer_row_pitch_bytes),
274                                    &(this->buffer_slice_pitch_bytes),
275                                    0, NULL, &event, &err);
276 
277   if (err == CL_SUCCESS) {
278     log_error("Calling clEnqueueMapImage (CL_MAP_WRITE) on a memory object created with the CL_MEM_HOST_READ_ONLY flag should not return CL_SUCCESS\n");
279     err = FAILURE;
280     return FAILURE;
281 
282   } else {
283     log_info("Test succeeded\n\n");
284     err = CL_SUCCESS;
285   }
286 
287   return err;
288 }
289 
290 #endif
291