• 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 #include "harness/compat.h"
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 
24 
25 #include "procs.h"
26 
27 static const char *rgba8888_kernel_code =
28 "\n"
29 "__kernel void test_rgba8888(read_only image2d_t srcimg, write_only image2d_t dstimg, sampler_t sampler)\n"
30 "{\n"
31 "    int    tid_x = get_global_id(0);\n"
32 "    int    tid_y = get_global_id(1);\n"
33 "    float4 color;\n"
34 "\n"
35 "    if ( (tid_x >= get_image_width(dstimg)) || (tid_y >= get_image_height(dstimg)) )\n"
36 "        return;\n"
37 "    color = read_imagef(srcimg, sampler, (int2)(tid_x, tid_y));\n"
38 "    write_imagef(dstimg, (int2)(tid_x, tid_y), color);\n"
39 "\n"
40 "}\n";
41 
42 
43 static unsigned char *
generate_8888_image(int w,int h,MTdata d)44 generate_8888_image(int w, int h, MTdata d)
45 {
46     unsigned char   *ptr = (unsigned char*)malloc(w * h * 4);
47     int             i;
48 
49     for (i=0; i<w*h*4; i++)
50         ptr[i] = (unsigned char)genrand_int32(d);
51 
52     return ptr;
53 }
54 
55 static int
verify_rgba8888_image(unsigned char * src,unsigned char * dst,int w,int h)56 verify_rgba8888_image(unsigned char *src, unsigned char *dst, int w, int h)
57 {
58     int     i;
59 
60     for (i=0; i<w*h*4; i++)
61     {
62         if (dst[i] != src[i])
63         {
64             log_error("NPOT_IMAGE_RGBA_UNORM_INT8 test for width = %d, height = %d failed\n", w, h);
65             return -1;
66         }
67     }
68 
69     log_info("NPOT_IMAGE_RGBA_UNORM_INT8 test for width = %d, height = %d passed\n", w, h);
70     return 0;
71 }
72 
73 
74 int    img_width_selection[] = { 97, 111, 322, 479 };
75 int    img_height_selection[] = { 149, 222, 754, 385 };
76 
77 int
test_imagenpot(cl_device_id device_id,cl_context context,cl_command_queue queue,int num_elements)78 test_imagenpot(cl_device_id device_id, cl_context context, cl_command_queue queue, int num_elements)
79 {
80     cl_mem            streams[2];
81     cl_image_format    img_format;
82     unsigned char    *input_ptr, *output_ptr;
83     cl_program        program;
84     cl_kernel        kernel;
85     size_t    global_threads[3], local_threads[3];
86     size_t            local_workgroup_size;
87     int                img_width;
88     int                img_height;
89     int                err;
90     cl_uint            m;
91     size_t max_local_workgroup_size[3];
92     MTdata          d;
93 
94     PASSIVE_REQUIRE_IMAGE_SUPPORT( device_id )
95 
96     cl_device_type device_type;
97     err = clGetDeviceInfo(device_id, CL_DEVICE_TYPE, sizeof(device_type), &device_type, NULL);
98     if (err) {
99         log_error("Failed to get device type: %d\n",err);
100         return -1;
101     }
102 
103     d = init_genrand( gRandomSeed );
104     for (m=0; m<sizeof(img_width_selection)/sizeof(int); m++)
105     {
106         img_width = img_width_selection[m];
107         img_height = img_height_selection[m];
108         input_ptr = generate_8888_image(img_width, img_height, d);
109         output_ptr = (unsigned char*)malloc(sizeof(unsigned char) * 4 * img_width * img_height);
110 
111         img_format.image_channel_order = CL_RGBA;
112         img_format.image_channel_data_type = CL_UNORM_INT8;
113         streams[0] = create_image_2d(context, (cl_mem_flags)(CL_MEM_READ_WRITE),  &img_format,
114                                  img_width, img_height, 0, NULL, NULL);
115         if (!streams[0])
116         {
117             log_error("create_image_2d failed\n");
118             free_mtdata(d);
119             return -1;
120         }
121         img_format.image_channel_order = CL_RGBA;
122         img_format.image_channel_data_type = CL_UNORM_INT8;
123     streams[1] = create_image_2d(context, (cl_mem_flags)(CL_MEM_READ_WRITE),  &img_format,
124                                  img_width, img_height, 0, NULL, NULL);
125         if (!streams[1])
126         {
127             log_error("create_image_2d failed\n");
128             free_mtdata(d);
129             return -1;
130         }
131 
132         size_t origin[3] = {0,0,0}, region[3] = {img_width, img_height, 1};
133         err = clEnqueueWriteImage(queue, streams[0], CL_TRUE,
134                               origin, region, 0, 0,
135                               input_ptr,
136                               0, NULL, NULL);
137         if (err != CL_SUCCESS)
138         {
139             log_error("clWriteImage failed\n");
140             free_mtdata(d);
141             return -1;
142         }
143 
144 
145         err = create_single_kernel_helper(context, &program, &kernel, 1, &rgba8888_kernel_code, "test_rgba8888" );
146         if (err)
147         {
148             log_error("Failed to create kernel and program: %d\n", err);
149             free_mtdata(d);
150             return -1;
151         }
152 
153         cl_sampler sampler = clCreateSampler(context, CL_FALSE, CL_ADDRESS_CLAMP_TO_EDGE, CL_FILTER_NEAREST, &err);
154         test_error(err, "clCreateSampler failed");
155 
156         err = clSetKernelArg(kernel, 0, sizeof streams[0], &streams[0]);
157         err |= clSetKernelArg(kernel, 1, sizeof streams[1], &streams[1]);
158         err |= clSetKernelArg(kernel, 2, sizeof sampler, &sampler);
159         if (err != CL_SUCCESS)
160         {
161             log_error("clSetKernelArgs failed\n");
162             free_mtdata(d);
163             return -1;
164         }
165 
166         err = clGetKernelWorkGroupInfo(kernel, device_id, CL_KERNEL_WORK_GROUP_SIZE, sizeof(local_workgroup_size), &local_workgroup_size, NULL);
167         test_error(err, "clGetKernelWorkGroupInfo for CL_KERNEL_WORK_GROUP_SIZE failed");
168 
169         err = clGetDeviceInfo(device_id, CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(max_local_workgroup_size), max_local_workgroup_size, NULL);
170         test_error(err, "clGetDeviceInfo failed for CL_DEVICE_MAX_WORK_ITEM_SIZES");
171 
172         // Pick the minimum of the device and the kernel
173         if (local_workgroup_size > max_local_workgroup_size[0])
174             local_workgroup_size = max_local_workgroup_size[0];
175 
176         global_threads[0] = ((img_width + local_workgroup_size - 1) / local_workgroup_size) * local_workgroup_size;
177         global_threads[1] = img_height;
178         local_threads[0] = local_workgroup_size;
179         local_threads[1] = 1;
180         err = clEnqueueNDRangeKernel( queue, kernel, 2, NULL, global_threads, local_threads, 0, NULL, NULL );
181 
182         if (err != CL_SUCCESS)
183         {
184             log_error("%s clEnqueueNDRangeKernel failed\n", __FUNCTION__);
185             free_mtdata(d);
186             return -1;
187         }
188         err = clEnqueueReadImage(queue, streams[1], CL_TRUE,
189                              origin, region, 0, 0,
190                              (void *)output_ptr,
191                              0, NULL, NULL);
192         if (err != CL_SUCCESS)
193         {
194             log_error("clEnqueueReadBuffer failed\n");
195             return -1;
196         }
197 
198         err = verify_rgba8888_image(input_ptr, output_ptr, img_width, img_height);
199 
200         // cleanup
201         clReleaseSampler(sampler);
202         clReleaseMemObject(streams[0]);
203         clReleaseMemObject(streams[1]);
204         clReleaseKernel(kernel);
205         clReleaseProgram(program);
206         free(input_ptr);
207         free(output_ptr);
208 
209         if (err)
210             break;
211     }
212 
213     free_mtdata(d);
214 
215     return err;
216 }
217 
218 
219 
220 
221 
222