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