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