-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathtest_harness_image.cpp
More file actions
422 lines (378 loc) · 15.8 KB
/
test_harness_image.cpp
File metadata and controls
422 lines (378 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
*
* Copyright (C) 2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "test_harness/test_harness.hpp"
#include "gtest/gtest.h"
#include "logging/logging.hpp"
#include "utils/utils.hpp"
#include <level_zero/ze_api.h>
namespace lzt = level_zero_tests;
namespace level_zero_tests {
bool image_support() {
ze_device_image_properties_t properties{};
properties.stype = ZE_STRUCTURE_TYPE_IMAGE_PROPERTIES;
properties.pNext = nullptr;
ze_result_t result = zeDeviceGetImageProperties(
lzt::zeDevice::get_instance()->get_device(), &properties);
if ((result != ZE_RESULT_SUCCESS) ||
((properties.maxImageDims1D == 0) && (properties.maxImageDims2D == 0) &&
(properties.maxImageDims3D == 0) &&
(properties.maxImageBufferSize == 0) &&
(properties.maxImageArraySlices == 0) && (properties.maxSamplers == 0) &&
(properties.maxReadImageArgs == 0) &&
(properties.maxWriteImageArgs == 0))) {
return false;
} else {
return true;
}
}
void copy_image_from_mem(lzt::ImagePNG32Bit input, ze_image_handle_t output) {
auto command_list = lzt::create_command_list();
EXPECT_EQ(ZE_RESULT_SUCCESS, zeCommandListAppendImageCopyFromMemory(
command_list, output, input.raw_data(),
nullptr, nullptr, 0, nullptr));
lzt::append_barrier(command_list, nullptr, 0, nullptr);
lzt::close_command_list(command_list);
auto command_queue = lzt::create_command_queue();
lzt::execute_command_lists(command_queue, 1, &command_list, nullptr);
lzt::synchronize(command_queue, UINT64_MAX);
lzt::destroy_command_queue(command_queue);
lzt::destroy_command_list(command_list);
}
void copy_image_to_mem(ze_image_handle_t input, lzt::ImagePNG32Bit &output) {
auto command_list = lzt::create_command_list();
EXPECT_EQ(ZE_RESULT_SUCCESS, zeCommandListAppendImageCopyToMemory(
command_list, output.raw_data(), input,
nullptr, nullptr, 0, nullptr));
lzt::append_barrier(command_list, nullptr, 0, nullptr);
lzt::close_command_list(command_list);
auto command_queue = lzt::create_command_queue();
lzt::execute_command_lists(command_queue, 1, &command_list, nullptr);
lzt::synchronize(command_queue, UINT64_MAX);
lzt::destroy_command_queue(command_queue);
lzt::destroy_command_list(command_list);
}
ze_image_handle_t create_ze_image(ze_context_handle_t context,
ze_device_handle_t dev,
ze_image_desc_t image_descriptor) {
ze_image_handle_t image = nullptr;
auto context_initial = context;
auto device_initial = dev;
ze_result_t result = zeImageCreate(context, dev, &image_descriptor, &image);
EXPECT_EQ(context, context_initial);
EXPECT_EQ(dev, device_initial);
EXPECT_TRUE((result == ZE_RESULT_SUCCESS) ||
(result == ZE_RESULT_ERROR_UNSUPPORTED_IMAGE_FORMAT));
if (result == ZE_RESULT_ERROR_UNSUPPORTED_IMAGE_FORMAT) {
print_image_descriptor_unsupported(image_descriptor);
}
if (result == ZE_RESULT_SUCCESS) {
EXPECT_NE(nullptr, image);
}
return image;
}
ze_image_handle_t create_ze_image(ze_device_handle_t dev,
ze_image_desc_t image_descriptor) {
return create_ze_image(lzt::get_default_context(), dev, image_descriptor);
}
ze_image_handle_t create_ze_image(ze_image_desc_t image_descriptor) {
return create_ze_image(lzt::get_default_context(),
lzt::zeDevice::get_instance()->get_device(),
image_descriptor);
}
ze_image_handle_t create_ze_image() {
ze_image_desc_t descriptor = {};
descriptor.stype = ZE_STRUCTURE_TYPE_IMAGE_DESC;
descriptor.pNext = nullptr;
return create_ze_image(descriptor);
}
void destroy_ze_image(ze_image_handle_t image) {
EXPECT_EQ(ZE_RESULT_SUCCESS, zeImageDestroy(image));
}
#define DEFAULT_WIDTH 128
#define DEFAULT_HEIGHT 128
const ze_image_desc_t zeImageCreateCommon::dflt_ze_image_desc = {
ZE_STRUCTURE_TYPE_IMAGE_DESC,
nullptr,
ZE_IMAGE_FLAG_KERNEL_WRITE,
ZE_IMAGE_TYPE_2D,
{ZE_IMAGE_FORMAT_LAYOUT_8_8_8_8, ZE_IMAGE_FORMAT_TYPE_UNORM,
ZE_IMAGE_FORMAT_SWIZZLE_R, ZE_IMAGE_FORMAT_SWIZZLE_G,
ZE_IMAGE_FORMAT_SWIZZLE_B, ZE_IMAGE_FORMAT_SWIZZLE_A},
DEFAULT_WIDTH,
DEFAULT_HEIGHT,
1,
0,
0};
zeImageCreateCommon::zeImageCreateCommon()
: dflt_host_image_(DEFAULT_WIDTH, DEFAULT_HEIGHT) {
dflt_device_image_ = create_ze_image(dflt_ze_image_desc);
dflt_device_image_2_ = create_ze_image(dflt_ze_image_desc);
write_image_data_pattern(dflt_host_image_, dflt_data_pattern);
}
zeImageCreateCommon::~zeImageCreateCommon() {
destroy_ze_image(dflt_device_image_);
destroy_ze_image(dflt_device_image_2_);
}
void print_image_descriptor_unsupported(const ze_image_desc_t descriptor) {
LOG_WARNING << "Unsupported Image Format";
LOG_WARNING << " TYPE = " << descriptor.type;
LOG_WARNING << " LAYOUT = " << descriptor.format.layout
<< " FORMAT_TYPE = " << descriptor.format.type;
}
void print_image_format_descriptor(const ze_image_format_t descriptor) {
LOG_DEBUG << " LAYOUT = " << descriptor.layout
<< " TYPE = " << descriptor.type << " X = " << descriptor.x
<< " Y = " << descriptor.y << " Z = " << descriptor.z
<< " w = " << descriptor.w;
}
void print_image_descriptor(const ze_image_desc_t descriptor) {
LOG_DEBUG << "STYPE= " << descriptor.stype
<< " FLAGS = " << descriptor.flags
<< " TYPE = " << descriptor.type;
print_image_format_descriptor(descriptor.format);
LOG_DEBUG << " WIDTH = " << descriptor.width
<< " HEIGHT = " << descriptor.height
<< " DEPTH = " << descriptor.depth
<< " ARRAYLEVELS = " << descriptor.arraylevels
<< " MIPLEVELS = " << descriptor.miplevels;
}
ze_image_properties_t
get_ze_image_properties(ze_image_desc_t image_descriptor) {
ze_image_properties_t image_properties = {ZE_STRUCTURE_TYPE_IMAGE_PROPERTIES};
EXPECT_EQ(ZE_RESULT_SUCCESS,
zeImageGetProperties(lzt::zeDevice::get_instance()->get_device(),
&image_descriptor, &image_properties));
return image_properties;
}
static inline uint32_t mask_and_shift(int8_t v, uint8_t m, size_t s) {
return static_cast<uint32_t>(v & m) << s;
}
static inline uint32_t make_pixel(int8_t r, uint8_t r_idx, int8_t g,
uint8_t g_idx, int8_t b, uint8_t b_idx,
int8_t a, uint8_t a_idx) {
return mask_and_shift(r, 0xff, r_idx) | mask_and_shift(g, 0xff, g_idx) |
mask_and_shift(b, 0xff, b_idx) | mask_and_shift(a, 0xff, a_idx);
}
static void clip_to_uint8_t(int8_t &sd, int8_t addvalue) {
int16_t sd16 = sd;
sd16 += addvalue;
if ((sd16 > 127) || (sd16 < -128)) {
sd = 0;
} else {
sd = static_cast<int8_t>(sd16);
}
}
enum RGBA_index {
X_IDX = 0,
Y_IDX = 8,
Z_IDX = 16,
W_IDX = 24,
UNKNOWN_IDX = 255
};
static inline uint8_t lookup_idx(ze_image_format_swizzle_t s,
const ze_image_format_t &fmt) {
if (s == fmt.x) {
return X_IDX;
} else if (s == fmt.y) {
return Y_IDX;
} else if (s == fmt.z) {
return Z_IDX;
} else if (s == fmt.w) {
return W_IDX;
} else {
return UNKNOWN_IDX;
}
}
static void write_image_data_pattern(lzt::ImagePNG32Bit &image, int8_t dp,
const ze_image_format_t &image_format,
int originX, int originY, int width,
int height) {
int8_t pixel_r = dp * 1;
uint8_t r_idx = lookup_idx(ZE_IMAGE_FORMAT_SWIZZLE_R, image_format);
int8_t pixel_g = dp * 2;
uint8_t g_idx = lookup_idx(ZE_IMAGE_FORMAT_SWIZZLE_G, image_format);
int8_t pixel_b = dp * 3;
uint8_t b_idx = lookup_idx(ZE_IMAGE_FORMAT_SWIZZLE_B, image_format);
int8_t pixel_a = dp * 4;
uint8_t a_idx = lookup_idx(ZE_IMAGE_FORMAT_SWIZZLE_A, image_format);
for (int y = originY; y < height; y++) {
for (int x = originX; x < width; x++) {
uint32_t pixel = make_pixel(pixel_r, r_idx, pixel_g, g_idx, pixel_b,
b_idx, pixel_a, a_idx);
image.set_pixel(x, y, pixel);
clip_to_uint8_t(pixel_r, dp * 1);
clip_to_uint8_t(pixel_g, dp * 2);
clip_to_uint8_t(pixel_b, dp * 3);
clip_to_uint8_t(pixel_a, dp * 4);
}
}
}
void write_image_data_pattern(lzt::ImagePNG32Bit &image, int8_t dp,
const ze_image_format_t &image_format) {
write_image_data_pattern(image, dp, image_format, 0, 0, image.width(),
image.height());
}
void write_image_data_pattern(lzt::ImagePNG32Bit &image, int8_t dp) {
ze_image_desc_t img_desc = zeImageCreateCommon::dflt_ze_image_desc;
write_image_data_pattern(image, dp, img_desc.format, 0, 0, image.width(),
image.height());
}
static inline uint32_t get_pixel(const uint32_t *image, int x, int y,
int row_width) {
return image[y * row_width + x];
}
int compare_data_pattern(const lzt::ImagePNG32Bit &imagepng1,
const lzt::ImagePNG32Bit &imagepng2, int origin1X,
int origin1Y, int width1, int height1, int origin2X,
int origin2Y, int width2, int height2) {
ze_image_desc_t img_desc = zeImageCreateCommon::dflt_ze_image_desc;
return compare_data_pattern(imagepng1, img_desc.format, imagepng2,
img_desc.format, origin1X, origin1Y, width1,
height1, origin2X, origin2Y, width2, height2);
}
static void decompose_pixel(uint32_t pixel, int8_t &r, uint8_t r_idx, int8_t &g,
uint8_t g_idx, int8_t &b, uint8_t b_idx, int8_t &a,
uint8_t a_idx) {
r = (pixel >> r_idx) & 0xff;
g = (pixel >> g_idx) & 0xff;
b = (pixel >> b_idx) & 0xff;
a = (pixel >> a_idx) & 0xff;
}
// Returns number of errors found, color order for both images are
// define in the image_format parameters:
int compare_data_pattern(const lzt::ImagePNG32Bit &imagepng1,
const ze_image_format_t &image1_format,
const lzt::ImagePNG32Bit &imagepng2,
const ze_image_format_t &image2_format, int origin1X,
int origin1Y, int width1, int height1, int origin2X,
int origin2Y, int width2, int height2) {
uint8_t r1_idx = lookup_idx(ZE_IMAGE_FORMAT_SWIZZLE_R, image1_format);
uint8_t g1_idx = lookup_idx(ZE_IMAGE_FORMAT_SWIZZLE_G, image1_format);
uint8_t b1_idx = lookup_idx(ZE_IMAGE_FORMAT_SWIZZLE_B, image1_format);
uint8_t a1_idx = lookup_idx(ZE_IMAGE_FORMAT_SWIZZLE_A, image1_format);
uint8_t r2_idx = lookup_idx(ZE_IMAGE_FORMAT_SWIZZLE_R, image2_format);
uint8_t g2_idx = lookup_idx(ZE_IMAGE_FORMAT_SWIZZLE_G, image2_format);
uint8_t b2_idx = lookup_idx(ZE_IMAGE_FORMAT_SWIZZLE_B, image2_format);
uint8_t a2_idx = lookup_idx(ZE_IMAGE_FORMAT_SWIZZLE_A, image2_format);
const bool must_decompose_colors = (r1_idx != r2_idx) || (g1_idx != g2_idx) ||
(b1_idx != b2_idx) || (a1_idx != a2_idx);
const uint32_t *image1 = imagepng1.raw_data();
const uint32_t *image2 = imagepng2.raw_data();
int errCnt = 0, successCnt = 0;
for (int y1 = origin1Y, y2 = origin2Y;
(y1 < (origin1Y + height1)) && (y2 < (origin2Y + height2)); y1++, y2++) {
for (int x1 = origin1X, x2 = origin2X;
(x1 < (origin1X + width1)) && (x2 < (origin2X + width2)); x1++, x2++) {
if (x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0)
continue;
uint32_t pixel1 = get_pixel(image1, x1, y1, width1);
uint32_t pixel2 = get_pixel(image2, x2, y2, width2);
bool correct;
if (must_decompose_colors) {
int8_t r1, g1, b1, a1;
int8_t r2, g2, b2, a2;
decompose_pixel(pixel1, r1, r1_idx, g1, g1_idx, b1, b1_idx, a1, a1_idx);
decompose_pixel(pixel2, r2, r2_idx, g2, g2_idx, b2, b2_idx, a2, a2_idx);
if ((r1 != r2) || (g1 != g2) || (b1 != b2) || (a1 != a2)) {
correct = false;
} else {
correct = true;
}
} else {
if (pixel1 != pixel2) {
correct = false;
} else {
correct = true;
}
}
if (!correct) {
LOG_DEBUG << "errCnt: " << errCnt << " successCnt: " << successCnt
<< " x1: " << x1 << " y1: " << y1 << " x2: " << x2
<< " y2: " << y2 << " pixel1: 0x" << std::hex << pixel1
<< " pixel2: 0x" << pixel2;
errCnt++;
} else {
successCnt++;
}
}
}
return errCnt;
}
int compare_data_pattern(
const lzt::ImagePNG32Bit &image, const ze_image_region_t *region,
const lzt::ImagePNG32Bit &expected_fg, // expected foreground
const lzt::ImagePNG32Bit &expected_bg) {
const ze_image_region_t full_image_region = {uint32_t(0),
uint32_t(0),
uint32_t(0),
uint32_t(image.width()),
uint32_t(image.height()),
uint32_t(1)};
if (region == nullptr) {
region = &full_image_region;
}
int errCnt = 0;
LOG_DEBUG << "region: originX: " << region->originX
<< " originY: " << region->originY
<< " originZ: " << region->originZ << " width: " << region->width
<< " height: " << region->height << std::endl;
for (unsigned row = 0; row < image.height(); row++) {
for (unsigned column = 0; column < image.width(); column++) {
uint32_t pixel = image.get_pixel(column, row);
const lzt::ImagePNG32Bit *expected_image;
LOG_DEBUG << "row: " << row << " and column: " << column
<< " corresponds to the ";
if ((row >= region->originY &&
row < (region->originY + region->height)) &&
(column >= region->originX &&
column < (region->originX + region->width))) {
// The pixel is expected to be in the foreground image:
LOG_DEBUG << "foreground." << std::endl;
expected_image = &expected_fg;
} else {
// The pixel is expected to be in the background image:
LOG_DEBUG << "background." << std::endl;
expected_image = &expected_bg;
}
if (row > expected_image->height() || column > expected_image->width()) {
LOG_DEBUG << "expected image does not have a pixel for row: " << row
<< " and column: " << column << std::endl;
errCnt++;
} else {
if (pixel != expected_image->get_pixel(column, row)) {
LOG_DEBUG << "image's pixel: " << pixel
<< " does not match expected image's pixel: "
<< expected_image->get_pixel(column, row)
<< " for row: " << row << " and column: " << column
<< std::endl;
errCnt++;
}
}
}
}
return errCnt;
}
bool check_unsupported(ze_result_t result) {
bool unsupported = false;
if (result != ZE_RESULT_SUCCESS) {
if (result == ZE_RESULT_ERROR_UNSUPPORTED_FEATURE ||
result == ZE_RESULT_ERROR_UNSUPPORTED_IMAGE_FORMAT ||
result == ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION ||
result == ZE_RESULT_ERROR_UNSUPPORTED_ALIGNMENT ||
result == ZE_RESULT_ERROR_UNSUPPORTED_SIZE) {
auto res_string = lzt::to_string(result);
std::size_t pos = res_string.find("UNSUPPORTED");
std::string unsupported_type_string = res_string.substr(pos + 12);
LOG_INFO << "[" << unsupported_type_string
<< " not supported on this device or driver]";
unsupported = true;
}
}
return unsupported;
}
}; // namespace level_zero_tests