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_kernel_code =
28 "__kernel void test_rgba16(read_only image3d_t srcimg, __global ushort4 *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 tid_z = get_global_id(2);\n"
33 " int indx = (tid_z * get_image_height(srcimg) + tid_y) * get_image_width(srcimg) + tid_x;\n"
34 " float4 color;\n"
35 "\n"
36 " color = read_imagef(srcimg, sampler, (int4)(tid_x, tid_y, tid_z, 0));\n"
37 " ushort4 dst_write;\n"
38 " dst_write.x = convert_ushort_rte(color.x * 65535.0f);\n"
39 " dst_write.y = convert_ushort_rte(color.y * 65535.0f);\n"
40 " dst_write.z = convert_ushort_rte(color.z * 65535.0f);\n"
41 " dst_write.w = convert_ushort_rte(color.w * 65535.0f);\n"
42 " dst[indx] = dst_write;\n"
43 "\n"
44 "}\n";
45
46
47 static unsigned short *
generate_16bit_image(int w,int h,int d,MTdata data)48 generate_16bit_image(int w, int h, int d, MTdata data)
49 {
50 unsigned short *ptr = (cl_ushort*)malloc(w * h * d * 4 * sizeof(cl_ushort));
51 int i;
52
53 for (i=0; i<w*h*d*4; i++)
54 ptr[i] = (cl_ushort)genrand_int32(data);
55
56 return ptr;
57 }
58
59 static int
verify_16bit_image(cl_ushort * image,cl_ushort * outptr,int w,int h,int d)60 verify_16bit_image(cl_ushort *image, cl_ushort *outptr, int w, int h, int d)
61 {
62 int i;
63
64 for (i=0; i<w*h*d*4; i++)
65 {
66 if (outptr[i] != image[i])
67 {
68 log_error("READ_IMAGE3D_RGBA_UNORM_INT16 test failed\n");
69 return -1;
70 }
71 }
72
73 log_info("READ_IMAGE3D_RGBA_UNORM_INT16 test passed\n");
74 return 0;
75 }
76
test_readimage3d_int16(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)77 int test_readimage3d_int16(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
78 {
79 cl_mem streams[2];
80 cl_program program;
81 cl_kernel kernel;
82 cl_image_format img_format;
83 cl_ushort *input_ptr, *output_ptr;
84 size_t threads[3];
85 int img_width = 64;
86 int img_height = 64;
87 int img_depth = 64;
88 int err;
89 size_t origin[3] = {0, 0, 0};
90 size_t region[3] = {img_width, img_height, img_depth};
91 size_t length = img_width * img_height * img_depth * 4 * sizeof(cl_ushort);
92
93 PASSIVE_REQUIRE_3D_IMAGE_SUPPORT( device )
94
95 MTdata d = init_genrand( gRandomSeed );
96 input_ptr = generate_16bit_image(img_width, img_height, img_depth, d);
97 free_mtdata(d); d = NULL;
98
99 output_ptr = (cl_ushort*)malloc(length);
100
101 img_format.image_channel_order = CL_RGBA;
102 img_format.image_channel_data_type = CL_UNORM_INT16;
103 streams[0] = create_image_3d(context, CL_MEM_READ_ONLY, &img_format, img_width, img_height, img_depth, 0, 0, NULL, &err);
104 test_error(err, "create_image_3d failed");
105
106 streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, &err);
107 test_error(err, "clCreateBuffer failed");
108
109 err = clEnqueueWriteImage(queue, streams[0], CL_TRUE, origin, region, 0, 0, input_ptr, 0, NULL, NULL);
110 test_error(err, "clEnqueueWriteImage failed");
111
112 err = create_single_kernel_helper(context, &program, &kernel, 1, &rgba16_kernel_code, "test_rgba16" );
113 if (err)
114 return -1;
115
116 cl_sampler sampler = clCreateSampler(context, CL_FALSE, CL_ADDRESS_CLAMP_TO_EDGE, CL_FILTER_NEAREST, &err);
117 test_error(err, "clCreateSampler failed");
118
119 err = clSetKernelArg(kernel, 0, sizeof streams[0], &streams[0]);
120 err |= clSetKernelArg(kernel, 1, sizeof streams[1], &streams[1]);
121 err |= clSetKernelArg(kernel, 2, sizeof sampler, &sampler);
122 test_error(err, "clSetKernelArg failed");
123
124 threads[0] = (unsigned int)img_width;
125 threads[1] = (unsigned int)img_height;
126 threads[2] = (unsigned int)img_depth;
127 err = clEnqueueNDRangeKernel(queue, kernel, 3, NULL, threads, NULL, 0, NULL, NULL);
128 test_error(err, "clEnqueueNDRangeKernel failed");
129
130 err = clEnqueueReadBuffer(queue, streams[1], CL_TRUE, 0, length, output_ptr, 0, NULL, NULL);
131 test_error(err, "clEnqueueReadBuffer failed");
132
133 err = verify_16bit_image(input_ptr, output_ptr, img_width, img_height, img_depth);
134
135 // cleanup
136 clReleaseSampler(sampler);
137 clReleaseMemObject(streams[0]);
138 clReleaseMemObject(streams[1]);
139 clReleaseKernel(kernel);
140 clReleaseProgram(program);
141 free(input_ptr);
142 free(output_ptr);
143
144 return err;
145 }
146
147
148