• 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 *rgba16_write_kernel_code =
28 "__kernel void test_rgba16_write(__global unsigned short *src, write_only image2d_t dstimg)\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(dstimg) + tid_x;\n"
33 "    float4         color;\n"
34 "\n"
35 "    indx *= 4;\n"
36 "    color = (float4)((float)src[indx+0], (float)src[indx+1], (float)src[indx+2], (float)src[indx+3]);\n"
37 "    color /= 65535.0f;\n"
38 "    write_imagef(dstimg, (int2)(tid_x, tid_y), color);\n"
39 "\n"
40 "}\n";
41 
42 
43 static unsigned short *
generate_16bit_image(int w,int h,MTdata d)44 generate_16bit_image(int w, int h, MTdata d)
45 {
46     cl_ushort  *ptr = (cl_ushort*)malloc(w * h * 4 * sizeof(cl_ushort));
47     int             i;
48 
49     for (i=0; i<w*h*4; i++)
50         ptr[i] = (cl_ushort)genrand_int32(d);
51 
52     return ptr;
53 }
54 
55 // normalized 16bit ints ... get dived by 64k then muled by 64k...
56 // give the poor things some tolerance
57 #define MAX_ERR 1
58 
59 static int
verify_16bit_image(const char * string,cl_ushort * image,cl_ushort * outptr,int w,int h)60 verify_16bit_image(const char *string, cl_ushort *image, cl_ushort *outptr, int w, int h)
61 {
62     int     i;
63 
64     for (i=0; i<w*h*4; i++)
65     {
66         if (abs(outptr[i] - image[i]) > MAX_ERR)
67         {
68             log_error("%s failed\n", string);
69             return -1;
70         }
71     }
72 
73     log_info("%s passed\n", string);
74     return 0;
75 }
76 
test_writeimage_int16(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)77 int test_writeimage_int16(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
78 {
79     cl_mem streams[3];
80     cl_program program;
81     cl_kernel kernel[2];
82     cl_image_format    img_format;
83     cl_ushort *input_ptr, *output_ptr;
84     size_t threads[2];
85     int img_width = 512;
86     int img_height = 512;
87     int i, err, any_err = 0;
88     size_t origin[3] = {0, 0, 0};
89     size_t region[3] = {img_width, img_height, 1};
90     size_t length = img_width * img_height * 4 * sizeof(cl_ushort);
91 
92     PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
93 
94     MTdata d = init_genrand( gRandomSeed );
95     input_ptr = generate_16bit_image(img_width, img_height, d);
96     free_mtdata(d); d = NULL;
97 
98     output_ptr = (cl_ushort*)malloc(length);
99 
100     img_format.image_channel_order = CL_RGBA;
101     img_format.image_channel_data_type = CL_UNORM_INT16;
102     streams[0] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format, img_width, img_height, 0, NULL, NULL);
103     if (!streams[0])
104     {
105         log_error("create_image_2d failed\n");
106         return -1;
107     }
108 
109     img_format.image_channel_order = CL_RGBA;
110     img_format.image_channel_data_type = CL_UNORM_INT16;
111     streams[1] = create_image_2d(context, CL_MEM_WRITE_ONLY, &img_format, img_width, img_height, 0, NULL, NULL);
112     if (!streams[1])
113     {
114         log_error("create_image_2d failed\n");
115         return -1;
116     }
117   streams[2] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, NULL);
118     if (!streams[2])
119     {
120         log_error("clCreateArray failed\n");
121         return -1;
122     }
123 
124   err = clEnqueueWriteBuffer(queue, streams[2], CL_TRUE, 0, length, input_ptr, 0, NULL, NULL);
125     if (err != CL_SUCCESS)
126     {
127         log_error("clEnqueueWriteBuffer failed\n");
128         return -1;
129     }
130 
131   err = create_single_kernel_helper(context, &program, &kernel[0], 1, &rgba16_write_kernel_code, "test_rgba16_write" );
132   if (err)
133     return -1;
134     kernel[1] = clCreateKernel(program, "test_rgba16_write", NULL);
135     if (!kernel[1])
136     {
137         log_error("clCreateKernel failed\n");
138         return -1;
139     }
140 
141   err  = clSetKernelArg(kernel[0], 0, sizeof streams[2], &streams[2]);
142   err |= clSetKernelArg(kernel[0], 1, sizeof streams[0], &streams[0]);
143     if (err != CL_SUCCESS)
144     {
145         log_error("clSetKernelArgs failed\n");
146         return -1;
147     }
148 
149   err  = clSetKernelArg(kernel[1], 0, sizeof streams[2], &streams[2]);
150   err |= clSetKernelArg(kernel[1], 1, sizeof streams[1], &streams[1]);
151     if (err != CL_SUCCESS)
152     {
153         log_error("clSetKernelArgs failed\n");
154         return -1;
155     }
156 
157   threads[0] = (unsigned int)img_width;
158   threads[1] = (unsigned int)img_height;
159 
160     for (i=0; i<2; i++)
161     {
162     err = clEnqueueNDRangeKernel(queue, kernel[i], 2, NULL, threads, NULL, 0, NULL, NULL);
163         if (err != CL_SUCCESS)
164         {
165             log_error("clExecuteKernel failed\n");
166             return -1;
167         }
168 
169     err = clEnqueueReadImage(queue, streams[i], CL_TRUE, origin, region, 0, 0, output_ptr, 0, NULL, NULL);
170         if (err != CL_SUCCESS)
171         {
172             log_error("clReadImage failed\n");
173             return -1;
174         }
175 
176         err = verify_16bit_image((i == 0) ? "WRITE_IMAGE_RGBA_UNORM_INT16 test with memflags = CL_MEM_READ_WRITE" :
177                              "WRITE_IMAGE_RGBA_UNORM_INT16 test with memflags = CL_MEM_WRITE_ONLY",
178                              input_ptr, output_ptr, img_width, img_height);
179         any_err |= err;
180     }
181 
182     // cleanup
183     clReleaseMemObject(streams[0]);
184     clReleaseMemObject(streams[1]);
185     clReleaseMemObject(streams[2]);
186     clReleaseKernel(kernel[0]);
187     clReleaseKernel(kernel[1]);
188     clReleaseProgram(program);
189     free(input_ptr);
190     free(output_ptr);
191 
192     return any_err;
193 }
194 
195 
196