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 *r_uint8_kernel_code =
28 "__kernel void test_r_uint8(read_only image2d_t srcimg, __global unsigned char *dst, sampler_t sampler)\n"
29 "{\n"
30 " int tid_x = get_global_id(0);\n"
31 " int tid_y = get_global_id(1);\n"
32 " int indx = tid_y * get_image_width(srcimg) + tid_x;\n"
33 " uint4 color;\n"
34 "\n"
35 " color = read_imageui(srcimg, sampler, (int2)(tid_x, tid_y));\n"
36 " dst[indx] = (unsigned char)(color.x);\n"
37 "\n"
38 "}\n";
39
40
41 static unsigned char *
generate_8bit_image(int w,int h,MTdata d)42 generate_8bit_image(int w, int h, MTdata d)
43 {
44 unsigned char *ptr = (unsigned char*)malloc(w * h * sizeof(unsigned char));
45 int i;
46
47 for (i=0; i<w*h; i++)
48 ptr[i] = (unsigned char)genrand_int32(d);
49
50 return ptr;
51 }
52
53 static int
verify_8bit_image(unsigned char * image,unsigned char * outptr,int w,int h)54 verify_8bit_image(unsigned char *image, unsigned char *outptr, int w, int h)
55 {
56 int i;
57
58 for (i=0; i<w*h; i++)
59 {
60 if (outptr[i] != image[i])
61 {
62 log_error("READ_IMAGE_R_UNSIGNED_INT8 test failed\n");
63 return -1;
64 }
65 }
66
67 log_info("READ_IMAGE_R_UNSIGNED_INT8 test passed\n");
68 return 0;
69 }
70
71 int
test_image_r8(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)72 test_image_r8(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
73 {
74 cl_mem streams[2];
75 cl_image_format img_format;
76 cl_uchar *input_ptr, *output_ptr;
77 cl_program program;
78 cl_kernel kernel;
79 size_t threads[3];
80 int img_width = 512;
81 int img_height = 512;
82 int err;
83 MTdata d;
84
85 PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
86
87 img_format.image_channel_order = CL_R;
88 img_format.image_channel_data_type = CL_UNSIGNED_INT8;
89
90 // early out if this image type is not supported
91 if (!is_image_format_supported(context, CL_MEM_READ_ONLY,
92 CL_MEM_OBJECT_IMAGE2D, &img_format))
93 {
94 log_info("WARNING: Image type not supported; skipping test.\n");
95 return 0;
96 }
97
98 d = init_genrand( gRandomSeed );
99 input_ptr = generate_8bit_image(img_width, img_height, d);
100 free_mtdata(d); d = NULL;
101
102 output_ptr = (cl_uchar*)malloc(sizeof(cl_uchar) * img_width * img_height);
103 streams[0] = create_image_2d(context, CL_MEM_READ_ONLY, &img_format,
104 img_width, img_height, 0, NULL, NULL);
105 if (!streams[0])
106 {
107 log_error("create_image_2d failed\n");
108 return -1;
109 }
110
111 streams[1] =
112 clCreateBuffer(context, CL_MEM_READ_WRITE,
113 sizeof(cl_uchar) * img_width * img_height, NULL, NULL);
114 if (!streams[1])
115 {
116 log_error("clCreateBuffer failed\n");
117 return -1;
118 }
119
120 size_t origin[3] = {0,0,0}, region[3]={img_width, img_height, 1};
121 err = clEnqueueWriteImage(queue, streams[0], CL_TRUE,
122 origin, region, 0, 0,
123 input_ptr,
124 0, NULL, NULL);
125 if (err != CL_SUCCESS)
126 {
127 log_error("clWriteImage failed: %d\n", err);
128 return -1;
129 }
130
131 err = create_single_kernel_helper(context, &program, &kernel, 1, &r_uint8_kernel_code, "test_r_uint8" );
132 if (err) {
133 log_error("Failed to create kernel and program: %d\n", err);
134 return -1;
135 }
136
137 cl_sampler sampler = clCreateSampler(context, CL_FALSE, CL_ADDRESS_CLAMP_TO_EDGE, CL_FILTER_NEAREST, &err);
138 test_error(err, "clCreateSampler failed");
139
140 err = clSetKernelArg(kernel, 0, sizeof streams[0], &streams[0]);
141 err |= clSetKernelArg(kernel, 1, sizeof streams[1], &streams[1]);
142 err |= clSetKernelArg(kernel, 2, sizeof sampler, &sampler);
143 if (err != CL_SUCCESS)
144 {
145 log_error("clSetKernelArgs failed: %d\n", err);
146 return -1;
147 }
148
149 threads[0] = (size_t)img_width;
150 threads[1] = (size_t)img_height;
151 err = clEnqueueNDRangeKernel( queue, kernel, 2, NULL, threads, NULL, 0, NULL, NULL );
152 if (err != CL_SUCCESS)
153 {
154 log_error("clEnqueueNDRangeKernel failed\n");
155 return -1;
156 }
157
158 err = clEnqueueReadBuffer( queue, streams[1], CL_TRUE, 0, sizeof(cl_uchar)*img_width*img_height, (void *)output_ptr, 0, NULL, NULL );
159 if (err != CL_SUCCESS)
160 {
161 log_error("clEnqueueReadBuffer failed\n");
162 return -1;
163 }
164
165 err = verify_8bit_image(input_ptr, output_ptr, img_width, img_height);
166
167
168 // cleanup
169 clReleaseMemObject(streams[0]);
170 clReleaseMemObject(streams[1]);
171 clReleaseKernel(kernel);
172 clReleaseProgram(program);
173 clReleaseSampler(sampler);
174 free(input_ptr);
175 free(output_ptr);
176
177 return err;
178 }
179
180
181
182
183
184