|
1 | 1 | // |
2 | 2 | // Copyright (c) 2017 The Khronos Group Inc. |
3 | | -// |
| 3 | +// |
4 | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | // you may not use this file except in compliance with the License. |
6 | 6 | // You may obtain a copy of the License at |
|
20 | 20 | #include <string.h> |
21 | 21 | #include <sys/types.h> |
22 | 22 | #include <sys/stat.h> |
| 23 | +#include <memory> |
23 | 24 |
|
24 | 25 | #include "testBase.h" |
25 | 26 |
|
26 | | -static unsigned char * |
27 | | -generate_rgba8_image(int w, int h, MTdata d) |
| 27 | +static std::unique_ptr<unsigned char[]> generate_rgba8_image(int w, int h, |
| 28 | + MTdata d) |
28 | 29 | { |
29 | | - unsigned char *ptr = (unsigned char*)malloc(w * h * 4); |
30 | | - int i; |
| 30 | + std::unique_ptr<unsigned char[]> ptr{ new unsigned char[w * h * 4] }; |
31 | 31 |
|
32 | | - for (i=0; i<w*h*4; i++) |
| 32 | + for (int i = 0; i < w * h * 4; i++) |
33 | 33 | ptr[i] = (unsigned char)genrand_int32(d); |
34 | 34 |
|
35 | 35 | return ptr; |
36 | 36 | } |
37 | 37 |
|
38 | | -static int |
39 | | -verify_rgba8_image(unsigned char *image, unsigned char *outptr, int w, int h) |
| 38 | +static int verify_rgba8_image(const unsigned char *image, |
| 39 | + const unsigned char *outptr, int w, int h) |
40 | 40 | { |
41 | | - int i; |
| 41 | + int i; |
42 | 42 |
|
43 | | - for (i=0; i<w*h*4; i++) |
| 43 | + for (i = 0; i < w * h * 4; i++) |
44 | 44 | { |
45 | | - if (outptr[i] != image[i]) |
46 | | - return -1; |
| 45 | + if (outptr[i] != image[i]) return -1; |
47 | 46 | } |
48 | 47 |
|
49 | 48 | return 0; |
50 | 49 | } |
51 | 50 |
|
52 | 51 |
|
53 | | -static unsigned short * |
54 | | -generate_rgba16_image(int w, int h, MTdata d) |
| 52 | +static std::unique_ptr<unsigned short[]> generate_rgba16_image(int w, int h, |
| 53 | + MTdata d) |
55 | 54 | { |
56 | | - unsigned short *ptr = (unsigned short *)malloc(w * h * 4 * sizeof(unsigned short)); |
57 | | - int i; |
| 55 | + std::unique_ptr<unsigned short[]> ptr{ new unsigned short[w * h * 4] }; |
58 | 56 |
|
59 | | - for (i=0; i<w*h*4; i++) |
| 57 | + for (int i = 0; i < w * h * 4; i++) |
60 | 58 | ptr[i] = (unsigned short)genrand_int32(d); |
61 | 59 |
|
62 | 60 | return ptr; |
63 | 61 | } |
64 | 62 |
|
65 | | -static int |
66 | | -verify_rgba16_image(unsigned short *image, unsigned short *outptr, int w, int h) |
| 63 | +static int verify_rgba16_image(const unsigned short *image, |
| 64 | + const unsigned short *outptr, int w, int h) |
67 | 65 | { |
68 | | - int i; |
| 66 | + int i; |
69 | 67 |
|
70 | | - for (i=0; i<w*h*4; i++) |
| 68 | + for (i = 0; i < w * h * 4; i++) |
71 | 69 | { |
72 | | - if (outptr[i] != image[i]) |
73 | | - return -1; |
| 70 | + if (outptr[i] != image[i]) return -1; |
74 | 71 | } |
75 | 72 |
|
76 | 73 | return 0; |
77 | 74 | } |
78 | 75 |
|
79 | 76 |
|
80 | | -static float * |
81 | | -generate_rgbafp_image(int w, int h, MTdata d) |
| 77 | +static std::unique_ptr<float[]> generate_rgbafp_image(int w, int h, MTdata d) |
82 | 78 | { |
83 | | - float *ptr = (float*)malloc(w * h * 4 * sizeof(float)); |
84 | | - int i; |
| 79 | + std::unique_ptr<float[]> ptr{ new float[w * h * 4] }; |
85 | 80 |
|
86 | | - for (i=0; i<w*h*4; i++) |
| 81 | + for (int i = 0; i < w * h * 4; i++) |
87 | 82 | ptr[i] = get_random_float(-0x40000000, 0x40000000, d); |
88 | 83 |
|
89 | 84 | return ptr; |
90 | 85 | } |
91 | 86 |
|
92 | | -static int |
93 | | -verify_rgbafp_image(float *image, float *outptr, int w, int h) |
| 87 | +static int verify_rgbafp_image(const float *image, const float *outptr, int w, |
| 88 | + int h) |
94 | 89 | { |
95 | | - int i; |
| 90 | + int i; |
96 | 91 |
|
97 | | - for (i=0; i<w*h*4; i++) |
| 92 | + for (i = 0; i < w * h * 4; i++) |
98 | 93 | { |
99 | | - if (outptr[i] != image[i]) |
100 | | - return -1; |
| 94 | + if (outptr[i] != image[i]) return -1; |
101 | 95 | } |
102 | 96 |
|
103 | 97 | return 0; |
104 | 98 | } |
105 | 99 |
|
| 100 | +static constexpr cl_image_format image_formats[] = { { CL_RGBA, CL_UNORM_INT8 }, |
| 101 | + { CL_RGBA, |
| 102 | + CL_UNORM_INT16 }, |
| 103 | + { CL_RGBA, CL_FLOAT } }; |
106 | 104 |
|
107 | | -REGISTER_TEST(imagecopy) |
| 105 | +static int test_imagecopy_impl(cl_device_id device, cl_context context, |
| 106 | + cl_command_queue queue, int num_elements, |
| 107 | + cl_mem_flags src_image_flags) |
108 | 108 | { |
109 | | - cl_image_format img_format; |
110 | | - unsigned char *rgba8_inptr, *rgba8_outptr; |
111 | | - unsigned short *rgba16_inptr, *rgba16_outptr; |
112 | | - float *rgbafp_inptr, *rgbafp_outptr; |
113 | | - clMemWrapper streams[6]; |
114 | | - int img_width = 512; |
115 | | - int img_height = 512; |
116 | | - int i, err; |
117 | | - MTdata d; |
118 | | - |
119 | | - PASSIVE_REQUIRE_IMAGE_SUPPORT( device ) |
120 | | - |
121 | | - d = init_genrand( gRandomSeed ); |
122 | | - rgba8_inptr = (unsigned char *)generate_rgba8_image(img_width, img_height, d); |
123 | | - rgba16_inptr = (unsigned short *)generate_rgba16_image(img_width, img_height, d); |
124 | | - rgbafp_inptr = (float *)generate_rgbafp_image(img_width, img_height, d); |
125 | | - free_mtdata(d); d = NULL; |
126 | | - |
127 | | - rgba8_outptr = (unsigned char*)malloc(sizeof(unsigned char) * 4 * img_width * img_height); |
128 | | - rgba16_outptr = (unsigned short*)malloc(sizeof(unsigned short) * 4 * img_width * img_height); |
129 | | - rgbafp_outptr = (float*)malloc(sizeof(float) * 4 * img_width * img_height); |
130 | | - |
131 | | - img_format.image_channel_order = CL_RGBA; |
132 | | - img_format.image_channel_data_type = CL_UNORM_INT8; |
133 | | - streams[0] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format, |
134 | | - img_width, img_height, 0, NULL, &err); |
135 | | - test_error(err, "create_image_2d failed"); |
136 | | - streams[1] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format, |
137 | | - img_width, img_height, 0, NULL, &err); |
138 | | - test_error(err, "create_image_2d failed"); |
139 | | - |
140 | | - img_format.image_channel_order = CL_RGBA; |
141 | | - img_format.image_channel_data_type = CL_UNORM_INT16; |
142 | | - streams[2] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format, |
143 | | - img_width, img_height, 0, NULL, &err); |
144 | | - test_error(err, "create_image_2d failed"); |
145 | | - streams[3] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format, |
146 | | - img_width, img_height, 0, NULL, &err); |
147 | | - test_error(err, "create_image_2d failed"); |
148 | | - |
149 | | - img_format.image_channel_order = CL_RGBA; |
150 | | - img_format.image_channel_data_type = CL_FLOAT; |
151 | | - streams[4] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format, |
152 | | - img_width, img_height, 0, NULL, &err); |
153 | | - test_error(err, "create_image_2d failed"); |
154 | | - streams[5] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format, |
155 | | - img_width, img_height, 0, NULL, &err); |
156 | | - test_error(err, "create_image_2d failed"); |
157 | | - |
158 | | - for (i=0; i<3; i++) |
| 109 | + constexpr size_t image_formats_count = ARRAY_SIZE(image_formats); |
| 110 | + std::unique_ptr<unsigned char[]> rgba8_inptr, rgba8_outptr; |
| 111 | + std::unique_ptr<unsigned short[]> rgba16_inptr, rgba16_outptr; |
| 112 | + std::unique_ptr<float[]> rgbafp_inptr, rgbafp_outptr; |
| 113 | + clMemWrapper streams[6]; |
| 114 | + int img_width = 512; |
| 115 | + int img_height = 512; |
| 116 | + int i, err; |
| 117 | + MTdataHolder d(gRandomSeed); |
| 118 | + |
| 119 | + rgba8_inptr = generate_rgba8_image(img_width, img_height, d); |
| 120 | + rgba16_inptr = generate_rgba16_image(img_width, img_height, d); |
| 121 | + rgbafp_inptr = generate_rgbafp_image(img_width, img_height, d); |
| 122 | + |
| 123 | + rgba8_outptr.reset(new unsigned char[4 * img_width * img_height]); |
| 124 | + rgba16_outptr.reset(new unsigned short[4 * img_width * img_height]); |
| 125 | + rgbafp_outptr.reset(new float[4 * img_width * img_height]); |
| 126 | + |
| 127 | + for (size_t index = 0; index < image_formats_count; ++index) |
159 | 128 | { |
160 | | - void *p, *outp; |
161 | | - int x, y, delta_w = img_width/8, delta_h = img_height/16; |
| 129 | + void *ptr = nullptr; |
| 130 | + if (src_image_flags & CL_MEM_USE_HOST_PTR |
| 131 | + || src_image_flags & CL_MEM_COPY_HOST_PTR) |
| 132 | + |
| 133 | + { |
| 134 | + switch (index) |
| 135 | + { |
| 136 | + case 0: ptr = rgba8_inptr.get(); break; |
| 137 | + case 1: ptr = rgba16_inptr.get(); break; |
| 138 | + case 2: ptr = rgbafp_inptr.get(); break; |
| 139 | + default: break; |
| 140 | + } |
| 141 | + } |
| 142 | + streams[index * 2] = |
| 143 | + create_image_2d(context, src_image_flags, &image_formats[index], |
| 144 | + img_width, img_height, 0, ptr, &err); |
| 145 | + test_error(err, "create_image_2d failed"); |
| 146 | + |
| 147 | + streams[index * 2 + 1] = |
| 148 | + create_image_2d(context, CL_MEM_READ_WRITE, &image_formats[index], |
| 149 | + img_width, img_height, 0, nullptr, &err); |
| 150 | + test_error(err, "create_image_2d failed"); |
| 151 | + } |
| 152 | + |
| 153 | + for (i = 0; i < 3; i++) |
| 154 | + { |
| 155 | + void *p, *outp; |
| 156 | + int x, y, delta_w = img_width / 8, delta_h = img_height / 16; |
162 | 157 |
|
163 | 158 | switch (i) |
164 | 159 | { |
165 | 160 | case 0: |
166 | | - p = (void *)rgba8_inptr; |
167 | | - outp = (void *)rgba8_outptr; |
168 | | - log_info("Testing CL_RGBA CL_UNORM_INT8\n"); |
| 161 | + p = rgba8_inptr.get(); |
| 162 | + outp = rgba8_outptr.get(); |
| 163 | + log_info("Testing CL_RGBA CL_UNORM_INT8\n"); |
169 | 164 | break; |
170 | 165 | case 1: |
171 | | - p = (void *)rgba16_inptr; |
172 | | - outp = (void *)rgba16_outptr; |
173 | | - log_info("Testing CL_RGBA CL_UNORM_INT16\n"); |
| 166 | + p = rgba16_inptr.get(); |
| 167 | + outp = rgba16_outptr.get(); |
| 168 | + log_info("Testing CL_RGBA CL_UNORM_INT16\n"); |
174 | 169 | break; |
175 | 170 | case 2: |
176 | | - p = (void *)rgbafp_inptr; |
177 | | - outp = (void *)rgbafp_outptr; |
178 | | - log_info("Testing CL_RGBA CL_FLOAT\n"); |
| 171 | + p = rgbafp_inptr.get(); |
| 172 | + outp = rgbafp_outptr.get(); |
| 173 | + log_info("Testing CL_RGBA CL_FLOAT\n"); |
179 | 174 | break; |
180 | 175 | } |
181 | 176 |
|
182 | | - size_t origin[3] = {0,0,0}, region[3] = {img_width, img_height, 1}; |
183 | | - err = clEnqueueWriteImage(queue, streams[i*2], CL_TRUE, origin, region, 0, 0, p, 0, NULL, NULL); |
184 | | - test_error(err, "create_image_2d failed"); |
| 177 | + size_t origin[3] = { 0, 0, 0 }, |
| 178 | + region[3] = { img_width, img_height, 1 }; |
| 179 | + if (!(src_image_flags & CL_MEM_USE_HOST_PTR |
| 180 | + || src_image_flags & CL_MEM_COPY_HOST_PTR)) |
| 181 | + { |
| 182 | + err = clEnqueueWriteImage(queue, streams[i * 2], CL_TRUE, origin, |
| 183 | + region, 0, 0, p, 0, nullptr, nullptr); |
| 184 | + test_error(err, "create_image_2d failed"); |
| 185 | + } |
185 | 186 |
|
186 | 187 | int copy_number = 0; |
187 | | - for (y=0; y<img_height; y+=delta_h) |
| 188 | + for (y = 0; y < img_height; y += delta_h) |
188 | 189 | { |
189 | | - for (x=0; x<img_width; x+=delta_w) |
| 190 | + for (x = 0; x < img_width; x += delta_w) |
190 | 191 | { |
191 | | - copy_number++; |
192 | | - size_t copy_origin[3] = {x,y,0}, copy_region[3]={delta_w, delta_h, 1}; |
193 | | - err = clEnqueueCopyImage(queue, streams[i*2], streams[i*2+1], |
194 | | - copy_origin, copy_origin, copy_region, |
195 | | - 0, NULL, NULL); |
196 | | - if (err) { |
197 | | - log_error("Copy %d (origin [%d, %d], size [%d, %d], image size [%d x %d]) Failed\n", copy_number, x, y, delta_w, delta_h, img_width, img_height); |
198 | | - } |
199 | | - test_error(err, "clEnqueueCopyImage failed"); |
| 192 | + copy_number++; |
| 193 | + size_t copy_origin[3] = { x, y, 0 }, |
| 194 | + copy_region[3] = { delta_w, delta_h, 1 }; |
| 195 | + err = clEnqueueCopyImage( |
| 196 | + queue, streams[i * 2], streams[i * 2 + 1], copy_origin, |
| 197 | + copy_origin, copy_region, 0, NULL, NULL); |
| 198 | + if (err) |
| 199 | + { |
| 200 | + log_error("Copy %d (origin [%d, %d], size [%d, %d], image " |
| 201 | + "size [%d x %d]) Failed\n", |
| 202 | + copy_number, x, y, delta_w, delta_h, img_width, |
| 203 | + img_height); |
| 204 | + } |
| 205 | + test_error(err, "clEnqueueCopyImage failed"); |
200 | 206 | } |
201 | 207 | } |
202 | 208 |
|
203 | | - err = clEnqueueReadImage(queue, streams[i*2+1], CL_TRUE, origin, region, 0, 0, outp, 0, NULL, NULL); |
| 209 | + err = clEnqueueReadImage(queue, streams[i * 2 + 1], CL_TRUE, origin, |
| 210 | + region, 0, 0, outp, 0, NULL, NULL); |
204 | 211 | test_error(err, "clEnqueueReadImage failed"); |
205 | 212 |
|
206 | 213 | switch (i) |
207 | 214 | { |
208 | 215 | case 0: |
209 | | - err = verify_rgba8_image(rgba8_inptr, rgba8_outptr, img_width, img_height); |
| 216 | + err = verify_rgba8_image(rgba8_inptr.get(), rgba8_outptr.get(), |
| 217 | + img_width, img_height); |
210 | 218 | break; |
211 | 219 | case 1: |
212 | | - err = verify_rgba16_image(rgba16_inptr, rgba16_outptr, img_width, img_height); |
| 220 | + err = |
| 221 | + verify_rgba16_image(rgba16_inptr.get(), rgba16_outptr.get(), |
| 222 | + img_width, img_height); |
213 | 223 | break; |
214 | 224 | case 2: |
215 | | - err = verify_rgbafp_image(rgbafp_inptr, rgbafp_outptr, img_width, img_height); |
| 225 | + err = |
| 226 | + verify_rgbafp_image(rgbafp_inptr.get(), rgbafp_outptr.get(), |
| 227 | + img_width, img_height); |
216 | 228 | break; |
217 | 229 | } |
218 | 230 |
|
219 | | - if (err) |
220 | | - break; |
| 231 | + if (err) break; |
221 | 232 | } |
222 | 233 |
|
223 | | - free(rgba8_inptr); |
224 | | - free(rgba16_inptr); |
225 | | - free(rgbafp_inptr); |
226 | | - free(rgba8_outptr); |
227 | | - free(rgba16_outptr); |
228 | | - free(rgbafp_outptr); |
229 | | - |
230 | 234 | if (err) |
231 | 235 | log_error("IMAGE copy test failed\n"); |
232 | 236 | else |
233 | 237 | log_info("IMAGE copy test passed\n"); |
234 | 238 |
|
235 | 239 | return err; |
236 | 240 | } |
| 241 | + |
| 242 | +REGISTER_TEST(imagecopy) |
| 243 | +{ |
| 244 | + PASSIVE_REQUIRE_IMAGE_SUPPORT(device); |
| 245 | + |
| 246 | + return test_imagecopy_impl(device, context, queue, num_elements, |
| 247 | + CL_MEM_READ_WRITE); |
| 248 | +} |
0 commit comments